void FindDisplayBinding (FilePath fileName, string mimeType, Project ownerProject)
		{
			findingDisplayBinding = true;

			try {
				string mappingKey = lazyFileViewer.GetMappingKey ();

				var fileViewer = OpenWithFileViewer.GetFileViewers (
					fileName,
					mimeType,
					ownerProject).FirstOrDefault (item => item.GetMappingKey () == mappingKey);

				if (fileViewer != null) {
					displayBinding = DisplayBindingFactory.CreateDisplayBinding (
						fileName,
						mimeType,
						fileViewer) as IExternalDisplayBinding;
				}
			} finally {
				findingDisplayBinding = false;
			}
		}
Esempio n. 2
0
        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);
        }