]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/storage/content.cpp
Merge branch 'master' of ssh://bacula.git.sourceforge.net/gitroot/bacula
[bacula/bacula] / bacula / src / qt-console / storage / content.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2009 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 #include "bat.h"
30 #include <QAbstractEventDispatcher>
31 #include <QMenu>
32 #include "content.h"
33 #include "label/label.h"
34 #include "mount/mount.h"
35 #include "util/fmtwidgetitem.h"
36 #include "status/storstat.h"
37
38 // 
39 // TODO: List tray
40 //       List drives in autochanger
41 //       use user selection to add slot= argument
42 // 
43
44 Content::Content(QString storage)
45 {
46    setupUi(this);
47    pgInitialize(tr("Storage Content"));
48    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
49    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/package-x-generic.png")));
50
51    m_populated = false;
52    m_firstpopulation = true;
53    m_checkcurwidget = true;
54    m_closeable = true;
55    m_currentStorage = storage;
56
57    connect(pbUpdate, SIGNAL(clicked()), this,
58            SLOT(consoleUpdateSlots()));
59
60    connect(pbLabel, SIGNAL(clicked()), this,
61            SLOT(consoleLabelStorage()));
62
63    connect(pbMount, SIGNAL(clicked()), this,
64            SLOT(consoleMountStorage()));
65
66    connect(pbUnmount, SIGNAL(clicked()), this,
67            SLOT(consoleUnMountStorage()));
68
69    connect(pbStatus, SIGNAL(clicked()), this,
70            SLOT(statusStorageWindow()));
71
72    connect(pbRelease, SIGNAL(clicked()), this,
73            SLOT(consoleRelease()));
74
75    dockPage();
76    setCurrent();
77 }
78
79 /* Label Media populating current storage by default */
80 void Content::consoleLabelStorage()
81 {
82    new labelPage(m_currentStorage);
83 }
84
85 /* Mount currently selected storage */
86 void Content::consoleMountStorage()
87 {
88    setConsoleCurrent();
89    /* if this storage is an autochanger, lets ask for the slot */
90    new mountDialog(m_console, m_currentStorage);
91 }
92
93 /* Unmount Currently selected storage */
94 void Content::consoleUnMountStorage()
95 {
96    QString cmd("umount storage=");
97    cmd += m_currentStorage;
98    consoleCommand(cmd);
99 }
100
101 void Content::statusStorageWindow()
102 {
103    /* if one exists, then just set it current */
104    bool found = false;
105    foreach(Pages *page, mainWin->m_pagehash) {
106       if (mainWin->currentConsole() == page->console()) {
107          if (page->name() == tr("Storage Status %1").arg(m_currentStorage)) {
108             found = true;
109             page->setCurrent();
110          }
111       }
112    }
113    if (!found) {
114       QTreeWidgetItem *parentItem = mainWin->getFromHash(this);
115       new StorStat(m_currentStorage, parentItem);
116    }
117 }
118
119 /*
120  * The main meat of the class!!  The function that querries the director and 
121  * creates the widgets with appropriate values.
122  */
123 void Content::populateContent()
124 {
125    char buf[200];
126    time_t t;
127    struct tm tm;
128
129    QStringList results_all;
130    QString cmd("status slots drive=0 storage=\"" + m_currentStorage + "\"");
131    m_console->dir_cmd(cmd, results_all);
132
133    Freeze frz(*tableContent); /* disable updating*/
134    tableContent->clearContents();
135
136    // take only valid records
137    QStringList results = results_all.filter(QRegExp("^[0-9]+\\|"));
138    tableContent->setRowCount(results.size());
139
140    QString resultline;
141    QStringList fieldlist;
142    int row = 0;
143
144    foreach (resultline, results) {
145       fieldlist = resultline.split("|");
146       if (fieldlist.size() < 9)
147          continue; /* some fields missing, ignore row */
148
149       int index=0;
150       QStringListIterator fld(fieldlist);
151       TableItemFormatter slotitem(*tableContent, row);
152
153       /* Slot */
154       slotitem.setNumericFld(index++, fld.next()); 
155
156       /* Real Slot */
157       if (fld.next() != "") {
158
159          /* Volume */
160          slotitem.setTextFld(index++, fld.next());
161          
162          /* Bytes */
163          slotitem.setBytesFld(index++, fld.next());
164          
165          /* Status */
166          slotitem.setVolStatusFld(index++, fld.next());
167          
168          /* MediaType */
169          slotitem.setTextFld(index++, fld.next());
170          
171          /* Pool */
172          slotitem.setTextFld(index++, fld.next());
173          
174          t = fld.next().toInt();
175          if (t > 0) {
176             /* LastW */
177             localtime_r(&t, &tm);
178             strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
179             slotitem.setTextFld(index++, QString(buf));
180             
181             /* Expire */
182             t = fld.next().toInt();
183             localtime_r(&t, &tm);
184             strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
185             slotitem.setTextFld(index++, QString(buf));
186          }
187       }
188       row++;
189    }
190
191
192    tableContent->verticalHeader()->hide();
193    tableContent->sortByColumn(0, Qt::AscendingOrder);
194    tableContent->setSortingEnabled(true);
195    tableContent->resizeColumnsToContents();
196    tableContent->resizeRowsToContents();
197
198    /* make read only */
199    int rcnt = tableContent->rowCount();
200    int ccnt = tableContent->columnCount();
201    for(int r=0; r < rcnt; r++) {
202       for(int c=0; c < ccnt; c++) {
203          QTableWidgetItem* item = tableContent->item(r, c);
204          if (item) {
205             item->setFlags(Qt::ItemFlags(item->flags() & (~Qt::ItemIsEditable)));
206          }
207       }
208    }
209    m_populated = true;
210
211    tableTray->verticalHeader()->hide();
212    tableDrive->verticalHeader()->hide();
213 }
214
215 /*
216  * Virtual function which is called when this page is visible on the stack
217  */
218 void Content::currentStackItem()
219 {
220    if(!m_populated) {
221       populateContent();
222    }
223 }
224
225 void Content::treeItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)
226 {
227
228 }
229
230 /* Update Slots */
231 void Content::consoleUpdateSlots()
232 {
233    // TODO: get selected slots
234    QString cmd("update slots storage=");
235    cmd += m_currentStorage;
236    consoleCommand(cmd);
237 }
238
239 /* Release a tape in the drive */
240 void Content::consoleRelease()
241 {
242    QString cmd("release storage=");
243    cmd += m_currentStorage;
244    consoleCommand(cmd);
245 }