1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <dlfcn.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 typedef int   gboolean;
35 typedef char  gchar;
36 typedef struct _GError GError;
37 
38 struct _GError
39 {
40   int domain;
41   int code;
42   char *message;
43 };
44 
45 typedef enum {
46   GNOME_VFS_OK
47 } GnomeVFSResult;
48 
49 
50 /*
51  * HACK: avoid error messages caused by not setting a GNOME program name
52  */
53 
54 gchar* gnome_gconf_get_gnome_libs_settings_relative (const gchar *subkey)
55 {
56     void* handle = dlopen("libglib-2.0.so.0", RTLD_LAZY);
57 
58     (void)subkey; /* avoid warning due to unused parameter */
59 
60     if( NULL != handle )
61     {
62         gchar* (* g_strdup)(const gchar*) = (gchar* (*)(const gchar*)) dlsym(handle, "g_strdup");
63 
64         if( NULL != g_strdup)
65             return g_strdup("/apps/gnome-settings/gnome-open-url");
66     }
67 
68     return NULL;
69 }
70 
71 /*
72  * Wrapper function which extracs gnome_url_show from libgnome
73  */
74 
75 gboolean gnome_url_show (const char *url, GError **error)
76 {
77     void* handle = dlopen("libgnomevfs-2.so.0", RTLD_LAZY);
78     gboolean ret = 0;
79 
80     (void)error; /* avoid warning due to unused parameter */
81 
82     if( NULL != handle )
83     {
84         gboolean (* init) (void) =
85             (gboolean (*) (void)) dlsym(handle, "gnome_vfs_init");
86 
87         if( NULL != init && init() )
88         {
89             GnomeVFSResult (* func) (const char *url) =
90                 (GnomeVFSResult (*) (const char *)) dlsym(handle, "gnome_vfs_url_show");
91 
92             if( NULL != func )
93                 ret = (GNOME_VFS_OK == func(url));
94         }
95 
96         dlclose(handle);
97     }
98 
99     return ret;
100 }
101 
102 /*
103  * The intended use of this tool is to pass the argument to
104  * the gnome_show_url function of libgnome2.
105  */
106 
107 int main(int argc, char *argv[] )
108 {
109     GError *error = NULL;
110     char *fallback;
111     char *index;
112 
113     if( argc != 2 )
114     {
115         fprintf( stderr, "Usage: gnome-open-url <uri>\n" );
116         return -1;
117     }
118 
119     if( gnome_url_show(argv[1], &error) )
120     {
121         return 0;
122     }
123 
124     /*
125      * launch open-url command by replacing gnome-open-url from
126      * the command line. This is the fallback when running on
127      * remote machines with no GNOME installed.
128      */
129 
130     fallback = strdup(argv[0]);
131     index = strstr(fallback, "gnome-open-url");
132     if ( NULL != index )
133     {
134         char *args[3];
135         strncpy(index, "open-url", 9);
136         args[0] = fallback;
137         args[1] = argv[1];
138         args[2] = NULL;
139         return execv(fallback, args);
140     }
141 
142     return -1;
143 }
144 
145 
146 
147