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