1 /*
2   This file is part of Lokalize
3 
4   SPDX-FileCopyrightText: 2008-2014 Nick Shaforostoff <shafff@ukr.net>
5   SPDX-FileCopyrightText: 2018-2019 Simon Depiets <sdepiets@gmail.com>
6 
7   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
8 */
9 
10 #ifndef LOKALIZEMAINWINDOW_H
11 #define LOKALIZEMAINWINDOW_H
12 
13 #include "pos.h"
14 
15 #include <kxmlguiwindow.h>
16 #include <kconfiggroup.h>
17 
18 #include <QPointer>
19 #include <QMap>
20 #include <QUrl>
21 #include <QMdiArea>
22 #include <QDBusObjectPath>
23 
24 class QLabel;
25 class QMdiSubWindow;
26 class QMdiArea;
27 class QActionGroup;
28 class LokalizeMdiArea;
29 class KRecentFilesAction;
30 class EditorTab;
31 class MultiEditorAdaptor;
32 class FileSearchTab;
33 namespace TM
34 {
35 class TMTab;
36 }
37 
38 /**
39  * @short Lokalize MDI (tabbed) window.
40  *
41  * Sets up actions, and maintains their connection with active subwindow via ActionProxy
42  * As such, it handles the menus, toolbars, and status bars.
43  *
44  * It is known as Lokalize in kross scripts and as
45  * '/ThisIsWhatYouWant : org.kde.Lokalize.MainWindow' in qdbusviewer
46  *
47  * @author Nick Shaforostoff <shafff@ukr.net>
48  */
49 class LokalizeMainWindow: public KXmlGuiWindow
50 {
51     Q_OBJECT
52     Q_CLASSINFO("D-Bus Interface", "org.kde.Lokalize.MainWindow")
53     //qdbuscpp2xml -m -s lokalizemainwindow.h -o org.kde.lokalize.MainWindow.xml
54 
55 public:
56     LokalizeMainWindow();
57     ~LokalizeMainWindow() override;
58 
59 protected:
60     void saveProjectState(KConfigGroup&);
61     void saveProperties(KConfigGroup& stateGroup) override;
62     bool queryClose() override;
63     void readProperties(const KConfigGroup& stateGroup) override;
64     void registerDBusAdaptor();
65     void setupActions();
66 
67 private Q_SLOTS:
68     void slotSubWindowActivated(QMdiSubWindow*);
69     void initLater();
70     void applyToBeActiveSubWindow();
71     void projectLoaded();
72     void projectSettingsChanged();
73 
74     void editorClosed(QObject* obj);
75     void resetMultiEditorAdaptor();
76 
openProject(const QUrl & url)77     void openProject(const QUrl& url)
78     {
79         openProject(url.toLocalFile());   //convenience overload for recent projects action
80     }
openProject()81     void openProject()
82     {
83         openProject(QString());
84     }
85 
86 
87 public Q_SLOTS:
88     /**
89      * Adds new editor with @param path loaded,
90      * or just activates already existing editor with this file.
91      */
92     Q_SCRIPTABLE int openFileInEditor(const QString& path);
93     Q_SCRIPTABLE int openFileInEditorAt(const QString& path, const QString& source, const QString& ctxt);
94     int lookupInTranslationMemory(DocPosition::Part part, const QString& text);
95     Q_SCRIPTABLE int lookupInTranslationMemory(const QString& source, const QString& target);
96     Q_SCRIPTABLE int showTranslationMemory();
97     Q_SCRIPTABLE void showProjectOverview();
98     Q_SCRIPTABLE QObject* projectOverview();
99 
100     Q_SCRIPTABLE bool closeProject();
101     Q_SCRIPTABLE void openProject(QString path);
102     Q_SCRIPTABLE QString currentProject();
103 
104     /// @returns 0 if current tab is not of Editor type
105     Q_SCRIPTABLE QObject* activeEditor();
106 
107     /// @returns editor with @param path loaded or 0 if there is no such editor.
108     Q_SCRIPTABLE QObject* editorForFile(const QString& path);
109     /**
110      * # part of editor DBus path: /ThisIsWhatYouWant/Editor/#
111      * @returns -1 if there is no such editor
112      */
113     Q_SCRIPTABLE int editorIndexForFile(const QString& path);
114 
115     /// @returns Unix process ID
116     Q_SCRIPTABLE int pid();
117 
118     /// @returns smth like 'org.kde.lokalize-####' where #### is pid()
119     Q_SCRIPTABLE QString dbusName();
120 
121     Q_SCRIPTABLE void busyCursor(bool busy);
122     //Q_SCRIPTABLE void processEvents();
123 
124     //returns 0 if error
125     EditorTab* fileOpen_(QString url, const bool setAsActive);
126     EditorTab* fileOpen(QString url = QString(), int entry = 0, bool setAsActive = true, const QString& mergeFile = QString(), bool silent = false);
127     EditorTab* fileOpen(const QString& url, const QString& source, const QString& ctxt, const bool setAsActive);
128     EditorTab* fileOpen(const QString& url, DocPosition docPos, int selection, const bool setAsActive);
fileOpen(const QUrl & url)129     EditorTab* fileOpen(const QUrl& url)
130     {
131         return fileOpen(url.toLocalFile(), 0, true);
132     }
133     TM::TMTab* showTM();
134     FileSearchTab* showFileSearch(bool activate = true);
135     void fileSearchNext();
136     void addFilesToSearch(const QStringList&);
137 
138     void widgetTextCapture();
139 Q_SIGNALS:
140     Q_SCRIPTABLE void editorAdded();
141     Q_SCRIPTABLE void editorActivated();
142 
143 private:
144     LokalizeMdiArea* m_mdiArea;
145     QPointer<QMdiSubWindow> m_prevSubWindow;
146     QPointer<QMdiSubWindow> m_projectSubWindow;
147     QPointer<QMdiSubWindow> m_translationMemorySubWindow;
148     QPointer<QMdiSubWindow> m_fileSearchSubWindow;
149     QPointer<QMdiSubWindow> m_toBeActiveSubWindow;//used during session restore
150 
151     QActionGroup* m_editorActions;
152     QActionGroup* m_managerActions;
153     KRecentFilesAction* m_openRecentFileAction;
154     KRecentFilesAction* m_openRecentProjectAction;
155     QVector<QLabel*> m_statusBarLabels;
156 
157     QByteArray m_lastEditorState;
158 
159     //used for kross API
160     EditorTab* m_spareEditor;
161     MultiEditorAdaptor* m_multiEditorAdaptor;
162 
163     //using QPointer switches it.value() to 0 before we get to destroyed() handler
164     //typedef QMap<QUrl, QPointer<QMdiSubWindow> > FileToEditor;
165     typedef QMap<QString, QMdiSubWindow*> FileToEditor;
166     FileToEditor m_fileToEditor;
167 };
168 
169 class LokalizeMdiArea: public QMdiArea
170 {
171     Q_OBJECT
172 public Q_SLOTS:
173     void activateNextSubWindow();
174     void activatePreviousSubWindow();
175 };
176 
177 class DelayedFileOpener: public QObject
178 {
179     Q_OBJECT
180 public:
181     DelayedFileOpener(const QVector<QString>& urls, LokalizeMainWindow* lmw);
182 
183 private Q_SLOTS:
184     void doOpen();
185 
186 private:
187     QVector<QString> m_urls;
188     LokalizeMainWindow* m_lmw;
189 };
190 
191 
192 #endif
193