Commit 4a0e5e5e authored by Max von Buelow's avatar Max von Buelow Committed by Jonathan Marten
Browse files

Store tags names rather than tag IDs in KContacts::Addressee.

Solves the issue that contact tags are represented as IDs in the vCard CATEGORIES field, which leads to loss of tags when using address books across multiple devices. This update uses tag names in favor of IDs and automatically creates them when necessary.

"Old" tag IDs in the database are still interpreted for backward compatibility.

See !17

Related PRs in other projects:
* kaddressbook!17
* frameworks/kcontacts!33

BUG: 332358
parent c3797ba5
Pipeline #174786 passed with stage
in 3 minutes and 9 seconds
......@@ -13,6 +13,7 @@
#include <Akonadi/TagWidget>
#include <KPluginFactory>
#include <QHBoxLayout>
#include <akonadi/tagcreatejob.h>
K_PLUGIN_CLASS_WITH_JSON(CategoriesEditWidget, "categorieseditwidgetplugin.json")
......@@ -39,12 +40,31 @@ void CategoriesEditWidget::loadContact(const KContacts::Addressee &contact)
const QStringList categories = contact.categories();
tags.reserve(categories.count());
for (const QString &category : categories) {
tags.append(Akonadi::Tag::fromUrl(QUrl(category)));
if (category.startsWith(QLatin1String("akonadi:"))) {
tags.append(Akonadi::Tag::fromUrl(QUrl(category)));
} else {
Akonadi::Tag missingTag = Akonadi::Tag(category);
auto createJob = new Akonadi::TagCreateJob(missingTag, this);
createJob->setMergeIfExisting(true);
connect(createJob, &Akonadi::TagCreateJob::result, this, &CategoriesEditWidget::onMissingTagCreated);
}
}
mTagWidget->setSelection(tags);
}
void CategoriesEditWidget::onMissingTagCreated(KJob *job)
{
if (job->error()) {
return;
}
auto createJob = static_cast<Akonadi::TagCreateJob *>(job);
auto selectedTags{mTagWidget->selection()};
selectedTags += createJob->tag();
mTagWidget->setSelection(selectedTags);
}
void CategoriesEditWidget::storeContact(KContacts::Addressee &contact) const
{
QStringList categories;
......@@ -52,7 +72,7 @@ void CategoriesEditWidget::storeContact(KContacts::Addressee &contact) const
const Akonadi::Tag::List tags = mTagWidget->selection();
categories.reserve(tags.count());
for (const Akonadi::Tag &tag : tags) {
categories.append(tag.url().url());
categories.append(tag.name());
}
contact.setCategories(categories);
......
......@@ -10,6 +10,7 @@
#include <Akonadi/CategoriesEditAbstractWidget>
#include <KContacts/Addressee>
#include <KJob>
#include <QVariant>
namespace Akonadi
{
......@@ -34,4 +35,7 @@ public:
private:
Akonadi::TagWidget *const mTagWidget;
private Q_SLOTS:
void onMissingTagCreated(KJob *);
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment