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