Exemplo n.º 1
0
        private async void GetUserInformation()
        {
            var client = await ClientService.GetClient();

            if (client == null)
            {
                return;
            }

            var username = SettingsService.Default.Value.LocalSettings.Username;

            if (string.IsNullOrEmpty(username))
            {
                return;
            }

            try {
                User = await client.GetUserAttributes(username);

                var converter = new BytesToHumanReadableConverter();
                QuotaUsedOfTotalString = LocalizationService.Instance.GetString(
                    "QuotaUsedOfTotal",
                    converter.Convert(User.Quota.Used, typeof(string), null, CultureInfo.CurrentCulture.ToString()),
                    converter.Convert(User.Quota.Total, typeof(string), null, CultureInfo.CurrentCulture.ToString())
                    );

                switch (SettingsService.Default.Value.LocalSettings.PreviewImageDownloadMode)
                {
                case PreviewImageDownloadMode.Always:
                    UserAvatarUrl = await client.GetUserAvatarUrl(username, 120);

                    break;

                case PreviewImageDownloadMode.WiFiOnly:
                    var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
                    // connectionProfile can be null (e.g. airplane mode)
                    if (connectionProfile != null && connectionProfile.IsWlanConnectionProfile)
                    {
                        UserAvatarUrl = await client.GetUserAvatarUrl(username, 120);
                    }
                    break;

                case PreviewImageDownloadMode.Never:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (ResponseError e)
            {
                ResponseErrorHandlerService.HandleException(e);
            }
        }
        private async Task <bool> CheckServerStatus()
        {
            try
            {
                var status = await NextcloudClient.NextcloudClient.GetServerStatus(ServerAddress, SettingsService.Instance.LocalSettings.IgnoreServerCertificateErrors);

                if (status == null)
                {
                    await ShowServerAddressNotFoundMessage();

                    return(false);
                }
                if (!status.Installed)
                {
                    var dialog = new ContentDialog
                    {
                        Title   = _resourceLoader.GetString("AnErrorHasOccurred"),
                        Content = new TextBlock
                        {
                            Text         = _resourceLoader.GetString("Auth_Unauthorized"),
                            TextWrapping = TextWrapping.WrapWholeWords,
                            Margin       = new Thickness(0, 20, 0, 0)
                        },
                        PrimaryButtonText = _resourceLoader.GetString("OK")
                    };
                    await _dialogService.ShowAsync(dialog);

                    return(false);
                }
                if (status.Maintenance)
                {
                    var dialog = new ContentDialog
                    {
                        Title   = _resourceLoader.GetString("AnErrorHasOccurred"),
                        Content = new TextBlock
                        {
                            Text         = _resourceLoader.GetString("Auth_MaintenanceEnabled"),
                            TextWrapping = TextWrapping.WrapWholeWords,
                            Margin       = new Thickness(0, 20, 0, 0)
                        },
                        PrimaryButtonText = _resourceLoader.GetString("OK")
                    };
                    await _dialogService.ShowAsync(dialog);

                    return(false);
                }
            }
            catch (ResponseError e)
            {
                ResponseErrorHandlerService.HandleException(e);
                return(false);
            }
            return(true);
        }
Exemplo n.º 3
0
        private async void DownloadPreviewImages()
        {
            var client = await ClientService.GetClient();

            if (client == null)
            {
                return;
            }

            switch (SettingsService.Default.Value.LocalSettings.PreviewImageDownloadMode)
            {
            case PreviewImageDownloadMode.Always:
                break;

            case PreviewImageDownloadMode.WiFiOnly:
                var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
                // connectionProfile can be null (e.g. airplane mode)
                if (connectionProfile == null || !connectionProfile.IsWlanConnectionProfile)
                {
                    return;
                }
                break;

            case PreviewImageDownloadMode.Never:
                return;

            default:
                throw new ArgumentOutOfRangeException();
            }

            try
            {
                Stream stream = null;
                try
                {
                    stream = await client.GetThumbnail(ResourceInfo, 120, 120);
                }
                catch (ResponseError e)
                {
                    ResponseErrorHandlerService.HandleException(e);
                }

                if (stream == null)
                {
                    return;
                }
                var bitmap = new BitmapImage();
                using (var memStream = new MemoryStream())
                {
                    await stream.CopyToAsync(memStream);

                    memStream.Position = 0;
                    bitmap.SetSource(memStream.AsRandomAccessStream());
                }
                Thumbnail = bitmap;
            }
            catch (ResponseError)
            {
                Thumbnail = new BitmapImage
                {
                    UriSource = new Uri("ms-appx:///Assets/Images/ThumbnailNotFound.png")
                };
            }
        }
Exemplo n.º 4
0
        private async void OnUnhandledException(object sender, UnhandledExceptionEventArgs args)
        {
            var exceptionStackTrace = string.Empty;

            try
            {
                exceptionStackTrace = args.Exception.StackTrace + "";
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch { }
            var exceptionMessage   = args.Message;
            var exceptionType      = string.Empty;
            var innerExceptionType = string.Empty;
            var exceptionHashCode  = string.Empty;

            if (args.Exception != null)
            {
                // Tasks will throw a canceled exception if they get canceled
                // We don't care, but avoid closing the app
                if (args.Exception.GetType() == typeof(TaskCanceledException))
                {
                    args.Handled = true;
                    return;
                }
                if (args.Exception.GetType() == typeof(OperationCanceledException))
                {
                    args.Handled = true;
                    return;
                }
                if (args.Exception.GetType() == typeof(FileNotFoundException))
                {
                    args.Handled = true;
                    return;
                }
                // Temporary Workaround for WP10
                if (args.Exception.GetType() == typeof(ArgumentException))
                {
                    args.Handled = true;
                    return;
                }
                if (args.Exception.GetType() == typeof(ResponseError))
                {
                    args.Handled = true;
                    ResponseErrorHandlerService.HandleException((ResponseError)args.Exception);
                    return;
                }
                // 0x8000000B, E_BOUNDS, System.Exception, OutOfBoundsException
                if ((uint)args.Exception.HResult == 0x80004004)
                {
                    args.Handled = true;
                    return;
                }
                // 0x80072EE7, ERROR_WINHTTP_NAME_NOT_RESOLVED, The server name or address could not be resolved
                if ((uint)args.Exception.HResult == 0x80072EE7)
                {
                    args.Handled = true;
                    var resourceLoader = Container.Resolve <IResourceLoader>();
                    var dialogService  = Container.Resolve <DialogService>();
                    var dialog         = new ContentDialog
                    {
                        Title   = resourceLoader.GetString("AnErrorHasOccurred"),
                        Content = new TextBlock
                        {
                            Text         = resourceLoader.GetString("ServerNameOrAddressCouldNotBeResolved"),
                            TextWrapping = TextWrapping.WrapWholeWords,
                            Margin       = new Thickness(0, 20, 0, 0)
                        },
                        PrimaryButtonText = resourceLoader.GetString("OK")
                    };
                    await dialogService.ShowAsync(dialog);

                    return;
                }
                exceptionType = args.Exception.GetType().ToString();
                if (args.Exception.InnerException != null)
                {
                    innerExceptionType = args.Exception.InnerException.GetType().ToString();
                }
                exceptionHashCode = string.IsNullOrEmpty(exceptionStackTrace)
                    ? args.Exception.GetHashCode().ToString()
                    : exceptionStackTrace.GetHashCode().ToString();
            }
            if (args.Handled)
            {
                return;
            }
            args.Handled = true;
            await
            ExceptionReportService.Handle(exceptionType, exceptionMessage, exceptionStackTrace,
                                          innerExceptionType, exceptionHashCode);
        }
        public override async void OnNavigatedTo(NavigatedToEventArgs e, Dictionary <string, object> viewModelState)
        {
            base.OnNavigatedTo(e, viewModelState);

            var client = await ClientService.GetClient();

            if (client == null)
            {
                return;
            }

            _cts = new CancellationTokenSource();

            var parameters   = SingleFileDownloadPageParameters.Deserialize(e.Parameter);
            var resourceInfo = parameters?.ResourceInfo;

            if (resourceInfo == null)
            {
                return;
            }

            if (resourceInfo.ContentType == "dav/directory")
            {
                ResourceInfo = new ResourceInfo
                {
                    Name        = resourceInfo.Name + ".zip",
                    ContentType = "application/zip"
                };
            }
            else
            {
                ResourceInfo = resourceInfo;
            }

            var savePicker = new FileSavePicker();

            savePicker.FileTypeChoices.Add(ResourceInfo.ContentType, new List <string> {
                Path.GetExtension(ResourceInfo.Name)
            });
            savePicker.SuggestedFileName = ResourceInfo.Name;

            var localFile = await savePicker.PickSaveFileAsync();

            if (localFile == null)
            {
                return;
            }

            _currentFile = localFile;

            // Prevent updates to the remote version of the file until
            // we finish making changes and call CompleteUpdatesAsync.
            CachedFileManager.DeferUpdates(localFile);

            try
            {
                IProgress <HttpProgress>        progress = new Progress <HttpProgress>(ProgressHandler);
                Windows.Storage.Streams.IBuffer buffer;
                switch (resourceInfo.ContentType)
                {
                case "dav/directory":
                    buffer = await client.DownloadDirectoryAsZip(ResourceInfo.Path, _cts, progress);

                    break;

                default:
                    buffer = await client.Download(ResourceInfo.Path + "/" + ResourceInfo.Name, _cts, progress);

                    break;
                }
                await FileIO.WriteBufferAsync(localFile, buffer);
            }
            catch (ResponseError e2)
            {
                ResponseErrorHandlerService.HandleException(e2);
            }

            // Let Windows know that we're finished changing the file so
            // the other app can update the remote version of the file.
            // Completing updates may require Windows to ask for user input.
            var status = await CachedFileManager.CompleteUpdatesAsync(localFile);

            if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
            {
                //this.textBlock.Text = "Path " + file.Name + " was saved.";
            }
            else
            {
                //this.textBlock.Text = "Path " + file.Name + " couldn't be saved.";
            }

            _navigationService.GoBack();
        }
Exemplo n.º 6
0
        public override async void OnNavigatedTo(NavigatedToEventArgs e, Dictionary <string, object> viewModelState)
        {
            base.OnNavigatedTo(e, viewModelState);

            var client = ClientService.GetClient();

            if (client == null)
            {
                return;
            }

            _cts = new CancellationTokenSource();
            var parameters   = FileUploadPageParameters.Deserialize(e.Parameter);
            var resourceInfo = parameters?.ResourceInfo;

            if (resourceInfo == null)
            {
                return;
            }
            ResourceInfo              = resourceInfo;
            SuggestedStartLocation    = parameters.PickerLocationId;
            UploadingFilesTitle       = null;
            UploadingFileProgressText = null;
            var i = 0;

            var openPicker = new FileOpenPicker
            {
                SuggestedStartLocation = SuggestedStartLocation
            };

            openPicker.FileTypeFilter.Add("*");
            var storageFiles = await openPicker.PickMultipleFilesAsync();

            foreach (var localFile in storageFiles)
            {
                _currentFile = localFile;

                // Prevent updates to the remote version of the file until
                // we finish making changes and call CompleteUpdatesAsync.
                CachedFileManager.DeferUpdates(localFile);

                if (storageFiles.Count > 1)
                {
                    UploadingFilesTitle = string.Format(_resourceLoader.GetString("UploadingFiles"), ++i, storageFiles.Count);
                }

                try
                {
                    var properties = await localFile.GetBasicPropertiesAsync();

                    BytesTotal = (long)properties.Size;

                    var stream = await localFile.OpenAsync(FileAccessMode.ReadWrite);

                    IProgress <HttpProgress> progress = new Progress <HttpProgress>(ProgressHandler);
                    await client.Upload(ResourceInfo.Path + localFile.Name, stream, localFile.ContentType, _cts, progress);
                }
                catch (ResponseError e2)
                {
                    ResponseErrorHandlerService.HandleException(e2);
                }

                // Let Windows know that we're finished changing the file so
                // the other app can update the remote version of the file.
                // Completing updates may require Windows to ask for user input.
                await CachedFileManager.CompleteUpdatesAsync(localFile);

                UploadingFileProgressText = null;
            }
            _navigationService.GoBack();
        }
Exemplo n.º 7
0
        private async Task Download(ResourceInfo resourceInfo, NextcloudClient.NextcloudClient client, StorageFolder folder)
        {
            if (resourceInfo.ContentType == "dav/directory")
            {
                ResourceInfo = new ResourceInfo
                {
                    Name        = resourceInfo.Name + ".zip",
                    ContentType = "application/zip",
                    Path        = resourceInfo.Path
                };
            }
            else
            {
                ResourceInfo = resourceInfo;
            }

            var savePicker = new FileSavePicker();

            savePicker.FileTypeChoices.Add(ResourceInfo.ContentType, new List <string> {
                Path.GetExtension(ResourceInfo.Name)
            });
            savePicker.SuggestedFileName = ResourceInfo.Name;

            StorageFile localFile;

            if (folder != null)
            {
                try
                {
                    localFile = await folder.CreateFileAsync(
                        savePicker.SuggestedFileName,
                        CreationCollisionOption.GenerateUniqueName);
                }
                catch (FileNotFoundException)
                {
                    //this.textBlock.Text = "Error " + ex;
                    return;
                }
            }
            else
            {
                localFile = await savePicker.PickSaveFileAsync();
            }
            if (localFile == null)
            {
                return;
            }

            _currentFile = localFile;

            // Prevent updates to the remote version of the file until
            // we finish making changes and call CompleteUpdatesAsync.
            CachedFileManager.DeferUpdates(localFile);

            try
            {
                IProgress <WebDavProgress> progress = new Progress <WebDavProgress>(ProgressHandler);

                using (var randomAccessStream = await localFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var targetStream = randomAccessStream.AsStreamForWrite();

                    switch (resourceInfo.ContentType)
                    {
                    case "dav/directory":
                        await client.DownloadDirectoryAsZip(ResourceInfo.Path, targetStream, progress, _cts.Token);

                        break;

                    default:
                        await client.Download(ResourceInfo.Path + "/" + ResourceInfo.Name, targetStream, progress, _cts.Token);

                        break;
                    }
                }
            }
            catch (ResponseError e2)
            {
                ResponseErrorHandlerService.HandleException(e2);
            }

            // Let Windows know that we're finished changing the file so
            // the other app can update the remote version of the file.
            // Completing updates may require Windows to ask for user input.
            var status = await CachedFileManager.CompleteUpdatesAsync(localFile);

            if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
            {
                //this.textBlock.Text = "Path " + file.Name + " was saved.";
            }
            else
            {
                //this.textBlock.Text = "Path " + file.Name + " couldn't be saved.";
            }
        }