From 4f277063f076acfb8483d42415119b68c348ad9d Mon Sep 17 00:00:00 2001 From: JosephMcc Date: Sat, 21 Jan 2017 01:31:05 -0800 Subject: [PATCH] Remove the xed style scheme manager instance https://github.com/GNOME/gedit/commit/f5d46ef3cce4ee9e73802815e7ec8eecbaa057ad --- xed/Makefile.am | 2 - xed/dialogs/xed-preferences-dialog.c | 288 +++++++++++++++++++-- xed/xed-app.c | 12 + xed/xed-dirs.c | 118 +++++---- xed/xed-dirs.h | 22 +- xed/xed-document.c | 6 +- xed/xed-settings.c | 9 +- xed/xed-style-scheme-manager.c | 372 --------------------------- xed/xed-style-scheme-manager.h | 56 ---- 9 files changed, 365 insertions(+), 520 deletions(-) delete mode 100644 xed/xed-style-scheme-manager.c delete mode 100644 xed/xed-style-scheme-manager.h diff --git a/xed/Makefile.am b/xed/Makefile.am index 0a68f0a..b8339aa 100644 --- a/xed/Makefile.am +++ b/xed/Makefile.am @@ -60,7 +60,6 @@ NOINST_H_FILES = \ xed-session.h \ xed-settings.h \ xed-status-combo-box.h \ - xed-style-scheme-manager.h \ xed-tab-label.h \ xedtextregion.h \ xed-ui.h \ @@ -142,7 +141,6 @@ libxed_c_files = \ xed-searchbar.c \ xed-statusbar.c \ xed-status-combo-box.c \ - xed-style-scheme-manager.c \ xed-tab.c \ xed-tab-label.c \ xed-utils.c \ diff --git a/xed/dialogs/xed-preferences-dialog.c b/xed/dialogs/xed-preferences-dialog.c index 4a4f111..68a75d3 100755 --- a/xed/dialogs/xed-preferences-dialog.c +++ b/xed/dialogs/xed-preferences-dialog.c @@ -35,17 +35,19 @@ #include #include +#include #include +#include #include #include #include +#include #include "xed-preferences-dialog.h" #include "xed-utils.h" #include "xed-debug.h" #include "xed-document.h" -#include "xed-style-scheme-manager.h" #include "xed-help.h" #include "xed-dirs.h" #include "xed-settings.h" @@ -429,14 +431,36 @@ setup_font_colors_page_font_section (XedPreferencesDialog *dlg) gtk_widget_set_sensitive (dlg->priv->font_hbox, !use_default_font); } +static gboolean +is_xed_user_style_scheme (const gchar *scheme_id) +{ + GtkSourceStyleSchemeManager *manager; + GtkSourceStyleScheme *scheme; + gboolean res = FALSE; + + manager = gtk_source_style_scheme_manager_get_default (); + scheme = gtk_source_style_scheme_manager_get_scheme (manager, scheme_id); + if (scheme != NULL) + { + const gchar *filename; + + filename = gtk_source_style_scheme_get_filename (scheme); + if (filename != NULL) + { + res = g_str_has_prefix (filename, xed_dirs_get_user_styles_dir ()); + } + } + + return res; +} + static void set_buttons_sensisitivity_according_to_scheme (XedPreferencesDialog *dlg, const gchar *scheme_id) { gboolean editable; - editable = (scheme_id != NULL) && - _xed_style_scheme_manager_scheme_is_xed_user_scheme (xed_get_style_scheme_manager (), scheme_id); + editable = ((scheme_id != NULL) && is_xed_user_style_scheme (scheme_id)); gtk_widget_set_sensitive (dlg->priv->uninstall_scheme_button, editable); } @@ -465,9 +489,10 @@ static const gchar * ensure_color_scheme_id (XedPreferencesDialog *dlg, const gchar *id) { + GtkSourceStyleSchemeManager *manager; GtkSourceStyleScheme *scheme = NULL; - GtkSourceStyleSchemeManager *manager = xed_get_style_scheme_manager (); + manager = gtk_source_style_scheme_manager_get_default (); if (id == NULL) { gchar *pref_id; @@ -496,15 +521,13 @@ ensure_color_scheme_id (XedPreferencesDialog *dlg, return gtk_source_style_scheme_get_id (scheme); } -/* If def_id is NULL, use the default scheme as returned by - * xed_style_scheme_manager_get_default_scheme. If this one returns NULL - * use the first available scheme as default */ static const gchar * populate_color_scheme_list (XedPreferencesDialog *dlg, const gchar *def_id) { - GSList *schemes; - GSList *l; + GtkSourceStyleSchemeManager *manager; + const gchar * const *ids; + gint i; gtk_list_store_clear (dlg->priv->schemes_treeview_model); @@ -516,47 +539,262 @@ populate_color_scheme_list (XedPreferencesDialog *dlg, return NULL; } - schemes = xed_style_scheme_manager_list_schemes_sorted (xed_get_style_scheme_manager ()); - l = schemes; - while (l != NULL) + manager = gtk_source_style_scheme_manager_get_default (); + ids = gtk_source_style_scheme_manager_get_scheme_ids (manager); + for (i = 0; ids[i] != NULL; i++) { GtkSourceStyleScheme *scheme; - const gchar *id; const gchar *name; const gchar *description; GtkTreeIter iter; - scheme = GTK_SOURCE_STYLE_SCHEME (l->data); - - id = gtk_source_style_scheme_get_id (scheme); + scheme = gtk_source_style_scheme_manager_get_scheme (manager, ids[i]); name = gtk_source_style_scheme_get_name (scheme); description = gtk_source_style_scheme_get_description (scheme); gtk_list_store_append (dlg->priv->schemes_treeview_model, &iter); gtk_list_store_set (dlg->priv->schemes_treeview_model, &iter, - ID_COLUMN, id, + ID_COLUMN, ids[i], NAME_COLUMN, name, DESC_COLUMN, description, -1); g_return_val_if_fail (def_id != NULL, NULL); - if (strcmp (id, def_id) == 0) + if (strcmp (ids[i], def_id) == 0) { GtkTreeSelection *selection; selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dlg->priv->schemes_treeview)); gtk_tree_selection_select_iter (selection, &iter); } - - l = g_slist_next (l); } - g_slist_free (schemes); - return def_id; } +/* + * file_copy: + * @name: a pointer to a %NULL-terminated string, that names + * the file to be copied, in the GLib file name encoding + * @dest_name: a pointer to a %NULL-terminated string, that is the + * name for the destination file, in the GLib file name encoding + * @error: return location for a #GError, or %NULL + * + * Copies file @name to @dest_name. + * + * If the call was successful, it returns %TRUE. If the call was not + * successful, it returns %FALSE and sets @error. The error domain + * is #G_FILE_ERROR. Possible error + * codes are those in the #GFileError enumeration. + * + * Return value: %TRUE on success, %FALSE otherwise. + */ +static gboolean +file_copy (const gchar *name, + const gchar *dest_name, + GError **error) +{ + gchar *contents; + gsize length; + gchar *dest_dir; + + /* FIXME - Paolo (Aug. 13, 2007): + * Since the style scheme files are relatively small, we can implement + * file copy getting all the content of the source file in a buffer and + * then write the content to the destination file. In this way we + * can use the g_file_get_contents and g_file_set_contents and avoid to + * write custom code to copy the file (with sane error management). + * If needed we can improve this code later. */ + + g_return_val_if_fail (name != NULL, FALSE); + g_return_val_if_fail (dest_name != NULL, FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + /* Note: we allow to copy a file to itself since this is not a problem + * in our use case */ + + /* Ensure the destination directory exists */ + dest_dir = g_path_get_dirname (dest_name); + + errno = 0; + if (g_mkdir_with_parents (dest_dir, 0755) != 0) + { + gint save_errno = errno; + gchar *display_filename = g_filename_display_name (dest_dir); + + g_set_error (error, + G_FILE_ERROR, + g_file_error_from_errno (save_errno), + _("Directory '%s' could not be created: g_mkdir_with_parents() failed: %s"), + display_filename, + g_strerror (save_errno)); + + g_free (dest_dir); + g_free (display_filename); + + return FALSE; + } + + g_free (dest_dir); + + if (!g_file_get_contents (name, &contents, &length, error)) + { + return FALSE; + } + + if (!g_file_set_contents (dest_name, contents, length, error)) + { + return FALSE; + } + + g_free (contents); + + return TRUE; +} + +/* + * install_style_scheme: + * @manager: a #GtkSourceStyleSchemeManager + * @fname: the file name of the style scheme to be installed + * + * Install a new user scheme. + * This function copies @fname in #XED_STYLES_DIR and ask the style manager to + * recompute the list of available style schemes. It then checks if a style + * scheme with the right file name exists. + * + * If the call was succesful, it returns the id of the installed scheme + * otherwise %NULL. + * + * Return value: the id of the installed scheme, %NULL otherwise. + */ +static const gchar * +install_style_scheme (const gchar *fname) +{ + GtkSourceStyleSchemeManager *manager; + gchar *new_file_name = NULL; + gchar *dirname; + const gchar *styles_dir; + GError *error = NULL; + gboolean copied = FALSE; + const gchar* const *ids; + + g_return_val_if_fail (fname != NULL, NULL); + + manager = gtk_source_style_scheme_manager_get_default (); + + dirname = g_path_get_dirname (fname); + styles_dir = xed_dirs_get_user_styles_dir (); + + if (strcmp (dirname, styles_dir) != 0) + { + gchar *basename; + + basename = g_path_get_basename (fname); + new_file_name = g_build_filename (styles_dir, basename, NULL); + g_free (basename); + + /* Copy the style scheme file into GEDIT_STYLES_DIR */ + if (!file_copy (fname, new_file_name, &error)) + { + g_free (new_file_name); + + g_message ("Cannot install style scheme:\n%s", error->message); + + return NULL; + } + + copied = TRUE; + } + else + { + new_file_name = g_strdup (fname); + } + + g_free (dirname); + + /* Reload the available style schemes */ + gtk_source_style_scheme_manager_force_rescan (manager); + + /* Check the new style scheme has been actually installed */ + ids = gtk_source_style_scheme_manager_get_scheme_ids (manager); + + while (*ids != NULL) + { + GtkSourceStyleScheme *scheme; + const gchar *filename; + + scheme = gtk_source_style_scheme_manager_get_scheme (manager, *ids); + + filename = gtk_source_style_scheme_get_filename (scheme); + + if (filename && (strcmp (filename, new_file_name) == 0)) + { + /* The style scheme has been correctly installed */ + g_free (new_file_name); + + return gtk_source_style_scheme_get_id (scheme); + } + ++ids; + } + + /* The style scheme has not been correctly installed */ + if (copied) + { + g_unlink (new_file_name); + } + + g_free (new_file_name); + + return NULL; +} + +/** + * uninstall_style_scheme: + * @manager: a #GtkSourceStyleSchemeManager + * @id: the id of the style scheme to be uninstalled + * + * Uninstall a user scheme. + * + * If the call was succesful, it returns %TRUE + * otherwise %FALSE. + * + * Return value: %TRUE on success, %FALSE otherwise. + */ +static gboolean +uninstall_style_scheme (const gchar *id) +{ + GtkSourceStyleSchemeManager *manager; + GtkSourceStyleScheme *scheme; + const gchar *filename; + + g_return_val_if_fail (id != NULL, FALSE); + + manager = gtk_source_style_scheme_manager_get_default (); + + scheme = gtk_source_style_scheme_manager_get_scheme (manager, id); + if (scheme == NULL) + { + return FALSE; + } + + filename = gtk_source_style_scheme_get_filename (scheme); + if (filename == NULL) + { + return FALSE; + } + + if (g_unlink (filename) == -1) + { + return FALSE; + } + + /* Reload the available style schemes */ + gtk_source_style_scheme_manager_force_rescan (manager); + + return TRUE; +} + static void add_scheme_chooser_response_cb (GtkDialog *chooser, gint res_id, @@ -573,11 +811,13 @@ add_scheme_chooser_response_cb (GtkDialog *chooser, filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser)); if (filename == NULL) + { return; + } gtk_widget_hide (GTK_WIDGET (chooser)); - scheme_id = _xed_style_scheme_manager_install_scheme (xed_get_style_scheme_manager (), filename); + scheme_id = install_style_scheme (filename); g_free (filename); if (scheme_id == NULL) @@ -660,7 +900,7 @@ uninstall_scheme_clicked (GtkButton *button, gtk_tree_model_get (model, &iter, ID_COLUMN, &id, NAME_COLUMN, &name, -1); - if (!_xed_style_scheme_manager_uninstall_scheme (xed_get_style_scheme_manager (), id)) + if (!uninstall_style_scheme (id)) { xed_warning (GTK_WINDOW (dlg), _("Could not remove color scheme \"%s\"."), name); } diff --git a/xed/xed-app.c b/xed/xed-app.c index a06f2be..f23a567 100644 --- a/xed/xed-app.c +++ b/xed/xed-app.c @@ -37,6 +37,7 @@ #include #include +#include #include "xed-app.h" #include "xed-commands.h" @@ -351,12 +352,23 @@ extension_removed (PeasExtensionSet *extensions, static void xed_app_init (XedApp *app) { + GtkSourceStyleSchemeManager *manager; + app->priv = XED_APP_GET_PRIVATE (app); load_accels (); app->priv->settings = xed_settings_new (); app->priv->window_settings = g_settings_new ("org.x.editor.state.window"); + + /* + * We use the default gtksourceview style scheme manager so that plugins + * can obtain it easily without a xed specific api, but we need to + * add our search path at startup before the manager is actually used. + */ + manager = gtk_source_style_scheme_manager_get_default (); + gtk_source_style_scheme_manager_append_search_path (manager, xed_dirs_get_user_styles_dir ()); + app->priv->extensions = peas_extension_set_new (PEAS_ENGINE (xed_plugins_engine_get_default ()), XED_TYPE_APP_ACTIVATABLE, "app", app, NULL); diff --git a/xed/xed-dirs.c b/xed/xed-dirs.c index e125d5e..271a6cf 100644 --- a/xed/xed-dirs.c +++ b/xed/xed-dirs.c @@ -27,97 +27,115 @@ #include "xed-dirs.h" -gchar* xed_dirs_get_user_config_dir(void) +gchar * +xed_dirs_get_user_config_dir (void) { - gchar* config_dir = NULL; + gchar* config_dir = NULL; - config_dir = g_build_filename(g_get_user_config_dir(), "xed", NULL); + config_dir = g_build_filename(g_get_user_config_dir(), "xed", NULL); - return config_dir; + return config_dir; } -gchar* xed_dirs_get_user_cache_dir(void) +gchar * +xed_dirs_get_user_cache_dir (void) { - const gchar* cache_dir; + const gchar* cache_dir; - cache_dir = g_get_user_cache_dir(); + cache_dir = g_get_user_cache_dir(); - return g_build_filename(cache_dir, "xed", NULL); + return g_build_filename(cache_dir, "xed", NULL); } -gchar* xed_dirs_get_user_plugins_dir(void) +gchar * +xed_dirs_get_user_styles_dir (void) { - gchar* plugin_dir; + gchar *user_style_dir; - plugin_dir = g_build_filename(g_get_user_data_dir(), "xed", "plugins", NULL); - - return plugin_dir; + user_style_dir = g_build_filename (g_get_user_data_dir (), "xed", "styles", NULL); } -gchar* xed_dirs_get_user_accels_file(void) +gchar * +xed_dirs_get_user_plugins_dir (void) { - gchar* accels = NULL; - gchar *config_dir = NULL; + gchar* plugin_dir; - config_dir = xed_dirs_get_user_config_dir(); - accels = g_build_filename(config_dir, "accels", NULL); + plugin_dir = g_build_filename(g_get_user_data_dir(), "xed", "plugins", NULL); - g_free(config_dir); - - return accels; + return plugin_dir; } -gchar* xed_dirs_get_xed_data_dir(void) +gchar * +xed_dirs_get_user_accels_file (void) { - return g_build_filename(DATADIR, "xed", NULL); + gchar* accels = NULL; + gchar *config_dir = NULL; + + config_dir = xed_dirs_get_user_config_dir(); + accels = g_build_filename(config_dir, "accels", NULL); + + g_free(config_dir); + + return accels; } -gchar* xed_dirs_get_xed_locale_dir(void) +gchar * +xed_dirs_get_xed_data_dir (void) { - return g_build_filename(DATADIR, "locale", NULL); + return g_build_filename(DATADIR, "xed", NULL); } -gchar* xed_dirs_get_xed_lib_dir(void) +gchar * +xed_dirs_get_xed_locale_dir (void) { - return g_build_filename(LIBDIR, "xed", NULL); + return g_build_filename(DATADIR, "locale", NULL); } -gchar* xed_dirs_get_xed_plugins_dir(void) +gchar * +xed_dirs_get_xed_lib_dir (void) { - gchar* lib_dir; - gchar* plugin_dir; - - lib_dir = xed_dirs_get_xed_lib_dir(); - - plugin_dir = g_build_filename(lib_dir, "plugins", NULL); - g_free(lib_dir); - - return plugin_dir; + return g_build_filename(LIBDIR, "xed", NULL); } -gchar* xed_dirs_get_xed_plugins_data_dir (void) +gchar * +xed_dirs_get_xed_plugins_dir (void) { - gchar* data_dir; + gchar* lib_dir; + gchar* plugin_dir; + + lib_dir = xed_dirs_get_xed_lib_dir(); + + plugin_dir = g_build_filename(lib_dir, "plugins", NULL); + g_free(lib_dir); + + return plugin_dir; +} + +gchar * +xed_dirs_get_xed_plugins_data_dir (void) +{ + gchar* data_dir; gchar* plugin_data_dir; - data_dir = xed_dirs_get_xed_data_dir (); + data_dir = xed_dirs_get_xed_data_dir (); - plugin_data_dir = g_build_filename (data_dir, "plugins", NULL); + plugin_data_dir = g_build_filename (data_dir, "plugins", NULL); g_free (data_dir); - return plugin_data_dir; + return plugin_data_dir; } -gchar* xed_dirs_get_ui_file(const gchar* file) +gchar * +xed_dirs_get_ui_file (const gchar* file) { - gchar* datadir; - gchar* ui_file; + gchar* datadir; + gchar* ui_file; - g_return_val_if_fail(file != NULL, NULL); + g_return_val_if_fail(file != NULL, NULL); - datadir = xed_dirs_get_xed_data_dir(); - ui_file = g_build_filename(datadir, "ui", file, NULL); - g_free(datadir); + datadir = xed_dirs_get_xed_data_dir(); + ui_file = g_build_filename(datadir, "ui", file, NULL); + g_free(datadir); - return ui_file; + return ui_file; } diff --git a/xed/xed-dirs.h b/xed/xed-dirs.h index eb2d09f..97bcc6d 100644 --- a/xed/xed-dirs.h +++ b/xed/xed-dirs.h @@ -28,25 +28,27 @@ G_BEGIN_DECLS -gchar *xed_dirs_get_user_config_dir (void); +gchar *xed_dirs_get_user_config_dir (void); -gchar *xed_dirs_get_user_cache_dir (void); +gchar *xed_dirs_get_user_cache_dir (void); -gchar *xed_dirs_get_user_plugins_dir (void); +gchar *xed_dirs_get_user_styles_dir (void); -gchar *xed_dirs_get_user_accels_file (void); +gchar *xed_dirs_get_user_plugins_dir (void); -gchar *xed_dirs_get_xed_data_dir (void); +gchar *xed_dirs_get_user_accels_file (void); -gchar *xed_dirs_get_xed_locale_dir (void); +gchar *xed_dirs_get_xed_data_dir (void); -gchar *xed_dirs_get_xed_lib_dir (void); +gchar *xed_dirs_get_xed_locale_dir (void); -gchar *xed_dirs_get_xed_plugins_dir (void); +gchar *xed_dirs_get_xed_lib_dir (void); -gchar *xed_dirs_get_xed_plugins_data_dir (void); +gchar *xed_dirs_get_xed_plugins_dir (void); -gchar *xed_dirs_get_ui_file (const gchar *file); +gchar *xed_dirs_get_xed_plugins_data_dir (void); + +gchar *xed_dirs_get_ui_file (const gchar *file); G_END_DECLS diff --git a/xed/xed-document.c b/xed/xed-document.c index ec0942e..73d573d 100644 --- a/xed/xed-document.c +++ b/xed/xed-document.c @@ -40,13 +40,13 @@ #include #include #include +#include #include "xed-settings.h" #include "xed-document.h" #include "xed-debug.h" #include "xed-utils.h" #include "xed-language-manager.h" -#include "xed-style-scheme-manager.h" #include "xed-document-loader.h" #include "xed-document-saver.h" #include "xed-marshal.h" @@ -730,11 +730,11 @@ set_encoding (XedDocument *doc, static GtkSourceStyleScheme * get_default_style_scheme (GSettings *editor_settings) { + GtkSourceStyleSchemeManager *manager; gchar *scheme_id; GtkSourceStyleScheme *def_style; - GtkSourceStyleSchemeManager *manager; - manager = xed_get_style_scheme_manager (); + manager = gtk_source_style_scheme_manager_get_default (); scheme_id = g_settings_get_string (editor_settings, XED_SETTINGS_SCHEME); def_style = gtk_source_style_scheme_manager_get_scheme (manager, scheme_id); diff --git a/xed/xed-settings.c b/xed/xed-settings.c index 02d263c..a5052f9 100644 --- a/xed/xed-settings.c +++ b/xed/xed-settings.c @@ -23,6 +23,8 @@ #include +#include + #include "xed-settings.h" #include "xed-app.h" #include "xed-debug.h" @@ -30,7 +32,6 @@ #include "xed-window.h" #include "xed-notebook.h" #include "xed-plugins-engine.h" -#include "xed-style-scheme-manager.h" #include "xed-dirs.h" #include "xed-utils.h" #include "xed-window-private.h" @@ -187,6 +188,7 @@ on_scheme_changed (GSettings *settings, const gchar *key, XedSettings *xs) { + GtkSourceStyleSchemeManager *manager; GtkSourceStyleScheme *style; gchar *scheme; GList *docs; @@ -202,13 +204,14 @@ on_scheme_changed (GSettings *settings, g_free (xs->priv->old_scheme); xs->priv->old_scheme = scheme; - style = gtk_source_style_scheme_manager_get_scheme (xed_get_style_scheme_manager (), scheme); + manager = gtk_source_style_scheme_manager_get_default (); + style = gtk_source_style_scheme_manager_get_scheme (manager, scheme); if (style == NULL) { g_warning ("Default style scheme '%s' not found, falling back to 'classic'", scheme); - style = gtk_source_style_scheme_manager_get_scheme (xed_get_style_scheme_manager (), "classic"); + style = gtk_source_style_scheme_manager_get_scheme (manager, "classic"); if (style == NULL) { diff --git a/xed/xed-style-scheme-manager.c b/xed/xed-style-scheme-manager.c deleted file mode 100644 index cb40bfa..0000000 --- a/xed/xed-style-scheme-manager.c +++ /dev/null @@ -1,372 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * xed-source-style-manager.c - * - * Copyright (C) 2007 - Paolo Borelli and Paolo Maggi - * - * 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., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -/* - * Modified by the xed Team, 2007. See the AUTHORS file for a - * list of people on the xed Team. - * See the ChangeLog files for a list of changes. - * - * $Id$ - */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include - -#include -#include - -#include -#include - -#include "xed-style-scheme-manager.h" -#include "xed-dirs.h" - -static GtkSourceStyleSchemeManager *style_scheme_manager = NULL; - -static gchar * -get_xed_styles_path (void) -{ - gchar *config_dir; - gchar *dir = NULL; - - config_dir = xed_dirs_get_user_config_dir (); - - if (config_dir != NULL) - { - dir = g_build_filename (config_dir, - "styles", - NULL); - g_free (config_dir); - } - - return dir; -} - -static void -add_xed_styles_path (GtkSourceStyleSchemeManager *mgr) -{ - gchar *dir; - - dir = get_xed_styles_path(); - - if (dir != NULL) - { - gtk_source_style_scheme_manager_append_search_path (mgr, dir); - g_free (dir); - } -} - -GtkSourceStyleSchemeManager * -xed_get_style_scheme_manager (void) -{ - if (style_scheme_manager == NULL) - { - style_scheme_manager = gtk_source_style_scheme_manager_new (); - add_xed_styles_path (style_scheme_manager); - } - - return style_scheme_manager; -} - -static gint -schemes_compare (gconstpointer a, gconstpointer b) -{ - GtkSourceStyleScheme *scheme_a = (GtkSourceStyleScheme *)a; - GtkSourceStyleScheme *scheme_b = (GtkSourceStyleScheme *)b; - - const gchar *name_a = gtk_source_style_scheme_get_name (scheme_a); - const gchar *name_b = gtk_source_style_scheme_get_name (scheme_b); - - return g_utf8_collate (name_a, name_b); -} - -GSList * -xed_style_scheme_manager_list_schemes_sorted (GtkSourceStyleSchemeManager *manager) -{ - const gchar * const * scheme_ids; - GSList *schemes = NULL; - - g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME_MANAGER (manager), NULL); - - scheme_ids = gtk_source_style_scheme_manager_get_scheme_ids (manager); - - while (*scheme_ids != NULL) - { - GtkSourceStyleScheme *scheme; - - scheme = gtk_source_style_scheme_manager_get_scheme (manager, - *scheme_ids); - - schemes = g_slist_prepend (schemes, scheme); - - ++scheme_ids; - } - - if (schemes != NULL) - schemes = g_slist_sort (schemes, (GCompareFunc)schemes_compare); - - return schemes; -} - -gboolean -_xed_style_scheme_manager_scheme_is_xed_user_scheme (GtkSourceStyleSchemeManager *manager, - const gchar *scheme_id) -{ - GtkSourceStyleScheme *scheme; - const gchar *filename; - gchar *dir; - gboolean res = FALSE; - - scheme = gtk_source_style_scheme_manager_get_scheme (manager, scheme_id); - if (scheme == NULL) - return FALSE; - - filename = gtk_source_style_scheme_get_filename (scheme); - if (filename == NULL) - return FALSE; - - dir = get_xed_styles_path (); - - res = g_str_has_prefix (filename, dir); - - g_free (dir); - - return res; -} - -/** - * file_copy: - * @name: a pointer to a %NULL-terminated string, that names - * the file to be copied, in the GLib file name encoding - * @dest_name: a pointer to a %NULL-terminated string, that is the - * name for the destination file, in the GLib file name encoding - * @error: return location for a #GError, or %NULL - * - * Copies file @name to @dest_name. - * - * If the call was successful, it returns %TRUE. If the call was not - * successful, it returns %FALSE and sets @error. The error domain - * is #G_FILE_ERROR. Possible error - * codes are those in the #GFileError enumeration. - * - * Return value: %TRUE on success, %FALSE otherwise. - */ -static gboolean -file_copy (const gchar *name, - const gchar *dest_name, - GError **error) -{ - gchar *contents; - gsize length; - gchar *dest_dir; - - /* FIXME - Paolo (Aug. 13, 2007): - * Since the style scheme files are relatively small, we can implement - * file copy getting all the content of the source file in a buffer and - * then write the content to the destination file. In this way we - * can use the g_file_get_contents and g_file_set_contents and avoid to - * write custom code to copy the file (with sane error management). - * If needed we can improve this code later. */ - - g_return_val_if_fail (name != NULL, FALSE); - g_return_val_if_fail (dest_name != NULL, FALSE); - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - - /* Note: we allow to copy a file to itself since this is not a problem - * in our use case */ - - /* Ensure the destination directory exists */ - dest_dir = g_path_get_dirname (dest_name); - - errno = 0; - if (g_mkdir_with_parents (dest_dir, 0755) != 0) - { - gint save_errno = errno; - gchar *display_filename = g_filename_display_name (dest_dir); - - g_set_error (error, - G_FILE_ERROR, - g_file_error_from_errno (save_errno), - _("Directory '%s' could not be created: g_mkdir_with_parents() failed: %s"), - display_filename, - g_strerror (save_errno)); - - g_free (dest_dir); - g_free (display_filename); - - return FALSE; - } - - g_free (dest_dir); - - if (!g_file_get_contents (name, &contents, &length, error)) - return FALSE; - - if (!g_file_set_contents (dest_name, contents, length, error)) - return FALSE; - - g_free (contents); - - return TRUE; -} - -/** - * _xed_style_scheme_manager_install_scheme: - * @manager: a #GtkSourceStyleSchemeManager - * @fname: the file name of the style scheme to be installed - * - * Install a new user scheme. - * This function copies @fname in #XED_STYLES_DIR and ask the style manager to - * recompute the list of available style schemes. It then checks if a style - * scheme with the right file name exists. - * - * If the call was succesful, it returns the id of the installed scheme - * otherwise %NULL. - * - * Return value: the id of the installed scheme, %NULL otherwise. - */ -const gchar * -_xed_style_scheme_manager_install_scheme (GtkSourceStyleSchemeManager *manager, - const gchar *fname) -{ - gchar *new_file_name = NULL; - gchar *dirname; - gchar *styles_dir; - GError *error = NULL; - gboolean copied = FALSE; - - const gchar* const *ids; - - g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME_MANAGER (manager), NULL); - g_return_val_if_fail (fname != NULL, NULL); - - dirname = g_path_get_dirname (fname); - styles_dir = get_xed_styles_path(); - - if (strcmp (dirname, styles_dir) != 0) - { - gchar *basename; - - basename = g_path_get_basename (fname); - new_file_name = g_build_filename (styles_dir, basename, NULL); - g_free (basename); - - /* Copy the style scheme file into XED_STYLES_DIR */ - if (!file_copy (fname, new_file_name, &error)) - { - g_free (new_file_name); - - g_message ("Cannot install style scheme:\n%s", - error->message); - - g_free (dirname); - g_free (styles_dir); - return NULL; - } - - copied = TRUE; - } - else - { - new_file_name = g_strdup (fname); - } - - g_free (dirname); - g_free (styles_dir); - - /* Reload the available style schemes */ - gtk_source_style_scheme_manager_force_rescan (manager); - - /* Check the new style scheme has been actually installed */ - ids = gtk_source_style_scheme_manager_get_scheme_ids (manager); - - while (*ids != NULL) - { - GtkSourceStyleScheme *scheme; - const gchar *filename; - - scheme = gtk_source_style_scheme_manager_get_scheme ( - xed_get_style_scheme_manager (), *ids); - - filename = gtk_source_style_scheme_get_filename (scheme); - - if (filename && (strcmp (filename, new_file_name) == 0)) - { - /* The style scheme has been correctly installed */ - g_free (new_file_name); - - return gtk_source_style_scheme_get_id (scheme); - } - ++ids; - } - - /* The style scheme has not been correctly installed */ - if (copied) - g_unlink (new_file_name); - - g_free (new_file_name); - - return NULL; -} - -/** - * _xed_style_scheme_manager_uninstall_scheme: - * @manager: a #GtkSourceStyleSchemeManager - * @id: the id of the style scheme to be uninstalled - * - * Uninstall a user scheme. - * - * If the call was succesful, it returns %TRUE - * otherwise %FALSE. - * - * Return value: %TRUE on success, %FALSE otherwise. - */ -gboolean -_xed_style_scheme_manager_uninstall_scheme (GtkSourceStyleSchemeManager *manager, - const gchar *id) -{ - GtkSourceStyleScheme *scheme; - const gchar *filename; - - g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME_MANAGER (manager), FALSE); - g_return_val_if_fail (id != NULL, FALSE); - - scheme = gtk_source_style_scheme_manager_get_scheme (manager, id); - if (scheme == NULL) - return FALSE; - - filename = gtk_source_style_scheme_get_filename (scheme); - if (filename == NULL) - return FALSE; - - if (g_unlink (filename) == -1) - return FALSE; - - /* Reload the available style schemes */ - gtk_source_style_scheme_manager_force_rescan (manager); - - return TRUE; -} diff --git a/xed/xed-style-scheme-manager.h b/xed/xed-style-scheme-manager.h deleted file mode 100644 index 60d7718..0000000 --- a/xed/xed-style-scheme-manager.h +++ /dev/null @@ -1,56 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * xed-style-scheme-manager.h - * - * Copyright (C) 2007 - Paolo Borelli - * - * 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., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301, USA. - * - * $Id: xed-source-style-manager.h 5598 2007-04-15 13:16:24Z pborelli $ - */ - -#ifndef __XED_STYLE_SCHEME_MANAGER_H__ -#define __XED_STYLE_SCHEME_MANAGER_H__ - -#include - -G_BEGIN_DECLS - -GtkSourceStyleSchemeManager * - xed_get_style_scheme_manager (void); - -/* Returns a sorted list of style schemes */ -GSList *xed_style_scheme_manager_list_schemes_sorted - (GtkSourceStyleSchemeManager *manager); - -/* - * Non exported functions - */ -gboolean _xed_style_scheme_manager_scheme_is_xed_user_scheme - (GtkSourceStyleSchemeManager *manager, - const gchar *scheme_id); - -const gchar *_xed_style_scheme_manager_install_scheme - (GtkSourceStyleSchemeManager *manager, - const gchar *fname); - -gboolean _xed_style_scheme_manager_uninstall_scheme - (GtkSourceStyleSchemeManager *manager, - const gchar *id); - -G_END_DECLS - -#endif /* __XED_STYLE_SCHEME_MANAGER_H__ */