Changeset View
Changeset View
Standalone View
Standalone View
src/kpeoplevcard.cpp
Show All 19 Lines | |||||
20 | #include "kpeoplevcard.h" | 20 | #include "kpeoplevcard.h" | ||
21 | #include <QDebug> | 21 | #include <QDebug> | ||
22 | #include <QImage> | 22 | #include <QImage> | ||
23 | #include <QDir> | 23 | #include <QDir> | ||
24 | #include <QStandardPaths> | 24 | #include <QStandardPaths> | ||
25 | #include <KContacts/VCardConverter> | 25 | #include <KContacts/VCardConverter> | ||
26 | #include <KContacts/Picture> | 26 | #include <KContacts/Picture> | ||
27 | #include <KPeopleBackend/BasePersonsDataSource> | 27 | #include <KPeopleBackend/BasePersonsDataSource> | ||
28 | #include <KPeopleBackend/AbstractEditableContact> | ||||
28 | #include <KLocalizedString> | 29 | #include <KLocalizedString> | ||
29 | 30 | | |||
30 | #include <KPluginFactory> | 31 | #include <KPluginFactory> | ||
31 | #include <KPluginLoader> | 32 | #include <KPluginLoader> | ||
33 | #include <KFileUtils> | ||||
32 | 34 | | |||
33 | using namespace KPeople; | 35 | using namespace KPeople; | ||
34 | 36 | | |||
35 | Q_GLOBAL_STATIC_WITH_ARGS(QString, vcardsLocation, (QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + ("/kpeoplevcard"))) | 37 | Q_GLOBAL_STATIC_WITH_ARGS(QString, vcardsLocation, (QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + ("/kpeoplevcard"))) | ||
38 | Q_GLOBAL_STATIC_WITH_ARGS(QString, vcardsWriteLocation, (QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + ("/kpeoplevcard/own/"))) | ||||
bshah: Shouldn't we potentially use the same location? Any specific reason to use different location? | |||||
I don't really know, does it matter? If you think it's better I'm fine with it. At the moment, kdeconnect-sms already puts it into a subdirectory per device. apol: I don't really know, does it matter? If you think it's better I'm fine with it.
At the moment… | |||||
36 | 39 | | |||
37 | class VCardContact : public AbstractContact | 40 | class VCardContact : public AbstractEditableContact | ||
38 | { | 41 | { | ||
39 | public: | 42 | public: | ||
40 | VCardContact() {} | 43 | VCardContact() {} | ||
41 | VCardContact(const KContacts::Addressee& addr) : m_addressee(addr) {} | 44 | VCardContact(const KContacts::Addressee& addr, const QUrl &location) : m_addressee(addr), m_location(location) {} | ||
42 | void setAddressee(const KContacts::Addressee& addr) { m_addressee = addr; } | 45 | void setAddressee(const KContacts::Addressee& addr) { m_addressee = addr; } | ||
46 | void setUrl(const QUrl &url) { m_location = url; } | ||||
43 | 47 | | |||
44 | QVariant customProperty(const QString & key) const override | 48 | QVariant customProperty(const QString & key) const override | ||
45 | { | 49 | { | ||
46 | QVariant ret; | 50 | QVariant ret; | ||
47 | if (key == NameProperty) { | 51 | if (key == NameProperty) { | ||
48 | const QString name = m_addressee.realName(); | 52 | const QString name = m_addressee.realName(); | ||
49 | if (!name.isEmpty()) { | 53 | if (!name.isEmpty()) { | ||
50 | return name; | 54 | return name; | ||
Show All 28 Lines | 81 | else if (key == AllPhoneNumbersProperty) { | |||
79 | QVariantList numbers; | 83 | QVariantList numbers; | ||
80 | for (const KContacts::PhoneNumber &phoneNumber : phoneNumbers) { | 84 | for (const KContacts::PhoneNumber &phoneNumber : phoneNumbers) { | ||
81 | // convert from KContacts specific format to QString | 85 | // convert from KContacts specific format to QString | ||
82 | numbers << phoneNumber.number(); | 86 | numbers << phoneNumber.number(); | ||
83 | } | 87 | } | ||
84 | return numbers; | 88 | return numbers; | ||
85 | } else if (key == PhoneNumberProperty) { | 89 | } else if (key == PhoneNumberProperty) { | ||
86 | return m_addressee.phoneNumbers().isEmpty() ? QVariant() : m_addressee.phoneNumbers().at(0).number(); | 90 | return m_addressee.phoneNumbers().isEmpty() ? QVariant() : m_addressee.phoneNumbers().at(0).number(); | ||
91 | } else if (key == VCardProperty) { | ||||
92 | KContacts::VCardConverter converter; | ||||
93 | return converter.createVCard(m_addressee); | ||||
87 | } | 94 | } | ||
88 | 95 | | |||
89 | return ret; | 96 | return ret; | ||
90 | } | 97 | } | ||
91 | 98 | | |||
99 | bool setCustomProperty(const QString & key, const QVariant & value) override { | ||||
100 | if (key == VCardProperty) { | ||||
101 | QFile f(m_location.toLocalFile()); | ||||
102 | if (!f.open(QIODevice::WriteOnly)) | ||||
103 | return false; | ||||
104 | f.write(value.toByteArray()); | ||||
105 | return true; | ||||
106 | } | ||||
107 | return false; | ||||
108 | } | ||||
109 | | ||||
92 | static QString createUri(const QString& path) { | 110 | static QString createUri(const QString& path) { | ||
93 | return QStringLiteral("vcard:/") + path; | 111 | return QStringLiteral("vcard:/") + path; | ||
94 | } | 112 | } | ||
95 | private: | 113 | private: | ||
96 | KContacts::Addressee m_addressee; | 114 | KContacts::Addressee m_addressee; | ||
115 | QUrl m_location; | ||||
97 | }; | 116 | }; | ||
98 | 117 | | |||
99 | class VCardDataSource : public KPeople::BasePersonsDataSource | 118 | class VCardDataSource : public KPeople::BasePersonsDataSourceV2 | ||
100 | { | 119 | { | ||
101 | public: | 120 | public: | ||
102 | VCardDataSource(QObject *parent, const QVariantList &data); | 121 | VCardDataSource(QObject *parent, const QVariantList &data); | ||
103 | ~VCardDataSource() override; | 122 | ~VCardDataSource() override; | ||
104 | QString sourcePluginId() const override; | 123 | QString sourcePluginId() const override; | ||
105 | 124 | | |||
106 | KPeople::AllContactsMonitor* createAllContactsMonitor() override; | 125 | KPeople::AllContactsMonitor* createAllContactsMonitor() override; | ||
126 | bool addContact(const QVariantMap & properties) override { | ||||
127 | if (!properties.contains("vcard")) | ||||
128 | return false; | ||||
129 | | ||||
130 | if (!QDir().mkpath(*vcardsWriteLocation)) | ||||
131 | return false; | ||||
132 | | ||||
133 | QFile f(*vcardsWriteLocation + KFileUtils::suggestName(QUrl::fromLocalFile(*vcardsWriteLocation), QStringLiteral("contact.vcard"))); | ||||
134 | if (!f.open(QFile::WriteOnly)) { | ||||
135 | qWarning() << "could not open file to write" << f.fileName(); | ||||
136 | return false; | ||||
137 | } | ||||
138 | | ||||
139 | f.write(properties.value("vcard").toByteArray()); | ||||
140 | return true; | ||||
141 | } | ||||
107 | }; | 142 | }; | ||
108 | 143 | | |||
109 | KPeopleVCard::KPeopleVCard() | 144 | KPeopleVCard::KPeopleVCard() | ||
110 | : KPeople::AllContactsMonitor() | 145 | : KPeople::AllContactsMonitor() | ||
111 | , m_fs(new KDirWatch(this)) | 146 | , m_fs(new KDirWatch(this)) | ||
112 | { | 147 | { | ||
113 | QDir().mkpath(*vcardsLocation); | 148 | QDir().mkpath(*vcardsLocation); | ||
114 | 149 | | |||
Show All 40 Lines | 181 | { | |||
155 | 190 | | |||
156 | KContacts::VCardConverter conv; | 191 | KContacts::VCardConverter conv; | ||
157 | const KContacts::Addressee addressee = conv.parseVCard(f.readAll()); | 192 | const KContacts::Addressee addressee = conv.parseVCard(f.readAll()); | ||
158 | 193 | | |||
159 | QString uri = VCardContact::createUri(path); | 194 | QString uri = VCardContact::createUri(path); | ||
160 | auto it = m_contactForUri.find(uri); | 195 | auto it = m_contactForUri.find(uri); | ||
161 | if (it != m_contactForUri.end()) { | 196 | if (it != m_contactForUri.end()) { | ||
162 | static_cast<VCardContact*>(it->data())->setAddressee(addressee); | 197 | static_cast<VCardContact*>(it->data())->setAddressee(addressee); | ||
198 | static_cast<VCardContact*>(it->data())->setUrl(QUrl::fromLocalFile(path)); | ||||
163 | Q_EMIT contactChanged(uri, *it); | 199 | Q_EMIT contactChanged(uri, *it); | ||
164 | } else { | 200 | } else { | ||
165 | KPeople::AbstractContact::Ptr contact(new VCardContact(addressee)); | 201 | KPeople::AbstractContact::Ptr contact(new VCardContact(addressee, QUrl::fromLocalFile(path))); | ||
166 | m_contactForUri.insert(uri, contact); | 202 | m_contactForUri.insert(uri, contact); | ||
167 | Q_EMIT contactAdded(uri, contact); | 203 | Q_EMIT contactAdded(uri, contact); | ||
168 | } | 204 | } | ||
169 | } | 205 | } | ||
170 | 206 | | |||
171 | void KPeopleVCard::deleteVCard(const QString &path) | 207 | void KPeopleVCard::deleteVCard(const QString &path) | ||
172 | { | 208 | { | ||
173 | if (QFile::exists(path)) | 209 | if (QFile::exists(path)) | ||
174 | return; | 210 | return; | ||
175 | QString uri = VCardContact::createUri(path); | 211 | QString uri = VCardContact::createUri(path); | ||
176 | int r = m_contactForUri.remove(uri); | 212 | int r = m_contactForUri.remove(uri); | ||
177 | Q_ASSERT(r); | 213 | Q_ASSERT(r); | ||
178 | Q_EMIT contactRemoved(uri); | 214 | Q_EMIT contactRemoved(uri); | ||
179 | } | 215 | } | ||
180 | 216 | | |||
181 | QString KPeopleVCard::contactsVCardPath() | 217 | QString KPeopleVCard::contactsVCardPath() | ||
182 | { | 218 | { | ||
183 | return *vcardsLocation; | 219 | return *vcardsLocation; | ||
184 | } | 220 | } | ||
185 | 221 | | |||
186 | VCardDataSource::VCardDataSource(QObject *parent, const QVariantList &args) | 222 | VCardDataSource::VCardDataSource(QObject *parent, const QVariantList &args) | ||
187 | : BasePersonsDataSource(parent) | 223 | : BasePersonsDataSourceV2(parent) | ||
188 | { | 224 | { | ||
189 | Q_UNUSED(args); | 225 | Q_UNUSED(args); | ||
190 | } | 226 | } | ||
191 | 227 | | |||
192 | VCardDataSource::~VCardDataSource() | 228 | VCardDataSource::~VCardDataSource() | ||
193 | { | 229 | { | ||
194 | } | 230 | } | ||
195 | 231 | | |||
Show All 14 Lines |
Shouldn't we potentially use the same location? Any specific reason to use different location?