]> git.0d.be Git - empathy.git/blob - libempathy/empathy-ft-factory.c
Rethink a bit the logic for an incoming transfer.
[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 G_DEFINE_TYPE (EmpathyFTFactory, empathy_ft_factory, G_TYPE_OBJECT);
32
33 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyFTFactory)
34
35 enum {
36   NEW_FT_HANDLER,
37   NEW_INCOMING_TRANSFER,
38   LAST_SIGNAL
39 };
40
41 static EmpathyFTFactory *factory_singleton = NULL;
42 static guint signals[LAST_SIGNAL] = { 0 };
43
44 static GObject *
45 do_constructor (GType type,
46                 guint n_props,
47                 GObjectConstructParam *props)
48 {
49         GObject *retval;
50
51         if (factory_singleton) {
52                 retval = g_object_ref (factory_singleton);
53         } else {
54                 retval = G_OBJECT_CLASS (empathy_ft_factory_parent_class)->constructor
55                         (type, n_props, props);
56
57                 factory_singleton = EMPATHY_FT_FACTORY (retval);
58                 g_object_add_weak_pointer (retval, (gpointer *) &factory_singleton);
59         }
60
61         return retval;
62 }
63
64 static void
65 empathy_ft_factory_class_init (EmpathyFTFactoryClass *klass)
66 {
67   GObjectClass *object_class = G_OBJECT_CLASS (klass);
68
69   object_class->constructor = do_constructor;
70
71   signals[NEW_FT_HANDLER] =
72     g_signal_new ("new-ft-handler",
73       G_TYPE_FROM_CLASS (klass),
74       G_SIGNAL_RUN_LAST, 0,
75       NULL, NULL,
76       _empathy_marshal_VOID__OBJECT_BOOLEAN,
77       G_TYPE_NONE,
78       2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_BOOLEAN);
79
80   signals[NEW_INCOMING_TRANSFER] =
81     g_signal_new ("new-incoming-transfer",
82       G_TYPE_FROM_CLASS (klass),
83       G_SIGNAL_RUN_LAST, 0,
84       NULL, NULL,
85       g_cclosure_marshal_VOID__OBJECT,
86       G_TYPE_NONE, 1, EMPATHY_TYPE_FT_HANDLER);
87 }
88
89 static void
90 empathy_ft_factory_init (EmpathyFTFactory *self)
91 {
92   /* do nothing */
93 }
94
95 static void
96 ft_handler_outgoing_ready_cb (EmpathyFTHandler *handler,
97                               GError *error,
98                               gpointer user_data)
99 {
100   EmpathyFTFactory *factory = user_data;
101
102   if (error != NULL)
103     {
104       /* TODO: error handling */
105       return;
106     }
107
108   g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, TRUE);
109 }
110
111 static void
112 ft_handler_incoming_ready_cb (EmpathyFTHandler *handler,
113                               GError *error,
114                               gpointer user_data)
115 {
116   EmpathyFTFactory *factory = user_data;
117
118   if (error != NULL)
119     {
120       /* TODO: error handling */
121       return;
122     }
123
124   g_signal_emit (factory, signals[NEW_INCOMING_TRANSFER], 0, handler);
125 }
126
127 /* public methods */
128
129 EmpathyFTFactory*
130 empathy_ft_factory_dup_singleton (void)
131 {
132   return g_object_new (EMPATHY_TYPE_FT_FACTORY, NULL);
133 }
134
135 void
136 empathy_ft_factory_new_transfer_outgoing (EmpathyFTFactory *factory,
137                                           EmpathyContact *contact,
138                                           GFile *source)
139 {
140   g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
141   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
142   g_return_if_fail (G_IS_FILE (source));
143
144   empathy_ft_handler_new_outgoing (contact, source,
145       ft_handler_outgoing_ready_cb, factory);
146 }
147
148 void
149 empathy_ft_factory_claim_channel (EmpathyFTFactory *factory,
150                                   EmpathyDispatchOperation *operation)
151 {
152   EmpathyTpFile *tp_file;
153
154   g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
155   g_return_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation));
156
157   /* own a reference to the EmpathyTpFile */
158   tp_file = EMPATHY_TP_FILE
159       ((empathy_dispatch_operation_get_channel_wrapper (operation)));
160
161   empathy_ft_handler_new_incoming (tp_file, ft_handler_incoming_ready_cb,
162       factory);
163
164   empathy_dispatch_operation_claim (operation);
165 }
166
167 void
168 empathy_ft_factory_set_destination_for_incoming_handler
169                                                  (EmpathyFTFactory *factory,
170                                                   EmpathyFTHandler *handler,
171                                                   GFile *destination)
172 {
173   g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
174   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
175   g_return_if_fail (G_IS_FILE (destination));
176
177   empathy_ft_handler_incoming_set_destination (handler, destination);
178
179   g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, FALSE);
180 }