]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mediaedit/mediaedit.cpp
Add icon to label class in page selector.
[bacula/bacula] / bacula / src / qt-console / mediaedit / mediaedit.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  *   Version $Id: batstack.h 4230 2007-02-21 20:07:37Z kerns $
30  *
31  *   Dirk Bartley, March 2007
32  */
33  
34 #include <QAbstractEventDispatcher>
35 #include <QTableWidgetItem>
36 #include <QMessageBox>
37 #include "bat.h"
38 #include "mediaedit.h"
39 #include <inttypes.h>
40
41 /*
42  * A constructor 
43  */
44 MediaEdit::MediaEdit(QTreeWidgetItem *parentWidget, QString &mediaId)
45 {
46    setupUi(this);
47    m_name = "Media Edit";
48    pgInitialize(parentWidget);
49    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
50    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge-edit.svg")));
51    m_closeable = true;
52    dockPage();
53    setCurrent();
54
55    connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
56    connect(cancelButton, SIGNAL(pressed()), this, SLOT(cancelButtonPushed()));
57    connectSpins();
58    connect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
59    connect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
60    connect(retentionRadio, SIGNAL(pressed()), this, SLOT(retentionRadioPressed()));
61    connect(useDurationRadio, SIGNAL(pressed()), this, SLOT(useDurationRadioPressed()));
62
63    m_pool = "";
64    m_status = "";
65    m_slot = 0;
66
67    if (!m_console->preventInUseConnect())
68       return;
69
70    /* The media's pool */
71    poolCombo->addItems(m_console->pool_list);
72
73    /* The media's Status */
74    QStringList statusList = (QStringList() << "Full" << "Used" << "Append" << "Error" << "Purged" << "Recycled");
75    statusCombo->addItems(statusList);
76
77    /* Set up the query for the default values */
78    QStringList FieldList = (QStringList()
79       << "Media.VolumeName" << "Pool.Name" << "Media.VolStatus" << "Media.Slot"
80       << "Media.VolRetention" << "Media.VolUseDuration");
81    QStringList AsList = (QStringList()
82       << "VolumeName" << "PoolName" << "Status" << "Slot"
83       << "Retention" << "UseDuration");
84    int i = 0;
85    QString query("SELECT ");
86    foreach (QString field, FieldList) {
87       if (i != 0) {
88          query += ", ";
89       }
90       query += field + " AS " + AsList[i];
91       i += 1;
92    }
93    query += " FROM Media, Pool WHERE Media.PoolId=Pool.PoolId";
94    query += " AND Media.MediaId='" + mediaId + "'";
95    query += " ORDER BY Pool.Name";
96
97    if (mainWin->m_sqlDebug) {
98       Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
99    }
100    QStringList results;
101    if (m_console->sql_cmd(query, results)) {
102       QString field;
103       QStringList fieldlist;
104
105       /* Iterate through the lines of results, there should only be one. */
106       foreach (QString resultline, results) {
107          fieldlist = resultline.split("\t");
108          i = 0;
109
110          /* Iterate through fields in the record */
111          foreach (field, fieldlist) {
112             field = field.trimmed();  /* strip leading & trailing spaces */
113             bool ok;
114             if (i == 0) {
115                m_mediaName = field;
116                volumeLabel->setText(QString("Volume : %1").arg(m_mediaName));
117             } else if (i == 1) {
118                m_pool = field;
119             } else if (i == 2) {
120                m_status = field;
121             } else if (i == 3) {
122                m_slot = field.toInt(&ok, 10);
123                if (!ok){ m_slot = 0; }
124             } else if (i == 4) {
125                m_retention = field.toLong(&ok, 10);
126                if (!ok){ m_retention = 0; }
127             } else if (i == 5) {
128                m_useDuration = field.toLong(&ok, 10);
129                if (!ok){ m_useDuration = 0; }
130             }
131             i++;
132          } /* foreach field */
133       } /* foreach resultline */
134    } /* if results from query */
135
136    if (m_mediaName != "") {
137       int index;
138       /* default value for pool */
139       index = poolCombo->findText(m_pool, Qt::MatchExactly);
140       if (index != -1) {
141          poolCombo->setCurrentIndex(index);
142       }
143
144       /* default value for status */
145       index = statusCombo->findText(m_status, Qt::MatchExactly);
146       if (index != -1) {
147          statusCombo->setCurrentIndex(index);
148       }
149       slotSpin->setValue(m_slot);
150       retentionSpin->setValue(m_retention);
151       useDurationSpin->setValue(m_useDuration);
152
153       this->show();
154    } else {
155       QMessageBox::warning(this, "No Volume name", "No Volume name given",
156                            QMessageBox::Ok, QMessageBox::Ok);
157       return;
158    }
159 }
160
161 /*
162  * Function to handle updating the record then closing the page
163  */
164 void MediaEdit::okButtonPushed()
165 {
166 //update volume=xxx slots MaxVolJobs=nnn MaxVolBytes=nnn Recycle=yes|no
167 //         enabled=n recyclepool=zzz
168 // done pool=yyy volstatus=xxx slot=nnn VolUse=ddd VolRetention=ddd
169    QString scmd;
170    this->hide();
171    bool docmd = false;
172    scmd = QString("update volume=\"%1\"")
173                   .arg(m_mediaName);
174    if (m_pool != poolCombo->currentText()) {
175        scmd += " pool=\"" + poolCombo->currentText() + "\"";
176        docmd = true;
177    }
178    if (m_status != statusCombo->currentText()) {
179        scmd += " volstatus=\"" + statusCombo->currentText() + "\"";
180        docmd = true;
181    }
182    if (m_slot != slotSpin->value()) {
183        scmd += " slot=" + QString().setNum(slotSpin->value());
184        docmd = true;
185    }
186    if (m_retention != retentionSpin->value()) {
187        scmd += " VolRetention=" + QString().setNum(retentionSpin->value());
188        docmd = true;
189    }
190    if (m_useDuration != useDurationSpin->value()) {
191        scmd += " VolUse=" + QString().setNum(useDurationSpin->value());
192        docmd = true;
193    }
194    if (docmd) {
195       if (mainWin->m_commandDebug) {
196          Pmsg1(000, "sending command : %s\n",scmd.toUtf8().data());
197       }
198       consoleCommand(scmd);
199    }
200    closeStackPage();
201 }
202
203 /* close if cancel */
204 void MediaEdit::cancelButtonPushed()
205 {
206    closeStackPage();
207 }
208
209 /*
210  * Slot for user changed retention
211  */
212 void MediaEdit::retentionChanged()
213 {
214    retentionRadio->setChecked(true);
215    setSpins(retentionSpin->value());
216 }
217
218 /*
219  * Slot for user changed the use duration
220  */
221 void MediaEdit::useDurationChanged()
222 {
223    useDurationRadio->setChecked(true);
224    setSpins(useDurationSpin->value());
225 }
226
227 /*
228  * Set the 5 duration spins from a known duration value
229  */
230 void MediaEdit::setSpins(int value)
231 {
232    int years, days, hours, minutes, seconds, left;
233         
234    years = abs(value / 31536000);
235    left = value - years * 31536000;
236    days = abs(left / 86400);
237    left = left - days * 86400;
238    hours = abs(left / 3600);
239    left = left - hours * 3600;
240    minutes = abs(left / 60);
241    seconds = left - minutes * 60;
242    disconnectSpins();
243    yearsSpin->setValue(years);
244    daysSpin->setValue(days);
245    hoursSpin->setValue(hours);
246    minutesSpin->setValue(minutes);
247    secondsSpin->setValue(seconds);
248    connectSpins();
249 }
250
251 /*
252  * This slot is called any time any one of the 5 duration spins a changed.
253  */
254 void MediaEdit::durationChanged()
255 {
256    disconnectSpins();
257    if (secondsSpin->value() == -1) {
258       secondsSpin->setValue(59);
259       minutesSpin->setValue(minutesSpin->value()-1);
260    }
261    if (minutesSpin->value() == -1) {
262       minutesSpin->setValue(59);
263       hoursSpin->setValue(hoursSpin->value()-1);
264    }
265    if (hoursSpin->value() == -1) {
266       hoursSpin->setValue(23);
267       daysSpin->setValue(daysSpin->value()-1);
268    }
269    if (daysSpin->value() == -1) {
270       daysSpin->setValue(364);
271       yearsSpin->setValue(yearsSpin->value()-1);
272    }
273    if (yearsSpin->value() == -1) {
274       yearsSpin->setValue(0);
275    }
276
277    if (secondsSpin->value() == 60) {
278       secondsSpin->setValue(0);
279       minutesSpin->setValue(minutesSpin->value()+1);
280    }
281    if (minutesSpin->value() == 60) {
282       minutesSpin->setValue(0);
283       hoursSpin->setValue(hoursSpin->value()+1);
284    }
285    if (hoursSpin->value() == 24) {
286       hoursSpin->setValue(0);
287       daysSpin->setValue(daysSpin->value()+1);
288    }
289    if (daysSpin->value() == 365) {
290       daysSpin->setValue(0);
291       yearsSpin->setValue(yearsSpin->value()+1);
292    }
293    connectSpins();
294    if (retentionRadio->isChecked()) {
295       int retention;
296       retention = secondsSpin->value() + minutesSpin->value() * 60 + 
297          hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
298          yearsSpin->value() * 31536000;
299       disconnect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
300       retentionSpin->setValue(retention);
301       connect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
302    }
303    if (useDurationRadio->isChecked()) {
304       int useDuration;
305       useDuration = secondsSpin->value() + minutesSpin->value() * 60 + 
306          hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
307          yearsSpin->value() * 31536000;
308       disconnect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
309       useDurationSpin->setValue(useDuration);
310       connect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
311    }
312 }
313
314 /* Connect the spins */
315 void MediaEdit::connectSpins()
316 {
317    connect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
318    connect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
319    connect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
320    connect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
321    connect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
322 }
323
324 /* disconnect spins so that we can set the value of other spin from changed duration spin */
325 void MediaEdit::disconnectSpins()
326 {
327    disconnect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
328    disconnect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
329    disconnect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
330    disconnect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
331    disconnect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
332 }
333
334 /* slot for setting spins when retention radio checked */
335 void MediaEdit::retentionRadioPressed()
336 {
337    setSpins(retentionSpin->value());
338 }
339
340 /* slot for setting spins when duration radio checked */
341 void MediaEdit::useDurationRadioPressed()
342 {
343    setSpins(useDurationSpin->value());
344 }