1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 Kern Sibbald
5 
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8 
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13 
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16 
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 
20 #ifndef RESTOREUICONTROLLER_H
21 #define RESTOREUICONTROLLER_H
22 
23 #include <QObject>
24 #include <QString>
25 #include "tray_conf.h"
26 #include "config-storage.h"
27 #include "resmodel.h"
28 #include <android/log.h>
29 #include "jcr.h"
30 #include "task.h"
31 #include "common.h"
32 #include "jobmodel.h"
33 #include "runjobmodel.h"
34 #include "restorejobmodel.h"
35 #include "filesmodel.h"
36 #include <QAbstractItemModel>
37 #include <android/log.h>
38 
39 /*
40    RestoreJobUiController - controls the screen where the user specifies which Job it wants to restore
41 */
42 class RestoreUiController : public QObject
43 {
44     Q_OBJECT
45 
46     // Contains the Director that will restore the Job
47     Q_PROPERTY(RestoreJobModel *model WRITE setModel)
48 
49     // List of Jobs that can be restored
50     Q_PROPERTY(QList<QObject *> jobs READ getJobs NOTIFY jobsChanged())
51 
52     // List of clients that the user can choose
53     // The Job will be restored into the selected client
54     Q_PROPERTY(QStringList clients READ getClients NOTIFY clientsChanged())
55 
56     // Restore Options
57     Q_PROPERTY(QString where READ getWhereParam WRITE setWhereParam NOTIFY whereParamChanged())
58     Q_PROPERTY(int replaceIndex READ getReplaceIndex WRITE setReplaceIndex NOTIFY replaceIndexChanged())
59     Q_PROPERTY(QStringList replaceOptions READ getReplaceOptions NOTIFY replaceOptionsChanged())
60 
61     // Optional comment
62     Q_PROPERTY(QString comment READ getComment WRITE setComment NOTIFY commentChanged())
63 
64     // Model usued to show the whole directory structure associated with the selected Job
65     // It starts at "/", and the user can move down into the directory tree
66     // to select specific files
67     Q_PROPERTY(QStandardItemModel *files READ getFileModel NOTIFY fileModelChanged())
68 
69     //Information dialog message
70     Q_PROPERTY(QString dialogText READ getDialogText NOTIFY dialogTextChanged())
71 
72     Q_PROPERTY(bool isConnecting READ isConnecting WRITE setIsConnecting NOTIFY isConnectingChanged)
73 
74 private:
75     ConfigStorage *m_config;
76     RestoreJobModel *m_model;
77     FileSourceModel *m_sourceModel = new FileSourceModel();
78 
79     QStringList *m_clients = new QStringList();
80     QStringList m_replaceOptions;
81     QList<QObject *> *m_jobs = new QList<QObject *>();
82 
83     QString m_where;
84     QString m_comment;
85     QString m_selectedJobId;
86     QString m_dialogText;
87 
88     int m_replaceIndex = 0;
89     int m_selectedClient = 0;
90     int m_targetClient = 0;
91 
92     int m_currentSourceId;
93     QString m_currentPathStr;
94 
95     QMap<int, QModelIndex> m_selectedFileIds;
96     QMap<int, QModelIndex> m_selectedDirIds;
97     bool m_connecting = false;
98 
99     void setDefaultValues(RESMON *dir);
100 
101 public:
102     explicit RestoreUiController(QObject *parent = nullptr);
103     ~RestoreUiController();
104 
105     // Getters / Setters for data that is used by our GUI
isConnecting()106     bool isConnecting() { return m_connecting; }
107 
setIsConnecting(bool isConnecting)108     void setIsConnecting(bool isConnecting) {
109        if (m_connecting != isConnecting) {
110            m_connecting = isConnecting;
111            emit isConnectingChanged();
112        }
113     }
114 
getDialogText()115     QString getDialogText() {
116         return m_dialogText;
117     }
118 
setDialogText(QString dialogText)119     void setDialogText(QString dialogText) {
120         if (m_dialogText == dialogText) {
121             return;
122         }
123 
124         m_dialogText = dialogText;
125         emit dialogTextChanged();
126     }
127 
128     // Clients
getClients()129     QStringList getClients() {
130         return *m_clients;
131     }
132 
setClients(alist * clients)133     void setClients(alist *clients) {
134         char *cli;
135         foreach_alist(cli, clients) {
136             m_clients->append(QString(cli));
137         }
138 
139         emit clientsChanged();
140     }
141 
142     // Model
setModel(RestoreJobModel * model)143     void setModel(RestoreJobModel *model) {
144         m_model = model;
145         RESMON *dir = model->getDirector();
146         setClients(dir->clients);
147 
148         QStringList replaceOptions;
149         replaceOptions << "Never" << "Always" << "IfNewer" << "IfOlder";
150         setReplaceOptions(replaceOptions);
151     }
152 
153     // Jobs
getJobs()154     QList<QObject *> getJobs() {
155         return *m_jobs;
156     }
157 
setJobs(QStandardItemModel * jobs)158     void setJobs(QStandardItemModel *jobs) {
159         JobModel *job;
160         m_jobs->clear();
161         for(;;) {
162             QList<QStandardItem *> jobFields = jobs->takeRow(0);
163             if (jobFields.size() < 2) {
164                 break;
165             }
166 
167             job = new JobModel();
168             job->setData(jobFields);
169             m_jobs->append(job);
170         }
171         emit jobsChanged();
172     }
173 
174     // Where Param
getWhereParam()175     QString getWhereParam() {
176         return m_where;
177     }
178 
setWhereParam(QString where)179     void setWhereParam(QString where) {
180         if (m_where == where) {
181             return;
182         }
183 
184         m_where = where;
185         emit whereParamChanged();
186     }
187 
188     // Replace Options
getReplaceOptions()189     QStringList getReplaceOptions() {
190         return m_replaceOptions;
191     }
192 
setReplaceOptions(QStringList replaceOptions)193     void setReplaceOptions(QStringList replaceOptions) {
194         m_replaceOptions = replaceOptions;
195         emit replaceOptionsChanged();
196     }
197 
198     // Replace Index
getReplaceIndex()199     int getReplaceIndex() {
200         return m_replaceIndex;
201     }
202 
203     // Comment
getComment()204     QString getComment() {
205         return m_comment;
206     }
207 
setComment(QString comment)208     void setComment(QString comment) {
209         if (comment == m_comment)
210             return;
211 
212         m_comment = comment;
213         emit commentChanged();
214     }
215 
getFileModel()216     QStandardItemModel *getFileModel() {
217         return m_sourceModel;
218     }
219 
220     void fetchDefaultValues();
221     void fetchPath();
222 
223 signals:
224     void clientsChanged();
225     void jobsChanged();
226     void whereParamChanged();
227     void replaceOptionsChanged();
228     void replaceIndexChanged();
229     void commentChanged();
230     void fileModelChanged();
231     void dialogTextChanged();
232     void isConnectingChanged();
233 
234 public slots:
setTargetClient(int targetClient)235     void setTargetClient(int targetClient) {
236         m_targetClient = targetClient;
237     }
238 
setReplaceIndex(int index)239     void setReplaceIndex(int index) {
240         if(index == m_replaceIndex)
241             return;
242 
243         m_replaceIndex = index;
244         emit replaceIndexChanged();
245     }
246 
247     // Called when the user selects a client. Calls the Director to fetch Jobs associated with it
248     void handleClientChange(int clientIndex);
249     void clientJobsCallback(task *t);
250 
251     // Called when the user selects a job. Calls the Director to fetch Files associated with it
252     void handleJobSelection(QString jobId, QString jobName);
253     void jobFilesCallback(task *t);
254 
255     // Called when the user navigates under the directory tree
256     // to selected files to be restored
257     void handleFileTreeNodeClick(int fileIndex);
258     void handleFileSelection(int fileIndex, bool checked);
259     bool fileIsSelected(int fileIndex);
260 
261     // Called when the user wishes to start the restore
262     void restore();
263 
264     // Called after we asked the resource to start the restore
265     void restoreJobCallback(task *t);
266 };
267 
268 #endif // RESTOREUICONTROLLER_H
269