async Task <Document> LoadFile(FilePath fileName, ProgressMonitor monitor, DocumentControllerDescription binding, WorkspaceObject project, FileOpenInformation fileInfo) { // Make sure composition manager is ready since ScrollToRequestedCaretLocation will use it await Runtime.GetService <CompositionManager> (); string mimeType = desktopService.GetMimeTypeForUri(fileName); var fileDescriptor = new FileDescriptor(fileName, mimeType, project); try { Counters.OpenDocumentTimer.Trace("Creating content"); DocumentController controller; try { fileInfo.DocumentControllerDescription = binding; controller = fileInfo.DocumentController = await binding.CreateController(fileDescriptor); } catch (InvalidEncodingException iex) { monitor.ReportError(GettextCatalog.GetString("The file '{0}' could not opened. {1}", fileName, iex.Message), null); return(null); } catch (OverflowException) { monitor.ReportError(GettextCatalog.GetString("The file '{0}' could not opened. File too large.", fileName), null); return(null); } if (controller == null) { monitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), null); return(null); } Counters.OpenDocumentTimer.Trace("Loading file"); try { await controller.Initialize(fileDescriptor, GetStoredMemento (fileName)); controller.OriginalContentName = fileInfo.OriginalFileName; if (fileInfo.Owner != null) { controller.Owner = fileInfo.Owner; } } catch (InvalidEncodingException iex) { monitor.ReportError(GettextCatalog.GetString("The file '{0}' could not opened. {1}", fileName, iex.Message), iex); return(null); } catch (OverflowException) { monitor.ReportError(GettextCatalog.GetString("The file '{0}' could not opened. File too large.", fileName), null); return(null); } } catch (Exception ex) { monitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), ex); return(null); } Counters.OpenDocumentTimer.Trace("Showing view"); var doc = await ShowView(fileInfo); ScrollToRequestedCaretLocation(doc, fileInfo); return(doc); }
public override Task <DocumentController> CreateController(ModelDescriptor modelDescriptor, DocumentControllerDescription controllerDescription) { throw new NotImplementedException(); }
async Task <(bool Success, Document Content)> RealOpenFile(ProgressMonitor monitor, FileOpenInformation openFileInfo) { FilePath fileName; await InitDesktopService(); Counters.OpenDocumentTimer.Trace("Checking file"); string origName = openFileInfo.FileName; if (origName == null) { monitor.ReportError(GettextCatalog.GetString("Invalid file name"), null); return(false, null); } fileName = openFileInfo.FileName; if (!origName.StartsWith("http://", StringComparison.Ordinal)) { fileName = fileName.FullPath; } //Debug.Assert(FileService.IsValidPath(fileName)); if (FileService.IsDirectory(fileName)) { monitor.ReportError(GettextCatalog.GetString("{0} is a directory", fileName), null); return(false, null); } // test, if file fileName exists if (!origName.StartsWith("http://", StringComparison.Ordinal)) { // test, if an untitled file should be opened if (!Path.IsPathRooted(origName)) { foreach (Document doc in Documents) { if (doc.IsNewDocument && doc.FilePath == origName) { doc.Select(); ScrollToRequestedCaretLocation(doc, openFileInfo); return(true, doc); } } } if (!File.Exists(fileName)) { monitor.ReportError(GettextCatalog.GetString("File not found: {0}", fileName), null); return(false, null); } } Counters.OpenDocumentTimer.Trace("Looking for binding"); var documentControllerService = await ServiceProvider.GetService <DocumentControllerService> (); IExternalDisplayBinding externalBinding = null; DocumentControllerDescription internalBinding = null; WorkspaceObject project = null; if (openFileInfo.Owner == null) { var workspace = await ServiceProvider.GetService <RootWorkspace> (); // Set the project if one can be found. The project on the FileOpenInformation // is used to add project metadata to the OpenDocumentTimer counter. project = workspace.GetProjectContainingFile(fileName); // In some cases, the file may be a symlinked file. We cannot find the resolved symlink path // in the project, so we should try looking up the original file. if (project == null) { project = workspace.GetProjectContainingFile(openFileInfo.OriginalFileName); } openFileInfo.Owner = project; } else { project = openFileInfo.Owner; } var displayBindingService = await ServiceProvider.GetService <DisplayBindingService> (); var fileDescriptor = new FileDescriptor(fileName, null, project); var internalViewers = await documentControllerService.GetSupportedControllers(fileDescriptor); var externalViewers = displayBindingService.GetDisplayBindings(fileName, null, project as Project).OfType <IExternalDisplayBinding> ().ToList(); if (openFileInfo.DocumentControllerDescription != null) { internalBinding = openFileInfo.DocumentControllerDescription; } else { var bindings = displayBindingService.GetDisplayBindings(fileName, null, project as Project).ToList(); if (openFileInfo.Options.HasFlag(OpenDocumentOptions.OnlyInternalViewer)) { internalBinding = internalViewers.FirstOrDefault(d => d.CanUseAsDefault) ?? internalViewers.FirstOrDefault(); } else if (openFileInfo.Options.HasFlag(OpenDocumentOptions.OnlyExternalViewer)) { externalBinding = externalViewers.FirstOrDefault(d => d.CanUseAsDefault) ?? externalViewers.FirstOrDefault(); } else { internalBinding = internalViewers.FirstOrDefault(d => d.CanUseAsDefault); if (internalBinding == null) { externalBinding = externalViewers.FirstOrDefault(d => d.CanUseAsDefault); if (externalBinding == null) { internalBinding = internalViewers.FirstOrDefault(); if (internalBinding == null) { externalBinding = externalViewers.FirstOrDefault(); } } } } } Document newContent = null; try { if (internalBinding != null) { newContent = await LoadFile(fileName, monitor, internalBinding, project, openFileInfo); } else if (externalBinding != null) { var extBinding = (IExternalDisplayBinding)externalBinding; var app = extBinding.GetApplication(fileName, null, project as Project); app.Launch(fileName); } else if (!openFileInfo.Options.HasFlag(OpenDocumentOptions.OnlyInternalViewer)) { try { Counters.OpenDocumentTimer.Trace("Showing in browser"); desktopService.OpenFile(fileName); } catch (Exception ex) { LoggingService.LogError("Error opening file: " + fileName, ex); MessageService.ShowError(GettextCatalog.GetString("File '{0}' could not be opened", fileName)); return(false, null); } } Counters.OpenDocumentTimer.Trace("Adding to recent files"); desktopService.RecentFiles.AddFile(fileName, project); } catch (Exception ex) { monitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), ex); return(false, null); } return(true, newContent); }
public override Task <DocumentController> CreateController(FileDescriptor modelDescriptor, DocumentControllerDescription controllerDescription) { return(Task.FromResult <DocumentController> (new TestFileController())); }
/// <summary> /// Creates a controller for editing the provided file /// </summary> public abstract Task <DocumentController> CreateController(ModelDescriptor modelDescriptor, DocumentControllerDescription controllerDescription);
public override Task <DocumentController> CreateController(FileDescriptor file, DocumentControllerDescription controllerDescription) { var node = (InstanceExtensionNode)attribute.ExtensionNode; return(Task.FromResult((DocumentController)node.CreateInstance(typeof(DocumentController)))); }
public override Task <DocumentController> CreateController(ModelDescriptor modelDescriptor, DocumentControllerDescription controllerDescription) { return(CreateController((FileDescriptor)modelDescriptor, controllerDescription)); }