]> git.0d.be Git - empathy.git/blob - libempathy/empathy-ft-factory.c
97fe6022b7459101273af50d6cbc7601d23e9d38
[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_POINTER,
77       G_TYPE_NONE, 2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_POINTER);
78
79   signals[NEW_INCOMING_TRANSFER] =
80     g_signal_new ("new-incoming-transfer",
81       G_TYPE_FROM_CLASS (klass),
82       G_SIGNAL_RUN_LAST, 0,
83       NULL, NULL,
84       _empathy_marshal_VOID__OBJECT_POINTER,
85       G_TYPE_NONE, 2, EMPATHY_TYPE_FT_HANDLER, G_TYPE_POINTER);
86 }
87
88 static void
89 empathy_ft_factory_init (EmpathyFTFactory *self)
90 {
91   /* do nothing */
92 }
93
94 static void
95 ft_handler_outgoing_ready_cb (EmpathyFTHandler *handler,
96                               GError *error,
97                               gpointer user_data)
98 {
99   EmpathyFTFactory *factory = user_data;
100
101   g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, error);
102 }
103
104 static void
105 ft_handler_incoming_ready_cb (EmpathyFTHandler *handler,
106                               GError *error,
107                               gpointer user_data)
108 {
109   EmpathyFTFactory *factory = user_data;
110
111   g_signal_emit (factory, signals[NEW_INCOMING_TRANSFER], 0, handler, error);
112 }
113
114 /* public methods */
115
116 EmpathyFTFactory*
117 empathy_ft_factory_dup_singleton (void)
118 {
119   return g_object_new (EMPATHY_TYPE_FT_FACTORY, NULL);
120 }
121
122 void
123 empathy_ft_factory_new_transfer_outgoing (EmpathyFTFactory *factory,
124                                           EmpathyContact *contact,
125                                           GFile *source,
126                                           gboolean use_hash)
127 {
128   g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
129   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
130   g_return_if_fail (G_IS_FILE (source));
131
132   empathy_ft_handler_new_outgoing (contact, source, use_hash,
133       ft_handler_outgoing_ready_cb, factory);
134 }
135
136 void
137 empathy_ft_factory_claim_channel (EmpathyFTFactory *factory,
138                                   EmpathyDispatchOperation *operation)
139 {
140   EmpathyTpFile *tp_file;
141
142   g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
143   g_return_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation));
144
145   /* own a reference to the EmpathyTpFile */
146   tp_file = EMPATHY_TP_FILE
147       ((empathy_dispatch_operation_get_channel_wrapper (operation)));
148
149   empathy_ft_handler_new_incoming (tp_file, ft_handler_incoming_ready_cb,
150       factory);
151
152   empathy_dispatch_operation_claim (operation);
153 }
154
155 void
156 empathy_ft_factory_set_destination_for_incoming_handler
157                                                  (EmpathyFTFactory *factory,
158                                                   EmpathyFTHandler *handler,
159                                                   GFile *destination,
160                                                   gboolean use_hash)
161 {
162   g_return_if_fail (EMPATHY_IS_FT_FACTORY (factory));
163   g_return_if_fail (EMPATHY_IS_FT_HANDLER (handler));
164   g_return_if_fail (G_IS_FILE (destination));
165
166   empathy_ft_handler_incoming_set_destination (handler, destination, use_hash);
167
168   g_signal_emit (factory, signals[NEW_FT_HANDLER], 0, handler, FALSE);
169 }