[latexila] Templates: code clean-up
- From: SÃbastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [latexila] Templates: code clean-up
- Date: Sat, 10 Mar 2012 19:58:54 +0000 (UTC)
commit 38105bbc86e34e1e98bba96ea76f90813457e0c9
Author: SÃbastien Wilmet <swilmet src gnome org>
Date: Sat Mar 10 20:59:54 2012 +0100
Templates: code clean-up
src/templates.vala | 245 ++++++++++++++++++++++++----------------------------
src/utils.vala | 60 +++++++++++++-
2 files changed, 172 insertions(+), 133 deletions(-)
---
diff --git a/src/templates.vala b/src/templates.vala
index 2719b8f..ff0e475 100644
--- a/src/templates.vala
+++ b/src/templates.vala
@@ -31,6 +31,9 @@ public class Templates : GLib.Object
private int _nb_personal_templates;
+ // The contents of the personal templates are saved in the user data directory.
+ // The first personal template is 0.tex, the second 1.tex, and so on.
+ // The names and the icons of the personal templates are saved in an rc file.
private File _data_dir;
private File _rc_file;
@@ -137,8 +140,9 @@ public class Templates : GLib.Object
return _data_dir.get_child (filename);
}
+
/*************************************************************************/
- // Add templates: from string, from file, ...
+ // Add and delete templates, save rc file.
private void add_template_from_string (ListStore store, string name,
string icon_id, string contents)
@@ -156,19 +160,10 @@ public class Templates : GLib.Object
private bool add_template_from_file (ListStore store, string name,
string icon_id, File file)
{
- uint8[] chars;
-
- try
- {
- file.load_contents (null, out chars, null);
- }
- catch (Error e)
- {
- warning ("Impossible to load the template '%s': %s", name, e.message);
+ string? contents = Utils.load_file (file);
+ if (contents == null)
return false;
- }
- string contents = (string) (owned) chars;
add_template_from_string (store, name, icon_id, contents);
return true;
@@ -199,45 +194,6 @@ public class Templates : GLib.Object
warning ("Template '%s' not found.", name);
}
- public IconView create_icon_view_default_templates ()
- {
- return create_icon_view (_default_store);
- }
-
- public IconView create_icon_view_personal_templates ()
- {
- return create_icon_view (_personal_store);
- }
-
- private IconView create_icon_view (ListStore store)
- {
- IconView icon_view = new IconView.with_model (store);
- icon_view.set_selection_mode (SelectionMode.SINGLE);
-
- CellRendererPixbuf pixbuf_renderer = new CellRendererPixbuf ();
- pixbuf_renderer.stock_size = IconSize.DIALOG;
- pixbuf_renderer.xalign = (float) 0.5;
- pixbuf_renderer.yalign = (float) 1.0;
- icon_view.pack_start (pixbuf_renderer, false);
- icon_view.set_attributes (pixbuf_renderer,
- "icon-name", TemplateColumn.PIXBUF,
- null);
-
- // We also use a CellRenderer for the text column, because with set_text_column()
- // the text is not centered (when a CellRenderer is used for the pixbuf).
- CellRendererText text_renderer = new CellRendererText ();
- text_renderer.alignment = Pango.Alignment.CENTER;
- text_renderer.wrap_mode = Pango.WrapMode.WORD;
- text_renderer.xalign = (float) 0.5;
- text_renderer.yalign = (float) 0.0;
- icon_view.pack_end (text_renderer, false);
- icon_view.set_attributes (text_renderer,
- "text", TemplateColumn.NAME,
- null);
-
- return icon_view;
- }
-
public void delete_personal_template (TreePath template_path)
{
/* Delete the template from the personal store */
@@ -277,59 +233,7 @@ public class Templates : GLib.Object
save_rc_file ();
File file = get_personal_template_file (_nb_personal_templates - 1);
-
- try
- {
- // check if parent directories exist, if not, create it
- File parent = file.get_parent ();
- if (parent != null && ! parent.query_exists ())
- parent.make_directory_with_parents ();
-
- file.replace_contents (contents.data, null, false,
- FileCreateFlags.NONE, null);
- }
- catch (Error e)
- {
- warning ("Impossible to save the templates: %s", e.message);
- }
- }
-
- public string get_icon_id (TreePath default_template_path)
- {
- TreeModel model = (TreeModel) _default_store;
- TreeIter iter;
- model.get_iter (out iter, default_template_path);
-
- string icon_id;
- model.get (iter, TemplateColumn.ICON_ID, out icon_id);
-
- return icon_id;
- }
-
- public string get_default_template_contents (TreePath path)
- {
- return get_template_contents (_default_store, path);
- }
-
- public string get_personal_template_contents (TreePath path)
- {
- return get_template_contents (_personal_store, path);
- }
-
- private string get_template_contents (ListStore store, TreePath path)
- {
- TreeIter iter;
- TreeModel model = store as TreeModel;
- if (! model.get_iter (out iter, path))
- {
- warning ("Failed to get template contents");
- return "";
- }
-
- string contents;
- model.get (iter, TemplateColumn.CONTENTS, out contents);
-
- return contents;
+ Utils.save_file (file, contents);
}
public void save_rc_file ()
@@ -340,47 +244,35 @@ public class Templates : GLib.Object
return;
}
- // the names and the icons of all personal templates
+ // The names and the icons of all personal templates.
string[] names = new string[_nb_personal_templates];
string[] icons = new string[_nb_personal_templates];
- // traverse the list store
+ // Traverse the list store.
TreeIter iter;
- TreeModel model = (TreeModel) _personal_store;
+ TreeModel model = _personal_store as TreeModel;
bool valid_iter = model.get_iter_first (out iter);
- int i = 0;
+ int template_num = 0;
+
while (valid_iter)
{
model.get (iter,
- TemplateColumn.NAME, out names[i],
- TemplateColumn.ICON_ID, out icons[i],
- -1);
+ TemplateColumn.NAME, out names[template_num],
+ TemplateColumn.ICON_ID, out icons[template_num]);
+
valid_iter = model.iter_next (ref iter);
- i++;
+ template_num++;
}
- /* save the rc file */
- try
- {
- KeyFile key_file = new KeyFile ();
- key_file.set_string_list (Config.APP_NAME, "names", names);
- key_file.set_string_list (Config.APP_NAME, "icons", icons);
-
- string key_file_data = key_file.to_data ();
+ // Contents of the rc file
+ KeyFile key_file = new KeyFile ();
+ key_file.set_string_list (Config.APP_NAME, "names", names);
+ key_file.set_string_list (Config.APP_NAME, "icons", icons);
- // check if parent directories exist, if not, create it
- // TODO move this in a function in Utils
- File parent = _rc_file.get_parent ();
- if (parent != null && ! parent.query_exists ())
- parent.make_directory_with_parents ();
+ string key_file_data = key_file.to_data ();
- _rc_file.replace_contents (key_file_data.data, null, false,
- FileCreateFlags.NONE, null);
- }
- catch (Error e)
- {
- warning ("Impossible to save the templates: %s", e.message);
- }
+ // Save the rc file
+ Utils.save_file (_rc_file, key_file_data);
}
// For compatibility reasons. 'icon_id' is the string stored in the rc file,
@@ -414,4 +306,93 @@ public class Templates : GLib.Object
return_val_if_reached (null);
}
}
+
+
+ /*************************************************************************/
+ // Get templates data: icon id, contents.
+
+ public string get_icon_id (TreePath default_template_path)
+ {
+ TreeModel model = _default_store as TreeModel;
+ TreeIter iter;
+ if (! model.get_iter (out iter, default_template_path))
+ {
+ warning ("Failed to get template icon id");
+ return "";
+ }
+
+ string icon_id;
+ model.get (iter, TemplateColumn.ICON_ID, out icon_id);
+
+ return icon_id;
+ }
+
+ public string get_default_template_contents (TreePath path)
+ {
+ return get_template_contents (_default_store, path);
+ }
+
+ public string get_personal_template_contents (TreePath path)
+ {
+ return get_template_contents (_personal_store, path);
+ }
+
+ private string get_template_contents (ListStore store, TreePath path)
+ {
+ TreeIter iter;
+ TreeModel model = store as TreeModel;
+ if (! model.get_iter (out iter, path))
+ {
+ warning ("Failed to get template contents");
+ return "";
+ }
+
+ string contents;
+ model.get (iter, TemplateColumn.CONTENTS, out contents);
+
+ return contents;
+ }
+
+
+ /*************************************************************************/
+ // Create icon view for the dialog windows.
+
+ public IconView create_icon_view_default_templates ()
+ {
+ return create_icon_view (_default_store);
+ }
+
+ public IconView create_icon_view_personal_templates ()
+ {
+ return create_icon_view (_personal_store);
+ }
+
+ private IconView create_icon_view (ListStore store)
+ {
+ IconView icon_view = new IconView.with_model (store);
+ icon_view.set_selection_mode (SelectionMode.SINGLE);
+
+ CellRendererPixbuf pixbuf_renderer = new CellRendererPixbuf ();
+ pixbuf_renderer.stock_size = IconSize.DIALOG;
+ pixbuf_renderer.xalign = (float) 0.5;
+ pixbuf_renderer.yalign = (float) 1.0;
+ icon_view.pack_start (pixbuf_renderer, false);
+ icon_view.set_attributes (pixbuf_renderer,
+ "icon-name", TemplateColumn.PIXBUF,
+ null);
+
+ // We also use a CellRenderer for the text column, because with set_text_column()
+ // the text is not centered (when a CellRenderer is used for the pixbuf).
+ CellRendererText text_renderer = new CellRendererText ();
+ text_renderer.alignment = Pango.Alignment.CENTER;
+ text_renderer.wrap_mode = Pango.WrapMode.WORD;
+ text_renderer.xalign = (float) 0.5;
+ text_renderer.yalign = (float) 0.0;
+ icon_view.pack_end (text_renderer, false);
+ icon_view.set_attributes (text_renderer,
+ "text", TemplateColumn.NAME,
+ null);
+
+ return icon_view;
+ }
}
diff --git a/src/utils.vala b/src/utils.vala
index 8bdf3cf..59ba1ef 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -1,7 +1,7 @@
/*
* This file is part of LaTeXila.
*
- * Copyright  2010-2011 SÃbastien Wilmet
+ * Copyright  2010-2012 SÃbastien Wilmet
*
* LaTeXila is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -164,6 +164,64 @@ namespace Utils
}
}
+ public bool create_parent_directories (File file)
+ {
+ File parent = file.get_parent ();
+
+ if (parent == null || parent.query_exists ())
+ return true;
+
+ try
+ {
+ parent.make_directory_with_parents ();
+ }
+ catch (Error e)
+ {
+ warning ("Failed to create directory parents for the file '%s': %s",
+ file.get_parse_name (), e.message);
+ return false;
+ }
+
+ return true;
+ }
+
+ public bool save_file (File file, string contents, bool make_backup = false)
+ {
+ if (! create_parent_directories (file))
+ return false;
+
+ try
+ {
+ file.replace_contents (contents.data, null, make_backup,
+ FileCreateFlags.NONE, null);
+ }
+ catch (Error e)
+ {
+ warning ("Failed to save the file '%s': %s", file.get_parse_name (),
+ e.message);
+ return false;
+ }
+
+ return true;
+ }
+
+ // Retruns null on error.
+ public string? load_file (File file)
+ {
+ try
+ {
+ uint8[] chars;
+ file.load_contents (null, out chars, null);
+ return (string) (owned) chars;
+ }
+ catch (Error e)
+ {
+ warning ("Failed to load the file '%s': %s", file.get_parse_name (),
+ e.message);
+ return null;
+ }
+ }
+
// origin can be equal to common_dir, but target must be different
public string? get_relative_path (File origin, File target, File common_dir)
{
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]