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