Пример #1
0
        public async Task DownloadAsyncAlternativ(string downloadUrl, string programName)
        {
            InitDownloads();
            downloadUrl = "download/881.catrobat";
            programName = "SKYPASCAL";
            // optionally create a new cancellation token to be able to cancel every download individually
            try
            {
                Uri    downloadSource      = new Uri(ApplicationResourcesHelper.Get("POCEKTCODE_BASE_ADDRESS") + downloadUrl);
                string downloadDestination = Path.Combine(StorageConstants.TempProgramImportZipPath, programName + ApplicationResourcesHelper.Get("EXTENSION"));

                StorageFile destinationFile = await GetFileAsync(downloadDestination); // Mehtods copied from StorageWindowsShared

                BackgroundDownloader backgroundDownloader = new BackgroundDownloader();
                DownloadOperation    downloadOperation    = backgroundDownloader.CreateDownload(downloadSource, destinationFile);
                ManageDownloadsAsync(downloadOperation, true);
            }
            catch (HttpRequestException)
            {
                //return null;
            }
            catch (Exception)
            {
                //return null;
            }
        }
        private async void ShareStorageItemsHandler(DataTransferManager sender,
                                                    DataRequestedEventArgs e)
        {
            var request = e.Request;

            request.Data.Properties.Title       = AppResourcesHelper.Get("Export_FileTitle");
            request.Data.Properties.Description = ApplicationResourcesHelper.Get("CATROBAT_URL");
            var deferral = request.GetDeferral();

            try
            {
                var rootFolder  = ApplicationData.Current.LocalFolder;
                var fileToShare = await rootFolder.GetFileAsync(_pathToShareFile);

                var storageItems = new List <IStorageItem> {
                    fileToShare
                };
                request.Data.SetStorageItems(storageItems);
            }
            catch
            {
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
            }
            finally
            {
                deferral.Complete();
            }
        }
Пример #3
0
        public async Task <List <OnlineProgramHeader> > LoadOnlineProgramsAsync(
            string filterText, int offset, int count,
            CancellationToken taskCancellationToken)
        {
            using (var httpClient = new HttpClient())
            {
                //httpClient.BaseAddress = new Uri(ApplicationResourcesHelper.Get("API_BASE_ADDRESS);
                //testserver: use for uploads for TDD: https://catroid-test.catrob.at/pocketcode/api/
                //old: httpClient.BaseAddress = new Uri("https://pocketcode.org/api/");
                httpClient.BaseAddress = new Uri("https://share.catrob.at/pocketcode/api/");
                try
                {
                    HttpResponseMessage httpResponse = null;

                    if (filterText == "")
                    {
                        httpResponse = await httpClient.GetAsync(
                            String.Format(ApplicationResourcesHelper.Get("API_RECENT_PROJECTS"),
                                          count, offset), taskCancellationToken);
                    }
                    else
                    {
                        string encoded_filter_text = WebUtility.UrlEncode(filterText);
                        httpResponse = await httpClient.GetAsync(String.Format(
                                                                     ApplicationResourcesHelper.Get("API_SEARCH_PROJECTS"), encoded_filter_text,
                                                                     count, offset), taskCancellationToken);
                    }
                    httpResponse.EnsureSuccessStatusCode();

                    string jsonResult = await httpResponse.Content.ReadAsStringAsync();

                    OnlineProgramOverview recentPrograms = null;

                    recentPrograms = await Task.Run(() => JsonConvert.DeserializeObject <OnlineProgramOverview>(jsonResult));

                    return(recentPrograms.CatrobatProjects);
                }
                catch (HttpRequestException)
                {
                    return(null);
                }
                catch (Newtonsoft.Json.JsonSerializationException)
                {
                    return(null);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
Пример #4
0
        public async Task <JSONStatusResponse> LoginOrRegisterAsync(string username, string password, string userEmail,
                                                                    string language = "en", string country = "AT")
        {
            var parameters = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_REG_USERNAME"), ((username == null) ? "" : username)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_REG_PASSWORD"), ((password == null) ? "" : password)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_REG_EMAIL"), ((userEmail == null) ? "" : userEmail)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_REG_COUNTRY"), ((country == null) ? "" : country)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_LANGUAGE"), ((language == null) ? "" : language))
            };

            HttpContent postParameters = new FormUrlEncodedContent(parameters);

            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(ApplicationResourcesHelper.Get("API_BASE_ADDRESS"));
                JSONStatusResponse statusResponse = null;
                try
                {
                    HttpResponseMessage httpResponse = await httpClient.PostAsync(ApplicationResourcesHelper.Get("API_LOGIN_REGISTER"), postParameters);

                    httpResponse.EnsureSuccessStatusCode();

                    string jsonResult = await httpResponse.Content.ReadAsStringAsync();

                    statusResponse = JsonConvert.DeserializeObject <JSONStatusResponse>(jsonResult);
                }
                catch (HttpRequestException)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.HTTPRequestFailed;
                }
                catch (Newtonsoft.Json.JsonSerializationException)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.JSONSerializationFailed;
                }
                catch (Exception)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.UnknownError;
                }
                return(statusResponse);
            }
        }
Пример #5
0
        public async Task <JSONStatusResponse> ChangePasswordAsync(string newPassword, string newPasswortRepeated, string language = "en")
        {
            var parameters = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_HASH"), ((_recoveryHash == null) ? "" : _recoveryHash)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_NEW_PWD"), ((newPassword == null) ? "" : newPassword)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_NEW_PWD_REPEAT"), ((newPasswortRepeated == null) ? "" : newPasswortRepeated)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_LANGUAGE"), ((language == null) ? "" : language))
            };

            HttpContent postParameters = new FormUrlEncodedContent(parameters);

            using (var httpClient = new HttpClient())
            {
                //httpClient.BaseAddress = new Uri(ApplicationResourcesHelper.Get("POCEKTCODE_BASE_ADDRESS);
                httpClient.BaseAddress = new Uri("https://catroid-test.catrob.at");
                JSONStatusResponse statusResponse = null;
                try
                {
                    HttpResponseMessage httpResponse = await httpClient.PostAsync(ApplicationResourcesHelper.Get("CATROWEB_CHANGE_PWD"), postParameters);

                    httpResponse.EnsureSuccessStatusCode();

                    string jsonResult = await httpResponse.Content.ReadAsStringAsync();

                    statusResponse = JsonConvert.DeserializeObject <JSONStatusResponse>(jsonResult);
                }
                catch (HttpRequestException)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.HTTPRequestFailed;
                }
                catch (Newtonsoft.Json.JsonSerializationException)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.JSONSerializationFailed;
                }
                catch (Exception)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.UnknownError;
                }
                return(statusResponse);
            }
        }
Пример #6
0
        public async Task <JSONStatusResponse> ReportAsInappropriateAsync(string programId, string flagReason, string language = "en")
        {
            var parameters = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_PROJECTID"), ((programId == null) ? "" : programId)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_FLAG_REASON"), ((flagReason == null) ? "" : flagReason)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_LANGUAGE"), ((language == null) ? "" : language))
            };

            HttpContent postParameters = new FormUrlEncodedContent(parameters);

            using (var httpClient = new HttpClient())
            {
                //httpClient.BaseAddress = new Uri(ApplicationResourcesHelper.Get("POCEKTCODE_BASE_ADDRESS);
                httpClient.BaseAddress = new Uri("https://catroid-test.catrob.at");
                JSONStatusResponse statusResponse = null;
                try
                {
                    HttpResponseMessage httpResponse = await httpClient.PostAsync(ApplicationResourcesHelper.Get("CATROWEB_REPORT_AS_INAPPROPRIATE"), postParameters);

                    httpResponse.EnsureSuccessStatusCode();

                    string jsonResult = await httpResponse.Content.ReadAsStringAsync();

                    statusResponse = JsonConvert.DeserializeObject <JSONStatusResponse>(jsonResult);
                }
                catch (HttpRequestException)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.HTTPRequestFailed;
                }
                catch (Newtonsoft.Json.JsonSerializationException)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.JSONSerializationFailed;
                }
                catch (Exception)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.UnknownError;
                }
                return(statusResponse);
            }
        }
Пример #7
0
        public async Task <JSONStatusResponse> CheckTokenAsync(string username, string token, string language = "en")
        {
            var parameters = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_USERNAME"), ((username == null) ? "" : username)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_TOKEN"), ((token == null) ? "" : token)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_LANGUAGE"), ((language == null) ? "" : language))
            };

            HttpContent postParameters = new FormUrlEncodedContent(parameters);

            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(ApplicationResourcesHelper.Get("API_BASE_ADDRESS"));
                JSONStatusResponse statusResponse = null;
                try
                {
                    HttpResponseMessage httpResponse = await httpClient.PostAsync(ApplicationResourcesHelper.Get("API_CHECK_TOKEN"), postParameters);

                    httpResponse.EnsureSuccessStatusCode();

                    string jsonResult = await httpResponse.Content.ReadAsStringAsync();

                    statusResponse = JsonConvert.DeserializeObject <JSONStatusResponse>(jsonResult);
                }
                catch (HttpRequestException)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.HTTPRequestFailed;
                }
                catch (Newtonsoft.Json.JsonSerializationException)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.JSONSerializationFailed;
                }
                catch (Exception)
                {
                    statusResponse            = new JSONStatusResponse();
                    statusResponse.statusCode = StatusCodes.UnknownError;
                }
                return(statusResponse);
            }
        }
Пример #8
0
        //old simple portable downloader
        public async Task <Stream> DownloadAsync(string downloadUrl, string programName, CancellationToken taskCancellationToken)
        {
            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(ApplicationResourcesHelper.Get("POCEKTCODE_BASE_ADDRESS"));
                try
                {
                    var httpResponse = await httpClient.GetAsync(downloadUrl, HttpCompletionOption.ResponseContentRead, taskCancellationToken);

                    httpResponse.EnsureSuccessStatusCode();

                    return(await httpResponse.Content.ReadAsStreamAsync());
                }
                catch (HttpRequestException)
                {
                    return(null);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
        }
Пример #9
0
 private void TouAction()
 {
     ServiceLocator.NavigationService.NavigateToWebPage(
         ApplicationResourcesHelper.Get("CATROBAT_TOU_URL"));
 }
Пример #10
0
 private void LicenseAction()
 {
     ServiceLocator.NavigationService.NavigateToWebPage(
         ApplicationResourcesHelper.Get("CATROBAT_LICENSES_URL"));
 }
Пример #11
0
        public async Task <JSONStatusResponse> UploadProgramAsync(string programTitle,
                                                                  string username, string token, CancellationToken taskCancellationToken, string language = "en")
        {
            var parameters = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_USERNAME"), ((username == null) ? "" : username)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_TOKEN"), ((token == null) ? "" : token)),
                new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_LANGUAGE"), ((language == null) ? "" : language))
            };

            using (var postParameters = new MultipartFormDataContent())
            {
                using (var storage = StorageSystem.GetStorage())
                {
                    JSONStatusResponse statusResponse = null;
                    try
                    {
                        _uploadCounter++;
                        byte[] programData;
                        using (var stream = await storage.OpenFileAsync(Path.Combine(StorageConstants.TempProgramExportZipPath, programTitle + ApplicationResourcesHelper.Get("EXTENSION")),
                                                                        StorageFileMode.Open, StorageFileAccess.Read))
                        {
                            var memoryStream = new MemoryStream();
                            await stream.CopyToAsync(memoryStream);

                            programData = memoryStream.ToArray();
                        }

                        parameters.Add(new KeyValuePair <string, string>(ApplicationResourcesHelper.Get("API_PARAM_CHECKSUM"), UtilTokenHelper.ToHex(MD5Core.GetHash(programData))));

                        // store parameters as MultipartFormDataContent
                        StringContent content = null;
                        foreach (var keyValuePair in parameters)
                        {
                            content = new StringContent(keyValuePair.Value);
                            content.Headers.Remove("Content-Type");
                            postParameters.Add(content, String.Format("\"{0}\"", keyValuePair.Key));
                        }

                        ByteArrayContent fileContent = new ByteArrayContent(programData);

                        fileContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/zip");
                        postParameters.Add(fileContent, String.Format("\"{0}\"", ApplicationResourcesHelper.Get("API_PARAM_UPLOAD")), String.Format("\"{0}\"", programTitle + ApplicationResourcesHelper.Get("EXTENSION")));

                        using (var httpClient = new HttpClient())
                        {
                            httpClient.BaseAddress = new Uri(ApplicationResourcesHelper.Get("API_BASE_ADDRESS"));
                            HttpResponseMessage httpResponse = await httpClient.PostAsync(ApplicationResourcesHelper.Get("API_UPLOAD"), postParameters, taskCancellationToken);

                            httpResponse.EnsureSuccessStatusCode();
                            string jsonResult = await httpResponse.Content.ReadAsStringAsync();

                            statusResponse = JsonConvert.DeserializeObject <JSONStatusResponse>(jsonResult);
                        }
                    }
                    catch (HttpRequestException)
                    {
                        statusResponse            = new JSONStatusResponse();
                        statusResponse.statusCode = StatusCodes.HTTPRequestFailed;
                    }
                    catch (Newtonsoft.Json.JsonSerializationException)
                    {
                        statusResponse            = new JSONStatusResponse();
                        statusResponse.statusCode = StatusCodes.JSONSerializationFailed;
                    }
                    catch (Exception)
                    {
                        statusResponse            = new JSONStatusResponse();
                        statusResponse.statusCode = StatusCodes.UnknownError;
                    }
                    finally
                    {
                        _uploadCounter--;
                    }
                    return(statusResponse);
                }
            }
        }