2 * Copyright (C) 2008, 2009 Collabora Ltd.
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.
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.
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.
19 * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20 * Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
26 #include <glib/gi18n-lib.h>
30 #include <telepathy-glib/telepathy-glib.h>
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"
37 #include "libempathy-gtk/empathy-roster-model.h"
38 #include "libempathy-gtk/empathy-roster-model-manager.h"
39 #include "libempathy-gtk/empathy-contact-chooser.h"
40 #include "libempathy-gtk/empathy-ui-utils.h"
41 #include "libempathy-gtk/empathy-roster-view.h"
42 #include "libempathy-gtk/empathy-roster-contact.h"
45 #include "nautilus-sendto-plugin.h"
47 static EmpathyFTFactory *factory = NULL;
48 static guint transfers = 0;
50 #define BOX_DATA_KEY "roster_view"
52 static gboolean destroy (NstPlugin *plugin);
55 init (NstPlugin *plugin)
57 g_print ("Init %s plugin\n", plugin->info->id);
64 static EmpathyContact *
65 dup_contact_from_individual (FolksIndividual *individual)
67 EmpathyContact *contact;
68 gboolean can_do_action;
70 if (individual == NULL)
73 contact = empathy_contact_dup_best_for_action (individual,
74 EMPATHY_ACTION_SEND_FILE);
78 can_do_action = empathy_contact_can_do_action (contact,
79 EMPATHY_ACTION_SEND_FILE);
83 /* If contact can't do FT we don't care about him */
84 g_object_unref (contact);
92 filter_individual (GtkWidget *child,
95 FolksIndividual *individual;
96 EmpathyContact *contact;
98 if (!EMPATHY_IS_ROSTER_CONTACT (child))
101 individual = empathy_roster_contact_get_individual (EMPATHY_ROSTER_CONTACT (child));
103 contact = dup_contact_from_individual (individual);
107 g_object_unref (contact);
112 get_contacts_widget (NstPlugin *plugin)
114 GtkWidget *roster_view, *box, *scrolled;
115 EmpathyIndividualManager *mgr;
116 EmpathyRosterModel *model;
118 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
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);
124 g_object_unref (model);
126 scrolled = gtk_scrolled_window_new (NULL, NULL);
128 g_object_unref (mgr);
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,
134 egg_list_box_add_to_scrolled (EGG_LIST_BOX (roster_view),
135 GTK_SCROLLED_WINDOW (scrolled));
137 gtk_box_pack_start (GTK_BOX (box), scrolled, TRUE, TRUE, 0);
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);
144 g_object_set_data (G_OBJECT (box), BOX_DATA_KEY, roster_view);
149 static EmpathyContact *
150 get_selected_contact (GtkWidget *widget)
152 EmpathyRosterView *roster_view;
153 FolksIndividual *individual;
154 EmpathyContact *contact;
156 roster_view = g_object_get_data (G_OBJECT (widget), BOX_DATA_KEY);
157 individual = empathy_roster_view_get_selected_individual (roster_view);
159 if (individual == NULL)
162 contact = dup_contact_from_individual (individual);
164 g_object_unref (individual);
169 validate_destination (NstPlugin *plugin,
170 GtkWidget *contact_widget,
173 EmpathyContact *contact = NULL;
175 contact = get_selected_contact (contact_widget);
180 g_object_unref (contact);
196 transfer_done_cb (EmpathyFTHandler *handler,
197 TpFileTransferChannel *channel,
204 transfer_error_cb (EmpathyFTHandler *handler,
212 error_dialog_cb (GtkDialog *dialog,
216 gtk_widget_destroy (GTK_WIDGET (dialog));
221 handler_ready_cb (EmpathyFTFactory *fact,
222 EmpathyFTHandler *handler,
229 dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR,
230 GTK_BUTTONS_CLOSE, "%s",
231 error->message ? error->message : _("No error message"));
233 g_signal_connect (dialog, "response", G_CALLBACK (error_dialog_cb), NULL);
234 gtk_widget_show (dialog);
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);
243 empathy_ft_handler_start_transfer (handler);
248 send_files (NstPlugin *plugin,
249 GtkWidget *contact_widget,
252 EmpathyContact *contact;
255 contact = get_selected_contact (contact_widget);
260 factory = empathy_ft_factory_dup_singleton ();
262 g_signal_connect (factory, "new-ft-handler",
263 G_CALLBACK (handler_ready_cb), plugin);
265 for (l = file_list; l; l = l->next)
267 gchar *path = l->data;
270 file = g_file_new_for_uri (path);
274 empathy_ft_factory_new_transfer_outgoing (factory,
275 contact, file, empathy_get_current_action_time ());
277 g_object_unref (file);
280 g_object_unref (contact);
292 destroy (NstPlugin *plugin)
295 g_object_unref (factory);
301 NstPluginInfo plugin_info = {
304 N_("Instant Message (Empathy)"),
309 validate_destination,
314 NST_INIT_PLUGIN (plugin_info)