aboutsummaryrefslogtreecommitdiffstats
path: root/kde2/kpsion/setupdialog.cpp
blob: 0498545bb052eeab771d0c944772b6a7ffed213d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*-*-c++-*-
 * $Id$
 *
 * This file is part of plptools.
 *
 *  Copyright (C) 1999-2001 Fritz Elfert <felfert@to.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "kpsionconfig.h"
#include "setupdialog.h"

#include <kapp.h>
#include <kconfig.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <kmessagebox.h>

#include <qlayout.h>
#include <qwhatsthis.h>

#include <errno.h>

SetupDialog::SetupDialog(QWidget *parent, rfsv *plpRfsv, rpcs *plpRpcs)
    : KDialogBase(Tabbed, "Settings", Ok|Apply|Default|Cancel, Ok, parent,
		  "settingsDialog", true, true)
{
    int i;
    QString tmp;
    QStringList sl;
    QStringList::Iterator sli;
    KPsionConfig pcfg;

    KConfig *config = kapp->config();

    // Page 1
    page1 = addPage(i18n("Backup &folder"));
    QGridLayout *grid = new QGridLayout(page1, 1, 1,
					marginHint() * 2, spacingHint() * 2);

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_BACKUPDIR));
    tmp = config->readEntry(pcfg.getOptionName(KPsionConfig::OPT_BACKUPDIR));
    oldBDir = tmp;
    bdirLabel = new QLabel(page1, "bdirLabel");
    bdirLabel->setText(tmp);
    bdirButton = new QPushButton(i18n("Browse"), page1);

    QWhatsThis::add(bdirLabel,
		    i18n("This is the name of the backup folder."));
    QWhatsThis::add(bdirButton,
		    i18n("Click here, for opening a dialog which lets you easily select the backup folder."));
    grid->addWidget(bdirLabel, 0, 1);
    grid->addWidget(bdirButton, 0, 2);
    grid->setColStretch(0, 1);
    connect(bdirButton, SIGNAL(clicked()), SLOT(slotBdirBrowse()));


    // Page 2
    page2 = addPage(i18n("Backup &policy"));
    grid = new QGridLayout(page2, 1, 1, marginHint() * 2, spacingHint() * 2);

    QLabel *l = new QLabel(i18n("&Incremental backup interval"), page2,
		   "iBackupIntLabel");
    grid->addWidget(l, 0, 0);

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_INCINTERVAL));
    i = config->readNumEntry(pcfg.getOptionName(KPsionConfig::OPT_INCINTERVAL));
    iIntCombo = new KComboBox(false, page2, "iIntCombo");
    iIntCombo->insertStringList(pcfg.getConfigBackupInterval());
    iIntCombo->setCurrentItem(i);
    grid->addWidget(iIntCombo, 0, 1);
    l->setBuddy(iIntCombo);

    l = new QLabel(i18n("&Full backup interval"), page2, "fBackupIntLabel");
    grid->addWidget(l, 1, 0);

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_FULLINTERVAL));
    i = config->readNumEntry(pcfg.getOptionName(KPsionConfig::OPT_FULLINTERVAL));
    fIntCombo = new KComboBox(false, page2, "fIntCombo");
    fIntCombo->insertStringList(pcfg.getConfigBackupInterval());
    fIntCombo->setCurrentItem(i);
    grid->addWidget(fIntCombo, 1, 1);
    l->setBuddy(fIntCombo);

    l = new QLabel(i18n("Backup &generations"), page2, "backupGenLabel");
    grid->addWidget(l, 2, 0);

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_BACKUPGEN));
    i = config->readNumEntry(pcfg.getOptionName(KPsionConfig::OPT_BACKUPGEN));
    genSpin = new KIntSpinBox(0, 10, 1, i, 10, page2, "backupGenSpin");
    grid->addWidget(genSpin, 2, 1);
    l->setBuddy(genSpin);

    grid->setColStretch(0, 1);

    // Page 3
    page3 = addPage(i18n("&Connection"));
    grid = new QGridLayout(page3, 1, 1, marginHint() * 2, spacingHint() * 2);

    l = new QLabel(i18n("&Connection retry interval (sec.)"), page3,
		   "rconLabel");
    grid->addWidget(l, 0, 0);

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_CONNRETRY));
    i = config->readNumEntry(pcfg.getOptionName(KPsionConfig::OPT_CONNRETRY));
    rconSpin = new KIntSpinBox(0, 600, 1, i, 10, page3, "rconSpin");
    grid->addWidget(rconSpin, 0, 1);
    l->setBuddy(rconSpin);

    l = new QLabel(i18n("Serial &device"), page3, "devLabel");
    grid->addWidget(l, 1, 0);

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_SERIALDEV));
    tmp = config->readEntry(pcfg.getOptionName(KPsionConfig::OPT_SERIALDEV));

    devCombo = new KComboBox(false, page3, "devCombo");
    sl = pcfg.getConfigDevices();
    devCombo->insertStringList(sl);
    if (tmp == "off")
	devCombo->setCurrentItem(0);
    else
	for (i = 0, sli = sl.begin(); sli != sl.end(); ++i, ++sli) {
	    if (*sli == tmp)
		devCombo->setCurrentItem(i);
	}
    grid->addWidget(devCombo, 1, 1);
    l->setBuddy(devCombo);

    l = new QLabel(i18n("Serial &speed"), page3, "speedLabel");
    grid->addWidget(l, 2, 0);

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_SERIALSPEED));
    tmp = config->readEntry(pcfg.getOptionName(KPsionConfig::OPT_SERIALSPEED));

    speedCombo = new KComboBox(false, page3, "speedCombo");
    sl = pcfg.getConfigSpeeds();
    speedCombo->insertStringList(sl);
    for (i = 0, sli = sl.begin(); sli != sl.end(); ++i, ++sli) {
	if (*sli == tmp)
	    speedCombo->setCurrentItem(i);
    }
    grid->addWidget(speedCombo, 2, 1);
    l->setBuddy(speedCombo);

    grid->setColStretch(0, 1);

    connect(this, SIGNAL(defaultClicked()), SLOT(slotDefaultClicked()));
    connect(this, SIGNAL(okClicked()), SLOT(slotSaveSettings()));
    connect(this, SIGNAL(applyClicked()), SLOT(slotSaveSettings()));
}

void SetupDialog::
slotDefaultClicked() {
    KPsionConfig pcfg;

    bdirLabel->setText(pcfg.getStrDefault(KPsionConfig::DEF_BACKUPDIR));
    iIntCombo->setCurrentItem(pcfg.getIntDefault(KPsionConfig::DEF_INCINTERVAL));
    fIntCombo->setCurrentItem(pcfg.getIntDefault(KPsionConfig::DEF_FULLINTERVAL));
    genSpin->setValue(pcfg.getIntDefault(KPsionConfig::DEF_BACKUPGEN));
    rconSpin->setValue(pcfg.getIntDefault(KPsionConfig::DEF_CONNRETRY));
    devCombo->setCurrentItem(pcfg.getIntDefault(KPsionConfig::DEF_SERIALDEV));
    speedCombo->setCurrentItem(pcfg.getIntDefault(KPsionConfig::DEF_SERIALSPEED));
}

void SetupDialog::
slotBdirBrowse() {
    QString dir = KFileDialog::getExistingDirectory(bdirLabel->text(), this,
						    i18n("Backup folder"));
    checkBackupDir(dir);
}

void SetupDialog::
slotSaveSettings() {
    KConfig *config = kapp->config();
    KPsionConfig pcfg;

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_BACKUPDIR));
    config->writeEntry(pcfg.getOptionName(KPsionConfig::OPT_BACKUPDIR),
		       bdirLabel->text());

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_BACKUPGEN));
    config->writeEntry(pcfg.getOptionName(KPsionConfig::OPT_BACKUPGEN),
		       genSpin->value());

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_INCINTERVAL));
    config->writeEntry(pcfg.getOptionName(KPsionConfig::OPT_INCINTERVAL),
		       iIntCombo->currentItem());

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_FULLINTERVAL));
    config->writeEntry(pcfg.getOptionName(KPsionConfig::OPT_FULLINTERVAL),
		       fIntCombo->currentItem());

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_CONNRETRY));
    config->writeEntry(pcfg.getOptionName(KPsionConfig::OPT_CONNRETRY),
		       rconSpin->value());

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_SERIALDEV));
    if (devCombo->currentItem() == 0)
	config->writeEntry(pcfg.getOptionName(KPsionConfig::OPT_SERIALDEV),
			   "off");
    else
	config->writeEntry(pcfg.getOptionName(KPsionConfig::OPT_SERIALDEV),
			   devCombo->currentText());

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_SERIALSPEED));
    config->writeEntry(pcfg.getOptionName(KPsionConfig::OPT_SERIALSPEED),
		       speedCombo->currentText());
}

bool SetupDialog::
showPage(int index) {
    switch (activePageIndex()) {
	case 1:
	    QString dir(bdirLabel->text());
	    if (!checkBackupDir(dir))
		return false;
    }
    return KDialogBase::showPage(index);
}

void SetupDialog::
closeEvent(QCloseEvent *e) {
    reject();
}

bool SetupDialog::
checkBackupDir(QString &dir) {
    KConfig *config = kapp->config();
    KPsionConfig pcfg;
    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_BACKUPDIR));
    QString tmp =
	config->readEntry(pcfg.getOptionName(KPsionConfig::OPT_BACKUPDIR));

    bool rmFlag = false;

    if (!bdirCreated.isEmpty()) {
	if (bdirCreated != dir) {
	    rmFlag = true;
	    bdirCreated = "";
	}
    }
    if (!dir.isEmpty()) {
	QDir d(dir);
	if (!d.exists()) {
	    if (KMessageBox::questionYesNo(this,
					   i18n("<QT>The folder <B>%1</B> does <B>not</B> exist.<BR/>Shall it be created?</QT>").arg(dir)) == KMessageBox::No) {
		bdirLabel->setText(tmp);
		return false;
	    }
	    if (mkdir(dir.data(), 0700) != 0) {
		QString msg = i18n("<QT>The specified folder<BR/><B>%1</B><BR/>could <B>not</B> be created");
		switch (errno) {
		    case EACCES:
		    case EPERM:
		    case EROFS:
			msg += i18n(", because you either don't have sufficient rights to do that, or the filesystem is readonly.");
			// Insufficient permissions/ readonly FS
			break;
		    case ENOSPC:
			msg += i18n(", because the filesystem has not enough space.");
			// No space
			break;
		    case EEXIST:
			// shouldn't happen, we checked already
			// for existence.
			msg += i18n(", because there already exists another object with the same name.");
			break;
		    case EFAULT:
		    case ENOMEM:
		    case ENAMETOOLONG:
			// shouldn't happen.
			msg += ".";
			break;
		    case ENOENT:
			// propably dangling symlink
			msg += i18n(", because you specified a path which probably contains a dangling symbolic link.");
			break;
		    case ENOTDIR:
			msg += i18n(", because you specified a path which contains an element which is not a folder.");
			// path element not dir.
			break;
		    case ELOOP:
			msg += i18n(", because you specified a path which contains too many symbolic links.");
			// Too many symlinks
			break;


		}
		bdirLabel->setText(tmp);
		msg += i18n("<BR/>Please select another folder.</QT>");
		KMessageBox::error(this, msg.arg(dir));
		return false;
	    }
	    bdirCreated = dir;
	}
	QDir od(oldBDir);
	if ((!oldBDir.isEmpty()) && (oldBDir != dir) && (od.exists())) {
	    QStringList entries = od.entryList();
	    QStringList::Iterator ui;
	    QStringList::Iterator ei;
	    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_UIDS));
	    QStringList uids = config->readListEntry(
		pcfg.getOptionName(KPsionConfig::OPT_UIDS));
	    for (ei = entries.begin(); ei != entries.end(); ++ei) {
		for (ui = uids.begin(); ui != uids.end(); ++ui) {
		    if ((*ei) == (*ui)) {
			QString from = oldBDir;
			QString to = dir;
			
			from += "/"; from += *ui;
			to += "/"; to += *ui;
			if (::rename(from.latin1(), to.latin1()) != 0) {
			    KMessageBox::error(this,
					       i18n("Could not move existing backup "
						    "for machine %1 to %2.").arg(*ui).arg(to));
			}
		    }
		}
	    }
	}
	if (rmFlag)
	    ::rmdir(oldBDir.latin1());
	bdirLabel->setText(dir);
	oldBDir = dir;
	return true;
    }
    bdirLabel->setText(tmp);
    return false;
}

/*
 * Local variables:
 * c-basic-offset: 4
 * End:
 */