2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 Bacula® is a registered trademark of Kern Sibbald.
19 * Dirk Bartley, March 2007
23 #include <QAbstractEventDispatcher>
24 #include <QTableWidgetItem>
25 #include <QMessageBox>
26 #include "mediaedit.h"
31 MediaEdit::MediaEdit(QTreeWidgetItem *parentWidget, QString &mediaId)
35 pgInitialize(tr("Media Edit"), parentWidget);
36 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
37 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge-edit.png")));
41 connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
42 connect(cancelButton, SIGNAL(pressed()), this, SLOT(cancelButtonPushed()));
44 connect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
45 connect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
46 connect(retentionRadio, SIGNAL(pressed()), this, SLOT(retentionRadioPressed()));
47 connect(useDurationRadio, SIGNAL(pressed()), this, SLOT(useDurationRadioPressed()));
54 /* The media's pool */
55 poolCombo->addItems(m_console->pool_list);
57 /* The media's Status */
58 QStringList statusList = (QStringList() << "Full" << "Used" << "Append"
59 << "Error" << "Purged" << "Recycle" << "Read-Only" << "Cleaning");
60 statusCombo->addItems(statusList);
62 /* Set up the query for the default values */
63 QStringList FieldList = (QStringList()
64 << "Media.VolumeName" << "Pool.Name" << "Media.VolStatus" << "Media.Slot"
65 << "Media.VolRetention" << "Media.VolUseDuration" << "Media.MaxVolJobs"
66 << "Media.MaxVolFiles" << "Media.MaxVolBytes" << "Media.Recycle" << "Media.Enabled"
68 QStringList AsList = (QStringList()
69 << "VolumeName" << "PoolName" << "Status" << "Slot"
70 << "Retention" << "UseDuration" << "MaxJobs"
71 << "MaxFiles" << "MaxBytes" << "Recycle" << "Enabled"
74 QString query("SELECT ");
75 foreach (QString field, FieldList) {
79 query += field + " AS " + AsList[i];
83 QString where = " WHERE Media.VolumeName = '" + mediaId + "' ";
84 if (mediaId.contains(QRegExp("^[0-9]+$"))) {
85 where = " WHERE Media.MediaId=" + mediaId;
87 query += " FROM Media"
88 " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
89 " LEFT OUTER JOIN Pool AS Pol ON (Media.RecyclePoolId=Pol.PoolId)"
92 if (mainWin->m_sqlDebug) {
93 Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
96 if (m_console->sql_cmd(query, results)) {
98 QStringList fieldlist;
100 /* Iterate through the lines of results, there should only be one. */
101 foreach (QString resultline, results) {
102 fieldlist = resultline.split("\t");
105 /* Iterate through fields in the record */
106 foreach (field, fieldlist) {
107 field = field.trimmed(); /* strip leading & trailing spaces */
111 volumeLabel->setText(QString("Volume : %1").arg(m_mediaName));
117 m_slot = field.toInt(&ok, 10);
118 if (!ok){ m_slot = 0; }
120 m_retention = field.toInt(&ok, 10);
121 if (!ok){ m_retention = 0; }
123 m_useDuration = field.toInt(&ok, 10);
124 if (!ok){ m_useDuration = 0; }
126 m_maxVolJobs = field.toInt(&ok, 10);
127 if (!ok){ m_maxVolJobs = 0; }
129 m_maxVolFiles = field.toInt(&ok, 10);
130 if (!ok){ m_maxVolFiles = 0; }
132 m_maxVolBytes = field.toInt(&ok, 10);
133 if (!ok){ m_maxVolBytes = 0; }
135 if (field == "1") m_recycle = true;
136 else m_recycle = false;
137 } else if (i == 10) {
138 if (field == "1") m_enabled = true;
139 else m_enabled = false;
140 } else if (i == 11) {
141 m_recyclePool = field;
144 } /* foreach field */
145 } /* foreach resultline */
146 } /* if results from query */
148 if (m_mediaName != "") {
150 /* default value for pool */
151 index = poolCombo->findText(m_pool, Qt::MatchExactly);
153 poolCombo->setCurrentIndex(index);
156 /* default value for status */
157 index = statusCombo->findText(m_status, Qt::MatchExactly);
159 statusCombo->setCurrentIndex(index);
161 slotSpin->setValue(m_slot);
162 retentionSpin->setValue(m_retention);
163 useDurationSpin->setValue(m_useDuration);
164 setSpins(retentionSpin->value());
165 retentionRadio->setChecked(true);
166 maxJobsSpin->setValue(m_maxVolJobs);
167 maxFilesSpin->setValue(m_maxVolFiles);
168 maxBytesSpin->setValue(m_maxVolBytes);
169 if (m_recycle) recycleCheck->setCheckState(Qt::Checked);
170 else recycleCheck->setCheckState(Qt::Unchecked);
171 if (m_enabled) enabledCheck->setCheckState(Qt::Checked);
172 else enabledCheck->setCheckState(Qt::Unchecked);
173 /* default for recycle pool */
174 recyclePoolCombo->addItems(m_console->pool_list);
175 recyclePoolCombo->insertItem(0, "*None*");
176 index = recyclePoolCombo->findText(m_recyclePool, Qt::MatchExactly);
180 recyclePoolCombo->setCurrentIndex(index);
182 QMessageBox::warning(this, tr("No Volume name"), tr("No Volume name given"),
183 QMessageBox::Ok, QMessageBox::Ok);
189 * Function to handle updating the record then closing the page
191 void MediaEdit::okButtonPushed()
196 scmd = QString("update volume=\"%1\"")
198 if (m_pool != poolCombo->currentText()) {
199 scmd += " pool=\"" + poolCombo->currentText() + "\"";
202 if (m_status != statusCombo->currentText()) {
203 scmd += " volstatus=\"" + statusCombo->currentText() + "\"";
206 if (m_slot != slotSpin->value()) {
207 scmd += " slot=" + QString().setNum(slotSpin->value());
210 if (m_retention != retentionSpin->value()) {
211 scmd += " VolRetention=" + QString().setNum(retentionSpin->value());
214 if (m_useDuration != useDurationSpin->value()) {
215 scmd += " VolUse=" + QString().setNum(useDurationSpin->value());
218 if (m_maxVolJobs != maxJobsSpin->value()) {
219 scmd += " MaxVolJobs=" + QString().setNum(maxJobsSpin->value());
222 if (m_maxVolFiles != maxFilesSpin->value()) {
223 scmd += " MaxVolFiles=" + QString().setNum(maxFilesSpin->value());
226 if (m_maxVolBytes != maxBytesSpin->value()) {
227 scmd += " MaxVolBytes=" + QString().setNum(maxBytesSpin->value());
230 if ((m_recycle) && (recycleCheck->checkState() == Qt::Unchecked)) {
231 scmd += " Recycle=no";
234 if ((!m_recycle) && (recycleCheck->checkState() == Qt::Checked)) {
235 scmd += " Recycle=yes";
238 if ((m_enabled) && (enabledCheck->checkState() == Qt::Unchecked)) {
239 scmd += " enabled=no";
242 if ((!m_enabled) && (enabledCheck->checkState() == Qt::Checked)) {
243 scmd += " enabled=yes";
246 if (m_recyclePool != recyclePoolCombo->currentText()) {
247 scmd += " recyclepool=\"";
248 if (recyclePoolCombo->currentText() != "*None*") {
249 scmd += recyclePoolCombo->currentText();
255 if (mainWin->m_commandDebug) {
256 Pmsg1(000, "sending command : %s\n",scmd.toUtf8().data());
258 consoleCommand(scmd);
263 /* close if cancel */
264 void MediaEdit::cancelButtonPushed()
270 * Slot for user changed retention
272 void MediaEdit::retentionChanged()
274 retentionRadio->setChecked(true);
275 setSpins(retentionSpin->value());
279 * Slot for user changed the use duration
281 void MediaEdit::useDurationChanged()
283 useDurationRadio->setChecked(true);
284 setSpins(useDurationSpin->value());
288 * Set the 5 duration spins from a known duration value
290 void MediaEdit::setSpins(int value)
292 int years, months, days, hours, minutes, seconds, left;
294 years = abs(value / 31536000);
295 left = value - years * 31536000;
296 months = abs(left / 2592000);
297 left = left - months * 2592000;
298 days = abs(left / 86400);
299 left = left - days * 86400;
300 hours = abs(left / 3600);
301 left = left - hours * 3600;
302 minutes = abs(left / 60);
303 seconds = left - minutes * 60;
305 yearsSpin->setValue(years);
306 monthsSpin->setValue(months);
307 daysSpin->setValue(days);
308 hoursSpin->setValue(hours);
309 minutesSpin->setValue(minutes);
310 secondsSpin->setValue(seconds);
315 * This slot is called any time any one of the 5 duration spins a changed.
317 void MediaEdit::durationChanged()
320 if (secondsSpin->value() == -1) {
321 secondsSpin->setValue(59);
322 minutesSpin->setValue(minutesSpin->value()-1);
324 if (minutesSpin->value() == -1) {
325 minutesSpin->setValue(59);
326 hoursSpin->setValue(hoursSpin->value()-1);
328 if (hoursSpin->value() == -1) {
329 hoursSpin->setValue(23);
330 daysSpin->setValue(daysSpin->value()-1);
332 if (daysSpin->value() == -1) {
333 daysSpin->setValue(29);
334 monthsSpin->setValue(monthsSpin->value()-1);
336 if (monthsSpin->value() == -1) {
337 monthsSpin->setValue(11);
338 yearsSpin->setValue(yearsSpin->value()-1);
340 if (yearsSpin->value() == -1) {
341 yearsSpin->setValue(0);
344 if (secondsSpin->value() == 60) {
345 secondsSpin->setValue(0);
346 minutesSpin->setValue(minutesSpin->value()+1);
348 if (minutesSpin->value() == 60) {
349 minutesSpin->setValue(0);
350 hoursSpin->setValue(hoursSpin->value()+1);
352 if (hoursSpin->value() == 24) {
353 hoursSpin->setValue(0);
354 daysSpin->setValue(daysSpin->value()+1);
356 if (daysSpin->value() == 30) {
357 daysSpin->setValue(0);
358 monthsSpin->setValue(monthsSpin->value()+1);
360 if (monthsSpin->value() == 12) {
361 monthsSpin->setValue(0);
362 yearsSpin->setValue(yearsSpin->value()+1);
365 if (retentionRadio->isChecked()) {
367 retention = secondsSpin->value() + minutesSpin->value() * 60 +
368 hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
369 monthsSpin->value() * 2592000 +
370 yearsSpin->value() * 31536000;
371 disconnect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
372 retentionSpin->setValue(retention);
373 connect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
375 if (useDurationRadio->isChecked()) {
377 useDuration = secondsSpin->value() + minutesSpin->value() * 60 +
378 hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
379 monthsSpin->value() * 2592000 +
380 yearsSpin->value() * 31536000;
381 disconnect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
382 useDurationSpin->setValue(useDuration);
383 connect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
387 /* Connect the spins */
388 void MediaEdit::connectSpins()
390 connect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
391 connect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
392 connect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
393 connect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
394 connect(monthsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
395 connect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
398 /* disconnect spins so that we can set the value of other spin from changed duration spin */
399 void MediaEdit::disconnectSpins()
401 disconnect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
402 disconnect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
403 disconnect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
404 disconnect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
405 disconnect(monthsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
406 disconnect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
409 /* slot for setting spins when retention radio checked */
410 void MediaEdit::retentionRadioPressed()
412 setSpins(retentionSpin->value());
415 /* slot for setting spins when duration radio checked */
416 void MediaEdit::useDurationRadioPressed()
418 setSpins(useDurationSpin->value());