1 /*
2     SPDX-FileCopyrightText: 2001-2005, 2009 Otto Bruggeman <bruggie@gmail.com>
3     SPDX-FileCopyrightText: 2001-2003 John Firebaugh <jfirebaugh@kde.org>
4     SPDX-FileCopyrightText: 2004 Jeff Snyder <jeff@caffeinated.me.uk>
5     SPDX-FileCopyrightText: 2007-2011 Kevin Kofler <kevin.kofler@chello.at>
6 
7     SPDX-License-Identifier: GPL-2.0-or-later
8 */
9 
10 #ifndef KOMPAREPART_H
11 #define KOMPAREPART_H
12 
13 #include <komparepartdebug.h>
14 #include "kompareinterface.h"
15 // libkomparediff2
16 #include <libkomparediff2/kompare.h>
17 // KF
18 #include <kparts_version.h>
19 #include <KParts/ReadWritePart>
20 // Qt
21 #include <QVariantList>
22 
23 class QAction;
24 class QPrinter;
25 class QUrl;
26 class QWidget;
27 
28 class KPluginMetaData;
29 
30 namespace Diff2 {
31 class Difference;
32 class DiffModel;
33 class DiffModelList;
34 class KompareModelList;
35 }
36 class DiffSettings;
37 class ViewSettings;
38 class KompareSplitter;
39 class KompareView;
40 
41 /**
42  * This is a "Part".  It does all the real work in a KPart
43  * application.
44  *
45  * @short Main Part
46  * @author John Firebaugh <jfirebaugh@kde.org>
47  * @author Otto Bruggeman <bruggie@home.nl>
48  * @version 0.3
49  */
50 class KomparePart : public KParts::ReadWritePart,
51                     public KompareInterface
52 {
53     Q_OBJECT
54     Q_INTERFACES(KompareInterface)
55 public:
56     enum Modus {
57         ReadOnlyModus = 0,
58         ReadWriteModus = 1
59     };
60 
61     /**
62     * Default constructor
63     */
64     KomparePart(QWidget* parentWidget, QObject* parent, const KPluginMetaData& metaData, Modus modus);
65 
66     /**
67     * Destructor
68     */
69     ~KomparePart() override;
70 
71     // Sessionmanagement stuff, added to the kompare iface
72     // because they are not in the Part class where they belong
73     // Should be added when bic changes are allowed again (kde 4.0)
74     int readProperties(KConfig* config) override;
75     int saveProperties(KConfig* config) override;
76     // this one is called when the shell_app is about to close.
77     // we need it now to save the properties of the part when apps don't (can't)
78     // use the readProperties and saveProperties methods
79     bool queryClose() override;
80     void setReadWrite(bool readWrite) override;
81 
82     // Do we really want to expose this ???
model()83     const Diff2::KompareModelList* model() const { return m_modelList; };
84 
85 public:
86     // Reimplemented from the KompareInterface
87     /**
88      * Open and parse the diff file at diffUrl.
89      */
90     bool openDiff(const QUrl& diffUrl) override;
91 
92     /** Added on request of Harald Fernengel */
93     bool openDiff(const QString& diffOutput) override;
94 
95     /** Open and parse the diff3 file at diff3Url */
96     bool openDiff3(const QUrl& diff3URL) override;
97 
98     /** Open and parse the file diff3Output with the output of diff3 */
99     bool openDiff3(const QString& diff3Output) override;
100 
101     /** Compare, with diff, source with destination */
102     void compare(const QUrl& sourceFile, const QUrl& destinationFile) override;
103 
104     /** Compare a Source file to a custom Destination string */
105     void compareFileString(const QUrl& sourceFile, const QString& destination) override;
106 
107     /** Compare a custom Source string to a Destination file */
108     void compareStringFile(const QString& source, const QUrl& destinationFile) override;
109 
110     /** Compare, with diff, source with destination */
111     void compareFiles(const QUrl& sourceFile, const QUrl& destinationFile) override;
112 
113     /** Compare, with diff, source with destination */
114     void compareDirs(const QUrl& sourceDir, const QUrl& destinationDir) override;
115 
116     /** Compare, with diff3, originalFile with changedFile1 and changedFile2 */
117     void compare3Files(const QUrl& originalFile, const QUrl& changedFile1, const QUrl& changedFile2) override;
118 
119     /** This will show the file and the file with the diff applied */
120     void openFileAndDiff(const QUrl& file, const QUrl& diffFile) override;
121 
122     /** This will show the directory and the directory with the diff applied */
123     void openDirAndDiff(const QUrl& dir,  const QUrl& diffFile) override;
124 
125     /** Reimplementing this because this one knows more about the real part then the interface */
126     void setEncoding(const QString& encoding) override;
127 
128     // This is the interpart interface, it is signal and slot based so no "real" interface here
129     // All you have to do is connect the parts from your application.
130     // These just point to their counterpart in the KompareModelList or get called from their
131     // counterpart in KompareModelList.
132 Q_SIGNALS:
133     void modelsChanged(const Diff2::DiffModelList* models);
134 
135     void setSelection(const Diff2::DiffModel* model, const Diff2::Difference* diff);
136     void setSelection(const Diff2::Difference* diff);
137 
138     void selectionChanged(const Diff2::DiffModel* model, const Diff2::Difference* diff);
139     void selectionChanged(const Diff2::Difference* diff);
140 
141     void applyDifference(bool apply);
142     void applyAllDifferences(bool apply);
143     void applyDifference(const Diff2::Difference*, bool apply);
144 
145     void configChanged();
146 
147     /*
148     ** This is emitted when a difference is clicked in the kompare view. You can connect to
149     ** it so you can use it to jump to this particular line in the editor in your app.
150     */
151     void differenceClicked(int lineNumber);
152 
153     // Stuff that can probably be removed by putting it in the part where it belongs in my opinion
154 public Q_SLOTS:
155     /** Save all destinations. */
156     bool saveAll();
157 
158     /** Save the results of a comparison as a diff file. */
159     void saveDiff();
160 
161     /** To enable printing, the part has the only interesting printable content so putting it here */
162     void slotFilePrint();
163     void slotFilePrintPreview();
164 
165 Q_SIGNALS:
166     void appliedChanged();
167     void diffURLChanged();
168     void kompareInfo(Kompare::Info* info);
169     void setStatusBarModelInfo(int modelIndex, int differenceIndex, int modelCount, int differenceCount, int appliedCount);
170 //     void setStatusBarText( const QString& text );
171     void diffString(const QString&);
172 
173 protected:
174     /**
175      * This is the method that gets called when the file is opened,
176      * when using openURL( const QUrl& ) or in our case also openDiff( const QUrl& );
177      * return true when everything went ok, false if there were problems
178      */
179     bool openFile() override;
180     // ... Uhm we return true without saving ???
saveFile()181     bool saveFile() override { return true; };
182 
183 protected Q_SLOTS:
184     void slotSetStatus(Kompare::Status status);
185     void slotShowError(const QString& error);
186 
187     void slotSwap();
188     void slotShowDiffstats();
189     void slotRefreshDiff();
190     void optionsPreferences();
191 
192     void updateActions();
193     void updateCaption();
194     void updateStatus();
195     void compareAndUpdateAll();
196 
197     void slotPaintRequested(QPrinter*);
198 
199 private:
200     void cleanUpTemporaryFiles();
201     void setupActions(Modus modus);
202     bool isDirectory(const QUrl& url);
203     // FIXME (like in cpp file not urgent) Replace with enum, cant find a proper
204     // name now but it is private anyway so can not be used from outside
205     bool fetchURL(const QUrl& url, bool isSource);
206 
207 private Q_SLOTS:
208     void onContextMenuRequested(const QPoint& pos);
209 
210 private:
211     // Uhm why were these static again ???
212     // Ah yes, so multiple instances of kompare use the
213     // same settings after one of them changes them
214     static ViewSettings* m_viewSettings;
215     static DiffSettings* m_diffSettings;
216 
217     Diff2::KompareModelList* m_modelList;
218 
219     KompareView*             m_view;
220     KompareSplitter*         m_splitter;
221 
222     QAction*                 m_saveAll;
223     QAction*                 m_saveDiff;
224     QAction*                 m_swap;
225     QAction*                 m_diffStats;
226     QAction*                 m_diffRefresh;
227     QAction*                 m_print;
228     QAction*                 m_printPreview;
229 
230     struct Kompare::Info     m_info;
231 };
232 
233 #endif // KOMPAREPART_H
234