libkdepim

kincidencechooser.cpp

00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2004 Lutz Rogowski <rogowski@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "kincidencechooser.h"
00026 #include "libkcal/incidence.h"
00027 #include "libkcal/incidenceformatter.h"
00028 
00029 #include <klocale.h>
00030 
00031 #include <qbuttongroup.h>
00032 #include <qhbox.h>
00033 #include <qlabel.h>
00034 #include <qlayout.h>
00035 #include <qpushbutton.h>
00036 #include <qradiobutton.h>
00037 #include <qtooltip.h>
00038 #include <qwhatsthis.h>
00039 
00040 int KIncidenceChooser::chooseMode = KIncidenceChooser::ask;
00041 
00042 KIncidenceChooser::KIncidenceChooser( QWidget *parent, char *name )
00043   : KDialog( parent, name, true )
00044 {
00045   KDialog *topFrame = this;
00046   QGridLayout *topLayout = new QGridLayout( topFrame, 5, 3 );
00047 
00048   int iii = 0;
00049   setCaption( i18n( "Conflict Detected" ) );
00050   QLabel *lab;
00051   lab = new QLabel(
00052     i18n( "<qt>A conflict was detected. This probably means someone edited "
00053           "the same incidence on the server while you changed it locally."
00054           "<p>"
00055           "<b>NOTE</b>: You have to check mail again to apply your changes "
00056           "to the server.</qt>" ), topFrame );
00057   topLayout->addMultiCellWidget( lab, iii, iii, 0, 2 );
00058   ++iii;
00059 
00060   QHBox *b_box = new QHBox( topFrame );
00061   topLayout->addMultiCellWidget( b_box, iii, iii, 0, 2 );
00062   ++iii;
00063 
00064   QPushButton *locBut = new QPushButton( i18n( "Take Local" ), b_box );
00065   connect( locBut, SIGNAL(clicked()), this, SLOT (takeIncidence1()) );
00066   QToolTip::add(
00067     locBut,
00068     i18n( "Take your local copy of the incidence" ) );
00069   QWhatsThis::add(
00070     locBut,
00071     i18n( "A conflict was detected between your local copy of the incidence "
00072           "and the remote incidence on the server. Press the \"Take Local\" "
00073           "button to make sure your local copy is used." ) );
00074 
00075   QPushButton *remBut = new QPushButton( i18n( "Take New" ), b_box );
00076   connect( remBut, SIGNAL(clicked()), this, SLOT (takeIncidence2()) );
00077   QToolTip::add(
00078     remBut,
00079     i18n( "Take the server copy of the incidence" ) );
00080   QWhatsThis::add(
00081     remBut,
00082     i18n( "A conflict was detected between your local copy of the incidence "
00083           "and the remote incidence on the server. Press the \"Take New\" "
00084           "button to use the server copy, thereby overwriting your local copy" ) );
00085 
00086   QPushButton *bothBut = new QPushButton( i18n( "Take Both" ), b_box );
00087   bothBut->setFocus(); //kolab/issue4147:  "Take Both" should be default
00088   connect( bothBut, SIGNAL(clicked()), this, SLOT (takeBoth()) );
00089   QToolTip::add(
00090     bothBut,
00091     i18n( "Take both copies of the incidence" ) );
00092   QWhatsThis::add(
00093     bothBut,
00094     i18n( "A conflict was detected between your local copy of the incidence "
00095           "and the remote incidence on the server. Press the \"Take Both\" "
00096           "button to keep both the local and the server copies." ) );
00097 
00098   topLayout->setSpacing( spacingHint() );
00099   topLayout->setMargin( marginHint() );
00100 
00101   mInc1lab = new QLabel( i18n( "Local incidence" ), topFrame );
00102   topLayout->addWidget( mInc1lab, iii, 0 );
00103 
00104   mInc1Sumlab = new QLabel( i18n( "Local incidence summary" ), topFrame );
00105   topLayout->addMultiCellWidget( mInc1Sumlab, iii, iii, 1, 2 );
00106   ++iii;
00107 
00108   topLayout->addWidget( new QLabel( i18n( "Last modified:" ), topFrame ), iii, 0 );
00109 
00110   mMod1lab = new QLabel( "Set Last modified", topFrame );
00111   topLayout->addWidget( mMod1lab, iii, 1 );
00112 
00113   mShowDetails1 = new QPushButton( i18n( "Show Details" ), topFrame );
00114   QToolTip::add(
00115     mShowDetails1,
00116     i18n( "Hide/Show incidence details" ) );
00117   QWhatsThis::add(
00118     mShowDetails1,
00119     i18n( "Press this button to toggle the incidence details display." ) );
00120   connect( mShowDetails1, SIGNAL(clicked()), this, SLOT (showIncidence1()) );
00121   topLayout->addWidget( mShowDetails1, iii, 2 );
00122   ++iii;
00123 
00124   mInc2lab = new QLabel( "Local incidence", topFrame );
00125   topLayout->addWidget( mInc2lab, iii, 0 );
00126 
00127   mInc2Sumlab = new QLabel( "Local incidence summary", topFrame );
00128   topLayout->addMultiCellWidget( mInc2Sumlab, iii, iii, 1, 2 );
00129   ++iii;
00130 
00131   topLayout->addWidget( new QLabel( i18n( "Last modified:" ), topFrame ), iii, 0 );
00132 
00133   mMod2lab = new QLabel( "Set Last modified", topFrame );
00134   topLayout->addWidget( mMod2lab, iii, 1 );
00135 
00136   mShowDetails2 = new QPushButton( i18n( "Show Details" ), topFrame );
00137   QToolTip::add(
00138     mShowDetails2,
00139     i18n( "Hide/Show incidence details" ) );
00140   QWhatsThis::add(
00141     mShowDetails2,
00142     i18n( "Press this button to toggle the incidence details display." ) );
00143   connect( mShowDetails2, SIGNAL(clicked()), this, SLOT(showIncidence2()) );
00144   topLayout->addWidget( mShowDetails2, iii, 2 );
00145   ++iii;
00146 
00147 #if 0
00148   // commented out for now, because the diff code has too many bugs
00149   mDiffBut = new QPushButton( i18n( "Show Differences" ), topFrame );
00150   QToolTip::add(
00151     mDiffBut,
00152     i18n( "Show the differences between the two incidences" ) );
00153   QWhatsThis::add(
00154     mDiffBut,
00155     i18n( "Press the \"Show Differences\" button to see the specific "
00156           "differences between the incidences which are in conflict." ) );
00157   connect ( mDiffBut, SIGNAL(clicked()), this, SLOT (showDiff()) );
00158   topLayout->addMultiCellWidget( diffBut, iii, iii, 0, 2 );
00159   ++iii;
00160 #else
00161   mDiffBut = 0;
00162 #endif
00163   mBg = new QButtonGroup( 1, Qt::Horizontal, i18n( "Sync Preferences" ), topFrame );
00164   topLayout->addMultiCellWidget( mBg, iii, iii, 0, 2 );
00165   ++iii;
00166 
00167   QRadioButton *locRad = new QRadioButton(
00168     i18n( "Take local copy on conflict" ), mBg );
00169   mBg->insert( locRad, KIncidenceChooser::local );
00170   QToolTip::add(
00171     locRad,
00172     i18n( "Take local copy of the incidence on conflicts" ) );
00173   QWhatsThis::add(
00174     locRad,
00175     i18n( "When a conflict is detected between a local copy of an incidence "
00176           "and a remote incidence on the server, this option enforces using "
00177           "the local copy." ) );
00178 
00179   QRadioButton *remRad = new QRadioButton(
00180     i18n( "Take remote copy on conflict" ), mBg );
00181   mBg->insert( remRad, KIncidenceChooser::remote );
00182   QToolTip::add(
00183     remRad,
00184     i18n( "Take remote copy of the incidence on conflicts" ) );
00185   QWhatsThis::add(
00186     remRad,
00187     i18n( "When a conflict is detected between a local copy of an incidence "
00188           "and a remote incidence on the server, this option enforces using "
00189           "the remote copy." ) );
00190 
00191   QRadioButton *newRad = new QRadioButton(
00192     i18n( "Take newest incidence on conflict" ), mBg );
00193   mBg->insert( newRad, KIncidenceChooser::newest );
00194   QToolTip::add(
00195     newRad,
00196     i18n( "Take newest version of the incidence on conflicts" ) );
00197   QWhatsThis::add(
00198     newRad,
00199     i18n( "When a conflict is detected between a local copy of an incidence "
00200           "and a remote incidence on the server, this option enforces using "
00201           "the newest version available." ) );
00202 
00203   QRadioButton *askRad = new QRadioButton(
00204     i18n( "Ask for every conflict" ), mBg );
00205   mBg->insert( askRad, KIncidenceChooser::ask );
00206   QToolTip::add(
00207     askRad,
00208     i18n( "Ask for every incidence conflict" ) );
00209   QWhatsThis::add(
00210     askRad,
00211     i18n( "When a conflict is detected between a local copy of an incidence "
00212           "and a remote incidence on the server, this option says to ask "
00213           "the user which version they want to keep." ) );
00214 
00215   QRadioButton *bothRad = new QRadioButton(
00216     i18n( "Take both on conflict" ), mBg );
00217   mBg->insert( bothRad, KIncidenceChooser::both );
00218   QToolTip::add(
00219     bothRad,
00220     i18n( "Take both incidences on conflict" ) );
00221   QWhatsThis::add(
00222     bothRad,
00223     i18n( "When a conflict is detected between a local copy of an incidence "
00224           "and a remote incidence on the server, this option says to keep "
00225           "both versions of the incidence." ) );
00226 
00227   mBg->setButton ( chooseMode );
00228 
00229   QPushButton *applyBut = new QPushButton(
00230     i18n( "Apply preference to all conflicts of this sync" ), topFrame );
00231   connect( applyBut, SIGNAL(clicked()), this, SLOT(setSyncMode()) );
00232   QToolTip::add(
00233     applyBut,
00234     i18n( "Apply the preference to all conflicts that may occur during the sync" ) );
00235   QWhatsThis::add(
00236     applyBut,
00237     i18n( "Press this button to apply the selected preference to all "
00238           "future conflicts that might occur during this sync." ) );
00239   topLayout->addMultiCellWidget( applyBut, iii, iii, 0, 2 );
00240 
00241   mTbL = 0;
00242   mTbN =  0;
00243   mDisplayDiff = 0;
00244   mSelIncidence = 0;
00245 }
00246 
00247 KIncidenceChooser::~KIncidenceChooser()
00248 {
00249   if ( mTbL ) {
00250     delete mTbL;
00251   }
00252   if ( mTbN ) {
00253     delete mTbN;
00254   }
00255   if ( mDisplayDiff ) {
00256     delete mDisplayDiff;
00257     delete diff;
00258   }
00259 }
00260 
00261 void KIncidenceChooser::setIncidence( Incidence *local, Incidence *remote )
00262 {
00263   mInc1 = local;
00264   mInc2 = remote;
00265   setLabels();
00266 
00267 }
00268 
00269 Incidence *KIncidenceChooser::getIncidence( )
00270 {
00271   Incidence *retval = mSelIncidence;
00272   if ( chooseMode == KIncidenceChooser::local ) {
00273     retval = mInc1;
00274   } else if ( chooseMode == KIncidenceChooser::remote ) {
00275     retval = mInc2;
00276   } else if ( chooseMode == KIncidenceChooser::both ) {
00277     retval = 0;
00278   } else if ( chooseMode == KIncidenceChooser::newest ) {
00279     if ( mInc1->lastModified() == mInc2->lastModified() ) {
00280       retval = 0;
00281     }
00282     if ( mInc1->lastModified() >  mInc2->lastModified() ) {
00283       retval =  mInc1;
00284     } else {
00285       retval = mInc2;
00286     }
00287   }
00288   return retval;
00289 }
00290 
00291 void KIncidenceChooser::setSyncMode()
00292 {
00293   chooseMode = mBg->selectedId ();
00294   if ( chooseMode != KIncidenceChooser::ask ) {
00295     QDialog::accept();
00296   }
00297 }
00298 
00299 void KIncidenceChooser::useGlobalMode()
00300 {
00301   if ( chooseMode != KIncidenceChooser::ask ) {
00302     QDialog::reject();
00303   }
00304 }
00305 
00306 void KIncidenceChooser::setLabels()
00307 {
00308   Incidence *inc = mInc1;
00309   QLabel *des = mInc1lab;
00310   QLabel *sum = mInc1Sumlab;
00311 
00312   if ( inc->type() == "Event" ) {
00313     des->setText( i18n( "Local Event" ) );
00314     sum->setText( inc->summary().left( 30 ) );
00315     if ( mDiffBut ) {
00316       mDiffBut->setEnabled( true );
00317     }
00318   } else if ( inc->type() == "Todo" ) {
00319     des->setText( i18n( "Local Todo" ) );
00320     sum->setText( inc->summary().left( 30 ) );
00321     if ( mDiffBut ) {
00322       mDiffBut->setEnabled( true );
00323     }
00324   } else if ( inc->type() == "Journal" ) {
00325     des->setText( i18n( "Local Journal" ) );
00326     sum->setText( inc->description().left( 30 ) );
00327     if ( mDiffBut ) {
00328       mDiffBut->setEnabled( false );
00329     }
00330   }
00331   mMod1lab->setText( KGlobal::locale()->formatDateTime( inc->lastModified() ) );
00332   inc = mInc2;
00333   des = mInc2lab;
00334   sum = mInc2Sumlab;
00335   if ( inc->type() == "Event" ) {
00336     des->setText( i18n( "New Event" ) );
00337     sum->setText( inc->summary().left( 30 ) );
00338   } else if ( inc->type() == "Todo" ) {
00339     des->setText( i18n( "New Todo" ) );
00340     sum->setText( inc->summary().left( 30 ) );
00341   } else if ( inc->type() == "Journal" ) {
00342     des->setText( i18n( "New Journal" ) );
00343     sum->setText( inc->description().left( 30 ) );
00344   }
00345   mMod2lab->setText( KGlobal::locale()->formatDateTime( inc->lastModified() ) );
00346 }
00347 
00348 void KIncidenceChooser::showIncidence1()
00349 {
00350   if ( mTbL ) {
00351     if ( mTbL->isVisible() ) {
00352       mShowDetails1->setText( i18n( "Show Details" ) );
00353       mTbL->hide();
00354     } else {
00355       mShowDetails1->setText( i18n( "Hide Details" ) );
00356       mTbL->show();
00357       mTbL->raise();
00358     }
00359     return;
00360   }
00361   mTbL = new KDialogBase( this, "", false/*not modal*/, mInc1lab->text(), KDialogBase::Ok );
00362   mTbL->setEscapeButton( KDialogBase::Ok );
00363   connect( mTbL, SIGNAL(okClicked()), this, SLOT(detailsDialogClosed()) );
00364   QTextBrowser *textBrowser = new QTextBrowser( mTbL );
00365   mTbL->setMainWidget( textBrowser );
00366   textBrowser->setText( IncidenceFormatter::extensiveDisplayStr( 0, mInc1 ) );
00367   QToolTip::add( textBrowser, i18n( "Incidence details" ) );
00368   QWhatsThis::add( textBrowser, i18n( "This area shows the incidence details" ) );
00369   mTbL->setMinimumSize( 400, 400 );
00370   mShowDetails1->setText( i18n( "Hide Details" ) );
00371   mTbL->show();
00372   mTbL->raise();
00373 }
00374 
00375 void KIncidenceChooser::detailsDialogClosed()
00376 {
00377   KDialogBase* dialog = static_cast<KDialogBase *>( const_cast<QObject *>( sender() ) );
00378   if ( dialog == mTbL ) {
00379     mShowDetails1->setText( i18n( "Show details..." ) );
00380   } else {
00381     mShowDetails2->setText( i18n( "Show details..." ) );
00382   }
00383 }
00384 
00385 void KIncidenceChooser::showDiff()
00386 {
00387   if ( mDisplayDiff ) {
00388     mDisplayDiff->show();
00389     mDisplayDiff->raise();
00390     return;
00391   }
00392   mDisplayDiff = new KPIM::HTMLDiffAlgoDisplay ( this );
00393   if ( mInc1->summary().left( 20 ) != mInc2->summary().left( 20 ) ) {
00394     mDisplayDiff->setCaption(
00395       i18n( "Differences of %1 and %2" ).
00396       arg( mInc1->summary().left( 20 ) ).arg( mInc2->summary().left( 20 ) ) );
00397   } else {
00398     mDisplayDiff->setCaption(
00399       i18n( "Differences of %1" ).arg( mInc1->summary().left( 20 ) ) );
00400   }
00401   diff = new KPIM::CalendarDiffAlgo( mInc1, mInc2 );
00402   diff->setLeftSourceTitle( i18n( "Local incidence" ) );
00403   diff->setRightSourceTitle( i18n( "Remote incidence" ) );
00404   diff->addDisplay( mDisplayDiff );
00405   diff->run();
00406   mDisplayDiff->show();
00407   mDisplayDiff->raise();
00408 }
00409 
00410 void KIncidenceChooser::showIncidence2()
00411 {
00412   if ( mTbN ) {
00413     if ( mTbN->isVisible() ) {
00414       mShowDetails2->setText( i18n( "Show Details" ) );
00415       mTbN->hide();
00416     } else {
00417       mShowDetails2->setText( i18n( "Hide Details" ) );
00418       mTbN->show();
00419       mTbN->raise();
00420     }
00421     return;
00422   }
00423   mTbN = new KDialogBase( this, "", false/*not modal*/, mInc2lab->text(), KDialogBase::Ok );
00424   mTbN->setEscapeButton( KDialogBase::Ok );
00425   connect( mTbN, SIGNAL(okClicked()), this, SLOT(detailsDialogClosed()) );
00426   QTextBrowser *textBrowser = new QTextBrowser( mTbN );
00427   mTbN->setMainWidget( textBrowser );
00428   textBrowser->setText( IncidenceFormatter::extensiveDisplayStr( 0, mInc2 ) );
00429   QToolTip::add( textBrowser, i18n( "Incidence details" ) );
00430   QWhatsThis::add( textBrowser, i18n( "This area shows the incidence details" ) );
00431   mTbN->setMinimumSize( 400, 400 );
00432   mShowDetails2->setText( i18n( "Hide Details" ) );
00433   mTbN->show();
00434   mTbN->raise();
00435 }
00436 
00437 void KIncidenceChooser::takeIncidence1()
00438 {
00439   mSelIncidence = mInc1;
00440   QDialog::accept();
00441 }
00442 
00443 void KIncidenceChooser::takeIncidence2()
00444 {
00445   mSelIncidence = mInc2;
00446   QDialog::accept();
00447 }
00448 
00449 void KIncidenceChooser::takeBoth()
00450 {
00451   mSelIncidence = 0;
00452   QDialog::accept();
00453 }
00454 
00455 #include "kincidencechooser.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys