1  /*
2  *      templates.c
3  *
4  *      Copyright 2009-2013 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
5  *
6  *      This program is free software; you can redistribute it and/or modify
7  *      it under the terms of the GNU General Public License as published by
8  *      the Free Software Foundation; either version 2 of the License, or
9  *      (at your option) any later version.
10  *
11  *      This program is distributed in the hope that it will be useful,
12  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *      GNU General Public License for more details.
15  *
16  *      You should have received a copy of the GNU General Public License
17  *      along with this program; if not, write to the Free Software
18  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *      MA 02110-1301, USA.
20  */
21 
22 #include "templates.h"
23 
glatex_get_template_from_file(gchar * filepath)24 GString *glatex_get_template_from_file(gchar *filepath)
25 {
26 	gchar *template = NULL;
27 	GString *return_value = NULL;
28 
29 	if (filepath == NULL) return NULL;
30 
31 	g_file_get_contents(filepath, &template, NULL, NULL);
32 
33 	return_value = g_string_new(template);
34 	if (template != NULL)
35 		g_free(template);
36 	return return_value;
37 }
38 
39 
glatex_init_cutom_template_item(gchar * file,GPtrArray * array)40 static void glatex_init_cutom_template_item(gchar *file, GPtrArray *array)
41 {
42 	TemplateEntry *template = NULL;
43 	gchar *tmp = NULL;
44 
45 	/* Return if its not a searched file */
46 	if (g_str_has_suffix(file,".gtl") == FALSE)
47 		return;
48 
49 	template = g_new0(TemplateEntry, 1);
50 
51 	template->filepath = g_strdup(file);
52 
53 	tmp = g_path_get_basename(file);
54 	template->label = utils_remove_ext_from_filename(tmp);
55 	g_free(tmp);
56 
57 	/* Adding struct to array */
58 	template->template = glatex_get_template_from_file(file);
59 	g_ptr_array_add(array, template);
60 }
61 
62 
glatex_init_custom_templates(void)63 GPtrArray* glatex_init_custom_templates(void)
64 {
65 	gchar *tmp_basedir = NULL;
66 	GSList *file_list = NULL;
67 	GPtrArray *templates = NULL;
68 
69 	/* Creating up config dir for checking for templates */
70 	tmp_basedir = g_strconcat(geany->app->configdir,
71 			G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
72 			"LaTeX", G_DIR_SEPARATOR_S, NULL);
73 
74 	/* Putting all files in configdir to a file list */
75 	file_list = utils_get_file_list_full(tmp_basedir, TRUE, TRUE, NULL);
76 
77 	/* Init GPtrArray */
78 	templates = g_ptr_array_new();
79 
80 	/* Iterating on all list items */
81 	g_slist_foreach(file_list, (GFunc)glatex_init_cutom_template_item, templates);
82  	g_slist_foreach(file_list, (GFunc) g_free, NULL);
83 	g_slist_free(file_list);
84 
85 	g_free(tmp_basedir);
86 	return templates;
87 }
88 
89 /* Frees all elelements of struct */
glatex_free_template_entry(TemplateEntry * template,G_GNUC_UNUSED gpointer * data)90 void glatex_free_template_entry(TemplateEntry *template, G_GNUC_UNUSED gpointer *data){
91 	if (template->label != NULL)
92 		g_free(template->filepath);
93 	if (template->label != NULL)
94 		g_string_free(template->template, TRUE);
95 }
96 
97 
glatex_add_templates_to_combobox(GPtrArray * templates,GtkWidget * combobox)98 void glatex_add_templates_to_combobox(GPtrArray *templates, GtkWidget *combobox)
99 {
100 	guint i;
101 	TemplateEntry *tmp;
102 	for (i = 0; i < templates->len; i++)
103 	{
104 		tmp = g_ptr_array_index(templates,i);
105 		gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combobox),
106 			tmp->label);
107 	}
108 }
109