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