File indexing completed on 2024-05-19 04:00:02

0001 /*
0002     SPDX-FileCopyrightText: 2013 Christoph Cullmann <cullmann@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KTEXTEDITOR_APPLICATION_H
0008 #define KTEXTEDITOR_APPLICATION_H
0009 
0010 #include <ktexteditor_export.h>
0011 
0012 #include <QObject>
0013 
0014 namespace KTextEditor
0015 {
0016 class Plugin;
0017 class Document;
0018 class MainWindow;
0019 
0020 /**
0021  * \class Application application.h <KTextEditor/Application>
0022  *
0023  * This class allows the application that embeds the KTextEditor component to
0024  * allow it access to application wide information and interactions.
0025  *
0026  * For example the component can get the current active main window of the application.
0027  *
0028  * The application must pass a pointer to the Application object to the setApplication method of the
0029  * global editor instance and ensure that this object stays valid for the complete lifetime of the editor.
0030  *
0031  * It must not reimplement this class but construct an instance and pass a pointer to a QObject that
0032  * has the required slots to receive the requests.
0033  *
0034  * KTextEditor::Editor::instance()->application() will always return a non-nullptr object
0035  * to avoid the need for nullptr checks before calling the API.
0036  *
0037  * The same holds for activeMainWindow(), even if no main window is around, you will get a non-nullptr
0038  * interface object that allows to call the functions of the MainWindow without needs for a nullptr
0039  * check around it in the client code.
0040  *
0041  * @ref kte_plugin_hosting
0042  */
0043 class KTEXTEDITOR_EXPORT Application : public QObject
0044 {
0045     Q_OBJECT
0046 
0047 public:
0048     /**
0049      * Construct an Application wrapper object.
0050      * The passed parent is both the parent of this QObject and the receiver of all interface
0051      * calls via invokeMethod.
0052      * @param parent object the calls are relayed to
0053      */
0054     Application(QObject *parent);
0055 
0056     /**
0057      * Virtual Destructor
0058      */
0059     ~Application() override;
0060 
0061     /**
0062      * Ask app to quit. The app might interact with the user and decide that
0063      * quitting is not possible and return false.
0064      *
0065      * \return true if the app could quit
0066      */
0067     bool quit();
0068 
0069     //
0070     // MainWindow related accessors
0071     //
0072 public:
0073     /**
0074      * Get a list of all main windows.
0075      * @return all main windows, might be empty!
0076      */
0077     QList<KTextEditor::MainWindow *> mainWindows();
0078 
0079     /**
0080      * Accessor to the active main window.
0081      * \return a pointer to the active mainwindow, even if no main window is active you
0082      *         will get a non-nullptr dummy interface that allows you to call interface functions
0083      *         without the need for null checks
0084      */
0085     KTextEditor::MainWindow *activeMainWindow();
0086 
0087     //
0088     // Document related accessors
0089     //
0090 public:
0091     /**
0092      * Get a list of all documents that are managed by the application.
0093      * This might contain less documents than the editor has in his documents () list.
0094      * @return all documents the application manages, might be empty!
0095      */
0096     QList<KTextEditor::Document *> documents();
0097 
0098     /**
0099      * Get the document with the URL \p url.
0100      * if multiple documents match the searched url, return the first found one...
0101      * \param url the document's URL
0102      * \return the document with the given \p url or nullptr, if none found
0103      */
0104     KTextEditor::Document *findUrl(const QUrl &url);
0105 
0106     /**
0107      * Open the document \p url with the given \p encoding.
0108      * if the url is empty, a new empty document will be created
0109      * \param url the document's url
0110      * \param encoding the preferred encoding. If encoding is QString() the
0111      *        encoding will be guessed or the default encoding will be used.
0112      * \return a pointer to the created document
0113      */
0114     KTextEditor::Document *openUrl(const QUrl &url, const QString &encoding = QString());
0115 
0116     /**
0117      * Close the given \p document. If the document is modified, user will be asked if he wants that.
0118      * \param document the document to be closed
0119      * \return \e true on success, otherwise \e false
0120      */
0121     bool closeDocument(KTextEditor::Document *document);
0122 
0123     /**
0124      * Close a list of documents. If any of them are modified, user will be asked if he wants that.
0125      * Use this, if you want to close multiple documents at once, as the application might
0126      * be able to group the "do you really want that" dialogs into one.
0127      * \param documents list of documents to be closed
0128      * \return \e true on success, otherwise \e false
0129      */
0130     bool closeDocuments(const QList<KTextEditor::Document *> &documents);
0131 
0132 Q_SIGNALS:
0133     /**
0134      * This signal is emitted when the \p document was created.
0135      *
0136      * @param document document that was created
0137      */
0138     void documentCreated(KTextEditor::Document *document);
0139 
0140     /**
0141      * This signal is emitted before a \p document which should be closed is deleted
0142      * The document is still accessible and usable, but it will be deleted
0143      * after this signal was send.
0144      *
0145      * @param document document that will be deleted
0146      */
0147     void documentWillBeDeleted(KTextEditor::Document *document);
0148 
0149     /**
0150      * This signal is emitted when the \p document has been deleted.
0151      *
0152      * @warning Do not access the data referenced by the pointer, it is already invalid.
0153      * Use the pointer only to remove mappings in hash or maps
0154      *
0155      * @param document document that is deleted
0156      */
0157     void documentDeleted(KTextEditor::Document *document);
0158 
0159     //
0160     // Application plugin accessors
0161     //
0162 public:
0163     /**
0164      * Get a plugin for the plugin with with identifier \p name.
0165      * \param name the plugin's name
0166      * \return pointer to the plugin if a plugin with \p name is loaded, otherwise nullptr
0167      */
0168     KTextEditor::Plugin *plugin(const QString &name);
0169 
0170     //
0171     // Signals related to application plugins
0172     //
0173 Q_SIGNALS:
0174     /**
0175      * This signal is emitted when an Plugin was loaded.
0176      *
0177      * @param name name of plugin
0178      * @param plugin the new plugin
0179      */
0180     void pluginCreated(const QString &name, KTextEditor::Plugin *plugin);
0181 
0182     /**
0183      * This signal is emitted when an Plugin got deleted.
0184      *
0185      * @param name name of plugin
0186      * @param plugin the deleted plugin
0187      *
0188      * @warning Do not access the data referenced by the pointer, it is already invalid.
0189      * Use the pointer only to remove mappings in hash or maps
0190      */
0191     void pluginDeleted(const QString &name, KTextEditor::Plugin *plugin);
0192 
0193 private:
0194     /**
0195      * Private d-pointer class is our best friend ;)
0196      */
0197     friend class ApplicationPrivate;
0198 
0199     /**
0200      * Private d-pointer
0201      */
0202     class ApplicationPrivate *const d;
0203 };
0204 
0205 } // namespace KTextEditor
0206 
0207 #endif