kaddressbook Library API Documentation

advancedcustomfields.cpp

00001 /*
00002     This file is part of KAddressbook.
00003     Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qcheckbox.h>
00025 #include <qcombobox.h>
00026 #include <qdatetimeedit.h>
00027 #include <qlayout.h>
00028 #include <qobjectlist.h>
00029 #include <qspinbox.h>
00030 #include <qregexp.h>
00031 #include <qtextedit.h>
00032 #include <qwidgetfactory.h>
00033 
00034 #include <kdatepicker.h>
00035 #include <kdatetimewidget.h>
00036 #include <kdialog.h>
00037 #include <klineedit.h>
00038 #include <kstandarddirs.h>
00039 
00040 #include "customfieldswidget.h"
00041 
00042 #include "advancedcustomfields.h"
00043 
00044 
00045 AdvancedCustomFields::AdvancedCustomFields( const QString &uiFile, KABC::AddressBook *ab,
00046                                             QWidget *parent, const char *name )
00047   : KAB::ContactEditorWidget( ab, parent, name )
00048 {
00049   initGUI( uiFile );
00050 }
00051 
00052 void AdvancedCustomFields::loadContact( KABC::Addressee *addr )
00053 {
00054   QStringList customs = addr->customs();
00055     
00056   QString ns;
00057   if ( (mIdentifier.upper() == "KADDRESSBOOK") ||
00058         (QRegExp( "^Form\\d\\d?$").search(mIdentifier) >= 0 )
00059       )
00060     ns = "KADDRESSBOOK";
00061   else 
00062     ns = mIdentifier;
00063   
00064   // clear all custom page widgets 
00065   // we can't do this in the following loop, as it works on the 
00066   // custom fields of the vcard, which may not be set.
00067   QMap<QString, QWidget*>::Iterator widIt;
00068   for ( widIt = mWidgets.begin(); widIt != mWidgets.end(); ++widIt ) {
00069     QString value;
00070     if ( widIt.data()->inherits( "QLineEdit" ) ) {
00071       QLineEdit *wdg = static_cast<QLineEdit*>( widIt.data() );
00072       wdg->setText( QString::null );
00073     } else if ( widIt.data()->inherits( "QSpinBox" ) ) {
00074       QSpinBox *wdg = static_cast<QSpinBox*>( widIt.data() );
00075       wdg->setValue( wdg->minValue() );
00076     } else if ( widIt.data()->inherits( "QCheckBox" ) ) {
00077       QCheckBox *wdg = static_cast<QCheckBox*>( widIt.data() );
00078       wdg->setChecked( false );
00079     } else if ( widIt.data()->inherits( "QDateTimeEdit" ) ) {
00080       QDateTimeEdit *wdg = static_cast<QDateTimeEdit*>( widIt.data() );
00081       wdg->setDateTime( QDateTime::currentDateTime() );
00082     } else if ( widIt.data()->inherits( "KDateTimeWidget" ) ) {
00083       KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( widIt.data() );
00084       wdg->setDateTime( QDateTime::currentDateTime() );
00085     } else if ( widIt.data()->inherits( "KDatePicker" ) ) {
00086       KDatePicker *wdg = static_cast<KDatePicker*>( widIt.data() );
00087       wdg->setDate( QDate::currentDate() );
00088     } else if ( widIt.data()->inherits( "QComboBox" ) ) {
00089       QComboBox *wdg = static_cast<QComboBox*>( widIt.data() );
00090       wdg->setCurrentItem( 0 );
00091     } else if ( widIt.data()->inherits( "QTextEdit" ) ) {
00092       QTextEdit *wdg = static_cast<QTextEdit*>( widIt.data() );
00093       wdg->setText( QString::null );
00094     }
00095   }
00096 
00097   QStringList::ConstIterator it;
00098   for ( it = customs.begin(); it != customs.end(); ++it ) {
00099     QString app, name, value;
00100     splitField( *it, app, name, value );
00101 
00102     if ( app == ns ) {
00103       QMap<QString, QWidget*>::Iterator it = mWidgets.find( name );
00104       if ( it != mWidgets.end() ) {
00105         if ( it.data()->inherits( "QLineEdit" ) ) {
00106           QLineEdit *wdg = static_cast<QLineEdit*>( it.data() );
00107           wdg->setText( value );
00108         } else if ( it.data()->inherits( "QSpinBox" ) ) {
00109           QSpinBox *wdg = static_cast<QSpinBox*>( it.data() );
00110           wdg->setValue( value.toInt() );
00111         } else if ( it.data()->inherits( "QCheckBox" ) ) {
00112           QCheckBox *wdg = static_cast<QCheckBox*>( it.data() );
00113           wdg->setChecked( value == "true" || value == "1" );
00114         } else if ( it.data()->inherits( "QDateTimeEdit" ) ) {
00115           QDateTimeEdit *wdg = static_cast<QDateTimeEdit*>( it.data() );
00116           wdg->setDateTime( QDateTime::fromString( value, Qt::ISODate ) );
00117         } else if ( it.data()->inherits( "KDateTimeWidget" ) ) {
00118           KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() );
00119           wdg->setDateTime( QDateTime::fromString( value, Qt::ISODate ) );
00120         } else if ( it.data()->inherits( "KDatePicker" ) ) {
00121           KDatePicker *wdg = static_cast<KDatePicker*>( it.data() );
00122           wdg->setDate( QDate::fromString( value, Qt::ISODate ) );
00123         } else if ( it.data()->inherits( "QComboBox" ) ) {
00124           QComboBox *wdg = static_cast<QComboBox*>( it.data() );
00125           wdg->setCurrentText( value );
00126         } else if ( it.data()->inherits( "QTextEdit" ) ) {
00127           QTextEdit *wdg = static_cast<QTextEdit*>( it.data() );
00128           wdg->setText( value );
00129         }
00130       }
00131     }
00132   }
00133 }
00134 
00135 void AdvancedCustomFields::storeContact( KABC::Addressee *addr )
00136 {
00137   QMap<QString, QWidget*>::Iterator it;
00138   for ( it = mWidgets.begin(); it != mWidgets.end(); ++it ) {
00139     QString value;
00140     if ( it.data()->inherits( "QLineEdit" ) ) {
00141       QLineEdit *wdg = static_cast<QLineEdit*>( it.data() );
00142       value = wdg->text();
00143     } else if ( it.data()->inherits( "QSpinBox" ) ) {
00144       QSpinBox *wdg = static_cast<QSpinBox*>( it.data() );
00145       value = QString::number( wdg->value() );
00146     } else if ( it.data()->inherits( "QCheckBox" ) ) {
00147       QCheckBox *wdg = static_cast<QCheckBox*>( it.data() );
00148       value = ( wdg->isChecked() ? "true" : "false" );
00149     } else if ( it.data()->inherits( "QDateTimeEdit" ) ) {
00150       QDateTimeEdit *wdg = static_cast<QDateTimeEdit*>( it.data() );
00151       value = wdg->dateTime().toString( Qt::ISODate );
00152     } else if ( it.data()->inherits( "KDateTimeWidget" ) ) {
00153       KDateTimeWidget *wdg = static_cast<KDateTimeWidget*>( it.data() );
00154       value = wdg->dateTime().toString( Qt::ISODate );
00155     } else if ( it.data()->inherits( "KDatePicker" ) ) {
00156       KDatePicker *wdg = static_cast<KDatePicker*>( it.data() );
00157       value = wdg->date().toString( Qt::ISODate );
00158     } else if ( it.data()->inherits( "QComboBox" ) ) {
00159       QComboBox *wdg = static_cast<QComboBox*>( it.data() );
00160       value = wdg->currentText();
00161     } else if ( it.data()->inherits( "QTextEdit" ) ) {
00162       QTextEdit *wdg = static_cast<QTextEdit*>( it.data() );
00163       value = wdg->text();
00164    }
00165 
00166     QString ns;
00167     if ( (mIdentifier.upper() == "KADDRESSBOOK") ||
00168          (QRegExp( "^Form\\d\\d?$").search(mIdentifier) >= 0 )
00169        )
00170       ns = "KADDRESSBOOK";
00171     else 
00172       ns = mIdentifier;
00173 
00174     if ( value.isEmpty() )
00175       addr->removeCustom( ns.latin1(), it.key() );
00176     else
00177       addr->insertCustom( ns.latin1(), it.key(), value );
00178   }
00179 }
00180 
00181 void AdvancedCustomFields::setReadOnly( bool readOnly )
00182 {
00183   QMap<QString, QWidget*>::Iterator it;
00184   for ( it = mWidgets.begin(); it != mWidgets.end(); ++it )
00185     if ( mDisabledWidgets.find( it.data() ) == mDisabledWidgets.end() )
00186       it.data()->setEnabled( !readOnly );
00187 }
00188 
00189 void AdvancedCustomFields::initGUI( const QString &uiFile )
00190 {
00191   QVBoxLayout *layout = new QVBoxLayout( this, KDialog::marginHint(),
00192                                          KDialog::spacingHint() );
00193 
00194   QWidget *wdg = QWidgetFactory::create( uiFile, 0, this );
00195   if ( !wdg ) {
00196     kdError() << "No ui file found" << endl;
00197     return;
00198   }
00199 
00200   mTitle = wdg->caption();
00201   mIdentifier = wdg->name();
00202 
00203   layout->addWidget( wdg );
00204 
00205   QObjectList *list = wdg->queryList( "QWidget" );
00206   QObjectListIt it( *list );
00207 
00208   QStringList allowedTypes;
00209   allowedTypes << "QLineEdit"
00210                << "QTextEdit"
00211                << "QSpinBox"
00212                << "QCheckBox"
00213                << "QComboBox"
00214                << "QDateTimeEdit"
00215                << "KLineEdit"
00216                << "KDateTimeWidget"
00217                << "KDatePicker";
00218 
00219   while ( it.current() ) {
00220     if ( allowedTypes.contains( it.current()->className() ) ) {
00221       QString name = it.current()->name();
00222       if ( name.startsWith( "X_" ) ) {
00223         name = name.mid( 2 );
00224 
00225         QWidget *widget = static_cast<QWidget*>( it.current() );
00226         if ( !name.isEmpty() )
00227           mWidgets.insert( name, widget );
00228 
00229         if ( it.current()->inherits( "QLineEdit" ) )
00230           connect( it.current(), SIGNAL( textChanged( const QString& ) ),
00231                    this, SLOT( setModified() ) );
00232         else if ( it.current()->inherits( "QSpinBox" ) )
00233           connect( it.current(), SIGNAL( valueChanged( int ) ),
00234                    this, SLOT( setModified() ) );
00235         else if ( it.current()->inherits( "QCheckBox" ) )
00236           connect( it.current(), SIGNAL( toggled( bool ) ),
00237                    this, SLOT( setModified() ) );
00238         else if ( it.current()->inherits( "QComboBox" ) )
00239           connect( it.current(), SIGNAL( activated( const QString& ) ),
00240                    this, SLOT( setModified() ) );
00241         else if ( it.current()->inherits( "QDateTimeEdit" ) )
00242           connect( it.current(), SIGNAL( valueChanged( const QDateTime& ) ),
00243                    this, SLOT( setModified() ) );
00244         else if ( it.current()->inherits( "KDateTimeWidget" ) )
00245           connect( it.current(), SIGNAL( valueChanged( const QDateTime& ) ),
00246                    this, SLOT( setModified() ) );
00247         else if ( it.current()->inherits( "KDatePicker" ) )
00248           connect( it.current(), SIGNAL( dateChanged( QDate ) ),
00249                    this, SLOT( setModified() ) );
00250         else if ( it.current()->inherits( "QTextEdit" ) )
00251           connect( it.current(), SIGNAL( textChanged() ),
00252                    this, SLOT( setModified() ) );
00253 
00254         if ( !widget->isEnabled() )
00255           mDisabledWidgets.append( widget );
00256       }
00257     }
00258 
00259     ++it;
00260   }
00261 
00262   delete list;
00263 }
00264 
00265 QString AdvancedCustomFields::pageIdentifier() const
00266 {
00267   return mIdentifier;
00268 }
00269 
00270 QString AdvancedCustomFields::pageTitle() const
00271 {
00272   return mTitle;
00273 }
00274 
00275 #include "advancedcustomfields.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Aug 2 09:54:29 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003