2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2007 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 = "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" << "Error" << "Purged" << "Recycle" << "Read-Only" << "Cleaning");
76 statusCombo->addItems(statusList);
78 /* Set up the query for the default values */
79 QStringList FieldList = (QStringList()
80 << "Media.VolumeName" << "Pool.Name" << "Media.VolStatus" << "Media.Slot"
81 << "Media.VolRetention" << "Media.VolUseDuration" << "Media.MaxVolJobs"
82 << "Media.MaxVolFiles" << "Media.MaxVolBytes" << "Media.Recycle" << "Media.Enabled"
84 QStringList AsList = (QStringList()
85 << "VolumeName" << "PoolName" << "Status" << "Slot"
86 << "Retention" << "UseDuration" << "MaxJobs"
87 << "MaxFiles" << "MaxBytes" << "Recycle" << "Enabled"
90 QString query("SELECT ");
91 foreach (QString field, FieldList) {
95 query += field + " AS " + AsList[i];
98 query += " FROM Media"
99 " JOIN Pool ON (Media.PoolId=Pool.PoolId)"
100 " LEFT OUTER JOIN Pool AS Pol ON (Media.recyclepoolid=Pol.PoolId)"
101 " WHERE Media.MediaId='" + mediaId + "'";
103 if (mainWin->m_sqlDebug) {
104 Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
107 if (m_console->sql_cmd(query, results)) {
109 QStringList fieldlist;
111 /* Iterate through the lines of results, there should only be one. */
112 foreach (QString resultline, results) {
113 fieldlist = resultline.split("\t");
116 /* Iterate through fields in the record */
117 foreach (field, fieldlist) {
118 field = field.trimmed(); /* strip leading & trailing spaces */
122 volumeLabel->setText(QString("Volume : %1").arg(m_mediaName));
128 m_slot = field.toInt(&ok, 10);
129 if (!ok){ m_slot = 0; }
131 m_retention = field.toInt(&ok, 10);
132 if (!ok){ m_retention = 0; }
134 m_useDuration = field.toInt(&ok, 10);
135 if (!ok){ m_useDuration = 0; }
137 m_maxVolJobs = field.toInt(&ok, 10);
138 if (!ok){ m_maxVolJobs = 0; }
140 m_maxVolFiles = field.toInt(&ok, 10);
141 if (!ok){ m_maxVolFiles = 0; }
143 m_maxVolBytes = field.toInt(&ok, 10);
144 if (!ok){ m_maxVolBytes = 0; }
146 if (field == "1") m_recycle = true;
147 else m_recycle = false;
148 } else if (i == 10) {
149 if (field == "1") m_enabled = true;
150 else m_enabled = false;
151 } else if (i == 11) {
152 m_recyclePool = field;
155 } /* foreach field */
156 } /* foreach resultline */
157 } /* if results from query */
159 if (m_mediaName != "") {
161 /* default value for pool */
162 index = poolCombo->findText(m_pool, Qt::MatchExactly);
164 poolCombo->setCurrentIndex(index);
167 /* default value for status */
168 index = statusCombo->findText(m_status, Qt::MatchExactly);
170 statusCombo->setCurrentIndex(index);
172 slotSpin->setValue(m_slot);
173 retentionSpin->setValue(m_retention);
174 useDurationSpin->setValue(m_useDuration);
175 setSpins(retentionSpin->value());
176 retentionRadio->setChecked(true);
177 maxJobsSpin->setValue(m_maxVolJobs);
178 maxFilesSpin->setValue(m_maxVolFiles);
179 maxBytesSpin->setValue(m_maxVolBytes);
180 if (m_recycle) recycleCheck->setCheckState(Qt::Checked);
181 else recycleCheck->setCheckState(Qt::Unchecked);
182 if (m_enabled) enabledCheck->setCheckState(Qt::Checked);
183 else enabledCheck->setCheckState(Qt::Unchecked);
184 /* default for recycle pool */
185 recyclePoolCombo->addItems(m_console->pool_list);
186 index = recyclePoolCombo->findText(m_recyclePool, Qt::MatchExactly);
188 recyclePoolCombo->insertItem(0, "");
189 index = recyclePoolCombo->findText(m_recyclePool, Qt::MatchExactly);
192 recyclePoolCombo->setCurrentIndex(index);
195 QMessageBox::warning(this, "No Volume name", "No Volume name given",
196 QMessageBox::Ok, QMessageBox::Ok);
202 * Function to handle updating the record then closing the page
204 void MediaEdit::okButtonPushed()
209 scmd = QString("update volume=\"%1\"")
211 if (m_pool != poolCombo->currentText()) {
212 scmd += " pool=\"" + poolCombo->currentText() + "\"";
215 if (m_status != statusCombo->currentText()) {
216 scmd += " volstatus=\"" + statusCombo->currentText() + "\"";
219 if (m_slot != slotSpin->value()) {
220 scmd += " slot=" + QString().setNum(slotSpin->value());
223 if (m_retention != retentionSpin->value()) {
224 scmd += " VolRetention=" + QString().setNum(retentionSpin->value());
227 if (m_useDuration != useDurationSpin->value()) {
228 scmd += " VolUse=" + QString().setNum(useDurationSpin->value());
231 if (m_maxVolJobs != maxJobsSpin->value()) {
232 scmd += " MaxVolJobs=" + QString().setNum(maxJobsSpin->value());
235 if (m_maxVolFiles != maxFilesSpin->value()) {
236 scmd += " MaxVolFiles=" + QString().setNum(maxFilesSpin->value());
239 if (m_maxVolBytes != maxBytesSpin->value()) {
240 scmd += " MaxVolBytes=" + QString().setNum(maxBytesSpin->value());
243 if ((m_recycle) && (recycleCheck->checkState() == Qt::Unchecked)) {
244 scmd += " Recycle=no";
247 if ((!m_recycle) && (recycleCheck->checkState() == Qt::Checked)) {
248 scmd += " Recycle=yes";
251 if ((m_enabled) && (enabledCheck->checkState() == Qt::Unchecked)) {
252 scmd += " enabled=no";
255 if ((!m_enabled) && (enabledCheck->checkState() == Qt::Checked)) {
256 scmd += " enabled=yes";
259 if (m_recyclePool != recyclePoolCombo->currentText()) {
260 scmd += " recyclepool=\"" + recyclePoolCombo->currentText() + "\"";
264 if (mainWin->m_commandDebug) {
265 Pmsg1(000, "sending command : %s\n",scmd.toUtf8().data());
267 consoleCommand(scmd);
272 /* close if cancel */
273 void MediaEdit::cancelButtonPushed()
279 * Slot for user changed retention
281 void MediaEdit::retentionChanged()
283 retentionRadio->setChecked(true);
284 setSpins(retentionSpin->value());
288 * Slot for user changed the use duration
290 void MediaEdit::useDurationChanged()
292 useDurationRadio->setChecked(true);
293 setSpins(useDurationSpin->value());
297 * Set the 5 duration spins from a known duration value
299 void MediaEdit::setSpins(int value)
301 int years, months, days, hours, minutes, seconds, left;
303 years = abs(value / 31536000);
304 left = value - years * 31536000;
305 months = abs(left / 2592000);
306 left = left - months * 2592000;
307 days = abs(left / 86400);
308 left = left - days * 86400;
309 hours = abs(left / 3600);
310 left = left - hours * 3600;
311 minutes = abs(left / 60);
312 seconds = left - minutes * 60;
314 yearsSpin->setValue(years);
315 monthsSpin->setValue(months);
316 daysSpin->setValue(days);
317 hoursSpin->setValue(hours);
318 minutesSpin->setValue(minutes);
319 secondsSpin->setValue(seconds);
324 * This slot is called any time any one of the 5 duration spins a changed.
326 void MediaEdit::durationChanged()
329 if (secondsSpin->value() == -1) {
330 secondsSpin->setValue(59);
331 minutesSpin->setValue(minutesSpin->value()-1);
333 if (minutesSpin->value() == -1) {
334 minutesSpin->setValue(59);
335 hoursSpin->setValue(hoursSpin->value()-1);
337 if (hoursSpin->value() == -1) {
338 hoursSpin->setValue(23);
339 daysSpin->setValue(daysSpin->value()-1);
341 if (daysSpin->value() == -1) {
342 daysSpin->setValue(29);
343 monthsSpin->setValue(monthsSpin->value()-1);
345 if (monthsSpin->value() == -1) {
346 monthsSpin->setValue(11);
347 yearsSpin->setValue(yearsSpin->value()-1);
349 if (yearsSpin->value() == -1) {
350 yearsSpin->setValue(0);
353 if (secondsSpin->value() == 60) {
354 secondsSpin->setValue(0);
355 minutesSpin->setValue(minutesSpin->value()+1);
357 if (minutesSpin->value() == 60) {
358 minutesSpin->setValue(0);
359 hoursSpin->setValue(hoursSpin->value()+1);
361 if (hoursSpin->value() == 24) {
362 hoursSpin->setValue(0);
363 daysSpin->setValue(daysSpin->value()+1);
365 if (daysSpin->value() == 30) {
366 daysSpin->setValue(0);
367 monthsSpin->setValue(monthsSpin->value()+1);
369 if (monthsSpin->value() == 12) {
370 monthsSpin->setValue(0);
371 yearsSpin->setValue(yearsSpin->value()+1);
374 if (retentionRadio->isChecked()) {
376 retention = secondsSpin->value() + minutesSpin->value() * 60 +
377 hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
378 monthsSpin->value() * 2592000 +
379 yearsSpin->value() * 31536000;
380 disconnect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
381 retentionSpin->setValue(retention);
382 connect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
384 if (useDurationRadio->isChecked()) {
386 useDuration = secondsSpin->value() + minutesSpin->value() * 60 +
387 hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
388 monthsSpin->value() * 2592000 +
389 yearsSpin->value() * 31536000;
390 disconnect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
391 useDurationSpin->setValue(useDuration);
392 connect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
396 /* Connect the spins */
397 void MediaEdit::connectSpins()
399 connect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
400 connect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
401 connect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
402 connect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
403 connect(monthsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
404 connect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
407 /* disconnect spins so that we can set the value of other spin from changed duration spin */
408 void MediaEdit::disconnectSpins()
410 disconnect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
411 disconnect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
412 disconnect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
413 disconnect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
414 disconnect(monthsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
415 disconnect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
418 /* slot for setting spins when retention radio checked */
419 void MediaEdit::retentionRadioPressed()
421 setSpins(retentionSpin->value());
424 /* slot for setting spins when duration radio checked */
425 void MediaEdit::useDurationRadioPressed()
427 setSpins(useDurationSpin->value());