Try legacy config file location if config file not found
[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)
51                 /* Not much to do in this case ... */
52                 return;
53
54         if (uri[0] == '/') {
55                 new_uri_len = strlen("file://") + strlen(uri) + 1;
56                 if (!(new_uri = calloc(new_uri_len, sizeof(char))))
57                         exit(1);
58                 snprintf(new_uri, new_uri_len, "%s%s", "file://", uri);
59
60                 launch_browser(&ctx, new_uri);
61                 /* If launch_browser didn't exec something in this process,
62                    we need to clean up after ourselves */
63                 free(new_uri);
64         } else {
65                 printf("open_address '%s'\n", uri);
66                 launch_browser(&ctx, (char *)uri);
67         }
68 }
69
70
71 /*
72  * The com.nokia.osso_browser D-Bus interface
73  */
74 gboolean osso_browser_load_url(OssoBrowser *obj,
75                 const char *uri, GError **error) {
76         open_address(uri);
77         return TRUE;
78 }
79
80 gboolean osso_browser_mime_open(OssoBrowser *obj,
81                 const char *uri, GError **error) {
82         open_address(uri);
83         return TRUE;
84 }
85
86 gboolean osso_browser_open_new_window(OssoBrowser *obj,
87                 const char *uri, GError **error) {
88         open_address(uri);
89         return TRUE;
90 }
91
92 gboolean osso_browser_top_application(OssoBrowser *obj,
93                 GError **error) {
94         launch_microb(&ctx, "new_window");
95         return TRUE;
96 }
97
98 /* This is a "undocumented", non-standard extension to the API, ONLY
99    for use by /usr/bin/browser wrapper to implement --url */
100 gboolean osso_browser_switchboard_launch_microb(OssoBrowser *obj,
101                 const char *uri, GError **error) {
102         launch_microb(&ctx, (char *)uri);
103         return TRUE;
104 }
105
106
107 /* Register the name com.nokia.osso_browser on the D-Bus session bus */
108 void dbus_request_osso_browser_name(struct swb_context *ctx) {
109         GError *error = NULL;
110         guint result;
111
112         if (!ctx || !ctx->dbus_proxy)
113                 return;
114
115         if (!dbus_g_proxy_call(ctx->dbus_proxy, "RequestName", &error,
116                                G_TYPE_STRING, "com.nokia.osso_browser",
117                                G_TYPE_UINT, DBUS_NAME_FLAG_REPLACE_EXISTING|DBUS_NAME_FLAG_DO_NOT_QUEUE,
118                                G_TYPE_INVALID,
119                                G_TYPE_UINT, &result,
120                                G_TYPE_INVALID)) {
121                 printf("Couldn't acquire name com.nokia.osso_browser\n");
122                 exit(1);
123         }
124         if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {  
125                 printf("Couldn't acquire name com.nokia.osso_browser\n");
126                 exit(1);
127         }
128 }
129
130 /* Release the name com.nokia.osso_browser on the D-Bus session bus */
131 void dbus_release_osso_browser_name(struct swb_context *ctx) {
132         GError *error = NULL;
133         guint result;
134
135         if (!ctx || !ctx->dbus_proxy)
136                 return;
137
138         dbus_g_proxy_call(ctx->dbus_proxy, "ReleaseName", &error,
139                           G_TYPE_STRING, "com.nokia.osso_browser",
140                           G_TYPE_INVALID,
141                           G_TYPE_UINT, &result,
142                           G_TYPE_INVALID);
143 }