Make config file parsing (mostly) compatible with the Python implementation
[browser-switch] / dbus-server-bindings.c
1 /*
2  * dbus-server-bindings.c -- osso_browser D-Bus interface implementation
3  *
4  * Copyright (C) 2009 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 <dbus/dbus-glib.h>
27
28 #include "browser-switchboard.h"
29 #include "launcher.h"
30 #include "dbus-server-bindings.h"
31
32 extern struct swb_context ctx;
33
34 G_DEFINE_TYPE(OssoBrowser, osso_browser, G_TYPE_OBJECT);
35 static void osso_browser_init(OssoBrowser *obj)
36 {
37 }
38
39 static void osso_browser_class_init(OssoBrowserClass *klass)
40 {
41 }
42
43 #include "dbus-server-glue.h"
44
45
46 static void open_address(const char * uri) {
47         char * new_uri;
48         size_t new_uri_len;
49
50         if (!uri && uri[0] == '/') {
51                 new_uri_len = strlen("file://") + strlen(uri) + 1;
52                 if (!(new_uri = calloc(new_uri_len, sizeof(char))))
53                         exit(1);
54                 strncpy(new_uri, "file://", strlen("file://"));
55                 strncat(new_uri, uri, strlen(uri));
56
57                 launch_browser(&ctx, new_uri);
58                 /* If launch_browser didn't exec something in this process,
59                    we need to clean up after ourselves */
60                 free(new_uri);
61         } else {
62                 printf("open_address '%s'\n", uri);
63                 launch_browser(&ctx, (char *)uri);
64         }
65 }
66
67
68 /*
69  * The com.nokia.osso_browser D-Bus interface
70  */
71 gboolean osso_browser_load_url(OssoBrowser *obj,
72                 const char * uri, GError **error) {
73         open_address(uri);
74         return TRUE;
75 }
76
77 gboolean osso_browser_mime_open(OssoBrowser *obj,
78                 const char * uri, GError **error) {
79         open_address(uri);
80         return TRUE;
81 }
82
83 gboolean osso_browser_open_new_window(OssoBrowser *obj,
84                 const char * uri, GError **error) {
85         open_address(uri);
86         return TRUE;
87 }
88
89 gboolean osso_browser_top_application(OssoBrowser *obj,
90                 GError **error) {
91         launch_microb(&ctx, "new_window");
92         return TRUE;
93 }
94
95 /* This is a "undocumented", non-standard extension to the API, ONLY
96    for use by /usr/bin/browser wrapper to implement --url */
97 gboolean osso_browser_switchboard_launch_microb(OssoBrowser *obj,
98                 const char * uri, GError **error) {
99         launch_microb(&ctx, (char *)uri);
100         return TRUE;
101 }
102
103
104 /* Register the name com.nokia.osso_browser on the D-Bus session bus */
105 void dbus_request_osso_browser_name(struct swb_context * ctx) {
106         GError * error = NULL;
107         guint result;
108
109         if (!ctx || !ctx->dbus_proxy)
110                 return;
111
112         if (!dbus_g_proxy_call(ctx->dbus_proxy, "RequestName", &error,
113                                 G_TYPE_STRING, "com.nokia.osso_browser",
114                                 G_TYPE_UINT, DBUS_NAME_FLAG_REPLACE_EXISTING|DBUS_NAME_FLAG_DO_NOT_QUEUE,
115                                 G_TYPE_INVALID,
116                                 G_TYPE_UINT, &result,
117                                 G_TYPE_INVALID)) {
118                 printf("Couldn't acquire name com.nokia.osso_browser\n");
119                 exit(1);
120         }
121         if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {  
122                 printf("Couldn't acquire name com.nokia.osso_browser\n");
123                 exit(1);
124         }
125 }
126
127 /* Release the name com.nokia.osso_browser on the D-Bus session bus */
128 void dbus_release_osso_browser_name(struct swb_context * ctx) {
129         GError * error = NULL;
130         guint result;
131
132         if (!ctx || !ctx->dbus_proxy)
133                 return;
134
135         dbus_g_proxy_call(ctx->dbus_proxy, "ReleaseName", &error,
136                                 G_TYPE_STRING, "com.nokia.osso_browser",
137                                 G_TYPE_INVALID,
138                                 G_TYPE_UINT, &result,
139                                 G_TYPE_INVALID);
140 }