1 /*
2     SPDX-FileCopyrightText: 2001-2010 Christoph Cullmann <cullmann@kde.org>
3     SPDX-FileCopyrightText: 2009 Erlend Hamberg <ehamberg@gmail.com>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #ifndef KATE_GLOBAL_H
9 #define KATE_GLOBAL_H
10 
11 #include <ktexteditor_export.h>
12 
13 #include <ktexteditor/editor.h>
14 #include <ktexteditor/view.h>
15 
16 #include <KAboutData>
17 #include <KSharedConfig>
18 
19 #include <KTextEditor/Application>
20 #include <KTextEditor/MainWindow>
21 
22 #include <QList>
23 #include <QPointer>
24 
25 #include <array>
26 #include <memory>
27 
28 class QStringListModel;
29 
30 class KateCmd;
31 class KateModeManager;
32 class KateGlobalConfig;
33 class KateDocumentConfig;
34 class KateViewConfig;
35 class KateRendererConfig;
36 namespace KTextEditor
37 {
38 class DocumentPrivate;
39 }
40 namespace KTextEditor
41 {
42 class ViewPrivate;
43 class Command;
44 }
45 class KateScriptManager;
46 class KDirWatch;
47 class KateHlManager;
48 class KateSpellCheckManager;
49 class KateWordCompletionModel;
50 class KateAbstractInputModeFactory;
51 class KateKeywordCompletionModel;
52 class KateVariableExpansionManager;
53 
54 namespace KTextEditor
55 {
56 /**
57  * KTextEditor::EditorPrivate
58  * One instance of this class is hold alive during
59  * a kate part session, as long as any factory, document
60  * or view stay around, here is the place to put things
61  * which are needed and shared by all this objects ;)
62  */
63 class KTEXTEDITOR_EXPORT EditorPrivate : public KTextEditor::Editor
64 {
65     Q_OBJECT
66 
67     friend class KTextEditor::Editor;
68 
69     // unit testing support
70 public:
71     /**
72      * Calling this function internally sets a flag such that unitTestMode()
73      * returns \p true.
74      */
75     static void enableUnitTestMode();
76 
77     /**
78      * Returns \p true, if the unit test mode was enabled through a call of
79      * enableUnitTestMode(), otherwise \p false.
80      */
81     static bool unitTestMode();
82 
83     // for setDefaultEncoding
84     friend class KateDocumentConfig;
85 
86 private:
87     /**
88      * Default constructor, private, as singleton
89      * @param staticInstance pointer to fill with content of this
90      */
91     explicit EditorPrivate(QPointer<KTextEditor::EditorPrivate> &staticInstance);
92 
93 public:
94     /**
95      * Destructor
96      */
97     ~EditorPrivate() override;
98 
99     /**
100      * Create a new document object
101      * @param parent parent object
102      * @return created KTextEditor::Document
103      */
104     KTextEditor::Document *createDocument(QObject *parent) override;
105 
106     /**
107      * Returns a list of all documents of this editor.
108      * @return list of all existing documents
109      */
documents()110     QList<KTextEditor::Document *> documents() override
111     {
112         return m_documents.keys();
113     }
114 
115     /**
116      * Set the global application object.
117      * This will allow the editor component to access
118      * the hosting application.
119      * @param application application object
120      */
setApplication(KTextEditor::Application * application)121     void setApplication(KTextEditor::Application *application) override
122     {
123         // switch back to dummy application?
124         m_application = application ? application : &m_dummyApplication;
125     }
126 
127     /**
128      * Current hosting application, if any set.
129      * @return current application object or nullptr
130      */
application()131     KTextEditor::Application *application() const override
132     {
133         return m_application;
134     }
135 
136     /**
137      * General Information about this editor
138      */
139 public:
140     /**
141      * return the about data
142      * @return about data of this editor part
143      */
aboutData()144     const KAboutData &aboutData() const override
145     {
146         return m_aboutData;
147     }
148 
149     /**
150      * Configuration management
151      */
152 public:
153     /**
154      * Shows a config dialog for the part, changes will be applied
155      * to the editor, but not saved anywhere automagically, call
156      * writeConfig to save them
157      */
158     void configDialog(QWidget *parent) override;
159 
160     /**
161      * Number of available config pages
162      * If the editor returns a number < 1, it doesn't support this
163      * and the embedding app should use the configDialog () instead
164      * @return number of config pages
165      */
166     int configPages() const override;
167 
168     /**
169      * returns config page with the given number,
170      * config pages from 0 to configPages()-1 are available
171      * if configPages() > 0
172      */
173     KTextEditor::ConfigPage *configPage(int number, QWidget *parent) override;
174 
175     /**
176      * Kate Part Internal stuff ;)
177      */
178 public:
179     /**
180      * singleton accessor
181      * @return instance of the factory
182      */
183     static KTextEditor::EditorPrivate *self();
184 
185     /**
186      * register document at the factory
187      * this allows us to loop over all docs for example on config changes
188      * @param doc document to register
189      */
190     void registerDocument(KTextEditor::DocumentPrivate *doc);
191 
192     /**
193      * unregister document at the factory
194      * @param doc document to register
195      */
196     void deregisterDocument(KTextEditor::DocumentPrivate *doc);
197 
198     /**
199      * register view at the factory
200      * this allows us to loop over all views for example on config changes
201      * @param view view to register
202      */
203     void registerView(KTextEditor::ViewPrivate *view);
204 
205     /**
206      * unregister view at the factory
207      * @param view view to unregister
208      */
209     void deregisterView(KTextEditor::ViewPrivate *view);
210 
211     /**
212      * return a list of all registered views
213      * @return all known views
214      */
views()215     QList<KTextEditor::ViewPrivate *> views()
216     {
217         return m_views.values();
218     }
219 
220     /**
221      * global dirwatch
222      * @return dirwatch instance
223      */
dirWatch()224     KDirWatch *dirWatch()
225     {
226         return m_dirWatch;
227     }
228 
229     /**
230      * The global configuration of katepart, e.g. katepartrc
231      * @return global shared access to katepartrc config
232      */
233     static KSharedConfigPtr config();
234 
235     /**
236      * global mode manager
237      * used to manage the modes centrally
238      * @return mode manager
239      */
modeManager()240     KateModeManager *modeManager()
241     {
242         return m_modeManager;
243     }
244 
245     /**
246      * fallback document config
247      * @return default config for all documents
248      */
documentConfig()249     KateDocumentConfig *documentConfig()
250     {
251         return m_documentConfig;
252     }
253 
254     /**
255      * fallback view config
256      * @return default config for all views
257      */
viewConfig()258     KateViewConfig *viewConfig()
259     {
260         return m_viewConfig;
261     }
262 
263     /**
264      * fallback renderer config
265      * @return default config for all renderers
266      */
rendererConfig()267     KateRendererConfig *rendererConfig()
268     {
269         return m_rendererConfig;
270     }
271 
272     /**
273      * Global script collection
274      */
scriptManager()275     KateScriptManager *scriptManager()
276     {
277         return m_scriptManager;
278     }
279 
280     /**
281      * hl manager
282      * @return hl manager
283      */
hlManager()284     KateHlManager *hlManager()
285     {
286         return m_hlManager;
287     }
288 
289     /**
290      * command manager
291      * @return command manager
292      */
cmdManager()293     KateCmd *cmdManager()
294     {
295         return m_cmdManager;
296     }
297 
298     /**
299      * spell check manager
300      * @return spell check manager
301      */
spellCheckManager()302     KateSpellCheckManager *spellCheckManager()
303     {
304         return m_spellCheckManager;
305     }
306 
307     /**
308      * global instance of the simple word completion mode
309      * @return global instance of the simple word completion mode
310      */
wordCompletionModel()311     KateWordCompletionModel *wordCompletionModel()
312     {
313         return m_wordCompletionModel;
314     }
315 
316     /**
317      * Global instance of the language-aware keyword completion model
318      * @return global instance of the keyword completion model
319      */
keywordCompletionModel()320     KateKeywordCompletionModel *keywordCompletionModel()
321     {
322         return m_keywordCompletionModel;
323     }
324 
325     /**
326      * query for command
327      * @param cmd name of command to query for
328      * @return found command or 0
329      */
330     KTextEditor::Command *queryCommand(const QString &cmd) const override;
331 
332     /**
333      * Get a list of all registered commands.
334      * \return list of all commands
335      */
336     QList<KTextEditor::Command *> commands() const override;
337 
338     /**
339      * Get a list of available commandline strings.
340      * \return commandline strings
341      */
342     QStringList commandList() const override;
343 
344     /**
345      * Copy text to clipboard an remember it in the history
346      * @param text text to copy to clipboard, does nothing if empty!
347      */
348     void copyToClipboard(const QString &text);
349 
350     /**
351      * Clipboard history, filled with text we ever copied
352      * to clipboard via copyToClipboard.
353      */
clipboardHistory()354     const QStringList &clipboardHistory() const
355     {
356         return m_clipboardHistory;
357     }
358 
359     /**
360      * return a list of all registered docs
361      * @return all known documents
362      */
kateDocuments()363     QList<KTextEditor::DocumentPrivate *> kateDocuments()
364     {
365         return m_documents.values();
366     }
367 
368     /**
369      * Dummy main window to be null safe.
370      * @return dummy main window
371      */
dummyMainWindow()372     KTextEditor::MainWindow *dummyMainWindow()
373     {
374         return &m_dummyMainWindow;
375     }
376 
377     /**
378      * @return list of available input mode factories
379      */
inputModeFactories()380     const std::array<std::unique_ptr<KateAbstractInputModeFactory>, KTextEditor::View::ViInputMode + 1> &inputModeFactories()
381     {
382         return m_inputModeFactories;
383     }
384 
385     /**
386      * Search pattern history shared among simple/power search instances.
387      */
388     QStringListModel *searchHistoryModel();
389 
390     /**
391      * Replace pattern history shared among simple/power search instances.
392      */
393     QStringListModel *replaceHistoryModel();
394 
395     /**
396      * Call this function to store the history models to the application config.
397      */
398     void saveSearchReplaceHistoryModels();
399 
400     /**
401      * Returns the variable expansion manager.
402      */
403     KateVariableExpansionManager *variableExpansionManager();
404 
405     /**
406      * Trigger delayed emission of config changed.
407      */
408     void triggerConfigChanged();
409 
410 private Q_SLOTS:
411     /**
412      * Emit configChanged if needed.
413      * Used to bundle emissions.
414      */
415     void emitConfigChanged();
416 
417 Q_SIGNALS:
418     /**
419      * Emitted if the history of clipboard changes via copyToClipboard
420      */
421     void clipboardHistoryChanged();
422 
423 protected:
424     bool eventFilter(QObject *, QEvent *) override;
425 
426 private Q_SLOTS:
427     void updateColorPalette();
428 
429 private:
430     /**
431      * about data (authors and more)
432      */
433     KAboutData m_aboutData;
434 
435     /**
436      * registered docs, map from general to specialized pointer
437      */
438     QHash<KTextEditor::Document *, KTextEditor::DocumentPrivate *> m_documents;
439 
440     /**
441      * registered views
442      */
443     QSet<KTextEditor::ViewPrivate *> m_views;
444 
445     /**
446      * global dirwatch object
447      */
448     KDirWatch *m_dirWatch;
449 
450     /**
451      * mode manager
452      */
453     KateModeManager *m_modeManager;
454 
455     /**
456      * global config
457      */
458     KateGlobalConfig *m_globalConfig;
459 
460     /**
461      * fallback document config
462      */
463     KateDocumentConfig *m_documentConfig;
464 
465     /**
466      * fallback view config
467      */
468     KateViewConfig *m_viewConfig;
469 
470     /**
471      * fallback renderer config
472      */
473     KateRendererConfig *m_rendererConfig;
474 
475     /**
476      * internal commands
477      */
478     QList<KTextEditor::Command *> m_cmds;
479 
480     /**
481      * script manager
482      */
483     KateScriptManager *m_scriptManager;
484 
485     /**
486      * hl manager
487      */
488     KateHlManager *m_hlManager;
489 
490     /**
491      * command manager
492      */
493     KateCmd *m_cmdManager;
494 
495     /**
496      * variable expansion manager
497      */
498     KateVariableExpansionManager *m_variableExpansionManager;
499 
500     /**
501      * spell check manager
502      */
503     KateSpellCheckManager *m_spellCheckManager;
504 
505     /**
506      * global instance of the simple word completion mode
507      */
508     KateWordCompletionModel *m_wordCompletionModel;
509 
510     /**
511      * global instance of the language-specific keyword completion model
512      */
513     KateKeywordCompletionModel *m_keywordCompletionModel;
514 
515     /**
516      * clipboard history
517      */
518     QStringList m_clipboardHistory;
519 
520     /**
521      * Dummy application object to be null safe
522      */
523     KTextEditor::Application m_dummyApplication;
524 
525     /**
526      * access to application
527      */
528     QPointer<KTextEditor::Application> m_application;
529 
530     /**
531      * Dummy main window to be null safe
532      */
533     KTextEditor::MainWindow m_dummyMainWindow;
534 
535     /**
536      * input modes factories
537      * for all input modes in the KTextEditor::View::InputMode we have here an entry
538      */
539     std::array<std::unique_ptr<KateAbstractInputModeFactory>, KTextEditor::View::ViInputMode + 1> m_inputModeFactories;
540 
541     /**
542      * Shared history models for search & replace.
543      */
544     QStringListModel *m_searchHistoryModel;
545     QStringListModel *m_replaceHistoryModel;
546 
547     /**
548      * We collapse configChanged signals to avoid that e.g.
549      * Document/View/Renderer/... updates cause X emitted signals in a row.
550      * This bool tells if we still shall emit a signal in the delayed connected
551      * slot.
552      */
553     bool m_configWasChanged = false;
554 };
555 
556 }
557 
558 #endif
559