[telepathy-qt4/master] Account: Removed manager method and use QDBusConnection/busName to create account.

Andre Moreira Magalhaes (andrunko) andre.magalhaes at collabora.co.uk
Tue Mar 31 15:52:52 PDT 2009


Rationale:
Before this change, to create a Account, the user would pass the
AccountManagerPtr as a param.
This way, it's desirable that manager always return the proper manager used on
Account creation, but this was not true in all cases, as the account manager
could be destroyed while the account was still alive.

Example:
    AccountPtr acc;
    {
        AccountManagerPtr am = AccountManager::create();
        ...
        acc = Account::create(am, path);
    }
    // am is detroyed - nobody referencing it, Account HAD a weakref to it
    Q_ASSERT(acc->manager()); // fail

As the AccountManager is supposed to be a singleton, there is no need for this
method, and to avoid misbehaviour let's remove it.
---
 TelepathyQt4/Client/account-manager.cpp |    2 +-
 TelepathyQt4/Client/account.cpp         |   51 +++++++++++++++++++-----------
 TelepathyQt4/Client/account.h           |   11 ++++---
 TelepathyQt4/Client/pending-account.cpp |    4 ++-
 examples/accounts/account-item.cpp      |    3 +-
 5 files changed, 44 insertions(+), 27 deletions(-)

diff --git a/TelepathyQt4/Client/account-manager.cpp b/TelepathyQt4/Client/account-manager.cpp
index e9ff6d8..05cfda0 100644
--- a/TelepathyQt4/Client/account-manager.cpp
+++ b/TelepathyQt4/Client/account-manager.cpp
@@ -318,7 +318,7 @@ AccountPtr AccountManager::accountForPath(const QString &path)
         return AccountPtr();
     }
 
-    AccountPtr account = Account::create(this, path);
+    AccountPtr account = Account::create(dbusConnection(), busName(), path);
     mPriv->accounts[path] = account;
     return account;
 }
diff --git a/TelepathyQt4/Client/account.cpp b/TelepathyQt4/Client/account.cpp
index b03bd25..c7dd8df 100644
--- a/TelepathyQt4/Client/account.cpp
+++ b/TelepathyQt4/Client/account.cpp
@@ -57,7 +57,7 @@ namespace Client
 
 struct Account::Private
 {
-    Private(Account *parent, const AccountManagerPtr &am);
+    Private(Account *parent);
     ~Private();
 
     void init();
@@ -71,7 +71,6 @@ struct Account::Private
 
     // Public object
     Account *parent;
-    WeakPtr<AccountManager> am;
 
     // Instance of generated interface class
     AccountInterface *baseInterface;
@@ -102,9 +101,8 @@ struct Account::Private
     ConnectionPtr connection;
 };
 
-Account::Private::Private(Account *parent, const AccountManagerPtr &am)
+Account::Private::Private(Account *parent)
     : parent(parent),
-      am(am),
       baseInterface(new AccountInterface(parent->dbusConnection(),
                     parent->busName(), parent->objectPath(), parent)),
       readinessHelper(parent->readinessHelper()),
@@ -198,43 +196,58 @@ const Feature Account::FeatureCore = Feature(Account::staticMetaObject.className
 const Feature Account::FeatureAvatar = Feature(Account::staticMetaObject.className(), 1);
 const Feature Account::FeatureProtocolInfo = Feature(Account::staticMetaObject.className(), 2);
 
-AccountPtr Account::create(const AccountManagerPtr &am,
+AccountPtr Account::create(const QString &busName,
         const QString &objectPath)
 {
-    return AccountPtr(new Account(am, objectPath));
+    return AccountPtr(new Account(busName, objectPath));
 }
 
+AccountPtr Account::create(const QDBusConnection &bus,
+        const QString &busName, const QString &objectPath)
+{
+    return AccountPtr(new Account(bus, busName, objectPath));
+}
+
+
 /**
  * Construct a new Account object.
  *
- * \param am Account manager.
+ * \param busName The account's well-known bus name
+ *                (sometimes called a "service name").
  * \param objectPath Account object path.
  */
-Account::Account(const AccountManagerPtr &am, const QString &objectPath)
-    : StatelessDBusProxy(am->dbusConnection(),
-            am->busName(), objectPath),
+Account::Account(const QString &busName, const QString &objectPath)
+    : StatelessDBusProxy(QDBusConnection::sessionBus(),
+            busName, objectPath),
       OptionalInterfaceFactory<Account>(this),
       ReadyObject(this, FeatureCore),
-      mPriv(new Private(this, am))
+      mPriv(new Private(this))
 {
 }
 
 /**
- * Class destructor.
+ * Construct a new Account object.
+ *
+ * \param bus QDBusConnection to use
+ * \param busName The account's well-known bus name
+ *                (sometimes called a "service name").
+ * \param objectPath Account object path.
  */
-Account::~Account()
+Account::Account(const QDBusConnection &bus,
+        const QString &busName, const QString &objectPath)
+    : StatelessDBusProxy(bus, busName, objectPath),
+      OptionalInterfaceFactory<Account>(this),
+      ReadyObject(this, FeatureCore),
+      mPriv(new Private(this))
 {
-    delete mPriv;
 }
 
 /**
- * Get the AccountManager from which this Account was created.
- *
- * \return A pointer to the AccountManager object that owns this Account.
+ * Class destructor.
  */
-AccountManagerPtr Account::manager() const
+Account::~Account()
 {
-    return mPriv->am;
+    delete mPriv;
 }
 
 /**
diff --git a/TelepathyQt4/Client/account.h b/TelepathyQt4/Client/account.h
index 806d1ca..6d0bc98 100644
--- a/TelepathyQt4/Client/account.h
+++ b/TelepathyQt4/Client/account.h
@@ -49,7 +49,6 @@ namespace Client
 {
 
 class Account;
-class AccountManager;
 class Connection;
 class PendingConnection;
 class PendingOperation;
@@ -70,13 +69,13 @@ public:
     static const Feature FeatureAvatar;
     static const Feature FeatureProtocolInfo;
 
-    static AccountPtr create(const AccountManagerPtr &am,
+    static AccountPtr create(const QDBusConnection &bus,
+            const QString &busName, const QString &objectPath);
+    static AccountPtr create(const QString &busName,
             const QString &objectPath);
 
     virtual ~Account();
 
-    AccountManagerPtr manager() const;
-
     bool isValidAccount() const;
 
     bool isEnabled() const;
@@ -178,7 +177,9 @@ Q_SIGNALS:
     void haveConnectionChanged(bool haveConnection);
 
 protected:
-    Account(const AccountManagerPtr &am, const QString &objectPath);
+    Account(const QString &busName, const QString &objectPath);
+    Account(const QDBusConnection &bus,
+            const QString &busName, const QString &objectPath);
 
     AccountInterface *baseInterface() const;
 
diff --git a/TelepathyQt4/Client/pending-account.cpp b/TelepathyQt4/Client/pending-account.cpp
index 21ab6ff..0260a81 100644
--- a/TelepathyQt4/Client/pending-account.cpp
+++ b/TelepathyQt4/Client/pending-account.cpp
@@ -126,7 +126,9 @@ AccountPtr PendingAccount::account() const
     }
 
     if (!mPriv->account) {
-        mPriv->account = Account::create(mPriv->manager, mPriv->objectPath.path());
+        AccountManagerPtr manager(mPriv->manager);
+        mPriv->account = Account::create(manager->dbusConnection(),
+                manager->busName(), mPriv->objectPath.path());
     }
 
     return mPriv->account;
diff --git a/examples/accounts/account-item.cpp b/examples/accounts/account-item.cpp
index 1655552..172fa74 100644
--- a/examples/accounts/account-item.cpp
+++ b/examples/accounts/account-item.cpp
@@ -31,7 +31,8 @@
 AccountItem::AccountItem(Telepathy::Client::AccountManagerPtr am,
         const QString &objectPath, QTableWidget *table, int row, QObject *parent)
     : QObject(parent),
-      mAcc(Telepathy::Client::Account::create(am, objectPath)),
+      mAcc(Telepathy::Client::Account::create(am->dbusConnection(),
+                  am->busName(), objectPath)),
       mTable(table),
       mRow(row)
 {
-- 
1.5.6.5




More information about the telepathy-commits mailing list