1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*
* Copyright (c) 2009 Nick Schermer <nick@xfce.org>.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <exo/exo.h>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
#define EXO_HELPER_LAUNCH HELPERDIR G_DIR_SEPARATOR_S "exo-helper-" LIBEXO_VERSION_API " --launch "
#define EXO_TYPE_GIO_MODULE (exo_gio_module_get_type ())
#define EXO_GIO_MODULE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_GIO_MODULE, ExoGioModule))
#define EXO_GIO_MODULE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), G_TYPE_GIO_MODULE, ExoGioModuleClass))
#define EXO_IS_GIO_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_GIO_MODULE))
#define EXO_IS_GIO_MODULE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_GIO_MODULE))
GType exo_gio_module_get_type (void);
static void exo_gio_module_app_info_lookup_iface_init (GDesktopAppInfoLookupIface *iface);
static GAppInfo *exo_gio_module_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
const gchar *uri_scheme);
typedef struct _ExoGioModuleClass ExoGioModuleClass;
typedef struct _ExoGioModule ExoGioModule;
typedef struct _KnownSchemes KnownSchemes;
struct _ExoGioModuleClass
{
GObjectClass __parent__;
};
struct _ExoGioModule
{
GObject __parent__;
};
struct _KnownSchemes
{
const gchar *pattern;
const gchar *category;
const gchar *app_name; /* name of the desktop files we use after gio 2.27 for thunar */
};
static KnownSchemes known_schemes[] =
{
{ "^(https?|ftps?|gopher)$", "WebBrowser", "exo-web-browser" },
{ "^mailto$", "MailReader", "exo-mail-reader" },
{ "^(file|trash|ssh)$", "FileManager", "exo-file-manager" }
};
#define _G_IMPLEMENT_INTERFACE_DYNAMIC(TYPE_IFACE, iface_init) \
{ \
const GInterfaceInfo g_implement_interface_info = { \
(GInterfaceInitFunc) iface_init, NULL, NULL \
}; \
g_type_module_add_interface (type_module, g_define_type_id, \
TYPE_IFACE, &g_implement_interface_info); \
}
G_DEFINE_DYNAMIC_TYPE_EXTENDED (ExoGioModule, exo_gio_module, G_TYPE_OBJECT, 0,
_G_IMPLEMENT_INTERFACE_DYNAMIC (G_TYPE_DESKTOP_APP_INFO_LOOKUP,
exo_gio_module_app_info_lookup_iface_init))
static void
exo_gio_module_class_init (ExoGioModuleClass *klass)
{
}
static void
exo_gio_module_init (ExoGioModule *module)
{
}
static void
exo_gio_module_class_finalize (ExoGioModuleClass *klass)
{
}
static void
exo_gio_module_app_info_lookup_iface_init (GDesktopAppInfoLookupIface *iface)
{
iface->get_default_for_uri_scheme = exo_gio_module_get_default_for_uri_scheme;
}
static GAppInfo *
exo_gio_module_get_default_for_uri_scheme (GDesktopAppInfoLookup *lookup,
const gchar *uri_scheme)
{
GAppInfo *info = NULL;
GError *error = NULL;
gchar *command;
gboolean found;
guint i;
/* do nothing when there is no scheme defined */
if (G_UNLIKELY (uri_scheme == NULL))
return NULL;
for (i = 0, found = FALSE; !found && i < G_N_ELEMENTS (known_schemes); i++)
{
/* see if the scheme matches */
found = g_regex_match_simple (known_schemes[i].pattern, uri_scheme, G_REGEX_CASELESS, 0);
if (found)
{
/* use the exo-helper directly here to avoid possible roundtrips with exo-open */
command = g_strconcat (EXO_HELPER_LAUNCH, known_schemes[i].category, NULL);
info = g_app_info_create_from_commandline (command, known_schemes[i].app_name,
G_APP_INFO_CREATE_SUPPORTS_URIS, &error);
if (G_UNLIKELY (info == NULL))
{
/* show error */
g_critical ("Failed to create GAppInfo from \"%s\" for URI-scheme \"%s\": %s.",
command, uri_scheme, error->message);
g_error_free (error);
}
/* cleanup */
g_free (command);
}
}
#ifndef NDEBUG
/* print debug message if scheme is not recognized by exo */
if (!found)
g_debug ("Unknown URI-scheme \"%s\".", uri_scheme);
#endif
return info;
}
G_MODULE_EXPORT void
g_io_module_load (GIOModule *module)
{
exo_gio_module_register_type (G_TYPE_MODULE (module));
g_io_extension_point_implement (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME,
EXO_TYPE_GIO_MODULE, "ExoGioModule", 15);
}
G_MODULE_EXPORT void
g_io_module_unload (GIOModule *module)
{
}
#if GLIB_CHECK_VERSION (2, 24, 0)
G_MODULE_EXPORT gchar **
g_io_module_query (void)
{
gchar *eps[] = {
G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME,
NULL
};
return g_strdupv (eps);
}
#endif
|