]> git.0d.be Git - empathy.git/blob - nautilus-sendto-plugin/empathy-nautilus-sendto.c
Update Simplified Chinese help translation.
[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-contact-manager.h>
35 #include <libempathy/empathy-ft-factory.h>
36 #include <libempathy/empathy-ft-handler.h>
37 #include <libempathy/empathy-tp-file.h>
38
39 #include <libempathy-gtk/empathy-contact-selector.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
41
42 #include "nautilus-sendto-plugin.h"
43
44 static EmpathyFTFactory *factory = NULL;
45 static guint transfers = 0;
46
47 static gboolean destroy (NstPlugin *plugin);
48
49 static gboolean
50 init (NstPlugin *plugin)
51 {
52   g_print ("Init %s plugin\n", plugin->info->id);
53
54   empathy_gtk_init ();
55
56   return TRUE;
57 }
58
59 static GtkWidget *
60 get_contacts_widget (NstPlugin *plugin)
61 {
62   EmpathyContactManager *manager;
63   GtkWidget *selector;
64
65   manager = empathy_contact_manager_dup_singleton ();
66   selector = empathy_contact_selector_new (EMPATHY_CONTACT_LIST (manager));
67
68   empathy_contact_selector_set_visible (EMPATHY_CONTACT_SELECTOR (selector),
69       (EmpathyContactSelectorFilterFunc) empathy_contact_can_send_files, NULL);
70
71   g_object_unref (manager);
72
73   return selector;
74 }
75
76 static EmpathyContact *
77 get_selected_contact (GtkWidget *contact_widget)
78 {
79   EmpathyContact *contact;
80   GtkTreeModel *model;
81   GtkTreeIter iter;
82
83   if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (contact_widget), &iter))
84     return NULL;
85
86   model = gtk_combo_box_get_model (GTK_COMBO_BOX (contact_widget));
87   gtk_tree_model_get (model, &iter,
88       EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
89
90   return contact;
91 }
92
93 static gboolean
94 validate_destination (NstPlugin *plugin,
95                       GtkWidget *contact_widget,
96                       gchar **error)
97 {
98   EmpathyContact *contact = NULL;
99   gboolean ret = TRUE;
100
101   contact = get_selected_contact (contact_widget);
102
103   if (!contact)
104     return FALSE;
105
106   if (!empathy_contact_can_send_files (contact))
107     {
108       *error = g_strdup (_("The selected contact cannot receive files."));
109       ret = FALSE;
110     }
111
112   if (ret && !empathy_contact_is_online (contact))
113     {
114       *error = g_strdup (_("The selected contact is offline."));
115       ret = FALSE;
116     }
117
118   g_object_unref (contact);
119
120   return ret;
121 }
122
123 static void
124 quit (void)
125 {
126   if (--transfers > 0)
127     return;
128
129   destroy (NULL);
130   gtk_main_quit ();
131 }
132
133 static void
134 transfer_done_cb (EmpathyFTHandler *handler,
135                   EmpathyTpFile *tp_file,
136                   NstPlugin *plugin)
137 {
138   quit ();
139 }
140
141 static void
142 transfer_error_cb (EmpathyFTHandler *handler,
143                    GError *error,
144                    NstPlugin *plugin)
145 {
146   quit ();
147 }
148
149 static void
150 error_dialog_cb (GtkDialog *dialog,
151                  gint arg,
152                  gpointer user_data)
153 {
154   gtk_widget_destroy (GTK_WIDGET (dialog));
155   quit ();
156 }
157
158 static void
159 handler_ready_cb (EmpathyFTFactory *fact,
160                   EmpathyFTHandler *handler,
161                   GError *error,
162                   NstPlugin *plugin)
163 {
164   if (error != NULL)
165     {
166       GtkWidget *dialog;
167       dialog = gtk_message_dialog_new (NULL, 0, GTK_MESSAGE_ERROR,
168           GTK_BUTTONS_CLOSE, "%s",
169           error->message ? error->message : _("No error message"));
170
171       g_signal_connect (dialog, "response", G_CALLBACK (error_dialog_cb), NULL);
172       gtk_widget_show (dialog);
173     }
174   else
175     {
176       g_signal_connect (handler, "transfer-done",
177           G_CALLBACK (transfer_done_cb), plugin);
178       g_signal_connect (handler, "transfer-error",
179           G_CALLBACK (transfer_error_cb), plugin);
180
181       empathy_ft_handler_start_transfer (handler);
182     }
183 }
184
185 static gboolean
186 send_files (NstPlugin *plugin,
187             GtkWidget *contact_widget,
188             GList *file_list)
189 {
190   EmpathyContact *contact;
191   GList *l;
192
193   contact = get_selected_contact (contact_widget);
194
195   if (!contact)
196     return FALSE;
197
198   factory = empathy_ft_factory_dup_singleton ();
199
200   g_signal_connect (factory, "new-ft-handler",
201       G_CALLBACK (handler_ready_cb), plugin);
202
203   for (l = file_list; l; l = l->next)
204     {
205       gchar *path = l->data;
206       GFile *file;
207
208       file = g_file_new_for_uri (path);
209
210       ++transfers;
211
212       empathy_ft_factory_new_transfer_outgoing (factory,
213           contact, file);
214
215       g_object_unref (file);
216     }
217
218   g_object_unref (contact);
219
220   if (transfers == 0)
221     {
222       destroy (NULL);
223       return TRUE;
224     }
225
226   return FALSE;
227 }
228
229 static gboolean
230 destroy (NstPlugin *plugin)
231 {
232   if (factory)
233     g_object_unref (factory);
234
235   return TRUE;
236 }
237
238 static
239 NstPluginInfo plugin_info = {
240   "im",
241   "empathy",
242   N_("Instant Message (Empathy)"),
243   GETTEXT_PACKAGE,
244   NAUTILUS_CAPS_NONE,
245   init,
246   get_contacts_widget,
247   validate_destination,
248   send_files,
249   destroy
250 };
251
252 NST_INIT_PLUGIN (plugin_info)
253