]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-dialogs.c
Merge branch 'sasl'
[empathy.git] / libempathy-gtk / empathy-individual-dialogs.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2010 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28 #include <glib/gi18n-lib.h>
29
30 #include <telepathy-glib/util.h>
31 #include <folks/folks.h>
32
33 #include <libempathy/empathy-individual-manager.h>
34 #include <libempathy/empathy-utils.h>
35
36 #include "empathy-individual-dialogs.h"
37 #include "empathy-contact-widget.h"
38 #include "empathy-ui-utils.h"
39
40 static GtkWidget *new_individual_dialog = NULL;
41
42 /*
43  *  New contact dialog
44  */
45
46 static void
47 can_add_contact_to_account (TpAccount *account,
48     EmpathyAccountChooserFilterResultCallback callback,
49     gpointer callback_data,
50     gpointer user_data)
51 {
52   EmpathyIndividualManager *individual_manager;
53   TpConnection *connection;
54   gboolean result;
55
56   connection = tp_account_get_connection (account);
57   if (connection == NULL)
58     {
59       callback (FALSE, callback_data);
60       return;
61     }
62
63   individual_manager = empathy_individual_manager_dup_singleton ();
64   result = empathy_connection_can_add_personas (connection);
65   g_object_unref (individual_manager);
66
67   callback (result, callback_data);
68 }
69
70 static void
71 new_individual_response_cb (GtkDialog *dialog,
72     gint response,
73     GtkWidget *contact_widget)
74 {
75   EmpathyIndividualManager *individual_manager;
76   EmpathyContact *contact;
77
78   individual_manager = empathy_individual_manager_dup_singleton ();
79   contact = empathy_contact_widget_get_contact (contact_widget);
80
81   if (contact && response == GTK_RESPONSE_OK)
82     empathy_individual_manager_add_from_contact (individual_manager, contact);
83
84   new_individual_dialog = NULL;
85   gtk_widget_destroy (GTK_WIDGET (dialog));
86   g_object_unref (individual_manager);
87 }
88
89 void
90 empathy_new_individual_dialog_show (GtkWindow *parent)
91 {
92   empathy_new_individual_dialog_show_with_individual (parent, NULL);
93 }
94
95 void
96 empathy_new_individual_dialog_show_with_individual (GtkWindow *parent,
97     FolksIndividual *individual)
98 {
99   GtkWidget *dialog;
100   GtkWidget *button;
101   EmpathyContact *contact = NULL;
102   GtkWidget *contact_widget;
103
104   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
105
106   if (new_individual_dialog)
107     {
108       gtk_window_present (GTK_WINDOW (new_individual_dialog));
109       return;
110     }
111
112   /* Create dialog */
113   dialog = gtk_dialog_new ();
114   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
115   gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
116
117   /* Cancel button */
118   button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
119   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
120   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
121       GTK_RESPONSE_CANCEL);
122   gtk_widget_show (button);
123
124   /* Add button */
125   button = gtk_button_new_with_label (GTK_STOCK_ADD);
126   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
127   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_OK);
128   gtk_widget_show (button);
129
130   /* Contact info widget */
131   if (individual != NULL)
132     contact = empathy_contact_dup_from_folks_individual (individual);
133
134   contact_widget = empathy_contact_widget_new (contact,
135       EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
136       EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
137       EMPATHY_CONTACT_WIDGET_EDIT_ID |
138       EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
139   gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
140   gtk_box_pack_start (
141       GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
142       contact_widget, TRUE, TRUE, 0);
143   empathy_contact_widget_set_account_filter (contact_widget,
144       can_add_contact_to_account, NULL);
145   gtk_widget_show (contact_widget);
146
147   new_individual_dialog = dialog;
148
149   g_signal_connect (dialog, "response", G_CALLBACK (new_individual_response_cb),
150       contact_widget);
151
152   if (parent != NULL)
153     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
154
155   gtk_widget_show (dialog);
156
157   tp_clear_object (&contact);
158 }