]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-connection-aggregator.c
update gnome-contacts's desktop file
[empathy.git] / libempathy / empathy-connection-aggregator.c
index 7074e5186b7d35e9b67bdd2011060c5595d5b2d5..763715f9e30c618beff4a56624de645c6762c0e0 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <config.h>
-
+#include "config.h"
 #include "empathy-connection-aggregator.h"
 
-#include <telepathy-glib/telepathy-glib.h>
-
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
 #include "empathy-debug.h"
-#include "empathy-utils.h"
 
+G_DEFINE_TYPE (EmpathyConnectionAggregator, empathy_connection_aggregator,
+    G_TYPE_OBJECT);
 
-#include "extensions/extensions.h"
+enum {
+  EVENT_CONTACT_LIST_CHANGED,
+  LAST_SIGNAL
+};
 
-G_DEFINE_TYPE (EmpathyConnectionAggregator, empathy_connection_aggregator, 
-    G_TYPE_OBJECT);
+static guint signals[LAST_SIGNAL];
 
 struct _EmpathyConnectionAggregatorPriv {
   TpAccountManager *mgr;
@@ -62,9 +62,28 @@ empathy_connection_aggregator_class_init (
 
   oclass->dispose = empathy_connection_aggregator_dispose;
 
+  signals[EVENT_CONTACT_LIST_CHANGED] =
+    g_signal_new ("contact-list-changed",
+      G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST,
+      0,
+      NULL, NULL,
+      g_cclosure_marshal_generic,
+      G_TYPE_NONE,
+      2, G_TYPE_PTR_ARRAY, G_TYPE_PTR_ARRAY);
+
   g_type_class_add_private (klass, sizeof (EmpathyConnectionAggregatorPriv));
 }
 
+static void
+contact_list_changed_cb (TpConnection *conn,
+    GPtrArray *added,
+    GPtrArray *removed,
+    EmpathyConnectionAggregator *self)
+{
+  g_signal_emit (self, signals[EVENT_CONTACT_LIST_CHANGED], 0, added, removed);
+}
+
 static void
 conn_invalidated_cb (TpConnection *conn,
     guint domain,
@@ -81,12 +100,29 @@ static void
 check_connection (EmpathyConnectionAggregator *self,
     TpConnection *conn)
 {
+  GPtrArray *contacts;
+
   if (g_list_find (self->priv->conns, conn) != NULL)
     return;
 
   self->priv->conns = g_list_prepend (self->priv->conns,
       g_object_ref (conn));
 
+  tp_g_signal_connect_object (conn, "contact-list-changed",
+      G_CALLBACK (contact_list_changed_cb), self, 0);
+
+  contacts = tp_connection_dup_contact_list (conn);
+  if (contacts != NULL)
+    {
+      GPtrArray *empty;
+
+      empty = g_ptr_array_new ();
+
+      contact_list_changed_cb (conn, contacts, empty, self);
+      g_ptr_array_unref (empty);
+    }
+  g_ptr_array_unref (contacts);
+
   tp_g_signal_connect_object (conn, "invalidated",
       G_CALLBACK (conn_invalidated_cb), self, 0);
 }
@@ -146,7 +182,7 @@ am_prepare_cb (GObject *source,
       goto out;
     }
 
-  accounts = tp_account_manager_get_valid_accounts (self->priv->mgr);
+  accounts = tp_account_manager_dup_valid_accounts (self->priv->mgr);
   for (l = accounts; l != NULL; l = g_list_next (l))
     {
       TpAccount *account = l->data;
@@ -157,7 +193,7 @@ am_prepare_cb (GObject *source,
   tp_g_signal_connect_object (self->priv->mgr, "account-validity-changed",
       G_CALLBACK (account_validity_changed_cb), self, 0);
 
-  g_list_free (accounts);
+  g_list_free_full (accounts, g_object_unref);
 
 out:
   g_object_unref (self);
@@ -189,6 +225,7 @@ empathy_connection_aggregator_dup_singleton (void)
   return aggregator;
 }
 
+/* (transfer container) */
 GList *
 empathy_connection_aggregator_get_all_groups (EmpathyConnectionAggregator *self)
 {
@@ -216,3 +253,73 @@ empathy_connection_aggregator_get_all_groups (EmpathyConnectionAggregator *self)
 
   return keys;
 }
+
+GPtrArray *
+empathy_connection_aggregator_dup_all_contacts (
+    EmpathyConnectionAggregator *self)
+{
+  GPtrArray *result;
+  GList *l;
+
+  result = g_ptr_array_new_with_free_func (g_object_unref);
+
+  for (l = self->priv->conns; l != NULL; l = g_list_next (l))
+    {
+      TpConnection *conn = l->data;
+      GPtrArray *contacts;
+
+      contacts = tp_connection_dup_contact_list (conn);
+      if (contacts == NULL)
+        continue;
+
+      tp_g_ptr_array_extend (result, contacts);
+
+      /* tp_g_ptr_array_extend() doesn't give us an extra ref */
+      g_ptr_array_foreach (contacts, (GFunc) g_object_ref, NULL);
+
+      g_ptr_array_unref (contacts);
+    }
+
+  return result;
+}
+
+static void
+rename_group_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  GError *error = NULL;
+
+  if (!tp_connection_rename_group_finish (TP_CONNECTION (source), result,
+        &error))
+    {
+      DEBUG ("Failed to rename group on %s: %s",
+          tp_proxy_get_object_path (source), error->message);
+      g_error_free (error);
+    }
+}
+
+void
+empathy_connection_aggregator_rename_group (EmpathyConnectionAggregator *self,
+    const gchar *old_name,
+    const gchar *new_name)
+{
+  GList *l;
+
+  for (l = self->priv->conns; l != NULL; l = g_list_next (l))
+    {
+      TpConnection *conn = l->data;
+      const gchar * const *groups;
+
+      groups = tp_connection_get_contact_groups (conn);
+
+      if (!tp_strv_contains (groups, old_name))
+        continue;
+
+      DEBUG ("Rename group '%s' to '%s' on %s", old_name, new_name,
+          tp_proxy_get_object_path (conn));
+
+      tp_connection_rename_group_async (conn, old_name, new_name,
+          rename_group_cb, NULL);
+    }
+}