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