]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/fileset/fileset.cpp
Backport from BEE
[bacula/bacula] / bacula / src / qt-console / fileset / fileset.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    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    Bacula® is a registered trademark of Kern Sibbald.
15 */
16
17 /*
18  *   Version $Id$
19  *
20  *  FileSet Class
21  *
22  *   Dirk Bartley, March 2007
23  *
24  */
25
26 #include "bat.h"
27 #include <QAbstractEventDispatcher>
28 #include <QMenu>
29 #include "fileset/fileset.h"
30 #include "util/fmtwidgetitem.h"
31
32 FileSet::FileSet() : Pages()
33 {
34    setupUi(this);
35    m_name = tr("FileSets");
36    pgInitialize();
37    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
38    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/system-file-manager.png")));
39
40    /* tableWidget, FileSet Tree Tree Widget inherited from ui_fileset.h */
41    m_populated = false;
42    m_checkcurwidget = true;
43    m_closeable = false;
44    readSettings();
45    /* add context sensitive menu items specific to this classto the page
46     * selector tree. m_contextActions is QList of QActions */
47    m_contextActions.append(actionRefreshFileSet);
48 }
49
50 FileSet::~FileSet()
51 {
52    writeSettings();
53 }
54
55 /*
56  * The main meat of the class!!  The function that querries the director and
57  * creates the widgets with appropriate values.
58  */
59 void FileSet::populateTable()
60 {
61
62    Freeze frz(*tableWidget); /* disable updating*/
63
64    m_checkcurwidget = false;
65    tableWidget->clear();
66    m_checkcurwidget = true;
67
68    QStringList headerlist = (QStringList() << tr("FileSet Name") << tr("FileSet Id")
69        << tr("Create Time"));
70
71    tableWidget->setColumnCount(headerlist.count());
72    tableWidget->setHorizontalHeaderLabels(headerlist);
73    tableWidget->horizontalHeader()->setHighlightSections(false);
74    tableWidget->verticalHeader()->hide();
75    tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
76    tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
77    tableWidget->setSortingEnabled(false); /* rows move on insert if sorting enabled */
78
79    QString fileset_comsep("");
80    bool first = true;
81    QStringList notFoundList = m_console->fileset_list;
82    foreach(QString filesetName, m_console->fileset_list) {
83       if (first) {
84          fileset_comsep += "'" + filesetName + "'";
85          first = false;
86       }
87       else
88          fileset_comsep += ",'" + filesetName + "'";
89    }
90
91    int row = 0;
92    tableWidget->setRowCount(m_console->fileset_list.count());
93    if (fileset_comsep != "") {
94       /* Set up query QString and header QStringList */
95       QString query("");
96       query += "SELECT FileSet AS Name, FileSetId AS Id, CreateTime"
97            " FROM FileSet"
98            " WHERE FileSetId IN (SELECT MAX(FileSetId) FROM FileSet WHERE";
99       query += " FileSet IN (" + fileset_comsep + ")";
100       query += " GROUP BY FileSet) ORDER BY FileSet";
101
102       QStringList results;
103       if (mainWin->m_sqlDebug) {
104          Pmsg1(000, "FileSet query cmd : %s\n",query.toUtf8().data());
105       }
106       if (m_console->sql_cmd(query, results)) {
107          QStringList fieldlist;
108
109          /* Iterate through the record returned from the query */
110          foreach (QString resultline, results) {
111             fieldlist = resultline.split("\t");
112
113             /* remove this fileSet from notFoundList */
114             int indexOf = notFoundList.indexOf(fieldlist[0]);
115             if (indexOf != -1) { notFoundList.removeAt(indexOf); }
116
117             TableItemFormatter item(*tableWidget, row);
118
119             /* Iterate through fields in the record */
120             QStringListIterator fld(fieldlist);
121             int col = 0;
122
123             /* name */
124             item.setTextFld(col++, fld.next());
125
126             /* id */
127             item.setNumericFld(col++, fld.next());
128
129             /* creation time */
130             item.setTextFld(col++, fld.next());
131
132             row++;
133          }
134       }
135    }
136    foreach(QString filesetName, notFoundList) {
137       TableItemFormatter item(*tableWidget, row);
138       item.setTextFld(0, filesetName);
139       row++;
140    }
141
142    /* set default sorting */
143    tableWidget->sortByColumn(headerlist.indexOf(tr("FileSet Name")), Qt::AscendingOrder);
144    tableWidget->setSortingEnabled(true);
145
146    /* Resize rows and columns */
147    tableWidget->resizeColumnsToContents();
148    tableWidget->resizeRowsToContents();
149
150    /* make read only */
151    int rcnt = tableWidget->rowCount();
152    int ccnt = tableWidget->columnCount();
153    for(int r=0; r < rcnt; r++) {
154       for(int c=0; c < ccnt; c++) {
155          QTableWidgetItem* item = tableWidget->item(r, c);
156          if (item) {
157             item->setFlags(Qt::ItemFlags(item->flags() & (~Qt::ItemIsEditable)));
158          }
159       }
160    }
161    m_populated = true;
162 }
163
164 /*
165  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
166  * The tree has been populated.
167  */
168 void FileSet::PgSeltreeWidgetClicked()
169 {
170    if (!m_populated) {
171       populateTable();
172       createContextMenu();
173    }
174    if (!isOnceDocked()) {
175       dockPage();
176    }
177 }
178
179 /*
180  * Added to set the context menu policy based on currently active treeWidgetItem
181  * signaled by currentItemChanged
182  */
183 void FileSet::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem)
184 {
185    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
186    if (m_checkcurwidget && currentwidgetitem) {
187       int currentRow = currentwidgetitem->row();
188       QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
189       m_currentlyselected = currentrowzeroitem->text();
190
191       /* The Previous item */
192       if (previouswidgetitem) { /* avoid a segfault if first time */
193          tableWidget->removeAction(actionStatusFileSetInConsole);
194          tableWidget->removeAction(actionShowJobs);
195       }
196
197       if (m_currentlyselected.length() != 0) {
198          /* set a hold variable to the fileset name in case the context sensitive
199           * menu is used */
200          tableWidget->addAction(actionStatusFileSetInConsole);
201          tableWidget->addAction(actionShowJobs);
202       }
203    }
204 }
205
206 /*
207  * Setup a context menu
208  * Made separate from populate so that it would not create context menu over and
209  * over as the tree is repopulated.
210  */
211 void FileSet::createContextMenu()
212 {
213    tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
214    tableWidget->addAction(actionRefreshFileSet);
215    connect(tableWidget, SIGNAL(
216            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
217            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
218    /* connect to the action specific to this pages class */
219    connect(actionRefreshFileSet, SIGNAL(triggered()), this,
220                 SLOT(populateTable()));
221    connect(actionStatusFileSetInConsole, SIGNAL(triggered()), this,
222                 SLOT(consoleShowFileSet()));
223    connect(actionShowJobs, SIGNAL(triggered()), this,
224                 SLOT(showJobs()));
225 }
226
227 /*
228  * Function responding to actionListJobsofFileSet which calls mainwin function
229  * to create a window of a list of jobs of this fileset.
230  */
231 void FileSet::consoleShowFileSet()
232 {
233    QString cmd("show fileset=\"");
234    cmd += m_currentlyselected + "\"";
235    consoleCommand(cmd);
236 }
237
238 /*
239  * Virtual function which is called when this page is visible on the stack
240  */
241 void FileSet::currentStackItem()
242 {
243    if (!m_populated) {
244       populateTable();
245       /* Create the context menu for the fileset table */
246       createContextMenu();
247    }
248 }
249
250 /*
251  * Save user settings associated with this page
252  */
253 void FileSet::writeSettings()
254 {
255    QSettings settings(m_console->m_dir->name(), "bat");
256    settings.beginGroup("FileSet");
257    settings.setValue("geometry", saveGeometry());
258    settings.endGroup();
259 }
260
261 /*
262  * Read and restore user settings associated with this page
263  */
264 void FileSet::readSettings()
265 {
266    QSettings settings(m_console->m_dir->name(), "bat");
267    settings.beginGroup("FileSet");
268    restoreGeometry(settings.value("geometry").toByteArray());
269    settings.endGroup();
270 }
271
272 /*
273  * Create a JobList object pre-populating a fileset
274  */
275 void FileSet::showJobs()
276 {
277    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
278    mainWin->createPageJobList("", "", "", m_currentlyselected, parentItem);
279 }