Whitespace fixes
[browser-switch] / dbus-server-bindings.c
1 /*
2  * dbus-server-bindings.c -- osso_browser D-Bus interface implementation
3  *
4  * Copyright (C) 2009-2010 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20  * USA.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <signal.h>
27 #include <dbus/dbus-glib.h>
28
29 #include "browser-switchboard.h"
30 #include "launcher.h"
31 #include "dbus-server-bindings.h"
32 #include "log.h"
33
34 extern struct swb_context ctx;
35
36 G_DEFINE_TYPE(OssoBrowser, osso_browser, G_TYPE_OBJECT);
37 static void osso_browser_init(OssoBrowser *obj)
38 {
39 }
40
41 static void osso_browser_class_init(OssoBrowserClass *klass)
42 {
43 }
44
45 #include "dbus-server-glue.h"
46
47
48 /* Ignore reconfiguration signal (SIGHUP)
49    When not running in continuous mode, no SIGHUP handler is installed, which
50    causes browser-switchboard to quit on a reconfig request.  This is normally
51    what we want -- but if we're already in the process of dispatching a
52    request, we'll quit anyway after finishing what we're doing, so ignoring the
53    signal is the right thing to do. */
54 static void ignore_reconfig_requests(void) {
55         struct sigaction act;
56
57         act.sa_flags = SA_RESTART;
58         sigemptyset(&(act.sa_mask));
59         act.sa_handler = SIG_IGN;
60         sigaction(SIGHUP, &act, NULL);
61 }
62
63 static void open_address(const char *uri) {
64         char *new_uri;
65         size_t new_uri_len;
66
67         if (!uri)
68                 /* Not much to do in this case ... */
69                 return;
70
71         log_msg("open_address '%s'\n", uri);
72         if (uri[0] == '/') {
73                 /* URI begins with a '/' -- assume it points to a local file
74                    and prefix with "file://" */
75                 new_uri_len = strlen("file://") + strlen(uri) + 1;
76                 if (!(new_uri = calloc(new_uri_len, sizeof(char))))
77                         exit(1);
78                 snprintf(new_uri, new_uri_len, "%s%s", "file://", uri);
79
80                 launch_browser(&ctx, new_uri);
81                 /* If launch_browser didn't exec something in this process,
82                    we need to clean up after ourselves */
83                 free(new_uri);
84         } else {
85 #ifdef FREMANTLE
86                 if (!strcmp(uri, "http://link.ovi.mobi/n900ovistore")) {
87                         /* Ovi Store webpage will not open correctly in
88                            any browser other than MicroB, so force the
89                            link in the provided bookmark to open in MicroB */
90                         launch_microb(&ctx, (char *)uri);
91                         return;
92                 }
93 #endif
94                 launch_browser(&ctx, (char *)uri);
95         }
96 }
97
98
99 /*
100  * The com.nokia.osso_browser D-Bus interface
101  */
102 gboolean osso_browser_load_url(OssoBrowser *obj,
103                 const char *uri, GError **error) {
104         if (!ctx.continuous_mode)
105                 ignore_reconfig_requests();
106         open_address(uri);
107         return TRUE;
108 }
109
110 gboolean osso_browser_load_url_sb(OssoBrowser *obj,
111                 const char *uri, gboolean fullscreen, GError **error) {
112         /* XXX don't ignore fullscreen requests */
113         if (!ctx.continuous_mode)
114                 ignore_reconfig_requests();
115         open_address(uri);
116         return TRUE;
117 }
118
119 gboolean osso_browser_mime_open(OssoBrowser *obj,
120                 const char *uri, GError **error) {
121         if (!ctx.continuous_mode)
122                 ignore_reconfig_requests();
123         open_address(uri);
124         return TRUE;
125 }
126
127 gboolean osso_browser_open_new_window(OssoBrowser *obj,
128                 const char *uri, GError **error) {
129         if (!ctx.continuous_mode)
130                 ignore_reconfig_requests();
131         open_address(uri);
132         return TRUE;
133 }
134
135 gboolean osso_browser_open_new_window_sb(OssoBrowser *obj,
136                 const char *uri, gboolean fullscreen, GError **error) {
137         /* XXX don't ignore fullscreen requests */
138         if (!ctx.continuous_mode)
139                 ignore_reconfig_requests();
140         open_address(uri);
141         return TRUE;
142 }
143
144 gboolean osso_browser_top_application(OssoBrowser *obj,
145                 GError **error) {
146         if (!ctx.continuous_mode)
147                 ignore_reconfig_requests();
148         launch_browser(&ctx, "new_window");
149         return TRUE;
150 }
151
152 /* This is a "undocumented", non-standard extension to the API, ONLY
153    for use by /usr/bin/microb wrapper */
154 gboolean osso_browser_switchboard_launch_microb(OssoBrowser *obj,
155                 const char *uri, GError **error) {
156         if (!ctx.continuous_mode)
157                 ignore_reconfig_requests();
158         launch_microb(&ctx, (char *)uri);
159         return TRUE;
160 }
161
162
163 /* Register the name com.nokia.osso_browser on the D-Bus session bus */
164 void dbus_request_osso_browser_name(struct swb_context *ctx) {
165         GError *error = NULL;
166         guint result;
167
168         if (!ctx || !ctx->dbus_proxy || !ctx->dbus_system_proxy)
169                 return;
170
171         /* Acquire the com.nokia.osso_browser name on the session bus */
172         if (!dbus_g_proxy_call(ctx->dbus_proxy, "RequestName", &error,
173                                G_TYPE_STRING, "com.nokia.osso_browser",
174                                G_TYPE_UINT, DBUS_NAME_FLAG_REPLACE_EXISTING|DBUS_NAME_FLAG_DO_NOT_QUEUE,
175                                G_TYPE_INVALID,
176                                G_TYPE_UINT, &result,
177                                G_TYPE_INVALID)) {
178                 log_msg("Couldn't acquire name com.nokia.osso_browser\n");
179                 exit(1);
180         }
181         if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
182                 log_msg("Couldn't acquire name com.nokia.osso_browser\n");
183                 exit(1);
184         }
185
186         /* Try to acquire the com.nokia.osso_browser name on the system bus
187            Treat a failure as non-fatal, which makes testing on desktop systems
188            easier */
189         if (!dbus_g_proxy_call(ctx->dbus_system_proxy, "RequestName", &error,
190                                G_TYPE_STRING, "com.nokia.osso_browser",
191                                G_TYPE_UINT, DBUS_NAME_FLAG_REPLACE_EXISTING|DBUS_NAME_FLAG_DO_NOT_QUEUE,
192                                G_TYPE_INVALID,
193                                G_TYPE_UINT, &result,
194                                G_TYPE_INVALID)) {
195                 log_msg("Couldn't acquire name com.nokia.osso_browser on system bus\n");
196                 g_error_free(error);
197         }
198         if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
199                 log_msg("Couldn't acquire name com.nokia.osso_browser on system bus\n");
200         }
201 }
202
203 /* Release the name com.nokia.osso_browser on the D-Bus session bus */
204 void dbus_release_osso_browser_name(struct swb_context *ctx) {
205         GError *error = NULL;
206         guint result;
207
208         if (!ctx || !ctx->dbus_proxy || !ctx->dbus_system_proxy)
209                 return;
210
211         dbus_g_proxy_call(ctx->dbus_proxy, "ReleaseName", &error,
212                           G_TYPE_STRING, "com.nokia.osso_browser",
213                           G_TYPE_INVALID,
214                           G_TYPE_UINT, &result,
215                           G_TYPE_INVALID);
216         dbus_g_proxy_call(ctx->dbus_system_proxy, "ReleaseName", &error,
217                           G_TYPE_STRING, "com.nokia.osso_browser",
218                           G_TYPE_INVALID,
219                           G_TYPE_UINT, &result,
220                           G_TYPE_INVALID);
221 }