1*badc9ebbSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*badc9ebbSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*badc9ebbSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*badc9ebbSAndrew Rist * distributed with this work for additional information
6*badc9ebbSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*badc9ebbSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*badc9ebbSAndrew Rist * "License"); you may not use this file except in compliance
9*badc9ebbSAndrew Rist * with the License. You may obtain a copy of the License at
10*badc9ebbSAndrew Rist *
11*badc9ebbSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*badc9ebbSAndrew Rist *
13*badc9ebbSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*badc9ebbSAndrew Rist * software distributed under the License is distributed on an
15*badc9ebbSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*badc9ebbSAndrew Rist * KIND, either express or implied. See the License for the
17*badc9ebbSAndrew Rist * specific language governing permissions and limitations
18*badc9ebbSAndrew Rist * under the License.
19*badc9ebbSAndrew Rist *
20*badc9ebbSAndrew Rist *************************************************************/
21*badc9ebbSAndrew Rist
22*badc9ebbSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include <stdlib.h>
25cdf0e10cSrcweir #include <stdio.h>
26cdf0e10cSrcweir #include <dlfcn.h>
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir #include <unistd.h>
29cdf0e10cSrcweir
30cdf0e10cSrcweir typedef int gboolean;
31cdf0e10cSrcweir typedef char gchar;
32cdf0e10cSrcweir typedef struct _GError GError;
33cdf0e10cSrcweir
34cdf0e10cSrcweir struct _GError
35cdf0e10cSrcweir {
36cdf0e10cSrcweir int domain;
37cdf0e10cSrcweir int code;
38cdf0e10cSrcweir char *message;
39cdf0e10cSrcweir };
40cdf0e10cSrcweir
41cdf0e10cSrcweir typedef enum {
42cdf0e10cSrcweir GNOME_VFS_OK
43cdf0e10cSrcweir } GnomeVFSResult;
44cdf0e10cSrcweir
45cdf0e10cSrcweir
46cdf0e10cSrcweir /*
47cdf0e10cSrcweir * HACK: avoid error messages caused by not setting a GNOME program name
48cdf0e10cSrcweir */
49cdf0e10cSrcweir
gnome_gconf_get_gnome_libs_settings_relative(const gchar * subkey)50cdf0e10cSrcweir gchar* gnome_gconf_get_gnome_libs_settings_relative (const gchar *subkey)
51cdf0e10cSrcweir {
52cdf0e10cSrcweir void* handle = dlopen("libglib-2.0.so.0", RTLD_LAZY);
53cdf0e10cSrcweir
54cdf0e10cSrcweir (void)subkey; /* avoid warning due to unused parameter */
55cdf0e10cSrcweir
56cdf0e10cSrcweir if( NULL != handle )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir gchar* (* g_strdup)(const gchar*) = (gchar* (*)(const gchar*)) dlsym(handle, "g_strdup");
59cdf0e10cSrcweir
60cdf0e10cSrcweir if( NULL != g_strdup)
61cdf0e10cSrcweir return g_strdup("/apps/gnome-settings/gnome-open-url");
62cdf0e10cSrcweir }
63cdf0e10cSrcweir
64cdf0e10cSrcweir return NULL;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir
67cdf0e10cSrcweir /*
68cdf0e10cSrcweir * Wrapper function which extracs gnome_url_show from libgnome
69cdf0e10cSrcweir */
70cdf0e10cSrcweir
gnome_url_show(const char * url,GError ** error)71cdf0e10cSrcweir gboolean gnome_url_show (const char *url, GError **error)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir void* handle = dlopen("libgnomevfs-2.so.0", RTLD_LAZY);
74cdf0e10cSrcweir gboolean ret = 0;
75cdf0e10cSrcweir
76cdf0e10cSrcweir (void)error; /* avoid warning due to unused parameter */
77cdf0e10cSrcweir
78cdf0e10cSrcweir if( NULL != handle )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir gboolean (* init) (void) =
81cdf0e10cSrcweir (gboolean (*) (void)) dlsym(handle, "gnome_vfs_init");
82cdf0e10cSrcweir
83cdf0e10cSrcweir if( NULL != init && init() )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir GnomeVFSResult (* func) (const char *url) =
86cdf0e10cSrcweir (GnomeVFSResult (*) (const char *)) dlsym(handle, "gnome_vfs_url_show");
87cdf0e10cSrcweir
88cdf0e10cSrcweir if( NULL != func )
89cdf0e10cSrcweir ret = (GNOME_VFS_OK == func(url));
90cdf0e10cSrcweir }
91cdf0e10cSrcweir
92cdf0e10cSrcweir dlclose(handle);
93cdf0e10cSrcweir }
94cdf0e10cSrcweir
95cdf0e10cSrcweir return ret;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir
98cdf0e10cSrcweir /*
99cdf0e10cSrcweir * The intended use of this tool is to pass the argument to
100cdf0e10cSrcweir * the gnome_show_url function of libgnome2.
101cdf0e10cSrcweir */
102cdf0e10cSrcweir
main(int argc,char * argv[])103cdf0e10cSrcweir int main(int argc, char *argv[] )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir GError *error = NULL;
106cdf0e10cSrcweir char *fallback;
107cdf0e10cSrcweir char *index;
108cdf0e10cSrcweir
109cdf0e10cSrcweir if( argc != 2 )
110cdf0e10cSrcweir {
111cdf0e10cSrcweir fprintf( stderr, "Usage: gnome-open-url <uri>\n" );
112cdf0e10cSrcweir return -1;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir
115cdf0e10cSrcweir if( gnome_url_show(argv[1], &error) )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir return 0;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir
120cdf0e10cSrcweir /*
121cdf0e10cSrcweir * launch open-url command by replacing gnome-open-url from
122cdf0e10cSrcweir * the command line. This is the fallback when running on
123cdf0e10cSrcweir * remote machines with no GNOME installed.
124cdf0e10cSrcweir */
125cdf0e10cSrcweir
126cdf0e10cSrcweir fallback = strdup(argv[0]);
127cdf0e10cSrcweir index = strstr(fallback, "gnome-open-url");
128cdf0e10cSrcweir if ( NULL != index )
129cdf0e10cSrcweir {
130cdf0e10cSrcweir char *args[3];
131cdf0e10cSrcweir strncpy(index, "open-url", 9);
132cdf0e10cSrcweir args[0] = fallback;
133cdf0e10cSrcweir args[1] = argv[1];
134cdf0e10cSrcweir args[2] = NULL;
135cdf0e10cSrcweir return execv(fallback, args);
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
138cdf0e10cSrcweir return -1;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir
141cdf0e10cSrcweir
142cdf0e10cSrcweir
143