1 /*
2     SPDX-FileCopyrightText: 1999-2001 Bernd Gehrmann <bernd@kdevelop.org>
3     SPDX-FileCopyrightText: 2008 Hamish Rodda <rodda@kde.org>
4     SPDX-FileCopyrightText: 2010 Silvère Lestang <silvere.lestang@gmail.com>
5     SPDX-FileCopyrightText: 2010 Julien Desgats <julien.desgats@gmail.com>
6 
7     SPDX-License-Identifier: GPL-2.0-or-later
8 */
9 
10 #ifndef KDEVPLATFORM_PLUGIN_GREPJOB_H
11 #define KDEVPLATFORM_PLUGIN_GREPJOB_H
12 
13 #include <QPointer>
14 #include <QUrl>
15 
16 #include <KJob>
17 
18 #include <interfaces/istatus.h>
19 
20 #include "grepfindthread.h"
21 #include "grepoutputmodel.h"
22 
23 namespace KDevelop
24 {
25     class IProject;
26 }
27 
28 class QRegExp;
29 class GrepViewPlugin;
30 class FindReplaceTest; //FIXME: this is useful only for tests
31 
32 struct GrepJobSettings
33 {
34     bool fromHistory = false;
35 
36     bool projectFilesOnly = false;
37     bool caseSensitive = true;
38     bool regexp = true;
39 
40     int depth = -1;
41 
42     QString pattern;
43     QString searchTemplate;
44     QString replacementTemplate;
45     QString files;
46     QString exclude;
47     QString searchPaths;
48 };
49 
50 Q_DECLARE_TYPEINFO(GrepJobSettings, Q_MOVABLE_TYPE);
51 
52 
53 class GrepJob : public KJob, public KDevelop::IStatus
54 {
55     Q_OBJECT
56     Q_INTERFACES( KDevelop::IStatus )
57 
58     friend class GrepViewPlugin;
59     friend class FindReplaceTest;
60 
61 private:
62     ///Job can only be instanciated by plugin
63     explicit GrepJob( QObject *parent = nullptr );
64 
65 public:
66     void setSettings(const GrepJobSettings& settings);
67     GrepJobSettings settings() const;
68 
69     void setOutputModel(GrepOutputModel * model);
70     void setDirectoryChoice(const QList<QUrl> &choice);
71 
72     void start() override;
73 
74     QString statusName() const override;
75 protected:
76     bool doKill() override;
77 
78 //    GrepOutputModel* model() const;
79 
80 private Q_SLOTS:
81     void slotFindFinished();
82     void testFinishState(KJob *job);
83 
84 Q_SIGNALS:
85     void clearMessage( KDevelop::IStatus* ) override;
86     void showMessage( KDevelop::IStatus*, const QString & message, int timeout = 0) override;
87     void showErrorMessage(const QString & message, int timeout = 0) override;
88     void hideProgress( KDevelop::IStatus* ) override;
89     void showProgress( KDevelop::IStatus*, int minimum, int maximum, int value) override;
90     void foundMatches( const QString& filename, const GrepOutputItem::List& matches);
91 
92 private:
93     Q_INVOKABLE void slotWork();
94 
95     QList<QUrl> m_directoryChoice;
96     QString m_errorMessage;
97 
98     QRegExp m_regExp;
99     QString m_regExpSimple;
100     GrepOutputModel *m_outputModel;
101 
102     enum {
103         WorkCollectFiles,
104         WorkGrep,
105         WorkIdle,
106         WorkCancelled
107     } m_workState;
108 
109     QList<QUrl> m_fileList;
110     int m_fileIndex;
111     QPointer<GrepFindFilesThread> m_findThread;
112 
113     GrepJobSettings m_settings;
114 
115     bool m_findSomething;
116 };
117 
118 //FIXME: this function is used externally only for tests, find a way to keep it
119 //       static for a regular compilation
120 GrepOutputItem::List grepFile(const QString &filename, const QRegExp &re);
121 
122 #endif
123