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

kaddressbook

  • sources
  • kde-4.14
  • kdepim
  • kaddressbook
  • merge
  • job
mergecontacts.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014-2015 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "mergecontacts.h"
19 
20 using namespace KABMergeContacts;
21 using namespace KABC;
22 MergeContacts::MergeContacts(const Akonadi::Item::List &items)
23  : mListItem(items)
24 {
25 }
26 
27 MergeContacts::~MergeContacts()
28 {
29 
30 }
31 
32 void MergeContacts::setItems(const Akonadi::Item::List &items)
33 {
34  mListItem = items;
35 }
36 
37 KABC::Addressee MergeContacts::mergedContact(bool excludeConflictPart)
38 {
39  KABC::Addressee newContact;
40  if (mListItem.count() <= 1)
41  return newContact;
42  bool firstAddress = true;
43  Q_FOREACH (const Akonadi::Item &item, mListItem) {
44  if (item.hasPayload<KABC::Addressee>()) {
45  KABC::Addressee address = item.payload<KABC::Addressee>();
46  if (firstAddress) {
47  firstAddress = false;
48  newContact = address;
49  } else {
50  mergeToContact(newContact, address, excludeConflictPart);
51  }
52  }
53  }
54  return newContact;
55 }
56 
57 void MergeContacts::mergeToContact(KABC::Addressee &newContact, const KABC::Addressee &fromContact, bool excludeConflictPart)
58 {
59  // Duplicate notes.
60  const QString fromContactNote = fromContact.note();
61  if (!fromContactNote.isEmpty()) {
62  QString newContactNote = newContact.note();
63  if (!newContactNote.isEmpty()) {
64  newContactNote += QLatin1Char('\n');
65  }
66  newContactNote += fromContactNote;
67  newContact.setNote(newContactNote);
68  }
69  // Duplicate emails
70  const QStringList emails = fromContact.emails();
71  if (!emails.isEmpty()) {
72  QStringList newContactsEmail = newContact.emails();
73  Q_FOREACH(const QString &email, emails) {
74  if (!newContactsEmail.contains(email)) {
75  newContactsEmail.append(email);
76  }
77  }
78  newContact.setEmails(newContactsEmail);
79  }
80  // Merge Categories
81  const QStringList categories = fromContact.categories();
82  if (!categories.isEmpty()) {
83  QStringList newContactsCategories = newContact.categories();
84  Q_FOREACH(const QString &category, categories) {
85  if (!newContactsCategories.contains(category)) {
86  newContactsCategories.append(category);
87  }
88  }
89  newContact.setCategories(newContactsCategories);
90  }
91 
92  // Merge Phone
93  const PhoneNumber::List listPhone = fromContact.phoneNumbers();
94  if (!listPhone.isEmpty()) {
95  PhoneNumber::List newContactsPhone = newContact.phoneNumbers();
96  Q_FOREACH(const PhoneNumber &phone, listPhone) {
97  if (!newContactsPhone.contains(phone)) {
98  newContact.insertPhoneNumber(phone);
99  }
100  }
101  }
102 
103  // Merge Address
104  const Address::List listAddress = fromContact.addresses();
105  if (!listAddress.isEmpty()) {
106  Address::List newContactsAddress = newContact.addresses();
107  Q_FOREACH(const Address &addr, listAddress) {
108  if (!newContactsAddress.contains(addr)) {
109  newContact.insertAddress(addr);
110  }
111  }
112  }
113 
114  if (!excludeConflictPart) {
115  // Merge Name
116  if (newContact.name().isEmpty() && !fromContact.name().isEmpty()) {
117  newContact.setName(fromContact.name());
118  }
119  // Merge organization
120  if (newContact.organization().isEmpty() && !fromContact.organization().isEmpty()) {
121  newContact.setOrganization(fromContact.organization());
122  }
123  // Merge NickName
124  if (newContact.nickName().isEmpty() && !fromContact.nickName().isEmpty()) {
125  newContact.setNickName(fromContact.nickName());
126  }
127  // Merge Title
128  if (newContact.title().isEmpty() && !fromContact.title().isEmpty()) {
129  newContact.setTitle(fromContact.title());
130  }
131  // Merge Departement
132  if (newContact.department().isEmpty() && !fromContact.department().isEmpty()) {
133  newContact.setDepartment(fromContact.department());
134  }
135  // Merge FamilyName
136  if (newContact.familyName().isEmpty() && !fromContact.familyName().isEmpty()) {
137  newContact.setFamilyName(fromContact.familyName());
138  }
139  // Merge HomePage
140  if (newContact.url().isEmpty() && !fromContact.url().isEmpty()) {
141  newContact.setUrl(fromContact.url());
142  }
143  // Merge geo
144  if (newContact.geo().isValid() && !fromContact.geo().isValid()) {
145  newContact.setGeo(fromContact.geo());
146  }
147  // Merge Photo
148  if (newContact.photo().isEmpty() && !fromContact.photo().isEmpty()) {
149  newContact.setPhoto(fromContact.photo());
150  }
151 
152  // Merge Logo
153  if (newContact.logo().isEmpty() && !fromContact.logo().isEmpty()) {
154  newContact.setLogo(fromContact.logo());
155  }
156  // Merge Birthday TODO
157 
158  // Merge Blog
159  mergeCustomValue(fromContact, QLatin1String( "BlogFeed" ), newContact);
160  // Merge profession
161  mergeCustomValue(fromContact, QLatin1String( "X-Profession" ), newContact);
162  // Merge Office
163  mergeCustomValue(fromContact, QLatin1String( "X-Office" ), newContact);
164  // Merge ManagersName
165  mergeCustomValue(fromContact, QLatin1String( "X-ManagersName" ), newContact);
166  // Merge AssistantsName
167  mergeCustomValue(fromContact, QLatin1String( "X-AssistantsName" ), newContact);
168  // Merge SpousesName
169  mergeCustomValue(fromContact, QLatin1String( "X-SpousesName" ), newContact);
170  // Merge Anniversary
171  mergeCustomValue(fromContact, QLatin1String( "X-Anniversary" ), newContact);
172  // Merge Key
173  }
174 }
175 
176 void MergeContacts::mergeCustomValue(const KABC::Addressee &fromContact, const QString &variable, KABC::Addressee &newContact)
177 {
178  const QString newValue = newContact.custom( QLatin1String( "KADDRESSBOOK" ), variable);
179  const QString value = fromContact.custom( QLatin1String( "KADDRESSBOOK" ), variable);
180  if (newValue.isEmpty() && !value.isEmpty()) {
181  newContact.insertCustom(QLatin1String( "KADDRESSBOOK" ), variable, value);
182  }
183 }
184 
185 
186 MergeContacts::ConflictInformations MergeContacts::requiresManualSelectionOfInformation()
187 {
188  MergeContacts::ConflictInformations result = None;
189  if (mListItem.count() < 2)
190  return result;
191  KABC::Addressee newContact;
192  Q_FOREACH (const Akonadi::Item &item, mListItem) {
193  if (item.hasPayload<KABC::Addressee>()) {
194  //Test Birthday
195  const KABC::Addressee address = item.payload<KABC::Addressee>();
196  if (address.birthday().date().isValid()) {
197  if (newContact.birthday().date().isValid()) {
198  if (newContact.birthday().date() != address.birthday().date()) {
199  result |= Birthday;
200  }
201  } else {
202  newContact.setBirthday(address.birthday());
203  }
204  }
205 
206  //Test Geo
207  const KABC::Geo geo = address.geo();
208  if (geo.isValid()) {
209  if (newContact.geo().isValid()) {
210  if (newContact.geo() != geo) {
211  result |= Geo;
212  }
213  } else {
214  newContact.setGeo(address.geo());
215  }
216  }
217  // Test Photo
218  const KABC::Picture photo = address.photo();
219  if (!photo.isEmpty()) {
220  if (!newContact.photo().isEmpty()) {
221  if (newContact.photo() != photo) {
222  result |= Photo;
223  }
224  } else {
225  newContact.setPhoto(address.photo());
226  }
227  }
228  //Test Logo
229  const KABC::Picture logo = address.logo();
230  if (!logo.isEmpty()) {
231  if (!newContact.logo().isEmpty()) {
232  if (newContact.logo() != logo) {
233  result |= Logo;
234  }
235  } else {
236  newContact.setLogo(address.logo());
237  }
238  }
239  // Test Name
240  const QString name = address.name();
241  if (!name.isEmpty()) {
242  if (!newContact.name().isEmpty()) {
243  if (newContact.name() != name) {
244  result |= Name;
245  }
246  } else {
247  newContact.setName(address.name());
248  }
249  }
250  // Test NickName
251  const QString nickName = address.nickName();
252  if (!nickName.isEmpty()) {
253  if (!newContact.nickName().isEmpty()) {
254  if (newContact.nickName() != nickName) {
255  result |= NickName;
256  }
257  } else {
258  newContact.setNickName(address.nickName());
259  }
260  }
261  // Test Organization
262  const QString organization = address.organization();
263  if (!organization.isEmpty()) {
264  if (!newContact.organization().isEmpty()) {
265  if (newContact.organization() != organization) {
266  result |= Organization;
267  }
268  } else {
269  newContact.setOrganization(address.organization());
270  }
271  }
272  // Test Title
273  const QString title = address.title();
274  if (!title.isEmpty()) {
275  if (!newContact.title().isEmpty()) {
276  if (newContact.title() != title) {
277  result |= Title;
278  }
279  } else {
280  newContact.setTitle(address.title());
281  }
282  }
283  // Test Departement
284  const QString departement = address.department();
285  if (!departement.isEmpty()) {
286  if (!newContact.department().isEmpty()) {
287  if (newContact.department() != departement) {
288  result |= Departement;
289  }
290  } else {
291  newContact.setDepartment(address.department());
292  }
293  }
294  // Test HomePage
295  const KUrl url = address.url();
296  if (url.isValid() && !url.isEmpty()) {
297  if (newContact.url().isValid() && !newContact.url().isEmpty()) {
298  if (newContact.url() != url) {
299  result |= HomePage;
300  }
301  } else {
302  newContact.setUrl(address.url());
303  }
304  }
305  // Test FamilyName
306  const QString familyName = address.familyName();
307  if (!familyName.isEmpty()) {
308  if (!newContact.familyName().isEmpty()) {
309  if (newContact.familyName() != familyName) {
310  result |= FamilyName;
311  }
312  } else {
313  newContact.setFamilyName(address.familyName());
314  }
315  }
316  // Test Blog
317  checkCustomValue(address, QLatin1String( "BlogFeed" ), newContact, result, Blog);
318  // Test profession
319  checkCustomValue(address, QLatin1String( "X-Profession" ), newContact, result, Profession);
320  // Test profession
321  checkCustomValue(address, QLatin1String( "X-Office" ), newContact, result, Office);
322  // Test ManagersName
323  checkCustomValue(address, QLatin1String( "X-ManagersName" ), newContact, result, ManagerName);
324  // Test AssistantsName
325  checkCustomValue(address, QLatin1String( "X-AssistantsName" ), newContact, result, Assistant);
326  // Test SpousesName
327  checkCustomValue(address, QLatin1String( "X-SpousesName" ), newContact, result, PartnerName);
328  //Test Anniversary
329  checkCustomValue(address, QLatin1String( "X-Anniversary" ), newContact, result, Anniversary);
330  }
331  }
332  //qDebug()<<" result "<<result;
333  return result;
334 }
335 
336 void MergeContacts::checkCustomValue(const KABC::Addressee &address, const QString &variable, KABC::Addressee &newContact, MergeContacts::ConflictInformations &result, MergeContacts::ConflictInformation conflict)
337 {
338  const QString value = address.custom( QLatin1String( "KADDRESSBOOK" ), variable);
339  if (!value.isEmpty()) {
340  const QString newValue = newContact.custom( QLatin1String( "KADDRESSBOOK" ), variable);
341  if (!newValue.isEmpty()) {
342  if (newValue != value) {
343  result |= conflict;
344  }
345  } else {
346  newContact.insertCustom(QLatin1String( "KADDRESSBOOK" ), variable, value);
347  }
348  }
349 }
KABMergeContacts::MergeContacts::setItems
void setItems(const Akonadi::Item::List &items)
Definition: mergecontacts.cpp:32
KABMergeContacts::MergeContacts::Departement
Definition: mergecontacts.h:46
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
KABMergeContacts::MergeContacts::Profession
Definition: mergecontacts.h:44
KABMergeContacts::MergeContacts::Blog
Definition: mergecontacts.h:41
KABMergeContacts::MergeContacts::None
Definition: mergecontacts.h:33
KABMergeContacts::MergeContacts::Anniversary
Definition: mergecontacts.h:38
KABMergeContacts::MergeContacts::Organization
Definition: mergecontacts.h:43
KABMergeContacts::MergeContacts::Photo
Definition: mergecontacts.h:36
KABMergeContacts::MergeContacts::mergedContact
KABC::Addressee mergedContact(bool excludeConflictPart=false)
Definition: mergecontacts.cpp:37
KABMergeContacts::MergeContacts::ManagerName
Definition: mergecontacts.h:48
QList::append
void append(const T &value)
KABMergeContacts::MergeContacts::HomePage
Definition: mergecontacts.h:42
mergecontacts.h
KABMergeContacts::MergeContacts::Logo
Definition: mergecontacts.h:37
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
KABMergeContacts::MergeContacts::FamilyName
Definition: mergecontacts.h:51
QString
QStringList
QLatin1Char
KABMergeContacts::MergeContacts::Office
Definition: mergecontacts.h:47
KABMergeContacts::MergeContacts::Name
Definition: mergecontacts.h:39
KABMergeContacts::MergeContacts::NickName
Definition: mergecontacts.h:40
KABMergeContacts::MergeContacts::MergeContacts
MergeContacts(const Akonadi::Item::List &items=Akonadi::Item::List())
Definition: mergecontacts.cpp:22
QLatin1String
KABMergeContacts::MergeContacts::ConflictInformation
ConflictInformation
Definition: mergecontacts.h:32
KABMergeContacts::MergeContacts::Title
Definition: mergecontacts.h:45
KABMergeContacts::MergeContacts::PartnerName
Definition: mergecontacts.h:52
KABMergeContacts::MergeContacts::requiresManualSelectionOfInformation
MergeContacts::ConflictInformations requiresManualSelectionOfInformation()
Definition: mergecontacts.cpp:186
KABMergeContacts::MergeContacts::~MergeContacts
~MergeContacts()
Definition: mergecontacts.cpp:27
KABMergeContacts::MergeContacts::Assistant
Definition: mergecontacts.h:49
KABMergeContacts::MergeContacts::Geo
Definition: mergecontacts.h:35
KABMergeContacts::MergeContacts::Birthday
Definition: mergecontacts.h:34
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:34 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

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