File indexing completed on 2024-04-28 04:38:53

0001 /*
0002     SPDX-FileCopyrightText: 1999-2001 Bernd Gehrmann <bernd@kdevelop.org>
0003     SPDX-FileCopyrightText: 2008 Hamish Rodda <rodda@kde.org>
0004     SPDX-FileCopyrightText: 2010 Silvère Lestang <silvere.lestang@gmail.com>
0005     SPDX-FileCopyrightText: 2010 Julien Desgats <julien.desgats@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #ifndef KDEVPLATFORM_PLUGIN_GREPJOB_H
0011 #define KDEVPLATFORM_PLUGIN_GREPJOB_H
0012 
0013 #include <QList>
0014 #include <QPointer>
0015 #include <QRegExp>
0016 #include <QString>
0017 #include <QUrl>
0018 
0019 #include <KJob>
0020 
0021 #include <interfaces/istatus.h>
0022 
0023 #include "grepoutputmodel.h"
0024 
0025 namespace KDevelop
0026 {
0027     class IProject;
0028 }
0029 
0030 class GrepFindFilesThread;
0031 class GrepViewPlugin;
0032 class FindReplaceTest; //FIXME: this is useful only for tests
0033 
0034 class QDebug;
0035 
0036 struct GrepJobSettings
0037 {
0038     bool fromHistory = false;
0039 
0040     bool projectFilesOnly = false;
0041     bool caseSensitive = true;
0042     bool regexp = true;
0043 
0044     int depth = -1;
0045 
0046     QString pattern;
0047     QString searchTemplate;
0048     QString replacementTemplate;
0049     QString files;
0050     QString exclude;
0051     QString searchPaths;
0052 };
0053 
0054 Q_DECLARE_TYPEINFO(GrepJobSettings, Q_MOVABLE_TYPE);
0055 
0056 QDebug operator<<(QDebug debug, const GrepJobSettings&);
0057 
0058 class GrepJob : public KJob, public KDevelop::IStatus
0059 {
0060     Q_OBJECT
0061     Q_INTERFACES( KDevelop::IStatus )
0062 
0063     friend class GrepViewPlugin;
0064     friend class FindReplaceTest;
0065 
0066 private:
0067     ///Job can only be instanciated by plugin
0068     explicit GrepJob( QObject *parent = nullptr );
0069 
0070 public:
0071     ~GrepJob() override;
0072 
0073     void setSettings(const GrepJobSettings& settings);
0074     GrepJobSettings settings() const;
0075 
0076     void setOutputModel(GrepOutputModel * model);
0077     void setDirectoryChoice(const QList<QUrl> &choice);
0078 
0079     void start() override;
0080 
0081     QString statusName() const override;
0082 protected:
0083     bool doKill() override;
0084 
0085 private Q_SLOTS:
0086     void slotFindFinished();
0087     void testFinishState(KJob *job);
0088 
0089 Q_SIGNALS:
0090     void clearMessage( KDevelop::IStatus* ) override;
0091     void showMessage( KDevelop::IStatus*, const QString & message, int timeout = 0) override;
0092     void showErrorMessage(const QString& message, int timeout = 5) override;
0093     void hideProgress( KDevelop::IStatus* ) override;
0094     void showProgress( KDevelop::IStatus*, int minimum, int maximum, int value) override;
0095     void foundMatches( const QString& filename, const GrepOutputItem::List& matches);
0096 
0097 private:
0098     Q_INVOKABLE void slotWork();
0099     void die();
0100     void dieAfterCancellation();
0101 
0102     QList<QUrl> m_directoryChoice;
0103     QString m_errorMessage;
0104 
0105     QRegExp m_regExp;
0106     QString m_regExpSimple;
0107     QPointer<GrepOutputModel> m_outputModel;
0108 
0109     enum {
0110         WorkUnstarted,
0111         WorkStarting,
0112         WorkCollectFiles,
0113         WorkGrep,
0114         WorkCancelled,
0115         WorkDead
0116     } m_workState;
0117 
0118     QList<QUrl> m_fileList;
0119     int m_fileIndex;
0120     GrepFindFilesThread* m_findThread;
0121 
0122     GrepJobSettings m_settings;
0123 
0124     bool m_findSomething;
0125 };
0126 
0127 //FIXME: this function is used externally only for tests, find a way to keep it
0128 //       static for a regular compilation
0129 GrepOutputItem::List grepFile(const QString &filename, const QRegExp &re);
0130 
0131 #endif