[SCM] ktp-common-internals packaging branch, master, updated. debian/15.12.1-2-1839-gf0635e9

Maximiliano Curia maxy at moszumanska.debian.org
Mon May 9 09:07:20 UTC 2016


Gitweb-URL: http://git.debian.org/?p=pkg-kde/applications/ktp-common-internals.git;a=commitdiff;h=0a4f3fd

The following commit has been merged in the master branch:
commit 0a4f3fd9c404d7a99b0a0de3b6bae0d9574384ee
Author: George Goldberg <george.goldberg at collabora.co.uk>
Date:   Thu Jul 21 11:46:53 2011 +0100

    Load all the accounts and contacts from Nepomuk on startup rather than running a query each time an account or contact is added.
---
 kpeople/nepomuk-feeder/nepomuk-storage.cpp    | 208 +++++++++++++-------------
 kpeople/nepomuk-feeder/tests/storage-test.cpp |  11 ++
 2 files changed, 114 insertions(+), 105 deletions(-)

diff --git a/kpeople/nepomuk-feeder/nepomuk-storage.cpp b/kpeople/nepomuk-feeder/nepomuk-storage.cpp
index 9278064..0138293 100644
--- a/kpeople/nepomuk-feeder/nepomuk-storage.cpp
+++ b/kpeople/nepomuk-feeder/nepomuk-storage.cpp
@@ -172,6 +172,9 @@ NepomukStorage::NepomukStorage(QObject *parent)
 {
     kDebug();
 
+    // *********************************************************************************************
+    // Nepomuk error handling
+
     // Create an instance of the Nepomuk Resource Manager, and connect to it's error signal.
     m_resourceManager = Nepomuk::ResourceManager::instance();
 
@@ -179,6 +182,9 @@ NepomukStorage::NepomukStorage(QObject *parent)
             SIGNAL(error(QString,int)),
             SLOT(onNepomukError(QString,int)));
 
+    // *********************************************************************************************
+    // Get the ME PIMO:Person and NCO:PersonContact (creating them if necessary)
+
     // Here we get the "me" person contact.
     // FIXME: Port to new OSCAF standard for accessing "me" as soon as it
     // becomes available.
@@ -210,70 +216,125 @@ NepomukStorage::NepomukStorage(QObject *parent)
         m_mePersonContact = Nepomuk::PersonContact("nepomuk:/myself-person-contact");
         me.addGroundingOccurrence(m_mePersonContact);
     }
-}
 
-NepomukStorage::~NepomukStorage()
-{
-    // Don't delete the Nepomuk Resource manager. Nepomuk should take care of this itself.
-}
+    // *********************************************************************************************
+    // Load all the relevant accounts and contacts that are already in Nepomuk.
 
-void NepomukStorage::onNepomukError(const QString &uri, int errorCode)
-{
-    kWarning() << "A Nepomuk Error occurred:" << uri << errorCode;
-}
+    // We load everything now, because this is much more efficient than re-running a query for each
+    // and every account and contact individually when we get to that one.
 
-void NepomukStorage::createAccount(const QString &path, const QString &id, const QString &protocol)
-{
-    kDebug() << "Creating a new Account";
+    // Query Nepomuk for all of the ME PersonContact's IMAccounts.
+    QList<Nepomuk::Query::Result> results;
+    {
+        using namespace Nepomuk::Query;
 
-    // First check if we already have this account.
-    if (m_accounts.contains(path)) {
-        kWarning() << "Account has already been created.";
-        return;
+        ComparisonTerm accountTerm(Nepomuk::Vocabulary::NCO::hasIMAccount(),
+                                   ResourceTerm(m_mePersonContact));
+        accountTerm.setInverted(true);
+
+        Query query(AndTerm(accountTerm, ResourceTypeTerm(Nepomuk::Vocabulary::NCO::IMAccount())));
+
+        bool queryResult = true;
+        results = QueryServiceClient::syncQuery(query, &queryResult);
+
+        if (!queryResult) {
+            kWarning() << "Query failed.";
+        }
+    }
+
+    kDebug() << results.size();
+
+    // Iterate over all the IMAccounts found.
+    foreach (const Nepomuk::Query::Result &result, results) {
+        Nepomuk::IMAccount foundImAccount(result.resource());
+        kDebug() << this << ": Found IM Account: " << foundImAccount.uri();
+
+        // If no Telepathy identifier, then the account is ignored.
+        if (foundImAccount.accountIdentifier().isEmpty()) {
+            kDebug() << "Account does not have a Telepathy Account Identifier. Oops. Ignoring.";
+            continue;
+        }
+
+        kDebug() << "Found a Telepathy account in Nepomuk, ID:"
+                 << foundImAccount.accountIdentifier();
+
+        // If it does have a telepathy identifier, then it is added to the cache.
+        m_accounts.insert(foundImAccount.accountIdentifier(), foundImAccount);
     }
 
-    // Query Nepomuk for IMAccount that the "me" PersonContact has with a specific path
-    QList< Nepomuk::Query::Result > results;
+    // Query Nepomuk for all know Contacts.
     {
         using namespace Nepomuk::Query;
 
-        // Match the account
-        ComparisonTerm accountTerm(Nepomuk::Vocabulary::NCO::hasIMAccount(), ResourceTerm(m_mePersonContact));
+        // Get the person contact owning this IMAccount
+        ComparisonTerm pcterm(Nepomuk::Vocabulary::NCO::hasIMAccount(),
+                              ResourceTypeTerm(Nepomuk::Vocabulary::NCO::PersonContact()));
+        pcterm.setVariableName("person");
+        pcterm.setInverted(true);
+
+        // Special case: if we're buddy of an account we do own, we want to create a new
+        // resource for that.
+        // This avoids race conditions and a lot of bad things.
+        ComparisonTerm accountTerm(Nepomuk::Vocabulary::NCO::hasIMAccount(),
+                                   ResourceTerm(m_mePersonContact));
         accountTerm.setInverted(true);
 
-        // Match the ID
-        ComparisonTerm pathTerm(Nepomuk::Vocabulary::Telepathy::accountIdentifier(),
-                                LiteralTerm(path), Nepomuk::Query::ComparisonTerm::Equal);
+        ComparisonTerm accessedByTerm(Nepomuk::Vocabulary::NCO::isAccessedBy(),
+                                      ResourceTypeTerm(Nepomuk::Vocabulary::NCO::IMAccount()));
+        accessedByTerm.setVariableName("accessedBy");
 
-        Query query(AndTerm(accountTerm, pathTerm,
+        Query query(AndTerm(pcterm, NegationTerm::negateTerm(accountTerm), accessedByTerm,
                             ResourceTypeTerm(Nepomuk::Vocabulary::NCO::IMAccount())));
 
         bool queryResult = true;
         results = QueryServiceClient::syncQuery(query, &queryResult);
 
         if (!queryResult) {
-            // TODO: Maybe an error notification here?
+            kWarning() << "Query failed.";
         }
     }
-    // TODO: Maybe check if there is more than one result, and throw an error?
-    kDebug() << results.size();
 
     // Iterate over all the IMAccounts found.
     foreach (const Nepomuk::Query::Result &result, results) {
         Nepomuk::IMAccount foundImAccount(result.resource());
-        kDebug() << this << ": Found IM Account: " << foundImAccount.uri();
+        Nepomuk::IMAccount foundPersonContact(result.additionalBinding("person").toUrl());
+        Nepomuk::IMAccount foundImAccountAccessedBy(result.additionalBinding("accessedBy").toUrl());
 
-        // See if the Account has the same Telepathy Account Identifier as the account this
-        // TelepathyAccount instance has been created to look after.
-        if (foundImAccount.accountIdentifier().isEmpty()) {
-            kDebug() << "Account does not have a Telepathy Account Identifier. Oops. Ignoring.";
+        // Check that the IM account only has one ID.
+        QStringList accountIDs = foundImAccount.imIDs();
+
+        if (accountIDs.size() != 1) {
+            kDebug() << "Account does not have 1 ID. Oops. Ignoring."
+            << "Number of Identifiers: "
+            << accountIDs.size();
             continue;
         }
 
-        kDebug() << this << ": Found the corresponding IMAccount in Nepomuk.";
-        // It matches.
-        // Add the IMAccount to the hash.
-        m_accounts.insert(path, foundImAccount);
+        // Cache the contact
+        m_contacts.insert(ContactIdentifier(foundImAccountAccessedBy.accountIdentifier(),
+                                            foundImAccount.imIDs().first()),
+                          ContactResources(foundPersonContact, foundImAccount));
+        kDebug() << "Contact found in Nepomuk. Caching.";
+    }
+}
+
+NepomukStorage::~NepomukStorage()
+{
+    // Don't delete the Nepomuk Resource manager. Nepomuk should take care of this itself.
+}
+
+void NepomukStorage::onNepomukError(const QString &uri, int errorCode)
+{
+    kWarning() << "A Nepomuk Error occurred:" << uri << errorCode;
+}
+
+void NepomukStorage::createAccount(const QString &path, const QString &id, const QString &protocol)
+{
+    kDebug() << "Creating a new Account";
+
+    // First check if we already have this account.
+    if (m_accounts.contains(path)) {
+        kWarning() << "Account has already been created.";
         return;
     }
 
@@ -348,79 +409,16 @@ void NepomukStorage::createContact(const QString &path, const QString &id)
         return;
     }
 
-    kDebug() << "Don't yet have a record for this contact. Query Nepomuk.";
-
-    // Assume that the Account this contact is related to exists.
-    Q_ASSERT(m_accounts.contains(path));
-    if (!m_accounts.contains(path)) {
-        kWarning() << "Account not found.";
-        return;
-    }
-
-    // Get the local account this contact is related to.
-    Nepomuk::IMAccount account = m_accounts.value(path);
-
-    // Query Nepomuk for all IM accounts that isBuddyOf the local account.
-    QList< Nepomuk::Query::Result > results;
-    {
-        using namespace Nepomuk::Query;
-
-        // Get the person contact owning this IMAccount
-        ComparisonTerm pcterm(Nepomuk::Vocabulary::NCO::hasIMAccount(),
-                              ResourceTypeTerm(Nepomuk::Vocabulary::NCO::PersonContact()));
-        pcterm.setVariableName("person");
-        pcterm.setInverted(true);
-
-        // Special case: if we're buddy of an account we do own, we want to create a new
-        // resource for that.
-        // This avoids race conditions and a lot of bad things.
-        ComparisonTerm accountTerm(Nepomuk::Vocabulary::NCO::hasIMAccount(),
-                                   ResourceTerm(m_mePersonContact));
-        accountTerm.setInverted(true);
-
-        // And the ID has to match
-        ComparisonTerm idTerm(Nepomuk::Vocabulary::NCO::imID(),
-                              LiteralTerm(id), Nepomuk::Query::ComparisonTerm::Equal);
-
-        // And the account has to be accessedBy this account.
-        ComparisonTerm accessedByTerm(Nepomuk::Vocabulary::NCO::isAccessedBy(),
-                                      ResourceTerm(account), Nepomuk::Query::ComparisonTerm::Equal);
-
-        Query query(AndTerm(pcterm, idTerm, NegationTerm::negateTerm(accountTerm), accessedByTerm,
-                            ResourceTypeTerm(Nepomuk::Vocabulary::NCO::IMAccount())));
-
-        bool queryResult = true;
-        results = QueryServiceClient::syncQuery(query, &queryResult);
-
-        if (!queryResult) {
-            // TODO: Maybe an error notification here?
-        }
-    }
-    // TODO: Maybe check if there is more than one result, and throw an error?
-
-    // Iterate over all the IMAccounts found.
-    foreach (const Nepomuk::Query::Result &result, results) {
-        Nepomuk::IMAccount foundImAccount(result.resource());
-        Nepomuk::IMAccount foundPersonContact(result.additionalBinding("person").toUrl());
-
-        // Check that the IM account only has one ID.
-        QStringList accountIDs = foundImAccount.imIDs();
+    // Contact not found. Need to create it.
+    kDebug() << "Contact not found in Nepomuk. Creating it.";
 
-        if (accountIDs.size() != 1) {
-            kDebug() << "Account does not have 1 ID. Oops. Ignoring."
-                        << "Number of Identifiers: "
-                           << accountIDs.size();
-                           continue;
-        }
-        // It matches, so cache the contact record.
-        m_contacts.insert(identifier, ContactResources(foundPersonContact, foundImAccount));
-        kDebug() << "Contact found in Nepomuk. Caching and Returning.";
+    Nepomuk::IMAccount account(m_accounts.value(path));
+    Q_ASSERT(m_accounts.keys().contains(path));
+    if (!m_accounts.keys().contains(path)) {
+        kWarning() << "Corresponding account not cached.";
         return;
     }
 
-    // Contact not found. Need to create it.
-    kDebug() << "Contact not found in Nepomuk. Creating it.";
-
     Nepomuk::PersonContact newPersonContact;
     Nepomuk::IMAccount newImAccount;
     Nepomuk::Thing newPimoPerson;
diff --git a/kpeople/nepomuk-feeder/tests/storage-test.cpp b/kpeople/nepomuk-feeder/tests/storage-test.cpp
index ed3d35c..c1771f1 100644
--- a/kpeople/nepomuk-feeder/tests/storage-test.cpp
+++ b/kpeople/nepomuk-feeder/tests/storage-test.cpp
@@ -155,6 +155,11 @@ void StorageTest::testCreateAccount()
     QVERIFY(imAcc2.exists());
     mePersonContact.addIMAccount(imAcc2);
 
+    // Need to delete the storage class to get it to see the newly added account.
+    m_storage->deleteLater();
+    m_storage = new NepomukStorage(this);
+    accounts = TestBackdoors::nepomukStorageAccounts(m_storage);
+
     // Now tell the storage about that account.
     m_storage->createAccount(QLatin1String("/foo/bar/baz/bong"),
                              QLatin1String("foo.bar at baz.bong"),
@@ -438,6 +443,12 @@ void StorageTest::testCreateContact()
     QCOMPARE(pC3.iMAccounts().size(), 1);
     QCOMPARE(pC3.iMAccounts().first(), imAcc3);
 
+    // Recreate the Storage class to get it to reload the contacts
+    m_storage->deleteLater();
+    m_storage = new NepomukStorage(this);
+    accounts = TestBackdoors::nepomukStorageAccounts(m_storage);
+    contacts = TestBackdoors::nepomukStorageContacts(m_storage);
+
     // Tell the storage about the contact
     m_storage->createContact(QLatin1String("/foo/bar/baz"),
                              QLatin1String("test2 at remote-contact.com"));

-- 
ktp-common-internals packaging



More information about the pkg-kde-commits mailing list