aboutsummaryrefslogtreecommitdiffstats
path: root/kde2/plugins/plpprops.cc
blob: 6db2c5bf44aa7f876329509910b15dbaf31c2c1e (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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/*-*-c++-*-
 * $Id$
 *
 * This file is part of plptools.
 *
 *  Copyright (C) 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 "plpprops.h"
#include "pie3dwidget.h"

#include <kdebug.h>
#include <klocale.h>
#include <kio/slaveinterface.h>
#include <krun.h>

#include <qlayout.h>
#include <qlabel.h>
#include <qobjectlist.h>
#include <qtabwidget.h>
#include <qcheckbox.h>
#include <qwhatsthis.h>

#include <rfsv.h>
#include <rpcs.h>

#include <strstream>
#include <iomanip>

#define PLP_CMD_DRIVEINFO 1
#define PLP_CMD_OWNERINFO 2
#define PLP_CMD_GETATTR   3
#define PLP_CMD_SETATTR   4
#define PLP_CMD_MACHINFO  5

#define KIO_ARGS QByteArray packedArgs; \
QDataStream stream( packedArgs, IO_WriteOnly ); stream

class PlpPropsPlugin::PlpPropsPluginPrivate {
public:
    PlpPropsPluginPrivate() { }
    ~PlpPropsPluginPrivate() { }

    QFrame *frame;
};

/*
 * A VERY UGLY HACK for removing the Permissions-Page from
 * the Properties dialog.
 */
static void
removeDialogPage(QWidget *theDialog, QString theLabel) {
    QObject *qtabwidget = 0L;
    QFrame *permframe = 0L;

    //
    // First, find the QTabWidget in the dialog.
    // Easy, cause there's only one.
    //
    QObjectList *l = theDialog->queryList("QTabWidget");
    QObjectListIt it(*l);
    QObject * obj;
    while ((obj=it.current()) != 0) {
	++it;
	qtabwidget = obj;
    }
    delete l;

    // Now, walk thru all Childs of the QTabWidget which are
    // inherited from class QFrame. For every found frame,
    // compare the tabLabel against the required label (&Permissions).
    //
    if (qtabwidget != 0L) {
	l = qtabwidget->queryList("QFrame");
	QObjectListIt it(*l);

	while ((obj = it.current()) != 0) {
	    if (theLabel ==
		((QTabWidget *)qtabwidget)->tabLabel((QWidget*)obj)) {
		permframe = (QFrame *)obj;
		break;
	    }
	    ++it;
	}
	delete l;

	// If we found it, remove it.
	if (permframe != 0)
	    ((QTabWidget *)qtabwidget)->removePage(permframe);
    }
}

PlpPropsPlugin::PlpPropsPlugin(KPropertiesDialog *_props)
    : KPropsDlgPlugin( _props )
{
    d = new PlpPropsPluginPrivate;
    bool removePerms = false;
    bool removeGeneral = false;

    if (!supports(properties->items()))
	return;

    if (PlpFileAttrPage::supports(properties->items())) {
	PlpFileAttrPage *p = new PlpFileAttrPage(_props);
	connect(p, SIGNAL(changed()), SLOT(doChange()));
	connect(this, SIGNAL(save()), p, SLOT(applyChanges()));
	removePerms = true;
    }
    if (PlpDriveAttrPage::supports(properties->items())) {
	PlpDriveAttrPage *p = new PlpDriveAttrPage(_props);
	removePerms = true;
    }
    if (PlpMachinePage::supports(properties->items())) {
	PlpMachinePage *p = new PlpMachinePage(_props);
	removePerms = true;
	removeGeneral = true;
    }
    if (PlpOwnerPage::supports(properties->items())) {
	PlpOwnerPage *p = new PlpOwnerPage(_props);
	removePerms = true;
	removeGeneral = true;
    }
    if (removePerms)
	removeDialogPage(properties->dialog(), i18n("&Permissions"));
    if (removeGeneral)
	removeDialogPage(properties->dialog(), i18n("&General"));
}

PlpPropsPlugin::~PlpPropsPlugin() {
    delete d;
}

bool PlpPropsPlugin::supports(KFileItemList _items) {
    for (KFileItemListIterator it(_items); it.current(); ++it) {
	KFileItem *fi = it.current();

	if (fi->url().protocol() != QString::fromLatin1("psion"))
	    return false;
    }
    return true;
}

void PlpPropsPlugin::applyChanges() {
    emit save();
}

void PlpPropsPlugin::doChange() {
    emit changed();
}

class PlpFileAttrPage::PlpFileAttrPagePrivate {

    typedef struct {
	const char * const lbl;
	const u_int32_t mask;
	bool inverted;
	bool direnabled;
	bool s5enabled;
    } UIelem;

public:

    PlpFileAttrPagePrivate();
    ~PlpFileAttrPagePrivate() {}

    KPropertiesDialog *props;

    bool  jobReturned;
    u_int32_t flags;
    u_int32_t attr;

    const UIelem *generic;
    const UIelem *s3;
    const UIelem *s5;

    QFrame *frame;
    QLabel *psiPath;
    QCheckBox *genCb[5];  // MUST match initializers below!!!
    QCheckBox *specCb[3]; // MUST match initializers below!!!
};

PlpFileAttrPage::PlpFileAttrPagePrivate::PlpFileAttrPagePrivate() {
    int i;

    static const UIelem _generic[] = {
	{ "Readable",  rfsv::PSI_A_READ,    false, false, false }, // Fake for S5
	{ "Writeable", rfsv::PSI_A_RDONLY,  true,  true,  true  },
	{ "Hidden",    rfsv::PSI_A_HIDDEN,  false, true,  true  },
	{ "System",    rfsv::PSI_A_SYSTEM,  false, false, true  },
	{ "Archive",   rfsv::PSI_A_ARCHIVE, false, true,  true  },
	{ 0L,          0L,                  false, false, true  },
    };
    static const UIelem _s3[] = {
	{ "Executable", rfsv::PSI_A_EXEC,   false, false, true },
	{ "Stream",     rfsv::PSI_A_STREAM, false, false, true },
	{ "Text",       rfsv::PSI_A_TEXT,   false, false, true },
	{ 0L,           0L,                 false, false, true },
    };
    static const UIelem _s5[] = {
	{ "Normal",     rfsv::PSI_A_NORMAL,     false, false, true },
	{ "Temporary",  rfsv::PSI_A_TEMP,       false, false, true },
	{ "Compressed", rfsv::PSI_A_COMPRESSED, false, false, true },
	{ 0L,          0L,                      false, false, true },
    };
    generic = _generic;
    s3 = _s3;
    s5 = _s5;
}

PlpFileAttrPage::PlpFileAttrPage(KPropertiesDialog *_props) {
    QGridLayout *mgl;
    QGridLayout *gl;
    QGroupBox *gb;
    QLabel *l;
    int i;

    d = new PlpFileAttrPagePrivate;
    d->props = _props;
    d->frame = _props->dialog()->addPage(i18n("Psion &Attributes"));

    mgl = new QGridLayout(d->frame, 1, 1, KDialog::marginHint(),
			  KDialog::spacingHint(), "mainLayout");

    l = new QLabel(i18n("Path on Psion:"), d->frame, "psiPathLabel");
    mgl->addWidget(l, 0, 0);

    d->psiPath = new QLabel(QString::fromLatin1("?"), d->frame, "psiPath");
    mgl->addWidget(d->psiPath, 0, 1);
    mgl->setColStretch(1, 1);

    gb = new QGroupBox(i18n("Generic attributes"), d->frame, "genattrBox");
    mgl->addMultiCellWidget(gb, 1, 1, 0, 1);
    gl = new QGridLayout (gb, 1, 1, KDialog::marginHint(),
			  KDialog::spacingHint(), "genattrLayout");
    for (i = 0; d->generic[i].lbl; i++) {
	QString lbl = KGlobal::locale()->translate(d->generic[i].lbl);
	d->genCb[i] = new QCheckBox(lbl, gb, d->generic[i].lbl);
	d->genCb[i]->setEnabled(false);
	connect(d->genCb[i], SIGNAL(toggled(bool)), SLOT(slotCbToggled(bool)));
	gl->addWidget(d->genCb[i], 0, i);
    }

    gb = new QGroupBox(i18n("Machine specific attributes"), d->frame,
		       "specattrBox");
    mgl->addMultiCellWidget(gb, 2, 2, 0, 1);
    gl = new QGridLayout (gb, 1, 1, KDialog::marginHint(),
			  KDialog::spacingHint(), "specattrLayout");
    for (i = 0; d->s5[i].lbl; i++) {
	QString lbl = KGlobal::locale()->translate(d->s5[i].lbl);
	d->specCb[i] = new QCheckBox(lbl, gb, d->s5[i].lbl);
	d->specCb[i]->setEnabled(false);
	connect(d->specCb[i], SIGNAL(toggled(bool)), SLOT(slotCbToggled(bool)));
	gl->addWidget(d->specCb[i], 0, i);
    }
    mgl->addRowSpacing(3, KDialog::marginHint());

    d->jobReturned = false;
    KIO_ARGS << int(PLP_CMD_GETATTR) << _props->item()->url().path();
    KIO::StatJob *job = new KIO::StatJob(KURL("psion:/"), KIO::CMD_SPECIAL,
					 packedArgs, false);
    connect(job, SIGNAL(result(KIO::Job *)),
	    SLOT(slotGetSpecialFinished(KIO::Job *)));
}

PlpFileAttrPage::~PlpFileAttrPage() {
    delete d;
}

bool PlpFileAttrPage::supports(KFileItemList _items) {
    for (KFileItemListIterator it(_items); it.current(); ++it) {
	KFileItem *fi = it.current();

	QString path = fi->url().path(-1);
	if (path.contains('/') == 1)
	    return false;
    }
    return true;
}

void PlpFileAttrPage::applyChanges() {
    u_int32_t attr = 0;
    bool isS5 = ((d->flags & 1) != 0);
    bool val;
    int i;

    for (i = 0; d->generic[i].lbl; i++) {
	val = d->genCb[i]->isChecked();
	if (d->generic[i].inverted)
	    val = !val;
	if (val)
	    attr |= d->generic[i].mask;
    }
    if (isS5)
	for (i = 0; d->s5[i].lbl; i++) {
	    val = d->specCb[i]->isChecked();
	    if (d->s5[i].inverted)
		val = !val;
	    if (val)
		attr |= d->s5[i].mask;
	}
    else
	for (i = 0; d->s3[i].lbl; i++) {
	    val = d->specCb[i]->isChecked();
	    if (d->s3[i].inverted)
		val = !val;
	    if (val)
		attr |= d->s3[i].mask;
	}
    if (d->attr != attr) {
	u_int32_t mask = d->attr ^ attr;
	u_int32_t sattr = attr & mask;
	u_int32_t dattr = ~sattr & mask;
	cout << "apply: old=" << hex << d->attr << " new=" << attr << endl;
	cout << "apply: m=" << hex << mask << " s=" << sattr << " d=" << dattr << endl;

	KIO_ARGS << int(PLP_CMD_SETATTR) << sattr << dattr
		 << d->props->item()->url().path();
	KIO::SimpleJob *sjob = new KIO::SimpleJob(KURL("psion:/"),
						  KIO::CMD_SPECIAL, packedArgs,
						  false);
	connect(sjob, SIGNAL(result(KIO::Job *)),
		SLOT(slotSetSpecialFinished(KIO::Job *)));
    }
}

void PlpFileAttrPage::slotCbToggled(bool) {
    if (d->jobReturned)
	emit changed();
}

void PlpFileAttrPage::slotSetSpecialFinished(KIO::Job *job) {
    KIO::SimpleJob *sJob = static_cast<KIO::SimpleJob *>(job);

    if (sJob->error())
	job->showErrorDialog(d->props->dialog());
}

void PlpFileAttrPage::slotGetSpecialFinished(KIO::Job *job) {
    KIO::StatJob *sJob = static_cast<KIO::StatJob *>(job);

    if (sJob->error())
	job->showErrorDialog(d->props->dialog());
    else {
	KIO::UDSEntry e = sJob->statResult();
	bool attr_found = false;
	bool flags_found = false;
	u_int32_t flags;
	u_int32_t attr;

	for (KIO::UDSEntry::ConstIterator it = e.begin(); it != e.end(); ++it) {
	    if ((*it).m_uds == KIO::UDS_SIZE) {
		attr_found = true;
		attr = (unsigned long)((*it).m_long);
	    }
	    if ((*it).m_uds == KIO::UDS_CREATION_TIME) {
		flags_found = true;
		flags = (unsigned long)((*it).m_long);
	    }
	    if ((*it).m_uds == KIO::UDS_NAME)
		d->psiPath->setText((*it).m_str);
	}
	if (attr_found && flags_found) {
	    bool isS5 = ((flags & 1) != 0);
	    bool isRom = ((flags & 2) != 0);
	    bool noDir = ((attr & rfsv::PSI_A_DIR) == 0);
	    int i;

	    d->attr = attr;
	    d->flags = flags;
	    for (i = 0; d->generic[i].lbl; i++) {
		bool val = ((attr & d->generic[i].mask) != 0);
		if (d->generic[i].inverted)
		    val = !val;
		d->genCb[i]->setChecked(val);
		if ((!isRom) && (d->generic[i].s5enabled) &&
		    (noDir || d->generic[i].direnabled)) {
		    d->genCb[i]->setEnabled(true);
		}
	    }
	    if (isS5)
		for (i = 0; d->s5[i].lbl; i++) {
		    bool val = ((attr & d->s5[i].mask) != 0);
		    d->specCb[i]->setChecked(val);
		    if ((!isRom) && (noDir || d->s5[i].direnabled))
			d->specCb[i]->setEnabled(true);
		}
	    else
		for (i = 0; d->s3[i].lbl; i++) {
		    bool val = ((attr & d->s3[i].mask) != 0);
		    QString lbl = KGlobal::locale()->translate(d->s3[i].lbl);
		    d->specCb[i]->setText(lbl);
		    d->specCb[i]->setChecked(val);
		    if ((!isRom) && (noDir || d->s3[i].direnabled))
			d->specCb[i]->setEnabled(true);
		}
	}
    }
    d->jobReturned = true;
}

class PlpDriveAttrPage::PlpDriveAttrPagePrivate {
public:
    PlpDriveAttrPagePrivate() { }
    ~PlpDriveAttrPagePrivate() { }

    QColor  usedColor;
    QColor  freeColor;
    QString driveLetter;

    KPropertiesDialog *props;
    QFrame *frame;
    QGroupBox *gb;
    Pie3DWidget *pie;
    QLabel *typeLabel;
    QLabel *totalLabel;
    QLabel *freeLabel;
    QLabel *uidLabel;
    QPushButton *restoreButton;
    QPushButton *formatButton;
};

PlpDriveAttrPage::PlpDriveAttrPage(KPropertiesDialog *_props) {
    d = new PlpDriveAttrPagePrivate;
    d->props = _props;
    d->frame = _props->dialog()->addPage(i18n("Psion &Drive"));

    QBoxLayout *box = new QVBoxLayout(d->frame, KDialog::spacingHint());
    QLabel *l;
    QGridLayout *gl;
    QPushButton *b;
    d->usedColor = QColor(219, 58, 197);
    d->freeColor = QColor(39, 56, 167);
    d->driveLetter = "";

    d->gb = new QGroupBox(i18n("Information"), d->frame, "driveinfo");
    box->addWidget(d->gb);

    gl = new QGridLayout(d->gb, 7, 4, 15);
    gl->addRowSpacing(0, 10);

    l = new QLabel(i18n("Type"), d->gb, "typeLabel");
    gl->addWidget(l, 1, 0);

    d->typeLabel = new QLabel(d->gb);
    gl->addWidget(d->typeLabel, 2, 0);
    QWhatsThis::add(d->typeLabel,
		    i18n("The type of the drive is shown here."));

    l = new QLabel(i18n("Total capacity"), d->gb, "capacityLabel");
    gl->addWidget (l, 1, 1);

    d->totalLabel = new QLabel(i18n(" "), d->gb, "capacityValue");
    gl->addWidget(d->totalLabel, 2, 1);
    QWhatsThis::add(d->totalLabel,
		    i18n("This shows the total capacity of the drive."));

    l = new QLabel(i18n("Free space"), d->gb, "spaceLabel");
    gl->addWidget (l, 1, 2);

    d->freeLabel = new QLabel(i18n(" "), d->gb, "spaceValue");
    gl->addWidget(d->freeLabel, 2, 2);
    QWhatsThis::add(d->freeLabel,
		    i18n("This shows the available space of the drive."));

    l = new QLabel(i18n("Unique ID"), d->gb, "uidLabel");
    gl->addWidget (l, 1, 3);

    d->uidLabel = new QLabel(i18n(" "), d->gb, "uidValue");
    gl->addWidget(d->uidLabel, 2, 3);
    QWhatsThis::add(d->freeLabel,
		    i18n("This shows unique ID of the drive. For ROM drives, this is always 0."));

    d->pie = new Pie3DWidget(d->gb, "pie");

    gl->addMultiCellWidget(d->pie, 3, 4, 1, 2);

    QHBoxLayout *blay = new QHBoxLayout(KDialog::spacingHint(), "buttons");
    gl->addMultiCellLayout(blay, 5, 6, 1, 3);
    blay->setAlignment(AlignRight | AlignVCenter);

    b  = new QPushButton(i18n("Backup"), d->gb, "backupButton");
    connect(b, SIGNAL(clicked()), SLOT(slotBackupClicked()));
    blay->addWidget(b, 0);
    QWhatsThis::add(b,
		    i18n("Click here, to do a backup of this drive. This launches KPsion to perform that task."));

    b = new QPushButton(i18n("Restore"), d->gb, "restoreButton");
    connect(b, SIGNAL(clicked()), SLOT(slotRestoreClicked()));
    blay->addWidget(b, 0);
    d->restoreButton = b;
    QWhatsThis::add(b,
		    i18n("Click here, to do a restore of this drive. This launches KPsion to perform that task."));

    b = new QPushButton(i18n("Format"), d->gb, "formatButton");
    connect(b, SIGNAL(clicked()), SLOT(slotFormatClicked()));
    blay->addWidget(b, 0);
    d->formatButton = b;
    QWhatsThis::add(b,
		    i18n("Click here, to format this drive. This launches KPsion to perform that task."));

    blay->addStretch(10);

    box->addStretch(10);

    KIO_ARGS << int(PLP_CMD_DRIVEINFO) << _props->item()->url().path();
    KIO::StatJob *job = new KIO::StatJob(KURL("psion:/"), KIO::CMD_SPECIAL,
					 packedArgs, false);
    connect(job, SIGNAL(result(KIO::Job *)),
	    SLOT(slotSpecialFinished(KIO::Job *)));

}

PlpDriveAttrPage::~PlpDriveAttrPage() {
    delete d;
}

bool PlpDriveAttrPage::supports(KFileItemList _items) {
    for (KFileItemListIterator it(_items); it.current(); ++it) {
	KFileItem *fi = it.current();

	QString path = fi->url().path(-1);
	if (path.contains('/') != 1)
	    return false;
	if (fi->url().path() == QString::fromLatin1("/"))
	    return false;
    }
    return true;
}

void PlpDriveAttrPage::slotBackupClicked() {
    if (!d->driveLetter.isEmpty())
	KRun::runCommand(
	    QString::fromLatin1("kpsion --backup %1").arg(d->driveLetter));
}

void PlpDriveAttrPage::slotRestoreClicked() {
    if (!d->driveLetter.isEmpty())
	KRun::runCommand(
	    QString::fromLatin1("kpsion --restore %1").arg(d->driveLetter));
}

void PlpDriveAttrPage::slotFormatClicked() {
    if (!d->driveLetter.isEmpty())
	KRun::runCommand(
	    QString::fromLatin1("kpsion --format %1").arg(d->driveLetter));
}

void PlpDriveAttrPage::slotSpecialFinished(KIO::Job *job) {
    KIO::StatJob *sJob = static_cast<KIO::StatJob *>(job);

    if (sJob->error())
	job->showErrorDialog(d->props->dialog());
    else {
	KIO::UDSEntry e = sJob->statResult();
	bool total_found = false;
	bool free_found = false;
	u_int32_t total;
	u_int32_t unused;

	for (KIO::UDSEntry::ConstIterator it = e.begin(); it != e.end(); ++it) {
	    if ((*it).m_uds == KIO::UDS_SIZE) {
		total_found = true;
		total = (unsigned long)((*it).m_long);
	    }
	    if ((*it).m_uds == KIO::UDS_MODIFICATION_TIME) {
		free_found = true;
		unused = (unsigned long)((*it).m_long);
	    }
	    if ((*it).m_uds == KIO::UDS_CREATION_TIME) {
		unsigned long uid = (unsigned long)((*it).m_long);
		d->uidLabel->setText(QString::fromLatin1("%1").arg(uid, 8, 16));
	    }
	    if ((*it).m_uds == KIO::UDS_NAME) {
		QString name = ((*it).m_str);
		d->typeLabel->setText(name);
		if (name == QString::fromLatin1("ROM")) {
		    d->restoreButton->setEnabled(false);
		    d->formatButton->setEnabled(false);
		    // Can't change anything
		    removeDialogPage(d->props->dialog(), i18n("&General"));
		}
	    }
	    if ((*it).m_uds == KIO::UDS_USER) {
		d->driveLetter = ((*it).m_str);
		d->gb->setTitle(QString(i18n("Information for Psion drive %1: (%2)")).arg(d->driveLetter).arg(d->props->item()->name()));
	    }
	}
	if (total_found && free_found) {
	    d->totalLabel->setText(QString::fromLatin1("%1 (%2)").arg(KIO::convertSize(total)).arg(KGlobal::locale()->formatNumber(total, 0)));
	    d->freeLabel->setText(QString::fromLatin1("%1 (%2)").arg(KIO::convertSize(unused)).arg(KGlobal::locale()->formatNumber(unused, 0)));
	    d->pie->addPiece(total - unused, d->usedColor);
	    d->pie->addPiece(unused, d->freeColor);
	}
    }
}

class PlpMachinePage::PlpMachinePagePrivate {
public:
    PlpMachinePagePrivate() { }
    ~PlpMachinePagePrivate() { }

    KPropertiesDialog *props;

    QFrame *f;
    QGridLayout *g;

    QLabel *machType;
    QLabel *machName;
    QLabel *machUID;
    QLabel *machLang;

    QLabel *romVersion;
    QLabel *romSize;
    QLabel *romProg;

    QLabel *ramSize;
    QLabel *ramFree;
    QLabel *ramMaxFree;
    QLabel *ramDiskSz;

    QLabel *regSize;
    QLabel *dispGeo;
    QLabel *machTime;
    QLabel *machUTCo;
    QLabel *machDST;

    QLabel *mbattChanged;
    QLabel *mbattUsage;
    QLabel *mbattStatus;
    QLabel *mbattPower;
    QLabel *mbattCurrent;
    QLabel *mbattVoltage;
    QLabel *mbattMaxVoltage;

    QLabel *bbattStatus;
    QLabel *bbattUsage;
    QLabel *bbattVoltage;
    QLabel *bbattMaxVoltage;

    rpcs::machineInfo mi;
};

QLabel *PlpMachinePage::
makeEntry(QString text, QWidget *w, int y) {
    QLabel *l = new QLabel(w);
    d->g->addWidget(new QLabel(text, w), y, 0);
    d->g->addWidget(l, y, 1);
    return l;
}

PlpMachinePage::PlpMachinePage( KPropertiesDialog *_props ) {
    d = new PlpMachinePagePrivate;
    d->props = _props;

    QGroupBox *gb;
    QBoxLayout *box;

    d->f = _props->dialog()->addPage(i18n("Psion &Machine"));

    box = new QVBoxLayout(d->f, KDialog::spacingHint());
    gb = new QGroupBox(i18n("General"), d->f, "genInfBox");
    box->addWidget(gb);
    d->g = new QGridLayout(gb, 1, 1, KDialog::marginHint(),
			   KDialog::spacingHint());

    d->machType = makeEntry(i18n("Machine type:"), gb, 1);
    QWhatsThis::add(d->machType,
		    i18n("Here, the type of the connected device is shown."));
    d->machName = makeEntry(i18n("Model name:"), gb, 2);
    QWhatsThis::add(d->machName,
		    i18n("Here, the model name of the connected device is shown."));
    d->machUID  = makeEntry(i18n("Machine UID:"), gb, 3);
    QWhatsThis::add(d->machUID,
		    i18n("Here, the unique ID of the connected device is shown."));
    d->machLang = makeEntry(i18n("UI language:"), gb, 4);
    QWhatsThis::add(d->machLang,
		    i18n("Here, the user interface language of the connected device is shown."));
    d->dispGeo  = makeEntry(i18n("Display geometry:"), gb, 5);
    QWhatsThis::add(d->dispGeo,
		    i18n("Here, the display geometry of the connected device is shown."));
    d->regSize  = makeEntry(i18n("Registry size:"), gb, 6);
    QWhatsThis::add(d->regSize,
		    i18n("Here, the size of the registry data is shown."));
    d->g->addRowSpacing(0, KDialog::marginHint());
    d->g->setColStretch(1, 1);

    gb = new QGroupBox(i18n("Time"), d->f, "timeInfBox");
    box->addWidget(gb);
    d->g = new QGridLayout(gb, 1, 1, KDialog::marginHint(),
			   KDialog::spacingHint());
    d->machTime = makeEntry(i18n("Date/Time:"), gb, 1);
    QWhatsThis::add(d->machTime,
		    i18n("Here, the current time setting of the connected device is shown."));
    d->machUTCo = makeEntry(i18n("UTC offset"), gb, 2);
    QWhatsThis::add(d->machUTCo,
		    i18n("Here, the offset of the connected device's time zone relative to GMT is shown."));
    d->machDST  = makeEntry(i18n("Daylight saving"), gb, 3);
    QWhatsThis::add(d->machDST,
		    i18n("Here, you can see, if daylight saving time is currently active on the connected device."));
    d->g->addRowSpacing(0, KDialog::marginHint());
    d->g->setColStretch(1, 1);
    box->addStretch(10);

    d->f = _props->dialog()->addPage(i18n("Psion &Battery"));
    box = new QVBoxLayout(d->f, KDialog::spacingHint());
    gb = new QGroupBox(i18n("Main battery"), d->f, "mbatInfBox");
    box->addWidget(gb);
    d->g = new QGridLayout(gb, 1, 1, KDialog::marginHint(),
			   KDialog::spacingHint());
    d->mbattChanged    = makeEntry(i18n("Changed at:"), gb, 1);
    QWhatsThis::add(d->mbattChanged,
		    i18n("This shows the time of last battery change."));
    d->mbattUsage      = makeEntry(i18n("Usage time:"), gb, 2);
    QWhatsThis::add(d->mbattUsage,
		    i18n("This shows the accumulated time of running on battery power."));
    d->mbattStatus     = makeEntry(i18n("Status:"), gb, 3);
    QWhatsThis::add(d->mbattStatus,
		    i18n("This shows current status of the battery."));
    d->mbattPower      = makeEntry(i18n("Total consumed power:"), gb, 4);
    QWhatsThis::add(d->mbattPower,
		    i18n("This shows accumulated power consumtion of the device."));
    d->mbattCurrent    = makeEntry(i18n("Current:"), gb, 5);
    QWhatsThis::add(d->mbattCurrent,
		    i18n("This shows the current, drawn from power supply (battery or mains)."));
    d->mbattVoltage    = makeEntry(i18n("Voltage:"), gb, 6);
    QWhatsThis::add(d->mbattVoltage,
		    i18n("This shows the current battery voltage."));
    d->mbattMaxVoltage = makeEntry(i18n("Max. voltage:"), gb, 7);
    QWhatsThis::add(d->mbattMaxVoltage,
		    i18n("This shows the maximum battery voltage."));
    d->g->addRowSpacing(0, KDialog::marginHint());
    d->g->setColStretch(1, 1);

    gb = new QGroupBox(i18n("Backup battery"), d->f, "bbatInfBox");
    box->addWidget(gb);
    d->g = new QGridLayout(gb, 1, 1, KDialog::marginHint(),
			   KDialog::spacingHint());
    d->bbattUsage      = makeEntry(i18n("Usage time:"), gb, 1);
    QWhatsThis::add(d->bbattUsage,
		    i18n("This shows the accumulated time of running on backup battery power."));
    d->bbattStatus     = makeEntry(i18n("Status:"), gb, 2);
    QWhatsThis::add(d->bbattStatus,
		    i18n("This shows current status of the backup battery."));
    d->bbattVoltage    = makeEntry(i18n("Voltage:"), gb, 3);
    QWhatsThis::add(d->bbattVoltage,
		    i18n("This shows the current backup battery voltage."));
    d->bbattMaxVoltage = makeEntry(i18n("Max. voltage:"), gb, 4);
    QWhatsThis::add(d->bbattMaxVoltage,
		    i18n("This shows the maximum backup battery voltage."));
    d->g->addRowSpacing(0, KDialog::marginHint());
    d->g->setColStretch(1, 1);
    box->addStretch(10);

    d->f = _props->dialog()->addPage(i18n("Psion M&emory"));

    box = new QVBoxLayout(d->f, KDialog::spacingHint());
    gb = new QGroupBox(i18n("ROM"), d->f, "romInfBox");
    box->addWidget(gb);
    d->g = new QGridLayout(gb, 1, 1, KDialog::marginHint(),
			   KDialog::spacingHint());
    d->romVersion = makeEntry(i18n("Version:"), gb, 1);
    QWhatsThis::add(d->romVersion,
		    i18n("This shows the firmware version."));
    d->romSize    = makeEntry(i18n("Size:"), gb, 2);
    QWhatsThis::add(d->romSize,
		    i18n("This shows the size of the ROM."));
    d->romProg    = makeEntry(i18n("Programmable:"), gb, 3);
    QWhatsThis::add(d->romProg,
		    i18n("This shows, whether the ROM is flashable or not."));
    d->g->addRowSpacing(0, KDialog::marginHint());
    d->g->setColStretch(1, 1);

    gb = new QGroupBox(i18n("RAM"), d->f, "ramInfBox");
    box->addWidget(gb);
    d->g = new QGridLayout(gb, 1, 1, KDialog::marginHint(),
			   KDialog::spacingHint());
    d->ramSize    = makeEntry(i18n("Size:"), gb, 1);
    QWhatsThis::add(d->ramSize,
		    i18n("This shows the total capacity of the RAM."));
    d->ramFree    = makeEntry(i18n("Free:"), gb, 2);
    QWhatsThis::add(d->ramFree,
		    i18n("This shows the free capacity of the RAM."));
    d->ramMaxFree = makeEntry(i18n("Max. free:"), gb, 3);
    QWhatsThis::add(d->ramMaxFree,
		    i18n("This shows the size of the largest free block of the RAM."));
    d->ramDiskSz  = makeEntry(i18n("RAMDisk size:"), gb, 4);
    QWhatsThis::add(d->ramMaxFree,
		    i18n("This shows, how much RAM is currently used for the RAMDisc."));
    d->g->addRowSpacing(0, KDialog::marginHint());
    d->g->setColStretch(1, 1);
    box->addStretch(10);

    KIO_ARGS << int(PLP_CMD_MACHINFO);
    PlpMachInfoJob *job = new PlpMachInfoJob(packedArgs);
    connect(job, SIGNAL(result(KIO::Job *)),
	    SLOT(slotJobFinished(KIO::Job *)));
    connect(job, SIGNAL(data(KIO::Job *, const QByteArray &)),
	    SLOT(slotJobData(KIO::Job *, const QByteArray &)));
}

PlpMachinePage::~PlpMachinePage() {
    delete d;
}

bool PlpMachinePage::supports(KFileItemList _items) {
    for (KFileItemListIterator it(_items); it.current(); ++it) {
	KFileItem *fi = it.current();

	if (fi->url().path() != QString::fromLatin1("/"))
	    return false;
    }
    return true;
}

void PlpMachinePage::slotJobData(KIO::Job *job, const QByteArray &data) {
    cout << "Mjdata" << endl;
    if (data.size() == sizeof(d->mi)) {
	memcpy((char *)&d->mi, data, sizeof(d->mi));
	cout << "got machInfo" << endl;

	d->machType->setText(KGlobal::locale()->translate(d->mi.machineType));
	d->machName->setText(QString::fromLatin1(d->mi.machineName));
	// ??! None of QString's formatting methods knows about long long.
	ostrstream s;
	s << hex << setw(16) << d->mi.machineUID << '\0';
	d->machUID->setText(QString::fromLatin1(s.str()));
	d->machLang->setText(KGlobal::locale()->translate(d->mi.uiLanguage));
	d->dispGeo->setText(QString::fromLatin1("%1x%2").arg(d->mi.displayWidth).arg(d->mi.displayHeight));
	d->regSize->setText(QString::fromLatin1("%1 (%2)").arg(KIO::convertSize(d->mi.registrySize)).arg(KGlobal::locale()->formatNumber(d->mi.registrySize, 0)));

	QString rev;
	rev.sprintf("%d.%02d(%d)", d->mi.romMajor, d->mi.romMinor,
		    d->mi.romBuild);
	d->romVersion->setText(rev);
	d->romSize->setText(QString::fromLatin1("%1 (%2)").arg(KIO::convertSize(d->mi.romSize)).arg(KGlobal::locale()->formatNumber(d->mi.romSize, 0)));
	d->romProg->setText(d->mi.romProgrammable ? i18n("yes") : i18n("no"));

	d->ramSize->setText(QString::fromLatin1("%1 (%2)").arg(KIO::convertSize(d->mi.ramSize)).arg(KGlobal::locale()->formatNumber(d->mi.ramSize, 0)));
	d->ramFree->setText(QString::fromLatin1("%1 (%2)").arg(KIO::convertSize(d->mi.ramFree)).arg(KGlobal::locale()->formatNumber(d->mi.ramFree, 0)));
	d->ramMaxFree->setText(QString::fromLatin1("%1 (%2)").arg(KIO::convertSize(d->mi.ramMaxFree)).arg(KGlobal::locale()->formatNumber(d->mi.ramMaxFree, 0)));
	d->ramDiskSz->setText(QString::fromLatin1("%1 (%2)").arg(KIO::convertSize(d->mi.ramDiskSize)).arg(KGlobal::locale()->formatNumber(d->mi.ramDiskSize, 0)));


	PsiTime pt(&d->mi.time, &d->mi.tz);
	QDateTime dt;
	dt.setTime_t(pt.getTime());
	d->machTime->setText(KGlobal::locale()->formatDateTime(dt, false));
	d->machUTCo->setText(i18n("%1 seconds").arg(d->mi.tz.utc_offset));
	d->machDST->setText((d->mi.tz.dst_zones & PsiTime::PSI_TZ_HOME) ? i18n("yes") : i18n("no"));

	ostrstream mbs;
	mbs << d->mi.mainBatteryUsedTime << '\0';
	d->mbattUsage->setText(QString::fromLatin1(mbs.str()));
	pt.setPsiTime(&d->mi.mainBatteryInsertionTime);
	dt.setTime_t(pt.getTime());
	d->mbattChanged->setText(KGlobal::locale()->formatDateTime(dt, false));
	d->mbattStatus->setText(
	    KGlobal::locale()->translate(d->mi.mainBatteryStatus));
	d->mbattPower->setText(QString::fromLatin1("%1 mAs").arg(KGlobal::locale()->formatNumber(d->mi.mainBatteryUsedPower, 0)));
	d->mbattCurrent->setText(QString::fromLatin1("%1 mA").arg(KGlobal::locale()->formatNumber(d->mi.mainBatteryCurrent, 0)));
	d->mbattVoltage->setText(QString::fromLatin1("%1 mV").arg(KGlobal::locale()->formatNumber(d->mi.mainBatteryVoltage, 0)));
	d->mbattMaxVoltage->setText(QString::fromLatin1("%1 mV").arg(KGlobal::locale()->formatNumber(d->mi.mainBatteryMaxVoltage, 0)));

	ostrstream bbs;
	bbs << d->mi.backupBatteryUsedTime << '\0';
	d->bbattUsage->setText(QString::fromLatin1(bbs.str()));
	d->bbattStatus->setText(
	    KGlobal::locale()->translate(d->mi.backupBatteryStatus));
	d->bbattVoltage->setText(QString::fromLatin1("%1 mV").arg(KGlobal::locale()->formatNumber(d->mi.backupBatteryVoltage, 0)));
	d->bbattMaxVoltage->setText(QString::fromLatin1("%1 mV").arg(KGlobal::locale()->formatNumber(d->mi.backupBatteryMaxVoltage, 0)));
    }
}

void PlpMachinePage::slotJobFinished(KIO::Job *job) {
    PlpMachInfoJob *mJob = static_cast<PlpMachInfoJob *>(job);

    if (mJob->error())
	job->showErrorDialog(d->props->dialog());
}

class PlpOwnerPage::PlpOwnerPagePrivate
{
public:
    PlpOwnerPagePrivate()	{ }
    ~PlpOwnerPagePrivate() { }

    QFrame *frame;
    KPropertiesDialog *props;
    QMultiLineEdit *owneredit;
};

PlpOwnerPage::PlpOwnerPage( KPropertiesDialog *_props ) {
    d = new PlpOwnerPagePrivate;
    d->props = _props;
    d->frame = _props->dialog()->addPage(i18n("Psion &Owner"));
    QBoxLayout *box = new QVBoxLayout( d->frame, KDialog::spacingHint() );
    d->owneredit = new QMultiLineEdit(d->frame, "ownerinfo");
    d->owneredit->setReadOnly(true);
    box->addWidget(d->owneredit);
    QWhatsThis::add(d->owneredit,
		    i18n("This shows the owner's information of the connected device."));

    KIO_ARGS << int(PLP_CMD_OWNERINFO);
    KIO::StatJob *job = new KIO::StatJob(KURL("psion:/"),
					 KIO::CMD_SPECIAL, packedArgs, false);
    connect(job, SIGNAL(result(KIO::Job *)),
	    SLOT(slotSpecialFinished(KIO::Job *)));

    box->addStretch(10);
}

PlpOwnerPage::~PlpOwnerPage() {
    delete d;
}

bool PlpOwnerPage::supports(KFileItemList _items) {
    for (KFileItemListIterator it(_items); it.current(); ++it) {
	KFileItem *fi = it.current();

	if (fi->url().path() != QString::fromLatin1("/"))
	    return false;
    }
    return true;
}

void PlpOwnerPage::slotSpecialFinished(KIO::Job *job) {
    KIO::StatJob *sJob = static_cast<KIO::StatJob *>(job);

    if (sJob->error())
	job->showErrorDialog(d->props->dialog());
    else {
	KIO::UDSEntry e = sJob->statResult();
	for (KIO::UDSEntry::ConstIterator it = e.begin();
	     it != e.end(); ++it) {
	    if ((*it).m_uds == KIO::UDS_NAME)
		d->owneredit->setText((*it).m_str);
	}
    }
}

PlpMachInfoJob::PlpMachInfoJob(const QByteArray &packedArgs)
    : KIO::TransferJob(KURL("psion:/"), KIO::CMD_SPECIAL,
		       packedArgs, QByteArray(), false) {
}

void PlpMachInfoJob::
slotFinished() {
    SimpleJob::slotFinished();
}

#include "plpprops.moc"
/*
 * Local variables:
 * c-basic-offset: 4
 * End:
 */