]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/mediaedit/mediaedit.cpp
Add the remainder of the editible fields to mediaedit. Also add those
[bacula/bacula] / bacula / src / qt-console / mediaedit / mediaedit.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 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 plus additions
11    that are listed 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 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.
27 */
28 /*
29  *   Version $Id: batstack.h 4230 2007-02-21 20:07:37Z kerns $
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    m_name = "Media Edit";
48    pgInitialize(parentWidget);
49    QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
50    thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/cartridge-edit.svg")));
51    m_closeable = true;
52    dockPage();
53    setCurrent();
54
55    connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
56    connect(cancelButton, SIGNAL(pressed()), this, SLOT(cancelButtonPushed()));
57    connectSpins();
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()));
62
63    m_pool = "";
64    m_recyclePool = "";
65    m_status = "";
66    m_slot = 0;
67
68    if (!m_console->preventInUseConnect())
69       return;
70
71    /* The media's pool */
72    poolCombo->addItems(m_console->pool_list);
73
74    /* The media's Status */
75    QStringList statusList = (QStringList() << "Full" << "Used" << "Append" << "Error" << "Purged" << "Recycled");
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             " LEFT OUTER 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             " ORDER BY Pool.Name";
103
104    if (mainWin->m_sqlDebug) {
105       Pmsg1(000, "MediaList query cmd : %s\n",query.toUtf8().data());
106    }
107    QStringList results;
108    if (m_console->sql_cmd(query, results)) {
109       QString field;
110       QStringList fieldlist;
111
112       /* Iterate through the lines of results, there should only be one. */
113       foreach (QString resultline, results) {
114          fieldlist = resultline.split("\t");
115          i = 0;
116
117          /* Iterate through fields in the record */
118          foreach (field, fieldlist) {
119             field = field.trimmed();  /* strip leading & trailing spaces */
120             bool ok;
121             if (i == 0) {
122                m_mediaName = field;
123                volumeLabel->setText(QString("Volume : %1").arg(m_mediaName));
124             } else if (i == 1) {
125                m_pool = field;
126             } else if (i == 2) {
127                m_status = field;
128             } else if (i == 3) {
129                m_slot = field.toInt(&ok, 10);
130                if (!ok){ m_slot = 0; }
131             } else if (i == 4) {
132                m_retention = field.toInt(&ok, 10);
133                if (!ok){ m_retention = 0; }
134             } else if (i == 5) {
135                m_useDuration = field.toInt(&ok, 10);
136                if (!ok){ m_useDuration = 0; }
137             } else if (i == 6) {
138                m_maxVolJobs = field.toInt(&ok, 10);
139                if (!ok){ m_maxVolJobs = 0; }
140             } else if (i == 7) {
141                m_maxVolFiles = field.toInt(&ok, 10);
142                if (!ok){ m_maxVolFiles = 0; }
143             } else if (i == 8) {
144                m_maxVolBytes = field.toInt(&ok, 10);
145                if (!ok){ m_maxVolBytes = 0; }
146             } else if (i == 9) {
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;
154             }
155             i++;
156          } /* foreach field */
157       } /* foreach resultline */
158    } /* if results from query */
159
160    if (m_mediaName != "") {
161       int index;
162       /* default value for pool */
163       index = poolCombo->findText(m_pool, Qt::MatchExactly);
164       if (index != -1) {
165          poolCombo->setCurrentIndex(index);
166       }
167
168       /* default value for status */
169       index = statusCombo->findText(m_status, Qt::MatchExactly);
170       if (index != -1) {
171          statusCombo->setCurrentIndex(index);
172       }
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);
188       if (index == -1) {
189          recyclePoolCombo->insertItem(0, "");
190          index = recyclePoolCombo->findText(m_recyclePool, Qt::MatchExactly);
191       }
192       if (index != -1) {
193          recyclePoolCombo->setCurrentIndex(index);
194       }
195    } else {
196       QMessageBox::warning(this, "No Volume name", "No Volume name given",
197                            QMessageBox::Ok, QMessageBox::Ok);
198       return;
199    }
200 }
201
202 /*
203  * Function to handle updating the record then closing the page
204  */
205 void MediaEdit::okButtonPushed()
206 {
207    QString scmd;
208    this->hide();
209    bool docmd = false;
210    scmd = QString("update volume=\"%1\"")
211                   .arg(m_mediaName);
212    if (m_pool != poolCombo->currentText()) {
213       scmd += " pool=\"" + poolCombo->currentText() + "\"";
214       docmd = true;
215    }
216    if (m_status != statusCombo->currentText()) {
217       scmd += " volstatus=\"" + statusCombo->currentText() + "\"";
218       docmd = true;
219    }
220    if (m_slot != slotSpin->value()) {
221       scmd += " slot=" + QString().setNum(slotSpin->value());
222       docmd = true;
223    }
224    if (m_retention != retentionSpin->value()) {
225       scmd += " VolRetention=" + QString().setNum(retentionSpin->value());
226       docmd = true;
227    }
228    if (m_useDuration != useDurationSpin->value()) {
229       scmd += " VolUse=" + QString().setNum(useDurationSpin->value());
230       docmd = true;
231    }
232    if (m_maxVolJobs != maxJobsSpin->value()) {
233       scmd += " MaxVolJobs=" + QString().setNum(maxJobsSpin->value());
234       docmd = true;
235    }
236    if (m_maxVolFiles != maxFilesSpin->value()) {
237       scmd += " MaxVolFiles=" + QString().setNum(maxFilesSpin->value());
238       docmd = true;
239    }
240    if (m_maxVolBytes != maxBytesSpin->value()) {
241       scmd += " MaxVolBytes=" + QString().setNum(maxBytesSpin->value());
242       docmd = true;
243    }
244    if ((m_recycle) && (recycleCheck->checkState() == Qt::Unchecked)) {
245       scmd += " Recycle=no";
246       docmd = true;
247    }
248    if ((!m_recycle) && (recycleCheck->checkState() == Qt::Checked)) {
249       scmd += " Recycle=yes";
250       docmd = true;
251    }
252    if ((m_enabled) && (enabledCheck->checkState() == Qt::Unchecked)) {
253       scmd += " enabled=no";
254       docmd = true;
255    }
256    if ((!m_enabled) && (enabledCheck->checkState() == Qt::Checked)) {
257       scmd += " enabled=yes";
258       docmd = true;
259    }
260    if (m_recyclePool != recyclePoolCombo->currentText()) {
261       scmd += " recyclepool=\"" + recyclePoolCombo->currentText() + "\"";
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, days, hours, minutes, seconds, left;
303         
304    years = abs(value / 31536000);
305    left = value - years * 31536000;
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;
312    disconnectSpins();
313    yearsSpin->setValue(years);
314    daysSpin->setValue(days);
315    hoursSpin->setValue(hours);
316    minutesSpin->setValue(minutes);
317    secondsSpin->setValue(seconds);
318    connectSpins();
319 }
320
321 /*
322  * This slot is called any time any one of the 5 duration spins a changed.
323  */
324 void MediaEdit::durationChanged()
325 {
326    disconnectSpins();
327    if (secondsSpin->value() == -1) {
328       secondsSpin->setValue(59);
329       minutesSpin->setValue(minutesSpin->value()-1);
330    }
331    if (minutesSpin->value() == -1) {
332       minutesSpin->setValue(59);
333       hoursSpin->setValue(hoursSpin->value()-1);
334    }
335    if (hoursSpin->value() == -1) {
336       hoursSpin->setValue(23);
337       daysSpin->setValue(daysSpin->value()-1);
338    }
339    if (daysSpin->value() == -1) {
340       daysSpin->setValue(364);
341       yearsSpin->setValue(yearsSpin->value()-1);
342    }
343    if (yearsSpin->value() == -1) {
344       yearsSpin->setValue(0);
345    }
346
347    if (secondsSpin->value() == 60) {
348       secondsSpin->setValue(0);
349       minutesSpin->setValue(minutesSpin->value()+1);
350    }
351    if (minutesSpin->value() == 60) {
352       minutesSpin->setValue(0);
353       hoursSpin->setValue(hoursSpin->value()+1);
354    }
355    if (hoursSpin->value() == 24) {
356       hoursSpin->setValue(0);
357       daysSpin->setValue(daysSpin->value()+1);
358    }
359    if (daysSpin->value() == 365) {
360       daysSpin->setValue(0);
361       yearsSpin->setValue(yearsSpin->value()+1);
362    }
363    connectSpins();
364    if (retentionRadio->isChecked()) {
365       int retention;
366       retention = secondsSpin->value() + minutesSpin->value() * 60 + 
367          hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
368          yearsSpin->value() * 31536000;
369       disconnect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
370       retentionSpin->setValue(retention);
371       connect(retentionSpin, SIGNAL(valueChanged(int)), this, SLOT(retentionChanged()));
372    }
373    if (useDurationRadio->isChecked()) {
374       int useDuration;
375       useDuration = secondsSpin->value() + minutesSpin->value() * 60 + 
376          hoursSpin->value() * 3600 + daysSpin->value() * 86400 +
377          yearsSpin->value() * 31536000;
378       disconnect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
379       useDurationSpin->setValue(useDuration);
380       connect(useDurationSpin, SIGNAL(valueChanged(int)), this, SLOT(useDurationChanged()));
381    }
382 }
383
384 /* Connect the spins */
385 void MediaEdit::connectSpins()
386 {
387    connect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
388    connect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
389    connect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
390    connect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
391    connect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
392 }
393
394 /* disconnect spins so that we can set the value of other spin from changed duration spin */
395 void MediaEdit::disconnectSpins()
396 {
397    disconnect(secondsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
398    disconnect(minutesSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
399    disconnect(hoursSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
400    disconnect(daysSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
401    disconnect(yearsSpin, SIGNAL(valueChanged(int)), this, SLOT(durationChanged()));
402 }
403
404 /* slot for setting spins when retention radio checked */
405 void MediaEdit::retentionRadioPressed()
406 {
407    setSpins(retentionSpin->value());
408 }
409
410 /* slot for setting spins when duration radio checked */
411 void MediaEdit::useDurationRadioPressed()
412 {
413    setSpins(useDurationSpin->value());
414 }