blob: 34f3ec7355d57b94051276b3243ec074a5044f9a [file] [log] [blame]
Alexandre Lision3b0bd332015-03-15 18:43:07 -04001/*
2 * Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
3 * Author: Alexandre Lision <alexandre.lision@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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall import the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30#import "AddressBookBackend.h"
31
32#import <AddressBook/AddressBook.h>
33
34//Qt
35#import <QtCore/QFile>
36#import <QtCore/QDir>
37#import <QtCore/QHash>
38#import <QtWidgets/QApplication>
39#import <QtCore/QStandardPaths>
40#import <QTimer>
41#import <QtGlobal>
42
43//Ring
44#import <Person.h>
45#import <account.h>
46#import <person.h>
47#import <contactmethod.h>
48
49/**
50 *
51 *kABFirstNameProperty
52 kABLastNameProperty
53 kABFirstNamePhoneticProperty
54 kABLastNamePhoneticProperty
55 kABBirthdayProperty
56 kABOrganizationProperty
57 kABJobTitleProperty
58 kABHomePageProperty
59 kABEmailProperty
60 kABAddressProperty
61 kABPhoneProperty
62 kABAIMInstantProperty
63 kABJabberInstantProperty
64 kABMSNInstantProperty
65 kABYahooInstantProperty
66 kABICQInstantProperty
67 kABNoteProperty
68 kABMiddleNameProperty
69 kABMiddleNamePhoneticProperty
70 kABTitleProperty
71 kABSuffixProperty
72 kABNicknameProperty
73 kABMaidenNameProperty
74 */
75
76class AddressBookEditor : public CollectionEditor<Person>
77{
78public:
79 AddressBookEditor(CollectionMediator<Person>* m, AddressBookBackend* parent);
80 virtual bool save ( const Person* item ) override;
81 virtual bool remove ( const Person* item ) override;
82 virtual bool edit ( Person* item ) override;
83 virtual bool addNew ( const Person* item ) override;
84 virtual bool addExisting( const Person* item ) override;
85
86private:
87 virtual QVector<Person*> items() const override;
88
89 //Helpers
90 void savePerson(QTextStream& stream, const Person* Person);
91 bool regenFile(const Person* toIgnore);
92
93 //Attributes
94 QVector<Person*> m_lItems;
95 AddressBookBackend* m_pCollection;
96};
97
98AddressBookEditor::AddressBookEditor(CollectionMediator<Person>* m, AddressBookBackend* parent) :
99CollectionEditor<Person>(m),m_pCollection(parent)
100{
101
102}
103
104AddressBookBackend::AddressBookBackend(CollectionMediator<Person>* mediator) :
105CollectionInterface(new AddressBookEditor(mediator,this)),m_pMediator(mediator)
106{
107
108}
109
110AddressBookBackend::~AddressBookBackend()
111{
112
113}
114
115void AddressBookEditor::savePerson(QTextStream& stream, const Person* Person)
116{
117
118 qDebug() << "Saving Person!";
119}
120
121bool AddressBookEditor::regenFile(const Person* toIgnore)
122{
123 QDir dir(QString('/'));
124 dir.mkpath(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1Char('/') + QString());
125
126
127 return false;
128}
129
130bool AddressBookEditor::save(const Person* Person)
131{
132 //if (Person->collection()->editor<Person>() != this)
133 // return addNew(Person);
134
135 return regenFile(nullptr);
136}
137
138bool AddressBookEditor::remove(const Person* item)
139{
140 return regenFile(item);
141}
142
143bool AddressBookEditor::edit( Person* item)
144{
145 Q_UNUSED(item)
146 return false;
147}
148
149bool AddressBookEditor::addNew(const Person* Person)
150{
151 QDir dir(QString('/'));
152 dir.mkpath(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + QLatin1Char('/') + QString());
153
154 return false;
155}
156
157bool AddressBookEditor::addExisting(const Person* item)
158{
159 m_lItems << const_cast<Person*>(item);
160 mediator()->addItem(item);
161 return true;
162}
163
164QVector<Person*> AddressBookEditor::items() const
165{
166 return m_lItems;
167}
168
169QString AddressBookBackend::name () const
170{
171 return QObject::tr("AddressBook backend");
172}
173
174QString AddressBookBackend::category () const
175{
176 return QObject::tr("Persons");
177}
178
179QVariant AddressBookBackend::icon() const
180{
181 return QVariant();
182}
183
184bool AddressBookBackend::isEnabled() const
185{
186 return true;
187}
188
189bool AddressBookBackend::load()
190{
191 QTimer::singleShot(100, [=] {
192 asyncLoad(0);
193 });
194 return false;
195}
196
197void AddressBookBackend::asyncLoad(int startingPoint)
198{
199 ABAddressBook *book = [ABAddressBook sharedAddressBook];
200 NSArray *everyone = [book people];
201 int endPoint = qMin(startingPoint + 10, (int)everyone.count);
202
203 for (int i = startingPoint; i < endPoint; ++i) {
204
205 Person* person = new Person(QByteArray::fromNSData(((ABPerson*)[everyone objectAtIndex:i]).vCardRepresentation),
206 Person::Encoding::vCard,
207 this);
208 if([person->formattedName().toNSString() isEqualToString:@""] &&
209 [person->secondName().toNSString() isEqualToString:@""] &&
210 [person->firstName().toNSString() isEqualToString:@""]) {
211 continue;
212 }
213 person->setCollection(this);
214
215 editor<Person>()->addExisting(person);
216 }
217
218 if(endPoint < everyone.count) {
219 QTimer::singleShot(100, [=] {
220 asyncLoad(endPoint);
221 });
222 }
223
224}
225
226
227bool AddressBookBackend::reload()
228{
229 return false;
230}
231
Alexandre Lision39224632015-05-01 11:52:17 -0400232FlagPack<AddressBookBackend::SupportedFeatures> AddressBookBackend::supportedFeatures() const
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400233{
Alexandre Lision39224632015-05-01 11:52:17 -0400234 return (FlagPack<SupportedFeatures>) (
Alexandre Lision3b0bd332015-03-15 18:43:07 -0400235 CollectionInterface::SupportedFeatures::NONE |
236 CollectionInterface::SupportedFeatures::LOAD |
237 CollectionInterface::SupportedFeatures::CLEAR |
238 CollectionInterface::SupportedFeatures::REMOVE|
239 CollectionInterface::SupportedFeatures::ADD );
240}
241
242bool AddressBookBackend::clear()
243{
244 /* TODO: insert confirm dialog? */
245 return true;
246}
247
248QByteArray AddressBookBackend::id() const
249{
250 return "abb";
251}