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