]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/fileset/fileset.cpp
Add icons for the page selector.
[bacula/bacula] / bacula / src / qt-console / fileset / fileset.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 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 plus additions
11    that are listed 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 John Walker.
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: fileset.cpp 4230 2007-02-21 20:07:37Z kerns $
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
43 FileSet::FileSet()
44 {
45    setupUi(this);
46    m_name = "FileSets";
47    pgInitialize();
48    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
49    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/system-file-manager.png")));
50
51    /* mp_treeWidget, FileSet Tree Tree Widget inherited from ui_fileset.h */
52    m_populated = false;
53    m_checkcurwidget = true;
54    m_closeable = false;
55    readSettings();
56 }
57
58 FileSet::~FileSet()
59 {
60    writeSettings();
61 }
62
63 /*
64  * The main meat of the class!!  The function that querries the director and 
65  * creates the widgets with appropriate values.
66  */
67 void FileSet::populateTree()
68 {
69    QTreeWidgetItem *filesetItem, *topItem;
70
71    if (!m_console->preventInUseConnect())
72        return;
73
74    m_checkcurwidget = false;
75    mp_treeWidget->clear();
76    m_checkcurwidget = true;
77
78    QStringList headerlist = (QStringList() << "  FileSet Name  " << "FileSet Id"
79        << "Create Time");
80
81    topItem = new QTreeWidgetItem(mp_treeWidget);
82    topItem->setText(0, "FileSet");
83    topItem->setData(0, Qt::UserRole, 0);
84    topItem->setExpanded(true);
85
86    mp_treeWidget->setColumnCount(headerlist.count());
87    mp_treeWidget->setHeaderLabels(headerlist);
88
89    foreach(QString filesetName, m_console->fileset_list) {
90       filesetItem = new QTreeWidgetItem(topItem);
91       filesetItem->setText(0, filesetName);
92       filesetItem->setData(0, Qt::UserRole, 1);
93       filesetItem->setExpanded(true);
94
95       /* Set up query QString and header QStringList */
96       QString query("");
97       query += "SELECT FileSet AS Name, FileSetId AS Id, CreateTime"
98            " FROM FileSet"
99            " WHERE ";
100       query += " FileSet='" + filesetName + "'";
101       query += " ORDER BY FileSet";
102
103       QStringList results;
104       if (mainWin->m_sqlDebug) {
105          Pmsg1(000, "FileSet query cmd : %s\n",query.toUtf8().data());
106       }
107       if (m_console->sql_cmd(query, results)) {
108          int resultCount = results.count();
109          if (resultCount == 1){
110             QString resultline;
111             QString field;
112             QStringList fieldlist;
113             /* there will only be one of these */
114             foreach (resultline, results) {
115                fieldlist = resultline.split("\t");
116                int index = 0;
117                /* Iterate through fields in the record */
118                foreach (field, fieldlist) {
119                   field = field.trimmed();  /* strip leading & trailing spaces */
120                   filesetItem->setData(index, Qt::UserRole, 1);
121                   /* Put media fields under the pool tree item */
122                   filesetItem->setData(index, Qt::UserRole, 1);
123                   filesetItem->setText(index, field);
124                   index++;
125                }
126             }
127          }
128       }
129    }
130    /* Resize the columns */
131    for (int cnter=1; cnter<headerlist.size(); cnter++) {
132       mp_treeWidget->resizeColumnToContents(cnter);
133    }
134
135 }
136
137 /*
138  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
139  * The tree has been populated.
140  */
141 void FileSet::PgSeltreeWidgetClicked()
142 {
143    if (!m_populated) {
144       populateTree();
145       createContextMenu();
146       m_populated = true;
147    }
148 }
149
150 /*
151  * Added to set the context menu policy based on currently active treeWidgetItem
152  * signaled by currentItemChanged
153  */
154 void FileSet::treeItemChanged(QTreeWidgetItem *currentwidgetitem, 
155                               QTreeWidgetItem *previouswidgetitem )
156 {
157    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
158    if (m_checkcurwidget) {
159       /* The Previous item */
160       if (previouswidgetitem) { /* avoid a segfault if first time */
161          int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
162          if (treedepth == 1) {
163             mp_treeWidget->removeAction(actionStatusFileSetInConsole);
164          }
165       }
166
167       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
168       if (treedepth == 1){
169          /* set a hold variable to the fileset name in case the context sensitive
170           * menu is used */
171          m_currentlyselected=currentwidgetitem->text(0);
172          mp_treeWidget->addAction(actionStatusFileSetInConsole);
173       }
174    }
175 }
176
177 /* 
178  * Setup a context menu 
179  * Made separate from populate so that it would not create context menu over and
180  * over as the tree is repopulated.
181  */
182 void FileSet::createContextMenu()
183 {
184    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
185    mp_treeWidget->addAction(actionRefreshFileSet);
186    connect(mp_treeWidget, SIGNAL(
187            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
188            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
189    /* connect to the action specific to this pages class */
190    connect(actionRefreshFileSet, SIGNAL(triggered()), this,
191                 SLOT(populateTree()));
192    connect(actionStatusFileSetInConsole, SIGNAL(triggered()), this,
193                 SLOT(consoleStatusFileSet()));
194 }
195
196 /*
197  * Function responding to actionListJobsofFileSet which calls mainwin function
198  * to create a window of a list of jobs of this fileset.
199  */
200 void FileSet::consoleStatusFileSet()
201 {
202    QString cmd("status fileset=");
203    cmd += m_currentlyselected;
204    consoleCommand(cmd);
205 }
206
207 /*
208  * Virtual function which is called when this page is visible on the stack
209  */
210 void FileSet::currentStackItem()
211 {
212    if(!m_populated) {
213       populateTree();
214       /* add context sensitive menu items specific to this classto the page
215        * selector tree. m_contextActions is QList of QActions, so this is 
216        * only done once with the first population. */
217       m_contextActions.append(actionRefreshFileSet);
218       /* Create the context menu for the fileset tree */
219       createContextMenu();
220       m_populated=true;
221    }
222 }
223
224 /*
225  * Save user settings associated with this page
226  */
227 void FileSet::writeSettings()
228 {
229    QSettings settings(m_console->m_dir->name(), "bat");
230    settings.beginGroup("FileSet");
231    settings.setValue("geometry", saveGeometry());
232    settings.endGroup();
233 }
234
235 /*
236  * Read and restore user settings associated with this page
237  */
238 void FileSet::readSettings()
239
240    QSettings settings(m_console->m_dir->name(), "bat");
241    settings.beginGroup("FileSet");
242    restoreGeometry(settings.value("geometry").toByteArray());
243    settings.endGroup();
244 }