]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-contact-dialogs.c
Merge commit 'staz/dnd'
[empathy.git] / libempathy-gtk / empathy-contact-dialogs.c
index 437e255fa80d0d01c236579216692082f6b0faae..fed8d04cb262a99c97a603627c7aa36394e978af 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 <telepathy-glib/account-manager.h>
 
 #include <libempathy/empathy-contact-manager.h>
 #include <libempathy/empathy-contact-list.h>
+#include <libempathy/empathy-tp-contact-factory.h>
+#include <libempathy/empathy-utils.h>
 
 #include "empathy-contact-dialogs.h"
 #include "empathy-contact-widget.h"
 
 static GList *subscription_dialogs = NULL;
 static GList *information_dialogs = NULL;
+static GList *edit_dialogs = NULL;
+static GtkWidget *personal_dialog = NULL;
 static GtkWidget *new_contact_dialog = NULL;
 
-
 static gint
-contact_dialogs_find (GtkDialog     *dialog,
+contact_dialogs_find (GtkDialog      *dialog,
                      EmpathyContact *contact)
 {
        GtkWidget     *contact_widget;
@@ -51,7 +54,7 @@ contact_dialogs_find (GtkDialog     *dialog,
        contact_widget = g_object_get_data (G_OBJECT (dialog), "contact_widget");
        this_contact = empathy_contact_widget_get_contact (contact_widget);
 
-       return !empathy_contact_equal (contact, this_contact);
+       return contact != this_contact;
 }
 
 /*
@@ -64,18 +67,27 @@ subscription_dialog_response_cb (GtkDialog *dialog,
                                 GtkWidget *contact_widget)
 {
        EmpathyContactManager *manager;
-       EmpathyContact         *contact;
+       EmpathyContact        *contact;
 
-       manager = empathy_contact_manager_new ();
+       manager = empathy_contact_manager_dup_singleton ();
        contact = empathy_contact_widget_get_contact (contact_widget);
 
        if (response == GTK_RESPONSE_YES) {
-               empathy_contact_list_process_pending (EMPATHY_CONTACT_LIST (manager),
-                                                     contact, TRUE);
+               EmpathyTpContactFactory *factory;
+
+               factory = empathy_tp_contact_factory_dup_singleton (
+                       empathy_contact_get_connection (contact));
+
+               empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
+                                         contact, "");
+               empathy_tp_contact_factory_set_alias (factory, contact,
+                       empathy_contact_widget_get_alias (contact_widget));
+
+               g_object_unref (factory);
        }
        else if (response == GTK_RESPONSE_NO) {
-               empathy_contact_list_process_pending (EMPATHY_CONTACT_LIST (manager),
-                                                     contact, FALSE);
+               empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
+                                            contact, "");
        }
 
        subscription_dialogs = g_list_remove (subscription_dialogs, dialog);
@@ -87,10 +99,12 @@ void
 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 (EMPATHY_IS_CONTACT (contact));
 
@@ -102,19 +116,25 @@ empathy_subscription_dialog_show (EmpathyContact *contact,
                return;
        }
 
-       empathy_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);
 
+       /* Contact info widget */
        contact_widget = empathy_contact_widget_new (contact,
-                                                    CONTACT_WIDGET_TYPE_SUBSCRIPTION);
+                                                    EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS |
+                                                    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);
@@ -135,24 +155,22 @@ empathy_subscription_dialog_show (EmpathyContact *contact,
  */
 
 static void
-contact_information_response_cb (GtkDialog *dialog,
-                                gint       response,
-                                GtkWidget *contact_widget)
+contact_dialogs_response_cb (GtkDialog *dialog,
+                            gint       response,
+                            GList    **dialogs)
 {
-       information_dialogs = g_list_remove (information_dialogs, dialog);
+       *dialogs = g_list_remove (*dialogs, dialog);
        gtk_widget_destroy (GTK_WIDGET (dialog));
 }
 
 void
 empathy_contact_information_dialog_show (EmpathyContact *contact,
-                                        GtkWindow      *parent,
-                                        gboolean        edit)
+                                        GtkWindow      *parent)
 {
-       GtkWidget                *dialog;
-       GtkWidget                *button;
-       GtkWidget                *contact_widget;
-       GList                    *l;
-       EmpathyContactWidgetType  type;
+       GtkWidget *dialog;
+       GtkWidget *button;
+       GtkWidget *contact_widget;
+       GList     *l;
 
        g_return_if_fail (EMPATHY_IS_CONTACT (contact));
 
@@ -164,13 +182,12 @@ empathy_contact_information_dialog_show (EmpathyContact *contact,
                return;
        }
 
-       type = edit ? CONTACT_WIDGET_TYPE_EDIT : CONTACT_WIDGET_TYPE_SHOW;
-
        /* 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), _("Contact information"));
+       gtk_window_set_title (GTK_WINDOW (dialog),
+               empathy_contact_get_name (contact));
 
        /* Close button */
        button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
@@ -178,20 +195,86 @@ empathy_contact_information_dialog_show (EmpathyContact *contact,
        gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
                                      button,
                                      GTK_RESPONSE_CLOSE);
+       gtk_widget_set_can_default (button, TRUE);
+       gtk_window_set_default (GTK_WINDOW (dialog), button);
        gtk_widget_show (button);
-       
+
        /* Contact info widget */
-       contact_widget = empathy_contact_widget_new (contact, type);
-       gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
+       contact_widget = empathy_contact_widget_new (contact,
+               EMPATHY_CONTACT_WIDGET_SHOW_LOCATION |
+               EMPATHY_CONTACT_WIDGET_EDIT_NONE);
+       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);
+       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);
+                         G_CALLBACK (contact_dialogs_response_cb),
+                         &information_dialogs);
+
+       if (parent) {
+               gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
+       }
+
+       gtk_widget_show (dialog);
+}
+
+void
+empathy_contact_edit_dialog_show (EmpathyContact *contact,
+                                 GtkWindow      *parent)
+{
+       GtkWidget *dialog;
+       GtkWidget *button;
+       GtkWidget *contact_widget;
+       GList     *l;
+
+       g_return_if_fail (EMPATHY_IS_CONTACT (contact));
+
+       l = g_list_find_custom (edit_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);
+       gtk_window_set_title (GTK_WINDOW (dialog), _("Edit 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_can_default (button, TRUE);
+       gtk_window_set_default (GTK_WINDOW (dialog), button);
+       gtk_widget_show (button);
+
+       /* Contact info widget */
+       contact_widget = empathy_contact_widget_new (contact,
+               EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
+               EMPATHY_CONTACT_WIDGET_EDIT_GROUPS |
+               EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE);
+       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);
+       gtk_widget_show (contact_widget);
+
+       g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
+       edit_dialogs = g_list_prepend (edit_dialogs, dialog);
+
+       g_signal_connect (dialog, "response",
+                         G_CALLBACK (contact_dialogs_response_cb),
+                         &edit_dialogs);
 
        if (parent) {
                gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
@@ -200,10 +283,82 @@ empathy_contact_information_dialog_show (EmpathyContact *contact,
        gtk_widget_show (dialog);
 }
 
+void
+empathy_contact_personal_dialog_show (GtkWindow *parent)
+{
+       GtkWidget *button;
+       GtkWidget *contact_widget;
+
+       if (personal_dialog) {
+               gtk_window_present (GTK_WINDOW (personal_dialog));
+               return;
+       }
+
+       /* Create dialog */
+       personal_dialog = gtk_dialog_new ();
+       gtk_dialog_set_has_separator (GTK_DIALOG (personal_dialog), FALSE);
+       gtk_window_set_resizable (GTK_WINDOW (personal_dialog), FALSE);
+       gtk_window_set_title (GTK_WINDOW (personal_dialog), _("Personal 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 (personal_dialog),
+                                     button,
+                                     GTK_RESPONSE_CLOSE);
+       gtk_widget_set_can_default (button, TRUE);
+       gtk_window_set_default (GTK_WINDOW (personal_dialog), button);
+       gtk_widget_show (button);
+
+       /* Contact info widget */
+       contact_widget = empathy_contact_widget_new (NULL,
+               EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
+               EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
+               EMPATHY_CONTACT_WIDGET_EDIT_AVATAR);
+       gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
+       gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (personal_dialog))),
+                           contact_widget,
+                           TRUE, TRUE, 0);
+       empathy_contact_widget_set_account_filter (contact_widget,
+               empathy_account_chooser_filter_is_connected, NULL);
+       gtk_widget_show (contact_widget);
+
+       g_signal_connect (personal_dialog, "response",
+                         G_CALLBACK (gtk_widget_destroy), NULL);
+       g_object_add_weak_pointer (G_OBJECT (personal_dialog),
+                                  (gpointer) &personal_dialog);
+
+       if (parent) {
+               gtk_window_set_transient_for (GTK_WINDOW (personal_dialog), parent);
+       }
+
+       gtk_widget_show (personal_dialog);
+}
+
 /*
  *  New contact dialog
  */
 
+static gboolean
+can_add_contact_to_account (TpAccount *account,
+                           gpointer   user_data)
+{
+       EmpathyContactManager *contact_manager;
+       TpConnection          *connection;
+       gboolean               result;
+
+       connection = tp_account_get_connection (account);
+       if (connection == NULL)
+               return FALSE;
+
+       contact_manager = empathy_contact_manager_dup_singleton ();
+       result = empathy_contact_manager_get_flags_for_connection (
+               contact_manager, connection) & EMPATHY_CONTACT_LIST_CAN_ADD;
+       g_object_unref (contact_manager);
+
+       return result;
+}
+
 static void
 new_contact_response_cb (GtkDialog *dialog,
                         gint       response,
@@ -212,13 +367,12 @@ new_contact_response_cb (GtkDialog *dialog,
        EmpathyContactManager *manager;
        EmpathyContact         *contact;
 
-       manager = empathy_contact_manager_new ();
+       manager = empathy_contact_manager_dup_singleton ();
        contact = empathy_contact_widget_get_contact (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."));
+                                         contact, "");
        }
 
        new_contact_dialog = NULL;
@@ -228,6 +382,13 @@ new_contact_response_cb (GtkDialog *dialog,
 
 void
 empathy_new_contact_dialog_show (GtkWindow *parent)
+{
+       empathy_new_contact_dialog_show_with_contact (parent, NULL);
+}
+
+void
+empathy_new_contact_dialog_show_with_contact (GtkWindow *parent,
+                                              EmpathyContact *contact)
 {
        GtkWidget *dialog;
        GtkWidget *button;
@@ -242,7 +403,7 @@ empathy_new_contact_dialog_show (GtkWindow *parent)
        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 contact"));
+       gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
 
        /* Cancel button */
        button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
@@ -251,7 +412,7 @@ empathy_new_contact_dialog_show (GtkWindow *parent)
                                      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);
@@ -261,10 +422,19 @@ empathy_new_contact_dialog_show (GtkWindow *parent)
        gtk_widget_show (button);
 
        /* Contact info widget */
-       contact_widget = empathy_contact_widget_new (NULL, CONTACT_WIDGET_TYPE_ADD);
-       gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
+       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_contact_dialog = dialog;