aboutsummaryrefslogtreecommitdiffstats
path: root/kde2/kpsion/kpsionbackuplistview.cpp
blob: 48977536d0ed5dfb773adbdc8d62612bcb5ae5e6 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*-*-c++-*-
 * $Id$
 *
 * This file is part of plptools.
 *
 *  Copyright (C) 1999  Philip Proudman <philip.proudman@btinternet.com>
 *  Copyright (C) 2000, 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 "kpsion.h"
#include "kpsionbackuplistview.h"
#include "kpsionconfig.h"

#include <kapp.h>
#include <klocale.h>
#include <kconfig.h>
#include <kiconloader.h>

#include <qwhatsthis.h>
#include <qiodevice.h>
#include <qheader.h>
#include <qdir.h>

class KPsionCheckListItem::KPsionCheckListItemMetaData {
    friend class KPsionCheckListItem;

private:
    KPsionCheckListItemMetaData();
    ~KPsionCheckListItemMetaData() { };

    bool parentIsKPsionCheckListItem;
    bool dontPropagate;
    bool gray;
    int backupType;
    int size;
    time_t when;
    u_int32_t timeHi;
    u_int32_t timeLo;
    u_int32_t attr;
    QString name;
};

KPsionCheckListItem::KPsionCheckListItemMetaData::KPsionCheckListItemMetaData() {
    when = 0;
    size = 0;
    timeHi = 0;
    timeLo = 0;
    attr = 0;
    gray = false;
    name = QString::null;
    backupType = KPsionBackupListView::UNKNOWN;
}

KPsionCheckListItem::~KPsionCheckListItem() {
    delete meta;
}

/**
 * Must re-implement this since QT3 does not leave the expansion square
 * enabled anymore if the item is disabled.
 */
void KPsionCheckListItem::
paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int align)
{
    QColorGroup tmp(cg);
    if (meta->gray)
	tmp.setColor(QColorGroup::Text,
		     listView()->palette().color(QPalette::Disabled,
						 QColorGroup::Text));
    else
	tmp.setColor(QColorGroup::Text,
		     listView()->palette().color(QPalette::Normal,
						 QColorGroup::Text));
    QCheckListItem::paintCell(p, tmp, column, width, align);
}

KPsionCheckListItem *KPsionCheckListItem::
firstChild() const {
    return (KPsionCheckListItem *)QListViewItem::firstChild();
}

KPsionCheckListItem *KPsionCheckListItem::
nextSibling() const {
    return (KPsionCheckListItem *)QListViewItem::nextSibling();
}

void KPsionCheckListItem::
init(bool myparent) {
    setSelectable(false);
    meta = new KPsionCheckListItemMetaData();
    meta->dontPropagate = false;
    meta->parentIsKPsionCheckListItem = myparent;
}

void KPsionCheckListItem::
setMetaData(int type, time_t when, QString name, int size,
	    u_int32_t tHi, u_int32_t tLo, u_int32_t attr) {
	meta->backupType = type;
	meta->when = when;
	meta->name = name;
	meta->size = size;
	meta->timeHi = tHi;
	meta->timeLo = tLo;
	meta->attr = attr;
}

void KPsionCheckListItem::
stateChange(bool state) {
    if (!state)
	meta->gray = false;
    QCheckListItem::stateChange(state);
    if (meta->dontPropagate)
	return;
    if (meta->parentIsKPsionCheckListItem)
	((KPsionCheckListItem *)QListViewItem::parent())->propagateUp(state);
    else
	emit rootToggled();
    propagateDown(state);
}

void KPsionCheckListItem::
propagateDown(bool state) {
    setOn(state);
    KPsionCheckListItem *child = firstChild();
    while (child) {
	child->meta->dontPropagate = true;
	child->propagateDown(state);
	child->meta->dontPropagate = false;
	child = child->nextSibling();
    }
}

void KPsionCheckListItem::
propagateUp(bool state) {
    bool makeGray = false;

    KPsionCheckListItem *child = firstChild();
    while (child) {
	if (child->isOn() != state) {
	    makeGray = true;
	    break;
	}
	child = child->nextSibling();
    }
    meta->dontPropagate = true;
    if (makeGray) {
	meta->gray = true;
	setOn(true);
    } else {
	meta->gray = false;
	setOn(state);
    }
    // Bug in QListView? It doesn't update, when
    // enabled/disabled without activating.
    // -> force it.
    listView()->repaintItem(this);
    meta->dontPropagate = false;
    if (meta->parentIsKPsionCheckListItem)
	((KPsionCheckListItem *)QListViewItem::parent())->propagateUp(state);
    else
	emit rootToggled();
}

QString KPsionCheckListItem::
key(int column, bool ascending) const {
    if (meta->when) {
	QString tmp;
	tmp.sprintf("%08d", meta->when);
	return tmp;
    }
    return text();
}

QString KPsionCheckListItem::
psionname() {
    if (meta->parentIsKPsionCheckListItem)
	return meta->name;
    else
	return QString::null;
}

QString KPsionCheckListItem::
unixname() {
    if (meta->parentIsKPsionCheckListItem)
	return KPsionMainWindow::psion2unix(meta->name);
    else
	return QString::null;
}

QString KPsionCheckListItem::
tarname() {
    if (meta->parentIsKPsionCheckListItem)
	return ((KPsionCheckListItem *)QListViewItem::parent())->tarname();
    else
	return meta->name;
}

QString KPsionCheckListItem::
psionpath() {
    QString tmp = text();
    QCheckListItem *p = this;
    while (p->depth() > 1) {
	p = (QCheckListItem *)p->parent();
	tmp = p->text() + "/" + tmp;
    }
    return KPsionMainWindow::unix2psion(tmp);
}

int KPsionCheckListItem::
backupType() {
    if (meta->parentIsKPsionCheckListItem)
	return ((KPsionCheckListItem *)QListViewItem::parent())->backupType();
    else
	return meta->backupType;
}

time_t KPsionCheckListItem::
when() {
    if (meta->parentIsKPsionCheckListItem)
	return ((KPsionCheckListItem *)QListViewItem::parent())->when();
    else
	return meta->when;
}

PlpDirent KPsionCheckListItem::
plpdirent() {
    assert(meta->parentIsKPsionCheckListItem);
    return PlpDirent(meta->size, meta->attr, meta->timeHi, meta->timeLo,
		     meta->name);
}

KPsionBackupListView::KPsionBackupListView(QWidget *parent, const char *name)
    : KListView(parent, name) {

    toRestore.clear();
    uid = QString::null;
    KConfig *config = kapp->config();
    KPsionConfig pcfg;

    config->setGroup(pcfg.getSectionName(KPsionConfig::OPT_BACKUPDIR));
    backupDir = config->readEntry(
	pcfg.getOptionName(KPsionConfig::OPT_BACKUPDIR));
    addColumn(i18n("Available backups"));
    setRootIsDecorated(true);
    setSorting(-1);
}

KPsionCheckListItem *KPsionBackupListView::
firstChild() const {
    return (KPsionCheckListItem *)QListView::firstChild();
}

void KPsionBackupListView::
readBackups(QString uid) {
    QString bdir(backupDir);
    bdir += "/";
    bdir += uid;
    QDir d(bdir);
    kapp->processEvents();
    const QFileInfoList *fil =
	d.entryInfoList("*.tar.gz", QDir::Files|QDir::Readable, QDir::Time);
    QFileInfoListIterator it(*fil);
    QFileInfo *fi;
    while ((fi = it.current())) {
	kapp->processEvents();

	bool isValid = false;
#if KDE_VERSION >= 300
	KTar tgz(fi->absFilePath());
#else
	KTarGz tgz(fi->absFilePath());
#endif
	const KTarEntry *te;
	QString bTypeName;
	int bType;
	QDateTime date;

	tgz.open(IO_ReadOnly);
	te = tgz.directory()->entry("KPsionFullIndex");
	if (te && (!te->isDirectory())) {
	    date.setTime_t(te->date());
	    bTypeName = i18n("Full");
	    bType = FULL;
	    isValid = true;
	} else {
	    te = tgz.directory()->entry("KPsionIncrementalIndex");
	    if (te && (!te->isDirectory())) {
		date.setTime_t(te->date());
		bTypeName = i18n("Incremental");
		bType = INCREMENTAL;
		isValid = true;
	    }
	}

	if (isValid) {
	    QString n =	i18n("%1 backup, created at %2").arg(bTypeName).arg(KGlobal::locale()->formatDateTime(date, false));
	    QByteArray a = ((KTarFile *)te)->data();
	    QTextIStream indexData(a);

	    indexData.readLine();
	    indexData.flags(QTextStream::hex);
	    indexData.fill('0');
	    indexData.width(8);

	    KPsionCheckListItem *i =
		new KPsionCheckListItem(this, n,
					KPsionCheckListItem::CheckBox);
	    i->setMetaData(bType, te->date(), tgz.fileName(), 0, 0, 0, 0);
	    i->setPixmap(0, KGlobal::iconLoader()->loadIcon("tgz",
							 KIcon::Small));
	    connect(i, SIGNAL(rootToggled()), this, SLOT(slotRootToggled()));

	    indexDataList_t idList;
	    while (!indexData.atEnd()) {
		indexData_t id;

		indexData >> id.timeHi >> id.timeLo >> id.size >> id.attr;
		id.name = indexData.readLine().mid(1);
		idList.push_back(id);
	    }

	    QStringList files = tgz.directory()->entries();
	    for (QStringList::Iterator f = files.begin();
		 f != files.end(); f++)
		if ((*f != "KPsionFullIndex") &&
		    (*f != "KPsionIncrementalIndex"))
		    listTree(i, tgz.directory()->entry(*f), idList, 0);
	}
	tgz.close();
	++it;
    }
    header()->setClickEnabled(false);
    header()->setResizeEnabled(false);
    header()->setMovingEnabled(false);
    setMinimumSize(columnWidth(0) + 4, height());
    QWhatsThis::add(this, i18n(
			"<qt>Here, you can select the available backups."
			" Select the items you want to restore, the click"
			" on <b>Start</b> to start restoring these items."
			"</qt>"));
}

void KPsionBackupListView::
listTree(KPsionCheckListItem *cli, const KTarEntry *te, indexDataList_t &idx,
	 int level) {
    KPsionCheckListItem *i =
	new KPsionCheckListItem(cli, te->name(),
				KPsionCheckListItem::CheckBox);
    kapp->processEvents();
    if (te->isDirectory()) {
	if (level)
	    i->setPixmap(0, KGlobal::iconLoader()->loadIcon("folder",
							    KIcon::Small));
	else
	    i->setPixmap(0, KGlobal::iconLoader()->loadIcon("hdd_unmount",
							    KIcon::Small));
	i->setMetaData(0, 0, QString::null, 0, 0, 0, 0);
	KTarDirectory *td = (KTarDirectory *)te;
	QStringList files = td->entries();
	for (QStringList::Iterator f = files.begin(); f != files.end(); f++)
	    listTree(i, td->entry(*f), idx, level + 1);
    } else {
	indexDataList_t::iterator ii;

	QString name = i->psionpath();
	for (ii = idx.begin(); ii != idx.end(); ii++) {
	    if (ii->name == name) {
		i->setMetaData(0, 0, name, ii->size, ii->timeHi, ii->timeLo,
			       ii->attr);
		break;
	    }
	}

	i->setPixmap(0, KGlobal::iconLoader()->loadIcon("mime_empty",
							KIcon::Small));
    }
}

void KPsionBackupListView::
slotRootToggled() {
    bool anyOn = false;
    KPsionCheckListItem *i = firstChild();
    while (i != 0L) {
	if (i->isOn()) {
	    anyOn = true;
	    break;
	}
	i = i->nextSibling();
    }
    emit itemsEnabled(anyOn);
}

QStringList KPsionBackupListView::
getSelectedTars() {
    QStringList l;

    KPsionCheckListItem *i = firstChild();
    while (i != 0L) {
	if (i->isOn())
	    l += i->tarname();
	i = i->nextSibling();
    }
    return l;
}

QStringList KPsionBackupListView::
getFormatDrives() {
    QStringList l;

    KPsionCheckListItem *i = firstChild();
    while (i != 0L) {
	if (i->isOn()) {
	    KPsionCheckListItem *c = i->firstChild();
	    while (c != 0L) {
		if (c->isOn()) {
		    QString drv = c->text().left(1);
		    if (l.find(drv) == l.end())
			l += drv;
		}
		c = c->nextSibling();
	    }
	}
	i = i->nextSibling();
    }
    return l;
}

bool KPsionBackupListView::
autoSelect(QString drive) {
    KPsionCheckListItem *latest = NULL;
    time_t stamp = 0;

    drive += ":";
    // Find latest full backup for given drive
    KPsionCheckListItem *i = firstChild();
    while (i != 0L) {
	if ((i->backupType() == FULL) && (i->when() > stamp)) {
	    KPsionCheckListItem *c = i->firstChild();
	    while (c != 0L) {
		if (c->text() == drive) {
		    latest = c;
		    stamp = i->when();
		    break;
		}
		c = c->nextSibling();
	    }
	}
	i = i->nextSibling();
    }
    // Now find all incremental backups for given drive which are newer than
    // selected backup
    if (latest != 0) {
	latest->setOn(true);
	i = firstChild();
	while (i != 0L) {
	    if ((i->backupType() == INCREMENTAL) && (i->when() >= stamp)) {
		KPsionCheckListItem *c = i->firstChild();
		while (c != 0L) {
		    if (c->text() == drive)
			c->setOn(true);
		    c = c->nextSibling();
		}
	    }
	    i = i->nextSibling();
	}
    }
    return (latest != 0L);
}

void KPsionBackupListView::
collectEntries(KPsionCheckListItem *i) {
    while (i != 0L) {
	KPsionCheckListItem *c = i->firstChild();
	if (c == 0L) {
	    if (i->isOn())
		toRestore.push_back(i->plpdirent());
	} else
	    collectEntries(c);
	i = i->nextSibling();
    }
}

PlpDir &KPsionBackupListView::
getRestoreList(QString tarname) {
    toRestore.clear();
    KPsionCheckListItem *i = firstChild();
    while (i != 0L) {
	if ((i->tarname() == tarname) && (i->isOn())) {
	    collectEntries(i->firstChild());
	    break;
	}
	i = i->nextSibling();
    }
    return toRestore;
}

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