]> git.0d.be Git - empathy.git/blob - nautilus-sendto-plugin/empathy-nautilus-sendto.c
Fix for misaligned 16x16 app icon
[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 #include <libempathy/empathy-tp-file.h>
37
38 #include <libempathy-gtk/empathy-contact-chooser.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
40
41 #include "nautilus-sendto-plugin.h"
42
43 static EmpathyFTFactory *factory = NULL;
44 static guint transfers = 0;
45
46 static gboolean destroy (NstPlugin *plugin);
47
48 static gboolean
49 init (NstPlugin *plugin)
50 {
51   g_print ("Init %s plugin\n", plugin->info->id);
52
53   empathy_gtk_init ();
54
55   return TRUE;
56 }
57
58 static EmpathyContact *
59 dup_contact_from_individual (FolksIndividual *individual)
60 {
61   EmpathyContact *contact;
62   gboolean can_do_action;
63
64   if (individual == NULL)
65     return NULL;
66
67   contact = empathy_contact_dup_best_for_action (individual,
68       EMPATHY_ACTION_SEND_FILE);
69   if (contact == NULL)
70     return NULL;
71
72   can_do_action = empathy_contact_can_do_action (contact,
73       EMPATHY_ACTION_SEND_FILE);
74
75   if (!can_do_action)
76     {
77       /* If contact can't do FT we don't care about him */
78       g_object_unref (contact);
79       return NULL;
80     }
81
82   return contact;
83 }
84
85 static gboolean
86 filter_individual (EmpathyContactChooser *chooser,
87     FolksIndividual *individual,
88     gboolean is_online,
89     gboolean searching,
90     gpointer user_data)
91 {
92   EmpathyContact *contact;
93
94   if (!is_online)
95     return FALSE;
96
97   contact = dup_contact_from_individual (individual);
98   if (contact == NULL)
99     return FALSE;
100
101   g_object_unref (contact);
102   return TRUE;
103 }
104
105 static GtkWidget *
106 get_contacts_widget (NstPlugin *plugin)
107 {
108   GtkWidget *chooser;
109
110   chooser = empathy_contact_chooser_new ();
111   empathy_contact_chooser_set_filter_func (EMPATHY_CONTACT_CHOOSER (chooser),
112       filter_individual, plugin);
113
114   empathy_contact_chooser_show_search_entry (EMPATHY_CONTACT_CHOOSER (chooser),
115       FALSE);
116
117   /* Make sure to display some contacts */
118   gtk_widget_set_size_request (chooser, -1, 300);
119   return chooser;
120 }
121
122 static EmpathyContact *
123 get_selected_contact (GtkWidget *widget)
124 {
125   FolksIndividual *individual;
126   EmpathyContact *contact;
127
128   individual = empathy_contact_chooser_dup_selected (
129       EMPATHY_CONTACT_CHOOSER (widget));
130   if (individual == NULL)
131     return NULL;
132
133   contact = dup_contact_from_individual (individual);
134
135   g_object_unref (individual);
136   return contact;
137 }
138
139 static gboolean
140 validate_destination (NstPlugin *plugin,
141                       GtkWidget *contact_widget,
142                       gchar **error)
143 {
144   EmpathyContact *contact = NULL;
145   gboolean ret = TRUE;
146
147   contact = get_selected_contact (contact_widget);
148
149   if (contact == NULL)
150     return FALSE;
151
152   g_object_unref (contact);
153
154   return TRUE;
155 }
156
157 static void
158 quit (void)
159 {
160   if (--transfers > 0)
161     return;
162
163   destroy (NULL);
164   gtk_main_quit ();
165 }
166
167 static void
168 transfer_done_cb (EmpathyFTHandler *handler,
169                   EmpathyTpFile *tp_file,
170                   NstPlugin *plugin)
171 {
172   quit ();
173 }
174
175 static void
176 transfer_error_cb (EmpathyFTHandler *handler,
177                    GError *error,
178                    NstPlugin *plugin)
179 {
180   quit ();
181 }
182
183 static void
184 error_dialog_cb (GtkDialog *dialog,
185                  gint arg,
186                  gpointer user_data)
187 {
188   gtk_widget_destroy (GTK_WIDGET (dialog));
189   quit ();
190 }
191
192 static void
193 handler_ready_cb (EmpathyFTFactory *fact,
194                   EmpathyFTHandler *handler,
195                   GError *error,
196                   NstPlugin *plugin)
197 {
198   if (error != NULL)
199     {
200       GtkWidget *dialog;
201       dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR,
202           GTK_BUTTONS_CLOSE, "%s",
203           error->message ? error->message : _("No error message"));
204
205       g_signal_connect (dialog, "response", G_CALLBACK (error_dialog_cb), NULL);
206       gtk_widget_show (dialog);
207     }
208   else
209     {
210       g_signal_connect (handler, "transfer-done",
211           G_CALLBACK (transfer_done_cb), plugin);
212       g_signal_connect (handler, "transfer-error",
213           G_CALLBACK (transfer_error_cb), plugin);
214
215       empathy_ft_handler_start_transfer (handler);
216     }
217 }
218
219 static gboolean
220 send_files (NstPlugin *plugin,
221             GtkWidget *contact_widget,
222             GList *file_list)
223 {
224   EmpathyContact *contact;
225   GList *l;
226
227   contact = get_selected_contact (contact_widget);
228
229   if (contact == NULL)
230     return FALSE;
231
232   factory = empathy_ft_factory_dup_singleton ();
233
234   g_signal_connect (factory, "new-ft-handler",
235       G_CALLBACK (handler_ready_cb), plugin);
236
237   for (l = file_list; l; l = l->next)
238     {
239       gchar *path = l->data;
240       GFile *file;
241
242       file = g_file_new_for_uri (path);
243
244       ++transfers;
245
246       empathy_ft_factory_new_transfer_outgoing (factory,
247           contact, file, empathy_get_current_action_time ());
248
249       g_object_unref (file);
250     }
251
252   g_object_unref (contact);
253
254   if (transfers == 0)
255     {
256       destroy (NULL);
257       return TRUE;
258     }
259
260   return FALSE;
261 }
262
263 static gboolean
264 destroy (NstPlugin *plugin)
265 {
266   if (factory)
267     g_object_unref (factory);
268
269   return TRUE;
270 }
271
272 static
273 NstPluginInfo plugin_info = {
274   "im",
275   "empathy",
276   N_("Instant Message (Empathy)"),
277   GETTEXT_PACKAGE,
278   NAUTILUS_CAPS_NONE,
279   init,
280   get_contacts_widget,
281   validate_destination,
282   send_files,
283   destroy
284 };
285
286 NST_INIT_PLUGIN (plugin_info)
287