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