changeset 4:220e5619bf87

Profiles selection now fill contact list + new Jid class
author Goffi <goffi@goffi.org>
date Thu, 11 Aug 2011 00:02:25 +0200
parents 2195295a2058
children 0412dc960e3a
files bellaciao.cpp bellaciao.h bellaciao.pro bridge.cpp contact.cpp contact.h contact_list.cpp contact_list.h dbus_types.cpp dbus_types.h jid.cpp jid.h main.cpp session.cpp session.h settings.cpp settings.h
diffstat 17 files changed, 396 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/bellaciao.cpp	Mon Aug 08 10:59:34 2011 +0200
+++ b/bellaciao.cpp	Thu Aug 11 00:02:25 2011 +0200
@@ -18,11 +18,15 @@
 
 #include "bellaciao.h"
 #include "settings.h"
+#include "jid.h"
+
+Bellaciao* Bellaciao::_instance=0;
 
 Bellaciao::Bellaciao()
     :QMainWindow(0)
 {
     m_bridge = Bridge::getBridge();
+    m_session = Session::getSession();
 
     //Ui
     setupUi(this);
@@ -35,22 +39,63 @@
     connect(actionSettings, SIGNAL(triggered()), this, SLOT(showSettings()));
     connect(actionQuit, SIGNAL(triggered()), this, SLOT(quit()));
 
+    //D-Bus Signals
+    connect(m_bridge, SIGNAL(connected(QString)), this, SLOT(connected(QString)));
+    connect(m_bridge, SIGNAL(newContact(const QString&, StringDict, const QStringList&, const QString&)), this, SLOT(addContact(const QString&, StringDict, const QStringList&, const QString&)));
+
 
     addDockWidget(Qt::LeftDockWidgetArea, m_contactList);
     
-    
-    setWindowTitle("Bellaciao");
-    QList<MenuT> menus = m_bridge->getMenus();
-    foreach(const MenuT &menu, menus)
-        qDebug() << menu.name;
+   
+    //TODO: retrieve saved session's settings from SàT, and fill contactList 
+        
+
+}
+
+Bellaciao* Bellaciao::getInstance()
+{
+    if (_instance==0)
+        _instance = new Bellaciao;
+
+    return _instance;
+}
 
-    QList<ContactT> contacts = m_bridge->getContacts("jabberfr");
+void Bellaciao::connectProfiles()
+/* Connect profiles, and fill contact lists*/
+{
+    const QList<QString> & profiles = m_session->getProfiles();
+    m_contactList->clear();
+    foreach(const QString& profile, profiles) {
+        //We autoconnect profile at the moment, behaviour can change in the future
+        if (m_bridge->isConnected(profile))
+            fillContactList(profile);
+        m_bridge->connect(profile);
+    }
+}
+
+void Bellaciao::fillContactList(const QString& profile)
+{
+   
+    const QList<ContactT>& contacts = m_bridge->getContacts(profile);
 
     foreach(const ContactT &contact, contacts)
         m_contactList->addContact(contact.jid);
-        //qDebug() << contact.jid << contact.attributes << contact.groups;
-        
+
+}
+
+//slots
+
+//D-Bus slots
 
+void Bellaciao::connected(const QString & profile)
+{
+    fillContactList(profile);
+}
+
+void Bellaciao::addContact(const QString &s_jid, StringDict attributes, const QStringList &groups, const QString &profile)
+{
+    Jid jid = Jid(s_jid);
+    m_contactList->addContact(jid);
 }
 
 void Bellaciao::quit()
@@ -70,7 +115,6 @@
 
 void Bellaciao::showSettings()
 {
-    qDebug("Show settings");
     Settings::getDialog()->show();
     Settings::getDialog()->raise();
 }
@@ -81,5 +125,4 @@
         showNormal();
     else
         showFullScreen();
-    qDebug("on_actionToggle_fullscreen_triggered");
 }
--- a/bellaciao.h	Mon Aug 08 10:59:34 2011 +0200
+++ b/bellaciao.h	Thu Aug 11 00:02:25 2011 +0200
@@ -24,12 +24,17 @@
 #include "ui_main_win.h"
 #include "contact_list.h"
 #include "bridge.h"
+#include "session.h"
 
 class Bellaciao : public QMainWindow, private Ui::MainWindow
 {
     Q_OBJECT
 
     public:
+        static Bellaciao* getInstance();
+        void connectProfiles();
+
+    protected:
         Bellaciao();
         
 
@@ -37,10 +42,16 @@
         void quit();
         void showSettings();
         void on_actionToggle_fullscreen_triggered();
+        //D-Bus slots
+        void connected(const QString& profile);
+        void addContact(const QString& jid, StringDict attributes, const QStringList& groups, const QString& profile);
 
     private:
+        void fillContactList(const QString& profile);
+        static Bellaciao* _instance;
+        Bridge* m_bridge;
+        Session* m_session;
         ContactList* m_contactList;
-        Bridge* m_bridge;
 };
 
 #endif
--- a/bellaciao.pro	Mon Aug 08 10:59:34 2011 +0200
+++ b/bellaciao.pro	Thu Aug 11 00:02:25 2011 +0200
@@ -10,6 +10,6 @@
 CONFIG += debug
 
 # Input
-HEADERS += bellaciao.h contact_list.h dbus_bridge.h dbus_types.h bridge.h settings.h session.h
-SOURCES += main.cpp bellaciao.cpp contact_list.cpp dbus_bridge.cpp dbus_types.cpp bridge.cpp settings.cpp session.cpp
+HEADERS += bellaciao.h contact_list.h dbus_bridge.h dbus_types.h bridge.h settings.h session.h contact.h jid.h
+SOURCES += main.cpp bellaciao.cpp contact_list.cpp dbus_bridge.cpp dbus_types.cpp bridge.cpp settings.cpp session.cpp contact.cpp jid.cpp
 FORMS += main_win.ui settings.ui
--- a/bridge.cpp	Mon Aug 08 10:59:34 2011 +0200
+++ b/bridge.cpp	Thu Aug 11 00:02:25 2011 +0200
@@ -29,6 +29,7 @@
 Bridge* Bridge::getBridge()
 {
     if (_bridge_instance == 0) {
+        qDBusRegisterMetaType<Jid>();
         qDBusRegisterMetaType<StringDict>();
         qDBusRegisterMetaType<MenuT>();
         qDBusRegisterMetaType< QList<MenuT> >();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contact.cpp	Thu Aug 11 00:02:25 2011 +0200
@@ -0,0 +1,35 @@
+/*
+Bellaciao: a Salut à Toi frontend
+Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "contact.h"
+
+ContactT::ContactT()
+{
+    connected = false;
+}
+
+ContactT::~ContactT()
+{
+}
+
+
+bool ContactT::isConnected() {
+    return connected;
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/contact.h	Thu Aug 11 00:02:25 2011 +0200
@@ -0,0 +1,44 @@
+/*
+Bellaciao: a Salut à Toi frontend
+Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef CONTACT_H
+#define CONTACT_H
+
+#include "jid.h"
+#include <QtGui>
+
+typedef QHash<QString, QString> StringDict;
+
+class ContactT {
+    public:
+        ContactT();
+        ~ContactT();
+        Jid jid;
+        StringDict attributes;
+        QList<QString> groups;
+
+        bool isConnected();
+
+
+    private:
+        bool connected;
+        
+};
+
+
+#endif
--- a/contact_list.cpp	Mon Aug 08 10:59:34 2011 +0200
+++ b/contact_list.cpp	Thu Aug 11 00:02:25 2011 +0200
@@ -1,5 +1,5 @@
 /*
-Mignonne: a Salut à Toi frontend
+Bellaciao: a Salut à Toi frontend
 Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
 
 This program is free software: you can redistribute it and/or modify
@@ -18,7 +18,6 @@
 
 #include "contact_list.h"
 
-
 ContactList::ContactList(QWidget * parent):
     QDockWidget(tr("Contacts"), parent)
 
@@ -32,7 +31,12 @@
      
  }
 
-void ContactList::addContact(const QString& jid)
+void ContactList::addContact(const Jid& jid)
 {
-    new QListWidgetItem(jid, listWidget);
+    new QListWidgetItem(jid.getString(), listWidget);
 }
+
+void ContactList::clear()
+{
+    listWidget->clear();
+}
--- a/contact_list.h	Mon Aug 08 10:59:34 2011 +0200
+++ b/contact_list.h	Thu Aug 11 00:02:25 2011 +0200
@@ -1,5 +1,5 @@
 /*
-Mignonne: a Salut à Toi frontend
+Bellaciao: a Salut à Toi frontend
 Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
 
 This program is free software: you can redistribute it and/or modify
@@ -20,13 +20,15 @@
 #define CONTACTLIST_H
 
 #include <QtGui>
+#include "jid.h"
 
 class ContactList : public QDockWidget {
     Q_OBJECT
     
     public:
         ContactList(QWidget* parent = 0);
-        void addContact(const QString& jid);
+        void addContact(const Jid& jid);
+        void clear();
     private:
         QWidget* mainWidget;
         QListWidget* listWidget;
--- a/dbus_types.cpp	Mon Aug 08 10:59:34 2011 +0200
+++ b/dbus_types.cpp	Thu Aug 11 00:02:25 2011 +0200
@@ -1,5 +1,5 @@
 /*
-Mignonne: a Salut à Toi frontend
+Bellaciao: a Salut à Toi frontend
 Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
 
 This program is free software: you can redistribute it and/or modify
@@ -17,6 +17,20 @@
 */
 
 #include "dbus_types.h"
+#include <QtGlobal>
+
+QDBusArgument &operator<<(QDBusArgument &argument, const Jid &jid)
+{
+    return argument << jid.getString();
+}
+
+const QDBusArgument &operator>>(const QDBusArgument &argument, Jid &jid)
+{
+    QString _str = QString();
+    argument >> _str;
+    jid.fromString(_str);
+    return argument;
+}
 
 QDBusArgument &operator<<(QDBusArgument &argument, const ContactT &contact)
 {
--- a/dbus_types.h	Mon Aug 08 10:59:34 2011 +0200
+++ b/dbus_types.h	Thu Aug 11 00:02:25 2011 +0200
@@ -1,5 +1,5 @@
 /*
-Mignonne: a Salut à Toi frontend
+Bellaciao: a Salut à Toi frontend
 Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
 
 This program is free software: you can redistribute it and/or modify
@@ -24,15 +24,10 @@
 #include <QMap>
 #include <QString>
 #include <QList>
+#include "contact.h"
 
 typedef QHash<QString, QString> StringDict;
 
-struct ContactT {
-    QString jid;
-    StringDict attributes;
-    QList<QString> groups;
-};
-
 struct MessageT {
     QString from_jid;
     QString text;
@@ -58,6 +53,7 @@
 
 typedef QHash<QString, StringDict> ActionResultExtDataT;
 
+Q_DECLARE_METATYPE(Jid);
 Q_DECLARE_METATYPE(StringDict);
 Q_DECLARE_METATYPE(ContactT);
 Q_DECLARE_METATYPE(QList<ContactT>);
@@ -67,6 +63,8 @@
 Q_DECLARE_METATYPE(QList<MenuT>);
 Q_DECLARE_METATYPE(ActionResultExtDataT);
 
+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 MenuT &menu);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jid.cpp	Thu Aug 11 00:02:25 2011 +0200
@@ -0,0 +1,130 @@
+/*
+Bellaciao: a Salut à Toi frontend
+Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "jid.h"
+#include <QtDebug>
+
+Jid::Jid()
+    :m_valid(false)
+{
+}
+
+Jid::~Jid()
+{
+}
+
+Jid::Jid(const QString& str)
+{
+    fromString(str);
+}
+
+void Jid::fromString(const QString& str)
+//Replace jid with the one parsed in str
+//m_valid set to false is jid is not valid
+{
+    //We do a basic parsing of the string
+    //TODO: better parsing/validity check
+    m_valid = false;
+    m_user.clear();
+    m_domain.clear();
+    m_resource.clear();
+
+    QChar delimiter = '@';
+    QString* curr_dest = &m_user;
+
+    for (int i=0; i<str.length(); i++)
+    {
+        if (str[i]==delimiter) {
+            if (delimiter=='@'){
+                curr_dest = &m_domain;
+                delimiter='/';
+            }
+            else if (delimiter=='/') {
+                curr_dest = &m_resource;
+                delimiter=0;
+            }
+            else {
+                qWarning("This line should never be reached");
+            }
+        }
+        else {
+            if (str[i]=='@' or str[i]=='/')
+            {
+                qWarning() << "Invalid jid [" <<  str <<"]";
+                return;
+            }
+            curr_dest->append(str[i]);
+        }
+    }
+
+    if (!m_user.isEmpty() && !m_domain.isEmpty())
+        m_valid = true;
+}
+
+QString Jid::getString() const
+{
+    if (!m_valid) {
+        return m_user; //We return the user anyway if the jid is invalid
+    }
+    return m_resource.isEmpty() ? getBareString() : getFullString();
+}
+
+QString Jid::getBareString() const
+{
+    return QString("%1@%2").arg(m_user).arg(m_domain);
+}
+
+QString Jid::getFullString() const
+{
+    return QString("%1@%2/%3").arg(m_user).arg(m_domain).arg(m_resource);
+}
+
+const QString& Jid::getUser() const
+{
+    return m_user;
+}
+
+const QString& Jid::getDomain() const
+{
+    return m_domain;
+}
+
+const QString& Jid::getResource() const
+{
+    return m_resource;
+}
+
+void Jid::setUser(const QString& user)
+{
+   m_user = user; 
+}
+
+void Jid::setDomain(const QString& domain)
+{
+   m_domain = domain; 
+}
+
+void Jid::setResource(const QString& resource)
+{
+   m_resource = resource; 
+}
+
+bool Jid::isValid()
+{
+    return m_valid;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jid.h	Thu Aug 11 00:02:25 2011 +0200
@@ -0,0 +1,48 @@
+
+/*
+Bellaciao: a Salut à Toi frontend
+Copyright (C) 2011  Jérôme Poisson (goffi@goffi.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef JID_H
+#define JID_H
+
+#include <QString>
+
+class Jid {
+    public:
+        Jid();
+        ~Jid();
+        Jid(const QString& str);
+        void fromString(const QString& str);
+        QString getString() const;
+        QString getBareString() const;
+        QString getFullString() const;
+        const QString& getUser() const;
+        const QString& getDomain() const;
+        const QString& getResource() const;
+        void setUser(const QString& user);
+        void setDomain(const QString& domain);
+        void setResource(const QString& resource);
+        bool isValid();
+
+    private:
+        bool m_valid;
+        QString m_user, m_domain, m_resource;
+};
+
+#endif
+
--- a/main.cpp	Mon Aug 08 10:59:34 2011 +0200
+++ b/main.cpp	Thu Aug 11 00:02:25 2011 +0200
@@ -24,8 +24,7 @@
 {
     QApplication app(argv, args);
  
-    Bellaciao window;
-    window.show();
+    Bellaciao::getInstance()->show();
  
     return app.exec();
 }
--- a/session.cpp	Mon Aug 08 10:59:34 2011 +0200
+++ b/session.cpp	Thu Aug 11 00:02:25 2011 +0200
@@ -31,6 +31,11 @@
         _instance = new Session;
 
     return _instance;
-
 }
 
+QList<QString>& Session::getProfiles() {
+    return m_profiles;
+}
+
+
+
--- a/session.h	Mon Aug 08 10:59:34 2011 +0200
+++ b/session.h	Thu Aug 11 00:02:25 2011 +0200
@@ -19,16 +19,20 @@
 #ifndef SESSION_H
 #define SESSION_H
 
+#include <QtGui>
+
 class Session
 {
 public:
     static Session* getSession();
+    QList<QString>& getProfiles();
 
 protected:
     Session();
 
 private:
     static Session* _instance;
+    QList<QString> m_profiles;
 };
 
 
--- a/settings.cpp	Mon Aug 08 10:59:34 2011 +0200
+++ b/settings.cpp	Thu Aug 11 00:02:25 2011 +0200
@@ -17,6 +17,7 @@
 */
 
 #include "settings.h"
+#include "bellaciao.h"
 
 using namespace Qt;
 
@@ -49,20 +50,21 @@
 
     //We fill the profiles list
     m_bridge = Bridge::getBridge();
+    m_session = Session::getSession();
+
     const QList<QString>& profiles = m_bridge->getProfilesList();
     const QString& default_profile = m_bridge->getProfileName("@DEFAULT@");
+    const QList<QString>& checked_profiles = m_session->getProfiles(); 
+    
     foreach(const QString& profile, profiles) {
         ProfileListWidgetItem *item = new ProfileListWidgetItem(profile, profileList, profile==default_profile);
         item->setFlags(item->flags() | ItemIsUserCheckable);
-        item->setCheckState(Checked);
+        item->setCheckState(checked_profiles.contains(profile)?Checked:Unchecked);
     }
     
     connect(profileList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(profileChanged(QListWidgetItem*)));
     connect(profileList, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, SLOT(profileCurrentChanged(QListWidgetItem*, QListWidgetItem*)));
 
-    //Profiles manipulation buttons
-    connect(buttonsGroupConfirm, SIGNAL(accepted()), this, SLOT(saveSettings()));
-
     //Confirmation buttons
     connect(buttonsGroupConfirm, SIGNAL(accepted()), this, SLOT(saveSettings()));
     connect(buttonsGroupConfirm, SIGNAL(rejected()), this, SLOT(cancelSettings()));
@@ -84,7 +86,6 @@
 //Profile settings slots
 void Settings::profileChanged(QListWidgetItem* item)
 {
-    qDebug() << "profileChanged";
 }
 
 void Settings::profileCurrentChanged(QListWidgetItem* current, QListWidgetItem* previous)
@@ -165,11 +166,29 @@
 
 
 //Global settings slots
-void Settings::saveSettings()
+void Settings::saveSettings() //Save settings to session
 {
+    // We save profiles
+    QList<QString> previous_profiles;
+    QList<QString>& profiles = m_session->getProfiles();
+    previous_profiles = profiles;
+    profiles.clear();
+    for (int i=0; i<profileList->count(); i++)
+    {
+        ProfileListWidgetItem* item = dynamic_cast<ProfileListWidgetItem*>(profileList->item(i));
+        if (item->checkState() == Checked){
+            profiles.append(item->getProfile());
+        }
+    }
+
+    if (previous_profiles!=profiles) //The profiles checked are differents, we refill the contactList
+        Bellaciao::getInstance()->connectProfiles();
+
+    close();
+        
 }
 
 void Settings::cancelSettings()
 {
-    this->close();
+    close();
 }
--- a/settings.h	Mon Aug 08 10:59:34 2011 +0200
+++ b/settings.h	Thu Aug 11 00:02:25 2011 +0200
@@ -23,6 +23,7 @@
 #include <QtGui>
 #include "ui_settings.h"
 #include "bridge.h"
+#include "session.h"
 
 class ProfileListWidgetItem : public QListWidgetItem
 {
@@ -57,6 +58,7 @@
     private:
         static Settings* _instance;
         Bridge* m_bridge;
+        Session* m_session;
 
 };