kopete/kopete
kabcexport.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <qpushbutton.h>
00023 #include <qmap.h>
00024
00025 #include <klocale.h>
00026 #include <kmessagebox.h>
00027 #include <kabc/addressee.h>
00028 #include <kabc/addressbook.h>
00029 #include <kabc/phonenumber.h>
00030 #include <kabc/picture.h>
00031 #include <kabc/resource.h>
00032 #include <kabc/stdaddressbook.h>
00033
00034 #include <kabcpersistence.h>
00035 #include <kopetecontact.h>
00036 #include <kopetecontactlist.h>
00037 #include <kopeteproperty.h>
00038 #include <kopeteglobal.h>
00039 #include <kopetemetacontact.h>
00040 #include <kopetepicture.h>
00041
00042 #include "kabcexport.h"
00043
00044 class ContactLVI : public QListWidgetItem
00045 {
00046 public:
00047 ContactLVI ( Kopete::MetaContact * mc, QListWidget * parent, const QString & text, QListWidgetItem::ItemType tt = Type ) : QListWidgetItem( text,parent, tt ), mc( mc )
00048 {
00049
00050 }
00051 Kopete::MetaContact * mc;
00052 QString uid;
00053 };
00054
00055
00056 KabcExportWizard::KabcExportWizard( QWidget *parent )
00057 : KAssistantDialog(parent)
00058 {
00059 QWidget *page1Widget=new QWidget(this);
00060 m_page1.setupUi(page1Widget);
00061 m_page1WidgetItem=addPage(page1Widget,i18n("Select Address Book"));
00062 QWidget *page2Widget=new QWidget(this);
00063 m_page2.setupUi(page2Widget);
00064 m_page2WidgetItem=addPage(page2Widget,i18n("Select Contact"));
00065
00066
00067 connect( m_page1.addrBooks, SIGNAL( currentItemChanged ( QListWidgetItem * , QListWidgetItem * ) ),
00068 SLOT( slotResourceSelectionChanged( QListWidgetItem * )));
00069
00070 connect( m_page2.btnSelectAll, SIGNAL( clicked() ), SLOT( slotSelectAll() ) );
00071 connect( m_page2.btnDeselectAll, SIGNAL( clicked() ), SLOT( slotDeselectAll() ) );
00072
00073
00074 m_addressBook = Kopete::KABCPersistence::self()->addressBook();
00075
00076 QList<KABC::Resource*> kabcResources = m_addressBook->resources();
00077
00078 QListIterator<KABC::Resource*> resIt( kabcResources );
00079 KABC::Resource *resource;
00080
00081 uint counter = 0;
00082 while ( resIt.hasNext() )
00083 {
00084 resource = resIt.next();
00085 if ( !resource->readOnly() )
00086 {
00087 m_resourceMap.insert( counter, resource );
00088 m_page1.addrBooks->addItem( resource->resourceName() );
00089 counter++;
00090 }
00091 }
00092
00093 setValid(m_page1WidgetItem,false);
00094
00095
00096 if ( counter == 0 )
00097 {
00098 m_page1.addrBooks->addItem( i18n( "No writeable addressbook resource found." ) );
00099 m_page1.addrBooks->addItem( i18n( "Add or enable one using the KDE Control Center." ) );
00100 m_page1.addrBooks->setEnabled( false );
00101 }
00102
00103 if ( m_page1.addrBooks->count() == 1 )
00104 m_page1.addrBooks->setCurrentRow( 0 );
00105
00106
00107 QList<Kopete::MetaContact*> contacts = Kopete::ContactList::self()->metaContacts();
00108 QList<Kopete::MetaContact*>::iterator it, itEnd = contacts.end();
00109 counter = 0;
00110 QString alreadyIn = i18n( " (already in address book)" );
00111 for ( it = contacts.begin(); it != itEnd; ++it)
00112 {
00113 Kopete::MetaContact* mc = (*it);
00114 m_contactMap.insert( counter, mc );
00115 QListWidgetItem * lvi = new ContactLVI( mc, m_page2.contactList,
00116 mc->displayName() );
00117 lvi->setCheckState( Qt::Unchecked );
00118 if ( mc->metaContactId().contains(':') )
00119 {
00120 lvi->setCheckState( Qt::Checked );
00121 lvi->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
00122 }
00123 else
00124 {
00125 lvi->setText( lvi->text() + alreadyIn );
00126 lvi->setFlags( 0 );
00127 }
00128 }
00129 }
00130
00131 KabcExportWizard::~KabcExportWizard()
00132 {
00133
00134 }
00135
00136 void KabcExportWizard::slotDeselectAll()
00137 {
00138 for(int i=0;i<m_page2.contactList->count();i++)
00139 {
00140 ContactLVI *item = static_cast<ContactLVI *>( m_page2.contactList->item(i) );
00141 item->setCheckState( Qt::Unchecked );
00142 }
00143 }
00144
00145 void KabcExportWizard::slotSelectAll()
00146 {
00147 for(int i=0;i<m_page2.contactList->count();i++)
00148 {
00149 ContactLVI *item = static_cast<ContactLVI *>( m_page2.contactList->item(i) );
00150 if ( item->flags() & Qt::ItemIsEnabled)
00151 item->setCheckState( Qt::Checked );
00152 }
00153 }
00154
00155 void KabcExportWizard::slotResourceSelectionChanged( QListWidgetItem * lbi )
00156 {
00157 setValid( m_page1WidgetItem,true );
00158 }
00159
00160
00161 void KabcExportWizard::accept()
00162 {
00163
00164
00165 KABC::Resource * selectedResource =
00166 m_resourceMap[ ( m_page1.addrBooks->currentRow() ) ];
00167
00168 {
00169 for(int i=0;i<m_page2.contactList->count();i++)
00170 {
00171 ContactLVI *item = static_cast<ContactLVI *>( m_page2.contactList->item(i) );
00172
00173 if ( item->flags() & Qt::ItemIsEnabled && item->checkState() & Qt::Checked)
00174 {
00175 KABC::Addressee addr;
00176 addr = m_addressBook->findByUid( item->mc->metaContactId() );
00177 if ( addr.isEmpty() )
00178 {
00179 kDebug( 14000 ) << "creating addressee " << item->mc->displayName() << " in address book " << selectedResource->resourceName();
00180
00181 addr.setResource( selectedResource );
00182
00183
00184 QList<Kopete::Contact*> contacts = item->mc->contacts();
00185 if ( contacts.count() == 1 )
00186 {
00187 Kopete::Property prop;
00188 prop = contacts.first()->property(
00189 Kopete::Global::Properties::self()->fullName() );
00190 if ( prop.isNull() )
00191 addr.setNameFromString( item->mc->displayName() );
00192 else
00193 addr.setNameFromString( prop.value().toString() );
00194 }
00195 else
00196 addr.setNameFromString( item->mc->displayName() );
00197
00198
00199 exportDetails( item->mc, addr );
00200 m_addressBook->insertAddressee( addr );
00201
00202
00203 item->mc->setMetaContactId( addr.uid() );
00204 }
00205 else
00206 {
00207 exportDetails( item->mc, addr );
00208 m_addressBook->insertAddressee( addr );
00209 }
00210 }
00211 }
00212 }
00213
00214 Kopete::KABCPersistence::self()->writeAddressBook( selectedResource );
00215 QDialog::accept();
00216 }
00217
00218 void KabcExportWizard::exportDetails( Kopete::MetaContact * mc, KABC::Addressee & addr )
00219 {
00220 QList<Kopete::Contact*> contacts = mc->contacts();
00221 QList<Kopete::Contact*>::iterator cit, citEnd = contacts.begin();
00222 for( cit = contacts.begin(); cit != citEnd; ++cit )
00223 {
00224 Kopete::Property prop;
00225 prop = (*cit)->property( Kopete::Global::Properties::self()->emailAddress() );
00226 if ( !prop.isNull() )
00227 {
00228 addr.insertEmail( prop.value().toString() );
00229 }
00230 prop = (*cit)->property( Kopete::Global::Properties::self()->privatePhone() );
00231 if ( !prop.isNull() )
00232 {
00233 addr.insertPhoneNumber( KABC::PhoneNumber( prop.value().toString(), KABC::PhoneNumber::Home ) );
00234 }
00235 prop = (*cit)->property( Kopete::Global::Properties::self()->workPhone() );
00236 if ( !prop.isNull() )
00237 {
00238 addr.insertPhoneNumber( KABC::PhoneNumber( prop.value().toString(), KABC::PhoneNumber::Work ) );
00239 }
00240 prop = (*cit)->property( Kopete::Global::Properties::self()->privateMobilePhone() );
00241 if ( !prop.isNull() )
00242 {
00243 addr.insertPhoneNumber( KABC::PhoneNumber( prop.value().toString(), KABC::PhoneNumber::Cell ) );
00244 }
00245
00246 }
00247
00248 if( !mc->picture().isNull() )
00249 {
00250 QImage photo = mc->picture().image();
00251 addr.setPhoto( KABC::Picture( photo ) );
00252 }
00253 }
00254
00255 #include "kabcexport.moc"