Revision 3269 src/template.c

View differences:

template.c
32 32

  
33 33
static GSList *template_list;
34 34

  
35
static Template *template_load(gchar *filename)
35
static Template *template_load(gchar *filename, guint tmplid)
36 36
{
37 37
	Template *tmpl;
38 38
	FILE *fp;
......
44 44
	}
45 45

  
46 46
	tmpl = g_new(Template, 1);
47
	tmpl->tmplid = tmplid;
47 48
	tmpl->name = NULL;
48 49
	tmpl->to = NULL;
49 50
	tmpl->cc = NULL;
......
108 109
	g_slist_free(tmpl_list);
109 110
}
110 111

  
112
static gint template_compare_id(gconstpointer a, gconstpointer b)
113
{
114
	Template *ta, *tb;
115

  
116
	ta = a;
117
	tb = b;
118
	return (ta->tmplid - tb->tmplid);
119
}
120

  
111 121
GSList *template_read_config(void)
112 122
{
113 123
	const gchar *path;
......
116 126
	const gchar *dir_name;
117 127
	struct stat s;
118 128
	Template *tmpl;
129
	guint tmplid;
119 130
	GSList *tmpl_list = NULL;
120 131

  
121 132
	path = get_template_dir();
......
133 144
	}
134 145

  
135 146
	while ((dir_name = g_dir_read_name(dir)) != NULL) {
147
		tmplid = atoi(dir_name);
148
		if (tmplid <= 0) {
149
			continue;
150
		}
151

  
136 152
		filename = g_strconcat(path, G_DIR_SEPARATOR_S,
137 153
				       dir_name, NULL);
138 154

  
139 155
		if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
140 156
			debug_print("%s:%d %s is not an ordinary file\n",
141 157
				    __FILE__, __LINE__, filename);
158
			g_free(filename);
142 159
			continue;
143 160
		}
144 161

  
145
		tmpl = template_load(filename);
162
		tmpl = template_load(filename, tmplid);
146 163
		if (tmpl)
147
			tmpl_list = g_slist_append(tmpl_list, tmpl);
164
			tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl,
165
							  template_compare_id);
148 166

  
149 167
		g_free(filename);
150 168
	}
......
160 178
	GSList *cur;
161 179
	Template *tmpl;
162 180
	FILE *fp;
163
	gint tmpl_num;
164 181

  
165 182
	debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
166 183

  
......
177 194

  
178 195
	remove_all_files(path);
179 196

  
180
	for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
181
	     cur = cur->next, tmpl_num++) {
197
	for (cur = tmpl_list; cur != NULL; cur = cur->next) {
182 198
		gchar *filename;
183 199

  
184 200
		tmpl = cur->data;
185 201

  
186 202
		filename = g_strconcat(path, G_DIR_SEPARATOR_S,
187
				       itos(tmpl_num), NULL);
203
				       itos(tmpl->tmplid), NULL);
188 204

  
189 205
		if ((fp = g_fopen(filename, "wb")) == NULL) {
190 206
			FILE_OP_ERROR(filename, "fopen");

Also available in: Unified diff