]> git.0d.be Git - empathy.git/blob - nautilus-sendto-plugin/empathy-nautilus-sendto.c
Use a flat namespace for internal includes
[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/telepathy-glib.h>
31
32 #include "empathy-contact.h"
33 #include "empathy-debug.h"
34 #include "empathy-ft-factory.h"
35 #include "empathy-ft-handler.h"
36
37 #include "empathy-roster-model.h"
38 #include "empathy-roster-model-manager.h"
39 #include "empathy-contact-chooser.h"
40 #include "empathy-ui-utils.h"
41 #include "empathy-roster-view.h"
42 #include "empathy-roster-contact.h"
43
44
45 #include "nautilus-sendto-plugin.h"
46
47 static EmpathyFTFactory *factory = NULL;
48 static guint transfers = 0;
49
50 #define BOX_DATA_KEY "roster_view"
51
52 static gboolean destroy (NstPlugin *plugin);
53
54 static gboolean
55 init (NstPlugin *plugin)
56 {
57   g_print ("Init %s plugin\n", plugin->info->id);
58
59   empathy_gtk_init ();
60
61   return TRUE;
62 }
63
64 static EmpathyContact *
65 dup_contact_from_individual (FolksIndividual *individual)
66 {
67   EmpathyContact *contact;
68   gboolean can_do_action;
69
70   if (individual == NULL)
71     return NULL;
72
73   contact = empathy_contact_dup_best_for_action (individual,
74       EMPATHY_ACTION_SEND_FILE);
75   if (contact == NULL)
76     return NULL;
77
78   can_do_action = empathy_contact_can_do_action (contact,
79       EMPATHY_ACTION_SEND_FILE);
80
81   if (!can_do_action)
82     {
83       /* If contact can't do FT we don't care about him */
84       g_object_unref (contact);
85       return NULL;
86     }
87
88   return contact;
89 }
90
91 static gboolean
92 filter_individual (GtkWidget *child,
93     gpointer user_data)
94 {
95   FolksIndividual *individual;
96   EmpathyContact *contact;
97
98   if (!EMPATHY_IS_ROSTER_CONTACT (child))
99     return FALSE;
100
101   individual = empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
102
103   contact = dup_contact_from_individual (individual);
104   if (contact == NULL)
105     return FALSE;
106
107   g_object_unref (contact);
108   return TRUE;
109 }
110
111 static GtkWidget *
112 get_contacts_widget (NstPlugin *plugin)
113 {
114   GtkWidget *roster_view, *box, *scrolled;
115   EmpathyIndividualManager *mgr;
116   EmpathyRosterModel *model;
117
118   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
119
120   mgr = empathy_individual_manager_dup_singleton ();
121   model = EMPATHY_ROSTER_MODEL (empathy_roster_model_manager_new (mgr));
122   roster_view = empathy_roster_view_new (model);
123
124   g_object_unref (model);
125
126   scrolled = gtk_scrolled_window_new (NULL, NULL);
127
128   g_object_unref (mgr);
129
130   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
131       GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
132   egg_list_box_set_filter_func (EGG_LIST_BOX (roster_view), filter_individual,
133       roster_view, NULL);
134   egg_list_box_add_to_scrolled (EGG_LIST_BOX (roster_view),
135       GTK_SCROLLED_WINDOW (scrolled));
136
137   gtk_box_pack_start (GTK_BOX (box), scrolled, TRUE, TRUE, 0);
138
139   gtk_widget_set_size_request (box, -1, 300);
140   gtk_widget_show (roster_view);
141   gtk_widget_show (scrolled);
142   gtk_widget_show (box);
143
144   g_object_set_data (G_OBJECT (box), BOX_DATA_KEY, roster_view);
145
146   return box;
147 }
148
149 static EmpathyContact *
150 get_selected_contact (GtkWidget *widget)
151 {
152   EmpathyRosterView *roster_view;
153   FolksIndividual *individual;
154   EmpathyContact *contact;
155
156   roster_view = g_object_get_data (G_OBJECT (widget), BOX_DATA_KEY);
157   individual = empathy_roster_view_get_selected_individual (roster_view);
158
159   if (individual == NULL)
160     return NULL;
161
162   contact = dup_contact_from_individual (individual);
163
164   g_object_unref (individual);
165   return contact;
166 }
167
168 static gboolean
169 validate_destination (NstPlugin *plugin,
170                       GtkWidget *contact_widget,
171                       gchar **error)
172 {
173   EmpathyContact *contact = NULL;
174
175   contact = get_selected_contact (contact_widget);
176
177   if (contact == NULL)
178     return FALSE;
179
180   g_object_unref (contact);
181
182   return TRUE;
183 }
184
185 static void
186 quit (void)
187 {
188   if (--transfers > 0)
189     return;
190
191   destroy (NULL);
192   gtk_main_quit ();
193 }
194
195 static void
196 transfer_done_cb (EmpathyFTHandler *handler,
197                   TpFileTransferChannel *channel,
198                   NstPlugin *plugin)
199 {
200   quit ();
201 }
202
203 static void
204 transfer_error_cb (EmpathyFTHandler *handler,
205                    GError *error,
206                    NstPlugin *plugin)
207 {
208   quit ();
209 }
210
211 static void
212 error_dialog_cb (GtkDialog *dialog,
213                  gint arg,
214                  gpointer user_data)
215 {
216   gtk_widget_destroy (GTK_WIDGET (dialog));
217   quit ();
218 }
219
220 static void
221 handler_ready_cb (EmpathyFTFactory *fact,
222                   EmpathyFTHandler *handler,
223                   GError *error,
224                   NstPlugin *plugin)
225 {
226   if (error != NULL)
227     {
228       GtkWidget *dialog;
229       dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR,
230           GTK_BUTTONS_CLOSE, "%s",
231           error->message ? error->message : _("No error message"));
232
233       g_signal_connect (dialog, "response", G_CALLBACK (error_dialog_cb), NULL);
234       gtk_widget_show (dialog);
235     }
236   else
237     {
238       g_signal_connect (handler, "transfer-done",
239           G_CALLBACK (transfer_done_cb), plugin);
240       g_signal_connect (handler, "transfer-error",
241           G_CALLBACK (transfer_error_cb), plugin);
242
243       empathy_ft_handler_start_transfer (handler);
244     }
245 }
246
247 static gboolean
248 send_files (NstPlugin *plugin,
249             GtkWidget *contact_widget,
250             GList *file_list)
251 {
252   EmpathyContact *contact;
253   GList *l;
254
255   contact = get_selected_contact (contact_widget);
256
257   if (contact == NULL)
258     return FALSE;
259
260   factory = empathy_ft_factory_dup_singleton ();
261
262   g_signal_connect (factory, "new-ft-handler",
263       G_CALLBACK (handler_ready_cb), plugin);
264
265   for (l = file_list; l; l = l->next)
266     {
267       gchar *path = l->data;
268       GFile *file;
269
270       file = g_file_new_for_uri (path);
271
272       ++transfers;
273
274       empathy_ft_factory_new_transfer_outgoing (factory,
275           contact, file, empathy_get_current_action_time ());
276
277       g_object_unref (file);
278     }
279
280   g_object_unref (contact);
281
282   if (transfers == 0)
283     {
284       destroy (NULL);
285       return TRUE;
286     }
287
288   return FALSE;
289 }
290
291 static gboolean
292 destroy (NstPlugin *plugin)
293 {
294   if (factory)
295     g_object_unref (factory);
296
297   return TRUE;
298 }
299
300 static
301 NstPluginInfo plugin_info = {
302   "empathy",
303   "empathy",
304   N_("Instant Message (Empathy)"),
305   GETTEXT_PACKAGE,
306   NAUTILUS_CAPS_NONE,
307   init,
308   get_contacts_widget,
309   validate_destination,
310   send_files,
311   destroy
312 };
313
314 NST_INIT_PLUGIN (plugin_info)
315