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