Changeset View
Changeset View
Standalone View
Standalone View
resources/contacts/contactsresource.cpp
Show First 20 Lines • Show All 164 Lines • ▼ Show 20 Line(s) | 148 | for (const QFileInfo &entry : entries) { | |||
---|---|---|---|---|---|
165 | items.append(item); | 165 | items.append(item); | ||
166 | } | 166 | } | ||
167 | 167 | | |||
168 | itemsRetrieved(items); | 168 | itemsRetrieved(items); | ||
169 | } | 169 | } | ||
170 | 170 | | |||
171 | bool ContactsResource::retrieveItem(const Akonadi::Item &item, const QSet<QByteArray> &) | 171 | bool ContactsResource::retrieveItem(const Akonadi::Item &item, const QSet<QByteArray> &) | ||
172 | { | 172 | { | ||
173 | const QString filePath = directoryForCollection(item.parentCollection()) + QDir::separator() + item.remoteId(); | 173 | const QString filePath = directoryForCollection(item.parentCollection()) + QStringLiteral("/") + item.remoteId(); | ||
174 | 174 | | |||
175 | Item newItem(item); | 175 | Item newItem(item); | ||
176 | 176 | | |||
177 | QFile file(filePath); | 177 | QFile file(filePath); | ||
178 | if (!file.open(QIODevice::ReadOnly)) { | 178 | if (!file.open(QIODevice::ReadOnly)) { | ||
179 | cancelTask(i18n("Unable to open file '%1'", filePath)); | 179 | cancelTask(i18n("Unable to open file '%1'", filePath)); | ||
180 | return false; | 180 | return false; | ||
181 | } | 181 | } | ||
Show All 40 Lines | 217 | { | |||
222 | 222 | | |||
223 | const QString directoryPath = directoryForCollection(collection); | 223 | const QString directoryPath = directoryForCollection(collection); | ||
224 | 224 | | |||
225 | Item newItem(item); | 225 | Item newItem(item); | ||
226 | 226 | | |||
227 | if (item.hasPayload<KContacts::Addressee>()) { | 227 | if (item.hasPayload<KContacts::Addressee>()) { | ||
228 | const KContacts::Addressee contact = item.payload<KContacts::Addressee>(); | 228 | const KContacts::Addressee contact = item.payload<KContacts::Addressee>(); | ||
229 | 229 | | |||
230 | const QString fileName = directoryPath + QDir::separator() + contact.uid() + QLatin1String(".vcf"); | 230 | const QString fileName = directoryPath + QStringLiteral("/") + contact.uid() + QStringLiteral(".vcf"); | ||
231 | 231 | | |||
232 | KContacts::VCardConverter converter; | 232 | KContacts::VCardConverter converter; | ||
233 | const QByteArray content = converter.createVCard(contact); | 233 | const QByteArray content = converter.createVCard(contact); | ||
234 | 234 | | |||
235 | QFile file(fileName); | 235 | QFile file(fileName); | ||
236 | if (!file.open(QIODevice::WriteOnly)) { | 236 | if (!file.open(QIODevice::WriteOnly)) { | ||
237 | cancelTask(i18n("Unable to write to file '%1': %2", fileName, file.errorString())); | 237 | cancelTask(i18n("Unable to write to file '%1': %2", fileName, file.errorString())); | ||
238 | return; | 238 | return; | ||
239 | } | 239 | } | ||
240 | 240 | | |||
241 | file.write(content); | 241 | file.write(content); | ||
242 | file.close(); | 242 | file.close(); | ||
243 | 243 | | |||
244 | newItem.setRemoteId(contact.uid() + QLatin1String(".vcf")); | 244 | newItem.setRemoteId(contact.uid() + QStringLiteral(".vcf")); | ||
245 | } else if (item.hasPayload<KContacts::ContactGroup>()) { | 245 | } else if (item.hasPayload<KContacts::ContactGroup>()) { | ||
246 | const KContacts::ContactGroup group = item.payload<KContacts::ContactGroup>(); | 246 | const KContacts::ContactGroup group = item.payload<KContacts::ContactGroup>(); | ||
247 | 247 | | |||
248 | const QString fileName = directoryPath + QDir::separator() + group.id() + QLatin1String(".ctg"); | 248 | const QString fileName = directoryPath + QStringLiteral("/") + group.id() + QStringLiteral(".ctg"); | ||
249 | 249 | | |||
250 | QFile file(fileName); | 250 | QFile file(fileName); | ||
251 | if (!file.open(QIODevice::WriteOnly)) { | 251 | if (!file.open(QIODevice::WriteOnly)) { | ||
252 | cancelTask(i18n("Unable to write to file '%1': %2", fileName, file.errorString())); | 252 | cancelTask(i18n("Unable to write to file '%1': %2", fileName, file.errorString())); | ||
253 | return; | 253 | return; | ||
254 | } | 254 | } | ||
255 | 255 | | |||
256 | KContacts::ContactGroupTool::convertToXml(group, &file); | 256 | KContacts::ContactGroupTool::convertToXml(group, &file); | ||
Show All 12 Lines | |||||
269 | { | 269 | { | ||
270 | if (ContactsResourceSettings::self()->readOnly()) { | 270 | if (ContactsResourceSettings::self()->readOnly()) { | ||
271 | cancelTask(i18n("Trying to write to a read-only file: '%1'", item.remoteId())); | 271 | cancelTask(i18n("Trying to write to a read-only file: '%1'", item.remoteId())); | ||
272 | return; | 272 | return; | ||
273 | } | 273 | } | ||
274 | 274 | | |||
275 | Item newItem(item); | 275 | Item newItem(item); | ||
276 | 276 | | |||
277 | const QString fileName = directoryForCollection(item.parentCollection()) + QDir::separator() + item.remoteId(); | 277 | const QString fileName = directoryForCollection(item.parentCollection()) + QStringLiteral("/") + item.remoteId(); | ||
278 | 278 | | |||
279 | if (item.hasPayload<KContacts::Addressee>()) { | 279 | if (item.hasPayload<KContacts::Addressee>()) { | ||
280 | const KContacts::Addressee contact = item.payload<KContacts::Addressee>(); | 280 | const KContacts::Addressee contact = item.payload<KContacts::Addressee>(); | ||
281 | 281 | | |||
282 | KContacts::VCardConverter converter; | 282 | KContacts::VCardConverter converter; | ||
283 | const QByteArray content = converter.createVCard(contact); | 283 | const QByteArray content = converter.createVCard(contact); | ||
284 | 284 | | |||
285 | QFile file(fileName); | 285 | QFile file(fileName); | ||
Show All 37 Lines | 317 | { | |||
323 | // If the parent collection has no valid remote id, the parent | 323 | // If the parent collection has no valid remote id, the parent | ||
324 | // collection will be removed in a second, so stop here and remove | 324 | // collection will be removed in a second, so stop here and remove | ||
325 | // all items in collectionRemoved(). | 325 | // all items in collectionRemoved(). | ||
326 | if (item.parentCollection().remoteId().isEmpty()) { | 326 | if (item.parentCollection().remoteId().isEmpty()) { | ||
327 | changeProcessed(); | 327 | changeProcessed(); | ||
328 | return; | 328 | return; | ||
329 | } | 329 | } | ||
330 | 330 | | |||
331 | const QString fileName = directoryForCollection(item.parentCollection()) + QDir::separator() + item.remoteId(); | 331 | const QString fileName = directoryForCollection(item.parentCollection()) + QStringLiteral("/") + item.remoteId(); | ||
332 | 332 | | |||
333 | if (!QFile::remove(fileName)) { | 333 | if (!QFile::remove(fileName)) { | ||
334 | cancelTask(i18n("Unable to remove file '%1'", fileName)); | 334 | cancelTask(i18n("Unable to remove file '%1'", fileName)); | ||
335 | return; | 335 | return; | ||
336 | } | 336 | } | ||
337 | 337 | | |||
338 | changeProcessed(); | 338 | changeProcessed(); | ||
339 | } | 339 | } | ||
340 | 340 | | |||
341 | void ContactsResource::collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent) | 341 | void ContactsResource::collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent) | ||
342 | { | 342 | { | ||
343 | if (ContactsResourceSettings::self()->readOnly()) { | 343 | if (ContactsResourceSettings::self()->readOnly()) { | ||
344 | cancelTask(i18n("Trying to write to a read-only directory: '%1'", parent.remoteId())); | 344 | cancelTask(i18n("Trying to write to a read-only directory: '%1'", parent.remoteId())); | ||
345 | return; | 345 | return; | ||
346 | } | 346 | } | ||
347 | 347 | | |||
348 | const QString dirName = directoryForCollection(parent) + QDir::separator() + collection.name(); | 348 | const QString dirName = directoryForCollection(parent) + QStringLiteral("/") + collection.name(); | ||
349 | 349 | | |||
350 | if (!QDir::root().mkpath(dirName)) { | 350 | if (!QDir::root().mkpath(dirName)) { | ||
351 | cancelTask(i18n("Unable to create folder '%1'.", dirName)); | 351 | cancelTask(i18n("Unable to create folder '%1'.", dirName)); | ||
352 | return; | 352 | return; | ||
353 | } | 353 | } | ||
354 | 354 | | |||
355 | initializeDirectory(dirName); | 355 | initializeDirectory(dirName); | ||
356 | 356 | | |||
Show All 20 Lines | 363 | { | |||
377 | if (collection.remoteId() == collection.name()) { | 377 | if (collection.remoteId() == collection.name()) { | ||
378 | changeProcessed(); | 378 | changeProcessed(); | ||
379 | return; | 379 | return; | ||
380 | } | 380 | } | ||
381 | 381 | | |||
382 | const QString dirName = directoryForCollection(collection); | 382 | const QString dirName = directoryForCollection(collection); | ||
383 | 383 | | |||
384 | QFileInfo oldDirectory(dirName); | 384 | QFileInfo oldDirectory(dirName); | ||
385 | if (!QDir::root().rename(dirName, oldDirectory.absolutePath() + QDir::separator() + collection.name())) { | 385 | if (!QDir::root().rename(dirName, oldDirectory.absolutePath() + QStringLiteral("/") + collection.name())) { | ||
386 | cancelTask(i18n("Unable to rename folder '%1'.", collection.name())); | 386 | cancelTask(i18n("Unable to rename folder '%1'.", collection.name())); | ||
387 | return; | 387 | return; | ||
388 | } | 388 | } | ||
389 | 389 | | |||
390 | Collection newCollection(collection); | 390 | Collection newCollection(collection); | ||
391 | newCollection.setRemoteId(collection.name()); | 391 | newCollection.setRemoteId(collection.name()); | ||
392 | changeCommitted(newCollection); | 392 | changeCommitted(newCollection); | ||
393 | } | 393 | } | ||
Show All 15 Lines | 407 | if (!QDir(collectionDir).removeRecursively()) { | |||
409 | return; | 409 | return; | ||
410 | } | 410 | } | ||
411 | 411 | | |||
412 | changeProcessed(); | 412 | changeProcessed(); | ||
413 | } | 413 | } | ||
414 | 414 | | |||
415 | void ContactsResource::itemMoved(const Akonadi::Item &item, const Akonadi::Collection &collectionSource, const Akonadi::Collection &collectionDestination) | 415 | void ContactsResource::itemMoved(const Akonadi::Item &item, const Akonadi::Collection &collectionSource, const Akonadi::Collection &collectionDestination) | ||
416 | { | 416 | { | ||
417 | const QString sourceFileName = directoryForCollection(collectionSource) + QDir::separator() + item.remoteId(); | 417 | const QString sourceFileName = directoryForCollection(collectionSource) + QStringLiteral("/") + item.remoteId(); | ||
418 | const QString targetFileName = directoryForCollection(collectionDestination) + QDir::separator() + item.remoteId(); | 418 | const QString targetFileName = directoryForCollection(collectionDestination) + QStringLiteral("/") + item.remoteId(); | ||
419 | 419 | | |||
420 | if (QFile::rename(sourceFileName, targetFileName)) { | 420 | if (QFile::rename(sourceFileName, targetFileName)) { | ||
421 | changeProcessed(); | 421 | changeProcessed(); | ||
422 | } else { | 422 | } else { | ||
423 | cancelTask(i18n("Unable to move file '%1' to '%2', '%2' already exists.", sourceFileName, targetFileName)); | 423 | cancelTask(i18n("Unable to move file '%1' to '%2', '%2' already exists.", sourceFileName, targetFileName)); | ||
424 | } | 424 | } | ||
425 | } | 425 | } | ||
426 | 426 | | |||
427 | void ContactsResource::collectionMoved(const Akonadi::Collection &collection, const Akonadi::Collection &collectionSource, const Akonadi::Collection &collectionDestination) | 427 | void ContactsResource::collectionMoved(const Akonadi::Collection &collection, const Akonadi::Collection &collectionSource, const Akonadi::Collection &collectionDestination) | ||
428 | { | 428 | { | ||
429 | const QString sourceDirectoryName = directoryForCollection(collectionSource) + QDir::separator() + collection.remoteId(); | 429 | const QString sourceDirectoryName = directoryForCollection(collectionSource) + QStringLiteral("/") + collection.remoteId(); | ||
430 | const QString targetDirectoryName = directoryForCollection(collectionDestination) + QDir::separator() + collection.remoteId(); | 430 | const QString targetDirectoryName = directoryForCollection(collectionDestination) + QStringLiteral("/") + collection.remoteId(); | ||
431 | 431 | | |||
432 | if (QFile::rename(sourceDirectoryName, targetDirectoryName)) { | 432 | if (QFile::rename(sourceDirectoryName, targetDirectoryName)) { | ||
433 | changeProcessed(); | 433 | changeProcessed(); | ||
434 | } else { | 434 | } else { | ||
435 | cancelTask(i18n("Unable to move directory '%1' to '%2', '%2' already exists.", sourceDirectoryName, targetDirectoryName)); | 435 | cancelTask(i18n("Unable to move directory '%1' to '%2', '%2' already exists.", sourceDirectoryName, targetDirectoryName)); | ||
436 | } | 436 | } | ||
437 | } | 437 | } | ||
438 | 438 | | |||
439 | QString ContactsResource::baseDirectoryPath() const | 439 | QString ContactsResource::baseDirectoryPath() const | ||
440 | { | 440 | { | ||
441 | return ContactsResourceSettings::self()->path(); | 441 | return ContactsResourceSettings::self()->path(); | ||
442 | } | 442 | } | ||
443 | 443 | | |||
444 | void ContactsResource::initializeDirectory(const QString &path) const | 444 | void ContactsResource::initializeDirectory(const QString &path) const | ||
445 | { | 445 | { | ||
446 | QDir dir(path); | 446 | QDir dir(path); | ||
447 | 447 | | |||
448 | // if folder does not exists, create it | 448 | // if folder does not exists, create it | ||
449 | QDir::root().mkpath(dir.absolutePath()); | 449 | QDir::root().mkpath(dir.absolutePath()); | ||
450 | 450 | | |||
451 | // check whether warning file is in place... | 451 | // check whether warning file is in place... | ||
452 | QFile file(dir.absolutePath() + QDir::separator() + QLatin1String("WARNING_README.txt")); | 452 | QFile file(dir.absolutePath() + QStringLiteral("/WARNING_README.txt")); | ||
453 | if (!file.exists()) { | 453 | if (!file.exists()) { | ||
454 | // ... if not, create it | 454 | // ... if not, create it | ||
455 | file.open(QIODevice::WriteOnly); | 455 | file.open(QIODevice::WriteOnly); | ||
456 | file.write("Important Warning!!!\n\n" | 456 | file.write("Important Warning!!!\n\n" | ||
457 | "Don't create or copy vCards inside this folder manually, they are managed by the Akonadi framework!\n"); | 457 | "Don't create or copy vCards inside this folder manually, they are managed by the Akonadi framework!\n"); | ||
458 | file.close(); | 458 | file.close(); | ||
459 | } | 459 | } | ||
460 | } | 460 | } | ||
Show All 34 Lines | 482 | { | |||
495 | 495 | | |||
496 | const QString parentDirectory = directoryForCollection(collection.parentCollection()); | 496 | const QString parentDirectory = directoryForCollection(collection.parentCollection()); | ||
497 | if (parentDirectory.isNull()) { // invalid, != isEmpty() here! | 497 | if (parentDirectory.isNull()) { // invalid, != isEmpty() here! | ||
498 | return QString(); | 498 | return QString(); | ||
499 | } | 499 | } | ||
500 | 500 | | |||
501 | QString directory = parentDirectory; | 501 | QString directory = parentDirectory; | ||
502 | if (!directory.endsWith(QLatin1Char('/'))) { | 502 | if (!directory.endsWith(QLatin1Char('/'))) { | ||
503 | directory += QDir::separator() + collection.remoteId(); | 503 | directory += QStringLiteral("/") + collection.remoteId(); | ||
504 | } else { | 504 | } else { | ||
505 | directory += collection.remoteId(); | 505 | directory += collection.remoteId(); | ||
506 | } | 506 | } | ||
507 | 507 | | |||
508 | return directory; | 508 | return directory; | ||
509 | } | 509 | } | ||
510 | 510 | | |||
511 | AKONADI_RESOURCE_MAIN(ContactsResource) | 511 | AKONADI_RESOURCE_MAIN(ContactsResource) |