[pim/kdepim-addons] kaddressbook/plugins/sendvcards: Generate unique name otherwise if two vcard as same real name it will

Laurent Montel <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 949829537592d90a94d21bcef5dfaf41f59f0c3e by Laurent Montel.
Committed on 16/08/2026 at 06:59.
Pushed by mlaurent into branch 'master'.

Generate unique name otherwise if two vcard as same real name it will

override other file

M  +36   -1    kaddressbook/plugins/sendvcards/autotests/sendvcardsjobtest.cpp
M  +1    -0    kaddressbook/plugins/sendvcards/autotests/sendvcardsjobtest.h
M  +23   -2    kaddressbook/plugins/sendvcards/sendvcardsjob.cpp
M  +1    -0    kaddressbook/plugins/sendvcards/sendvcardsjob.h

https://invent.kde.org/pim/kdepim-addons/-/commit/949829537592d90a94d21bcef5dfaf41f59f0c3e

diff --git a/kaddressbook/plugins/sendvcards/autotests/sendvcardsjobtest.cpp b/kaddressbook/plugins/sendvcards/autotests/sendvcardsjobtest.cpp
index 5bf3992f3..08f12bcd2 100644
--- a/kaddressbook/plugins/sendvcards/autotests/sendvcardsjobtest.cpp
+++ b/kaddressbook/plugins/sendvcards/autotests/sendvcardsjobtest.cpp
@@ -10,6 +10,9 @@
 #include "sendvcards/sendvcardsjob.h"
 #include <Akonadi/Item>
 #include <QTest>
+
+QTEST_MAIN(SendVcardsJobTest)
+using namespace Qt::Literals::StringLiterals;
 SendVcardsJobTest::SendVcardsJobTest(QObject *parent)
     : QObject(parent)
 {
@@ -39,6 +42,38 @@ void SendVcardsJobTest::shouldChangeExportVersion()
     QCOMPARE(job.version(), KContacts::VCardConverter::v3_0);
 }
 
-QTEST_MAIN(SendVcardsJobTest)
+void SendVcardsJobTest::shouldGenerageUniqueFileName()
+{
+    {
+        QStringList existingVcard;
+        QString contactRealName = u"bla"_s;
+        QCOMPARE(KABSendVCards::SendVcardsJob::createUniqueAttachmentName(contactRealName, existingVcard), u"bla"_s);
+    }
+    {
+        QStringList existingVcard;
+        QString contactRealName;
+        QCOMPARE(KABSendVCards::SendVcardsJob::createUniqueAttachmentName(contactRealName, existingVcard), u"vcard"_s);
+    }
+    {
+        QStringList existingVcard = QStringList() << u"bla"_s;
+        QString contactRealName = u"bla"_s;
+        QCOMPARE(KABSendVCards::SendVcardsJob::createUniqueAttachmentName(contactRealName, existingVcard), u"bla_1"_s);
+    }
+    {
+        QStringList existingVcard = QStringList() << u"bla"_s << u"bla_1"_s;
+        QString contactRealName = u"bla"_s;
+        QCOMPARE(KABSendVCards::SendVcardsJob::createUniqueAttachmentName(contactRealName, existingVcard), u"bla_2"_s);
+    }
+    {
+        QStringList existingVcard = QStringList() << u"bla"_s << u"bla_1"_s;
+        QString contactRealName;
+        QCOMPARE(KABSendVCards::SendVcardsJob::createUniqueAttachmentName(contactRealName, existingVcard), u"vcard"_s);
+    }
+    {
+        QStringList existingVcard = QStringList() << u"bla"_s << u"bla_1"_s << u"vcard"_s;
+        QString contactRealName;
+        QCOMPARE(KABSendVCards::SendVcardsJob::createUniqueAttachmentName(contactRealName, existingVcard), u"vcard_1"_s);
+    }
+}
 
 #include "moc_sendvcardsjobtest.cpp"
diff --git a/kaddressbook/plugins/sendvcards/autotests/sendvcardsjobtest.h b/kaddressbook/plugins/sendvcards/autotests/sendvcardsjobtest.h
index 689c292c0..c69ac304b 100644
--- a/kaddressbook/plugins/sendvcards/autotests/sendvcardsjobtest.h
+++ b/kaddressbook/plugins/sendvcards/autotests/sendvcardsjobtest.h
@@ -20,4 +20,5 @@ private Q_SLOTS:
     void shouldNotStartWhenListAddressIsEmpty();
     void shouldHasDefaultValue();
     void shouldChangeExportVersion();
+    void shouldGenerageUniqueFileName();
 };
diff --git a/kaddressbook/plugins/sendvcards/sendvcardsjob.cpp b/kaddressbook/plugins/sendvcards/sendvcardsjob.cpp
index 611b87705..d9a7b2fde 100644
--- a/kaddressbook/plugins/sendvcards/sendvcardsjob.cpp
+++ b/kaddressbook/plugins/sendvcards/sendvcardsjob.cpp
@@ -17,7 +17,7 @@
 #include <QTemporaryDir>
 
 using namespace KABSendVCards;
-
+using namespace Qt::Literals::StringLiterals;
 SendVcardsJob::SendVcardsJob(const Akonadi::Item::List &listItem, QObject *parent)
     : QObject(parent)
     , mListItem(listItem)
@@ -34,6 +34,24 @@ SendVcardsJob::~SendVcardsJob()
     mAttachmentTemporary = nullptr;
 }
 
+QString SendVcardsJob::createUniqueAttachmentName(const QString &contactRealName, const QStringList &existingVcard)
+{
+    QString newContactRealName = contactRealName;
+    if (newContactRealName.isEmpty()) {
+        newContactRealName = QStringLiteral("vcard");
+    }
+    if (existingVcard.contains(newContactRealName)) {
+        int index = 0;
+        QString uniqueContactRealName = newContactRealName;
+        do {
+            index++;
+            uniqueContactRealName = u"%1_%2"_s.arg(newContactRealName).arg(QString::number(index));
+        } while (existingVcard.contains(uniqueContactRealName));
+        newContactRealName = uniqueContactRealName;
+    }
+    return newContactRealName;
+}
+
 bool SendVcardsJob::start()
 {
     if (mListItem.isEmpty()) {
@@ -44,6 +62,7 @@ bool SendVcardsJob::start()
         return false;
     }
 
+    QStringList realNameLst;
     for (const Akonadi::Item &item : std::as_const(mListItem)) {
         if (item.hasPayload<KContacts::Addressee>()) {
             const auto contact = item.payload<KContacts::Addressee>();
@@ -52,7 +71,9 @@ bool SendVcardsJob::start()
             KContacts::adaptIMAttributes(data);
             createTemporaryDir();
             const QString contactRealName(contact.realName());
-            const QString attachmentName = (contactRealName.isEmpty() ? QStringLiteral("vcard") : contactRealName) + QStringLiteral(".vcf");
+            const QString generatedUniqueAttachmentName = createUniqueAttachmentName(contactRealName, realNameLst);
+            const QString attachmentName = generatedUniqueAttachmentName + QStringLiteral(".vcf");
+            realNameLst.append(generatedUniqueAttachmentName);
             createTemporaryFile(data, attachmentName);
         } else if (item.hasPayload<KContacts::ContactGroup>()) {
             ++mExpandGroupJobCount;
diff --git a/kaddressbook/plugins/sendvcards/sendvcardsjob.h b/kaddressbook/plugins/sendvcards/sendvcardsjob.h
index ed6f513c3..efb08f932 100644
--- a/kaddressbook/plugins/sendvcards/sendvcardsjob.h
+++ b/kaddressbook/plugins/sendvcards/sendvcardsjob.h
@@ -28,6 +28,7 @@ public:
     [[nodiscard]] KContacts::VCardConverter::Version version() const;
     void setVersion(KContacts::VCardConverter::Version version);
 
+    [[nodiscard]] static QString createUniqueAttachmentName(const QString &contactRealName, const QStringList &existingVcard);
 Q_SIGNALS:
     void sendVCardsError(const QString &error);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.