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