• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kopete/kopete

  • sources
  • kde-4.12
  • kdenetwork
  • kopete
  • kopete
  • contactlist
kopeteaddrbookexport.cpp
Go to the documentation of this file.
1 /*
2  kopeteaddrbookexport.cpp - Kopete Online Status
3 
4  Logic for exporting data acquired from messaging systems to the
5  KDE address book
6 
7  Copyright (c) 2004 by Will Stephenson <wstephenson@kde.org>
8 
9  Kopete (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
10 
11  *************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  *************************************************************************
19 */
20 
21 #include <kabc/phonenumber.h>
22 #include <qcombobox.h>
23 #include <qlabel.h>
24 #include <QPixmap>
25 
26 #include <kdialog.h>
27 #include <kiconloader.h>
28 #include <k3listbox.h>
29 #include <klocale.h>
30 
31 #include "kopeteaccount.h"
32 #include "kopeteglobal.h"
33 #include "kopetemetacontact.h"
34 #include "kopetecontact.h"
35 
36 #include "kopeteaddrbookexport.h"
37 
38 KopeteAddressBookExport::KopeteAddressBookExport( QWidget *parent, Kopete::MetaContact *mc ) : QObject( parent ), Ui::AddressBookExportUI()
39 {
40  // instantiate dialog and populate widgets
41  mParent = parent;
42  mAddressBook = KABC::StdAddressBook::self();
43  mMetaContact = mc;
44 }
45 
46 KopeteAddressBookExport::~KopeteAddressBookExport()
47 {
48 
49 }
50 
51 void KopeteAddressBookExport::initLabels()
52 {
53  if ( !mAddressee.isEmpty() )
54  {
55  mLblFirstName->setText( mAddressee.givenNameLabel() );
56  mLblLastName->setText( mAddressee.familyNameLabel() );
57  mLblEmail->setText( mAddressee.emailLabel() );
58  mLblUrl->setText( mAddressee.urlLabel() );
59  mLblHomePhone->setText( mAddressee.homePhoneLabel() );
60  mLblWorkPhone->setText( mAddressee.businessPhoneLabel() );
61  mLblMobilePhone->setText( mAddressee.mobilePhoneLabel() );
62  }
63 }
64 
65 void KopeteAddressBookExport::fetchKABCData()
66 {
67  if ( !mAddressee.isEmpty() )
68  {
69  mAddrBookIcon = SmallIcon( "office-address-book" );
70 
71  // given name
72  QString given = mAddressee.givenName();
73  if ( !given.isEmpty() )
74  mFirstName->addItem( QIcon(mAddrBookIcon), given );
75  else
76  mFirstName->addItem( QIcon(mAddrBookIcon), i18n("<Not Set>") );
77 
78  // family name
79  QString family = mAddressee.familyName();
80  if ( !family.isEmpty() )
81  mLastName->addItem( QIcon(mAddrBookIcon), family );
82  else
83  mLastName->addItem( QIcon(mAddrBookIcon), i18n("<Not Set>") );
84 
85  // url
86  QString url = mAddressee.url().url();
87  if ( !url.isEmpty() )
88  mUrl->addItem( QIcon(mAddrBookIcon), url );
89  else
90  mUrl->addItem( QIcon(mAddrBookIcon), i18n("<Not Set>") );
91 
92  // emails
93  QStringList emails = mAddressee.emails();
94  numEmails = emails.count();
95  for ( QStringList::Iterator it = emails.begin(); it != emails.end(); ++it )
96  mEmails->insertItem( mAddrBookIcon, *it );
97  if ( numEmails == 0 )
98  {
99  mEmails->insertItem( mAddrBookIcon, i18n("<Not Set>") );
100  numEmails = 1;
101  }
102 
103  // phone numbers
104  fetchPhoneNumbers( mHomePhones, KABC::PhoneNumber::Home, numHomePhones );
105  fetchPhoneNumbers( mWorkPhones, KABC::PhoneNumber::Work, numWorkPhones );
106  fetchPhoneNumbers( mMobilePhones, KABC::PhoneNumber::Cell, numMobilePhones );
107  }
108 }
109 
110 void KopeteAddressBookExport::fetchPhoneNumbers( K3ListBox * listBox, KABC::PhoneNumber::Type type, uint& counter )
111 {
112  KABC::PhoneNumber::List phones = mAddressee.phoneNumbers( type );
113  counter = phones.count();
114  KABC::PhoneNumber::List::Iterator it;
115  for ( it = phones.begin(); it != phones.end(); ++it )
116  listBox->insertItem( mAddrBookIcon, (*it).number() );
117  if ( counter == 0 )
118  {
119  listBox->insertItem( mAddrBookIcon, i18n("<Not Set>") );
120  counter = 1;
121  }
122 }
123 
124 void KopeteAddressBookExport::fetchIMData()
125 {
126  QList<Kopete::Contact*> contacts = mMetaContact->contacts();
127  QList<Kopete::Contact*>::iterator cit, citEnd = contacts.end();
128  for( cit = contacts.begin(); cit != citEnd; ++cit )
129  {
130  // for each contact, get the property content
131  Kopete::Contact* c = (*cit);
132  QPixmap contactIcon = c->account()->accountIcon( 16 );
133  // given name
134  populateIM( c, contactIcon, mFirstName, Kopete::Global::Properties::self()->firstName() );
135  // family name
136  populateIM( c, contactIcon, mLastName, Kopete::Global::Properties::self()->lastName() );
137  // url
138  // TODO: make URL/homepage a global template, currently only in IRC channel contact
139  // emails
140  populateIM( c, contactIcon, mEmails, Kopete::Global::Properties::self()->emailAddress() );
141  // home phone
142  populateIM( c, contactIcon, mHomePhones, Kopete::Global::Properties::self()->privatePhone() );
143  // work phone
144  populateIM( c, contactIcon, mWorkPhones, Kopete::Global::Properties::self()->workPhone() );
145  // mobile phone
146  populateIM( c, contactIcon, mMobilePhones, Kopete::Global::Properties::self()->privateMobilePhone() );
147  }
148 }
149 
150 void KopeteAddressBookExport::populateIM( const Kopete::Contact *contact, const QPixmap &icon, QComboBox *combo, const Kopete::PropertyTmpl &property )
151 {
152  Kopete::Property prop = contact->property( property );
153  if ( !prop.isNull() )
154  {
155  combo->addItem( QIcon(icon), prop.value().toString() );
156  }
157 }
158 
159 void KopeteAddressBookExport::populateIM( const Kopete::Contact *contact, const QPixmap &icon, K3ListBox *listBox, const Kopete::PropertyTmpl &property )
160 {
161  Kopete::Property prop = contact->property( property );
162  if ( !prop.isNull() )
163  {
164  listBox->insertItem( icon, prop.value().toString() );
165  }
166 }
167 
168 int KopeteAddressBookExport::showDialog()
169 {
170  mAddressee = mAddressBook->findByUid( mMetaContact->kabcId() );
171  if ( !mAddressee.isEmpty() )
172  {
173  numEmails = 0;
174  numHomePhones = 0;
175  numWorkPhones = 0;
176  numMobilePhones = 0;
177  mDialog = new KDialog( mParent );
178  mDialog->setCaption( i18n("Export to Address Book") );
179  mDialog->setButtons( KDialog::Ok|KDialog::Cancel );
180 
181  QWidget* w = new QWidget( mDialog );
182  setupUi( w );
183  mDialog->setMainWidget( w );
184  mDialog->setButtonGuiItem( KDialog::Ok, KGuiItem( i18n( "Export" ),
185  QString(), i18n( "Set address book fields using the selected data from Kopete" ) ) );
186 
187  initLabels();
188  // fetch existing data from kabc
189  fetchKABCData();
190  // fetch data from contacts
191  fetchIMData();
192 
193  return mDialog->exec();
194  }
195  else
196  return QDialog::Rejected;
197 }
198 
199 void KopeteAddressBookExport::exportData()
200 {
201  // write the data from the widget to KABC
202  // update the Addressee
203  // first name
204  bool dirty = false;
205  if ( newValue( mFirstName ) )
206  {
207  dirty = true;
208  mAddressee.setGivenName( mFirstName->currentText() );
209  }
210  // last name
211  if ( newValue( mLastName ) )
212  {
213  dirty = true;
214  mAddressee.setFamilyName( mLastName->currentText() );
215  }
216  // url
217  if ( newValue( mUrl ) )
218  {
219  dirty = true;
220  mAddressee.setUrl( KUrl( mUrl->currentText() ) );
221  }
222 
223  QStringList newVals;
224  // email
225  newVals = newValues( mEmails, numEmails );
226  for ( QStringList::Iterator it = newVals.begin(); it != newVals.end(); ++it )
227  {
228  dirty = true;
229  mAddressee.insertEmail( *it );
230  }
231  // home phone
232  newVals = newValues( mHomePhones, numHomePhones );
233  for ( QStringList::Iterator it = newVals.begin(); it != newVals.end(); ++it )
234  {
235  dirty = true;
236  mAddressee.insertPhoneNumber( KABC::PhoneNumber( *it, KABC::PhoneNumber::Home ) );
237  }
238  // work phone
239  newVals = newValues( mWorkPhones, numWorkPhones );
240  for ( QStringList::Iterator it = newVals.begin(); it != newVals.end(); ++it )
241  {
242  dirty = true;
243  mAddressee.insertPhoneNumber( KABC::PhoneNumber( *it, KABC::PhoneNumber::Work ) );
244  }
245  // mobile
246  newVals = newValues( mMobilePhones, numMobilePhones );
247  for ( QStringList::Iterator it = newVals.begin(); it != newVals.end(); ++it )
248  {
249  dirty = true;
250  mAddressee.insertPhoneNumber( KABC::PhoneNumber( *it, KABC::PhoneNumber::Cell ) );
251  }
252 
253  if ( dirty )
254  {
255  // write the changed addressbook
256  mAddressBook->insertAddressee( mAddressee );
257 
258  KABC::Ticket *ticket = mAddressBook->requestSaveTicket();
259  if ( !ticket )
260  kWarning( 14000 ) << "WARNING: Resource is locked by other application!";
261  else
262  {
263  if ( !mAddressBook->save( ticket ) )
264  {
265  kWarning( 14000 ) << "ERROR: Saving failed!";
266  mAddressBook->releaseSaveTicket( ticket );
267  }
268  }
269  kDebug( 14000 ) << "Finished writing KABC";
270  }
271 }
272 
273 bool KopeteAddressBookExport::newValue( QComboBox *combo )
274 {
275  // all data in position 0 is from KABC, so if position 0 is selected,
276  // or if the selection is the same as the data at 0, return false
277  return !( combo->currentIndex() == 0 ||
278  ( combo->itemText( combo->currentIndex() ) == combo->itemText( 0 ) ) );
279 }
280 
281 QStringList KopeteAddressBookExport::newValues( K3ListBox *listBox, uint counter )
282 {
283  QStringList newValues;
284  // need to iterate all items except those from KABC and check if selected and not same as the first
285  // counter is the number of KABC items, and hence the index of the first non KABC item
286  for ( uint i = counter; i < listBox->count(); ++i )
287  {
288  if ( listBox->isSelected( i ) )
289  {
290  // check whether it matches any KABC item
291  bool duplicate = false;
292  for ( uint j = 0; j < counter; ++j )
293  {
294  if ( listBox->text( i ) == listBox->text( j ) )
295  duplicate = true;
296  }
297  if ( !duplicate )
298  newValues.append( listBox->text( i ) );
299  }
300  }
301  return newValues;
302 }
KopeteAddressBookExport::exportData
void exportData()
Export the data to KABC if changed, omitting any duplicates.
Definition: kopeteaddrbookexport.cpp:199
KopeteAddressBookExport::mAddressBook
KABC::AddressBook * mAddressBook
Definition: kopeteaddrbookexport.h:96
KopeteAddressBookExport::mDialog
KDialog * mDialog
Definition: kopeteaddrbookexport.h:92
KopeteAddressBookExport::numEmails
uint numEmails
Definition: kopeteaddrbookexport.h:100
QWidget
KopeteAddressBookExport::newValue
bool newValue(QComboBox *combo)
Check the selected item is not the first (existing KABC) item, or the same as it. ...
Definition: kopeteaddrbookexport.cpp:273
KopeteAddressBookExport::mParent
QWidget * mParent
Definition: kopeteaddrbookexport.h:91
KopeteAddressBookExport::KopeteAddressBookExport
KopeteAddressBookExport(QWidget *parent, Kopete::MetaContact *mc)
Definition: kopeteaddrbookexport.cpp:38
KDialog
KopeteAddressBookExport::showDialog
int showDialog()
Display the dialog.
Definition: kopeteaddrbookexport.cpp:168
QObject
KopeteAddressBookExport::fetchPhoneNumbers
void fetchPhoneNumbers(K3ListBox *listBox, KABC::PhoneNumber::Type type, uint &counter)
Populate a listbox with a given type of phone number.
Definition: kopeteaddrbookexport.cpp:110
Kopete::Items::Type
Type
Definition: kopeteitembase.h:57
KopeteAddressBookExport::numMobilePhones
uint numMobilePhones
Definition: kopeteaddrbookexport.h:100
kopeteaddrbookexport.h
Kopete::Items::MetaContact
Definition: kopeteitembase.h:57
KopeteAddressBookExport::populateIM
void populateIM(const Kopete::Contact *contact, const QPixmap &icon, QComboBox *combo, const Kopete::PropertyTmpl &property)
Populate a combobox with a contact's IM data.
Definition: kopeteaddrbookexport.cpp:150
KopeteAddressBookExport::fetchKABCData
void fetchKABCData()
Populate the GUI with data from KABC.
Definition: kopeteaddrbookexport.cpp:65
KopeteAddressBookExport::initLabels
void initLabels()
Initialise the GUI labels with labels from KABC.
Definition: kopeteaddrbookexport.cpp:51
KopeteAddressBookExport::newValues
QStringList newValues(K3ListBox *listBox, uint counter)
Definition: kopeteaddrbookexport.cpp:281
KopeteAddressBookExport::numHomePhones
uint numHomePhones
Definition: kopeteaddrbookexport.h:100
KopeteAddressBookExport::mMetaContact
Kopete::MetaContact * mMetaContact
Definition: kopeteaddrbookexport.h:95
KopeteAddressBookExport::~KopeteAddressBookExport
~KopeteAddressBookExport()
Definition: kopeteaddrbookexport.cpp:46
KopeteAddressBookExport::fetchIMData
void fetchIMData()
Populate the GUI with data from IM systems.
Definition: kopeteaddrbookexport.cpp:124
KopeteAddressBookExport::numWorkPhones
uint numWorkPhones
Definition: kopeteaddrbookexport.h:100
KopeteAddressBookExport::mAddrBookIcon
QPixmap mAddrBookIcon
Definition: kopeteaddrbookexport.h:93
KopeteAddressBookExport::mAddressee
KABC::Addressee mAddressee
Definition: kopeteaddrbookexport.h:97
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/kopete

Skip menu "kopete/kopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal