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  * Restore Wizard: Job selection page
21  *
22  * Written by Norbert Bizet, May MMXVII
23  *
24  */
25 #include "common.h"
26 #include "jobselectwizardpage.h"
27 #include "conf.h"
28 #include "task.h"
29 #include "ui_jobselectwizardpage.h"
30 #include <QStandardItemModel>
31 
JobSelectWizardPage(QWidget * parent)32 JobSelectWizardPage::JobSelectWizardPage(QWidget *parent) :
33     QWizardPage(parent),
34     ui(new Ui::JobSelectWizardPage),
35     res(NULL),
36     model(new QStandardItemModel),
37     m_jobId(-1)
38 {
39     ui->setupUi(this);
40     /* currentJob in mandatory */
41     registerField("currentJob*", this, "currentJob", SIGNAL(currentJobChanged()));
42     /* assign model to widget */
43     ui->BackupTableView->setModel(model);
44     /* when selection change, change the field value and the next button state */
45     connect(ui->BackupTableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SIGNAL(currentJobChanged()));
46     connect(ui->BackupTableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SIGNAL(completeChanged()));
47 }
48 
~JobSelectWizardPage()49 JobSelectWizardPage::~JobSelectWizardPage()
50 {
51     delete model;
52     delete ui;
53 }
54 
initializePage()55 void JobSelectWizardPage::initializePage()
56 {
57     /* populate model */
58     if (res && (!res->terminated_jobs || res->terminated_jobs->empty())) {
59         /* get terminated_jobs info if not present. Queue populateModel() at end of task */
60         task *t = new task();
61         connect(t, SIGNAL(done(task*)), this , SLOT(populateModel()), Qt::QueuedConnection);
62         connect(t, SIGNAL(done(task*)), t, SLOT(deleteLater()));
63         t->init(res, TASK_STATUS);
64         res->wrk->queue(t);
65     } else {
66         /* populate Model directly */
67         populateModel();
68     }
69 }
70 
isComplete() const71 bool JobSelectWizardPage::isComplete() const
72 {
73     /* any selection will do since it's single selection */
74     return (ui->BackupTableView->selectionModel()
75             && !ui->BackupTableView->selectionModel()->selectedRows().isEmpty()
76             );
77 }
78 
currentJob() const79 qlonglong JobSelectWizardPage::currentJob() const
80 {
81     /* single selection */
82     QModelIndex idx = ui->BackupTableView->selectionModel()->currentIndex();
83     /* return the JobId in column 0 */
84     QModelIndex idIdx = idx.sibling(idx.row(), 0);
85     return idIdx.data().toLongLong();
86 }
87 
populateModel()88 void JobSelectWizardPage::populateModel()
89 {
90     if (res) {
91         /* populate model with jobs listed in currentClient */
92         task *t = new task();
93         connect(t, SIGNAL(done(task*)), t, SLOT(deleteLater()));
94         t->init(res, TASK_LIST_CLIENT_JOBS);
95         int idx = field("currentClient").toInt();
96         char *p = (char*) res->clients->get(idx);
97         POOL_MEM info;
98         pm_strcpy(info, p);
99         t->arg = info.c_str();
100         t->model = model;
101         res->wrk->queue(t);
102     }
103 }
104