Exemplo n.º 1
0
        void RealOpenFile(FileOpenInformation openFileInfo)
        {
            FilePath         fileName;
            IProgressMonitor monitor = openFileInfo.ProgressMonitor;

            using (monitor)
            {
                Counters.OpenDocumentTimer.Trace("Checking file");

                string origName = openFileInfo.FileName;

                if (origName == null)
                {
                    monitor.ReportError(GettextCatalog.GetString("Invalid file name"), null);
                    return;
                }

                if (origName.StartsWith("file://"))
                {
                    fileName = new Uri(origName).LocalPath;
                }
                else
                {
                    fileName = origName;
                }

                if (!origName.StartsWith("http://"))
                {
                    fileName = fileName.FullPath;
                }

                //Debug.Assert(FileService.IsValidPath(fileName));
                if (FileService.IsDirectory(fileName))
                {
                    monitor.ReportError(GettextCatalog.GetString("{0} is a directory", fileName), null);
                    return;
                }
                // test, if file fileName exists
                if (!origName.StartsWith("http://"))
                {
                    // test, if an untitled file should be opened
                    if (!System.IO.Path.IsPathRooted(origName))
                    {
                        foreach (Document doc in Documents)
                        {
                            if (doc.Window.ViewContent.IsUntitled && doc.Window.ViewContent.UntitledName == origName)
                            {
                                doc.Select();
                                openFileInfo.NewContent = doc.Window.ViewContent;
                                return;
                            }
                        }
                    }
                    if (!File.Exists(fileName))
                    {
                        monitor.ReportError(GettextCatalog.GetString("File not found: {0}", fileName), null);
                        return;
                    }
                }

                foreach (Document doc in Documents)
                {
                    if (doc.FileName == fileName)
                    {
                        if (openFileInfo.Options.HasFlag(OpenDocumentOptions.BringToFront))
                        {
                            doc.Select();
                            doc.RunWhenLoaded(delegate {
                                IEditableTextBuffer ipos = doc.GetContent <IEditableTextBuffer> ();
                                if (openFileInfo.Line > 0 && ipos != null)
                                {
                                    ipos.SetCaretTo(openFileInfo.Line, Math.Max(1, openFileInfo.Column), openFileInfo.Options.HasFlag(OpenDocumentOptions.HighlightCaretLine));
                                }
                            });
                        }
                        openFileInfo.NewContent = doc.Window.ViewContent;
                        return;
                    }
                }

                Counters.OpenDocumentTimer.Trace("Looking for binding");

                IDisplayBinding     binding     = null;
                IViewDisplayBinding viewBinding = null;
                Project             project     = GetProjectContainingFile(fileName);

                if (openFileInfo.DisplayBinding != null)
                {
                    binding = viewBinding = openFileInfo.DisplayBinding;
                }
                else
                {
                    var bindings = DisplayBindingService.GetDisplayBindings(fileName, null, project).Where(d => d.CanUseAsDefault);
                    if (openFileInfo.Options.HasFlag(OpenDocumentOptions.OnlyInternalViewer))
                    {
                        binding     = bindings.OfType <IViewDisplayBinding>().FirstOrDefault();
                        viewBinding = (IViewDisplayBinding)binding;
                    }
                    else if (openFileInfo.Options.HasFlag(OpenDocumentOptions.OnlyExternalViewer))
                    {
                        binding     = bindings.OfType <IExternalDisplayBinding>().FirstOrDefault();
                        viewBinding = null;
                    }
                    else
                    {
                        binding     = bindings.FirstOrDefault();
                        viewBinding = binding as IViewDisplayBinding;
                    }
                }

                if (binding != null)
                {
                    if (viewBinding != null)
                    {
                        var fw = new LoadFileWrapper(workbench, viewBinding, project, openFileInfo);
                        fw.Invoke(fileName);
                    }
                    else
                    {
                        var extBinding = (IExternalDisplayBinding)binding;
                        var app        = extBinding.GetApplication(fileName, null, project);
                        app.Launch(fileName);
                    }

                    Counters.OpenDocumentTimer.Trace("Adding to recent files");
                    DesktopService.RecentFiles.AddFile(fileName, project);
                }
                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));
                    }
                }
            }
        }
Exemplo n.º 2
0
        void RealOpenFile(FileOpenInformation openFileInfo)
        {
            FilePath         fileName;
            IProgressMonitor monitor = openFileInfo.ProgressMonitor;

            using (monitor)
            {
                Counters.OpenDocumentTimer.Trace("Checking file");

                string origName = openFileInfo.FileName;

                if (origName == null)
                {
                    monitor.ReportError(GettextCatalog.GetString("Invalid file name"), null);
                    return;
                }

                if (origName.StartsWith("file://"))
                {
                    fileName = new Uri(origName).LocalPath;
                }
                else
                {
                    fileName = origName;
                }

                if (!origName.StartsWith("http://"))
                {
                    fileName = fileName.FullPath;
                }

                //Debug.Assert(FileService.IsValidPath(fileName));
                if (FileService.IsDirectory(fileName))
                {
                    monitor.ReportError(GettextCatalog.GetString("{0} is a directory", fileName), null);
                    return;
                }
                // test, if file fileName exists
                if (!origName.StartsWith("http://"))
                {
                    // test, if an untitled file should be opened
                    if (!System.IO.Path.IsPathRooted(origName))
                    {
                        foreach (Document doc in Documents)
                        {
                            if (doc.Window.ViewContent.IsUntitled && doc.Window.ViewContent.UntitledName == origName)
                            {
                                doc.Select();
                                openFileInfo.NewContent = doc.Window.ViewContent;
                                return;
                            }
                        }
                    }
                    if (!File.Exists(fileName))
                    {
                        monitor.ReportError(GettextCatalog.GetString("File not found: {0}", fileName), null);
                        return;
                    }
                }

                foreach (Document doc in Documents)
                {
                    if (doc.FileName == fileName)
                    {
                        if (openFileInfo.BringToFront)
                        {
                            doc.Select();
                            IEditableTextBuffer ipos = doc.GetContent <IEditableTextBuffer> ();
                            if (openFileInfo.Line != -1 && ipos != null)
                            {
                                ipos.SetCaretTo(openFileInfo.Line, openFileInfo.Column != -1 ? openFileInfo.Column : 0, openFileInfo.HighlightCaretLine);
                            }
                        }
                        openFileInfo.NewContent = doc.Window.ViewContent;
                        return;
                    }
                }

                Counters.OpenDocumentTimer.Trace("Looking for binding");

                IDisplayBinding binding;

                if (openFileInfo.DisplayBinding != null)
                {
                    binding = openFileInfo.DisplayBinding;
                }
                else
                {
                    binding = DisplayBindingService.GetDefaultBinding(fileName, DesktopService.GetMimeTypeForUri(fileName));
                }

                if (binding != null)
                {
                    // When looking for the project to which the file belongs, look first
                    // in the active project, then the active solution, and so on
                    Project project = null;
                    if (IdeApp.ProjectOperations.CurrentSelectedProject != null)
                    {
                        if (IdeApp.ProjectOperations.CurrentSelectedProject.Files.GetFile(fileName) != null)
                        {
                            project = IdeApp.ProjectOperations.CurrentSelectedProject;
                        }
                    }
                    if (project == null && IdeApp.ProjectOperations.CurrentSelectedWorkspaceItem != null)
                    {
                        project = IdeApp.ProjectOperations.CurrentSelectedWorkspaceItem.GetProjectContainingFile(fileName);
                        if (project == null)
                        {
                            WorkspaceItem it = IdeApp.ProjectOperations.CurrentSelectedWorkspaceItem.ParentWorkspace;
                            while (it != null && project == null)
                            {
                                project = it.GetProjectContainingFile(fileName);
                                it      = it.ParentWorkspace;
                            }
                        }
                    }
                    if (project == null)
                    {
                        project = IdeApp.Workspace.GetProjectContainingFile(fileName);
                    }

                    LoadFileWrapper fw = new LoadFileWrapper(workbench, binding, project, openFileInfo);
                    fw.Invoke(fileName);

                    Counters.OpenDocumentTimer.Trace("Adding to recent files");
                    DesktopService.RecentFiles.AddFile(fileName, project);
                }
                else
                {
                    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));
                    }
                }
            }
        }