]> git.0d.be Git - empathy.git/blob - nautilus-sendto-plugin/empathy-nautilus-sendto.c
Clean up #include directives in source files
[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 (GtkWidget *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   egg_list_box_set_filter_func (EGG_LIST_BOX (roster_view), filter_individual,
125       roster_view, NULL);
126   egg_list_box_add_to_scrolled (EGG_LIST_BOX (roster_view),
127       GTK_SCROLLED_WINDOW (scrolled));
128
129   gtk_box_pack_start (GTK_BOX (box), scrolled, TRUE, TRUE, 0);
130
131   gtk_widget_set_size_request (box, -1, 300);
132   gtk_widget_show (roster_view);
133   gtk_widget_show (scrolled);
134   gtk_widget_show (box);
135
136   g_object_set_data (G_OBJECT (box), BOX_DATA_KEY, roster_view);
137
138   return box;
139 }
140
141 static EmpathyContact *
142 get_selected_contact (GtkWidget *widget)
143 {
144   EmpathyRosterView *roster_view;
145   FolksIndividual *individual;
146   EmpathyContact *contact;
147
148   roster_view = g_object_get_data (G_OBJECT (widget), BOX_DATA_KEY);
149   individual = empathy_roster_view_get_selected_individual (roster_view);
150
151   if (individual == NULL)
152     return NULL;
153
154   contact = dup_contact_from_individual (individual);
155
156   g_object_unref (individual);
157   return contact;
158 }
159
160 static gboolean
161 validate_destination (NstPlugin *plugin,
162                       GtkWidget *contact_widget,
163                       gchar **error)
164 {
165   EmpathyContact *contact = NULL;
166
167   contact = get_selected_contact (contact_widget);
168
169   if (contact == NULL)
170     return FALSE;
171
172   g_object_unref (contact);
173
174   return TRUE;
175 }
176
177 static void
178 quit (void)
179 {
180   if (--transfers > 0)
181     return;
182
183   destroy (NULL);
184   gtk_main_quit ();
185 }
186
187 static void
188 transfer_done_cb (EmpathyFTHandler *handler,
189                   TpFileTransferChannel *channel,
190                   NstPlugin *plugin)
191 {
192   quit ();
193 }
194
195 static void
196 transfer_error_cb (EmpathyFTHandler *handler,
197                    GError *error,
198                    NstPlugin *plugin)
199 {
200   quit ();
201 }
202
203 static void
204 error_dialog_cb (GtkDialog *dialog,
205                  gint arg,
206                  gpointer user_data)
207 {
208   gtk_widget_destroy (GTK_WIDGET (dialog));
209   quit ();
210 }
211
212 static void
213 handler_ready_cb (EmpathyFTFactory *fact,
214                   EmpathyFTHandler *handler,
215                   GError *error,
216                   NstPlugin *plugin)
217 {
218   if (error != NULL)
219     {
220       GtkWidget *dialog;
221       dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR,
222           GTK_BUTTONS_CLOSE, "%s",
223           error->message ? error->message : _("No error message"));
224
225       g_signal_connect (dialog, "response", G_CALLBACK (error_dialog_cb), NULL);
226       gtk_widget_show (dialog);
227     }
228   else
229     {
230       g_signal_connect (handler, "transfer-done",
231           G_CALLBACK (transfer_done_cb), plugin);
232       g_signal_connect (handler, "transfer-error",
233           G_CALLBACK (transfer_error_cb), plugin);
234
235       empathy_ft_handler_start_transfer (handler);
236     }
237 }
238
239 static gboolean
240 send_files (NstPlugin *plugin,
241             GtkWidget *contact_widget,
242             GList *file_list)
243 {
244   EmpathyContact *contact;
245   GList *l;
246
247   contact = get_selected_contact (contact_widget);
248
249   if (contact == NULL)
250     return FALSE;
251
252   factory = empathy_ft_factory_dup_singleton ();
253
254   g_signal_connect (factory, "new-ft-handler",
255       G_CALLBACK (handler_ready_cb), plugin);
256
257   for (l = file_list; l; l = l->next)
258     {
259       gchar *path = l->data;
260       GFile *file;
261
262       file = g_file_new_for_uri (path);
263
264       ++transfers;
265
266       empathy_ft_factory_new_transfer_outgoing (factory,
267           contact, file, empathy_get_current_action_time ());
268
269       g_object_unref (file);
270     }
271
272   g_object_unref (contact);
273
274   if (transfers == 0)
275     {
276       destroy (NULL);
277       return TRUE;
278     }
279
280   return FALSE;
281 }
282
283 static gboolean
284 destroy (NstPlugin *plugin)
285 {
286   if (factory)
287     g_object_unref (factory);
288
289   return TRUE;
290 }
291
292 static
293 NstPluginInfo plugin_info = {
294   "empathy",
295   "empathy",
296   N_("Instant Message (Empathy)"),
297   GETTEXT_PACKAGE,
298   NAUTILUS_CAPS_NONE,
299   init,
300   get_contacts_widget,
301   validate_destination,
302   send_files,
303   destroy
304 };
305
306 NST_INIT_PLUGIN (plugin_info)
307