Add logging functions with selectable log output targets
[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
33 extern struct swb_context ctx;
34
35 G_DEFINE_TYPE(OssoBrowser, osso_browser, G_TYPE_OBJECT);
36 static void osso_browser_init(OssoBrowser *obj)
37 {
38 }
39
40 static void osso_browser_class_init(OssoBrowserClass *klass)
41 {
42 }
43
44 #include "dbus-server-glue.h"
45
46
47 /* Ignore reconfiguration signal (SIGHUP)
48    When not running in continuous mode, no SIGHUP handler is installed, which
49    causes browser-switchboard to quit on a reconfig request.  This is normally
50    what we want -- but if we're already in the process of dispatching a
51    request, we'll quit anyway after finishing what we're doing, so ignoring the
52    signal is the right thing to do. */
53 static void ignore_reconfig_requests(void) {
54         struct sigaction act;
55
56         act.sa_flags = SA_RESTART;
57         sigemptyset(&(act.sa_mask));
58         act.sa_handler = SIG_IGN;
59         sigaction(SIGHUP, &act, NULL);
60 }
61
62 static void open_address(const char *uri) {
63         char *new_uri;
64         size_t new_uri_len;
65
66         if (!uri)
67                 /* Not much to do in this case ... */
68                 return;
69
70         printf("open_address '%s'\n", uri);
71         if (uri[0] == '/') {
72                 /* URI begins with a '/' -- assume it points to a local file
73                    and prefix with "file://" */
74                 new_uri_len = strlen("file://") + strlen(uri) + 1;
75                 if (!(new_uri = calloc(new_uri_len, sizeof(char))))
76                         exit(1);
77                 snprintf(new_uri, new_uri_len, "%s%s", "file://", uri);
78
79                 launch_browser(&ctx, new_uri);
80                 /* If launch_browser didn't exec something in this process,
81                    we need to clean up after ourselves */
82                 free(new_uri);
83         } else {
84 #ifdef FREMANTLE
85                 if (!strcmp(uri, "http://link.ovi.mobi/n900ovistore")) {
86                         /* Ovi Store webpage will not open correctly in
87                            any browser other than MicroB, so force the
88                            link in the provided bookmark to open in MicroB */
89                         launch_microb(&ctx, (char *)uri);
90                         return;
91                 }
92 #endif
93                 launch_browser(&ctx, (char *)uri);
94         }
95 }
96
97
98 /*
99  * The com.nokia.osso_browser D-Bus interface
100  */
101 gboolean osso_browser_load_url(OssoBrowser *obj,
102                 const char *uri, GError **error) {
103         if (!ctx.continuous_mode)
104                 ignore_reconfig_requests();
105         open_address(uri);
106         return TRUE;
107 }
108
109 gboolean osso_browser_mime_open(OssoBrowser *obj,
110                 const char *uri, GError **error) {
111         if (!ctx.continuous_mode)
112                 ignore_reconfig_requests();
113         open_address(uri);
114         return TRUE;
115 }
116
117 gboolean osso_browser_open_new_window(OssoBrowser *obj,
118                 const char *uri, GError **error) {
119         if (!ctx.continuous_mode)
120                 ignore_reconfig_requests();
121         open_address(uri);
122         return TRUE;
123 }
124
125 gboolean osso_browser_top_application(OssoBrowser *obj,
126                 GError **error) {
127         if (!ctx.continuous_mode)
128                 ignore_reconfig_requests();
129         launch_microb(&ctx, "new_window");
130         return TRUE;
131 }
132
133 /* This is a "undocumented", non-standard extension to the API, ONLY
134    for use by /usr/bin/browser wrapper to implement --url */
135 gboolean osso_browser_switchboard_launch_microb(OssoBrowser *obj,
136                 const char *uri, GError **error) {
137         if (!ctx.continuous_mode)
138                 ignore_reconfig_requests();
139         launch_microb(&ctx, (char *)uri);
140         return TRUE;
141 }
142
143
144 /* Register the name com.nokia.osso_browser on the D-Bus session bus */
145 void dbus_request_osso_browser_name(struct swb_context *ctx) {
146         GError *error = NULL;
147         guint result;
148
149         if (!ctx || !ctx->dbus_proxy)
150                 return;
151
152         if (!dbus_g_proxy_call(ctx->dbus_proxy, "RequestName", &error,
153                                G_TYPE_STRING, "com.nokia.osso_browser",
154                                G_TYPE_UINT, DBUS_NAME_FLAG_REPLACE_EXISTING|DBUS_NAME_FLAG_DO_NOT_QUEUE,
155                                G_TYPE_INVALID,
156                                G_TYPE_UINT, &result,
157                                G_TYPE_INVALID)) {
158                 printf("Couldn't acquire name com.nokia.osso_browser\n");
159                 exit(1);
160         }
161         if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {  
162                 printf("Couldn't acquire name com.nokia.osso_browser\n");
163                 exit(1);
164         }
165 }
166
167 /* Release the name com.nokia.osso_browser on the D-Bus session bus */
168 void dbus_release_osso_browser_name(struct swb_context *ctx) {
169         GError *error = NULL;
170         guint result;
171
172         if (!ctx || !ctx->dbus_proxy)
173                 return;
174
175         dbus_g_proxy_call(ctx->dbus_proxy, "ReleaseName", &error,
176                           G_TYPE_STRING, "com.nokia.osso_browser",
177                           G_TYPE_INVALID,
178                           G_TYPE_UINT, &result,
179                           G_TYPE_INVALID);
180 }