1 /*
2   This file is part of Lokalize
3 
4   SPDX-FileCopyrightText: 2007-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 EDITORTAB_H
11 #define EDITORTAB_H
12 
13 #ifdef HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16 
17 #include "pos.h"
18 #include "lokalizesubwindowbase.h"
19 
20 #include <QHash>
21 #include <QMdiSubWindow>
22 #include <QMap>
23 #include <KProcess>
24 
25 namespace Sonnet
26 {
27 class Dialog;
28 }
29 namespace Sonnet
30 {
31 class BackgroundChecker;
32 }
33 
34 #include <kxmlguiclient.h>
35 
36 class KFind;
37 class KReplace;
38 class KActionCategory;
39 
40 class Project;
41 class Catalog;
42 class EditorView;
43 class MergeView;
44 class CatalogView;
45 class MsgCtxtView;
46 class AltTransView;
47 namespace GlossaryNS
48 {
49 class GlossaryView;
50 }
51 
52 
53 
54 struct EditorState {
55 public:
EditorStateEditorState56     EditorState(): entry(0) {}
EditorStateEditorState57     EditorState(const EditorState& s): dockWidgets(s.dockWidgets), filePath(s.filePath), entry(0) {}
~EditorStateEditorState58     ~EditorState() {}
59 
60     QByteArray dockWidgets;
61     QString filePath;
62     QString mergeFilePath;
63     int entry;
64     //int offset;
65 };
66 
67 
68 /**
69  * @short Editor tab
70  *
71  * This class can be called a dispatcher for one message catalog.
72  *
73  * It is accessible via Lokalize.currentEditor() from kross scripts and via
74  * '/ThisIsWhatYouWant/Editor/# : org.kde.Lokalize.Editor' from qdbusviewer
75  *
76  * @author Nick Shaforostoff <shafff@ukr.net>
77  */
78 class EditorTab: public LokalizeSubwindowBase2
79 {
80     Q_OBJECT
81     Q_CLASSINFO("D-Bus Interface", "org.kde.Lokalize.Editor")
82     //qdbuscpp2xml -m -s editortab.h -o org.kde.lokalize.Editor.xml
83 #define qdbuscpp2xml
84 
85 public:
86     explicit EditorTab(QWidget* parent, bool valid = true);
87     ~EditorTab() override;
88 
89 
90     //interface for LokalizeMainWindow
91     void hideDocks() override;
92     void showDocks() override;
93     QString currentFilePath() override;
94     void setFullPathShown(bool);
95     void setProperCaption(QString, bool); //reimpl to remove ' - Lokalize'
96 public Q_SLOTS:
97     void setProperFocus();
98 public:
99     bool queryClose() override;
100     EditorState state();
guiClient()101     KXMLGUIClient* guiClient() override
102     {
103         return (KXMLGUIClient*)this;
104     }
105     QString dbusObjectPath();
dbusId()106     int dbusId()
107     {
108         return m_dbusId;
109     }
adaptor()110     QObject* adaptor()
111     {
112         return m_adaptor;
113     }
114 
115     //wrapper for cmdline handling
116     void mergeOpen(QString mergeFilePath);
117 
118     bool fileOpen(QString filePath = QString(), QString suggestedDirPath = QString(), QMap<QString, QMdiSubWindow*> openedFiles = QMap<QString, QMdiSubWindow*>(), bool silent = false);
119 public Q_SLOTS:
120     //for undo/redo, views
121     void gotoEntry(DocPosition pos);
122     void gotoEntry(DocPosition pos, int selection);
123 #ifdef qdbuscpp2xml
gotoEntry(int entry)124     Q_SCRIPTABLE void gotoEntry(int entry)
125     {
126         gotoEntry(DocPosition(entry));
127     }
gotoEntryForm(int entry,int form)128     Q_SCRIPTABLE void gotoEntryForm(int entry, int form)
129     {
130         gotoEntry(DocPosition(entry, form));
131     }
gotoEntryFormOffset(int entry,int form,int offset)132     Q_SCRIPTABLE void gotoEntryFormOffset(int entry, int form, int offset)
133     {
134         gotoEntry(DocPosition(entry, form, offset));
135     }
gotoEntryFormOffsetSelection(int entry,int form,int offset,int selection)136     Q_SCRIPTABLE void gotoEntryFormOffsetSelection(int entry, int form, int offset, int selection)
137     {
138         gotoEntry(DocPosition(entry, form, offset), selection);
139     }
140 
141     Q_SCRIPTABLE QString currentEntryId();
currentEntry()142     Q_SCRIPTABLE int currentEntry()
143     {
144         return m_currentPos.entry;
145     }
currentForm()146     Q_SCRIPTABLE int currentForm()
147     {
148         return m_currentPos.form;
149     }
150     Q_SCRIPTABLE QString selectionInTarget();
151     Q_SCRIPTABLE QString selectionInSource();
152 
153     Q_SCRIPTABLE void setEntryFilteredOut(int entry, bool filteredOut);
154     Q_SCRIPTABLE void setEntriesFilteredOut(bool filteredOut);
155 
156     Q_SCRIPTABLE int entryCount();
157     Q_SCRIPTABLE QString entrySource(int entry, int form);
158     Q_SCRIPTABLE QString entryTarget(int entry, int form);
159     Q_SCRIPTABLE void setEntryTarget(int entry, int form, const QString& content);
160     Q_SCRIPTABLE int entryPluralFormCount(int entry);
161     Q_SCRIPTABLE bool entryReady(int entry);
162     Q_SCRIPTABLE void addEntryNote(int entry, const QString& note);
163     Q_SCRIPTABLE void addTemporaryEntryNote(int entry, const QString& note);
164 
165     Q_SCRIPTABLE void addAlternateTranslation(int entry, const QString& translation);
166     Q_SCRIPTABLE void addTemporaryAlternateTranslation(int entry, const QString& translation);
167 
currentFile()168     Q_SCRIPTABLE QString currentFile()
169     {
170         return currentFilePath();
171     }
172     Q_SCRIPTABLE QByteArray currentFileContents();
173     Q_SCRIPTABLE QString sourceLangCode();
174     Q_SCRIPTABLE QString targetLangCode();
175 
176     Q_SCRIPTABLE void attachAlternateTranslationFile(const QString& path);
openSyncSource(QString path)177     Q_SCRIPTABLE void openSyncSource(QString path)
178     {
179         mergeOpen(path);
180     }
181     Q_SCRIPTABLE void reloadFile();
182 #endif
183     Q_SCRIPTABLE bool saveFile(const QString& filePath = QString());
184     Q_SCRIPTABLE bool saveFileAs(const QString& defaultPath = QString());
close()185     Q_SCRIPTABLE void close()
186     {
187         return parent()->deleteLater();
188     }
189     Q_SCRIPTABLE void gotoNextUnfiltered();
190     Q_SCRIPTABLE void gotoPrevUnfiltered();
191     Q_SCRIPTABLE void gotoFirstUnfiltered();
192     Q_SCRIPTABLE void gotoLastUnfiltered();
193     Q_SCRIPTABLE void gotoNext();
194     Q_SCRIPTABLE void gotoPrev();
195     Q_SCRIPTABLE void gotoFirst();
196     Q_SCRIPTABLE void gotoLast();
197 
198     Q_SCRIPTABLE void mergeIntoOpenDocument();
199 
200     Q_SCRIPTABLE bool findEntryBySourceContext(const QString& source, const QString& ctxt);
201 
isValid()202     Q_SCRIPTABLE bool isValid()
203     {
204         return m_valid;
205     }
setSrcFileOpenRequestAccepted(bool a)206     Q_SCRIPTABLE void setSrcFileOpenRequestAccepted(bool a)
207     {
208         m_srcFileOpenRequestAccepted = a;
209     }
210 
211 private Q_SLOTS:
212     void highlightFound(const QString &, int, int); //for find/replace
213     void highlightFound_(const QString &, int, int); //for find/replace
214 
215     void lookupSelectionInTranslationMemory();
216 
217     //statusbar indication
218     void numberOfFuzziesChanged();
219     void numberOfUntranslatedChanged();
220     //fuzzy, untr [statusbar] indication
221     void msgStrChanged();
222     //modif [caption] indication
223     void setModificationSign();
224     void updateCaptionPath();
225 
226     //gui
227     void switchForm(int);
228 
229     void undo();
230     void redo();
231     void findNext();
232     void findPrev();
233     void find();
234 
235     void replace();
236     void replaceNext();//internal
237     void doReplace(const QString&, int, int, int); //internal
238     void cleanupReplace();//internal
239 
240 //     void selectAll();
241 //     void deselectAll();
242 //     void clear();
243 //     void search2msgstr();
244 //     void plural2msgstr();
245 
246     void gotoEntry();
247 
248     void gotoPrevFuzzyUntr();
249     bool gotoNextFuzzyUntr(const DocPosition& pos);
250     bool gotoNextFuzzyUntr();
251     void gotoNextFuzzy();
252     void gotoPrevFuzzy();
253     void gotoNextUntranslated();
254     void gotoPrevUntranslated();
255 
256     void toggleApprovementGotoNextFuzzyUntr();
257     void setApproveActionTitle();
258 
259 
260     void spellcheck();
261     void spellcheckNext();
262     void spellcheckShow(const QString&, int);
263     void spellcheckReplace(QString, int, const QString&);
264     void spellcheckStop();
265     void spellcheckCancel();
266 
267     void gotoNextBookmark();
268     void gotoPrevBookmark();
269 
270     void displayWordCount();
271     void clearTranslatedEntries();
272     void launchPology();
273 
274     void openPhasesWindow();
275 
276     void defineNewTerm();
277 
278     void initLater();
279     void showStatesMenu();
280     void setState(QAction*);
281     void dispatchSrcFileOpenRequest(const QString& srcPath, int line);
282     void indexWordsForCompletion();
283 
284     void fileAutoSaveFailedWarning(const QString&);
285 
286     void pologyHasFinished(int exitCode, QProcess::ExitStatus exitStatus);
287 
288 protected:
289     void paintEvent(QPaintEvent* event) override;
290 
291 private:
292     void setupAccel();
293     void setupActions();
294     void setupStatusBar();
295 
296     void findNext(const DocPosition& startingPos);
297     void replaceNext(const DocPosition&);
298 
299 private:
300     Project* m_project;
301     Catalog* m_catalog;
302 
303     EditorView* m_view;
304     QAction* m_approveAndGoAction;
305     QAction* m_approveAction;
306     QAction* m_stateAction;
307 
308     KProcess* m_pologyProcess;
309     bool m_pologyProcessInProgress;
310 
311     DocPosition m_currentPos;
312     DocPosition _searchingPos; //for find/replace
313     DocPosition _replacingPos;
314     DocPosition _spellcheckPos;
315     DocPosition _spellcheckStartPos;
316 
317     Sonnet::BackgroundChecker* m_sonnetChecker;
318     Sonnet::Dialog* m_sonnetDialog;
319     int m_spellcheckStartUndoIndex;
320     bool m_spellcheckStop: 1;
321 
322     bool m_currentIsApproved: 1; //for statusbar animation
323     bool m_currentIsUntr: 1; //for statusbar animation
324 
325     bool m_fullPathShown: 1;
326 
327     bool m_doReplaceCalled: 1; //used to prevent non-clean catalog status
328     KFind* m_find;
329     KReplace* m_replace;
330 
331     //BEGIN views
332     MergeView* m_syncView;
333     MergeView* m_syncViewSecondary;
334     CatalogView* m_transUnitsView;
335     MsgCtxtView* m_notesView;
336     AltTransView* m_altTransView;
337     //END views
338 
339 
340     QString _captionPath;
341 
342     bool m_srcFileOpenRequestAccepted;
343 
344     //BEGIN dbus
345     bool m_valid;
346     QObject* m_adaptor;
347     int m_dbusId;
348     static QList<int> ids;
349     //END dbus
350 
351 Q_SIGNALS:
352     void tmLookupRequested(DocPosition::Part, const QString&);
353     void tmLookupRequested(const QString& source, const QString& target);
354 
355     Q_SCRIPTABLE void srcFileOpenRequested(const QString& srcPath, int line);
356 
357     void fileOpenRequested(const QString& filePath, const QString& str, const QString& ctxt, const bool setAsActive);
358 
359     //emitted when mainwindow is closed or another file is opened
360     void fileClosed();
361     Q_SCRIPTABLE void fileClosed(const QString& path);
362     Q_SCRIPTABLE void fileSaved(const QString& path);
363     Q_SCRIPTABLE void fileAboutToBeClosed();//old catalog is still accessible
364     Q_SCRIPTABLE void fileOpened();
365 
366     Q_SCRIPTABLE void entryDisplayed();
367     void signalNewEntryDisplayed(const DocPosition&);
368     void signalEntryWithMergeDisplayed(bool, const DocPosition&);
369     void signalFirstDisplayed(bool);
370     void signalLastDisplayed(bool);
371 
372     void signalEquivTranslatedEntryDisplayed(bool);
373     void signalApprovedEntryDisplayed(bool);
374 
375     void signalFuzzyEntryDisplayed(bool);
376     void signalPriorFuzzyAvailable(bool);
377     void signalNextFuzzyAvailable(bool);
378 
379     void signalPriorUntranslatedAvailable(bool);
380     void signalNextUntranslatedAvailable(bool);
381 
382     void signalPriorFuzzyOrUntrAvailable(bool);
383     void signalNextFuzzyOrUntrAvailable(bool);
384 
385     // merge mode signals gone to the view
386     //NOTE move these to catalog tree view?
387     void signalPriorBookmarkAvailable(bool);
388     void signalNextBookmarkAvailable(bool);
389     void signalBookmarkDisplayed(bool);
390 
391     Q_SCRIPTABLE void xliffFileOpened(bool);
392 };
393 
394 #endif
395