1 /*****************************************************************************
2  * Copyright (C) 2009 Csaba Karai <krusader@users.sourceforge.net>           *
3  * Copyright (C) 2009-2019 Krusader Krew [https://krusader.org]              *
4  *                                                                           *
5  * This file is part of Krusader [https://krusader.org].                     *
6  *                                                                           *
7  * Krusader is free software: you can redistribute it and/or modify          *
8  * it under the terms of the GNU General Public License as published by      *
9  * the Free Software Foundation, either version 2 of the License, or         *
10  * (at your option) any later version.                                       *
11  *                                                                           *
12  * Krusader is distributed in the hope that it will be useful,               *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
15  * GNU General Public License for more details.                              *
16  *                                                                           *
17  * You should have received a copy of the GNU General Public License         *
18  * along with Krusader.  If not, see [http://www.gnu.org/licenses/].         *
19  *****************************************************************************/
20 
21 #ifndef ABSTRACTTHREADEDJOB_H
22 #define ABSTRACTTHREADEDJOB_H
23 
24 // QtCore
25 #include <QThread>
26 #include <QEvent>
27 #include <QMimeDatabase>
28 #include <QMutex>
29 #include <QWaitCondition>
30 #include <QStack>
31 #include <QVariant>
32 #include <QList>
33 #include <QEventLoop>
34 #include <QTime>
35 #include <QUrl>
36 
37 #include <KIO/Job>
38 
39 class AbstractJobThread;
40 class QTemporaryDir;
41 class UserEvent;
42 class KRarcObserver;
43 class QTemporaryFile;
44 
45 class AbstractThreadedJob : public KIO::Job
46 {
47     friend class AbstractJobThread;
48 
49     Q_OBJECT
50 
51 protected:
52     AbstractThreadedJob();
53 
54     void addEventResponse(QList<QVariant> * obj);
55     QList<QVariant> * getEventResponse(UserEvent * event);
56     void  sendEvent(UserEvent * event);
57 
58     virtual ~AbstractThreadedJob();
59     virtual bool event(QEvent *) Q_DECL_OVERRIDE;
60     virtual void startAbstractJobThread(AbstractJobThread*);
doSuspend()61     virtual bool doSuspend() Q_DECL_OVERRIDE {
62         return false;
63     }
64 
65 protected slots:
66     void slotDownloadResult(KJob*);
67     void slotProcessedAmount(KJob *, KJob::Unit, qulonglong);
68     void slotTotalAmount(KJob *, KJob::Unit, qulonglong);
69     void slotSpeed(KJob *, unsigned long);
70     void slotDescription(KJob *job, const QString &title, const QPair<QString, QString> &field1,
71                          const QPair<QString, QString> &field2);
72 
73 public:
74     QMutex                    _locker;
75     QWaitCondition            _waiter;
76     QStack<QList<QVariant> *> _stack;
77     QString                   _title;
78     qulonglong                _maxProgressValue;
79     qulonglong                _currentProgress;
80     QTime                     _time;
81     bool                      _exiting;
82 
83 private:
84     AbstractJobThread *       _jobThread;
85 };
86 
87 
88 
89 class AbstractJobThread : public QThread
90 {
91     friend class AbstractThreadedJob;
92     friend class AbstractJobObserver;
93     Q_OBJECT
94 
95 public:
96     AbstractJobThread();
97     virtual ~AbstractJobThread();
98 
99     void abort();
100     KRarcObserver * observer();
101 
102 protected slots:
103     virtual void slotStart() = 0;
104 
105 protected:
106     virtual void run() Q_DECL_OVERRIDE;
setJob(AbstractThreadedJob * job)107     void setJob(AbstractThreadedJob * job) {
108         _job = job;
109     }
110 
111     QList<QUrl> remoteUrls(const QUrl &baseUrl, const QStringList & files);
112     QUrl downloadIfRemote(const QUrl &baseUrl, const QStringList & files);
113     void countLocalFiles(const QUrl &baseUrl, const QStringList &names, unsigned long &totalFiles);
114 
115     void sendError(int errorCode, QString message);
116     void sendInfo(QString message, QString a1 = QString(), QString a2 = QString(), QString a3 = QString(), QString a4 = QString());
117     void sendReset(QString message, QString a1 = QString(""), QString a2 = QString(""), QString a3 = QString(""), QString a4 = QString(""));
118     void sendSuccess();
119     void sendMessage(const QString &message);
120     void sendMaxProgressValue(qulonglong value);
121     void sendAddProgress(qulonglong value, const QString &progress = QString());
122 
setProgressTitle(const QString & title)123     void setProgressTitle(const QString &title) {
124         _progressTitle = title;
125     }
126 
127     QString tempFileIfRemote(const QUrl &kurl, const QString &type);
128     QString tempDirIfRemote(const QUrl &kurl);
129     bool uploadTempFiles();
130 
isExited()131     bool isExited() {
132         return _exited;
133     }
134     void terminate();
135 
136     QString getPassword(const QString &path);
137     bool getArchiveInformation(QString &, QString &, QString &, QString &, const QUrl &);
138 
139     AbstractThreadedJob *_job;
140     QEventLoop          *_loop;
141 
142     QTemporaryDir       *_downloadTempDir;
143     KRarcObserver       *_observer;
144 
145     QTemporaryFile      *_tempFile;
146     QString              _tempFileName;
147     QUrl                 _tempFileTarget;
148     QTemporaryDir       *_tempDir;
149     QString              _tempDirName;
150     QUrl                 _tempDirTarget;
151 
152     bool                 _exited;
153 
154     QString              _progressTitle;
155 };
156 
157 enum PossibleCommands {
158     CMD_ERROR                =  1,
159     CMD_INFO                 =  2,
160     CMD_RESET                =  3,
161     CMD_DOWNLOAD_FILES       =  4,
162     CMD_UPLOAD_FILES         =  5,
163     CMD_SUCCESS              =  6,
164     CMD_MAXPROGRESSVALUE     =  7,
165     CMD_ADD_PROGRESS         =  8,
166     CMD_GET_PASSWORD         =  9,
167     CMD_MESSAGE              = 10
168 };
169 
170 class UserEvent : public QEvent
171 {
172 public:
UserEvent(int command,const QList<QVariant> & args)173     UserEvent(int command, const QList<QVariant> &args) : QEvent(QEvent::User), _command(command), _args(args) {}
174 
command()175     inline int                     command() {
176         return _command;
177     }
args()178     inline const QList<QVariant> & args()    {
179         return _args;
180     }
181 
182 protected:
183     int             _command;
184     QList<QVariant> _args;
185 };
186 
187 #endif // __ABSTRACTTHREADED_JOB_H__
188 
189