Exemple #1
0
        /// <summary>
        /// Loads a document from the specified path. This performs several steps in the following
        /// order. The path is converted to a DocumentType. Next, each of the registered
        /// document preload handlers is called, if any return false the load is cancelled.
        /// The document type is used to retrieve the open document handler registered for that
        /// type and the handler is used to open the document.
        /// If no handler is available the document is (otionally) passed to the Windows shell
        /// to be opened externally. If this fails the unknown document handler is
        /// retrieved and used to open the document. If this is not available
        /// an optional message is shown and the load fails.
        /// </summary>
        /// <param name="path">The path of the document.</param>
        /// <returns>The loaded document.</returns>
        public IDockContent LoadDocument(string path)
        {
            ClientProfile profile = _applicationManager.ClientProfile;

            /*
             * Look for readonly flag - path will end with '?'. This is really
             * only for the benefit of the document serialisation which needs
             * to be able to restore a readonly file from the save document
             * configuration.
             */

            bool readOnly = false;

            if (path.EndsWith("?"))
            {
                readOnly = true;
                path     = path.TrimEnd('?');
            }

            DocumentType documentType = new DocumentType(path);

            /*
             * Document PreLoad Handlers
             *
             * The idea here is to allow applications to create a chain
             * of filters each being responsible for performing some
             * action on a particular document type. Each handler
             * should immediately return true if it's not interested
             * in the document type passed. The chain will end when the
             * first handler returns false and the document load will be
             * cancelled. Since there is no guarantee of the order in
             * which the handlers are called (registration order will
             * depend on plugin load order which can vary) there should
             * be only one handler for a particular type unless they are
             * completely independent.
             */

            foreach (DocumentPreLoadHandler d in
                     _applicationManager.DocumentPreLoadHandlers)
            {
                if (!d(documentType))
                {
                    return(null);
                }
            }

            OpenDocumentHandler handler =
                _applicationManager.GetOpenDocumentHandler(documentType);

            if (handler == null && _allowShellOpen)
            {
                try
                {
                    FileTools.ShellOpenFile(path);
                    return(null);
                }
                catch
                {
                    /*
                     * Suppress error - if the load fails the document
                     * gets passed to the unknown document handler.
                     */
                }
            }

            if (handler == null)
            {
                handler = _applicationManager.UnknownDocumentHandler;
            }

            if (handler == null)
            {
                if (_showNoHandlerMessage)
                {
                    MessageBox.Show(
                        Resources.NoHandlerMessage,
                        Resources.NoHandlerTitle,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }

                return(null);
            }
            else
            {
                return(handler(path, readOnly));
            }
        }