]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/fileset/fileset.cpp
kes Change Bacula trademark owner from John Walker to Kern Sibbald
[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 <QAbstractEventDispatcher>
39 #include <QMenu>
40 #include "bat.h"
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    QBrush blackBrush(Qt::black);
75
76    if (!m_console->preventInUseConnect())
77        return;
78
79    m_checkcurwidget = false;
80    tableWidget->clear();
81    m_checkcurwidget = true;
82
83    QStringList headerlist = (QStringList() << tr("FileSet Name") << tr("FileSet Id")
84        << tr("Create Time"));
85
86    tableWidget->setColumnCount(headerlist.count());
87    tableWidget->setHorizontalHeaderLabels(headerlist);
88    tableWidget->horizontalHeader()->setHighlightSections(false);
89    tableWidget->setRowCount(m_console->fileset_list.count());
90    tableWidget->verticalHeader()->hide();
91    tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
92    tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
93    tableWidget->setSortingEnabled(false); /* rows move on insert if sorting enabled */
94    int row = 0;
95
96    foreach(QString filesetName, m_console->fileset_list) {
97
98       /* Set up query QString and header QStringList */
99       QString query("");
100       query += "SELECT FileSet AS Name, FileSetId AS Id, CreateTime"
101            " FROM FileSet"
102            " WHERE ";
103       query += " FileSet='" + filesetName + "'";
104       query += " ORDER BY CreateTime DESC LIMIT 1";
105
106       QStringList results;
107       if (mainWin->m_sqlDebug) {
108          Pmsg1(000, "FileSet query cmd : %s\n",query.toUtf8().data());
109       }
110       if (m_console->sql_cmd(query, results)) {
111          int resultCount = results.count();
112          if (resultCount) {
113             /* only use the last one */
114             QString resultline = results[resultCount - 1];
115             QStringList fieldlist = resultline.split("\t");
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          }
133       }
134       row++;
135    }
136    /* set default sorting */
137    tableWidget->sortByColumn(headerlist.indexOf(tr("Create Time")), Qt::DescendingOrder);
138    tableWidget->setSortingEnabled(true);
139    
140    /* Resize rows and columns */
141    tableWidget->resizeColumnsToContents();
142    tableWidget->resizeRowsToContents();
143 }
144
145 /*
146  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
147  * The tree has been populated.
148  */
149 void FileSet::PgSeltreeWidgetClicked()
150 {
151    if (!m_populated) {
152       populateTable();
153       createContextMenu();
154       m_populated = true;
155    }
156 }
157
158 /*
159  * Added to set the context menu policy based on currently active treeWidgetItem
160  * signaled by currentItemChanged
161  */
162 void FileSet::tableItemChanged(QTableWidgetItem *currentwidgetitem, QTableWidgetItem *previouswidgetitem)
163 {
164    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
165    if (m_checkcurwidget) {
166       int currentRow = currentwidgetitem->row();
167       QTableWidgetItem *currentrowzeroitem = tableWidget->item(currentRow, 0);
168       m_currentlyselected = currentrowzeroitem->text();
169
170       /* The Previous item */
171       if (previouswidgetitem) { /* avoid a segfault if first time */
172          tableWidget->removeAction(actionStatusFileSetInConsole);
173          tableWidget->removeAction(actionShowJobs);
174       }
175
176       if (m_currentlyselected.length() != 0) {
177          /* set a hold variable to the fileset name in case the context sensitive
178           * menu is used */
179          tableWidget->addAction(actionStatusFileSetInConsole);
180          tableWidget->addAction(actionShowJobs);
181       }
182    }
183 }
184
185 /* 
186  * Setup a context menu 
187  * Made separate from populate so that it would not create context menu over and
188  * over as the tree is repopulated.
189  */
190 void FileSet::createContextMenu()
191 {
192    tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
193    tableWidget->addAction(actionRefreshFileSet);
194    connect(tableWidget, SIGNAL(
195            currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
196            this, SLOT(tableItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
197    /* connect to the action specific to this pages class */
198    connect(actionRefreshFileSet, SIGNAL(triggered()), this,
199                 SLOT(populateTable()));
200    connect(actionStatusFileSetInConsole, SIGNAL(triggered()), this,
201                 SLOT(consoleStatusFileSet()));
202    connect(actionShowJobs, SIGNAL(triggered()), this,
203                 SLOT(showJobs()));
204 }
205
206 /*
207  * Function responding to actionListJobsofFileSet which calls mainwin function
208  * to create a window of a list of jobs of this fileset.
209  */
210 void FileSet::consoleStatusFileSet()
211 {
212    QString cmd("status fileset=");
213    cmd += m_currentlyselected;
214    consoleCommand(cmd);
215 }
216
217 /*
218  * Virtual function which is called when this page is visible on the stack
219  */
220 void FileSet::currentStackItem()
221 {
222    if(!m_populated) {
223       populateTable();
224       /* Create the context menu for the fileset table */
225       createContextMenu();
226       m_populated=true;
227    }
228 }
229
230 /*
231  * Save user settings associated with this page
232  */
233 void FileSet::writeSettings()
234 {
235    QSettings settings(m_console->m_dir->name(), "bat");
236    settings.beginGroup("FileSet");
237    settings.setValue("geometry", saveGeometry());
238    settings.endGroup();
239 }
240
241 /*
242  * Read and restore user settings associated with this page
243  */
244 void FileSet::readSettings()
245
246    QSettings settings(m_console->m_dir->name(), "bat");
247    settings.beginGroup("FileSet");
248    restoreGeometry(settings.value("geometry").toByteArray());
249    settings.endGroup();
250 }
251
252 /*
253  * Create a JobList object pre-populating a fileset
254  */
255 void FileSet::showJobs()
256 {
257    QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
258    mainWin->createPageJobList("", "", "", m_currentlyselected, parentItem);
259 }