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