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 /*
21  *  Clients Class
22  *
23  *   Dirk Bartley, March 2007
24  *
25  */
26 
27 #include "bat.h"
28 #include <QAbstractEventDispatcher>
29 #include <QMenu>
30 #include "clients/clients.h"
31 #include "run/run.h"
32 #include "status/clientstat.h"
33 #include "util/fmtwidgetitem.h"
34 
Clients()35 Clients::Clients() : Pages()
36 {
37    setupUi(this);
38    m_name = tr("Clients");
39    pgInitialize();
40    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
41    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/network-server.png")));
42 
43    /* tableWidget, Storage Tree Tree Widget inherited from ui_client.h */
44    m_populated = false;
45    m_checkcurwidget = true;
46    m_closeable = false;
47    m_firstpopulation = true;
48    /* add context sensitive menu items specific to this classto the page
49     * selector tree. m_contextActions is QList of QActions */
50    m_contextActions.append(actionRefreshClients);
51    createContextMenu();
52 }
53 
~Clients()54 Clients::~Clients()
55 {
56 }
57 
58 /*
59  * The main meat of the class!!  The function that queries the director and
60  * creates the widgets with appropriate values.
61  */
populateTable()62 void Clients::populateTable()
63 {
64    m_populated = true;
65 
66    Freeze frz(*tableWidget); /* disable updating*/
67 
68    QStringList headerlist = (QStringList() << tr("Client Name") << tr("File Retention")
69        << tr("Job Retention") << tr("AutoPrune") << tr("ClientId") << tr("Uname") );
70 
71    int sortcol = headerlist.indexOf(tr("Client Name"));
72    Qt::SortOrder sortord = Qt::AscendingOrder;
73    if (tableWidget->rowCount()) {
74       sortcol = tableWidget->horizontalHeader()->sortIndicatorSection();
75       sortord = tableWidget->horizontalHeader()->sortIndicatorOrder();
76    }
77 
78    m_checkcurwidget = false;
79    tableWidget->clear();
80    m_checkcurwidget = true;
81 
82    tableWidget->setColumnCount(headerlist.count());
83    tableWidget->setHorizontalHeaderLabels(headerlist);
84    tableWidget->horizontalHeader()->setHighlightSections(false);
85    tableWidget->setRowCount(m_console->client_list.count());
86    tableWidget->verticalHeader()->hide();
87    tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
88    tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
89    tableWidget->setSortingEnabled(false); /* rows move on insert if sorting enabled */
90    bool first = true;
91    QString client_comsep("");
92    foreach (QString clientName, m_console->client_list){
93       if (first) {
94          client_comsep += "'" + clientName + "'";
95          first = false;
96       }
97       else
98          client_comsep += ",'" + clientName + "'";
99    }
100 
101    if (client_comsep != "") {
102       QString query("");
103       query += "SELECT Name, FileRetention, JobRetention, AutoPrune, ClientId, Uname"
104            " FROM Client"
105            " WHERE ClientId IN (SELECT MAX(ClientId) FROM Client WHERE";
106       query += " Name IN (" + client_comsep + ")";
107       query += " GROUP BY Name) ORDER BY Name";
108 
109       QStringList results;
110       if (mainWin->m_sqlDebug)
111          Pmsg1(000, "Clients query cmd : %s\n",query.toUtf8().data());
112       if (m_console->sql_cmd(query, results)) {
113          int row = 0;
114 
115          /* Iterate through the record returned from the query */
116          foreach (QString resultline, results) {
117             QStringList fieldlist = resultline.split("\t");
118 
119             if (fieldlist.size() < 5) { // Uname is checked after
120                Pmsg1(0, "Unexpected line %s\n", resultline.toUtf8().data());
121                continue;
122             }
123             if (m_firstpopulation) {
124                settingsOpenStatus(fieldlist[0]);
125             }
126 
127             TableItemFormatter item(*tableWidget, row);
128 
129             /* Iterate through fields in the record */
130             QStringListIterator fld(fieldlist);
131             int col = 0;
132 
133             /* name */
134             item.setTextFld(col++, fld.next());
135 
136             /* file retention */
137             item.setDurationFld(col++, fld.next());
138 
139             /* job retention */
140             item.setDurationFld(col++, fld.next());
141 
142             /* autoprune */
143             item.setBoolFld(col++, fld.next());
144 
145             /* client id */
146             item.setNumericFld(col++, fld.next());
147 
148             /* uname */
149             if (fld.hasNext()) {
150                item.setTextFld(col++, fld.next());
151             } else {
152                item.setTextFld(col++, "");
153             }
154 
155             row++;
156          }
157       }
158    }
159    /* set default sorting */
160    tableWidget->sortByColumn(sortcol, sortord);
161    tableWidget->setSortingEnabled(true);
162 
163    /* Resize rows and columns */
164    tableWidget->resizeColumnsToContents();
165    tableWidget->resizeRowsToContents();
166 
167    /* make read only */
168    int rcnt = tableWidget->rowCount();
169    int ccnt = tableWidget->columnCount();
170    for(int r=0; r < rcnt; r++) {
171       for(int c=0; c < ccnt; c++) {
172          QTableWidgetItem* item = tableWidget->item(r, c);
173          if (item) {
174             item->setFlags(Qt::ItemFlags(item->flags() & (~Qt::ItemIsEditable)));
175          }
176       }
177    }
178    m_firstpopulation = false;
179 }
180 
181 /*
182  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
183  * The tree has been populated.
184  */
PgSeltreeWidgetClicked()185 void Clients::PgSeltreeWidgetClicked()
186 {
187    if(!m_populated) {
188       populateTable();
189    }
190    if (!isOnceDocked()) {
191       dockPage();
192    }
193 }
194 
195 /*
196  * Added to set the context menu policy based on currently active treeWidgetItem
197  * signaled by currentItemChanged
198  */
tableItemChanged(QTableWidgetItem * currentwidgetitem,QTableWidgetItem * previouswidgetitem)199 void Clients::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem )
200 {
201    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
202    if (m_checkcurwidget) {
203       int currentRow = currentwidgetitem->row();
204       QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
205       m_currentlyselected = currentrowzeroitem->text();
206 
207       /* The Previous item */
208       if (previouswidgetitem) { /* avoid a segfault if first time */
209          tableWidget->removeAction(actionListJobsofClient);
210          tableWidget->removeAction(actionStatusClientWindow);
211          tableWidget->removeAction(actionPurgeJobs);
212          tableWidget->removeAction(actionPrune);
213       }
214 
215       if (m_currentlyselected.length() != 0) {
216          /* set a hold variable to the client name in case the context sensitive
217           * menu is used */
218          tableWidget->addAction(actionListJobsofClient);
219          tableWidget->addAction(actionStatusClientWindow);
220          tableWidget->addAction(actionPurgeJobs);
221          tableWidget->addAction(actionPrune);
222       }
223    }
224 }
225 
226 /*
227  * Setup a context menu
228  * Made separate from populate so that it would not create context menu over and
229  * over as the tree is repopulated.
230  */
createContextMenu()231 void Clients::createContextMenu()
232 {
233    tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
234    tableWidget->addAction(actionRefreshClients);
235    /* for the tableItemChanged to maintain m_currentJob */
236    connect(tableWidget, SIGNAL(
237            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
238            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
239 
240    /* connect to the action specific to this pages class */
241    connect(actionRefreshClients, SIGNAL(triggered()), this,
242                 SLOT(populateTable()));
243    connect(actionListJobsofClient, SIGNAL(triggered()), this,
244                 SLOT(showJobs()));
245    connect(actionStatusClientWindow, SIGNAL(triggered()), this,
246                 SLOT(statusClientWindow()));
247    connect(actionPurgeJobs, SIGNAL(triggered()), this,
248                 SLOT(consolePurgeJobs()));
249    connect(actionPrune, SIGNAL(triggered()), this,
250                 SLOT(prune()));
251 }
252 
253 /*
254  * Function responding to actionListJobsofClient which calls mainwin function
255  * to create a window of a list of jobs of this client.
256  */
showJobs()257 void Clients::showJobs()
258 {
259    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
260    mainWin->createPageJobList("", m_currentlyselected, "", "", parentItem);
261 }
262 
263 /*
264  * Function responding to actionListJobsofClient which calls mainwin function
265  * to create a window of a list of jobs of this client.
266  */
consoleStatusClient()267 void Clients::consoleStatusClient()
268 {
269    QString cmd("status client=");
270    cmd += m_currentlyselected;
271    consoleCommand(cmd);
272 }
273 
274 /*
275  * Virtual function which is called when this page is visible on the stack
276  */
currentStackItem()277 void Clients::currentStackItem()
278 {
279    if(!m_populated) {
280       populateTable();
281       /* Create the context menu for the client table */
282    }
283 }
284 
285 /*
286  * Function responding to actionPurgeJobs
287  */
consolePurgeJobs()288 void Clients::consolePurgeJobs()
289 {
290    if (QMessageBox::warning(this, "Bat",
291       tr("Are you sure you want to purge all jobs of client \"%1\" ?\n"
292 "The Purge command will delete associated Catalog database records from Jobs and"
293 " Volumes without considering the retention period. Purge  works only on the"
294 " Catalog database and does not affect data written to Volumes. This command can"
295 " be dangerous because you can delete catalog records associated with current"
296 " backups of files, and we recommend that you do not use it unless you know what"
297 " you are doing.\n\n"
298 " Is there any way I can get you to click Cancel here?  You really don't want to do"
299 " this\n\n"
300          "Press OK to proceed with the purge operation?").arg(m_currentlyselected),
301          QMessageBox::Ok | QMessageBox::Cancel,
302          QMessageBox::Cancel)
303       == QMessageBox::Cancel) { return; }
304 
305    QString cmd("purge jobs client=");
306    cmd += m_currentlyselected;
307    consoleCommand(cmd);
308 }
309 
310 /*
311  * Function responding to actionPrune
312  */
prune()313 void Clients::prune()
314 {
315    new prunePage("", m_currentlyselected);
316 }
317 
318 /*
319  * Function responding to action to create new client status window
320  */
statusClientWindow()321 void Clients::statusClientWindow()
322 {
323    /* if one exists, then just set it current */
324    bool found = false;
325    foreach(Pages *page, mainWin->m_pagehash) {
326       if (mainWin->currentConsole() == page->console()) {
327          if (page->name() == tr("Client Status %1").arg(m_currentlyselected)) {
328             found = true;
329             page->setCurrent();
330          }
331       }
332    }
333    if (!found) {
334       QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
335       new ClientStat(m_currentlyselected, parentItem);
336    }
337 }
338 
339 /*
340  * If first time, then check to see if there were status pages open the last time closed
341  * if so open
342  */
settingsOpenStatus(QString & client)343 void Clients::settingsOpenStatus(QString &client)
344 {
345    QSettings settings(m_console->m_dir->name(), "bat");
346 
347    settings.beginGroup("OpenOnExit");
348    QString toRead = "ClientStatus_" + client;
349    if (settings.value(toRead) == 1) {
350       new ClientStat(client, mainWin->getFromHash(this));
351       setCurrent();
352       mainWin->getFromHash(this)->setExpanded(true);
353    }
354    settings.endGroup();
355 }
356