]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-contact-manager.c
empathy-tp-tube: remove initiator and type member variables as they are not used
[empathy.git] / libempathy / empathy-contact-manager.c
index 575814fab8218dc4c8df4f00a8fa6385ead136b8..409f41c449625afa215c33015fd9eda341f02216 100644 (file)
@@ -1,21 +1,20 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
- * Copyright (C) 2007 Collabora Ltd.
+ * Copyright (C) 2007-2008 Collabora Ltd.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library 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
- * General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  * 
  * Authors: Xavier Claessens <xclaesse@gmail.com>
  */
 
 #include <string.h>
 
-#include <libtelepathy/tp-constants.h>
+#include <telepathy-glib/enums.h>
 
 #include "empathy-contact-manager.h"
+#include "empathy-account-manager.h"
+#include "empathy-contact-monitor.h"
 #include "empathy-contact-list.h"
 #include "empathy-utils.h"
-#include "empathy-debug.h"
-
-#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
-                      EMPATHY_TYPE_CONTACT_MANAGER, EmpathyContactManagerPriv))
 
-#define DEBUG_DOMAIN "ContactManager"
+#define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
+#include "empathy-debug.h"
 
-struct _EmpathyContactManagerPriv {
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactManager)
+typedef struct {
        GHashTable     *lists;
-       MissionControl *mc;
-};
+       EmpathyAccountManager *account_manager;
+       EmpathyContactMonitor *contact_monitor;
+} EmpathyContactManagerPriv;
 
-static void empathy_contact_manager_class_init (EmpathyContactManagerClass *klass);
-static void empathy_contact_manager_init       (EmpathyContactManager      *manager);
 static void contact_manager_iface_init         (EmpathyContactListIface    *iface);
 
 G_DEFINE_TYPE_WITH_CODE (EmpathyContactManager, empathy_contact_manager, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CONTACT_LIST,
                                                contact_manager_iface_init));
 
+static EmpathyContactManager *manager_singleton = NULL;
+
 static void
 contact_manager_members_changed_cb (EmpathyTpContactList  *list,
                                    EmpathyContact        *contact,
@@ -121,8 +121,7 @@ contact_manager_destroy_cb (EmpathyTpContactList  *list,
 
        account = empathy_tp_contact_list_get_account (list);
 
-       empathy_debug (DEBUG_DOMAIN, "Removing account: %s",
-                      mc_account_get_display_name (account));
+       DEBUG ("Removing account: %s", mc_account_get_display_name (account));
 
        contact_manager_disconnect_foreach (account, list, manager);
        g_hash_table_remove (priv->lists, account);
@@ -139,8 +138,7 @@ contact_manager_add_account (EmpathyContactManager *manager,
                return;
        }
 
-       empathy_debug (DEBUG_DOMAIN, "Adding new account: %s",
-                      mc_account_get_display_name (account));
+       DEBUG ("Adding new account: %s", mc_account_get_display_name (account));
 
        list = empathy_tp_contact_list_new (account);
        if (!list) {
@@ -165,23 +163,19 @@ contact_manager_add_account (EmpathyContactManager *manager,
 }
 
 static void
-contact_manager_status_changed_cb (MissionControl                  *mc,
-                                  TelepathyConnectionStatus        status,
-                                  McPresence                       presence,
-                                  TelepathyConnectionStatusReason  reason,
-                                  const gchar                     *unique_name,
-                                  EmpathyContactManager           *manager)
+contact_manager_connection_changed_cb (EmpathyAccountManager *account_manager,
+                                      McAccount *account,
+                                      TpConnectionStatusReason  reason,
+                                      TpConnectionStatus current,
+                                      TpConnectionStatus previous,
+                                      EmpathyContactManager *manager)
 {
-       McAccount *account;
-
-       if (status != TP_CONN_STATUS_CONNECTED) {
+       if (current != TP_CONNECTION_STATUS_CONNECTED) {
                /* We only care about newly connected accounts */
                return;
        }
 
-       account = mc_account_lookup (unique_name);
        contact_manager_add_account (manager, account);
-       g_object_unref (account);
 }
 
 static void
@@ -193,7 +187,35 @@ contact_manager_finalize (GObject *object)
                              contact_manager_disconnect_foreach,
                              object);
        g_hash_table_destroy (priv->lists);
-       g_object_unref (priv->mc);
+
+       g_signal_handlers_disconnect_by_func (priv->account_manager,
+                                             contact_manager_connection_changed_cb,
+                                             object);
+       g_object_unref (priv->account_manager);
+
+       if (priv->contact_monitor) {
+               g_object_unref (priv->contact_monitor);
+       }
+}
+
+static GObject *
+contact_manager_constructor (GType type,
+                            guint n_props,
+                            GObjectConstructParam *props)
+{
+       GObject *retval;
+
+       if (manager_singleton) {
+               retval = g_object_ref (manager_singleton);
+       } else {
+               retval = G_OBJECT_CLASS (empathy_contact_manager_parent_class)->constructor
+                       (type, n_props, props);
+
+               manager_singleton = EMPATHY_CONTACT_MANAGER (retval);
+               g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
+       }
+
+       return retval;
 }
 
 static void
@@ -202,6 +224,7 @@ empathy_contact_manager_class_init (EmpathyContactManagerClass *klass)
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
        object_class->finalize = contact_manager_finalize;
+       object_class->constructor = contact_manager_constructor;
 
        g_type_class_add_private (object_class, sizeof (EmpathyContactManagerPriv));
 }
@@ -209,45 +232,40 @@ empathy_contact_manager_class_init (EmpathyContactManagerClass *klass)
 static void
 empathy_contact_manager_init (EmpathyContactManager *manager)
 {
-       EmpathyContactManagerPriv *priv;
        GSList                    *accounts, *l;
+       MissionControl            *mc;
+       EmpathyContactManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
+               EMPATHY_TYPE_CONTACT_MANAGER, EmpathyContactManagerPriv);
 
-       priv = GET_PRIV (manager);
-
+       manager->priv = priv;
        priv->lists = g_hash_table_new_full (empathy_account_hash,
                                             empathy_account_equal,
                                             (GDestroyNotify) g_object_unref,
                                             (GDestroyNotify) g_object_unref);
+       priv->account_manager = empathy_account_manager_dup_singleton ();
+       priv->contact_monitor = NULL;
 
-       priv->mc = empathy_mission_control_new ();
+       g_signal_connect (priv->account_manager,
+                         "account-connection-changed",
+                         G_CALLBACK (contact_manager_connection_changed_cb), manager);
 
-       dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->mc),
-                                    "AccountStatusChanged",
-                                    G_CALLBACK (contact_manager_status_changed_cb),
-                                    manager, NULL);
+       mc = empathy_mission_control_dup_singleton ();
 
        /* Get ContactList for existing connections */
-       accounts = mission_control_get_online_connections (priv->mc, NULL);
+       accounts = mission_control_get_online_connections (mc, NULL);
        for (l = accounts; l; l = l->next) {
                contact_manager_add_account (manager, l->data);
                g_object_unref (l->data);
        }
+
        g_slist_free (accounts);
+       g_object_unref (mc);
 }
 
 EmpathyContactManager *
-empathy_contact_manager_new (void)
+empathy_contact_manager_dup_singleton (void)
 {
-       static EmpathyContactManager *manager = NULL;
-
-       if (!manager) {
-               manager = g_object_new (EMPATHY_TYPE_CONTACT_MANAGER, NULL);
-               g_object_add_weak_pointer (G_OBJECT (manager), (gpointer) &manager);
-       } else {
-               g_object_ref (manager);
-       }
-
-       return manager;
+       return g_object_new (EMPATHY_TYPE_CONTACT_MANAGER, NULL);
 }
 
 EmpathyTpContactList *
@@ -326,6 +344,18 @@ contact_manager_get_members (EmpathyContactList *manager)
        return contacts;
 }
 
+static EmpathyContactMonitor *
+contact_manager_get_monitor (EmpathyContactList *manager)
+{
+       EmpathyContactManagerPriv *priv = GET_PRIV (manager);
+
+       if (priv->contact_monitor == NULL) {
+               priv->contact_monitor = empathy_contact_monitor_new_for_iface (manager);
+       }
+
+       return priv->contact_monitor;
+}
+
 static void
 contact_manager_get_pendings_foreach (McAccount             *account,
                                      EmpathyTpContactList  *list,
@@ -478,17 +508,56 @@ contact_manager_rename_group (EmpathyContactList *manager,
                              &data);
 }
 
+static void contact_manager_remove_group_foreach (McAccount    *account,
+                                                 EmpathyTpContactList *list,
+                                                 const gchar *group)
+{
+       empathy_contact_list_remove_group (EMPATHY_CONTACT_LIST (list),
+                                          group);
+}
+
+static void
+contact_manager_remove_group (EmpathyContactList *manager,
+                             const gchar *group)
+{
+       EmpathyContactManagerPriv *priv = GET_PRIV (manager);
+       
+       g_return_if_fail (EMPATHY_IS_CONTACT_MANAGER (manager));
+
+       g_hash_table_foreach (priv->lists,
+                             (GHFunc) contact_manager_remove_group_foreach,
+                             (gpointer) group);
+}
+
 static void
 contact_manager_iface_init (EmpathyContactListIface *iface)
 {
        iface->add               = contact_manager_add;
        iface->remove            = contact_manager_remove;
        iface->get_members       = contact_manager_get_members;
+       iface->get_monitor       = contact_manager_get_monitor;
        iface->get_pendings      = contact_manager_get_pendings;
        iface->get_all_groups    = contact_manager_get_all_groups;
        iface->get_groups        = contact_manager_get_groups;
        iface->add_to_group      = contact_manager_add_to_group;
        iface->remove_from_group = contact_manager_remove_from_group;
        iface->rename_group      = contact_manager_rename_group;
+       iface->remove_group      = contact_manager_remove_group;
+}
+
+gboolean
+empathy_contact_manager_can_add (EmpathyContactManager *manager,
+                                McAccount             *account)
+{
+       EmpathyContactManagerPriv *priv = GET_PRIV (manager);
+       EmpathyTpContactList      *list;
+       
+       g_return_val_if_fail (EMPATHY_IS_CONTACT_MANAGER (manager), FALSE);
+
+       list = g_hash_table_lookup (priv->lists, account);
+       if (list == NULL)
+               return FALSE;
+
+       return empathy_tp_contact_list_can_add (list);
 }