• 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
kabcexport.cpp
Go to the documentation of this file.
1 /*
2  kabcexport.cpp - Export Contacts to Address Book Wizard for Kopete
3 
4  Copyright (c) 2005 by Will Stephenson <will@stevello.free-online.co.uk>
5  Resource selector taken from KRES::SelectDialog
6  Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
7  Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org>
8  Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
9 
10  Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>
11 
12  *************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  *************************************************************************
20 */
21 
22 #include <qpushbutton.h>
23 #include <qmap.h>
24 
25 #include <klocale.h>
26 #include <kmessagebox.h>
27 #include <kabc/addressee.h>
28 #include <kabc/addressbook.h>
29 #include <kabc/phonenumber.h>
30 #include <kabc/picture.h>
31 #include <kabc/resource.h>
32 #include <kabc/stdaddressbook.h>
33 
34 #include <kabcpersistence.h>
35 #include <kopetecontact.h>
36 #include <kopetecontactlist.h>
37 #include <kopeteproperty.h>
38 #include <kopeteglobal.h>
39 #include <kopetemetacontact.h>
40 #include <kopetepicture.h>
41 
42 #include "kabcexport.h"
43 
44 class ContactLVI : public QListWidgetItem
45 {
46  public:
47  ContactLVI ( Kopete::MetaContact * mc, QListWidget * parent, const QString & text, QListWidgetItem::ItemType tt = Type ) : QListWidgetItem( text,parent, tt ), mc( mc )
48  {
49 
50  }
51  Kopete::MetaContact * mc;
52  QString uid;
53 };
54 
55 // ctor populates the resource list and contact list, and enables the next button on the first page
56 KabcExportWizard::KabcExportWizard( QWidget *parent )
57  : KAssistantDialog(parent)
58 {
59  QWidget *page1Widget=new QWidget(this);
60  m_page1.setupUi(page1Widget);
61  m_page1WidgetItem=addPage(page1Widget,i18n("Select Address Book"));
62  QWidget *page2Widget=new QWidget(this);
63  m_page2.setupUi(page2Widget);
64  m_page2WidgetItem=addPage(page2Widget,i18n("Select Contact"));
65 
66 
67  connect( m_page1.addrBooks, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
68  SLOT(slotResourceSelectionChanged(QListWidgetItem*)));
69 
70  connect( m_page2.btnSelectAll, SIGNAL(clicked()), SLOT(slotSelectAll()) );
71  connect( m_page2.btnDeselectAll, SIGNAL(clicked()), SLOT(slotDeselectAll()) );
72 
73  // fill resource selector
74  m_addressBook = Kopete::KABCPersistence::self()->addressBook();
75 
76  QList<KABC::Resource*> kabcResources = m_addressBook->resources();
77 
78  QListIterator<KABC::Resource*> resIt( kabcResources );
79  KABC::Resource *resource;
80 
81  uint counter = 0;
82  while ( resIt.hasNext() )
83  {
84  resource = resIt.next();
85  if ( !resource->readOnly() )
86  {
87  m_resourceMap.insert( counter, resource );
88  m_page1.addrBooks->addItem( resource->resourceName() );
89  counter++;
90  }
91  }
92 
93  setValid(m_page1WidgetItem,false);
94 
95  // if there were no writable address books, tell the user
96  if ( counter == 0 )
97  {
98  m_page1.addrBooks->addItem( i18n( "No writeable address book resource found." ) );
99  m_page1.addrBooks->addItem( i18n( "Add or enable one using the KDE System Settings." ) );
100  m_page1.addrBooks->setEnabled( false );
101  }
102 
103  if ( m_page1.addrBooks->count() == 1 )
104  m_page1.addrBooks->setCurrentRow( 0 );
105 
106  // fill contact list
107  QList<Kopete::MetaContact*> contacts = Kopete::ContactList::self()->metaContacts();
108  QList<Kopete::MetaContact*>::iterator it, itEnd = contacts.end();
109  counter = 0;
110  QString alreadyIn = i18n( " (already in address book)" );
111  for ( it = contacts.begin(); it != itEnd; ++it)
112  {
113  Kopete::MetaContact* mc = (*it);
114  m_contactMap.insert( counter, mc );
115  QListWidgetItem * lvi = new ContactLVI( mc, m_page2.contactList,
116  mc->displayName() );
117  lvi->setCheckState( Qt::Unchecked );
118  const QString &kabcId = mc->kabcId();
119  if ( kabcId.isEmpty() || m_addressBook->findByUid(kabcId).isEmpty() )
120  {
121  lvi->setCheckState( Qt::Checked );
122  lvi->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
123  }
124  else
125  {
126  lvi->setText( lvi->text() + alreadyIn );
127  lvi->setFlags( 0 );
128  }
129  }
130 }
131 
132 KabcExportWizard::~KabcExportWizard()
133 {
134 
135 }
136 
137 void KabcExportWizard::slotDeselectAll()
138 {
139  for(int i=0;i<m_page2.contactList->count();i++)
140  {
141  ContactLVI *item = static_cast<ContactLVI *>( m_page2.contactList->item(i) );
142  item->setCheckState( Qt::Unchecked );
143  }
144 }
145 
146 void KabcExportWizard::slotSelectAll()
147 {
148  for(int i=0;i<m_page2.contactList->count();i++)
149  {
150  ContactLVI *item = static_cast<ContactLVI *>( m_page2.contactList->item(i) );
151  if ( item->flags() & Qt::ItemIsEnabled)
152  item->setCheckState( Qt::Checked );
153  }
154 }
155 
156 void KabcExportWizard::slotResourceSelectionChanged( QListWidgetItem * lbi )
157 {
158  setValid( m_page1WidgetItem,true );
159  Q_UNUSED(lbi);
160 }
161 
162 // accept runs the export algorithm
163 void KabcExportWizard::accept()
164 {
165  // first add an addressee to the selected resource
166  // then set the metacontactId of each MC to that of the new addressee
167  KABC::Resource * selectedResource =
168  m_resourceMap[ ( m_page1.addrBooks->currentRow() ) ];
169  // for each item checked
170  {
171  for(int i=0;i<m_page2.contactList->count();i++)
172  {
173  ContactLVI *item = static_cast<ContactLVI *>( m_page2.contactList->item(i) );
174  // if it is checked and enabled
175  if ( item->flags() & Qt::ItemIsEnabled && item->checkState() & Qt::Checked)
176  {
177  KABC::Addressee addr;
178  addr = m_addressBook->findByUid( item->mc->kabcId() );
179  if ( addr.isEmpty() ) // unassociated contact
180  {
181  kDebug( 14000 ) << "creating addressee " << item->mc->displayName() << " in address book " << selectedResource->resourceName();
182  // create a new addressee in the selected resource
183  addr.setResource( selectedResource );
184 
185  // set name
186  QList<Kopete::Contact*> contacts = item->mc->contacts();
187  if ( contacts.count() == 1 )
188  {
189  Kopete::Property prop;
190  prop = contacts.first()->property(
191  Kopete::Global::Properties::self()->fullName() );
192  if ( prop.isNull() )
193  addr.setNameFromString( item->mc->displayName() );
194  else
195  addr.setNameFromString( prop.value().toString() );
196  }
197  else
198  addr.setNameFromString( item->mc->displayName() );
199 
200  // set details
201  exportDetails( item->mc, addr );
202  m_addressBook->insertAddressee( addr );
203  // set the metacontact's id to that of the new addressee
204  // - this causes the addressbook to be written by libkopete
205  item->mc->setKabcId( addr.uid() );
206  }
207  else
208  {
209  exportDetails( item->mc, addr );
210  m_addressBook->insertAddressee( addr );
211  }
212  }
213  }
214  }
215  // request a write in case we only changed details on existing linked addressee
216  Kopete::KABCPersistence::self()->writeAddressBook( selectedResource );
217  QDialog::accept();
218 }
219 
220 void KabcExportWizard::exportDetails( Kopete::MetaContact * mc, KABC::Addressee & addr )
221 {
222  QList<Kopete::Contact*> contacts = mc->contacts();
223  QList<Kopete::Contact*>::iterator cit, citEnd = contacts.begin();
224  for( cit = contacts.begin(); cit != citEnd; ++cit )
225  {
226  Kopete::Property prop;
227  prop = (*cit)->property( Kopete::Global::Properties::self()->emailAddress() );
228  if ( !prop.isNull() )
229  {
230  addr.insertEmail( prop.value().toString() );
231  }
232  prop = (*cit)->property( Kopete::Global::Properties::self()->privatePhone() );
233  if ( !prop.isNull() )
234  {
235  addr.insertPhoneNumber( KABC::PhoneNumber( prop.value().toString(), KABC::PhoneNumber::Home ) );
236  }
237  prop = (*cit)->property( Kopete::Global::Properties::self()->workPhone() );
238  if ( !prop.isNull() )
239  {
240  addr.insertPhoneNumber( KABC::PhoneNumber( prop.value().toString(), KABC::PhoneNumber::Work ) );
241  }
242  prop = (*cit)->property( Kopete::Global::Properties::self()->privateMobilePhone() );
243  if ( !prop.isNull() )
244  {
245  addr.insertPhoneNumber( KABC::PhoneNumber( prop.value().toString(), KABC::PhoneNumber::Cell ) );
246  }
247 
248  }
249 
250  if( !mc->picture().isNull() )
251  {
252  QImage photo = mc->picture().image();
253  addr.setPhoto( KABC::Picture( photo ) );
254  }
255 }
256 
257 #include "kabcexport.moc"
KabcExportWizard::~KabcExportWizard
~KabcExportWizard()
Definition: kabcexport.cpp:132
QWidget
KabcExportWizard::KabcExportWizard
KabcExportWizard(QWidget *parent=0)
Definition: kabcexport.cpp:56
kabcexport.h
KabcExportWizard::slotResourceSelectionChanged
void slotResourceSelectionChanged(QListWidgetItem *lbi)
Definition: kabcexport.cpp:156
Kopete::Items::Type
Type
Definition: kopeteitembase.h:57
KabcExportWizard::accept
void accept()
Definition: kabcexport.cpp:163
KabcExportWizard::exportDetails
void exportDetails(Kopete::MetaContact *mc, KABC::Addressee &addr)
Definition: kabcexport.cpp:220
KabcExportWizard::slotSelectAll
void slotSelectAll()
Definition: kabcexport.cpp:146
KAssistantDialog
Kopete::Items::MetaContact
Definition: kopeteitembase.h:57
QListWidgetItem
KabcExportWizard::slotDeselectAll
void slotDeselectAll()
Definition: kabcexport.cpp:137
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