summaryrefslogtreecommitdiffstats
path: root/kpilot/conduitConfigDialog.cc
blob: f2a3d5485aa77404c1c23e7cd327316ec96bffca (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
/* KPilot
**
** Copyright (C) 2001 by Dan Pilone
** Copyright (C) 2002-2004 by Adriaan de Groot
** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
**
** This file defines a .ui-based configuration dialog for conduits.
*/

/*
** 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 in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to kde-pim@kde.org
*/

#include "options.h"

#include <tqlistview.h>
#include <tqlabel.h>
#include <tqtooltip.h>
#include <tqfile.h>
#include <tqpushbutton.h>
#include <tqhbox.h>
#include <tqlayout.h>
#include <tqwidgetstack.h>
#include <tqvbox.h>
#include <tqsplitter.h>
#include <tqheader.h>
#include <tqtimer.h>

#include <kservice.h>
#include <kservicetype.h>
#include <kuserprofile.h>
#include <kprocess.h>
#include <kmessagebox.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <klibloader.h>
#include <kseparator.h>
#include <kconfigskeleton.h>
#include <kdialogbase.h>

#include "plugin.h"
#include "kpilotConfig.h"
#include "kpilotConfigDialog.h"

#include "kpilotConfigWizard.h"

#include "conduitConfigDialog.moc"

#define CONDUIT_NAME    (0)
#define CONDUIT_COMMENT (1)
#define CONDUIT_DESKTOP (2)
#define CONDUIT_LIBRARY (3)
#define CONDUIT_ORDER	(4)


extern "C"
{
	KDE_EXPORT KCModule *create_kpilotconfig( TQWidget *tqparent, const char * )
	{
		FUNCTIONSETUP;
		return new ConduitConfigWidget( tqparent, "kcmkpilotconfig" );
	}

	KDE_EXPORT ConfigWizard *create_wizard(TQWidget *tqparent, int m)
	{
		FUNCTIONSETUP;
		return new ConfigWizard(tqparent,"Wizard", m);
	}
}


class ConduitTip : public TQToolTip
{
public:
	ConduitTip(TQListView *tqparent);
	virtual ~ConduitTip();

protected:
	virtual void maybeTip(const TQPoint &);

	TQListView *fListView;
} ;


ConduitTip::ConduitTip(TQListView *p) :
	TQToolTip(p->viewport(),0L),
	fListView(p)
{
	FUNCTIONSETUP;
}

ConduitTip::~ConduitTip()
{
	FUNCTIONSETUP;
}

/* virtual */ void ConduitTip::maybeTip(const TQPoint &p)
{
	FUNCTIONSETUP;

	TQListViewItem *l = fListView->itemAt(p);

	if (!l) return;

	// ConduitListItem *q = static_cast<ConduitListItem *>(l);

#ifdef DEBUG
	DEBUGKPILOT << fname
		<< ": Tip over "
		<< l->text(CONDUIT_NAME)
		<< " with text "
		<< l->text(CONDUIT_COMMENT)
		<< endl;
#endif

	TQString s = l->text(CONDUIT_COMMENT);

	if (s.isEmpty()) return;
	if (s.find(CSL1("<qt>"),0,false) == -1)
	{
		s.prepend(CSL1("<qt>"));
		s.append(CSL1("</qt>"));
	}

	tip(fListView->tqitemRect(l),s);
}

// implement our own check list items so we can detect if a given item was checked/unchecked. We need
// this to prevent the modified signal if one only wants to display a conduit's config widget. Currently,
// KListView doesn't provide any signal that indicates that the checked state of a checklist item was changed.
class KPilotCheckListItem : public TQCheckListItem
{
public:
	KPilotCheckListItem ( TQListViewItem * tqparent, const TQString & text, Type tt = RadioButtonController ) : TQCheckListItem(tqparent, text, tt),mOriginalState(false) {}
	~KPilotCheckListItem() {}

	void setOriginalState(bool state) { mOriginalState=state; setOn(state);}
	bool isOriginalState() { return isOn() == mOriginalState; }

protected:
	bool mOriginalState;
};


// Page numbers in the widget stack
#define OLD_CONDUIT      (1)
#define BROKEN_CONDUIT   (2)
#define INTERNAL_CONDUIT (3)
#define INTERNAL_EXPLN   (4)
#define CONDUIT_EXPLN    (5)
#define GENERAL_EXPLN    (6)
#define GENERAL_ABOUT    (7)
#define NEW_CONDUIT      (8)


/*
** Create a page in the widget stack @p tqparent on page @p pageno,
** bearing the given @p text. The remainder of the parameters are
** for esoteric things like:
**  @p buttons set to non-null to include (and return) a TQHBox suitable
**     for displaying a row of buttons in on the page.
**  @p label set to non-null to return the TQLabel used to display @p text.
*/
static void addDescriptionPage(TQWidgetStack *tqparent,
	int pageno,
	const TQString &text,
	TQHBox **buttons = 0L,
	TQLabel **label = 0L)
{
	TQVBox *v = new TQVBox(tqparent);
	TQLabel *l = 0L;

	v->setFrameShape(TQLabel::NoFrame);
	v->setMargin(SPACING);

	l = new TQLabel(v);
	l->setText(text);
	l->tqsetAlignment(TQt::AlignLeft | TQt::AlignVCenter | TQt::ExpandTabs | TQt::WordBreak);

	if (label) { *label = l; }

	if (buttons)
	{
		*buttons = new TQHBox(v);
		l = new TQLabel(v);
	}

	tqparent->addWidget(v,pageno);
}


ConduitConfigWidgetBase::ConduitConfigWidgetBase(TQWidget *tqparent, const char *n) :
	KCModule(tqparent, n),
	fConduitList(0L),
	fStack(0L),
	fConfigureButton(0L),
	fConfigureWizard(0L),
	fConfigureKontact(0L),
	fActionDescription(0L)
{
	FUNCTIONSETUP;

	TQWidget *w = 0L; // For spacing purposes only.
	TQHBox *btns = 0L;

	TQHBoxLayout *mainLayout = new TQHBoxLayout(this);
	mainLayout->setSpacing(10);

	// Create the left hand column
	fConduitList = new TQListView(this ,"ConduitList");
	fConduitList->addColumn(TQString());
	fConduitList->header()->hide();
	fConduitList->tqsetSizePolicy(
		TQSizePolicy(TQSizePolicy::Maximum, TQSizePolicy::Preferred));
	mainLayout->addWidget(fConduitList);

	// Create the title
	TQVBoxLayout *vbox = new TQVBoxLayout(this, 0, KDialog::spacingHint());
	// String below is just to make space; no need to translate.
	fTitleText = new TQLabel(CSL1("Conduit Setup - Addressbook"), this);
	TQFont titleFont(fTitleText->font());
	titleFont.setBold(true);
	fTitleText->setFont(titleFont);
	vbox->addWidget(fTitleText, 0, AlignLeft);
	vbox->addWidget(new KSeparator(TQFrame::HLine|TQFrame::Plain, this));

	// Right hand column
	fStack = new TQWidgetStack(this, "RightPart");
	vbox->addWidget(fStack, 10);

	mainLayout->addLayout(vbox);

	// First page in stack (right hand column)
	addDescriptionPage(fStack,BROKEN_CONDUIT,
		i18n("<qt>This conduit appears to be broken and cannot "
		"be configured.</qt>"));

	// Second page, now with tqlayout in a single column
	//
	// Probably deprecated.
	//
	addDescriptionPage(fStack,OLD_CONDUIT,
		i18n("<qt>This is an old-style conduit.</qt>"),&btns);
	w = new TQWidget(btns);
	btns->setStretchFactor(w,50);
	fConfigureButton = new TQPushButton(btns);
	fConfigureButton->setText(i18n("Configure..."));
	w = new TQWidget(btns);
	btns->setStretchFactor(w,50);

	// Page 3
	addDescriptionPage(fStack,INTERNAL_CONDUIT,
		TQString(),0L,&fActionDescription);

	// Page 5 - explanation about conduits
	addDescriptionPage(fStack,CONDUIT_EXPLN,
		i18n("<qt><i>Conduits</i> are external (possibly third-party) "
		"programs that perform synchronization actions. They may "
		"have individual configurations. Select a conduit to configure it, "
		"and enable it by clicking on its checkbox. "
		"</qt>"));

	// Page 6 - explanation about general setup
	addDescriptionPage(fStack,GENERAL_EXPLN,
		i18n("<qt><p>The <i>general</i> portion of KPilot's setup "
		"contains settings for your hardware and the way KPilot "
		"should display your data. For the basic setup, which should fulfill "
		"the need of most users, just use the setup wizard below.</p>"
		"If you need some special settings, this dialog provides all the options "
		"for fine-tuning KPilot. But be warned: The HotSync settings are "
		"various esoteric things.</p>"
		"<p>You can enable an action or conduit by clicking on its checkbox. "
		"Checked conduits will be run during a HotSync. "
		"Select a conduit to configure it.</p>"
		"</qt>"),&btns);
	w = new TQWidget(btns);
	btns->setStretchFactor(w,50);
	fConfigureWizard = new TQPushButton(i18n("Configuration Wizard"),btns);
	w = new TQWidget(btns);
	btns->setStretchFactor(w,50);


	fStack->addWidget(ConduitConfigBase::aboutPage(fStack,0L),GENERAL_ABOUT);
}

ConduitConfigWidget::ConduitConfigWidget(TQWidget *tqparent, const char *n,
	bool) :
	ConduitConfigWidgetBase(tqparent,n),
	fConfigure(0L),
	fCurrentConduit(0L),
	fGeneralPage(0L),
	fCurrentConfig(0L)
{
	FUNCTIONSETUP;

	fConduitList->setSorting(-1);
	fConduitList->setRootIsDecorated(true);
	fConduitList->setTreeStepSize(10);
	// fConduitList->removeColumn(CONDUIT_COMMENT);
	fillLists();

	fConduitList->resize(fConduitList->tqsizeHint());
	fConduitList->setMinimumSize(fConduitList->tqsizeHint());
	fConduitList->setColumnWidth(0, fConduitList->tqsizeHint().width());
	fConduitList->setResizeMode(TQListView::AllColumns);

	fStack->resize(fStack->tqsizeHint()+TQSize(10,40));
	fStack->setMinimumSize(fStack->tqsizeHint()+TQSize(10,40));

	TQObject::connect(fConduitList,
		TQT_SIGNAL(selectionChanged(TQListViewItem *)),
		this,TQT_SLOT(selected(TQListViewItem *)));
	TQObject::connect(fConduitList,
		TQT_SIGNAL(clicked(TQListViewItem*)),
		this, TQT_SLOT(conduitsChanged(TQListViewItem*)));

	// Deprecated?
	TQObject::connect(fConfigureButton,
		TQT_SIGNAL(clicked()),
		this,TQT_SLOT(configure()));

	TQObject::connect(fConfigureWizard,TQT_SIGNAL(clicked()),
		this,TQT_SLOT(configureWizard()));

	fGeneralPage->setSelected(true);
	fConduitList->setCurrentItem(fGeneralPage);
	selected(fGeneralPage);

	(void) new ConduitTip(fConduitList);
	setButtons(Apply);

}

ConduitConfigWidget::~ConduitConfigWidget()
{
	FUNCTIONSETUP;
	release();
}

void ConduitConfigWidget::fillLists()
{
	FUNCTIONSETUP;

	// 3 TQListViewItems for the three headings in the list
	TQListViewItem *general,*conduits;

	// And two generic pointers for the rest.
	TQListViewItem *q = 0L;
	KPilotCheckListItem *p = 0L;

	q = new TQListViewItem(fConduitList, i18n("About"));
	q->setText(CONDUIT_COMMENT, i18n("About KPilot. Credits."));
	q->setText(CONDUIT_LIBRARY, CSL1("general_about"));

	conduits = new TQListViewItem(fConduitList, i18n("Conduits"));

	general = new TQListViewItem( fConduitList, i18n("General Setup" ) );
	fGeneralPage = general;

	// Give them identifiers so they can be handled specially when selected.
	conduits->setText(CONDUIT_LIBRARY,CSL1("expln_conduits"));
	general->setText( CONDUIT_LIBRARY, CSL1("expln_general") );

	general->setText( CONDUIT_COMMENT,
		i18n("General setup of KPilot (User name, port, general sync settings)") );
	conduits->setText( CONDUIT_COMMENT,
		i18n("Actions for HotSync with individual configuration."));

	conduits->setOpen(true);
	general->setOpen(true);


	// Create entries under general.
#define CE(a,b,c) q = new TQListViewItem(general,a) ; \
	q->setText(CONDUIT_COMMENT,b) ; \
	q->setText(CONDUIT_LIBRARY,c) ;

	CE(i18n("Startup and Exit"), i18n("Behavior at startup and exit."), CSL1("general_startexit") );
	CE(i18n("Viewers"), i18n("Viewer settings."), CSL1("general_view") );
	CE(i18n("Backup"),i18n("Special settings for backup."),CSL1("general_backup"));
	CE(i18n("HotSync"),i18n("Special behavior during HotSync."),CSL1("general_sync"));
	CE(i18n("Device"),i18n("Hardware settings and startup and exit options."),CSL1("general_setup"));

#undef CE


	// List of installed (enabled) actions and conduits.
	TQStringList potentiallyInstalled = KPilotSettings::installedConduits();

	//  Create internal conduits.
	//
	//

#define IC(a,b,c) p = new KPilotCheckListItem(conduits,i18n(a),TQCheckListItem::CheckBox); \
	p->setText(CONDUIT_COMMENT,i18n(c)); \
	p->setText(CONDUIT_LIBRARY,CSL1("internal_" b)); \
	p->setText(CONDUIT_DESKTOP,CSL1("internal_" b)); \
	if (potentiallyInstalled.findIndex(p->text(CONDUIT_DESKTOP))>=0) \
		p->setOriginalState(true);

	IC("Install Files","fileinstall",
		"Install files that are dragged to KPilot onto the handheld.");
#undef IC



	KServiceTypeProfile::OfferList offers =
		KServiceTypeProfile::offers(CSL1("KPilotConduit"));

	TQValueListIterator < KServiceOffer > availList(offers.begin());
	while (availList != offers.end())
	{
		KSharedPtr < KService > o = (*availList).service();

#ifdef DEBUG
		DEBUGKPILOT << fname << ": "
			<< o->desktopEntryName()
			<< " = " << o->name() << endl;
#endif

		if (!o->exec().isEmpty())
		{
			WARNINGKPILOT << "Old-style conduit found "
				<< o->name()
				<< endl;
		}

		p = new KPilotCheckListItem(conduits,
			o->name(),
			TQCheckListItem::CheckBox);
		p->setMultiLinesEnabled(true);
		p->setText(CONDUIT_COMMENT,o->comment());
		p->setText(CONDUIT_DESKTOP,o->desktopEntryName());
		p->setText(CONDUIT_LIBRARY,o->library());

		if (potentiallyInstalled.findIndex(o->desktopEntryName()) < 0)
		{
			p->setOriginalState(false);
		}
		else
		{
			p->setOriginalState(true);
		}

		++availList;
	}
}

static void dumpConduitInfo(const KLibrary *lib)
{
#ifdef DEBUG
	FUNCTIONSETUP;
	DEBUGKPILOT << "Plugin version = " << PluginUtility::pluginVersion(lib) << endl;
	DEBUGKPILOT << "Plugin id      = " << PluginUtility::pluginVersionString(lib) << endl;
#endif
}

static ConduitConfigBase *handleGeneralPages(TQWidget *w, TQListViewItem *p)
{
	ConduitConfigBase *o = 0L;

	TQString s = p->text(CONDUIT_LIBRARY) ;

	if (s.startsWith(CSL1("general_setup")))
	{
		o = new DeviceConfigPage( w, "generalSetup" );
	}
	else if (s.startsWith(CSL1("general_sync")))
	{
		o = new SyncConfigPage( w, "syncSetup" );
	}
	else if (s.startsWith(CSL1("general_view")))
	{
		o = new ViewersConfigPage( w, "viewSetup" );
	}
	else if (s.startsWith(CSL1("general_startexit")))
	{
		o = new StartExitConfigPage(w,"startSetup");
	}
	else if (s.startsWith(CSL1("general_backup")))
	{
		o = new BackupConfigPage(w,"backupSetup");
	}

	return o;
}

void ConduitConfigWidget::loadAndConfigure(TQListViewItem *p) // ,bool exec)
{
	FUNCTIONSETUP;

	if (!p)
	{
#ifdef DEBUG
		DEBUGKPILOT << fname
			<< ": Executed NULL conduit?"
			<< endl;
#endif
		fStack->raiseWidget(GENERAL_EXPLN);
		return;
	}

	TQString libraryName = p->text(CONDUIT_LIBRARY);

#ifdef DEBUG
	DEBUGKPILOT << fname
		<< ": Executing conduit "
		<< p->text(CONDUIT_NAME)
		<< " (library " << libraryName << ")"
		<< endl;
#endif


	if (libraryName.isEmpty())
	{
		fStack->raiseWidget(BROKEN_CONDUIT);
		warnNoExec(p);
		return;
	}

	if (libraryName.startsWith(CSL1("internal_")))
	{
		fStack->raiseWidget(INTERNAL_CONDUIT);
		fActionDescription->setText(
			i18n("<qt>This is an internal action which has no "
			"configuration options. "
			"The action's description is: <i>%1</i> "
			"</qt>").tqarg(p->text(CONDUIT_COMMENT)));
		return;
	}

	if (libraryName == CSL1("expln_conduits"))
	{
		fStack->raiseWidget(CONDUIT_EXPLN);
		return;
	}
	if (libraryName == CSL1("expln_general"))
	{
		fStack->raiseWidget(GENERAL_EXPLN);
		return;
	}

	if (libraryName == CSL1("general_about"))
	{
		fStack->raiseWidget(GENERAL_ABOUT);
		return;
	}

	TQObject *o = 0L;

	// Page 4: General setup
	if (libraryName.startsWith(CSL1("general_")))
	{
		o = handleGeneralPages(fStack,p);
	}
	else
	{
		TQCString library = TQFile::encodeName(libraryName);

		KLibFactory *f = KLibLoader::self()->factory(library);
		if (!f)
		{
#ifdef DEBUG
			DEBUGKPILOT << fname
				<< ": No conduit library "
				<< library.data()
				<< " [" << library.size() << "]"
				<< " (" << libraryName << ")"
				<< " found."
				<< endl;
#endif
			fStack->raiseWidget(BROKEN_CONDUIT);
			warnNoLibrary(p);
			return;
		}

		dumpConduitInfo(KLibLoader::self()->library(library));

		TQStringList a;
		a.append(CSL1("modal"));

		o = f->create(TQT_TQOBJECT(fStack), 0L, "ConduitConfigBase", a);

		if (!o)
		{
#ifdef DEBUG
			DEBUGKPILOT << fname
				<< ": Can't create ConduitConfigBase - must be old conduit."
				<< endl;
#endif

			KLibLoader::self()->unloadLibrary(
				library);
			fStack->raiseWidget(BROKEN_CONDUIT);
			warnNoLibrary(p);
			return;
		}
	}

	ConduitConfigBase *d = dynamic_cast<ConduitConfigBase *>(o);

	if (!d)
	{
#ifdef DEBUG
		DEBUGKPILOT << fname
			<< ": Can't cast to config base object."
			<< endl;
#endif
		fStack->raiseWidget(BROKEN_CONDUIT);
		warnNoLibrary(p);
		return;
	}

	// Remove the config widget from the stack before we can add the new one
	TQWidget *oldConfigWidget = fStack->widget( NEW_CONDUIT );
	if ( oldConfigWidget )
	{
		fStack->removeWidget( oldConfigWidget );
		KPILOT_DELETE( oldConfigWidget );
	}
	if (fStack->addWidget(d->widget(),NEW_CONDUIT)<0)
	{
#ifdef DEBUG
		DEBUGKPILOT << fname
			<< ": Can't add config widget to stack."
			<< endl;
#endif
	}
	else
	{
		d->load();
		fStack->raiseWidget(NEW_CONDUIT);
		d->widget()->show();
		fCurrentConfig=d;
		// make sure the changed signal is propagated to the KCM*Dialog
		// and the apply button is enabled correspondingly.
		connect(d, TQT_SIGNAL(changed(bool)), this, TQT_SIGNAL(changed(bool)));
	}
}

bool ConduitConfigWidget::release()
{
	FUNCTIONSETUP;
	if (fCurrentConfig)
	{
		if (!fCurrentConfig->maybeSave())
			return false;
		fStack->raiseWidget(0);
		delete fCurrentConfig;
	}
	if (fCurrentConduit)
	{
		KLibLoader::self()->unloadLibrary(
			TQFile::encodeName(fCurrentConduit->text(CONDUIT_LIBRARY)));
	}
	fCurrentConduit=0L;
	fCurrentConfig=0L;
	return true;
}

void ConduitConfigWidget::unselect()
{
	fConduitList->setSelected( fCurrentConduit, true );
	fConduitList->setCurrentItem( fCurrentConduit );
}

void ConduitConfigWidget::selected(TQListViewItem *p)
{
	FUNCTIONSETUP;
#ifdef DEBUG
	DEBUGKPILOT << fname << ": "
		<< ( p ? p->text(CONDUIT_NAME) : CSL1("None") )
		<< endl;
#endif
	if (p!=fCurrentConduit)
	{
		if (!release())
		{
			fConduitList->blockSignals(true);
			TQTimer::singleShot(1,this,TQT_SLOT(unselect()));
			return;
		}
	}
	fCurrentConduit=p;
	loadAndConfigure(p);
//	fStack->adjustSize();
#ifdef DEBUG
	DEBUGKPILOT << fname << ": New widget size "
		<< fStack->size().width() << "x" << fStack->size().height() << endl;
	DEBUGKPILOT << fname << ": Current size "
		<< size().width() << "x"
		<< size().height() << endl;
#endif
	emit sizeChanged();
#ifdef DEBUG
	DEBUGKPILOT << fname << ": New size "
		<< size().width() << "x"
		<< size().height() << endl;
#endif

	// set the dialog title to the selected item
	TQListViewItem *pParent = p->tqparent();
	TQString title;
	title = pParent ? pParent->text(CONDUIT_NAME) + CSL1(" - ") : TQString() ;
	title += p ? p->text(CONDUIT_NAME) : i18n("KPilot Setup");
	fTitleText->setText(title);
}

void ConduitConfigWidget::configure()
{
	loadAndConfigure(fConduitList->selectedItem());
}

void ConduitConfigWidget::warnNoExec(const TQListViewItem * p)
{
	FUNCTIONSETUP;

	TQString msg = i18n("<qt>No library could be "
		"found for the conduit %1. This means that the "
		"conduit was not installed properly.</qt>")
		.tqarg(p->text(CONDUIT_NAME));

#ifdef DEBUG
	DEBUGKPILOT << fname << ": No library for "
		<< p->text(CONDUIT_NAME) << endl;
#endif

	KMessageBox::error(this, msg, i18n("Conduit Error"));
}

void ConduitConfigWidget::warnNoLibrary(const TQListViewItem *p)
{
	FUNCTIONSETUP;

	TQString msg = i18n("<qt>There was a problem loading the library "
		"for the conduit %1. This means that the "
		"conduit was not installed properly.</qt>")
		.tqarg(p->text(CONDUIT_NAME));

#ifdef DEBUG
	DEBUGKPILOT << fname << ": Can't load library for "
		<< p->text(CONDUIT_NAME) << endl;
#endif

	KMessageBox::error(this, msg, i18n("Conduit Error"));
}

void ConduitConfigWidget::save()
{
	FUNCTIONSETUP;

	// Only new-style conduits and the general setup have changes that need to be commited
	// old-style conduits have their own config dlg which commits them itself
	if ( fStack->id( fStack->visibleWidget())==NEW_CONDUIT )
	{
		if (fCurrentConfig) fCurrentConfig->commit();
	}

	TQStringList activeConduits;
	TQListViewItemIterator it( fConduitList );
	while ( it.current() ) {
		KPilotCheckListItem*p = dynamic_cast<KPilotCheckListItem*>(it.current());
		if ( p )
		{
			p->setOriginalState( p->isOn() );
			if ( p->isOn() )
			activeConduits.append(p->text(CONDUIT_DESKTOP));
		}
		++it;
	}
	KPilotSettings::setInstalledConduits(activeConduits);
	KPilotSettings::self()->writeConfig();
}


void ConduitConfigWidget::load()
{
	FUNCTIONSETUP;
	KPilotSettings::self()->readConfig();

	TQStringList potentiallyInstalled = KPilotSettings::installedConduits();
	TQListViewItem*p = fConduitList->firstChild();
	while (p)
	{
		TQListViewItem*q = p->firstChild();
		while (q)
		{
			TQCheckListItem*qq=dynamic_cast<TQCheckListItem*>(q);
			if (qq)
			{
				qq->setOn(! (potentiallyInstalled.findIndex(qq->text(CONDUIT_DESKTOP))<0) );
			}
			q = q->nextSibling();
		}
		p=p->nextSibling();
	}


	// Only new-style conduits and the general setup have changes that need to be commited
	// old-style conduits have their own config dlg which commits them itself
	if ( fStack->id( fStack->visibleWidget())==NEW_CONDUIT )
	{
		if (fCurrentConfig) fCurrentConfig->load();
	}
}


void ConduitConfigWidget::conduitsChanged(TQListViewItem*item)
{
	KPilotCheckListItem*i=dynamic_cast<KPilotCheckListItem*>(item);
	if (i)
	{
		if (!i->isOriginalState()) emit changed(true);
	}
}

void ConduitConfigWidget::reopenItem(TQListViewItem *i)
{
	i->setOpen(true);
}

void ConduitConfigWidget::configureWizard()
{
	FUNCTIONSETUP;
	ConfigWizard wiz(this, "Wizard");
	if (wiz.exec()) {
		KPilotSettings::self()->readConfig();
		load();
	}
}