Example #1
0
        public Document NewDocument(string defaultName, string mimeType, Stream content)
        {
            IViewDisplayBinding binding = DisplayBindingService.GetDefaultViewBinding(null, mimeType, null);

            if (binding == null)
            {
                throw new ApplicationException("Can't create display binding for mime type: " + mimeType);
            }

            IViewContent newContent = binding.CreateContent(null, mimeType, null);

            using (content) {
                newContent.LoadNew(content, mimeType);
            }

            if (newContent == null)
            {
                throw new ApplicationException(String.Format("Created view content was null{3}DefaultName:{0}{3}MimeType:{1}{3}Content:{2}",
                                                             defaultName, mimeType, content, Environment.NewLine));
            }

            newContent.UntitledName = defaultName;
            newContent.IsDirty      = true;
            workbench.ShowView(newContent, true);
            DisplayBindingService.AttachSubWindows(newContent.WorkbenchWindow);

            return(WrapDocument(newContent.WorkbenchWindow));
        }
Example #2
0
        public FileViewer[] GetFileViewers(FilePath fileName)
        {
            List <FileViewer> list = new List <FileViewer> ();

            string mimeType = DesktopService.GetMimeTypeForUri(fileName);

            foreach (IDisplayBinding bin in DisplayBindingService.GetBindingsForMimeType(mimeType))
            {
                list.Add(new FileViewer(bin));
            }

            foreach (var app in DesktopService.GetApplications(fileName))
            {
                list.Add(new FileViewer(app));
            }

            return(list.ToArray());
        }
Example #3
0
        public FileViewer[] GetFileViewers(FilePath fileName)
        {
            List <FileViewer> list = new List <FileViewer> ();

            string mimeType = DesktopService.GetMimeTypeForUri(fileName);

            foreach (IDisplayBinding bin in DisplayBindingService.GetBindingsForMimeType(mimeType))
            {
                list.Add(new FileViewer(bin));
            }

            foreach (DesktopApplication app in DesktopService.GetAllApplications(mimeType))
            {
                if (app.IsValid && app.Command != "monodevelop")
                {
                    list.Add(new FileViewer(app));
                }
            }

            return(list.ToArray());
        }
Example #4
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));
                    }
                }
            }
        }
Example #5
0
        public void Invoke(string fileName)
        {
            try {
                Counters.OpenDocumentTimer.Trace("Creating content");
                string mimeType = DesktopService.GetMimeTypeForUri(fileName);
                if (binding.CanHandle(fileName, mimeType, project))
                {
                    newContent = binding.CreateContent(fileName, mimeType, project);
                }
                else
                {
                    fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), null);
                }
                if (newContent == null)
                {
                    fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), null);
                    return;
                }

                if (project != null)
                {
                    newContent.Project = project;
                }

                Counters.OpenDocumentTimer.Trace("Loading file");

                IEncodedTextContent etc = newContent.GetContent <IEncodedTextContent> ();
                try {
                    if (fileInfo.Encoding != null && etc != null)
                    {
                        etc.Load(fileName, fileInfo.Encoding);
                    }
                    else
                    {
                        newContent.Load(fileName);
                    }
                } catch (InvalidEncodingException iex) {
                    fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not opened. {1}", fileName, iex.Message), null);
                    return;
                } catch (OverflowException oex) {
                    fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not opened. File too large.", fileName), null);
                    return;
                }
            } catch (Exception ex) {
                fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), ex);
                return;
            }
            // content got re-used
            if (newContent.WorkbenchWindow != null)
            {
                newContent.WorkbenchWindow.SelectWindow();
                fileInfo.NewContent = newContent;
                return;
            }

            Counters.OpenDocumentTimer.Trace("Showing view");

            workbench.ShowView(newContent, fileInfo.Options.HasFlag(OpenDocumentOptions.BringToFront));
            DisplayBindingService.AttachSubWindows(newContent.WorkbenchWindow);

            newContent.WorkbenchWindow.DocumentType = binding.Name;

            IEditableTextBuffer ipos = newContent.GetContent <IEditableTextBuffer> ();

            if (fileInfo.Line > 0 && ipos != null)
            {
                if (newContent.Control.IsRealized)
                {
                    JumpToLine();
                }
                else
                {
                    newContent.Control.Realized += HandleNewContentControlRealized;
                }
            }
            fileInfo.NewContent = newContent;
        }
Example #6
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));
                    }
                }
            }
        }
Example #7
0
        public void Invoke(string fileName)
        {
            try {
                Counters.OpenDocumentTimer.Trace("Creating content");
                if (binding.CanCreateContentForUri(fileName))
                {
                    newContent = binding.CreateContentForUri(fileName);
                }
                else
                {
                    string mimeType = DesktopService.GetMimeTypeForUri(fileName);
                    if (!binding.CanCreateContentForMimeType(mimeType))
                    {
                        fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), null);
                        return;
                    }
                    newContent = binding.CreateContentForMimeType(mimeType, null);
                }
                if (newContent == null)
                {
                    fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), null);
                    return;
                }

                if (project != null)
                {
                    newContent.Project = project;
                }

                Counters.OpenDocumentTimer.Trace("Loading file");

                IEncodedTextContent etc = newContent.GetContent <IEncodedTextContent> ();
                if (fileInfo.Encoding != null && etc != null)
                {
                    etc.Load(fileName, fileInfo.Encoding);
                }
                else
                {
                    newContent.Load(fileName);
                }
            } catch (Exception ex) {
                fileInfo.ProgressMonitor.ReportError(GettextCatalog.GetString("The file '{0}' could not be opened.", fileName), ex);
                return;
            }
            // content got re-used
            if (newContent.WorkbenchWindow != null)
            {
                newContent.WorkbenchWindow.SelectWindow();
                fileInfo.NewContent = newContent;
                return;
            }

            Counters.OpenDocumentTimer.Trace("Showing view");

            workbench.ShowView(newContent, fileInfo.BringToFront);
            DisplayBindingService.AttachSubWindows(newContent.WorkbenchWindow);

            newContent.WorkbenchWindow.DocumentType = binding.Name;

            IEditableTextBuffer ipos = newContent.GetContent <IEditableTextBuffer> ();

            if (fileInfo.Line != -1 && ipos != null)
            {
                GLib.Timeout.Add(10, new GLib.TimeoutHandler(JumpToLine));
            }
            fileInfo.NewContent = newContent;
        }