2 * empathy-ft-factory.c - Source for EmpathyFTFactory
3 * Copyright (C) 2009 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Author: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
22 /* empathy-ft-factory.c */
26 #include "empathy-ft-factory.h"
27 #include "empathy-ft-handler.h"
28 #include "empathy-marshal.h"
29 #include "empathy-utils.h"
32 * SECTION:empathy-ft-factory
33 * @title:EmpathyFTFactory
34 * @short_description: creates #EmpathyFTHandler objects
35 * @include: libempathy/empathy-ft-factory.h
37 * #EmpathyFTFactory takes care of the creation of the #EmpathyFTHandler
38 * objects used for file transfer. As the creation of the handlers is
39 * async, a client will have to connect to the ::new-ft-handler signal
40 * to receive the handler.
41 * In case of an incoming file transfer, the handler will need the destination
42 * file before being useful; as this is usually decided by the user (e.g. with
43 * a file selector), a ::new-incoming-transfer is emitted by the factory when
44 * a destination file is needed, which can be set later with
45 * empathy_ft_factory_set_destination_for_incoming_handler().
48 G_DEFINE_TYPE (EmpathyFTFactory, empathy_ft_factory, G_TYPE_OBJECT);
52 NEW_INCOMING_TRANSFER,
56 static EmpathyFTFactory *factory_singleton = NULL;
57 static guint signals[LAST_SIGNAL] = { 0 };
60 do_constructor (GType type,
62 GObjectConstructParam *props)
66 if (factory_singleton != NULL) {
67 retval = g_object_ref (factory_singleton);
69 retval = G_OBJECT_CLASS (empathy_ft_factory_parent_class)->constructor
70 (type, n_props, props);
72 factory_singleton = EMPATHY_FT_FACTORY (retval);
73 g_object_add_weak_pointer (retval, (gpointer *) &factory_singleton);
80 empathy_ft_factory_class_init (EmpathyFTFactoryClass *klass)
82 GObjectClass *object_class = G_OBJECT_CLASS (klass);
84 object_class->constructor = do_constructor;
87 * EmpathyFTFactory::new-ft-handler
88 * @factory: the object which received the signal
89 * @handler: the handler made available by the factory
90 * @error: a #GError or %NULL
92 * The signal is emitted when a new #EmpathyFTHandler is available.
93 * Note that @handler is never %NULL even if @error is set, as you might want
94 * to display the error in an UI; in that case, the handler won't support
97 signals[NEW_FT_HANDLER] =
98 g_signal_new ("new-ft-handler",
99 G_TYPE_FROM_CLASS (klass),
100 G_SIGNAL_RUN_LAST, 0,
102 _empathy_marshal_VOID__OBJECT_POINTER,
103 G_TYPE_NONE, 2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_POINTER);
106 * EmpathyFTFactory::new-incoming-transfer
107 * @factory: the object which received the signal
108 * @handler: the incoming handler being constructed
109 * @error: a #GError or %NULL
111 * The signal is emitted when a new incoming #EmpathyFTHandler is being
112 * constructed, and needs a destination #GFile to be useful.
113 * Clients that connect to this signal will have to call
114 * empathy_ft_factory_set_destination_for_incoming_handler() when they
116 * Note that @handler is never %NULL even if @error is set, as you might want
117 * to display the error in an UI; in that case, the handler won't support
120 signals[NEW_INCOMING_TRANSFER] =
121 g_signal_new ("new-incoming-transfer",
122 G_TYPE_FROM_CLASS (klass),
123 G_SIGNAL_RUN_LAST, 0,
125 _empathy_marshal_VOID__OBJECT_POINTER,
126 G_TYPE_NONE, 2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_POINTER);
130 empathy_ft_factory_init (EmpathyFTFactory *self)
136 ft_handler_outgoing_ready_cb (EmpathyFTHandler *handler,
140 EmpathyFTFactory *factory = user_data;
142 g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, error);
146 ft_handler_incoming_ready_cb (EmpathyFTHandler *handler,
150 EmpathyFTFactory *factory = user_data;
152 g_signal_emit (factory, signals[NEW_INCOMING_TRANSFER], 0, handler, error);
158 * empathy_ft_factory_dup_singleton:
160 * Gives the caller a reference to the #EmpathyFTFactory singleton,
161 * (creating it if necessary).
163 * Return value: an #EmpathyFTFactory object
166 empathy_ft_factory_dup_singleton (void)
168 return g_object_new (EMPATHY_TYPE_FT_FACTORY, NULL);
172 * empathy_ft_factory_new_transfer_outgoing:
173 * @factory: an #EmpathyFTFactory
174 * @contact: the #EmpathyContact destination of the transfer
175 * @source: the #GFile to be transferred to @contact
177 * Trigger the creation of an #EmpathyFTHandler object to send @source to
178 * the specified @contact.
181 empathy_ft_factory_new_transfer_outgoing (EmpathyFTFactory *factory,
182 EmpathyContact *contact,
185 g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
186 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
187 g_return_if_fail (G_IS_FILE (source));
189 empathy_ft_handler_new_outgoing (contact, source,
190 ft_handler_outgoing_ready_cb, factory);
194 * empathy_ft_factory_claim_channel:
195 * @factory: an #EmpathyFTFactory
196 * @operation: the #EmpathyDispatchOperation wrapping the channel
198 * Let the @factory claim the channel, starting the creation of a new
199 * incoming #EmpathyFTHandler.
202 empathy_ft_factory_claim_channel (EmpathyFTFactory *factory,
203 EmpathyDispatchOperation *operation)
205 EmpathyTpFile *tp_file;
207 g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
208 g_return_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation));
210 /* own a reference to the EmpathyTpFile */
211 tp_file = EMPATHY_TP_FILE
212 ((empathy_dispatch_operation_get_channel_wrapper (operation)));
214 empathy_ft_handler_new_incoming (tp_file, ft_handler_incoming_ready_cb,
217 empathy_dispatch_operation_claim (operation);
221 * empathy_ft_factory_set_destination_for_incoming_handler:
222 * @factory: an #EmpathyFTFactory
223 * @handler: the #EmpathyFTHandler to set the destination of
224 * @destination: the #GFile destination of the transfer
226 * Sets @destination as destination file for the transfer. After the call of
227 * this method, the ::new-ft-handler will be emitted for the incoming handler.
230 empathy_ft_factory_set_destination_for_incoming_handler (
231 EmpathyFTFactory *factory,
232 EmpathyFTHandler *handler,
235 g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
236 g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
237 g_return_if_fail (G_IS_FILE (destination));
239 empathy_ft_handler_incoming_set_destination (handler, destination);
241 g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, NULL);