kpilot

resolutionDialog.cc

Go to the documentation of this file.
00001 /* resolutionDialog.h           KPilot
00002 **
00003 ** Copyright (C) 2002-2003 by Reinhold Kainhofer
00004 **
00005 ** See the .cc file for an explanation of what this file is for.
00006 */
00007 
00008 /*
00009 ** This program is free software; you can redistribute it and/or modify
00010 ** it under the terms of the GNU General Public License as published by
00011 ** the Free Software Foundation; either version 2 of the License, or
00012 ** (at your option) any later version.
00013 **
00014 ** This program is distributed in the hope that it will be useful,
00015 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017 ** GNU General Public License for more details.
00018 **
00019 ** You should have received a copy of the GNU General Public License
00020 ** along with this program in a file called COPYING; if not, write to
00021 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00022 ** MA 02110-1301, USA.
00023 */
00024 
00025 /*
00026 ** Bug reports and questions can be sent to kde-pim@kde.org
00027 */
00028 
00029 #include "options.h"
00030 
00031 #include <qtimer.h>
00032 #include <qlabel.h>
00033 #include <qpushbutton.h>
00034 #include <qlistview.h>
00035 #include <qregexp.h>
00036 
00037 #include "resolutionTable.h"
00038 #include "resolutionDialog_base.h"
00039 
00040 #include "resolutionDialog.moc"
00041 
00049 class ResolutionCheckListItem : QCheckListItem {
00050 public:
00051     ResolutionCheckListItem(ResolutionItem*it, ResolutionTable*tb,
00052         QListView*parent);
00053     ResolutionCheckListItem(QString header, QString text,
00054         ResolutionCheckListItem*parent);
00055     ~ResolutionCheckListItem() {};
00056     virtual void stateChange(bool newstate);
00057     virtual void setValue(QString text);
00058     virtual void setCaption(QString caption);
00059 
00060 protected:
00061     void updateText();
00062     /* fResItem is only set for the controller */
00063     ResolutionItem*fResItem;
00064     bool isController;
00065     /* The description of the entry, e.g. Backup, PC, Palm for the radio buttons,
00066      * of the field name for the controllers
00067      */
00068     QString fCaption;
00069     /* The currrent value of the entry (for controllers this changes with the
00070      * selected button */
00071     QString fText;
00072 };
00073 
00074 
00075 ResolutionCheckListItem::ResolutionCheckListItem(ResolutionItem*it,
00076         ResolutionTable*tb, QListView*parent) :
00077     QCheckListItem(parent, QString::null, QCheckListItem::Controller),
00078     fResItem(it),
00079     isController(true),
00080     fCaption(it?(it->fName):(QString::null)),
00081     fText(it?(it->fResolved):(QString::null))
00082 {
00083     FUNCTIONSETUP;
00084     if (it && tb)
00085     {
00086         // If all three texts are identical, there is no need for
00087         // resolution so don't show the radio items below
00088         bool itemsEqual=true;
00089         QString testtext(QString::null);
00090         const enum eExistItems its[3]={eExistsPC, eExistsPalm, eExistsBackup};
00091         // get a valid text from a valid field, which will serve as the
00092         // test text for the comparison
00093         for (int i=0; i<3; i++)
00094         {
00095             if ((testtext.isNull()) && (it->fExistItems & its[i]) )
00096                 testtext=it->fEntries[i];
00097         }
00098         for (int i=0; i<3; i++)
00099         {
00100             if (it->fExistItems & its[i])
00101                 itemsEqual&=(it->fEntries[i]==testtext);
00102         }
00103         if (!itemsEqual)
00104         {
00105             ResolutionCheckListItem*item;
00106             for (int i=2; i>=0; i--)
00107             {
00108                 // Add only existing items
00109                 if (it->fExistItems & its[i])
00110                 {
00111                     item=new ResolutionCheckListItem(it->fEntries[i], tb->labels[i], this);
00112                     item->setOn(it->fEntries[i]==fText);
00113                 }
00114             }
00115         }
00116         updateText();
00117     }
00118     setOpen(true);
00119 }
00120 
00121 ResolutionCheckListItem::ResolutionCheckListItem(QString text, QString header,
00122         ResolutionCheckListItem*parent) :
00123     QCheckListItem(parent, QString(), QCheckListItem::RadioButton),
00124     fResItem(0L),
00125     isController(false),
00126     fCaption(header),
00127     fText(text)
00128 {
00129     updateText();
00130 }
00131 
00132 void ResolutionCheckListItem::stateChange(bool newstate)
00133 {
00134     if (newstate && !isController)
00135     {
00136         ResolutionCheckListItem*par=static_cast<ResolutionCheckListItem*>(parent());
00137         {
00138             par->setValue(fText);
00139         }
00140     }
00141 }
00142 
00143 void ResolutionCheckListItem::setValue(QString text)
00144 {
00145     FUNCTIONSETUP;
00146     fText=text;
00147     if (isController && fResItem)
00148     {
00149         fResItem->fResolved=text;
00150     }
00151     updateText();
00152 }
00153 
00154 void ResolutionCheckListItem::setCaption(QString caption)
00155 {
00156     fCaption=caption;
00157     updateText();
00158 }
00159 
00160 void ResolutionCheckListItem::updateText()
00161 {
00162     QString 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").arg(fCaption).arg(fText));
00163     newText.replace(QRegExp(CSL1("\n")),
00164         i18n("Denoting newlines in Address entries. No need to translate", " | "));
00165     setText(0, newText);
00166 }
00167 
00168 
00169 
00170 /*****************************************************************
00171  *
00172  *****************************************************************/
00173 
00174 ResolutionDlg::ResolutionDlg( QWidget* parent, KPilotLink*fH,
00175     const QString &caption, const QString &helpText, ResolutionTable*tab) :
00176     KDialogBase( parent, "ResolutionDlg", false, caption, Apply|Cancel, Apply),
00177     tickleTimer(0L),
00178     fHandle(fH),
00179     fTable(tab)
00180 {
00181     fWidget = new ResolutionDialogBase( this );
00182     setMainWidget(fWidget);
00183     fTable->fResolution=SyncAction::eDoNothing;
00184     fWidget->fIntroText->setText(helpText);
00185 
00186     fillListView();
00187     adjustButtons(tab);
00188 
00189     adjustSize();
00190     resize(size());
00191 
00192     if (fHandle) tickleTimer=new QTimer(this, "TickleTimer");
00193 
00194     if (tickleTimer)
00195     {
00196         connect( tickleTimer, SIGNAL(timeout()), this, SLOT(_tickle()));
00197         // tickle the palm every 10 seconds to prevent a timeout until the
00198         // sync is really finished.
00199         tickleTimer->start( 10000 );
00200     }
00201 
00202     connect(fWidget->fKeepBoth, SIGNAL(clicked()), SLOT(slotKeepBoth()));
00203     connect(fWidget->fBackupValues, SIGNAL(clicked()), SLOT(slotUseBackup()));
00204     connect(fWidget->fPalmValues, SIGNAL(clicked()), SLOT(slotUsePalm()));
00205     connect(fWidget->fPCValues, SIGNAL(clicked()), SLOT(slotUsePC()));
00206 }
00207 
00208 void ResolutionDlg::adjustButtons(ResolutionTable*tab)
00209 {
00210     FUNCTIONSETUP;
00211     if (!tab) return;
00212     if (!(tab->fExistItems & eExistsPC) )
00213     {
00214         fWidget->fPCValues->setText(i18n("Delete entry"));
00215         fWidget->fKeepBoth->setDisabled(TRUE);
00216         fWidget->fKeepBoth->hide();
00217     }
00218     if (!(tab->fExistItems & eExistsPalm) )
00219     {
00220         fWidget->fPalmValues->setText(i18n("Delete entry"));
00221         fWidget->fKeepBoth->setDisabled(TRUE);
00222         fWidget->fKeepBoth->hide();
00223     }
00224     if (!(tab->fExistItems & eExistsBackup) )
00225     {
00226         fWidget->fBackupValues->setDisabled(TRUE);
00227     }
00228 }
00229 
00230 void ResolutionDlg::fillListView()
00231 {
00232     FUNCTIONSETUP;
00233     fWidget->fResolutionView->setSorting(-1, FALSE);
00234     fWidget->fResolutionView->clear();
00235     for ( ResolutionItem* it = fTable->last(); it; it = fTable->prev() )
00236     {
00237 #ifdef DEBUG
00238         DEBUGKPILOT<<"Building table, items="<<it->fExistItems<<", PC="<<
00239             it->fEntries[0]<<", Palm="<<it->fEntries[1]<<", Backup="<<
00240             it->fEntries[2]<<endl;
00241 #endif
00242         bool hasValidValues=false;
00243         if (it->fExistItems & eExistsPC)
00244             hasValidValues = hasValidValues || !(it->fEntries[0].isEmpty());
00245         if (it->fExistItems & eExistsPalm)
00246             hasValidValues = hasValidValues || !(it->fEntries[1].isEmpty());
00247         if (it->fExistItems & eExistsBackup)
00248             hasValidValues = hasValidValues || !(it->fEntries[2].isEmpty());
00249         if (hasValidValues)
00250             new ResolutionCheckListItem(it, fTable, fWidget->fResolutionView);
00251     }
00252 }
00253 
00254 void ResolutionDlg::slotKeepBoth()
00255 {
00256     if ( (fTable->fExistItems & eExistsPC) && (fTable->fExistItems & eExistsPalm) )
00257     {
00258         fTable->fResolution=SyncAction::eDuplicate;
00259     }
00260     else
00261     {
00262         fTable->fResolution=SyncAction::eDoNothing;
00263     }
00264     done(fTable->fResolution);
00265 }
00266 
00267 void ResolutionDlg::slotUseBackup()
00268 {
00269     if (fTable->fExistItems & eExistsBackup)
00270     {
00271         fTable->fResolution=SyncAction::ePreviousSyncOverrides;
00272     }
00273     else
00274     {
00275         fTable->fResolution=SyncAction::eDoNothing;
00276     }
00277     done(fTable->fResolution);
00278 }
00279 
00280 void ResolutionDlg::slotUsePalm()
00281 {
00282     if (fTable->fExistItems & eExistsPalm)
00283     {
00284         fTable->fResolution=SyncAction::eHHOverrides;
00285     }
00286     else
00287     {
00288         fTable->fResolution=SyncAction::eDelete;
00289     }
00290     done(fTable->fResolution);
00291 }
00292 
00293 void ResolutionDlg::slotUsePC()
00294 {
00295     if (fTable->fExistItems & eExistsPC)
00296     {
00297         fTable->fResolution=SyncAction::ePCOverrides;
00298     }
00299     else
00300     {
00301         fTable->fResolution=SyncAction::eDelete;
00302     }
00303     done(fTable->fResolution);
00304 }
00305 
00306 void ResolutionDlg::slotApply()
00307 {
00308     fTable->fResolution=SyncAction::eAskUser;
00309     done(fTable->fResolution);
00310 }
00311 
00312 void ResolutionDlg::_tickle()
00313 {
00314     if (fHandle) fHandle->tickle();
00315 }
00316 
00317 /*
00318  *  Destroys the object and frees any allocated resources
00319  */
00320 ResolutionDlg::~ResolutionDlg()
00321 {
00322     // no need to delete child widgets, Qt does it all for us
00323 }