/// <summary> /// Refresh preview (if needed). Dependends on current environment and changed ETag. /// The preview is valid for remote and local view. /// First call will always lead to preview. /// /// /Files/LocalPreview/Server/Path-To-File/file.preview.ext /// /Files/LocalPreview/Server/Path-To-File/file.etag /// </summary> /// <param name="file"></param> public void RefreshPreview(File file) { _file = file; bool createEtag = false; string path = "Files/LocalPreview/" + _info.Hostname + "/" + _file.FilePath + ".etag"; var isf = App.DataContext.Storage; { if (isf.FileExists(path)) { var reader = new System.IO.StreamReader(isf.OpenFile(path, System.IO.FileMode.Open)); if (reader.ReadToEnd() == _file.ETag) { reader.Close(); return; } reader.Close(); } else { createEtag = true; } } // create main type if (_enabled) { switch (_file.FileType.Split('/').First()) { case "text": TextPreview(); break; case "image": ImagePreview(); break; case "application": // try PDF content fetch TextContentPreview(); break; default: PreviewIcon(file.IsDirectory); break; } } else { PreviewIcon(); createEtag = false; } if (createEtag) { var storagePath = System.IO.Path.GetDirectoryName(path); if (!isf.DirectoryExists(storagePath)) isf.CreateDirectory(storagePath); var stream = isf.CreateFile(path); System.IO.StreamWriter writer = new System.IO.StreamWriter(stream); writer.Write(_file.ETag); writer.Close(); } }
/// <summary> /// Try to display contents of the file. /// The action can fail depending on the file type and size, then the default icon view will be generated. /// Must be created an refreshed linear (no async!) as it would raise I/O-Exceptions. /// /// </summary> /// <param name="info">Associated Account info</param> /// <param name="file">Desired file</param> /// <param name="preview">If the current environment does not allow previews (remote view, no WLAN,) set this to false</param> public FileMultiTileViewControl(Account info, File file, bool preview) { InitializeComponent(); _enabled = preview; _info = info; _file = file; this.FileName.Text = file.FileName; RefreshPreview(file); }
private async Task OpenItem(File item) { if (item.IsDirectory) { await FetchStructure(item.FilePath); } else { //todo: win8 file+uri associations callers //todo: open file } }
private async Task QueryStructure() { var dav = new WebDAVClient(_workingAccount.GetUri(), await App.AccountService.GetCredentials(_workingAccount)); try { var entries = await dav.GetEntries(_workingPath, true); if (entries.Count == 0) throw new Exception("No entries found"); bool _firstItem = false; // display all items linear // we cannot wait till an item is displayed, instead for a fluid // behaviour we should calculate fadeIn-delays. int delayStart = 0; int delayStep = 50; // ms foreach (var item in entries) { File fileItem = new File() { FileName = item.FileName, FilePath = item.FilePath.ToString(), FileSize = item.Size, FileType = item.MimeType, FileCreated = item.Created, FileLastModified = item.LastModified, IsDirectory = item.IsDirectory }; bool display = true; switch (_views[_viewIndex]) { case "detail": if (!_firstItem) { _firstItem = true; // Root if (fileItem.IsDirectory) { if (item.FilePath.ToString() == _workingAccount.WebDAVPath) { // cannot go up further display = false; } else { fileItem.IsRootItem = true; fileItem.FilePath = fileItem.FileParentPath; } } } if (display) { FileDetailViewControl detailControl = new FileDetailViewControl() { DataContext = fileItem, Opacity = 0, Background = new SolidColorBrush() { Color = Colors.Transparent }, }; DetailList.Items.Add(detailControl); detailControl.Delay(delayStart, () => { detailControl.FadeIn(100); }); delayStart += delayStep; } break; case "tile": if (!_firstItem) { _firstItem = true; // Root if (fileItem.IsDirectory) { if (item.FilePath.ToString() == _workingAccount.WebDAVPath) { // cannot go up further display = false; } else { fileItem.IsRootItem = true; fileItem.FilePath = fileItem.FileParentPath; } } } if (display) { FileMultiTileViewControl multiControl = new FileMultiTileViewControl(_workingAccount, fileItem, true) { Width = 200, Height = 200, Opacity = 0, Margin = new Thickness(0, 0, 10, 10), }; multiControl.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(TileListSelectionChanged); // sometimes the exception "wrong parameter" is thrown - but why??? TileView.Children.Add(multiControl); multiControl.Delay(delayStart, () => { multiControl.FadeIn(100); }); } break; } } progress.IsVisible = false; } catch (Exception ex) { progress.IsVisible = false; var webException = ex as WebException; var webResponse = webException != null ? webException.Response as HttpWebResponse : null; if (webException != null && webException.Status == WebExceptionStatus.ProtocolError && webResponse != null && webResponse.StatusCode == HttpStatusCode.Unauthorized) { MessageBox.Show("FetchFile_Unauthorized".Translate(), "Error_Caption".Translate(), MessageBoxButton.OK); } else { MessageBox.Show("FetchFile_Unexpected_Result".Translate(), "Error_Caption".Translate(), MessageBoxButton.OK); } } }
private void FetchStructureComplete(DAVRequestResult result, object userObj) { if (result.Status == ServerStatus.MultiStatus && !result.Request.ErrorOccured && result.Items.Count > 0) { bool _firstItem = false; // display all items linear // we cannot wait till an item is displayed, instead for a fluid // behaviour we should calculate fadeIn-delays. int delayStart = 0; int delayStep = 50; // ms foreach (DAVRequestResult.Item item in result.Items) { File fileItem = new File() { FileName = item.LocalReference, FilePath = item.Reference, FileSize = item.ContentLength, FileType = item.ContentType, FileCreated = item.CreationDate, FileLastModified = item.LastModified, IsDirectory = item.ResourceType == ResourceType.Collection }; bool display = true; Dispatcher.BeginInvoke(() => { switch (_views[_viewIndex]) { case "detail": if (!_firstItem) { _firstItem = true; // Root if (fileItem.IsDirectory) { if (item.Reference == _workingAccount.WebDAVPath) { // cannot go up further display = false; } else { fileItem.IsRootItem = true; fileItem.FilePath = fileItem.FileParentPath; } } } if (display) { FileDetailViewControl detailControl = new FileDetailViewControl() { DataContext = fileItem, Opacity = 0, Background = new SolidColorBrush() { Color = Colors.Transparent }, }; DetailList.Items.Add(detailControl); detailControl.Delay(delayStart, () => { detailControl.FadeIn(100); }); delayStart += delayStep; } break; case "tile": if (!_firstItem) { _firstItem = true; // Root if (fileItem.IsDirectory) { if (item.Reference == _workingAccount.WebDAVPath) { // cannot go up further display = false; } else { fileItem.IsRootItem = true; fileItem.FilePath = fileItem.FileParentPath; } } } if (display) { FileMultiTileViewControl multiControl = new FileMultiTileViewControl(_workingAccount, fileItem, true) { Width = 200, Height = 200, Opacity = 0, Margin = new Thickness(0, 0, 10, 10), }; multiControl.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(TileListSelectionChanged); // sometimes the exception "wrong parameter" is thrown - but why??? TileView.Children.Add(multiControl); multiControl.Delay(delayStart, () => { multiControl.FadeIn(100); }); } break; } }); } Dispatcher.BeginInvoke(() => { progress.IsVisible = false; }); } else { Dispatcher.BeginInvoke(() => { progress.IsVisible = false; if (result.Status == ServerStatus.Unauthorized) { MessageBox.Show("FetchFile_Unauthorized".Translate(), "Error_Caption".Translate(), MessageBoxButton.OK); } else { MessageBox.Show("FetchFile_Unexpected_Result".Translate(), "Error_Caption".Translate(), MessageBoxButton.OK); } }); } }