]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-dialogs.c
Implement editing and viewing contact information with right click on the
[empathy.git] / libempathy-gtk / empathy-contact-dialogs.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include <gtk/gtk.h>
29 #include <glade/glade.h>
30 #include <glib/gi18n.h>
31
32 #include <libempathy/empathy-contact-manager.h>
33 #include <libempathy/empathy-contact-list.h>
34
35 #include "empathy-contact-dialogs.h"
36 #include "empathy-contact-widget.h"
37 #include "gossip-ui-utils.h"
38
39 static GHashTable *subscription_dialogs = NULL;
40 static GHashTable *information_dialogs = NULL;
41
42 /*
43  *  Subscription dialog
44  */
45
46 static void
47 subscription_dialog_response_cb (GtkDialog *dialog,
48                                  gint       response,
49                                  GtkWidget *contact_widget)
50 {
51         EmpathyContactManager *manager;
52         GossipContact         *contact;
53
54         manager = empathy_contact_manager_new ();
55         contact = empathy_contact_widget_get_contact (contact_widget);
56         empathy_contact_widget_save (contact_widget);
57
58         if (response == GTK_RESPONSE_YES) {
59                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
60                                           contact,
61                                           _("I would like to add you to my contact list."));
62         }
63         else if (response == GTK_RESPONSE_NO) {
64                 empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
65                                              contact,
66                                              _("Sorry, I don't want you in my contact list."));
67         }
68
69         g_hash_table_remove (subscription_dialogs, contact);
70         g_object_unref (manager);
71 }
72
73 void
74 empathy_subscription_dialog_show (GossipContact *contact,
75                                   GtkWindow     *parent)
76 {
77         GtkWidget *dialog;
78         GtkWidget *hbox_subscription;
79         GtkWidget *contact_widget;
80
81         g_return_if_fail (GOSSIP_IS_CONTACT (contact));
82
83         if (!subscription_dialogs) {
84                 subscription_dialogs = g_hash_table_new_full (gossip_contact_hash,
85                                                               gossip_contact_equal,
86                                                               (GDestroyNotify) g_object_unref,
87                                                               (GDestroyNotify) gtk_widget_destroy);
88         }
89
90         dialog = g_hash_table_lookup (subscription_dialogs, contact);
91         if (dialog) {
92                 gtk_window_present (GTK_WINDOW (dialog));
93                 return;
94         }
95
96         gossip_glade_get_file_simple ("empathy-contact-dialogs.glade",
97                                       "subscription_request_dialog",
98                                       NULL,
99                                       "subscription_request_dialog", &dialog,
100                                       "hbox_subscription", &hbox_subscription,
101                                       NULL);
102
103         g_hash_table_insert (subscription_dialogs, g_object_ref (contact), dialog);
104
105         contact_widget = empathy_contact_widget_new (contact, TRUE);
106         gtk_box_pack_end (GTK_BOX (hbox_subscription),
107                           contact_widget,
108                           TRUE, TRUE,
109                           0);
110
111         g_signal_connect (dialog, "response",
112                           G_CALLBACK (subscription_dialog_response_cb),
113                           contact_widget);
114
115         if (parent) {
116                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
117         }
118
119         gtk_widget_show (dialog);
120 }
121
122 /*
123  *  Information dialog
124  */
125
126 static void
127 contact_information_response_cb (GtkDialog *dialog,
128                                  gint       response,
129                                  GtkWidget *contact_widget)
130 {
131         GossipContact *contact;
132
133         contact = empathy_contact_widget_get_contact (contact_widget);
134         empathy_contact_widget_save (contact_widget);
135
136         g_hash_table_remove (information_dialogs, contact);
137 }
138
139 void
140 empathy_contact_information_dialog_show (GossipContact *contact,
141                                          GtkWindow     *parent,
142                                          gboolean       edit)
143 {
144         GtkWidget *dialog;
145         GtkWidget *button;
146         GtkWidget *contact_widget;
147
148         g_return_if_fail (GOSSIP_IS_CONTACT (contact));
149
150         if (!information_dialogs) {
151                 information_dialogs = g_hash_table_new_full (gossip_contact_hash,
152                                                              gossip_contact_equal,
153                                                              (GDestroyNotify) g_object_unref,
154                                                              (GDestroyNotify) gtk_widget_destroy);
155         }
156
157         dialog = g_hash_table_lookup (information_dialogs, contact);
158         if (dialog) {
159                 gtk_window_present (GTK_WINDOW (dialog));
160                 return;
161         }
162
163         dialog = gtk_dialog_new ();
164         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
165         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
166
167         if (edit) {
168                 /* Cancel button */
169                 button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
170                 gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
171                 gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
172                                               button,
173                                               GTK_RESPONSE_CANCEL);
174                 gtk_widget_show (button);
175
176                 button = gtk_button_new_with_label (GTK_STOCK_SAVE);
177                 gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
178                 gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
179                                               button,
180                                               GTK_RESPONSE_OK);
181                 gtk_widget_show (button);
182         } else {
183                 /* Close button */
184                 button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
185                 gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
186                 gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
187                                               button,
188                                               GTK_RESPONSE_CLOSE);
189                 gtk_widget_show (button);
190         }
191         
192         contact_widget = empathy_contact_widget_new (contact, edit);
193         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
194                             contact_widget,
195                             TRUE, TRUE, 0);
196
197         g_signal_connect (dialog, "response",
198                           G_CALLBACK (contact_information_response_cb),
199                           contact_widget);
200
201         g_hash_table_insert (information_dialogs, g_object_ref (contact), dialog);
202
203         if (parent) {
204                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
205         }
206
207         gtk_widget_show (dialog);
208 }
209