]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/fileset/fileset.cpp
Create a prune page. Populate it from medialist and clients.
[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    /* add context sensitive menu items specific to this classto the page
57     * selector tree. m_contextActions is QList of QActions */
58    m_contextActions.append(actionRefreshFileSet);
59 }
60
61 FileSet::~FileSet()
62 {
63    writeSettings();
64 }
65
66 /*
67  * The main meat of the class!!  The function that querries the director and 
68  * creates the widgets with appropriate values.
69  */
70 void FileSet::populateTree()
71 {
72    QTreeWidgetItem *filesetItem, *topItem;
73
74    if (!m_console->preventInUseConnect())
75        return;
76
77    m_checkcurwidget = false;
78    mp_treeWidget->clear();
79    m_checkcurwidget = true;
80
81    QStringList headerlist = (QStringList() << "  FileSet Name  " << "FileSet Id"
82        << "Create Time");
83
84    topItem = new QTreeWidgetItem(mp_treeWidget);
85    topItem->setText(0, "FileSet");
86    topItem->setData(0, Qt::UserRole, 0);
87    topItem->setExpanded(true);
88
89    mp_treeWidget->setColumnCount(headerlist.count());
90    mp_treeWidget->setHeaderLabels(headerlist);
91
92    foreach(QString filesetName, m_console->fileset_list) {
93       filesetItem = new QTreeWidgetItem(topItem);
94       filesetItem->setText(0, filesetName);
95       filesetItem->setData(0, Qt::UserRole, 1);
96       filesetItem->setExpanded(true);
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 FileSet";
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 == 1){
113             QString resultline;
114             QString field;
115             QStringList fieldlist;
116             /* there will only be one of these */
117             foreach (resultline, results) {
118                fieldlist = resultline.split("\t");
119                int index = 0;
120                /* Iterate through fields in the record */
121                foreach (field, fieldlist) {
122                   field = field.trimmed();  /* strip leading & trailing spaces */
123                   filesetItem->setData(index, Qt::UserRole, 1);
124                   /* Put media fields under the pool tree item */
125                   filesetItem->setData(index, Qt::UserRole, 1);
126                   filesetItem->setText(index, field);
127                   index++;
128                }
129             }
130          }
131       }
132    }
133    /* Resize the columns */
134    for (int cnter=1; cnter<headerlist.size(); cnter++) {
135       mp_treeWidget->resizeColumnToContents(cnter);
136    }
137
138 }
139
140 /*
141  * When the treeWidgetItem in the page selector tree is singleclicked, Make sure
142  * The tree has been populated.
143  */
144 void FileSet::PgSeltreeWidgetClicked()
145 {
146    if (!m_populated) {
147       populateTree();
148       createContextMenu();
149       m_populated = true;
150    }
151 }
152
153 /*
154  * Added to set the context menu policy based on currently active treeWidgetItem
155  * signaled by currentItemChanged
156  */
157 void FileSet::treeItemChanged(QTreeWidgetItem *currentwidgetitem, 
158                               QTreeWidgetItem *previouswidgetitem )
159 {
160    /* m_checkcurwidget checks to see if this is during a refresh, which will segfault */
161    if (m_checkcurwidget) {
162       /* The Previous item */
163       if (previouswidgetitem) { /* avoid a segfault if first time */
164          int treedepth = previouswidgetitem->data(0, Qt::UserRole).toInt();
165          if (treedepth == 1) {
166             mp_treeWidget->removeAction(actionStatusFileSetInConsole);
167          }
168       }
169
170       int treedepth = currentwidgetitem->data(0, Qt::UserRole).toInt();
171       if (treedepth == 1){
172          /* set a hold variable to the fileset name in case the context sensitive
173           * menu is used */
174          m_currentlyselected=currentwidgetitem->text(0);
175          mp_treeWidget->addAction(actionStatusFileSetInConsole);
176       }
177    }
178 }
179
180 /* 
181  * Setup a context menu 
182  * Made separate from populate so that it would not create context menu over and
183  * over as the tree is repopulated.
184  */
185 void FileSet::createContextMenu()
186 {
187    mp_treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
188    mp_treeWidget->addAction(actionRefreshFileSet);
189    connect(mp_treeWidget, SIGNAL(
190            currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
191            this, SLOT(treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
192    /* connect to the action specific to this pages class */
193    connect(actionRefreshFileSet, SIGNAL(triggered()), this,
194                 SLOT(populateTree()));
195    connect(actionStatusFileSetInConsole, SIGNAL(triggered()), this,
196                 SLOT(consoleStatusFileSet()));
197 }
198
199 /*
200  * Function responding to actionListJobsofFileSet which calls mainwin function
201  * to create a window of a list of jobs of this fileset.
202  */
203 void FileSet::consoleStatusFileSet()
204 {
205    QString cmd("status fileset=");
206    cmd += m_currentlyselected;
207    consoleCommand(cmd);
208 }
209
210 /*
211  * Virtual function which is called when this page is visible on the stack
212  */
213 void FileSet::currentStackItem()
214 {
215    if(!m_populated) {
216       populateTree();
217       /* Create the context menu for the fileset tree */
218       createContextMenu();
219       m_populated=true;
220    }
221 }
222
223 /*
224  * Save user settings associated with this page
225  */
226 void FileSet::writeSettings()
227 {
228    QSettings settings(m_console->m_dir->name(), "bat");
229    settings.beginGroup("FileSet");
230    settings.setValue("geometry", saveGeometry());
231    settings.endGroup();
232 }
233
234 /*
235  * Read and restore user settings associated with this page
236  */
237 void FileSet::readSettings()
238
239    QSettings settings(m_console->m_dir->name(), "bat");
240    settings.beginGroup("FileSet");
241    restoreGeometry(settings.value("geometry").toByteArray());
242    settings.endGroup();
243 }