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 RESCONFIGUICONTROLLER_H
21 #define RESCONFIGUICONTROLLER_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 
36 /*
37    TrayUiController - Controls the screen displayed when a user selects one resource
38    (Director, File Daemon or Storage Daemon). It allows the user to:
39 
40    1 - Change the data that's required to connect with the resource (name, password, address and port)
41    2 - Connect to the resource, and therefore see it's terminated jobs and running jobs
42 
43    Also, if the resource is a director, it allows the user to start new backup jobs or restore jobs
44 */
45 class ResDetailsUiController : public QObject
46 {
47    Q_OBJECT
48 
49    // Data related to the resource that the user selected
50    Q_PROPERTY(ResourceModel *resModel WRITE setResModel)
51 
52    Q_PROPERTY(QString resourceName READ resourceName WRITE setResourceName NOTIFY resourceNameChanged)
53 
54    //These are loaded after successfully connecting to the resource
55    Q_PROPERTY(QString startedDate READ startedDate WRITE setStartedDate NOTIFY startedDateChanged)
56    Q_PROPERTY(QString resourceVersion READ resourceVersion WRITE setResourceVersion NOTIFY resourceVersionChanged)
57    Q_PROPERTY(QString resourcePlugins READ resourcePlugins WRITE setResourcePlugins NOTIFY resourcePluginsChanged)
58    Q_PROPERTY(QString bandwidthLimit READ bandwidthLimit WRITE setBandwidthLimit NOTIFY bandwidthLimitChanged)
59    Q_PROPERTY(QList<QObject *> terminatedJobs READ getTerminatedJobs NOTIFY terminatedJobsChanged())
60    Q_PROPERTY(QList<QObject *> runningJobs READ getRunningJobs NOTIFY runningJobsChanged())
61 
62    //Loaded if the connection was not successful
63    Q_PROPERTY(QString connectionError READ getConnectionError WRITE setConnectionError NOTIFY connectionError)
64 
65    //TODO merge both into a variable 'dialogMsg'
66    // Message displayed on the GUI dialogs
67    Q_PROPERTY(QString successMsg READ successMsg  WRITE setSuccessMessage NOTIFY successMessageChanged)
68    Q_PROPERTY(QString errorMsg READ errorMsg  WRITE setErrorMessage NOTIFY errorMessageChanged)
69 
70    Q_PROPERTY(bool isConnecting READ isConnecting WRITE setIsConnecting NOTIFY isConnectingChanged)
71 
72 private:
73    QString m_resourceName;
74    QString m_startedDate;
75    QString m_resourceVersion;
76    QString m_resourcePlugins;
77    QString m_bandwidthLimit;
78    QList<QObject *> *m_terminatedJobs = new QList<QObject *>();
79    QList<QObject *> *m_runningJobs = new QList<QObject *>();
80 
81    QString m_successMsg;
82    QString m_errorMsg;
83    QString m_connError;
84 
85    ConfigStorage *m_storage = NULL;
86 
87    rescode m_resCode;
88    RESMON *m_res = NULL;
89 
90    bool m_connecting = false;
91 
92 public:
93    explicit ResDetailsUiController(QObject *parent = nullptr);
94    ~ResDetailsUiController();
95 
96    // Getters / Setters for data that is used by our GUI
isConnecting()97    bool isConnecting() { return m_connecting; }
98 
99    // Resource Name
resourceName()100    QString resourceName() {
101       return m_resourceName;
102    }
103 
setResourceName(const QString & resourceName)104    void setResourceName(const QString &resourceName) {
105       if (resourceName == m_resourceName)
106          return;
107 
108       m_resourceName = resourceName;
109       emit resourceNameChanged();
110    }
111 
112    // Resource Started Date
startedDate()113    QString startedDate() {
114       return m_startedDate;
115    }
116 
setStartedDate(const QString & startedDate)117    void setStartedDate(const QString &startedDate) {
118       if (startedDate == m_startedDate)
119          return;
120 
121       m_startedDate = startedDate;
122       emit startedDateChanged();
123    }
124 
125    // Resource Version
resourceVersion()126    QString resourceVersion() {
127       return m_resourceVersion;
128    }
129 
setResourceVersion(const QString & resourceVersion)130    void setResourceVersion(const QString &resourceVersion) {
131       if (resourceVersion == m_resourceVersion)
132          return;
133 
134       m_resourceVersion = resourceVersion;
135       emit resourceVersionChanged();
136    }
137 
138    // Resource Plugins
resourcePlugins()139    QString resourcePlugins() {
140       return m_resourcePlugins;
141    }
142 
setResourcePlugins(const QString & resourcePlugins)143    void setResourcePlugins(const QString &resourcePlugins) {
144       if (resourcePlugins == m_resourcePlugins)
145          return;
146 
147       m_resourcePlugins = resourcePlugins;
148       emit resourcePluginsChanged();
149    }
150 
151    // Resource Bandwidth Limit
bandwidthLimit()152    QString bandwidthLimit() {
153       return m_bandwidthLimit;
154    }
155 
setBandwidthLimit(const QString & bandwidthLimit)156    void setBandwidthLimit(const QString &bandwidthLimit) {
157       if (bandwidthLimit == m_bandwidthLimit)
158          return;
159 
160       m_bandwidthLimit = bandwidthLimit;
161       emit bandwidthLimitChanged();
162    }
163 
164    // Terminated Jobs
getTerminatedJobs()165    QList<QObject *> getTerminatedJobs() {
166       return *m_terminatedJobs;
167    }
168 
setTerminatedJobs(QList<QObject * > * jobs)169    void setTerminatedJobs(QList<QObject *> *jobs) {
170       if(m_terminatedJobs != NULL) {
171          delete m_terminatedJobs;
172       }
173 
174       m_terminatedJobs = jobs;
175       emit terminatedJobsChanged();
176    }
177 
178    // Running Jobs
getRunningJobs()179    QList<QObject *> getRunningJobs() {
180       return *m_runningJobs;
181    }
182 
setRunningJobs(QList<QObject * > * jobs)183    void setRunningJobs(QList<QObject *> *jobs) {
184       if(m_runningJobs != NULL) {
185          delete m_runningJobs;
186       }
187 
188       m_runningJobs = jobs;
189       emit runningJobsChanged();
190    }
191 
192    // Dialog Success Message
successMsg()193    QString successMsg() {
194       return m_successMsg;
195    }
196 
setSuccessMessage(const QString & successMsg)197    void setSuccessMessage(const QString &successMsg) {
198       m_successMsg = successMsg;
199       emit successMessageChanged();
200    }
201 
202    // Dialog Error Message
errorMsg()203    QString errorMsg() {
204       return m_errorMsg;
205    }
206 
setErrorMessage(const QString & errorMsg)207    void setErrorMessage(const QString &errorMsg) {
208       m_errorMsg = errorMsg;
209       emit errorMessageChanged();
210    }
211 
212    // Connection Error Message
getConnectionError()213    QString getConnectionError() {
214       return m_connError;
215    }
216 
setConnectionError(const QString & connError)217    void setConnectionError(const QString &connError) {
218       m_connError = connError;
219       emit connectionError();
220    }
221 
setIsConnecting(bool isConnecting)222    void setIsConnecting(bool isConnecting) {
223        if (m_connecting != isConnecting) {
224            m_connecting = isConnecting;
225            emit isConnectingChanged();
226        }
227    }
228 
229    // Model (If Edition Mode)
setResModel(ResourceModel * resModel)230    void setResModel(ResourceModel *resModel) {
231       m_res = resModel->resource();
232       m_resCode = resModel->resCode();
233       setResourceName(m_res->hdr.name);
234    }
235 
236 signals:
237    // Events that are emitted to our GUI code to inform changes in our data
238    void resourceNameChanged();
239    void startedDateChanged();
240    void resourceVersionChanged();
241    void resourcePluginsChanged();
242    void bandwidthLimitChanged();
243    void terminatedJobsChanged();
244    void runningJobsChanged();
245    void successMessageChanged();
246    void errorMessageChanged();
247    void isConnectingChanged();
248 
249    // Events emitted to our GUI about the connection status with the resource
250    void connectionStarted();
251    void connectionError();
252    void connectionSuccess();
253 
254    // Tells the GUI to start the screen related to running a job
255    void runJobModelCreated(RunJobModel *model);
256 
257    // Tells the GUI to start the screen related to restoring a job
258    void restoreModelCreated(RestoreJobModel *model);
259 
260 public slots:
261    // Called when the user wants to connect to a resource
262    void connectToResource();
263    void connectCallback(task *t);
264 
265    // Called when the user wants to run a job
266    void createRunJobModel();
267    void createBackupModelCallback(task *t);
268 
269    // Called when the user wants to restore a job
270    void createRestoreJobModel();
271    void createRestoreModelCallback(task *t);
272 
canRunJobs()273    bool canRunJobs() {
274        if (m_res == NULL) {
275            return false;
276        }
277 
278        return m_resCode == R_DIRECTOR || m_res->use_remote;
279    }
280 };
281 
282 #endif // RESCONFIGUICONTROLLER_H
283