]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/fileset/fileset.cpp
Make table widget items read only again.
[bacula/bacula] / bacula / src / qt-console / fileset / fileset.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 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 two of the GNU 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 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()
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    dockPage();
61 }
62
63 FileSet::~FileSet()
64 {
65    writeSettings();
66 }
67
68 /*
69  * The main meat of the class!!  The function that querries the director and 
70  * creates the widgets with appropriate values.
71  */
72 void FileSet::populateTable()
73 {
74    m_populated = true;
75
76    Freeze frz(*tableWidget); /* disable updating*/
77
78    m_checkcurwidget = false;
79    tableWidget->clear();
80    m_checkcurwidget = true;
81
82    QStringList headerlist = (QStringList() << tr("FileSet Name") << tr("FileSet Id")
83        << tr("Create Time"));
84
85    tableWidget->setColumnCount(headerlist.count());
86    tableWidget->setHorizontalHeaderLabels(headerlist);
87    tableWidget->horizontalHeader()->setHighlightSections(false);
88    tableWidget->setRowCount(m_console->fileset_list.count());
89    tableWidget->verticalHeader()->hide();
90    tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
91    tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
92    tableWidget->setSortingEnabled(false); /* rows move on insert if sorting enabled */
93
94    QString fileset_comsep("");
95    bool first = true;
96    foreach(QString filesetName, m_console->fileset_list) {
97       if (first) {
98          fileset_comsep += "'" + filesetName + "'";
99          first = false;
100       }
101       else
102          fileset_comsep += ",'" + filesetName + "'";
103    }
104
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          int row = 0;
120          QStringList fieldlist;
121
122          /* Iterate through the record returned from the query */
123          foreach (QString resultline, results) {
124             fieldlist = resultline.split("\t");
125
126             TableItemFormatter item(*tableWidget, row);
127   
128             /* Iterate through fields in the record */
129             QStringListIterator fld(fieldlist);
130             int col = 0;
131
132             /* name */
133             item.setTextFld(col++, fld.next());
134
135             /* id */
136             item.setNumericFld(col++, fld.next());
137
138             /* creation time */
139             item.setTextFld(col++, fld.next());
140
141             row++;
142          }
143       }
144    }
145    /* set default sorting */
146    tableWidget->sortByColumn(headerlist.indexOf(tr("Create Time")), Qt::DescendingOrder);
147    tableWidget->setSortingEnabled(true);
148    
149    /* Resize rows and columns */
150    tableWidget->resizeColumnsToContents();
151    tableWidget->resizeRowsToContents();
152
153    /* make read only */
154    int rcnt = tableWidget->rowCount();
155    int ccnt = tableWidget->columnCount();
156    for(int r=0; r < rcnt; r++) {
157       for(int c=0; c < ccnt; c++) {
158          QTableWidgetItem* item = tableWidget->item(r, c);
159          item->setFlags(Qt::ItemFlags(item->flags() & (~Qt::ItemIsEditable)));
160       }
161    }
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 }
175
176 /*
177  * Added to set the context menu policy based on currently active treeWidgetItem
178  * signaled by currentItemChanged
179  */
180 void FileSet::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem)
181 {
182    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
183    if (m_checkcurwidget) {
184       int currentRow = currentwidgetitem->row();
185       QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
186       m_currentlyselected = currentrowzeroitem->text();
187
188       /* The Previous item */
189       if (previouswidgetitem) { /* avoid a segfault if first time */
190          tableWidget->removeAction(actionStatusFileSetInConsole);
191          tableWidget->removeAction(actionShowJobs);
192       }
193
194       if (m_currentlyselected.length() != 0) {
195          /* set a hold variable to the fileset name in case the context sensitive
196           * menu is used */
197          tableWidget->addAction(actionStatusFileSetInConsole);
198          tableWidget->addAction(actionShowJobs);
199       }
200    }
201 }
202
203 /* 
204  * Setup a context menu 
205  * Made separate from populate so that it would not create context menu over and
206  * over as the tree is repopulated.
207  */
208 void FileSet::createContextMenu()
209 {
210    tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
211    tableWidget->addAction(actionRefreshFileSet);
212    connect(tableWidget, SIGNAL(
213            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
214            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
215    /* connect to the action specific to this pages class */
216    connect(actionRefreshFileSet, SIGNAL(triggered()), this,
217                 SLOT(populateTable()));
218    connect(actionStatusFileSetInConsole, SIGNAL(triggered()), this,
219                 SLOT(consoleStatusFileSet()));
220    connect(actionShowJobs, SIGNAL(triggered()), this,
221                 SLOT(showJobs()));
222 }
223
224 /*
225  * Function responding to actionListJobsofFileSet which calls mainwin function
226  * to create a window of a list of jobs of this fileset.
227  */
228 void FileSet::consoleStatusFileSet()
229 {
230    QString cmd("status fileset=");
231    cmd += m_currentlyselected;
232    consoleCommand(cmd);
233 }
234
235 /*
236  * Virtual function which is called when this page is visible on the stack
237  */
238 void FileSet::currentStackItem()
239 {
240    if(!m_populated) {
241       populateTable();
242       /* Create the context menu for the fileset table */
243       createContextMenu();
244    }
245 }
246
247 /*
248  * Save user settings associated with this page
249  */
250 void FileSet::writeSettings()
251 {
252    QSettings settings(m_console->m_dir->name(), "bat");
253    settings.beginGroup("FileSet");
254    settings.setValue("geometry", saveGeometry());
255    settings.endGroup();
256 }
257
258 /*
259  * Read and restore user settings associated with this page
260  */
261 void FileSet::readSettings()
262
263    QSettings settings(m_console->m_dir->name(), "bat");
264    settings.beginGroup("FileSet");
265    restoreGeometry(settings.value("geometry").toByteArray());
266    settings.endGroup();
267 }
268
269 /*
270  * Create a JobList object pre-populating a fileset
271  */
272 void FileSet::showJobs()
273 {
274    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
275    mainWin->createPageJobList("", "", "", m_currentlyselected, parentItem);
276 }