blob: 95a76809a49a0cf892d607e395357045de1b797d [file] [log] [blame]
Edric Milaret67007d12015-05-07 09:40:09 -04001/***************************************************************************
2 * Copyright (C) 2015 by Savoir-Faire Linux *
3 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
19#include "windowscontactbackend.h"
20
21WindowsContactEditor::WindowsContactEditor(CollectionMediator<Person> *m
22 , WindowsContactBackend *parent)
23 : CollectionEditor<Person>(m),collection_(parent)
24{
25
26}
27
28WindowsContactEditor::~WindowsContactEditor()
29{
30
31}
32
33bool
34WindowsContactEditor::save(const Person *item)
35{
36 Q_UNUSED(item)
37 return false;
38}
39
40bool
41WindowsContactEditor::remove(const Person *item)
42{
43 Q_UNUSED(item)
44 return false;
45}
46
47bool
48WindowsContactEditor::edit(Person *item)
49{
50 Q_UNUSED(item)
51 return false;
52}
53
54bool
55WindowsContactEditor::addNew(const Person *item)
56{
57 Q_UNUSED(item)
58 return false;
59}
60
61bool
62WindowsContactEditor::addExisting(const Person *item)
63{
64 items_ << const_cast<Person*>(item);
65 mediator()->addItem(item);
66 return true;
67}
68
69QVector<Person*>
70WindowsContactEditor::items() const
71{
72 return items_;
73}
74
75WindowsContactBackend::WindowsContactBackend(CollectionMediator<Person>* mediator,
76 CollectionInterface* parent)
77 : CollectionInterface(new WindowsContactEditor(mediator,this), parent)
78 , mediator_(mediator)
79{
80
81}
82
83WindowsContactBackend::~WindowsContactBackend()
84{
85
86}
87
88bool
89WindowsContactBackend::load()
90{
91 QtConcurrent::run(this, &WindowsContactBackend::loadRun);
92 return true;
93}
94
95bool
96WindowsContactBackend::loadRun()
97{
98 QDir contactDir(QStandardPaths::writableLocation
99 (QStandardPaths::HomeLocation) + "/Contacts");
100 QStringList filesList = contactDir.entryList(QStringList("*.contact"));
101
102 for(auto contactFileName : filesList) {
103 QString contactFilePath
104 (contactDir.absolutePath() + "/" + contactFileName);
105 QFile contactFile(contactFilePath);
106 if (contactFile.open(QIODevice::ReadOnly)) {
107 QXmlStreamReader reader;
108 Person *p = new Person();
109 QVector<ContactMethod*> contactMethod;
110 reader.setDevice(&contactFile);
111 while (!reader.atEnd()) {
112 reader.readNext();
113 if (reader.isStartElement()) {
114 QString name = reader.name().toString();
115 if (name == "FormattedName")
116 p->setFormattedName(reader.readElementText());
117 else if (name == "NickName")
118 p->setNickName(reader.readElementText());
119 else if (name == "GivenName")
120 p->setFirstName(reader.readElementText());
121 else if (name == "FamilyName")
122 p->setFamilyName(reader.readElementText());
123 else if (name == "Company")
124 p->setOrganization(reader.readElementText());
125 else if (name == "Department")
126 p->setDepartment(reader.readElementText());
127 else if (name == "Number") {
128 QString number = reader.readElementText();
129 if (not number.isEmpty()) {
130 ContactMethod *contact = PhoneDirectoryModel::instance()->getNumber(number);
131 contactMethod.append(contact);
132 }
133 }
134 else if (name == "Photo") {
135 //FIXME: It seems to be possible to have multiple photo...
136 reader.readNext();
137 if (reader.name().toString() == "Value") {
138 QString photoValue = reader.readElementText();
139 QByteArray byteArray;
140 byteArray.append(photoValue);
141 p->setPhoto(byteArray);
142 }
143 }
144 else if (name == "EmailAddress") {
145 QString address;
146 bool isPreferred = false;
147 reader.readNext();
148 while (reader.name().toString() != "EmailAddress"
149 && !reader.atEnd()) {
150 if (reader.isStartElement()) {
151 QString tag = reader.name().toString();
152 if (tag == "Address")
153 address = reader.readElementText();
154 else if (tag == "Label")
155 if (reader.readElementText() == "Preferred")
156 isPreferred = true;
157 }
158 reader.readNext();
159 }
160 if (isPreferred)
161 p->setPreferredEmail(address);
162 }
163 }
164 }
165 if (reader.hasError()) {
166 qDebug() << reader.errorString();
167 } else {
168 if (contactMethod.size() > 0)
169 p->setContactMethods(contactMethod);
170 editor<Person>()->addExisting(p);
171 }
172 } else {
173 qDebug() << "Error Opening contact file : " << contactFileName;
174 }
175 }
176
177 return false;
178}
179
180bool
181WindowsContactBackend::reload()
182{
183 return false;
184}
185
186bool
187WindowsContactBackend::clear()
188{
189 return false;
190}
191
192QString
193WindowsContactBackend::name() const
194{
195 return "Windows Contact Backend";
196}
197
198QString
199WindowsContactBackend::category() const
200{
201 return "Contacts";
202}
203
204bool
205WindowsContactBackend::isEnabled() const
206{
207 return true;
208}
209
210QByteArray
211WindowsContactBackend::id() const
212{
213 return "wincb";
214}
215
216FlagPack<CollectionInterface::SupportedFeatures> WindowsContactBackend::supportedFeatures() const
217{
218 return (
219 CollectionInterface::SupportedFeatures::NONE |
220 CollectionInterface::SupportedFeatures::LOAD);
221}
222