libtdepim

kincidencechooser.cpp
1/*
2 This file is part of libtdepim.
3
4 Copyright (c) 2004 Lutz Rogowski <rogowski@kde.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 As a special exception, permission is given to link this program
21 with any edition of TQt, and distribute the resulting executable,
22 without including the source code for TQt in the source distribution.
23*/
24
25#include <tqlayout.h>
26#include <tqlabel.h>
27#include <tqbuttongroup.h>
28#include <tqvbox.h>
29#include <tqhbox.h>
30#include <tqradiobutton.h>
31#include <tqpushbutton.h>
32#include <tqlayout.h>
33#include <tqscrollview.h>
34#include <tqtextbrowser.h>
35#include <tqapplication.h>
36
37
38#include <tdelocale.h>
39#include <tdeglobal.h>
40
41#include "kincidencechooser.h"
42#include "libkcal/incidence.h"
43#include "libkcal/incidenceformatter.h"
44
45int KIncidenceChooser::chooseMode = KIncidenceChooser::ask ;
46
47KIncidenceChooser::KIncidenceChooser(TQWidget *parent, char *name) :
48 KDialog(parent,name,true)
49{
50 KDialog *topFrame = this;
51 TQGridLayout *topLayout = new TQGridLayout(topFrame,5,3);
52 int iii = 0;
53 setCaption( i18n("Conflict Detected"));
54 TQLabel * lab;
55 lab = new TQLabel( i18n(
56 "<qt>A conflict was detected. This probably means someone edited the same entry on the server while you changed it locally."
57 "<br/>NOTE: You have to check mail again to apply your changes to the server.</qt>"), topFrame);
58 topLayout->addMultiCellWidget(lab, iii,iii,0,2);
59 ++iii;
60 TQHBox * b_box = new TQHBox( topFrame );
61 topLayout->addMultiCellWidget(b_box, iii,iii,0,2);
62 ++iii;
63 TQPushButton* button = new TQPushButton( i18n("Take Local"), b_box );
64 connect ( button, TQ_SIGNAL( clicked()), this, TQ_SLOT (takeIncidence1() ) );
65 button = new TQPushButton( i18n("Take New"), b_box );
66 connect ( button, TQ_SIGNAL( clicked()), this, TQ_SLOT (takeIncidence2() ) );
67 button = new TQPushButton( i18n("Take Both"), b_box );
68 connect ( button, TQ_SIGNAL( clicked()), this, TQ_SLOT (takeBoth() ) );
69 topLayout->setSpacing(spacingHint());
70 topLayout->setMargin(marginHint());
71 // text is not translated, because text has to be set later
72 mInc1lab = new TQLabel ( i18n("Local incidence"), topFrame);
73 topLayout->addWidget(mInc1lab ,iii,0);
74 mInc1Sumlab = new TQLabel ( i18n("Local incidence summary"), topFrame);
75 topLayout->addMultiCellWidget(mInc1Sumlab, iii,iii,1,2);
76 ++iii;
77 topLayout->addWidget( new TQLabel ( i18n("Last modified:"), topFrame) ,iii,0);
78 mMod1lab = new TQLabel ( "Set Last modified", topFrame);
79 topLayout->addWidget(mMod1lab,iii,1);
80 mShowDetails1 = new TQPushButton( i18n("Show Details"),topFrame );
81 connect ( mShowDetails1, TQ_SIGNAL( clicked()), this, TQ_SLOT (showIncidence1() ) );
82 topLayout->addWidget(mShowDetails1,iii,2);
83 ++iii;
84
85 mInc2lab = new TQLabel ( "Local incidence", topFrame);
86 topLayout->addWidget(mInc2lab,iii,0);
87 mInc2Sumlab = new TQLabel ( "Local incidence summary", topFrame);
88 topLayout->addMultiCellWidget(mInc2Sumlab, iii,iii,1,2);
89 ++iii;
90 topLayout->addWidget( new TQLabel ( i18n("Last modified:"), topFrame) ,iii,0);
91 mMod2lab = new TQLabel ( "Set Last modified", topFrame);
92 topLayout->addWidget(mMod2lab,iii,1);
93 mShowDetails2 = new TQPushButton( i18n("Show Details"), topFrame);
94 connect ( mShowDetails2, TQ_SIGNAL( clicked()), this, TQ_SLOT (showIncidence2() ) );
95 topLayout->addWidget(mShowDetails2,iii,2);
96 ++iii;
97 //
98#if 0
99 // commented out for now, because the diff code has too many bugs
100 mDiffBut = new TQPushButton( i18n("Show Differences"), topFrame );
101 connect ( mDiffBut, TQ_SIGNAL( clicked()), this, TQ_SLOT ( showDiff() ) );
102 topLayout->addMultiCellWidget(mDiffBut, iii,iii,0,2);
103 ++iii;
104#else
105 mDiffBut = 0;
106#endif
107 mBg = new TQButtonGroup ( 1, TQt::Horizontal, i18n("Sync Preferences"), topFrame);
108 topLayout->addMultiCellWidget(mBg, iii,iii,0,2);
109 ++iii;
110 mBg->insert( new TQRadioButton ( i18n("Take local entry on conflict"), mBg ), KIncidenceChooser::local);
111 mBg->insert( new TQRadioButton ( i18n("Take new (remote) entry on conflict"), mBg ), KIncidenceChooser::remote);
112 mBg->insert( new TQRadioButton ( i18n("Take newest entry on conflict"), mBg ), KIncidenceChooser::newest );
113 mBg->insert( new TQRadioButton ( i18n("Ask for every entry on conflict"), mBg ),KIncidenceChooser::ask );
114 mBg->insert( new TQRadioButton ( i18n("Take both on conflict"), mBg ), KIncidenceChooser::both );
115 mBg->setButton ( chooseMode );
116 mTbL = 0;
117 mTbN = 0;
118 mDisplayDiff = 0;
119 mSelIncidence = 0;
120 button = new TQPushButton( i18n("Apply This to All Conflicts of This Sync"), topFrame );
121 connect ( button, TQ_SIGNAL( clicked()), this, TQ_SLOT ( setSyncMode() ) );
122 topLayout->addMultiCellWidget(button, iii,iii,0,2);
123}
124
125KIncidenceChooser::~KIncidenceChooser()
126{
127 if ( mTbL ) delete mTbL;
128 if ( mTbN ) delete mTbN;
129 if ( mDisplayDiff ) {
130 delete mDisplayDiff;
131 delete diff;
132 }
133}
134
135void KIncidenceChooser::setIncidence( KCal::Incidence* local ,KCal::Incidence* remote )
136{
137 mInc1 = local;
138 mInc2 = remote;
139 setLabels();
140
141}
142KCal::Incidence* KIncidenceChooser::getIncidence( )
143{
144
145 KCal::Incidence* retval = mSelIncidence;
146 if ( chooseMode == KIncidenceChooser::local )
147 retval = mInc1;
148 else if ( chooseMode == KIncidenceChooser::remote )
149 retval = mInc2;
150 else if ( chooseMode == KIncidenceChooser::both ) {
151 retval = 0;
152 }
153 else if ( chooseMode == KIncidenceChooser::newest ) {
154 if ( mInc1->lastModified() == mInc2->lastModified())
155 retval = 0;
156 if ( mInc1->lastModified() > mInc2->lastModified() )
157 retval = mInc1;
158 else
159 retval = mInc2;
160 }
161 return retval;
162}
163
164void KIncidenceChooser::setSyncMode()
165{
166 chooseMode = mBg->selectedId ();
167 if ( chooseMode != KIncidenceChooser::ask )
168 TQDialog::accept();
169
170}
171
172void KIncidenceChooser::useGlobalMode()
173{
174 if ( chooseMode != KIncidenceChooser::ask )
175 TQDialog::reject();
176}
177
178void KIncidenceChooser::setLabels()
179{
180 KCal::Incidence* inc = mInc1;
181 TQLabel* des = mInc1lab;
182 TQLabel * sum = mInc1Sumlab;
183
184
185 if ( inc->type() == "Event" ) {
186 des->setText( i18n( "Local Event") );
187 sum->setText( inc->summary().left( 30 ));
188 if ( mDiffBut )
189 mDiffBut->setEnabled( true );
190 }
191 else if ( inc->type() == "Todo" ) {
192 des->setText( i18n( "Local Todo") );
193 sum->setText( inc->summary().left( 30 ));
194 if ( mDiffBut )
195 mDiffBut->setEnabled( true );
196
197 }
198 else if ( inc->type() == "Journal" ) {
199 des->setText( i18n( "Local Journal") );
200 sum->setText( inc->description().left( 30 ));
201 if ( mDiffBut )
202 mDiffBut->setEnabled( false );
203 }
204 mMod1lab->setText( TDEGlobal::locale()->formatDateTime(inc->lastModified() ));
205 inc = mInc2;
206 des = mInc2lab;
207 sum = mInc2Sumlab;
208 if ( inc->type() == "Event" ) {
209 des->setText( i18n( "New Event") );
210 sum->setText( inc->summary().left( 30 ));
211 }
212 else if ( inc->type() == "Todo" ) {
213 des->setText( i18n( "New Todo") );
214 sum->setText( inc->summary().left( 30 ));
215
216 }
217 else if ( inc->type() == "Journal" ) {
218 des->setText( i18n( "New Journal") );
219 sum->setText( inc->description().left( 30 ));
220
221 }
222 mMod2lab->setText( TDEGlobal::locale()->formatDateTime(inc->lastModified() ));
223}
224
225void KIncidenceChooser::showIncidence1()
226{
227 if ( mTbL ) {
228 if ( mTbL->isVisible() ) {
229 mShowDetails1->setText( i18n("Show Details"));
230 mTbL->hide();
231 } else {
232 mShowDetails1->setText( i18n("Hide Details"));
233 mTbL->show();
234 mTbL->raise();
235 }
236 return;
237 }
238 mTbL = new KDialogBase( this, "", false /*not modal*/, mInc1lab->text(), KDialogBase::Ok );
239 mTbL->setEscapeButton( KDialogBase::Ok );
240 connect( mTbL, TQ_SIGNAL( okClicked() ), this, TQ_SLOT( detailsDialogClosed() ) );
241 TQTextBrowser* textBrowser = new TQTextBrowser( mTbL );
242 mTbL->setMainWidget( textBrowser );
243 textBrowser->setText( KCal::IncidenceFormatter::extensiveDisplayString( mInc1 ) );
244 mTbL->setMinimumSize( 400, 400 );
245 mShowDetails1->setText( i18n("Hide Details"));
246 mTbL->show();
247 mTbL->raise();
248}
249
250void KIncidenceChooser::detailsDialogClosed()
251{
252 KDialogBase* dialog = static_cast<KDialogBase *>(const_cast<TQObject*>(sender())) ;
253 if ( dialog == mTbL )
254 mShowDetails1->setText( i18n( "Show details..." ) );
255 else
256 mShowDetails2->setText( i18n( "Show details..." ) );
257}
258
259void KIncidenceChooser::showDiff()
260{
261 if ( mDisplayDiff ) {
262 mDisplayDiff->show();
263 mDisplayDiff->raise();
264 return;
265 }
266 mDisplayDiff = new KPIM::HTMLDiffAlgoDisplay (this);
267 if ( mInc1->summary().left( 20 ) != mInc2->summary().left( 20 ) )
268 mDisplayDiff->setCaption( i18n( "Differences of %1 and %2").arg( mInc1->summary().left( 20 ) ).arg( mInc2->summary().left( 20 ) ) );
269 else
270 mDisplayDiff->setCaption( i18n( "Differences of %1").arg( mInc1->summary().left( 20 ) ) );
271
272 diff = new KPIM::CalendarDiffAlgo( mInc1, mInc2);
273 diff->setLeftSourceTitle( i18n( "Local entry"));
274 diff->setRightSourceTitle(i18n( "New (remote) entry") );
275 diff->addDisplay( mDisplayDiff );
276 diff->run();
277 mDisplayDiff->show();
278 mDisplayDiff->raise();
279}
280
281void KIncidenceChooser::showIncidence2()
282{
283 if ( mTbN ) {
284 if ( mTbN->isVisible() ) {
285 mShowDetails2->setText( i18n("Show Details"));
286 mTbN->hide();
287 } else {
288 mShowDetails2->setText( i18n("Hide Details"));
289 mTbN->show();
290 mTbN->raise();
291 }
292 return;
293 }
294 mTbN = new KDialogBase( this, "", false /*not modal*/, mInc2lab->text(), KDialogBase::Ok );
295 mTbN->setEscapeButton( KDialogBase::Ok );
296 connect( mTbN, TQ_SIGNAL( okClicked() ), this, TQ_SLOT( detailsDialogClosed() ) );
297 TQTextBrowser* textBrowser = new TQTextBrowser( mTbN );
298 mTbN->setMainWidget( textBrowser );
299 textBrowser->setText( KCal::IncidenceFormatter::extensiveDisplayString( mInc2 ) );
300 mTbN->setMinimumSize( 400, 400 );
301 mShowDetails2->setText( i18n("Hide Details"));
302 mTbN->show();
303 mTbN->raise();
304}
305
306void KIncidenceChooser::takeIncidence1()
307{
308 mSelIncidence = mInc1;
309 TQDialog::accept();
310}
311
312void KIncidenceChooser::takeIncidence2()
313{
314 mSelIncidence = mInc2;
315 TQDialog::accept();
316}
317
318void KIncidenceChooser::takeBoth()
319{
320
321 mSelIncidence = 0;
322 TQDialog::accept();
323}
324
325
326#include "kincidencechooser.moc"
KIncidenceChooser(TQWidget *parent=0, char *name=0)
Initialize dialog and pages.