00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "imeditwidget.h"
00023 #include "customfieldseditwidget.h"
00024
00025 #include "im/imeditordialog.h"
00026 #include "im/improtocols.h"
00027
00028 #include <QtCore/QPointer>
00029 #include <QtGui/QHBoxLayout>
00030 #include <QtGui/QToolButton>
00031
00032 #include <kabc/addressee.h>
00033 #include <klineedit.h>
00034 #include <klocale.h>
00035
00036
00037 IMEditWidget::IMEditWidget( QWidget *parent )
00038 : QWidget( parent )
00039 {
00040 QHBoxLayout *layout = new QHBoxLayout( this );
00041 layout->setMargin( 0 );
00042
00043 mIMEdit = new KLineEdit;
00044 layout->addWidget( mIMEdit );
00045
00046 mEditButton = new QToolButton;
00047 mEditButton->setText( i18n( "..." ) );
00048 layout->addWidget( mEditButton );
00049
00050 connect( mEditButton, SIGNAL( clicked() ), SLOT( edit() ) );
00051 }
00052
00053 IMEditWidget::~IMEditWidget()
00054 {
00055 }
00056
00057 void IMEditWidget::loadContact( const KABC::Addressee &contact )
00058 {
00059 mIMEdit->setText( contact.custom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "X-IMAddress" ) ) );
00060
00061 const QStringList customs = contact.customs();
00062
00063 foreach ( const QString &custom, customs ) {
00064 QString app, name, value;
00065 splitCustomField( custom, app, name, value );
00066
00067 if ( app.startsWith( QLatin1String( "messaging/" ) ) ) {
00068 if ( name == QLatin1String( "All" ) ) {
00069 const QString protocol = app;
00070 const QStringList names = value.split( QChar( 0xE000 ), QString::SkipEmptyParts );
00071
00072 foreach ( const QString &name, names )
00073 mIMAddresses << IMAddress( protocol, name, (name == mIMEdit->text()) );
00074 }
00075 }
00076 }
00077 }
00078
00079 void IMEditWidget::storeContact( KABC::Addressee &contact ) const
00080 {
00081 if ( !mIMEdit->text().isEmpty() )
00082 contact.insertCustom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "X-IMAddress" ), mIMEdit->text() );
00083 else
00084 contact.removeCustom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "X-IMAddress" ) );
00085
00086
00087 QMap<QString, QStringList> protocolMap;
00088
00089
00090 foreach ( const QString &protocol, IMProtocols::self()->protocols() )
00091 protocolMap.insert( protocol, QStringList() );
00092
00093
00094 foreach ( const IMAddress &address, mIMAddresses )
00095 protocolMap[ address.protocol() ].append( address.name() );
00096
00097
00098 QMapIterator<QString, QStringList> it( protocolMap );
00099 while ( it.hasNext() ) {
00100 it.next();
00101
00102 if ( !it.value().isEmpty() ) {
00103 contact.insertCustom( it.key(), QLatin1String( "All" ), it.value().join( QString( 0xE000 ) ) );
00104 } else {
00105 contact.removeCustom( it.key(), QLatin1String( "All" ) );
00106 }
00107 }
00108 }
00109
00110 void IMEditWidget::setReadOnly( bool readOnly )
00111 {
00112 mIMEdit->setReadOnly( readOnly );
00113 mEditButton->setEnabled( !readOnly );
00114 }
00115
00116 void IMEditWidget::edit()
00117 {
00118 QPointer<IMEditorDialog> dlg = new IMEditorDialog( this );
00119 dlg->setAddresses( mIMAddresses );
00120
00121 if ( dlg->exec() == QDialog::Accepted ) {
00122 mIMAddresses = dlg->addresses();
00123
00124 foreach ( const IMAddress &address, mIMAddresses ) {
00125 if ( address.preferred() ) {
00126 mIMEdit->setText( address.name() );
00127 break;
00128 }
00129 }
00130 }
00131
00132 delete dlg;
00133 }
00134
00135 #include "imeditwidget.moc"