]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mediaedit/mediaedit.cpp
447bb183bd803d513554cef724ef27c2ed2ffc1d
[bacula/bacula] / bacula / src / qt-console / mediaedit / mediaedit.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  *   Version $Id$
30  *
31  *   Dirk Bartley, March 2007
32  */
33  
34 #include "bat.h"
35 #include <QAbstractEventDispatcher>
36 #include <QTableWidgetItem>
37 #include <QMessageBox>
38 #include "mediaedit.h"
39
40 /*
41  * A constructor 
42  */
43 MediaEdit::MediaEdit(QTreeWidgetItem *parentWidget, QString &mediaId)
44 {
45    setupUi(this);
46    pgInitialize(tr("Media Edit"), parentWidget);
47    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
48    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge-edit.png")));
49    m_closeable = true;
50    dockPage();
51    setCurrent();
52
53    connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
54    connect(cancelButton, SIGNAL(pressed()), this, SLOT(cancelButtonPushed()));
55    connectSpins();
56    connect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
57    connect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
58    connect(retentionRadio, SIGNAL(pressed()), this, SLOT(retentionRadioPressed()));
59    connect(useDurationRadio, SIGNAL(pressed()), this, SLOT(useDurationRadioPressed()));
60
61    m_pool = "";
62    m_recyclePool = "";
63    m_status = "";
64    m_slot = 0;
65
66    /* The media's pool */
67    poolCombo->addItems(m_console->pool_list);
68
69    /* The media's Status */
70    QStringList statusList = (QStringList() << "Full" << "Used" << "Append" 
71        << "Error" << "Purged" << "Recycle" << "Read-Only" << "Cleaning");
72    statusCombo->addItems(statusList);
73
74    /* Set up the query for the default values */
75    QStringList FieldList = (QStringList()
76       << "Media.VolumeName" << "Pool.Name" << "Media.VolStatus" << "Media.Slot"
77       << "Media.VolRetention" << "Media.VolUseDuration" << "Media.MaxVolJobs"
78       << "Media.MaxVolFiles" << "Media.MaxVolBytes" << "Media.Recycle" << "Media.Enabled"
79       << "Pol.Name");
80    QStringList AsList = (QStringList()
81       << "VolumeName" << "PoolName" << "Status" << "Slot"
82       << "Retention" << "UseDuration" << "MaxJobs"
83       << "MaxFiles" << "MaxBytes" << "Recycle" << "Enabled"
84       << "RecyclePool");
85    int i = 0;
86    QString query("SELECT ");
87    foreach (QString field, FieldList) {
88       if (i != 0) {
89          query += ", ";
90       }
91       query += field + " AS " + AsList[i];
92       i += 1;
93    }
94    query += " FROM Media"
95             " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
96             " LEFT OUTER JOIN Pool AS Pol ON (Media.recyclepoolid=Pol.PoolId)"
97             " WHERE Media.MediaId='" + mediaId + "'";
98
99    if (mainWin->m_sqlDebug) {
100       Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
101    }
102    QStringList results;
103    if (m_console->sql_cmd(query, results)) {
104       QString field;
105       QStringList fieldlist;
106
107       /* Iterate through the lines of results, there should only be one. */
108       foreach (QString resultline, results) {
109          fieldlist = resultline.split("\t");
110          i = 0;
111
112          /* Iterate through fields in the record */
113          foreach (field, fieldlist) {
114             field = field.trimmed();  /* strip leading & trailing spaces */
115             bool ok;
116             if (i == 0) {
117                m_mediaName = field;
118                volumeLabel->setText(QString("Volume : %1").arg(m_mediaName));
119             } else if (i == 1) {
120                m_pool = field;
121             } else if (i == 2) {
122                m_status = field;
123             } else if (i == 3) {
124                m_slot = field.toInt(&ok, 10);
125                if (!ok){ m_slot = 0; }
126             } else if (i == 4) {
127                m_retention = field.toInt(&ok, 10);
128                if (!ok){ m_retention = 0; }
129             } else if (i == 5) {
130                m_useDuration = field.toInt(&ok, 10);
131                if (!ok){ m_useDuration = 0; }
132             } else if (i == 6) {
133                m_maxVolJobs = field.toInt(&ok, 10);
134                if (!ok){ m_maxVolJobs = 0; }
135             } else if (i == 7) {
136                m_maxVolFiles = field.toInt(&ok, 10);
137                if (!ok){ m_maxVolFiles = 0; }
138             } else if (i == 8) {
139                m_maxVolBytes = field.toInt(&ok, 10);
140                if (!ok){ m_maxVolBytes = 0; }
141             } else if (i == 9) {
142                if (field == "1") m_recycle = true;
143                else m_recycle = false;
144             } else if (i == 10) {
145                if (field == "1") m_enabled = true;
146                else m_enabled = false;
147             } else if (i == 11) {
148                m_recyclePool = field;
149             }
150             i++;
151          } /* foreach field */
152       } /* foreach resultline */
153    } /* if results from query */
154
155    if (m_mediaName != "") {
156       int index;
157       /* default value for pool */
158       index = poolCombo->findText(m_pool, Qt::MatchExactly);
159       if (index != -1) {
160          poolCombo->setCurrentIndex(index);
161       }
162
163       /* default value for status */
164       index = statusCombo->findText(m_status, Qt::MatchExactly);
165       if (index != -1) {
166          statusCombo->setCurrentIndex(index);
167       }
168       slotSpin->setValue(m_slot);
169       retentionSpin->setValue(m_retention);
170       useDurationSpin->setValue(m_useDuration);
171       setSpins(retentionSpin->value());
172       retentionRadio->setChecked(true);
173       maxJobsSpin->setValue(m_maxVolJobs);
174       maxFilesSpin->setValue(m_maxVolFiles);
175       maxBytesSpin->setValue(m_maxVolBytes);
176       if (m_recycle) recycleCheck->setCheckState(Qt::Checked);
177       else recycleCheck->setCheckState(Qt::Unchecked);
178       if (m_enabled) enabledCheck->setCheckState(Qt::Checked);
179       else enabledCheck->setCheckState(Qt::Unchecked);
180       /* default for recycle pool */
181       recyclePoolCombo->addItems(m_console->pool_list);
182       recyclePoolCombo->insertItem(0, "*None*");
183       index = recyclePoolCombo->findText(m_recyclePool, Qt::MatchExactly);
184       if (index == -1) {
185          index = 0;
186       }
187       recyclePoolCombo->setCurrentIndex(index);
188    } else {
189       QMessageBox::warning(this, tr("No Volume name"), tr("No Volume name given"),
190                            QMessageBox::Ok, QMessageBox::Ok);
191       return;
192    }
193 }
194
195 /*
196  * Function to handle updating the record then closing the page
197  */
198 void MediaEdit::okButtonPushed()
199 {
200    QString scmd;
201    this->hide();
202    bool docmd = false;
203    scmd = QString("update volume=\"%1\"")
204                   .arg(m_mediaName);
205    if (m_pool != poolCombo->currentText()) {
206       scmd += " pool=\"" + poolCombo->currentText() + "\"";
207       docmd = true;
208    }
209    if (m_status != statusCombo->currentText()) {
210       scmd += " volstatus=\"" + statusCombo->currentText() + "\"";
211       docmd = true;
212    }
213    if (m_slot != slotSpin->value()) {
214       scmd += " slot=" + QString().setNum(slotSpin->value());
215       docmd = true;
216    }
217    if (m_retention != retentionSpin->value()) {
218       scmd += " VolRetention=" + QString().setNum(retentionSpin->value());
219       docmd = true;
220    }
221    if (m_useDuration != useDurationSpin->value()) {
222       scmd += " VolUse=" + QString().setNum(useDurationSpin->value());
223       docmd = true;
224    }
225    if (m_maxVolJobs != maxJobsSpin->value()) {
226       scmd += " MaxVolJobs=" + QString().setNum(maxJobsSpin->value());
227       docmd = true;
228    }
229    if (m_maxVolFiles != maxFilesSpin->value()) {
230       scmd += " MaxVolFiles=" + QString().setNum(maxFilesSpin->value());
231       docmd = true;
232    }
233    if (m_maxVolBytes != maxBytesSpin->value()) {
234       scmd += " MaxVolBytes=" + QString().setNum(maxBytesSpin->value());
235       docmd = true;
236    }
237    if ((m_recycle) && (recycleCheck->checkState() == Qt::Unchecked)) {
238       scmd += " Recycle=no";
239       docmd = true;
240    }
241    if ((!m_recycle) && (recycleCheck->checkState() == Qt::Checked)) {
242       scmd += " Recycle=yes";
243       docmd = true;
244    }
245    if ((m_enabled) && (enabledCheck->checkState() == Qt::Unchecked)) {
246       scmd += " enabled=no";
247       docmd = true;
248    }
249    if ((!m_enabled) && (enabledCheck->checkState() == Qt::Checked)) {
250       scmd += " enabled=yes";
251       docmd = true;
252    }
253    if (m_recyclePool != recyclePoolCombo->currentText()) {
254       scmd += " recyclepool=\"";
255       if (recyclePoolCombo->currentText() != "*None*") {
256          scmd += recyclePoolCombo->currentText();
257       }
258       scmd += "\"";
259       docmd = true;
260    }
261    if (docmd) {
262       if (mainWin->m_commandDebug) {
263          Pmsg1(000, "sending command : %s\n",scmd.toUtf8().data());
264       }
265       consoleCommand(scmd);
266    }
267    closeStackPage();
268 }
269
270 /* close if cancel */
271 void MediaEdit::cancelButtonPushed()
272 {
273    closeStackPage();
274 }
275
276 /*
277  * Slot for user changed retention
278  */
279 void MediaEdit::retentionChanged()
280 {
281    retentionRadio->setChecked(true);
282    setSpins(retentionSpin->value());
283 }
284
285 /*
286  * Slot for user changed the use duration
287  */
288 void MediaEdit::useDurationChanged()
289 {
290    useDurationRadio->setChecked(true);
291    setSpins(useDurationSpin->value());
292 }
293
294 /*
295  * Set the 5 duration spins from a known duration value
296  */
297 void MediaEdit::setSpins(int value)
298 {
299    int years, months, days, hours, minutes, seconds, left;
300         
301    years = abs(value / 31536000);
302    left = value - years * 31536000;
303    months = abs(left / 2592000);
304    left = left - months * 2592000;
305    days = abs(left / 86400);
306    left = left - days * 86400;
307    hours = abs(left / 3600);
308    left = left - hours * 3600;
309    minutes = abs(left / 60);
310    seconds = left - minutes * 60;
311    disconnectSpins();
312    yearsSpin->setValue(years);
313    monthsSpin->setValue(months);
314    daysSpin->setValue(days);
315    hoursSpin->setValue(hours);
316    minutesSpin->setValue(minutes);
317    secondsSpin->setValue(seconds);
318    connectSpins();
319 }
320
321 /*
322  * This slot is called any time any one of the 5 duration spins a changed.
323  */
324 void MediaEdit::durationChanged()
325 {
326    disconnectSpins();
327    if (secondsSpin->value() == -1) {
328       secondsSpin->setValue(59);
329       minutesSpin->setValue(minutesSpin->value()-1);
330    }
331    if (minutesSpin->value() == -1) {
332       minutesSpin->setValue(59);
333       hoursSpin->setValue(hoursSpin->value()-1);
334    }
335    if (hoursSpin->value() == -1) {
336       hoursSpin->setValue(23);
337       daysSpin->setValue(daysSpin->value()-1);
338    }
339    if (daysSpin->value() == -1) {
340       daysSpin->setValue(29);
341       monthsSpin->setValue(monthsSpin->value()-1);
342    }
343    if (monthsSpin->value() == -1) {
344       monthsSpin->setValue(11);
345       yearsSpin->setValue(yearsSpin->value()-1);
346    }
347    if (yearsSpin->value() == -1) {
348       yearsSpin->setValue(0);
349    }
350
351    if (secondsSpin->value() == 60) {
352       secondsSpin->setValue(0);
353       minutesSpin->setValue(minutesSpin->value()+1);
354    }
355    if (minutesSpin->value() == 60) {
356       minutesSpin->setValue(0);
357       hoursSpin->setValue(hoursSpin->value()+1);
358    }
359    if (hoursSpin->value() == 24) {
360       hoursSpin->setValue(0);
361       daysSpin->setValue(daysSpin->value()+1);
362    }
363    if (daysSpin->value() == 30) {
364       daysSpin->setValue(0);
365       monthsSpin->setValue(monthsSpin->value()+1);
366    }
367    if (monthsSpin->value() == 12) {
368       monthsSpin->setValue(0);
369       yearsSpin->setValue(yearsSpin->value()+1);
370    }
371    connectSpins();
372    if (retentionRadio->isChecked()) {
373       int retention;
374       retention = secondsSpin->value() + minutesSpin->value() * 60 + 
375          hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
376          monthsSpin->value() * 2592000 +
377          yearsSpin->value() * 31536000;
378       disconnect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
379       retentionSpin->setValue(retention);
380       connect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
381    }
382    if (useDurationRadio->isChecked()) {
383       int useDuration;
384       useDuration = secondsSpin->value() + minutesSpin->value() * 60 + 
385          hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
386          monthsSpin->value() * 2592000 +
387          yearsSpin->value() * 31536000;
388       disconnect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
389       useDurationSpin->setValue(useDuration);
390       connect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
391    }
392 }
393
394 /* Connect the spins */
395 void MediaEdit::connectSpins()
396 {
397    connect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
398    connect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
399    connect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
400    connect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
401    connect(monthsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
402    connect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
403 }
404
405 /* disconnect spins so that we can set the value of other spin from changed duration spin */
406 void MediaEdit::disconnectSpins()
407 {
408    disconnect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
409    disconnect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
410    disconnect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
411    disconnect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
412    disconnect(monthsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
413    disconnect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
414 }
415
416 /* slot for setting spins when retention radio checked */
417 void MediaEdit::retentionRadioPressed()
418 {
419    setSpins(retentionSpin->value());
420 }
421
422 /* slot for setting spins when duration radio checked */
423 void MediaEdit::useDurationRadioPressed()
424 {
425    setSpins(useDurationSpin->value());
426 }