]> git.0d.be Git - empathy.git/blob - libempathy/empathy-ft-factory.c
add myself to AUTHORS
[empathy.git] / libempathy / empathy-ft-factory.c
1 /*
2  * empathy-ft-factory.c - Source for EmpathyFTFactory
3  * Copyright (C) 2009 Collabora Ltd.
4  *
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.
9  *
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.
14  *
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
18  *
19  * Author: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
20  */
21
22 /* empathy-ft-factory.c */
23
24 #include <glib.h>
25
26 #include "empathy-ft-factory.h"
27 #include "empathy-ft-handler.h"
28 #include "empathy-marshal.h"
29 #include "empathy-utils.h"
30
31 /**
32  * SECTION:empathy-ft-factory
33  * @title:EmpathyFTFactory
34  * @short_description: creates #EmpathyFTHandler objects
35  * @include: libempathy/empathy-ft-factory.h
36  *
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().
46  */
47
48 G_DEFINE_TYPE (EmpathyFTFactory, empathy_ft_factory, G_TYPE_OBJECT);
49
50 enum {
51   NEW_FT_HANDLER,
52   NEW_INCOMING_TRANSFER,
53   LAST_SIGNAL
54 };
55
56 static EmpathyFTFactory *factory_singleton = NULL;
57 static guint signals[LAST_SIGNAL] = { 0 };
58
59 static GObject *
60 do_constructor (GType type,
61     guint n_props,
62     GObjectConstructParam *props)
63 {
64         GObject *retval;
65
66         if (factory_singleton != NULL) {
67                 retval = g_object_ref (factory_singleton);
68         } else {
69                 retval = G_OBJECT_CLASS (empathy_ft_factory_parent_class)->constructor
70                         (type, n_props, props);
71
72                 factory_singleton = EMPATHY_FT_FACTORY (retval);
73                 g_object_add_weak_pointer (retval, (gpointer *) &factory_singleton);
74         }
75
76         return retval;
77 }
78
79 static void
80 empathy_ft_factory_class_init (EmpathyFTFactoryClass *klass)
81 {
82   GObjectClass *object_class = G_OBJECT_CLASS (klass);
83
84   object_class->constructor = do_constructor;
85
86   /**
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
91    *
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
95    * any transfer.
96    */
97   signals[NEW_FT_HANDLER] =
98     g_signal_new ("new-ft-handler",
99       G_TYPE_FROM_CLASS (klass),
100       G_SIGNAL_RUN_LAST, 0,
101       NULL, NULL,
102       _empathy_marshal_VOID__OBJECT_POINTER,
103       G_TYPE_NONE, 2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_POINTER);
104
105   /**
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
110    *
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
115    * have a #GFile.
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
118    * any transfer.
119    */
120   signals[NEW_INCOMING_TRANSFER] =
121     g_signal_new ("new-incoming-transfer",
122       G_TYPE_FROM_CLASS (klass),
123       G_SIGNAL_RUN_LAST, 0,
124       NULL, NULL,
125       _empathy_marshal_VOID__OBJECT_POINTER,
126       G_TYPE_NONE, 2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_POINTER);
127 }
128
129 static void
130 empathy_ft_factory_init (EmpathyFTFactory *self)
131 {
132   /* do nothing */
133 }
134
135 static void
136 ft_handler_outgoing_ready_cb (EmpathyFTHandler *handler,
137     GError *error,
138     gpointer user_data)
139 {
140   EmpathyFTFactory *factory = user_data;
141
142   g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, error);
143 }
144
145 static void
146 ft_handler_incoming_ready_cb (EmpathyFTHandler *handler,
147     GError *error,
148     gpointer user_data)
149 {
150   EmpathyFTFactory *factory = user_data;
151
152   g_signal_emit (factory, signals[NEW_INCOMING_TRANSFER], 0, handler, error);
153 }
154
155 /* public methods */
156
157 /**
158  * empathy_ft_factory_dup_singleton:
159  *
160  * Gives the caller a reference to the #EmpathyFTFactory singleton,
161  * (creating it if necessary).
162  *
163  * Return value: an #EmpathyFTFactory object
164  */
165 EmpathyFTFactory *
166 empathy_ft_factory_dup_singleton (void)
167 {
168   return g_object_new (EMPATHY_TYPE_FT_FACTORY, NULL);
169 }
170
171 /**
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
176  *
177  * Trigger the creation of an #EmpathyFTHandler object to send @source to
178  * the specified @contact.
179  */
180 void
181 empathy_ft_factory_new_transfer_outgoing (EmpathyFTFactory *factory,
182     EmpathyContact *contact,
183     GFile *source)
184 {
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));
188
189   empathy_ft_handler_new_outgoing (contact, source,
190       ft_handler_outgoing_ready_cb, factory);
191 }
192
193 /**
194  * empathy_ft_factory_claim_channel:
195  * @factory: an #EmpathyFTFactory
196  * @operation: the #EmpathyDispatchOperation wrapping the channel
197  *
198  * Let the @factory claim the channel, starting the creation of a new
199  * incoming #EmpathyFTHandler.
200  */
201 void
202 empathy_ft_factory_claim_channel (EmpathyFTFactory *factory,
203     EmpathyDispatchOperation *operation)
204 {
205   EmpathyTpFile *tp_file;
206
207   g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
208   g_return_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation));
209
210   /* own a reference to the EmpathyTpFile */
211   tp_file = EMPATHY_TP_FILE
212       ((empathy_dispatch_operation_get_channel_wrapper (operation)));
213
214   empathy_ft_handler_new_incoming (tp_file, ft_handler_incoming_ready_cb,
215       factory);
216
217   empathy_dispatch_operation_claim (operation);
218 }
219
220 /**
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
225  *
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.
228  */
229 void
230 empathy_ft_factory_set_destination_for_incoming_handler (
231     EmpathyFTFactory *factory,
232     EmpathyFTHandler *handler,
233     GFile *destination)
234 {
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));
238
239   empathy_ft_handler_incoming_set_destination (handler, destination);
240
241   g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, NULL);
242 }