changeset 7:017925589d4c

Contact (formerly ContactT) now use shared data
author Goffi <goffi@goffi.org>
date Sun, 14 Aug 2011 18:03:54 +0200
parents 48045176d1c6
children c63d67895cbe
files bellaciao.cpp bridge.cpp contact.cpp contact.h contact_list.cpp contact_list.h dbus_types.cpp dbus_types.h org.goffi.sat.xml
diffstat 9 files changed, 120 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/bellaciao.cpp	Fri Aug 12 22:08:37 2011 +0200
+++ b/bellaciao.cpp	Sun Aug 14 18:03:54 2011 +0200
@@ -74,14 +74,17 @@
 
 void Bellaciao::fillContactList(const QString& profile)
 {
-    const QList<ContactT>& contacts = m_bridge->getContacts(profile);
+    //We first get the whole contact list
+    const QList<Contact>& contacts = m_bridge->getContacts(profile);
 
     for (int i=0; i<contacts.size(); i++) {
-        ContactT contact = contacts[i];
+        Contact contact(contacts[i]);
         contact.setProfile(profile);
         m_contactList->addContact(contact);
     }
 
+    //Then add connected contacts
+
 }
 
 //slots
@@ -95,10 +98,10 @@
 
 void Bellaciao::addContact(const QString &s_jid, StringDict attributes, const QStringList &groups, const QString &profile)
 {
-    ContactT contact;
-    contact.jid.fromString(s_jid);
+    Contact contact;
+    contact.getJid().fromString(s_jid);
     contact.setAttributes(attributes);
-    contact.groups = groups;
+    contact.getGroups() = groups;
     contact.setProfile(profile);
     m_contactList->addContact(contact);
 }
--- a/bridge.cpp	Fri Aug 12 22:08:37 2011 +0200
+++ b/bridge.cpp	Sun Aug 14 18:03:54 2011 +0200
@@ -32,8 +32,8 @@
         qDBusRegisterMetaType<StringDict>();
         qDBusRegisterMetaType<MenuT>();
         qDBusRegisterMetaType< QList<MenuT> >();
-        qDBusRegisterMetaType<ContactT>();
-        qDBusRegisterMetaType< QList<ContactT> >();
+        qDBusRegisterMetaType<Contact>();
+        qDBusRegisterMetaType< QList<Contact> >();
 
         _bridge_instance = new Bridge("org.goffi.SAT", "/org/goffi/SAT/bridge", QDBusConnection::sessionBus()); 
     }
--- a/contact.cpp	Fri Aug 12 22:08:37 2011 +0200
+++ b/contact.cpp	Sun Aug 14 18:03:54 2011 +0200
@@ -17,49 +17,90 @@
 */
 
 #include "contact.h"
+#include <QDebug>
 
-ContactT::ContactT()
+ContactData::ContactData()
     :m_connected(false), m_presence_to(false), m_presence_from(false), m_presence_ask(false)
 {
 }
 
-ContactT::~ContactT()
+ContactData::ContactData(const ContactData& other)
+    :QSharedData(other), jid(other.jid), groups(other.groups), m_connected(other.m_connected),
+     m_presence_to(other.m_presence_to), m_presence_from(other.m_presence_from),
+     m_presence_ask(other.m_presence_ask), m_profile(other.m_profile), m_name(other.m_name)
 {
 }
 
 
-void ContactT::setProfile(const QString& profile) {
-    m_profile = profile;
+Contact::Contact()
+{
+    d = new ContactData;
+}
+
+Contact::Contact(const Contact& other)
+    : d(other.d)
+{
 }
 
-const QString& ContactT::getProfile() const {
-    return m_profile;
+Jid& Contact::getJid() {
+    return d->jid;
+}
+
+const Jid& Contact::getJid() const {
+    return d->jid;
+}
+
+QList<QString>& Contact::getGroups() {
+    return d->groups;
+}
+
+const QList<QString>& Contact::getGroups() const {
+    return d->groups;
 }
 
-void ContactT::setAttributes(const QHash<QString, QString>& attr) {
-    m_name = attr.value("name");
-    m_presence_to = attr.value("to") == "True";
-    m_presence_from = attr.value("from") == "True";
-    m_presence_ask = attr.value("ask") == "True";
+void Contact::setProfile(const QString& profile)
+{
+    d->m_profile = profile;
+}
+
+const QString& Contact::getProfile() const
+{
+    return d->m_profile;
 }
 
-QHash<QString, QString> ContactT::getAttributes() const {
+void Contact::setAttributes(const QHash<QString, QString>& attr)
+{
+    d->m_name = attr.value("name");
+    d->m_presence_to = attr.value("to") == "True";
+    d->m_presence_from = attr.value("from") == "True";
+    d->m_presence_ask = attr.value("ask") == "True";
+}
+
+QHash<QString, QString> Contact::getAttributes() const
+{
     QHash<QString, QString> attr;
-    if (jid.isValid()) {
-        attr["name"] = m_name;
-        attr["to"] = m_presence_to ? "True" : "False";
-        attr["from"] = m_presence_from ? "True" : "False";
-        attr["ask"] = m_presence_ask ? "True" : "False";
+    if (d->jid.isValid()) {
+        attr["name"] = d->m_name;
+        attr["to"] = d->m_presence_to ? "True" : "False";
+        attr["from"] = d->m_presence_from ? "True" : "False";
+        attr["ask"] = d->m_presence_ask ? "True" : "False";
     }
 
     return attr;
 }
 
-const QString& ContactT::getName() const {
-    return m_name;
+const QString& Contact::getName() const
+{
+    return d->m_name;
 }
 
-bool ContactT::isConnected() {
-    return m_connected;
+void Contact::setName(const QString& name)
+{
+    d->m_name = name;
 }
 
+bool Contact::isConnected()
+{
+    return d->m_connected;
+}
+
--- a/contact.h	Fri Aug 12 22:08:37 2011 +0200
+++ b/contact.h	Sun Aug 14 18:03:54 2011 +0200
@@ -19,25 +19,20 @@
 #ifndef CONTACT_H
 #define CONTACT_H
 
-#include <QtGui>
+#include <QSharedData>
+#include <QString>
+#include <QHash>
+#include <QList>
 #include "jid.h"
 
-class ContactT {
+class ContactData: public QSharedData
+{
     public:
-        ContactT();
-        ~ContactT();
-        QList<QString> groups;
+        ContactData();
+        ContactData(const ContactData& other);
+        
         Jid jid;
-        void setProfile(const QString& profile);
-        const QString& getProfile() const;
-        void setAttributes(const QHash<QString, QString>& attr);
-        QHash<QString, QString> getAttributes() const;
-        const QString& getName() const;
-
-        bool isConnected();
-
-
-    private:
+        QList<QString> groups;
         bool m_connected;
         bool m_presence_to, m_presence_from, m_presence_ask;
         QString m_profile, m_name;
@@ -45,5 +40,26 @@
         
 };
 
+class Contact {
+    public:
+        Contact();
+        Contact(const Contact &other);
+       
+        Jid& getJid();
+        const Jid& getJid() const;
+        QList<QString>& getGroups();
+        const QList<QString>& getGroups() const;
+        void setProfile(const QString& profile);
+        const QString& getProfile() const;
+        void setAttributes(const QHash<QString, QString>& attr);
+        QHash<QString, QString> getAttributes() const;
+        const QString& getName() const;
+        void setName(const QString& name);
+        bool isConnected();
+
+    private:
+        QExplicitlySharedDataPointer<ContactData> d;
+};
+
 
 #endif
--- a/contact_list.cpp	Fri Aug 12 22:08:37 2011 +0200
+++ b/contact_list.cpp	Sun Aug 14 18:03:54 2011 +0200
@@ -31,13 +31,16 @@
      
  }
 
-void ContactList::addContact(const ContactT& contact)
+void ContactList::addContact(const Contact& contact)
 {
-    const QString &txt = contact.getName().isEmpty() ? contact.jid.getString() : contact.getName();
+    const QString & _jid = contact.getJid().getBareString();
+    m_contactMap.insert(_jid, contact);
+    const QString &txt = contact.getName().isEmpty() ? contact.getJid().getString() : contact.getName();
     new QListWidgetItem(txt, listWidget);
 }
 
 void ContactList::clear()
 {
     listWidget->clear();
+    m_contactMap.clear();
 }
--- a/contact_list.h	Fri Aug 12 22:08:37 2011 +0200
+++ b/contact_list.h	Sun Aug 14 18:03:54 2011 +0200
@@ -27,11 +27,12 @@
     
     public:
         ContactList(QWidget* parent = 0);
-        void addContact(const ContactT& contact);
+        void addContact(const Contact& contact);
         void clear();
     private:
         QWidget* mainWidget;
         QListWidget* listWidget;
+        QMap<QString, Contact> m_contactMap;
 };
 
 #endif
--- a/dbus_types.cpp	Fri Aug 12 22:08:37 2011 +0200
+++ b/dbus_types.cpp	Sun Aug 14 18:03:54 2011 +0200
@@ -32,22 +32,22 @@
     return argument;
 }
 
-QDBusArgument &operator<<(QDBusArgument &argument, const ContactT &contact)
+QDBusArgument &operator<<(QDBusArgument &argument, const Contact &contact)
 {
     argument.beginStructure();
     StringDict attr = contact.getAttributes();
-    argument << contact.jid << attr << contact.groups;
+    argument << contact.getJid() << attr << contact.getGroups();
     argument.endStructure();
     return argument;
 }
-const QDBusArgument &operator>>(const QDBusArgument &argument, ContactT &contact)
+const QDBusArgument &operator>>(const QDBusArgument &argument, Contact &contact)
 {
     argument.beginStructure();
-    argument >> contact.jid;
+    argument >> contact.getJid();
     StringDict attr;
     argument >> attr;
     contact.setAttributes(attr);
-    argument >> contact.groups;
+    argument >> contact.getGroups();
     argument.endStructure();
     return argument;
 }
--- a/dbus_types.h	Fri Aug 12 22:08:37 2011 +0200
+++ b/dbus_types.h	Sun Aug 14 18:03:54 2011 +0200
@@ -54,8 +54,8 @@
 typedef QHash<QString, StringDict> ActionResultExtDataT;
 
 Q_DECLARE_METATYPE(StringDict);
-Q_DECLARE_METATYPE(ContactT);
-Q_DECLARE_METATYPE(QList<ContactT>);
+Q_DECLARE_METATYPE(Contact);
+Q_DECLARE_METATYPE(QList<Contact>);
 Q_DECLARE_METATYPE(HistoryT);
 Q_DECLARE_METATYPE(PresenceStatusT);
 Q_DECLARE_METATYPE(MenuT);
@@ -64,8 +64,8 @@
 
 QDBusArgument &operator<<(QDBusArgument &argument, const Jid &jid);
 const QDBusArgument &operator>>(const QDBusArgument &argument, Jid &jid);
-QDBusArgument &operator<<(QDBusArgument &argument, const ContactT &contact);
-const QDBusArgument &operator>>(const QDBusArgument &argument, ContactT &contact);
+QDBusArgument &operator<<(QDBusArgument &argument, const Contact &contact);
+const QDBusArgument &operator>>(const QDBusArgument &argument, Contact &contact);
 QDBusArgument &operator<<(QDBusArgument &argument, const MenuT &menu);
 const QDBusArgument &operator>>(const QDBusArgument &argument, MenuT &menu);
 QDBusArgument &operator<<(QDBusArgument &argument, const MessageT &message);
--- a/org.goffi.sat.xml	Fri Aug 12 22:08:37 2011 +0200
+++ b/org.goffi.sat.xml	Sun Aug 14 18:03:54 2011 +0200
@@ -83,7 +83,7 @@
 		<method name="getContacts">
 			<arg direction="in" name="profile_key" type="s"/>
 			<arg direction="out" type="a(sa{ss}as)"/>
-			<annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="QList&lt;ContactT&gt;"/>
+			<annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="QList&lt;Contact&gt;"/>
 		</method>
 		<method name="getHistory">
 			<arg direction="in" name="from_jid" type="s"/>