Remove the xed style scheme manager instance

f5d46ef3cc
This commit is contained in:
JosephMcc 2017-01-21 01:31:05 -08:00
parent 1d6f119b04
commit 4f277063f0
9 changed files with 365 additions and 520 deletions

View File

@ -60,7 +60,6 @@ NOINST_H_FILES = \
xed-session.h \ xed-session.h \
xed-settings.h \ xed-settings.h \
xed-status-combo-box.h \ xed-status-combo-box.h \
xed-style-scheme-manager.h \
xed-tab-label.h \ xed-tab-label.h \
xedtextregion.h \ xedtextregion.h \
xed-ui.h \ xed-ui.h \
@ -142,7 +141,6 @@ libxed_c_files = \
xed-searchbar.c \ xed-searchbar.c \
xed-statusbar.c \ xed-statusbar.c \
xed-status-combo-box.c \ xed-status-combo-box.c \
xed-style-scheme-manager.c \
xed-tab.c \ xed-tab.c \
xed-tab-label.c \ xed-tab-label.c \
xed-utils.c \ xed-utils.c \

View File

@ -35,17 +35,19 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gtksourceview/gtksource.h> #include <gtksourceview/gtksource.h>
#include <libpeas-gtk/peas-gtk-plugin-manager.h> #include <libpeas-gtk/peas-gtk-plugin-manager.h>
#include <gtksourceview/gtksourcestyleschememanager.h>
#include "xed-preferences-dialog.h" #include "xed-preferences-dialog.h"
#include "xed-utils.h" #include "xed-utils.h"
#include "xed-debug.h" #include "xed-debug.h"
#include "xed-document.h" #include "xed-document.h"
#include "xed-style-scheme-manager.h"
#include "xed-help.h" #include "xed-help.h"
#include "xed-dirs.h" #include "xed-dirs.h"
#include "xed-settings.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); 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 static void
set_buttons_sensisitivity_according_to_scheme (XedPreferencesDialog *dlg, set_buttons_sensisitivity_according_to_scheme (XedPreferencesDialog *dlg,
const gchar *scheme_id) const gchar *scheme_id)
{ {
gboolean editable; gboolean editable;
editable = (scheme_id != NULL) && editable = ((scheme_id != NULL) && is_xed_user_style_scheme (scheme_id));
_xed_style_scheme_manager_scheme_is_xed_user_scheme (xed_get_style_scheme_manager (), scheme_id);
gtk_widget_set_sensitive (dlg->priv->uninstall_scheme_button, editable); gtk_widget_set_sensitive (dlg->priv->uninstall_scheme_button, editable);
} }
@ -465,9 +489,10 @@ static const gchar *
ensure_color_scheme_id (XedPreferencesDialog *dlg, ensure_color_scheme_id (XedPreferencesDialog *dlg,
const gchar *id) const gchar *id)
{ {
GtkSourceStyleSchemeManager *manager;
GtkSourceStyleScheme *scheme = NULL; GtkSourceStyleScheme *scheme = NULL;
GtkSourceStyleSchemeManager *manager = xed_get_style_scheme_manager ();
manager = gtk_source_style_scheme_manager_get_default ();
if (id == NULL) if (id == NULL)
{ {
gchar *pref_id; gchar *pref_id;
@ -496,15 +521,13 @@ ensure_color_scheme_id (XedPreferencesDialog *dlg,
return gtk_source_style_scheme_get_id (scheme); 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 * static const gchar *
populate_color_scheme_list (XedPreferencesDialog *dlg, populate_color_scheme_list (XedPreferencesDialog *dlg,
const gchar *def_id) const gchar *def_id)
{ {
GSList *schemes; GtkSourceStyleSchemeManager *manager;
GSList *l; const gchar * const *ids;
gint i;
gtk_list_store_clear (dlg->priv->schemes_treeview_model); gtk_list_store_clear (dlg->priv->schemes_treeview_model);
@ -516,47 +539,262 @@ populate_color_scheme_list (XedPreferencesDialog *dlg,
return NULL; return NULL;
} }
schemes = xed_style_scheme_manager_list_schemes_sorted (xed_get_style_scheme_manager ()); manager = gtk_source_style_scheme_manager_get_default ();
l = schemes; ids = gtk_source_style_scheme_manager_get_scheme_ids (manager);
while (l != NULL) for (i = 0; ids[i] != NULL; i++)
{ {
GtkSourceStyleScheme *scheme; GtkSourceStyleScheme *scheme;
const gchar *id;
const gchar *name; const gchar *name;
const gchar *description; const gchar *description;
GtkTreeIter iter; GtkTreeIter iter;
scheme = GTK_SOURCE_STYLE_SCHEME (l->data); scheme = gtk_source_style_scheme_manager_get_scheme (manager, ids[i]);
id = gtk_source_style_scheme_get_id (scheme);
name = gtk_source_style_scheme_get_name (scheme); name = gtk_source_style_scheme_get_name (scheme);
description = gtk_source_style_scheme_get_description (scheme); description = gtk_source_style_scheme_get_description (scheme);
gtk_list_store_append (dlg->priv->schemes_treeview_model, &iter); gtk_list_store_append (dlg->priv->schemes_treeview_model, &iter);
gtk_list_store_set (dlg->priv->schemes_treeview_model, gtk_list_store_set (dlg->priv->schemes_treeview_model,
&iter, &iter,
ID_COLUMN, id, ID_COLUMN, ids[i],
NAME_COLUMN, name, NAME_COLUMN, name,
DESC_COLUMN, description, DESC_COLUMN, description,
-1); -1);
g_return_val_if_fail (def_id != NULL, NULL); g_return_val_if_fail (def_id != NULL, NULL);
if (strcmp (id, def_id) == 0) if (strcmp (ids[i], def_id) == 0)
{ {
GtkTreeSelection *selection; GtkTreeSelection *selection;
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dlg->priv->schemes_treeview)); selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dlg->priv->schemes_treeview));
gtk_tree_selection_select_iter (selection, &iter); gtk_tree_selection_select_iter (selection, &iter);
} }
l = g_slist_next (l);
} }
g_slist_free (schemes);
return def_id; 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 static void
add_scheme_chooser_response_cb (GtkDialog *chooser, add_scheme_chooser_response_cb (GtkDialog *chooser,
gint res_id, gint res_id,
@ -573,11 +811,13 @@ add_scheme_chooser_response_cb (GtkDialog *chooser,
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser)); filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
if (filename == NULL) if (filename == NULL)
{
return; return;
}
gtk_widget_hide (GTK_WIDGET (chooser)); 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); g_free (filename);
if (scheme_id == NULL) 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); 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); xed_warning (GTK_WINDOW (dlg), _("Could not remove color scheme \"%s\"."), name);
} }

View File

@ -37,6 +37,7 @@
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <libpeas/peas-extension-set.h> #include <libpeas/peas-extension-set.h>
#include <gtksourceview/gtksourcestyleschememanager.h>
#include "xed-app.h" #include "xed-app.h"
#include "xed-commands.h" #include "xed-commands.h"
@ -351,12 +352,23 @@ extension_removed (PeasExtensionSet *extensions,
static void static void
xed_app_init (XedApp *app) xed_app_init (XedApp *app)
{ {
GtkSourceStyleSchemeManager *manager;
app->priv = XED_APP_GET_PRIVATE (app); app->priv = XED_APP_GET_PRIVATE (app);
load_accels (); load_accels ();
app->priv->settings = xed_settings_new (); app->priv->settings = xed_settings_new ();
app->priv->window_settings = g_settings_new ("org.x.editor.state.window"); 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 ()), app->priv->extensions = peas_extension_set_new (PEAS_ENGINE (xed_plugins_engine_get_default ()),
XED_TYPE_APP_ACTIVATABLE, "app", app, NULL); XED_TYPE_APP_ACTIVATABLE, "app", app, NULL);

View File

@ -27,7 +27,8 @@
#include "xed-dirs.h" #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;
@ -36,7 +37,8 @@ gchar* xed_dirs_get_user_config_dir(void)
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;
@ -45,7 +47,16 @@ gchar* xed_dirs_get_user_cache_dir(void)
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 *user_style_dir;
user_style_dir = g_build_filename (g_get_user_data_dir (), "xed", "styles", NULL);
}
gchar *
xed_dirs_get_user_plugins_dir (void)
{ {
gchar* plugin_dir; gchar* plugin_dir;
@ -54,7 +65,8 @@ gchar* xed_dirs_get_user_plugins_dir(void)
return plugin_dir; return plugin_dir;
} }
gchar* xed_dirs_get_user_accels_file(void) gchar *
xed_dirs_get_user_accels_file (void)
{ {
gchar* accels = NULL; gchar* accels = NULL;
gchar *config_dir = NULL; gchar *config_dir = NULL;
@ -67,22 +79,26 @@ gchar* xed_dirs_get_user_accels_file(void)
return accels; return accels;
} }
gchar* xed_dirs_get_xed_data_dir(void) gchar *
xed_dirs_get_xed_data_dir (void)
{ {
return g_build_filename(DATADIR, "xed", NULL); return g_build_filename(DATADIR, "xed", NULL);
} }
gchar* xed_dirs_get_xed_locale_dir(void) gchar *
xed_dirs_get_xed_locale_dir (void)
{ {
return g_build_filename(DATADIR, "locale", NULL); return g_build_filename(DATADIR, "locale", NULL);
} }
gchar* xed_dirs_get_xed_lib_dir(void) gchar *
xed_dirs_get_xed_lib_dir (void)
{ {
return g_build_filename(LIBDIR, "xed", NULL); return g_build_filename(LIBDIR, "xed", NULL);
} }
gchar* xed_dirs_get_xed_plugins_dir(void) gchar *
xed_dirs_get_xed_plugins_dir (void)
{ {
gchar* lib_dir; gchar* lib_dir;
gchar* plugin_dir; gchar* plugin_dir;
@ -95,7 +111,8 @@ gchar* xed_dirs_get_xed_plugins_dir(void)
return plugin_dir; return plugin_dir;
} }
gchar* xed_dirs_get_xed_plugins_data_dir (void) gchar *
xed_dirs_get_xed_plugins_data_dir (void)
{ {
gchar* data_dir; gchar* data_dir;
gchar* plugin_data_dir; gchar* plugin_data_dir;
@ -108,7 +125,8 @@ gchar* xed_dirs_get_xed_plugins_data_dir (void)
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* datadir;
gchar* ui_file; gchar* ui_file;

View File

@ -32,6 +32,8 @@ 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_styles_dir (void);
gchar *xed_dirs_get_user_plugins_dir (void); gchar *xed_dirs_get_user_plugins_dir (void);
gchar *xed_dirs_get_user_accels_file (void); gchar *xed_dirs_get_user_accels_file (void);

View File

@ -40,13 +40,13 @@
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gtksourceview/gtksource.h> #include <gtksourceview/gtksource.h>
#include <gtksourceview/gtksourcestyleschememanager.h>
#include "xed-settings.h" #include "xed-settings.h"
#include "xed-document.h" #include "xed-document.h"
#include "xed-debug.h" #include "xed-debug.h"
#include "xed-utils.h" #include "xed-utils.h"
#include "xed-language-manager.h" #include "xed-language-manager.h"
#include "xed-style-scheme-manager.h"
#include "xed-document-loader.h" #include "xed-document-loader.h"
#include "xed-document-saver.h" #include "xed-document-saver.h"
#include "xed-marshal.h" #include "xed-marshal.h"
@ -730,11 +730,11 @@ set_encoding (XedDocument *doc,
static GtkSourceStyleScheme * static GtkSourceStyleScheme *
get_default_style_scheme (GSettings *editor_settings) get_default_style_scheme (GSettings *editor_settings)
{ {
GtkSourceStyleSchemeManager *manager;
gchar *scheme_id; gchar *scheme_id;
GtkSourceStyleScheme *def_style; 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); scheme_id = g_settings_get_string (editor_settings, XED_SETTINGS_SCHEME);
def_style = gtk_source_style_scheme_manager_get_scheme (manager, scheme_id); def_style = gtk_source_style_scheme_manager_get_scheme (manager, scheme_id);

View File

@ -23,6 +23,8 @@
#include <string.h> #include <string.h>
#include <gtksourceview/gtksourcestyleschememanager.h>
#include "xed-settings.h" #include "xed-settings.h"
#include "xed-app.h" #include "xed-app.h"
#include "xed-debug.h" #include "xed-debug.h"
@ -30,7 +32,6 @@
#include "xed-window.h" #include "xed-window.h"
#include "xed-notebook.h" #include "xed-notebook.h"
#include "xed-plugins-engine.h" #include "xed-plugins-engine.h"
#include "xed-style-scheme-manager.h"
#include "xed-dirs.h" #include "xed-dirs.h"
#include "xed-utils.h" #include "xed-utils.h"
#include "xed-window-private.h" #include "xed-window-private.h"
@ -187,6 +188,7 @@ on_scheme_changed (GSettings *settings,
const gchar *key, const gchar *key,
XedSettings *xs) XedSettings *xs)
{ {
GtkSourceStyleSchemeManager *manager;
GtkSourceStyleScheme *style; GtkSourceStyleScheme *style;
gchar *scheme; gchar *scheme;
GList *docs; GList *docs;
@ -202,13 +204,14 @@ on_scheme_changed (GSettings *settings,
g_free (xs->priv->old_scheme); g_free (xs->priv->old_scheme);
xs->priv->old_scheme = 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) if (style == NULL)
{ {
g_warning ("Default style scheme '%s' not found, falling back to 'classic'", scheme); 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) if (style == NULL)
{ {

View File

@ -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 <config.h>
#endif
#include <string.h>
#include <errno.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>
#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;
}

View File

@ -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 <gtksourceview/gtksourcestyleschememanager.h>
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__ */