summaryrefslogtreecommitdiffstats
path: root/conduits/abbrowserconduit/resolutionDialog.cc
blob: ca61afe398224bde017cbc11470dd94c880db3e3 (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
/* resolutionDialog.h			KPilot
**
** Copyright (C) 2002-2003 by Reinhold Kainhofer
**
** See the .cc file for an explanation of what this file is for.
*/

/*
** 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 <tqtimer.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqlistview.h>
#include <tqregexp.h>

#include "resolutionTable.h"
#include "resolutionDialog_base.h"

#include "resolutionDialog.moc"

/** This class describes the controllers of the conflict resolution ListView,
 *  as well as its child radio buttons. There are two different constructors
 *  for them.
 *  Each controller has three child radio buttons, and if any of them is
 *  activated (stateChange), it sets the text of its tqparent (which is the
 *  controller, which is an instance of ResolutionCheckListItem, too).
 **/
class ResolutionCheckListItem : TQCheckListItem {
public:
	ResolutionCheckListItem(ResolutionItem*it, ResolutionTable*tb,
		TQListView*tqparent);
	ResolutionCheckListItem(TQString header, TQString text,
		ResolutionCheckListItem*tqparent);
	~ResolutionCheckListItem() {};
	virtual void stateChange(bool newstate);
	virtual void setValue(TQString text);
	virtual void setCaption(TQString caption);

protected:
	void updateText();
	/* fResItem is only set for the controller */
	ResolutionItem*fResItem;
	bool isController;
	/* The description of the entry, e.g. Backup, PC, Palm for the radio buttons,
	 * of the field name for the controllers
	 */
	TQString fCaption;
	/* The currrent value of the entry (for controllers this changes with the
	 * selected button */
	TQString fText;
};


ResolutionCheckListItem::ResolutionCheckListItem(ResolutionItem*it,
		ResolutionTable*tb, TQListView*tqparent) :
	TQCheckListItem(tqparent, TQString(), TQCheckListItem::Controller),
	fResItem(it),
	isController(true),
	fCaption(it?(it->fName):(TQString())),
	fText(it?(it->fResolved):(TQString()))
{
	FUNCTIONSETUP;
	if (it && tb)
	{
		// If all three texts are identical, there is no need for
		// resolution so don't show the radio items below
		bool itemsEqual=true;
		TQString testtext(TQString::null);
		const enum eExistItems its[3]={eExistsPC, eExistsPalm, eExistsBackup};
		// get a valid text from a valid field, which will serve as the
		// test text for the comparison
		for (int i=0; i<3; i++)
		{
			if ((testtext.isNull()) && (it->fExistItems & its[i]) )
				testtext=it->fEntries[i];
		}
		for (int i=0; i<3; i++)
		{
			if (it->fExistItems & its[i])
				itemsEqual&=(it->fEntries[i]==testtext);
		}
		if (!itemsEqual)
		{
			ResolutionCheckListItem*item;
			for (int i=2; i>=0; i--)
			{
				// Add only existing items
				if (it->fExistItems & its[i])
				{
					item=new ResolutionCheckListItem(it->fEntries[i], tb->labels[i], this);
					item->setOn(it->fEntries[i]==fText);
				}
			}
		}
		updateText();
	}
	setOpen(true);
}

ResolutionCheckListItem::ResolutionCheckListItem(TQString text, TQString header,
		ResolutionCheckListItem*tqparent) :
	TQCheckListItem(tqparent, TQString(), TQCheckListItem::RadioButton),
	fResItem(0L),
	isController(false),
	fCaption(header),
	fText(text)
{
	updateText();
}

void ResolutionCheckListItem::stateChange(bool newstate)
{
	if (newstate && !isController)
	{
		ResolutionCheckListItem*par=static_cast<ResolutionCheckListItem*>(tqparent());
		{
			par->setValue(fText);
		}
	}
}

void ResolutionCheckListItem::setValue(TQString text)
{
	FUNCTIONSETUP;
	fText=text;
	if (isController && fResItem)
	{
		fResItem->fResolved=text;
	}
	updateText();
}

void ResolutionCheckListItem::setCaption(TQString caption)
{
	fCaption=caption;
	updateText();
}

void ResolutionCheckListItem::updateText()
{
	TQString newText(i18n("Entries in the resolution dialog. First the name of the field, then the entry from the Handheld or PC after the colon", "%1: %2").tqarg(fCaption).tqarg(fText));
	newText.replace(TQRegExp(CSL1("\n")),
		i18n("Denoting newlines in Address entries. No need to translate", " | "));
	setText(0, newText);
}



/*****************************************************************
 *
 *****************************************************************/

ResolutionDlg::ResolutionDlg( TQWidget* tqparent, KPilotLink*fH,
	const TQString &caption, const TQString &helpText, ResolutionTable*tab) :
	KDialogBase( tqparent, "ResolutionDlg", false, caption, Apply|Cancel, Apply),
	tickleTimer(0L),
	fHandle(fH),
	fTable(tab)
{
	fWidget = new ResolutionDialogBase( this );
	setMainWidget(fWidget);
	fTable->fResolution=SyncAction::eDoNothing;
	fWidget->fIntroText->setText(helpText);

	fillListView();
	adjustButtons(tab);

	adjustSize();
	resize(size());

	if (fHandle) tickleTimer=new TQTimer(this, "TickleTimer");

	if (tickleTimer)
	{
		connect( tickleTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(_tickle()));
		// tickle the palm every 10 seconds to prevent a timeout until the
		// sync is really finished.
		tickleTimer->start( 10000 );
	}

	connect(fWidget->fKeepBoth, TQT_SIGNAL(clicked()), TQT_SLOT(slotKeepBoth()));
	connect(fWidget->fBackupValues, TQT_SIGNAL(clicked()), TQT_SLOT(slotUseBackup()));
	connect(fWidget->fPalmValues, TQT_SIGNAL(clicked()), TQT_SLOT(slotUsePalm()));
	connect(fWidget->fPCValues, TQT_SIGNAL(clicked()), TQT_SLOT(slotUsePC()));
}

void ResolutionDlg::adjustButtons(ResolutionTable*tab)
{
	FUNCTIONSETUP;
	if (!tab) return;
	if (!(tab->fExistItems & eExistsPC) )
	{
		fWidget->fPCValues->setText(i18n("Delete entry"));
		fWidget->fKeepBoth->setDisabled(TRUE);
		fWidget->fKeepBoth->hide();
	}
	if (!(tab->fExistItems & eExistsPalm) )
	{
		fWidget->fPalmValues->setText(i18n("Delete entry"));
		fWidget->fKeepBoth->setDisabled(TRUE);
		fWidget->fKeepBoth->hide();
	}
	if (!(tab->fExistItems & eExistsBackup) )
	{
		fWidget->fBackupValues->setDisabled(TRUE);
	}
}

void ResolutionDlg::fillListView()
{
	FUNCTIONSETUP;
	fWidget->fResolutionView->setSorting(-1, FALSE);
	fWidget->fResolutionView->clear();
	for ( ResolutionItem* it = fTable->last(); it; it = fTable->prev() )
	{
#ifdef DEBUG
		DEBUGKPILOT<<"Building table, items="<<it->fExistItems<<", PC="<<
			it->fEntries[0]<<", Palm="<<it->fEntries[1]<<", Backup="<<
			it->fEntries[2]<<endl;
#endif
		bool hasValidValues=false;
		if (it->fExistItems & eExistsPC)
			hasValidValues = hasValidValues || !(it->fEntries[0].isEmpty());
		if (it->fExistItems & eExistsPalm)
			hasValidValues = hasValidValues || !(it->fEntries[1].isEmpty());
		if (it->fExistItems & eExistsBackup)
			hasValidValues = hasValidValues || !(it->fEntries[2].isEmpty());
		if (hasValidValues)
			new ResolutionCheckListItem(it, fTable, fWidget->fResolutionView);
	}
}

void ResolutionDlg::slotKeepBoth()
{
	if ( (fTable->fExistItems & eExistsPC) && (fTable->fExistItems & eExistsPalm) )
	{
		fTable->fResolution=SyncAction::eDuplicate;
	}
	else
	{
		fTable->fResolution=SyncAction::eDoNothing;
	}
	done(fTable->fResolution);
}

void ResolutionDlg::slotUseBackup()
{
	if (fTable->fExistItems & eExistsBackup)
	{
		fTable->fResolution=SyncAction::ePreviousSyncOverrides;
	}
	else
	{
		fTable->fResolution=SyncAction::eDoNothing;
	}
	done(fTable->fResolution);
}

void ResolutionDlg::slotUsePalm()
{
	if (fTable->fExistItems & eExistsPalm)
	{
		fTable->fResolution=SyncAction::eHHOverrides;
	}
	else
	{
		fTable->fResolution=SyncAction::eDelete;
	}
	done(fTable->fResolution);
}

void ResolutionDlg::slotUsePC()
{
	if (fTable->fExistItems & eExistsPC)
	{
		fTable->fResolution=SyncAction::ePCOverrides;
	}
	else
	{
		fTable->fResolution=SyncAction::eDelete;
	}
	done(fTable->fResolution);
}

void ResolutionDlg::slotApply()
{
	fTable->fResolution=SyncAction::eAskUser;
	done(fTable->fResolution);
}

void ResolutionDlg::_tickle()
{
	if (fHandle) fHandle->tickle();
}

/*
 *  Destroys the object and frees any allocated resources
 */
ResolutionDlg::~ResolutionDlg()
{
    // no need to delete child widgets, TQt does it all for us
}