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