File indexing completed on 2024-04-28 04:37:16

0001 /*
0002     SPDX-FileCopyrightText: 2002 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
0003     SPDX-FileCopyrightText: 2002 Bernd Gehrmann <bernd@kdevelop.org>
0004     SPDX-FileCopyrightText: 2003 Roberto Raggi <roberto@kdevelop.org>
0005     SPDX-FileCopyrightText: 2003 Hamish Rodda <rodda@kde.org>
0006     SPDX-FileCopyrightText: 2003 Harald Fernengel <harry@kdevelop.org>
0007     SPDX-FileCopyrightText: 2003 Jens Dagerbo <jens.dagerbo@swipnet.se>
0008     SPDX-FileCopyrightText: 2005 Adam Treat <treat@kde.org>
0009     SPDX-FileCopyrightText: 2004-2007 Alexander Dymo <adymo@kdevelop.org>
0010     SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
0011 
0012     SPDX-License-Identifier: LGPL-2.0-or-later
0013 */
0014 
0015 #ifndef KDEVPLATFORM_DOCUMENTCONTROLLER_H
0016 #define KDEVPLATFORM_DOCUMENTCONTROLLER_H
0017 
0018 #include <QList>
0019 
0020 #include <interfaces/idocumentcontroller.h>
0021 
0022 #include "shellexport.h"
0023 
0024 namespace KTextEditor {
0025 class View;
0026 }
0027 
0028 namespace Sublime {
0029     class Document;
0030     class Area;
0031     class AreaIndex;
0032 }
0033 
0034 namespace KDevelop {
0035 class MainWindow;
0036 class DocumentControllerPrivate;
0037 
0038 /**
0039  * \short Interface to control open documents.
0040  * The document controller manages open documents in the IDE.
0041  * Open documents are usually editors, GUI designers, html documentation etc.
0042  *
0043  * Please note that this interface gives access to documents and not to their views.
0044  * It is possible that more than 1 view is shown in KDevelop for a document.
0045 */
0046 class KDEVPLATFORMSHELL_EXPORT DocumentController: public IDocumentController {
0047     Q_OBJECT
0048     Q_CLASSINFO( "D-Bus Interface", "org.kdevelop.DocumentController" )
0049 public:
0050 
0051     /**Constructor.
0052     @param parent The parent object.*/
0053     explicit DocumentController( QObject *parent = nullptr );
0054     ~DocumentController() override;
0055 
0056     /**Finds the first document object corresponding to a given url.
0057     @param url The Url of the document.
0058     @return The corresponding document, or null if not found.*/
0059     IDocument* documentForUrl( const QUrl & url ) const override;
0060 
0061     /**@return The list of open documents*/
0062     QList<IDocument*> openDocuments() const override;
0063 
0064     /**Refers to the document currently active or focused.
0065     @return The active document.*/
0066     IDocument* activeDocument() const override;
0067 
0068     KTextEditor::View* activeTextDocumentView() const override;
0069     
0070     /// Activate the given \a document. This convenience function does not add the document
0071     /// to the File/Recent Open menu. Use DocumentController::openDocument if that is desired.
0072     void activateDocument( IDocument * document, const KTextEditor::Range& range = KTextEditor::Range::invalid() ) override;
0073 
0074     void registerDocumentForMimetype( const QString&, KDevelop::IDocumentFactory* ) override;
0075 
0076     /// Request the document controller to save all documents.
0077     /// If the \a mode is not IDocument::Silent, ask the user which documents to save.
0078     /// Returns false if the user cancels the save dialog.
0079     bool saveAllDocuments(IDocument::DocumentSaveMode mode) override;
0080     bool saveAllDocumentsForWindow(KParts::MainWindow* mw, IDocument::DocumentSaveMode mode, bool currentAreaOnly = false) override;
0081 
0082     void initialize();
0083 
0084     void cleanup();
0085 
0086     virtual QStringList documentTypes() const;
0087 
0088     QString documentType(Sublime::Document* document) const;
0089 
0090     using IDocumentController::openDocument;
0091 
0092     /**checks that url is an url of empty document*/
0093     static bool isEmptyDocumentUrl(const QUrl &url);
0094     static QUrl nextEmptyDocumentUrl();
0095     
0096     IDocumentFactory* factory(const QString& mime) const override;
0097 
0098     
0099     bool openDocument(IDocument* doc,
0100                               const KTextEditor::Range& range = KTextEditor::Range::invalid(),
0101                               DocumentActivationParams activationParams = {},
0102                               IDocument* buddy = nullptr) override;
0103     
0104 public Q_SLOTS:
0105     /**Opens a new or existing document.
0106     @param url The full Url of the document to open. If it is empty, a dialog to choose the document will be opened.
0107     @param range The location information, if applicable.
0108     @param activationParams Indicates whether to fully activate the document.
0109     @param buddy The buddy document
0110     @return The opened document
0111     */
0112     IDocument* openDocument( const QUrl &url,
0113             const KTextEditor::Range& range = KTextEditor::Range::invalid(),
0114             DocumentActivationParams activationParams = {},
0115             const QString& encoding = {},
0116             IDocument* buddy = nullptr ) override;
0117 
0118     IDocument* openDocumentFromText( const QString& data ) override;
0119 
0120     KDevelop::IDocument* openDocument( const QUrl &url, const QString& prefName ) override;
0121 
0122     virtual bool closeDocument( const QUrl &url );
0123     void fileClose();
0124     void slotSaveAllDocuments();
0125     bool closeAllDocuments() override;
0126     void closeAllOtherDocuments();
0127     void reloadAllDocuments();
0128 
0129     // DBUS-compatible versions of openDocument
0130     virtual Q_SCRIPTABLE bool openDocumentSimple( QString url, int line = -1, int column = 0 );
0131     // Opens a list of documents, with optional split-view separators, like: "file1 / [ file2 - fil3 ]" (see kdevplatform_shell_environment.sh)
0132     virtual Q_SCRIPTABLE bool openDocumentsSimple( QStringList urls );
0133     virtual Q_SCRIPTABLE bool openDocumentFromTextSimple( QString text );
0134     
0135     // If 'target' is empty, returns the currently active document, or
0136     // the currently selected project-item if no document is active.
0137     // If 'target' is "[selection]", returns the path of the currently active selection.
0138     // If 'target' is the name of a project, returns the root-path of that project.
0139     // Whenever the returned path corresponds to a directory, a '/.' suffix is appended.
0140     Q_SCRIPTABLE QString activeDocumentPath(const QString& target = {}) const;
0141 
0142     // Returns all open documents in the current area
0143     Q_SCRIPTABLE QStringList activeDocumentPaths() const;
0144     void vcsAnnotateCurrentDocument();
0145 
0146 private Q_SLOTS:
0147     virtual void slotOpenDocument(const QUrl &url);
0148     void notifyDocumentClosed(Sublime::Document* doc);
0149 
0150 private:
0151     bool openDocumentsWithSplitSeparators( Sublime::AreaIndex* index, QStringList urlsWithSeparators, bool& isFirstView );
0152     QList<IDocument*> visibleDocumentsInWindow(MainWindow* mw) const;
0153     QList<IDocument*> documentsExclusivelyInWindow(MainWindow* mw, bool currentAreaOnly = false) const;
0154     QList<IDocument*> modifiedDocuments(const QList<IDocument*>& list) const;
0155 
0156     bool saveSomeDocuments(const QList<IDocument*>& list, IDocument::DocumentSaveMode mode) override;
0157 
0158     void setupActions();
0159 
0160 private:
0161     const QScopedPointer<class DocumentControllerPrivate> d_ptr;
0162     Q_DECLARE_PRIVATE(DocumentController)
0163 
0164     friend class DocumentControllerPrivate;
0165 };
0166 
0167 }
0168 
0169 #endif
0170