]> git.0d.be Git - empathy.git/blob - nautilus-sendto-plugin/empathy-nautilus-sendto.c
trivial: fix a GCC build warning
[empathy.git] / nautilus-sendto-plugin / empathy-nautilus-sendto.c
1 /*
2  * Copyright (C) 2008, 2009 Collabora Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more av.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301  USA.
18  *
19  * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include <glib/gi18n-lib.h>
27 #include <gtk/gtk.h>
28 #include <gio/gio.h>
29
30 #include <telepathy-glib/enums.h>
31
32 #include <libempathy/empathy-contact.h>
33 #include <libempathy/empathy-debug.h>
34 #include <libempathy/empathy-ft-factory.h>
35 #include <libempathy/empathy-ft-handler.h>
36
37 #include <libempathy-gtk/empathy-contact-chooser.h>
38 #include <libempathy-gtk/empathy-ui-utils.h>
39
40 #include "nautilus-sendto-plugin.h"
41
42 static EmpathyFTFactory *factory = NULL;
43 static guint transfers = 0;
44
45 static gboolean destroy (NstPlugin *plugin);
46
47 static gboolean
48 init (NstPlugin *plugin)
49 {
50   g_print ("Init %s plugin\n", plugin->info->id);
51
52   empathy_gtk_init ();
53
54   return TRUE;
55 }
56
57 static EmpathyContact *
58 dup_contact_from_individual (FolksIndividual *individual)
59 {
60   EmpathyContact *contact;
61   gboolean can_do_action;
62
63   if (individual == NULL)
64     return NULL;
65
66   contact = empathy_contact_dup_best_for_action (individual,
67       EMPATHY_ACTION_SEND_FILE);
68   if (contact == NULL)
69     return NULL;
70
71   can_do_action = empathy_contact_can_do_action (contact,
72       EMPATHY_ACTION_SEND_FILE);
73
74   if (!can_do_action)
75     {
76       /* If contact can't do FT we don't care about him */
77       g_object_unref (contact);
78       return NULL;
79     }
80
81   return contact;
82 }
83
84 static gboolean
85 filter_individual (EmpathyContactChooser *chooser,
86     FolksIndividual *individual,
87     gboolean is_online,
88     gboolean searching,
89     gpointer user_data)
90 {
91   EmpathyContact *contact;
92
93   if (!is_online)
94     return FALSE;
95
96   contact = dup_contact_from_individual (individual);
97   if (contact == NULL)
98     return FALSE;
99
100   g_object_unref (contact);
101   return TRUE;
102 }
103
104 static GtkWidget *
105 get_contacts_widget (NstPlugin *plugin)
106 {
107   GtkWidget *chooser;
108
109   chooser = empathy_contact_chooser_new ();
110   empathy_contact_chooser_set_filter_func (EMPATHY_CONTACT_CHOOSER (chooser),
111       filter_individual, plugin);
112
113   empathy_contact_chooser_show_search_entry (EMPATHY_CONTACT_CHOOSER (chooser),
114       FALSE);
115
116   /* Make sure to display some contacts */
117   gtk_widget_set_size_request (chooser, -1, 300);
118   return chooser;
119 }
120
121 static EmpathyContact *
122 get_selected_contact (GtkWidget *widget)
123 {
124   FolksIndividual *individual;
125   EmpathyContact *contact;
126
127   individual = empathy_contact_chooser_dup_selected (
128       EMPATHY_CONTACT_CHOOSER (widget));
129   if (individual == NULL)
130     return NULL;
131
132   contact = dup_contact_from_individual (individual);
133
134   g_object_unref (individual);
135   return contact;
136 }
137
138 static gboolean
139 validate_destination (NstPlugin *plugin,
140                       GtkWidget *contact_widget,
141                       gchar **error)
142 {
143   EmpathyContact *contact = NULL;
144
145   contact = get_selected_contact (contact_widget);
146
147   if (contact == NULL)
148     return FALSE;
149
150   g_object_unref (contact);
151
152   return TRUE;
153 }
154
155 static void
156 quit (void)
157 {
158   if (--transfers > 0)
159     return;
160
161   destroy (NULL);
162   gtk_main_quit ();
163 }
164
165 static void
166 transfer_done_cb (EmpathyFTHandler *handler,
167                   TpFileTransferChannel *channel,
168                   NstPlugin *plugin)
169 {
170   quit ();
171 }
172
173 static void
174 transfer_error_cb (EmpathyFTHandler *handler,
175                    GError *error,
176                    NstPlugin *plugin)
177 {
178   quit ();
179 }
180
181 static void
182 error_dialog_cb (GtkDialog *dialog,
183                  gint arg,
184                  gpointer user_data)
185 {
186   gtk_widget_destroy (GTK_WIDGET (dialog));
187   quit ();
188 }
189
190 static void
191 handler_ready_cb (EmpathyFTFactory *fact,
192                   EmpathyFTHandler *handler,
193                   GError *error,
194                   NstPlugin *plugin)
195 {
196   if (error != NULL)
197     {
198       GtkWidget *dialog;
199       dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR,
200           GTK_BUTTONS_CLOSE, "%s",
201           error->message ? error->message : _("No error message"));
202
203       g_signal_connect (dialog, "response", G_CALLBACK (error_dialog_cb), NULL);
204       gtk_widget_show (dialog);
205     }
206   else
207     {
208       g_signal_connect (handler, "transfer-done",
209           G_CALLBACK (transfer_done_cb), plugin);
210       g_signal_connect (handler, "transfer-error",
211           G_CALLBACK (transfer_error_cb), plugin);
212
213       empathy_ft_handler_start_transfer (handler);
214     }
215 }
216
217 static gboolean
218 send_files (NstPlugin *plugin,
219             GtkWidget *contact_widget,
220             GList *file_list)
221 {
222   EmpathyContact *contact;
223   GList *l;
224
225   contact = get_selected_contact (contact_widget);
226
227   if (contact == NULL)
228     return FALSE;
229
230   factory = empathy_ft_factory_dup_singleton ();
231
232   g_signal_connect (factory, "new-ft-handler",
233       G_CALLBACK (handler_ready_cb), plugin);
234
235   for (l = file_list; l; l = l->next)
236     {
237       gchar *path = l->data;
238       GFile *file;
239
240       file = g_file_new_for_uri (path);
241
242       ++transfers;
243
244       empathy_ft_factory_new_transfer_outgoing (factory,
245           contact, file, empathy_get_current_action_time ());
246
247       g_object_unref (file);
248     }
249
250   g_object_unref (contact);
251
252   if (transfers == 0)
253     {
254       destroy (NULL);
255       return TRUE;
256     }
257
258   return FALSE;
259 }
260
261 static gboolean
262 destroy (NstPlugin *plugin)
263 {
264   if (factory)
265     g_object_unref (factory);
266
267   return TRUE;
268 }
269
270 static
271 NstPluginInfo plugin_info = {
272   "im",
273   "empathy",
274   N_("Instant Message (Empathy)"),
275   GETTEXT_PACKAGE,
276   NAUTILUS_CAPS_NONE,
277   init,
278   get_contacts_widget,
279   validate_destination,
280   send_files,
281   destroy
282 };
283
284 NST_INIT_PLUGIN (plugin_info)
285