[Telepathy-commits] [telepathy-qt4/master] Account: Make AccountManager/PendingAccount return a QSharedPointer<Account> instead of Account *.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Fri Feb 13 09:32:55 PST 2009


---
 TelepathyQt4/Client/account-manager.cpp |   33 +++++++++++++++++++++---------
 TelepathyQt4/Client/account-manager.h   |   11 +++++----
 TelepathyQt4/Client/pending-account.cpp |   16 ++++++++------
 TelepathyQt4/Client/pending-account.h   |    5 ++-
 tests/dbus/account-basics.cpp           |    5 ++-
 5 files changed, 44 insertions(+), 26 deletions(-)

diff --git a/TelepathyQt4/Client/account-manager.cpp b/TelepathyQt4/Client/account-manager.cpp
index 36d4953..7768575 100644
--- a/TelepathyQt4/Client/account-manager.cpp
+++ b/TelepathyQt4/Client/account-manager.cpp
@@ -77,6 +77,7 @@ struct AccountManager::Private
     AccountManager::Features features;
     QSet<QString> validAccountPaths;
     QSet<QString> invalidAccountPaths;
+    QMap<QString, QSharedPointer<Account> > accounts;
 };
 
 class AccountManager::Private::PendingReady : public PendingOperation
@@ -240,7 +241,7 @@ QStringList AccountManager::allAccountPaths() const
  * \return A list of Account objects
  * \sa invalidAccounts(), allAccounts(), accountsForPaths()
  */
-QList<Account *> AccountManager::validAccounts()
+QList<QSharedPointer<Account> > AccountManager::validAccounts()
 {
     return accountsForPaths(validAccountPaths());
 }
@@ -257,7 +258,7 @@ QList<Account *> AccountManager::validAccounts()
  * \return A list of Account objects
  * \sa validAccounts(), allAccounts(), accountsForPaths()
  */
-QList<Account *> AccountManager::invalidAccounts()
+QList<QSharedPointer<Account> > AccountManager::invalidAccounts()
 {
     return accountsForPaths(invalidAccountPaths());
 }
@@ -274,7 +275,7 @@ QList<Account *> AccountManager::invalidAccounts()
  * \return A list of Account objects
  * \sa validAccounts(), invalidAccounts(), accountsForPaths()
  */
-QList<Account *> AccountManager::allAccounts()
+QList<QSharedPointer<Account> > AccountManager::allAccounts()
 {
     return accountsForPaths(allAccountPaths());
 }
@@ -292,11 +293,21 @@ QList<Account *> AccountManager::allAccounts()
  * \return A list of Account objects
  * \sa validAccounts(), invalidAccounts(), accountsForPaths()
  */
-Account *AccountManager::accountForPath(const QString &path)
+QSharedPointer<Account> AccountManager::accountForPath(const QString &path)
 {
-    // TODO should we use AM as parent of account,
-    //      or receive parent as a param?
-    return new Account(this, path, this);
+    if (mPriv->accounts.contains(path)) {
+        return mPriv->accounts[path];
+    }
+
+    if (!mPriv->validAccountPaths.contains(path) &&
+        !mPriv->invalidAccountPaths.contains(path)) {
+        return QSharedPointer<Account>();
+    }
+
+    QSharedPointer<Account> account = QSharedPointer<Account>(
+            new Account(this, path));
+    mPriv->accounts[path] = account;
+    return account;
 }
 
 /**
@@ -312,9 +323,9 @@ Account *AccountManager::accountForPath(const QString &path)
  * \return A list of Account objects
  * \sa validAccounts(), invalidAccounts(), allAccounts()
  */
-QList<Account *> AccountManager::accountsForPaths(const QStringList &paths)
+QList<QSharedPointer<Account> > AccountManager::accountsForPaths(const QStringList &paths)
 {
-    QList<Account *> result;
+    QList<QSharedPointer<Account> > result;
     Q_FOREACH (const QString &path, paths) {
         result << accountForPath(path);
     }
@@ -497,7 +508,9 @@ void AccountManager::onAccountRemoved(const QDBusObjectPath &objectPath)
     debug() << "Account removed:" << path;
     mPriv->validAccountPaths.remove(path);
     mPriv->invalidAccountPaths.remove(path);
-
+    if (mPriv->accounts.contains(path)) {
+        mPriv->accounts.remove(path);
+    }
     Q_EMIT accountRemoved(path);
 }
 
diff --git a/TelepathyQt4/Client/account-manager.h b/TelepathyQt4/Client/account-manager.h
index 715e28d..802adf5 100644
--- a/TelepathyQt4/Client/account-manager.h
+++ b/TelepathyQt4/Client/account-manager.h
@@ -33,6 +33,7 @@
 #include <TelepathyQt4/Client/OptionalInterfaceFactory>
 
 #include <QDBusObjectPath>
+#include <QSharedPointer>
 #include <QString>
 #include <QVariantMap>
 
@@ -74,12 +75,12 @@ public:
     QStringList invalidAccountPaths() const;
     QStringList allAccountPaths() const;
 
-    QList<Account *> validAccounts();
-    QList<Account *> invalidAccounts();
-    QList<Account *> allAccounts();
+    QList<QSharedPointer<Account> > validAccounts();
+    QList<QSharedPointer<Account> > invalidAccounts();
+    QList<QSharedPointer<Account> > allAccounts();
 
-    Account *accountForPath(const QString &path);
-    QList<Account *> accountsForPaths(const QStringList &paths);
+    QSharedPointer<Account> accountForPath(const QString &path);
+    QList<QSharedPointer<Account> > accountsForPaths(const QStringList &paths);
 
     PendingAccount *createAccount(const QString &connectionManager,
             const QString &protocol, const QString &displayName,
diff --git a/TelepathyQt4/Client/pending-account.cpp b/TelepathyQt4/Client/pending-account.cpp
index 4608866..cbb4783 100644
--- a/TelepathyQt4/Client/pending-account.cpp
+++ b/TelepathyQt4/Client/pending-account.cpp
@@ -57,7 +57,7 @@ struct PendingAccount::Private
     }
 
     AccountManager *manager;
-    Account *account;
+    QSharedPointer<Account> account;
 };
 
 /**
@@ -116,12 +116,14 @@ AccountManager *PendingAccount::manager() const
  *
  * \return Account object.
  */
-Account *PendingAccount::account() const
+QSharedPointer<Account> PendingAccount::account() const
 {
     if (!isFinished()) {
-        warning() <<
-            "PendingAccount::account called before finished, returning 0";
-        return 0;
+        warning() << "PendingAccount::account called before finished, returning 0";
+        return QSharedPointer<Account>();
+    } else if (!isValid()) {
+        warning() << "PendingAccount::account called when not valid, returning 0";
+        return QSharedPointer<Account>();
     }
 
     return mPriv->account;
@@ -135,8 +137,8 @@ void PendingAccount::onCallFinished(QDBusPendingCallWatcher *watcher)
         debug() << "Got reply to AccountManager.CreateAccount";
         QDBusObjectPath objectPath = reply.value();
         debug() << "Creating account for objectPath: " << objectPath.path();
-        mPriv->account = new Account(mPriv->manager,
-                objectPath.path(), mPriv->manager);
+        mPriv->account = QSharedPointer<Account>(
+                new Account(mPriv->manager, objectPath.path()));
         setFinished();
     } else {
         debug().nospace() <<
diff --git a/TelepathyQt4/Client/pending-account.h b/TelepathyQt4/Client/pending-account.h
index a5719b5..c483497 100644
--- a/TelepathyQt4/Client/pending-account.h
+++ b/TelepathyQt4/Client/pending-account.h
@@ -26,8 +26,10 @@
 #error IN_TELEPATHY_QT4_HEADER
 #endif
 
+#include <TelepathyQt4/Client/Account>
 #include <TelepathyQt4/Client/PendingOperation>
 
+#include <QSharedPointer>
 #include <QString>
 #include <QVariantMap>
 
@@ -39,7 +41,6 @@ namespace Client
 {
 
 class AccountManager;
-class Account;
 
 class PendingAccount : public PendingOperation
 {
@@ -49,7 +50,7 @@ public:
     ~PendingAccount();
 
     AccountManager *manager() const;
-    Account *account() const;
+    QSharedPointer<Account> account() const;
 
 private Q_SLOTS:
     void onCallFinished(QDBusPendingCallWatcher *watcher);
diff --git a/tests/dbus/account-basics.cpp b/tests/dbus/account-basics.cpp
index 96f320b..0226531 100644
--- a/tests/dbus/account-basics.cpp
+++ b/tests/dbus/account-basics.cpp
@@ -76,6 +76,7 @@ void TestAccountBasics::testBasics()
                     SIGNAL(finished(Telepathy::Client::PendingOperation *)),
                     SLOT(expectSuccessfulCall(Telepathy::Client::PendingOperation *))));
     QCOMPARE(mLoop->exec(), 0);
+    QVERIFY(!pacc->account().isNull());
 
     QCOMPARE(mAM->interfaces(), QStringList());
 
@@ -88,7 +89,7 @@ void TestAccountBasics::testBasics()
              QStringList() <<
                "/org/freedesktop/Telepathy/Account/foo/bar/Account0");
 
-    Account *acc = mAM->accountForPath(
+    QSharedPointer<Account> acc = mAM->accountForPath(
             "/org/freedesktop/Telepathy/Account/foo/bar/Account0");
     QVERIFY(connect(acc->becomeReady(),
                     SIGNAL(finished(Telepathy::Client::PendingOperation *)),
@@ -104,7 +105,7 @@ void TestAccountBasics::testBasics()
 
     QCOMPARE(acc->avatar().MIMEType, QString("image/png"));
 
-    QVERIFY(connect(acc,
+    QVERIFY(connect(acc.data(),
                     SIGNAL(avatarChanged(const Telepathy::Avatar &)),
                     SLOT(onAvatarChanged(const Telepathy::Avatar &))));
 
-- 
1.5.6.5



More information about the telepathy-commits mailing list