]> git.0d.be Git - empathy.git/commitdiff
Support contact adding
authorTravis Reitter <treitter@gmail.com>
Tue, 22 Jun 2010 15:43:18 +0000 (08:43 -0700)
committerTravis Reitter <treitter@gmail.com>
Tue, 20 Jul 2010 23:12:35 +0000 (16:12 -0700)
libempathy-gtk/Makefile.am
libempathy-gtk/empathy-individual-dialogs.c [new file with mode: 0644]
libempathy-gtk/empathy-individual-dialogs.h [new file with mode: 0644]
libempathy-gtk/empathy-individual-menu.c
libempathy/empathy-individual-manager.c
libempathy/empathy-individual-manager.h
src/empathy-main-window.c

index eb32ccd35fb9bf16e9d886a0716163f0371b8d05..56e2af3cc088a679ccd621a2763dc804b18da773 100644 (file)
@@ -52,6 +52,7 @@ libempathy_gtk_handwritten_source =                   \
        empathy-contact-selector-dialog.c \
        empathy-contact-widget.c                \
        empathy-geometry.c                      \
+       empathy-individual-dialogs.c            \
        empathy-individual-menu.c               \
        empathy-individual-store.c              \
        empathy-individual-view.c               \
@@ -102,6 +103,7 @@ libempathy_gtk_headers =                    \
        empathy-contact-widget.h                \
        empathy-geometry.h                      \
        empathy-images.h                        \
+       empathy-individual-dialogs.h            \
        empathy-individual-menu.h               \
        empathy-individual-store.h              \
        empathy-individual-view.h               \
diff --git a/libempathy-gtk/empathy-individual-dialogs.c b/libempathy-gtk/empathy-individual-dialogs.c
new file mode 100644 (file)
index 0000000..a905c19
--- /dev/null
@@ -0,0 +1,152 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2007-2008 Collabora Ltd.
+ *
+ * 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 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
+ * Lesser General Public License for more details.
+ *
+ * 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 <config.h>
+
+#include <string.h>
+#include <stdlib.h>
+
+#include <gtk/gtk.h>
+#include <glib/gi18n-lib.h>
+
+#include <folks/folks.h>
+
+#include <libempathy/empathy-individual-manager.h>
+#include <libempathy/empathy-utils.h>
+
+#include "empathy-individual-dialogs.h"
+#include "empathy-contact-widget.h"
+#include "empathy-ui-utils.h"
+
+static GtkWidget *new_individual_dialog = NULL;
+
+/*
+ *  New contact dialog
+ */
+
+static gboolean
+can_add_contact_to_account (TpAccount *account,
+    gpointer user_data)
+{
+  EmpathyIndividualManager *individual_manager;
+  TpConnection *connection;
+  gboolean result;
+
+  connection = tp_account_get_connection (account);
+  if (connection == NULL)
+    return FALSE;
+
+  individual_manager = empathy_individual_manager_dup_singleton ();
+  result = empathy_individual_manager_get_flags_for_connection (
+    individual_manager, connection) & EMPATHY_INDIVIDUAL_MANAGER_CAN_ADD;
+  g_object_unref (individual_manager);
+
+  return result;
+}
+
+static void
+new_individual_response_cb (GtkDialog *dialog,
+    gint response,
+    GtkWidget *contact_widget)
+{
+  EmpathyIndividualManager *individual_manager;
+  EmpathyContact *contact;
+
+  individual_manager = empathy_individual_manager_dup_singleton ();
+  contact = empathy_contact_widget_get_contact (contact_widget);
+
+  if (contact && response == GTK_RESPONSE_OK)
+    empathy_individual_manager_add_from_contact (individual_manager, contact);
+
+  new_individual_dialog = NULL;
+  gtk_widget_destroy (GTK_WIDGET (dialog));
+  g_object_unref (individual_manager);
+}
+
+void
+empathy_new_individual_dialog_show (GtkWindow *parent)
+{
+  empathy_new_individual_dialog_show_with_individual (parent, NULL);
+}
+
+void
+empathy_new_individual_dialog_show_with_individual (GtkWindow *parent,
+    FolksIndividual *individual)
+{
+  GtkWidget *dialog;
+  GtkWidget *button;
+  EmpathyContact *contact = NULL;
+  GtkWidget *contact_widget;
+
+  g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
+
+  if (new_individual_dialog)
+    {
+      gtk_window_present (GTK_WINDOW (new_individual_dialog));
+      return;
+    }
+
+  /* Create dialog */
+  dialog = gtk_dialog_new ();
+  gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
+  gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
+  gtk_window_set_title (GTK_WINDOW (dialog), _("New Individual"));
+
+  /* Cancel button */
+  button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
+  gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
+  gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
+      GTK_RESPONSE_CANCEL);
+  gtk_widget_show (button);
+
+  /* Add button */
+  button = gtk_button_new_with_label (GTK_STOCK_ADD);
+  gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
+  gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_OK);
+  gtk_widget_show (button);
+
+  /* Contact info widget */
+  if (FOLKS_IS_INDIVIDUAL (individual))
+    contact = empathy_contact_from_folks_individual (individual);
+
+  contact_widget = empathy_contact_widget_new (contact,
+      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
+      EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
+      EMPATHY_CONTACT_WIDGET_EDIT_ID |
+      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
+  gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
+  gtk_box_pack_start (
+      GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
+      contact_widget, TRUE, TRUE, 0);
+  empathy_contact_widget_set_account_filter (contact_widget,
+      can_add_contact_to_account, NULL);
+  gtk_widget_show (contact_widget);
+
+  new_individual_dialog = dialog;
+
+  g_signal_connect (dialog, "response", G_CALLBACK (new_individual_response_cb),
+      contact_widget);
+
+  if (parent != NULL)
+    gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
+
+  gtk_widget_show (dialog);
+}
diff --git a/libempathy-gtk/empathy-individual-dialogs.h b/libempathy-gtk/empathy-individual-dialogs.h
new file mode 100644 (file)
index 0000000..b34a7ab
--- /dev/null
@@ -0,0 +1,38 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2007-2010 Collabora Ltd.
+ *
+ * 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 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
+ * Lesser General Public License for more details.
+ *
+ * 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>
+ *          Travis Reitter <travis.reitter@collabora.co.uk>
+ */
+
+#ifndef __EMPATHY_INDIVIDUAL_DIALOGS_H__
+#define __EMPATHY_INDIVIDUAL_DIALOGS_H__
+
+#include <gtk/gtk.h>
+
+#include <folks/folks.h>
+
+G_BEGIN_DECLS
+
+void empathy_new_individual_dialog_show (GtkWindow *parent);
+void empathy_new_individual_dialog_show_with_individual (GtkWindow *parent,
+    FolksIndividual *individual);
+
+G_END_DECLS
+
+#endif /*  __EMPATHY_INDIVIDUAL_DIALOGS_H__ */
index c618d2c37341f23bc68a4acc2c4ba97b6fee4b0b..09309dbf98f88a6d1cff2e04c3747f9f393f922d 100644 (file)
@@ -40,6 +40,7 @@
 #include "empathy-images.h"
 #include "empathy-log-window.h"
 #include "empathy-contact-dialogs.h"
+#include "empathy-individual-dialogs.h"
 #include "empathy-ui-utils.h"
 #include "empathy-share-my-desktop.h"
 
@@ -160,18 +161,13 @@ empathy_individual_add_menu_item_activated (GtkMenuItem *item,
   FolksIndividual *individual)
 {
   GtkWidget *toplevel;
-  EmpathyContact *contact;
 
   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item));
   if (!gtk_widget_is_toplevel (toplevel) || !GTK_IS_WINDOW (toplevel))
     toplevel = NULL;
 
-  contact = empathy_contact_from_folks_individual (individual);
-  empathy_new_contact_dialog_show_with_contact (GTK_WINDOW (toplevel),
-      contact);
-
-  if (contact != NULL)
-    g_object_unref (contact);
+  empathy_new_individual_dialog_show_with_individual (GTK_WINDOW (toplevel),
+      individual);
 }
 
 GtkWidget *
index 95955d64272e2f82fd354ff253f46806e2689b5c..fe8ccadf915e7d1b9232f72e638f823f5bff393b 100644 (file)
@@ -260,6 +260,56 @@ empathy_individual_manager_lookup_member (EmpathyIndividualManager *self,
   return NULL;
 }
 
+static void
+aggregator_add_persona_from_details_cb (GObject *source,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  EmpathyIndividualManager *self = EMPATHY_INDIVIDUAL_MANAGER (user_data);
+  EmpathyIndividualManagerPriv *priv = GET_PRIV (self);
+  FolksPersona *persona;
+  GError *error = NULL;
+
+  persona = folks_individual_aggregator_add_persona_from_details_finish (
+      priv->aggregator, result, &error);
+  if (error != NULL)
+    {
+      g_warning ("failed to add individual from contact: %s", error->message);
+      g_clear_error (&error);
+    }
+}
+
+void
+empathy_individual_manager_add_from_contact (EmpathyIndividualManager *self,
+    EmpathyContact *contact)
+{
+  EmpathyIndividualManagerPriv *priv;
+  GHashTable* details;
+  TpAccount *account;
+  const gchar *store_id;
+
+  g_return_if_fail (EMPATHY_IS_INDIVIDUAL_MANAGER (self));
+  g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+
+  priv = GET_PRIV (self);
+
+  DEBUG (G_STRLOC ": adding individual from contact %s (%s)",
+      empathy_contact_get_id (contact), empathy_contact_get_name (contact));
+
+  account = empathy_contact_get_account (contact);
+  store_id = tp_proxy_get_object_path (TP_PROXY (account));
+
+  details = g_hash_table_new (g_str_hash, g_str_equal);
+  g_hash_table_insert (details, "contact",
+      (gchar*) empathy_contact_get_id (contact));
+
+  folks_individual_aggregator_add_persona_from_details (
+      priv->aggregator, NULL, "telepathy", store_id, details,
+      aggregator_add_persona_from_details_cb, self);
+
+  g_hash_table_destroy (details);
+}
+
 void
 empathy_individual_manager_remove (EmpathyIndividualManager *self,
     FolksIndividual *individual,
index 030ea815f7fdc520e0198085b921a42d08076f5b..2fab1ffb066451463384ee2221111ff7c13957a9 100644 (file)
@@ -71,6 +71,10 @@ FolksIndividual *empathy_individual_manager_lookup_member (
     EmpathyIndividualManager *manager,
     const gchar *id);
 
+void empathy_individual_manager_add_from_contact (
+    EmpathyIndividualManager *manager,
+    EmpathyContact *contact);
+
 void empathy_individual_manager_remove (EmpathyIndividualManager *manager,
     FolksIndividual *individual,
     const gchar *message);
index 0af2413fc3eb7cdb5f874c6cdf001ee97fdd5627..f6fe0c9b4836586e8d74872d62555c838dc1cb36 100644 (file)
@@ -52,6 +52,7 @@
 #include <libempathy-gtk/empathy-live-search.h>
 #include <libempathy-gtk/empathy-geometry.h>
 #include <libempathy-gtk/empathy-gtk-enum-types.h>
+#include <libempathy-gtk/empathy-individual-dialogs.h>
 #include <libempathy-gtk/empathy-individual-store.h>
 #include <libempathy-gtk/empathy-individual-view.h>
 #include <libempathy-gtk/empathy-new-message-dialog.h>
@@ -714,7 +715,7 @@ static void
 main_window_chat_add_contact_cb (GtkAction         *action,
                                 EmpathyMainWindow *window)
 {
-       empathy_new_contact_dialog_show (GTK_WINDOW (window));
+       empathy_new_individual_dialog_show (GTK_WINDOW (window));
 }
 
 static void