2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2008 Free Software Foundation Europe e.V.
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
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.
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
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.
31 * Dirk Bartley, March 2007
34 #include <QAbstractEventDispatcher>
35 #include <QTableWidgetItem>
36 #include <QMessageBox>
38 #include "mediaedit.h"
44 MediaEdit::MediaEdit(QTreeWidgetItem *parentWidget, QString &mediaId)
47 m_name = tr("Media Edit");
48 pgInitialize(parentWidget);
49 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
50 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge-edit.png")));
55 connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
56 connect(cancelButton, SIGNAL(pressed()), this, SLOT(cancelButtonPushed()));
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()));
68 if (!m_console->preventInUseConnect())
71 /* The media's pool */
72 poolCombo->addItems(m_console->pool_list);
74 /* The media's Status */
75 QStringList statusList = (QStringList() << "Full" << "Used" << "Append"
76 << "Error" << "Purged" << "Recycle" << "Read-Only" << "Cleaning");
77 statusCombo->addItems(statusList);
79 /* Set up the query for the default values */
80 QStringList FieldList = (QStringList()
81 << "Media.VolumeName" << "Pool.Name" << "Media.VolStatus" << "Media.Slot"
82 << "Media.VolRetention" << "Media.VolUseDuration" << "Media.MaxVolJobs"
83 << "Media.MaxVolFiles" << "Media.MaxVolBytes" << "Media.Recycle" << "Media.Enabled"
85 QStringList AsList = (QStringList()
86 << "VolumeName" << "PoolName" << "Status" << "Slot"
87 << "Retention" << "UseDuration" << "MaxJobs"
88 << "MaxFiles" << "MaxBytes" << "Recycle" << "Enabled"
91 QString query("SELECT ");
92 foreach (QString field, FieldList) {
96 query += field + " AS " + AsList[i];
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 Media.MediaId='" + mediaId + "'";
104 if (mainWin->m_sqlDebug) {
105 Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
108 if (m_console->sql_cmd(query, results)) {
110 QStringList fieldlist;
112 /* Iterate through the lines of results, there should only be one. */
113 foreach (QString resultline, results) {
114 fieldlist = resultline.split("\t");
117 /* Iterate through fields in the record */
118 foreach (field, fieldlist) {
119 field = field.trimmed(); /* strip leading & trailing spaces */
123 volumeLabel->setText(QString("Volume : %1").arg(m_mediaName));
129 m_slot = field.toInt(&ok, 10);
130 if (!ok){ m_slot = 0; }
132 m_retention = field.toInt(&ok, 10);
133 if (!ok){ m_retention = 0; }
135 m_useDuration = field.toInt(&ok, 10);
136 if (!ok){ m_useDuration = 0; }
138 m_maxVolJobs = field.toInt(&ok, 10);
139 if (!ok){ m_maxVolJobs = 0; }
141 m_maxVolFiles = field.toInt(&ok, 10);
142 if (!ok){ m_maxVolFiles = 0; }
144 m_maxVolBytes = field.toInt(&ok, 10);
145 if (!ok){ m_maxVolBytes = 0; }
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;
156 } /* foreach field */
157 } /* foreach resultline */
158 } /* if results from query */
160 if (m_mediaName != "") {
162 /* default value for pool */
163 index = poolCombo->findText(m_pool, Qt::MatchExactly);
165 poolCombo->setCurrentIndex(index);
168 /* default value for status */
169 index = statusCombo->findText(m_status, Qt::MatchExactly);
171 statusCombo->setCurrentIndex(index);
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 index = recyclePoolCombo->findText(m_recyclePool, Qt::MatchExactly);
189 recyclePoolCombo->insertItem(0, "");
192 recyclePoolCombo->setCurrentIndex(index);
194 QMessageBox::warning(this, tr("No Volume name"), tr("No Volume name given"),
195 QMessageBox::Ok, QMessageBox::Ok);
201 * Function to handle updating the record then closing the page
203 void MediaEdit::okButtonPushed()
208 scmd = QString("update volume=\"%1\"")
210 if (m_pool != poolCombo->currentText()) {
211 scmd += " pool=\"" + poolCombo->currentText() + "\"";
214 if (m_status != statusCombo->currentText()) {
215 scmd += " volstatus=\"" + statusCombo->currentText() + "\"";
218 if (m_slot != slotSpin->value()) {
219 scmd += " slot=" + QString().setNum(slotSpin->value());
222 if (m_retention != retentionSpin->value()) {
223 scmd += " VolRetention=" + QString().setNum(retentionSpin->value());
226 if (m_useDuration != useDurationSpin->value()) {
227 scmd += " VolUse=" + QString().setNum(useDurationSpin->value());
230 if (m_maxVolJobs != maxJobsSpin->value()) {
231 scmd += " MaxVolJobs=" + QString().setNum(maxJobsSpin->value());
234 if (m_maxVolFiles != maxFilesSpin->value()) {
235 scmd += " MaxVolFiles=" + QString().setNum(maxFilesSpin->value());
238 if (m_maxVolBytes != maxBytesSpin->value()) {
239 scmd += " MaxVolBytes=" + QString().setNum(maxBytesSpin->value());
242 if ((m_recycle) && (recycleCheck->checkState() == Qt::Unchecked)) {
243 scmd += " Recycle=no";
246 if ((!m_recycle) && (recycleCheck->checkState() == Qt::Checked)) {
247 scmd += " Recycle=yes";
250 if ((m_enabled) && (enabledCheck->checkState() == Qt::Unchecked)) {
251 scmd += " enabled=no";
254 if ((!m_enabled) && (enabledCheck->checkState() == Qt::Checked)) {
255 scmd += " enabled=yes";
258 if (m_recyclePool != recyclePoolCombo->currentText() && recyclePoolCombo->currentText() != "") {
259 scmd += " recyclepool=\"" + recyclePoolCombo->currentText() + "\"";
263 if (mainWin->m_commandDebug) {
264 Pmsg1(000, "sending command : %s\n",scmd.toUtf8().data());
266 consoleCommand(scmd);
271 /* close if cancel */
272 void MediaEdit::cancelButtonPushed()
278 * Slot for user changed retention
280 void MediaEdit::retentionChanged()
282 retentionRadio->setChecked(true);
283 setSpins(retentionSpin->value());
287 * Slot for user changed the use duration
289 void MediaEdit::useDurationChanged()
291 useDurationRadio->setChecked(true);
292 setSpins(useDurationSpin->value());
296 * Set the 5 duration spins from a known duration value
298 void MediaEdit::setSpins(int value)
300 int years, months, days, hours, minutes, seconds, left;
302 years = abs(value / 31536000);
303 left = value - years * 31536000;
304 months = abs(left / 2592000);
305 left = left - months * 2592000;
306 days = abs(left / 86400);
307 left = left - days * 86400;
308 hours = abs(left / 3600);
309 left = left - hours * 3600;
310 minutes = abs(left / 60);
311 seconds = left - minutes * 60;
313 yearsSpin->setValue(years);
314 monthsSpin->setValue(months);
315 daysSpin->setValue(days);
316 hoursSpin->setValue(hours);
317 minutesSpin->setValue(minutes);
318 secondsSpin->setValue(seconds);
323 * This slot is called any time any one of the 5 duration spins a changed.
325 void MediaEdit::durationChanged()
328 if (secondsSpin->value() == -1) {
329 secondsSpin->setValue(59);
330 minutesSpin->setValue(minutesSpin->value()-1);
332 if (minutesSpin->value() == -1) {
333 minutesSpin->setValue(59);
334 hoursSpin->setValue(hoursSpin->value()-1);
336 if (hoursSpin->value() == -1) {
337 hoursSpin->setValue(23);
338 daysSpin->setValue(daysSpin->value()-1);
340 if (daysSpin->value() == -1) {
341 daysSpin->setValue(29);
342 monthsSpin->setValue(monthsSpin->value()-1);
344 if (monthsSpin->value() == -1) {
345 monthsSpin->setValue(11);
346 yearsSpin->setValue(yearsSpin->value()-1);
348 if (yearsSpin->value() == -1) {
349 yearsSpin->setValue(0);
352 if (secondsSpin->value() == 60) {
353 secondsSpin->setValue(0);
354 minutesSpin->setValue(minutesSpin->value()+1);
356 if (minutesSpin->value() == 60) {
357 minutesSpin->setValue(0);
358 hoursSpin->setValue(hoursSpin->value()+1);
360 if (hoursSpin->value() == 24) {
361 hoursSpin->setValue(0);
362 daysSpin->setValue(daysSpin->value()+1);
364 if (daysSpin->value() == 30) {
365 daysSpin->setValue(0);
366 monthsSpin->setValue(monthsSpin->value()+1);
368 if (monthsSpin->value() == 12) {
369 monthsSpin->setValue(0);
370 yearsSpin->setValue(yearsSpin->value()+1);
373 if (retentionRadio->isChecked()) {
375 retention = secondsSpin->value() + minutesSpin->value() * 60 +
376 hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
377 monthsSpin->value() * 2592000 +
378 yearsSpin->value() * 31536000;
379 disconnect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
380 retentionSpin->setValue(retention);
381 connect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
383 if (useDurationRadio->isChecked()) {
385 useDuration = secondsSpin->value() + minutesSpin->value() * 60 +
386 hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
387 monthsSpin->value() * 2592000 +
388 yearsSpin->value() * 31536000;
389 disconnect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
390 useDurationSpin->setValue(useDuration);
391 connect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
395 /* Connect the spins */
396 void MediaEdit::connectSpins()
398 connect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
399 connect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
400 connect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
401 connect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
402 connect(monthsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
403 connect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
406 /* disconnect spins so that we can set the value of other spin from changed duration spin */
407 void MediaEdit::disconnectSpins()
409 disconnect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
410 disconnect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
411 disconnect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
412 disconnect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
413 disconnect(monthsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
414 disconnect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
417 /* slot for setting spins when retention radio checked */
418 void MediaEdit::retentionRadioPressed()
420 setSpins(retentionSpin->value());
423 /* slot for setting spins when duration radio checked */
424 void MediaEdit::useDurationRadioPressed()
426 setSpins(useDurationSpin->value());