1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*
* empathy-ft-factory.c - Source for EmpathyFTFactory
* Copyright (C) 2009 Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
*/
/* empathy-ft-factory.c */
#include <glib.h>
#include "empathy-ft-factory.h"
#include "empathy-ft-handler.h"
#include "empathy-marshal.h"
#include "empathy-utils.h"
/**
* SECTION:empathy-ft-factory
* @title:EmpathyFTFactory
* @short_description: creates #EmpathyFTHandler objects
* @include: libempathy/empathy-ft-factory.h
*
* #EmpathyFTFactory takes care of the creation of the #EmpathyFTHandler
* objects used for file transfer. As the creation of the handlers is
* async, a client will have to connect to the ::new-ft-handler signal
* to receive the handler.
* In case of an incoming file transfer, the handler will need the destination
* file before being useful; as this is usually decided by the user (e.g. with
* a file selector), a ::new-incoming-transfer is emitted by the factory when
* a destination file is needed, which can be set later with
* empathy_ft_factory_set_destination_for_incoming_handler().
*/
G_DEFINE_TYPE (EmpathyFTFactory, empathy_ft_factory, G_TYPE_OBJECT);
enum {
NEW_FT_HANDLER,
NEW_INCOMING_TRANSFER,
LAST_SIGNAL
};
static EmpathyFTFactory *factory_singleton = NULL;
static guint signals[LAST_SIGNAL] = { 0 };
static GObject *
do_constructor (GType type,
guint n_props,
GObjectConstructParam *props)
{
GObject *retval;
if (factory_singleton != NULL) {
retval = g_object_ref (factory_singleton);
} else {
retval = G_OBJECT_CLASS (empathy_ft_factory_parent_class)->constructor
(type, n_props, props);
factory_singleton = EMPATHY_FT_FACTORY (retval);
g_object_add_weak_pointer (retval, (gpointer *) &factory_singleton);
}
return retval;
}
static void
empathy_ft_factory_class_init (EmpathyFTFactoryClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructor = do_constructor;
/**
* EmpathyFTFactory::new-ft-handler
* @factory: the object which received the signal
* @handler: the handler made available by the factory
* @error: a #GError or %NULL
*
* The signal is emitted when a new #EmpathyFTHandler is available.
* Note that @handler is never %NULL even if @error is set, as you might want
* to display the error in an UI; in that case, the handler won't support
* any transfer.
*/
signals[NEW_FT_HANDLER] =
g_signal_new ("new-ft-handler",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0,
NULL, NULL,
_empathy_marshal_VOID__OBJECT_POINTER,
G_TYPE_NONE, 2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_POINTER);
/**
* EmpathyFTFactory::new-incoming-transfer
* @factory: the object which received the signal
* @handler: the incoming handler being constructed
* @error: a #GError or %NULL
*
* The signal is emitted when a new incoming #EmpathyFTHandler is being
* constructed, and needs a destination #GFile to be useful.
* Clients that connect to this signal will have to call
* empathy_ft_factory_set_destination_for_incoming_handler() when they
* have a #GFile.
* Note that @handler is never %NULL even if @error is set, as you might want
* to display the error in an UI; in that case, the handler won't support
* any transfer.
*/
signals[NEW_INCOMING_TRANSFER] =
g_signal_new ("new-incoming-transfer",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0,
NULL, NULL,
_empathy_marshal_VOID__OBJECT_POINTER,
G_TYPE_NONE, 2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_POINTER);
}
static void
empathy_ft_factory_init (EmpathyFTFactory *self)
{
/* do nothing */
}
static void
ft_handler_outgoing_ready_cb (EmpathyFTHandler *handler,
GError *error,
gpointer user_data)
{
EmpathyFTFactory *factory = user_data;
g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, error);
}
static void
ft_handler_incoming_ready_cb (EmpathyFTHandler *handler,
GError *error,
gpointer user_data)
{
EmpathyFTFactory *factory = user_data;
g_signal_emit (factory, signals[NEW_INCOMING_TRANSFER], 0, handler, error);
}
/* public methods */
/**
* empathy_ft_factory_dup_singleton:
*
* Gives the caller a reference to the #EmpathyFTFactory singleton,
* (creating it if necessary).
*
* Return value: an #EmpathyFTFactory object
*/
EmpathyFTFactory *
empathy_ft_factory_dup_singleton (void)
{
return g_object_new (EMPATHY_TYPE_FT_FACTORY, NULL);
}
/**
* empathy_ft_factory_new_transfer_outgoing:
* @factory: an #EmpathyFTFactory
* @contact: the #EmpathyContact destination of the transfer
* @source: the #GFile to be transferred to @contact
*
* Trigger the creation of an #EmpathyFTHandler object to send @source to
* the specified @contact.
*/
void
empathy_ft_factory_new_transfer_outgoing (EmpathyFTFactory *factory,
EmpathyContact *contact,
GFile *source)
{
g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
g_return_if_fail (EMPATHY_IS_CONTACT (contact));
g_return_if_fail (G_IS_FILE (source));
empathy_ft_handler_new_outgoing (contact, source,
ft_handler_outgoing_ready_cb, factory);
}
/**
* empathy_ft_factory_claim_channel:
* @factory: an #EmpathyFTFactory
* @operation: the #EmpathyDispatchOperation wrapping the channel
*
* Let the @factory claim the channel, starting the creation of a new
* incoming #EmpathyFTHandler.
*/
void
empathy_ft_factory_claim_channel (EmpathyFTFactory *factory,
EmpathyDispatchOperation *operation)
{
EmpathyTpFile *tp_file;
g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
g_return_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation));
/* own a reference to the EmpathyTpFile */
tp_file = EMPATHY_TP_FILE
((empathy_dispatch_operation_get_channel_wrapper (operation)));
empathy_ft_handler_new_incoming (tp_file, ft_handler_incoming_ready_cb,
factory);
empathy_dispatch_operation_claim (operation);
}
/**
* empathy_ft_factory_set_destination_for_incoming_handler:
* @factory: an #EmpathyFTFactory
* @handler: the #EmpathyFTHandler to set the destination of
* @destination: the #GFile destination of the transfer
*
* Sets @destination as destination file for the transfer. After the call of
* this method, the ::new-ft-handler will be emitted for the incoming handler.
*/
void
empathy_ft_factory_set_destination_for_incoming_handler (
EmpathyFTFactory *factory,
EmpathyFTHandler *handler,
GFile *destination)
{
g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
g_return_if_fail (G_IS_FILE (destination));
empathy_ft_handler_incoming_set_destination (handler, destination);
g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, NULL);
}
|