File indexing completed on 2024-04-28 17:01:41

0001 /*
0002 SPDX-FileCopyrightText: 2001-2003 John Firebaugh <jfirebaugh@kde.org>
0003 SPDX-FileCopyrightText: 2001-2005,2009 Otto Bruggeman <bruggie@gmail.com>
0004 SPDX-FileCopyrightText: 2007-2008 Kevin Kofler <kevin.kofler@chello.at>
0005 SPDX-FileCopyrightText: 2012 Jean -Nicolas Artaud <jeannicolasartaud@gmail.com>
0006 
0007 SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #ifndef KOMPAREDIFF2_KOMPAREMODELLIST_H
0011 #define KOMPAREDIFF2_KOMPAREMODELLIST_H
0012 
0013 #include <QObject>
0014 
0015 #include "diffmodel.h"
0016 #include "diffmodellist.h"
0017 #include "kompare.h"
0018 #include "komparediff2_export.h"
0019 
0020 class QAction;
0021 class QTemporaryFile;
0022 class QTextCodec;
0023 class KActionCollection;
0024 
0025 class DiffSettings;
0026 class KompareProcess;
0027 
0028 namespace Diff2
0029 {
0030 
0031 /**
0032  * @class KompareModelList komparemodellist.h <KompareDiff2/KompareModelList>
0033  *
0034  * KompareModelList
0035  */
0036 class KOMPAREDIFF2_EXPORT KompareModelList : public QObject
0037 {
0038     Q_OBJECT
0039 public:
0040 #if KOMPAREDIFF2_ENABLE_DEPRECATED_SINCE(5, 4)
0041     /// @deprecated Since 5.4, use the overload without widgetForKIO arg, widgetForKIO is unused
0042     KOMPAREDIFF2_DEPRECATED_VERSION(5, 4, "Use KompareModelList constructor without widgetForKIO arg instead")
0043     KompareModelList(DiffSettings* diffSettings, QWidget* widgetForKIO, QObject* parent, const char* name = nullptr, bool supportReadWrite = true);
0044 #endif
0045     /// @since 5.4
0046     KompareModelList(DiffSettings* diffSettings, QObject* parent, const char* name = nullptr, bool supportReadWrite = true);
0047     ~KompareModelList() override;
0048 
0049 public:
0050     void refresh();
0051     // Swap source with destination and show differences
0052     void swap();
0053 
0054     /* Comparing methods */
0055     bool compare();
0056 
0057     bool compare(Kompare::Mode);
0058 
0059     bool openDiff(const QString& diff);
0060 
0061     bool openFileAndDiff();
0062     bool openDirAndDiff();
0063 
0064     bool saveDiff(const QString& url, QString directory, DiffSettings* diffSettings);
0065     bool saveAll();
0066 
0067     bool saveDestination(DiffModel* model);
0068 
0069     void setEncoding(const QString& encoding);
0070 
0071     void setReadWrite(bool isReadWrite);
0072     bool isReadWrite() const;
0073 
0074     QString recreateDiff() const;
0075 
0076     // This parses the difflines and creates new models
0077     int parseDiffOutput(const QString& diff);
0078 
0079     // This open the difflines after parsing them
0080     bool parseAndOpenDiff(const QString& diff);
0081 
0082     // Call this to emit the signals to the rest of the "world" to show the diff
0083     void show();
0084 
0085     // This will blend the original URL (dir or file) into the diffmodel,
0086     // this is like patching but with a twist
0087     bool blendOriginalIntoModelList(const QString& localURL);
0088 
0089     // This mode() method is superfluous now so FIXME
0090     enum Kompare::Mode    mode()   const { return m_info->mode; };
0091     const DiffModelList*  models() const { return m_models; };
0092 
0093     KActionCollection* actionCollection() const;
0094     int modelCount() const;
0095     int differenceCount() const;
0096     int appliedCount() const;
0097 
0098     const DiffModel* modelAt(int i) const { return m_models->at(i); };
0099     DiffModel* modelAt(int i) { return m_models->at(i); };
0100     int              findModel(DiffModel* model) const { return m_models->indexOf(model); };
0101 
0102     bool hasUnsavedChanges() const;
0103 
0104     int currentModel() const      { return m_models->indexOf(m_selectedModel); };
0105     int currentDifference() const { return m_selectedModel ? m_selectedModel->findDifference(m_selectedDifference) : -1; };
0106 
0107     const DiffModel* selectedModel() const       { return m_selectedModel; };
0108     const Difference* selectedDifference() const { return m_selectedDifference; };
0109 
0110     void clear();
0111 
0112 private:
0113     Diff2::DiffModel* firstModel();
0114     Diff2::DiffModel* lastModel();
0115     Diff2::DiffModel* prevModel();
0116     Diff2::DiffModel* nextModel();
0117 
0118     bool setSelectedModel(Diff2::DiffModel* model);
0119 
0120     void updateModelListActions();
0121 
0122 protected:
0123     bool blendFile(DiffModel* model, const QString& lines);
0124 
0125 Q_SIGNALS:
0126     void status(Kompare::Status status);
0127     void setStatusBarModelInfo(int modelIndex, int differenceIndex, int modelCount, int differenceCount, int appliedCount);
0128     void error(QString error);
0129     void modelsChanged(const Diff2::DiffModelList* models);
0130     void setSelection(const Diff2::DiffModel* model, const Diff2::Difference* diff);
0131     void setSelection(const Diff2::Difference* diff);
0132     void applyDifference(bool apply);
0133     void applyAllDifferences(bool apply);
0134     void applyDifference(const Diff2::Difference* diff, bool apply);
0135     void diffString(const QString&);
0136     void updateActions();
0137 
0138 public Q_SLOTS:
0139     void slotSelectionChanged(const Diff2::DiffModel* model, const Diff2::Difference* diff);
0140     void slotSelectionChanged(const Diff2::Difference* diff);
0141 
0142     void slotApplyDifference(bool apply);
0143     void slotApplyAllDifferences(bool apply);
0144     void slotPreviousModel();
0145     void slotNextModel();
0146     void slotPreviousDifference();
0147     void slotNextDifference();
0148 
0149     void slotKompareInfo(struct Kompare::Info*);
0150 
0151 protected Q_SLOTS:
0152     void slotDiffProcessFinished(bool success);
0153     void slotWriteDiffOutput(bool success);
0154 
0155     void slotActionApplyDifference();
0156     void slotActionUnApplyDifference();
0157     void slotActionApplyAllDifferences();
0158     void slotActionUnapplyAllDifferences();
0159 
0160     /** Save the currently selected destination in a multi-file diff,
0161         or the single destination if a single file diff. */
0162     void slotSaveDestination();
0163 
0164 private Q_SLOTS:
0165     void slotDirectoryChanged(const QString&);
0166     void slotFileChanged(const QString&);
0167 
0168 private: // Helper methods
0169     bool isDirectory(const QString& url) const;
0170     bool isDiff(const QString& mimetype) const;
0171     QString readFile(const QString& fileName);
0172 
0173     bool hasPrevModel() const;
0174     bool hasNextModel() const;
0175     bool hasPrevDiff() const;
0176     bool hasNextDiff() const;
0177 
0178     QStringList split(const QString& diff);
0179     void setDepthAndApplied();
0180 
0181 private: // ### an exported class without a d pointer? Really? What about BC?
0182     QTemporaryFile*       m_diffTemp;
0183     QUrl                  m_diffURL;
0184 
0185     KompareProcess*       m_diffProcess;
0186 
0187     DiffSettings*         m_diffSettings;
0188 
0189     DiffModelList*        m_models;
0190 
0191     DiffModel*            m_selectedModel;
0192     Difference*           m_selectedDifference;
0193 
0194     int                   m_modelIndex;
0195 
0196     struct Kompare::Info* m_info;
0197 
0198     KActionCollection*    m_actionCollection;
0199     QAction*              m_applyDifference;
0200     QAction*              m_unApplyDifference;
0201     QAction*              m_applyAll;
0202     QAction*              m_unapplyAll;
0203     QAction*              m_previousFile;
0204     QAction*              m_nextFile;
0205     QAction*              m_previousDifference;
0206     QAction*              m_nextDifference;
0207 
0208     QAction*              m_save;
0209 
0210     QString               m_encoding;
0211     QTextCodec*           m_textCodec;
0212 
0213 #if KOMPAREDIFF2_BUILD_DEPRECATED_SINCE(5, 4)
0214     QT_WARNING_PUSH
0215     QT_WARNING_DISABLE_CLANG("-Wunused-private-field")
0216     // Unused, kept for ABI compatibility
0217     QWidget*              m_widgetForKIO;
0218     QT_WARNING_POP
0219  #endif
0220 
0221     bool                  m_isReadWrite;
0222 };
0223 
0224 } // End of namespace Diff2
0225 
0226 #endif