]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
Improve dispatcher. Fixes bug #465928.
[empathy.git] / libempathy-gtk / empathy-new-message-dialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 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 <glade/glade.h>
29 #include <glib/gi18n.h>
30
31 #include <libmissioncontrol/mc-account.h>
32 #include <libmissioncontrol/mission-control.h>
33
34 #include <libempathy/empathy-contact-factory.h>
35 #include <libempathy/empathy-dispatcher.h>
36 #include <libempathy/empathy-utils.h>
37
38 #include <libempathy-gtk/empathy-ui-utils.h>
39
40 #include "empathy-new-message-dialog.h"
41 #include "empathy-account-chooser.h"
42
43 typedef struct {
44         GtkWidget *dialog;
45         GtkWidget *table_contact;
46         GtkWidget *account_chooser;
47         GtkWidget *entry_id;
48         GtkWidget *button_chat;
49         GtkWidget *button_call;
50 } EmpathyNewMessageDialog;
51
52 static void
53 new_message_dialog_response_cb (GtkWidget               *widget,
54                                 gint                    response,
55                                 EmpathyNewMessageDialog *dialog)
56 {
57         McAccount   *account;
58         const gchar *id;
59
60         account = empathy_account_chooser_get_account (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser));
61         id = gtk_entry_get_text (GTK_ENTRY (dialog->entry_id));
62         if (!account || G_STR_EMPTY (id)) {
63                 if (account) {
64                         g_object_unref (account);
65                 }
66                 gtk_widget_destroy (widget);
67                 return;
68         }
69
70         if (response == 1) {
71                 empathy_dispatcher_call_with_contact_id (account, id);
72         }       
73         else if (response == 2) {
74                 empathy_dispatcher_chat_with_contact_id (account, id);
75         }
76
77         g_object_unref (account);
78         gtk_widget_destroy (widget);
79 }
80
81 static void
82 new_message_change_state_button_cb  (GtkEditable             *editable,
83                                      EmpathyNewMessageDialog *dialog)  
84 {
85         const gchar *id;
86         gboolean     sensitive;
87
88         id = gtk_entry_get_text (GTK_ENTRY (editable));
89         sensitive = !G_STR_EMPTY (id);
90         
91         gtk_widget_set_sensitive (dialog->button_chat, sensitive);
92         gtk_widget_set_sensitive (dialog->button_call, sensitive);
93 }
94
95 static void
96 new_message_dialog_destroy_cb (GtkWidget               *widget,
97                                EmpathyNewMessageDialog *dialog)
98 {       
99         g_free (dialog);
100 }
101
102 GtkWidget *
103 empathy_new_message_dialog_show (GtkWindow *parent)
104 {
105         static EmpathyNewMessageDialog *dialog = NULL;
106         GladeXML                       *glade;
107         gchar                          *filename;
108
109         if (dialog) {
110                 gtk_window_present (GTK_WINDOW (dialog->dialog));
111                 return dialog->dialog;
112         }
113
114         dialog = g_new0 (EmpathyNewMessageDialog, 1);
115
116         filename = empathy_file_lookup ("empathy-new-message-dialog.glade",
117                                         "libempathy-gtk");
118         glade = empathy_glade_get_file (filename,
119                                         "new_message_dialog",
120                                         NULL,
121                                         "new_message_dialog", &dialog->dialog,
122                                         "table_contact", &dialog->table_contact,
123                                         "entry_id", &dialog->entry_id,
124                                         "button_chat", &dialog->button_chat,
125                                         "button_call",&dialog->button_call,
126                                         NULL);
127         g_free (filename);
128
129         empathy_glade_connect (glade,
130                                dialog,
131                                "new_message_dialog", "destroy", new_message_dialog_destroy_cb,
132                                "new_message_dialog", "response", new_message_dialog_response_cb,
133                                "entry_id", "changed", new_message_change_state_button_cb,
134                                NULL);
135
136         g_object_add_weak_pointer (G_OBJECT (dialog->dialog), (gpointer) &dialog);
137
138         g_object_unref (glade);
139
140         /* Create account chooser */
141         dialog->account_chooser = empathy_account_chooser_new ();
142         gtk_table_attach_defaults (GTK_TABLE (dialog->table_contact),
143                                    dialog->account_chooser,
144                                    1, 2, 0, 1);
145         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
146                                             empathy_account_chooser_filter_is_connected,
147                                             NULL);
148         gtk_widget_show (dialog->account_chooser);
149
150         if (parent) {
151                 gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
152                                               GTK_WINDOW (parent));
153         }
154
155         gtk_widget_set_sensitive (dialog->button_chat, FALSE);
156         gtk_widget_set_sensitive (dialog->button_call, FALSE);
157
158         gtk_widget_show (dialog->dialog);
159
160         return dialog->dialog;
161 }
162