]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-dialogs.c
Port EmpathyContactDialogs to new API.
[empathy.git] / libempathy-gtk / empathy-contact-dialogs.c
index e38eb84d14def838824c980b41cbd687cbaf04b2..309cd649c8e8708d3530b4e171f79e98b70ed96e 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 <stdlib.h>
 
 #include <gtk/gtk.h>
-#include <glade/glade.h>
-#include <glib/gi18n.h>
+#include <glib/gi18n-lib.h>
+
+#include <libmissioncontrol/mission-control.h>
 
 #include <libempathy/empathy-contact-manager.h>
+#include <libempathy/empathy-account-manager.h>
 #include <libempathy/empathy-contact-list.h>
+#include <libempathy/empathy-utils.h>
 
 #include "empathy-contact-dialogs.h"
 #include "empathy-contact-widget.h"
-#include "gossip-ui-utils.h"
+#include "empathy-ui-utils.h"
+
+static GList *subscription_dialogs = NULL;
+static GList *information_dialogs = NULL;
+static GtkWidget *new_contact_dialog = NULL;
+
+
+static gint
+contact_dialogs_find (GtkDialog      *dialog,
+                     EmpathyContact *contact)
+{
+       GtkWidget     *contact_widget;
+       EmpathyContact *this_contact;
 
-static GHashTable *subscription_dialogs = NULL;
-static GHashTable *information_dialogs = NULL;
+       contact_widget = g_object_get_data (G_OBJECT (dialog), "contact_widget");
+       this_contact = empathy_contact_widget_get_contact (contact_widget);
+
+       return contact != this_contact;
+}
 
 /*
  *  Subscription dialog
@@ -49,64 +66,67 @@ subscription_dialog_response_cb (GtkDialog *dialog,
                                 GtkWidget *contact_widget)
 {
        EmpathyContactManager *manager;
-       GossipContact         *contact;
+       EmpathyContact        *contact;
 
-       manager = empathy_contact_manager_new ();
+       manager = empathy_contact_manager_dup_singleton ();
        contact = empathy_contact_widget_get_contact (contact_widget);
-       empathy_contact_widget_save (contact_widget);
 
        if (response == GTK_RESPONSE_YES) {
                empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
-                                         contact,
-                                         _("I would like to add you to my contact list."));
+                                         contact, "");
        }
        else if (response == GTK_RESPONSE_NO) {
                empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
-                                            contact,
-                                            _("Sorry, I don't want you in my contact list."));
+                                            contact, "");
        }
 
-       g_hash_table_remove (subscription_dialogs, contact);
+       subscription_dialogs = g_list_remove (subscription_dialogs, dialog);
+       gtk_widget_destroy (GTK_WIDGET (dialog));
        g_object_unref (manager);
 }
 
 void
-empathy_subscription_dialog_show (GossipContact *contact,
+empathy_subscription_dialog_show (EmpathyContact *contact,
                                  GtkWindow     *parent)
 {
+       GtkBuilder *gui;
        GtkWidget *dialog;
        GtkWidget *hbox_subscription;
        GtkWidget *contact_widget;
+       GList     *l;
+       gchar     *filename;
 
-       g_return_if_fail (GOSSIP_IS_CONTACT (contact));
+       g_return_if_fail (EMPATHY_IS_CONTACT (contact));
 
-       if (!subscription_dialogs) {
-               subscription_dialogs = g_hash_table_new_full (gossip_contact_hash,
-                                                             gossip_contact_equal,
-                                                             (GDestroyNotify) g_object_unref,
-                                                             (GDestroyNotify) gtk_widget_destroy);
-       }
-
-       dialog = g_hash_table_lookup (subscription_dialogs, contact);
-       if (dialog) {
-               gtk_window_present (GTK_WINDOW (dialog));
+       l = g_list_find_custom (subscription_dialogs,
+                               contact,
+                               (GCompareFunc) contact_dialogs_find);
+       if (l) {
+               gtk_window_present (GTK_WINDOW (l->data));
                return;
        }
 
-       gossip_glade_get_file_simple ("empathy-contact-dialogs.glade",
-                                     "subscription_request_dialog",
-                                     NULL,
+       filename = empathy_file_lookup ("empathy-contact-dialogs.ui",
+                                       "libempathy-gtk");
+       gui = empathy_builder_get_file (filename,
                                      "subscription_request_dialog", &dialog,
                                      "hbox_subscription", &hbox_subscription,
                                      NULL);
+       g_free (filename);
+       g_object_unref (gui);
 
-       g_hash_table_insert (subscription_dialogs, g_object_ref (contact), dialog);
-
-       contact_widget = empathy_contact_widget_new (contact, TRUE);
+       contact_widget = empathy_contact_widget_new (contact,
+                                                    EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
+                                                    EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
        gtk_box_pack_end (GTK_BOX (hbox_subscription),
                          contact_widget,
                          TRUE, TRUE,
                          0);
+       gtk_widget_show (contact_widget);
+
+
+       g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
+       subscription_dialogs = g_list_prepend (subscription_dialogs, dialog);
 
        g_signal_connect (dialog, "response",
                          G_CALLBACK (subscription_dialog_response_cb),
@@ -128,81 +148,198 @@ contact_information_response_cb (GtkDialog *dialog,
                                 gint       response,
                                 GtkWidget *contact_widget)
 {
-       GossipContact *contact;
+       information_dialogs = g_list_remove (information_dialogs, dialog);
+       gtk_widget_destroy (GTK_WIDGET (dialog));
+}
+
+void
+empathy_contact_information_dialog_show (EmpathyContact *contact,
+                                        GtkWindow      *parent,
+                                        gboolean        edit,
+                                        gboolean        is_user)
+{
+       GtkWidget                *dialog;
+       GtkWidget                *button;
+       GtkWidget                *contact_widget;
+       GList                    *l;
+       EmpathyContactWidgetFlags flags = 0;
+
+       g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+
+       l = g_list_find_custom (information_dialogs,
+                               contact,
+                               (GCompareFunc) contact_dialogs_find);
+       if (l) {
+               gtk_window_present (GTK_WINDOW (l->data));
+               return;
+       }
+
+       /* Create dialog */
+       dialog = gtk_dialog_new ();
+       gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
+       gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
+       if (is_user) {
+               gtk_window_set_title (GTK_WINDOW (dialog), _("Personal Information"));
+       }
+       else if (edit) {
+               gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
+       }
+       else {
+               gtk_window_set_title (GTK_WINDOW (dialog), _("Contact Information"));
+       }
+
+       /* Close button */
+       button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
+       gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
+       gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
+                                     button,
+                                     GTK_RESPONSE_CLOSE);
+       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
+       gtk_window_set_default (GTK_WINDOW (dialog), button);
+       gtk_widget_show (button);
+
+       /* Contact info widget */
+       if (edit) {
+               flags |= EMPATHY_CONTACT_WIDGET_EDIT_ALIAS;
+       }
+       if (is_user) {
+               flags |= EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT;
+               flags |= EMPATHY_CONTACT_WIDGET_EDIT_AVATAR;
+       }
+       if (!is_user && edit) {
+               flags |= EMPATHY_CONTACT_WIDGET_EDIT_GROUPS;
+       }
+       contact_widget = empathy_contact_widget_new (contact, flags);
+       gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
+       gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
+                           contact_widget,
+                           TRUE, TRUE, 0);
+       if (flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT) {
+               empathy_contact_widget_set_account_filter (contact_widget,
+                                                          empathy_account_chooser_filter_is_connected,
+                                                          NULL);
+       }
+       gtk_widget_show (contact_widget);
+
+       g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
+       information_dialogs = g_list_prepend (information_dialogs, dialog);
+
+       g_signal_connect (dialog, "response",
+                         G_CALLBACK (contact_information_response_cb),
+                         contact_widget);
+
+       if (parent) {
+               gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
+       }
+
+       gtk_widget_show (dialog);
+}
+
+/*
+ *  New contact dialog
+ */
+
+static gboolean
+can_add_contact_to_account (McAccount *account,
+                           gpointer   user_data)
+{
+       EmpathyAccountManager *account_manager;
+       EmpathyContactManager *contact_manager;
+       TpConnection          *connection;
+       gboolean               result;
+
+       account_manager = empathy_account_manager_dup_singleton ();
+       connection = empathy_account_manager_get_connection (account_manager,
+                                                            account);
+       if (!connection) {
+               g_object_unref (account_manager);
+               return FALSE;
+       }
 
+       contact_manager = empathy_contact_manager_dup_singleton ();
+       result = empathy_contact_manager_can_add (contact_manager, connection);
+       g_object_unref (contact_manager);
+       g_object_unref (account_manager);
+
+       return result;
+}
+
+static void
+new_contact_response_cb (GtkDialog *dialog,
+                        gint       response,
+                        GtkWidget *contact_widget)
+{
+       EmpathyContactManager *manager;
+       EmpathyContact         *contact;
+
+       manager = empathy_contact_manager_dup_singleton ();
        contact = empathy_contact_widget_get_contact (contact_widget);
 
-       if (response == GTK_RESPONSE_OK) {
-               empathy_contact_widget_save (contact_widget);
+       if (contact && response == GTK_RESPONSE_OK) {
+               empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
+                                         contact,
+                                         _("I would like to add you to my contact list."));
        }
 
-       g_hash_table_remove (information_dialogs, contact);
+       new_contact_dialog = NULL;
+       gtk_widget_destroy (GTK_WIDGET (dialog));
+       g_object_unref (manager);
 }
 
 void
-empathy_contact_information_dialog_show (GossipContact *contact,
-                                        GtkWindow     *parent,
-                                        gboolean       edit)
+empathy_new_contact_dialog_show (GtkWindow *parent)
 {
        GtkWidget *dialog;
        GtkWidget *button;
        GtkWidget *contact_widget;
 
-       g_return_if_fail (GOSSIP_IS_CONTACT (contact));
-
-       if (!information_dialogs) {
-               information_dialogs = g_hash_table_new_full (gossip_contact_hash,
-                                                            gossip_contact_equal,
-                                                            (GDestroyNotify) g_object_unref,
-                                                            (GDestroyNotify) gtk_widget_destroy);
-       }
-
-       dialog = g_hash_table_lookup (information_dialogs, contact);
-       if (dialog) {
-               gtk_window_present (GTK_WINDOW (dialog));
+       if (new_contact_dialog) {
+               gtk_window_present (GTK_WINDOW (new_contact_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);
-
-       if (edit) {
-               /* 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);
-
-               button = gtk_button_new_with_label (GTK_STOCK_SAVE);
-               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);
-       } else {
-               /* Close button */
-               button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
-               gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
-               gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
-                                             button,
-                                             GTK_RESPONSE_CLOSE);
-               gtk_widget_show (button);
-       }
+       gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
+
+       /* 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);
        
-       contact_widget = empathy_contact_widget_new (contact, edit);
+       /* 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 */
+       contact_widget = empathy_contact_widget_new (NULL,
+                                                    EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
+                                                    EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
+                                                    EMPATHY_CONTACT_WIDGET_EDIT_ID |
+                                                    EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
        gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
                            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_contact_dialog = dialog;
 
        g_signal_connect (dialog, "response",
-                         G_CALLBACK (contact_information_response_cb),
+                         G_CALLBACK (new_contact_response_cb),
                          contact_widget);
 
-       g_hash_table_insert (information_dialogs, g_object_ref (contact), dialog);
-
        if (parent) {
                gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
        }