]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-message-dialog.c
Add convinience functions empathy_chat_with_contact() and empathy_chat_with_contact_i...
[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 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-debug.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 #define DEBUG_DOMAIN "NewMessageDialog"
44
45 typedef struct {
46         GtkWidget *dialog;
47         GtkWidget *table_contact;
48         GtkWidget *account_chooser;
49         GtkWidget *entry_id;
50         GtkWidget *button_validate;
51         GtkWidget *button_voip;
52 } EmpathyNewMessageDialog;
53
54 static void
55 new_message_dialog_response_cb (GtkWidget               *widget,
56                                 gint                    response,
57                                 EmpathyNewMessageDialog *dialog)
58 {
59         McAccount   *account;
60         const gchar *id;
61
62         account = empathy_account_chooser_get_account (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser));
63         id = gtk_entry_get_text (GTK_ENTRY (dialog->entry_id));
64         if (!account || G_STR_EMPTY (id)) {
65                 if (account) {
66                         g_object_unref (account);
67                 }
68                 gtk_widget_destroy (widget);
69                 return;
70         }
71
72         if (response == GTK_RESPONSE_OK) {
73                 empathy_chat_with_contact_id (account, id);
74         }       
75         else if (response == 3) {
76                 EmpathyContactFactory *factory;
77                 EmpathyContact        *contact = NULL;
78
79                 factory = empathy_contact_factory_new ();
80                 contact = empathy_contact_factory_get_from_id (factory,
81                                                                account,
82                                                                id);
83                 if (contact) {
84                         empathy_call_contact (contact);
85                 } else {
86                         empathy_debug (DEBUG_DOMAIN,
87                                        "Contact ID %s does not exists",
88                                        id);
89                 }
90
91                 g_object_unref (contact);
92                 g_object_unref (factory);
93         }
94
95         g_object_unref (account);
96         gtk_widget_destroy (widget);
97 }
98
99 static void
100 new_message_change_state_button_cb  (GtkEditable             *editable,
101                                      EmpathyNewMessageDialog *dialog)  
102 {
103         const gchar *id;
104         gboolean     sensitive;
105
106         id = gtk_entry_get_text (GTK_ENTRY (editable));
107         sensitive = !G_STR_EMPTY (id);
108         
109         gtk_widget_set_sensitive(dialog->button_validate, sensitive);
110         gtk_widget_set_sensitive(dialog->button_voip, sensitive);
111 }
112
113 static void
114 new_message_dialog_destroy_cb (GtkWidget               *widget,
115                                EmpathyNewMessageDialog *dialog)
116 {       
117         g_free (dialog);
118 }
119
120 GtkWidget *
121 empathy_new_message_dialog_show (GtkWindow *parent)
122 {
123         static EmpathyNewMessageDialog *dialog = NULL;
124         GladeXML                       *glade;
125
126         if (dialog) {
127                 gtk_window_present (GTK_WINDOW (dialog->dialog));
128                 return dialog->dialog;
129         }
130
131         dialog = g_new0 (EmpathyNewMessageDialog, 1);
132
133         glade = empathy_glade_get_file ("empathy-new-message-dialog.glade",
134                                        "new_message_dialog",
135                                        NULL,
136                                        "new_message_dialog", &dialog->dialog,
137                                        "table_contact", &dialog->table_contact,
138                                        "entry_id", &dialog->entry_id,
139                                         "button_validate", &dialog->button_validate,
140                                         "button_voip",&dialog->button_voip,
141                                        NULL);
142
143         empathy_glade_connect (glade,
144                               dialog,
145                               "new_message_dialog", "destroy", new_message_dialog_destroy_cb,
146                               "new_message_dialog", "response", new_message_dialog_response_cb,
147                               "entry_id", "changed", new_message_change_state_button_cb,
148                               NULL);
149
150         g_object_add_weak_pointer (G_OBJECT (dialog->dialog), (gpointer) &dialog);
151
152         g_object_unref (glade);
153
154         /* Create account chooser */
155         dialog->account_chooser = empathy_account_chooser_new ();
156         gtk_table_attach_defaults (GTK_TABLE (dialog->table_contact),
157                                    dialog->account_chooser,
158                                    1, 2, 0, 1);
159         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
160                                             empathy_account_chooser_filter_is_connected,
161                                             NULL);
162         gtk_widget_show (dialog->account_chooser);
163
164         if (parent) {
165                 gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
166                                               GTK_WINDOW (parent));
167         }
168
169         gtk_widget_set_sensitive(dialog->button_validate, FALSE);
170         gtk_widget_set_sensitive(dialog->button_voip, FALSE);
171
172         gtk_widget_show (dialog->dialog);
173
174         return dialog->dialog;
175 }
176