22 #include "imeditwidget.h"
23 #include "customfieldseditwidget.h"
25 #include "im/imeditordialog.h"
26 #include "im/improtocols.h"
28 #include <QtCore/QPointer>
29 #include <QHBoxLayout>
30 #include <QToolButton>
32 #include <kabc/addressee.h>
33 #include <klineedit.h>
34 #include <klocalizedstring.h>
36 IMEditWidget::IMEditWidget( QWidget *parent )
39 QHBoxLayout *layout =
new QHBoxLayout(
this );
40 layout->setMargin( 0 );
42 mIMEdit =
new KLineEdit;
43 layout->addWidget( mIMEdit );
45 mEditButton =
new QToolButton;
46 mEditButton->setText( i18n(
"..." ) );
47 layout->addWidget( mEditButton );
48 setFocusProxy( mEditButton );
49 setFocusPolicy( Qt::StrongFocus );
51 connect( mEditButton, SIGNAL(clicked()), SLOT(edit()) );
54 IMEditWidget::~IMEditWidget()
58 void IMEditWidget::loadContact(
const KABC::Addressee &contact )
60 mIMEdit->setText( contact.custom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"X-IMAddress" ) ) );
62 const QStringList customs = contact.customs();
64 foreach (
const QString &custom, customs ) {
65 QString app, name, value;
66 splitCustomField( custom, app, name, value );
68 if ( app.startsWith( QLatin1String(
"messaging/" ) ) ) {
69 if ( name == QLatin1String(
"All" ) ) {
70 const QString protocol = app;
71 const QStringList names = value.split( QChar( 0xE000 ), QString::SkipEmptyParts );
73 foreach (
const QString &name, names ) {
74 mIMAddresses << IMAddress( protocol, name, ( name == mIMEdit->text() ) );
81 void IMEditWidget::storeContact( KABC::Addressee &contact )
const
83 if ( !mIMEdit->text().isEmpty() ) {
84 contact.insertCustom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"X-IMAddress" ), mIMEdit->text() );
86 contact.removeCustom( QLatin1String(
"KADDRESSBOOK" ), QLatin1String(
"X-IMAddress" ) );
90 QMap<QString, QStringList> protocolMap;
93 foreach (
const QString &protocol, IMProtocols::self()->protocols() ) {
94 protocolMap.insert( protocol, QStringList() );
98 foreach (
const IMAddress &address, mIMAddresses ) {
99 protocolMap[ address.protocol() ].append( address.name() );
103 QMapIterator<QString, QStringList> it( protocolMap );
104 while ( it.hasNext() ) {
107 if ( !it.value().isEmpty() ) {
108 contact.insertCustom( it.key(), QLatin1String(
"All" ), it.value().join( QString( 0xE000 ) ) );
110 contact.removeCustom( it.key(), QLatin1String(
"All" ) );
115 void IMEditWidget::setReadOnly(
bool readOnly )
117 mIMEdit->setReadOnly( readOnly );
118 mEditButton->setEnabled( !readOnly );
121 void IMEditWidget::edit()
123 QPointer<IMEditorDialog> dlg =
new IMEditorDialog(
this );
124 dlg->setAddresses( mIMAddresses );
126 if ( dlg->exec() == QDialog::Accepted ) {
127 mIMAddresses = dlg->addresses();
129 foreach (
const IMAddress &address, mIMAddresses ) {
130 if ( address.preferred() ) {
131 mIMEdit->setText( address.name() );