Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gdummyproxyresolver.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2010 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 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
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
21  */
22
23 #include "config.h"
24
25 #include "gdummyproxyresolver.h"
26
27 #include <glib.h>
28
29 #include "gasyncresult.h"
30 #include "gcancellable.h"
31 #include "gproxyresolver.h"
32 #include "gsimpleasyncresult.h"
33
34 #include "giomodule.h"
35 #include "giomodule-priv.h"
36
37 struct _GDummyProxyResolver {
38   GObject parent_instance;
39 };
40
41 static void g_dummy_proxy_resolver_iface_init (GProxyResolverInterface *iface);
42
43 #define g_dummy_proxy_resolver_get_type _g_dummy_proxy_resolver_get_type
44 G_DEFINE_TYPE_WITH_CODE (GDummyProxyResolver, g_dummy_proxy_resolver, G_TYPE_OBJECT,
45                          G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,
46                                                 g_dummy_proxy_resolver_iface_init)
47                          _g_io_modules_ensure_extension_points_registered ();
48                          g_io_extension_point_implement (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
49                                                          g_define_type_id,
50                                                          "dummy",
51                                                          -100))
52
53 static void
54 g_dummy_proxy_resolver_finalize (GObject *object)
55 {
56   /* must chain up */
57   G_OBJECT_CLASS (g_dummy_proxy_resolver_parent_class)->finalize (object);
58 }
59
60 static void
61 g_dummy_proxy_resolver_init (GDummyProxyResolver *resolver)
62 {
63 }
64
65 static gboolean
66 g_dummy_proxy_resolver_is_supported (GProxyResolver *resolver)
67 {
68   return TRUE;
69 }
70
71 static gchar **
72 g_dummy_proxy_resolver_lookup (GProxyResolver  *resolver,
73                                const gchar     *uri,
74                                GCancellable    *cancellable,
75                                GError         **error)
76 {
77   gchar **proxies;
78
79   if (g_cancellable_set_error_if_cancelled (cancellable, error))
80     return NULL;
81
82   proxies = g_new0 (gchar *, 2);
83   proxies[0] = g_strdup ("direct://");
84
85   return proxies;
86 }
87
88 static void
89 g_dummy_proxy_resolver_lookup_async (GProxyResolver      *resolver,
90                                      const gchar         *uri,
91                                      GCancellable        *cancellable,
92                                      GAsyncReadyCallback  callback,
93                                      gpointer             user_data)
94 {
95   GError *error = NULL;
96   GSimpleAsyncResult *simple;
97   gchar **proxies;
98
99   proxies = g_dummy_proxy_resolver_lookup (resolver, uri, cancellable, &error);
100
101   
102   simple = g_simple_async_result_new (G_OBJECT (resolver),
103                                       callback, user_data,
104                                       g_dummy_proxy_resolver_lookup_async);
105
106   if (proxies == NULL)
107     {
108       g_simple_async_result_set_from_error (simple, error);
109       g_error_free (error);
110     }
111   else
112     {
113       g_simple_async_result_set_op_res_gpointer (simple,
114                                                  proxies,
115                                                  NULL);
116     }
117
118   g_simple_async_result_complete_in_idle (simple);
119   g_object_unref (simple);
120 }
121
122 static gchar **
123 g_dummy_proxy_resolver_lookup_finish (GProxyResolver     *resolver,
124                                       GAsyncResult       *result,
125                                       GError            **error)
126 {
127   if (G_IS_SIMPLE_ASYNC_RESULT (result))
128     {
129       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
130
131       if (g_simple_async_result_propagate_error (simple, error))
132         return NULL;
133
134       return g_simple_async_result_get_op_res_gpointer (simple);
135     }
136
137   return NULL;
138 }
139
140 static void
141 g_dummy_proxy_resolver_class_init (GDummyProxyResolverClass *resolver_class)
142 {
143   GObjectClass *object_class;
144   
145   object_class = G_OBJECT_CLASS (resolver_class);
146   object_class->finalize = g_dummy_proxy_resolver_finalize;
147 }
148
149 static void
150 g_dummy_proxy_resolver_iface_init (GProxyResolverInterface *iface)
151 {
152   iface->is_supported = g_dummy_proxy_resolver_is_supported;
153   iface->lookup = g_dummy_proxy_resolver_lookup;
154   iface->lookup_async = g_dummy_proxy_resolver_lookup_async;
155   iface->lookup_finish = g_dummy_proxy_resolver_lookup_finish;
156 }