1 /* audio.c
2  * Copyright (C) 2004, 2005 Sylvain Cresto <scresto@gmail.com>
3  *
4  * This file is part of graveman!
5  *
6  * graveman! 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, or
9  * (at your option) any later version.
10  *
11  * graveman! 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 GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with program; see the file COPYING. If not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19  * MA 02111-1307, USA.
20  *
21  * URL: http://www.nongnu.org/graveman/
22  *
23  */
24 
25 #include "graveman.h"
26 
27 #define THEME_NAME    "NAME"
28 #define THEME_DESC    "DESC"
29 #define THEME_AUTHOR  "AUTHOR"
30 #define THEME_VERSION "VERSION"
31 #define THEME_DATE    "DATE"
32 #define THEME_URL     "URL"
33 #define THEME_ITEM    6
34 
free_theme(Ttheme * Atheme)35 Ttheme *free_theme(Ttheme *Atheme)
36 {
37   g_free(Atheme->path);
38   g_free(Atheme->name);
39   g_free(Atheme->desc);
40   g_free(Atheme->author);
41   g_free(Atheme->date);
42   g_free(Atheme->url);
43   g_free(Atheme);
44 
45   return NULL;
46 }
47 
48 
49 /* on decompose une locale fr_FR.utf8@euro */
split_locale()50 GSList *split_locale()
51 {
52   GSList *Lcurlocale = NULL;
53   gchar *s = Glocale;
54   gchar Lbuf[_BUF_SIZE];
55 #ifdef ENABLE_NLS
56   if (s) {
57     Lcurlocale = g_slist_append(Lcurlocale, g_strconcat("[", Glocale, "]=", NULL));
58 
59     g_strlcpy(Lbuf, Glocale, _BUF_SIZE-1);
60 
61     if ((s=strchr(Lbuf, '@'))) {
62       *s=0;
63       Lcurlocale = g_slist_append(Lcurlocale, g_strconcat("[", Lbuf, "]=", NULL));
64     }
65 
66     if (*Lbuf && ((s=strchr(Lbuf, '.')))) {
67       *s=0;
68       Lcurlocale = g_slist_append(Lcurlocale, g_strconcat("[", Lbuf, "]=", NULL));
69 
70       if (*Lbuf && ((s=strchr(Lbuf, '_')))) {
71         *s=0;
72         Lcurlocale = g_slist_append(Lcurlocale, g_strconcat("[", Lbuf, "]=", NULL));
73       }
74     }
75   }
76 #endif
77   Lcurlocale = g_slist_append(Lcurlocale, g_strdup("="));
78 
79   return Lcurlocale;
80 }
81 
free_locale(GSList * Alistlocale)82 GSList *free_locale(GSList *Alistlocale)
83 {
84   GSList *Lcurlocale = Alistlocale;
85   gpointer Ldata;
86 
87   while (Lcurlocale) {
88     if ((Ldata = Lcurlocale->data)) g_free(Ldata);
89     Lcurlocale = g_slist_next(Lcurlocale);
90   }
91 
92   g_slist_free(Alistlocale);
93 
94   return NULL;
95 }
96 
97 /* allocation theme */
read_theme_desc(const gchar * Afile,GError ** Aerror)98 Ttheme *read_theme_desc(const gchar *Afile, GError **Aerror)
99 {
100   gchar *Lbuf;
101   gchar **Larrbuf;
102   gint i;
103   gchar *s, *Ldata;
104   Ttheme *Ltheme = g_malloc0(sizeof(Ttheme));
105   GSList *Llistlocale = split_locale();
106   GSList *Lcurlocale;
107   gint Ltrouve = 0;
108 
109   if (!g_file_get_contents(Afile, &Lbuf, NULL, Aerror)) return NULL;
110   Larrbuf = g_strsplit(Lbuf, "\n", 0);
111   g_free(Lbuf);
112 
113 
114   for (Lcurlocale = Llistlocale; Lcurlocale; Lcurlocale = g_slist_next(Lcurlocale)) {
115     if (!(Ldata = (gchar *)Lcurlocale->data)) continue;
116 
117     for (i=0; Larrbuf[i]; i++) {
118       s=ltrim(Larrbuf[i]);
119       if (*s==';' || *s=='#' || !*s) continue;
120 
121       if (!Ltheme->name && !strncmp(s, THEME_NAME, strlen(THEME_NAME)) &&
122           ((!*Ldata) || strstr(s, Ldata))) {
123         Ltheme->name = g_strdup(strchr(s, '=')+1);
124         Ltrouve ++;
125       } else if (!Ltheme->desc && !strncmp(s, THEME_DESC, strlen(THEME_DESC)) &&
126           ((!*Ldata) || strstr(s, Ldata))) {
127         Ltheme->desc = g_strdup(strchr(s, '=')+1);
128         Ltrouve ++;
129       } else if (!Ltheme->author && !strncmp(s, THEME_AUTHOR, strlen(THEME_AUTHOR)) &&
130           ((!*Ldata) || strstr(s, Ldata))) {
131         Ltheme->author = g_strdup(strchr(s, '=')+1);
132         Ltrouve ++;
133       } else if (!Ltheme->version && !strncmp(s, THEME_VERSION, strlen(THEME_VERSION)) &&
134           ((!*Ldata) || strstr(s, Ldata))) {
135         Ltheme->version = atoi(ltrim(strchr(s, '=')+1));
136         Ltrouve ++;
137       } else if (!Ltheme->date && !strncmp(s, THEME_DATE, strlen(THEME_DATE)) &&
138           ((!*Ldata) || strstr(s, Ldata))) {
139         Ltheme->date = g_strdup(ltrim(strchr(s, '=')+1));
140         Ltrouve ++;
141       } else if (!Ltheme->url && !strncmp(s, THEME_URL, strlen(THEME_URL)) &&
142           ((!*Ldata) || strstr(s, Ldata))) {
143         Ltheme->url = g_strdup(ltrim(strchr(s, '=')+1));
144         Ltrouve ++;
145       }
146 
147       if (Ltrouve == THEME_ITEM) break;
148     }
149   }
150   g_strfreev(Larrbuf);
151 
152   Llistlocale = free_locale(Llistlocale);
153 
154   /* un theme valide doit au moins avoir un nom .. */
155   if (Ltheme->name && *Ltheme->name) {
156     Ltheme->path = g_strdup(Afile);
157     return Ltheme;
158   } else {
159     return free_theme(Ltheme);
160   }
161 }
162 
163 /* retourne un hash contenant un chemin d'acces vers toutes les images */
get_imagespath_plus_from_theme(Timages * Aimages,gchar * Atheme,gchar * Aplus,GError ** Aerror)164 GHashTable *get_imagespath_plus_from_theme(Timages *Aimages, gchar *Atheme, gchar *Aplus, GError **Aerror)
165 {
166   GHashTable *Lhashimagespath = _hash();
167   GSList *Lthemesemp = get_data_path();
168   GSList *Lcur;
169   Timages *Luneimage;
170   gchar Lpath[MAXPATHLEN];
171   gboolean Ltrouve;
172   gboolean Laumoinsun = FALSE;
173 
174   for (Luneimage = Aimages; Luneimage->code; Luneimage++) {
175     _DEB("on cherche l'image [%s]", Luneimage->code);
176 
177     Ltrouve = FALSE;
178     /* on recherche d'abord dans les repertoires themes specifiques */
179     if (Atheme) {
180       for (Lcur = Lthemesemp; Lcur && Lcur->data; Lcur = Lcur->next) {
181         g_snprintf(Lpath, sizeof(Lpath)-1, "%s/themes/%s/%s%s.%s", (gchar *)Lcur->data,
182             Atheme, Luneimage->code, Aplus ? Aplus : "",
183             Luneimage->type == _IMG_PNG ? "png" : "mng");
184 _DEB("on cherche dans [%s]\n", Lpath);
185         if (g_file_test(Lpath, G_FILE_TEST_IS_REGULAR)) {
186           Ltrouve = TRUE;
187           break;
188         }
189       }
190     }
191 
192     /* puis dans les repertoires themes par defaut */
193     if (Ltrouve == FALSE && ((!Atheme) || strcmp(Atheme, _THEME_DEFAULT))) {
194       for (Lcur = Lthemesemp; Lcur && Lcur->data; Lcur = Lcur->next) {
195         g_snprintf(Lpath, sizeof(Lpath)-1, "%s/themes/%s/%s%s.%s",
196             (gchar *)Lcur->data, _THEME_DEFAULT, Aplus ? Aplus : "", Luneimage->code,
197             Luneimage->type == _IMG_PNG ? "png" : "mng");
198 
199         if (g_file_test(Lpath, G_FILE_TEST_IS_REGULAR)) {
200           Ltrouve = TRUE;
201           break;
202         }
203       }
204     }
205 
206     if (Ltrouve == TRUE) {
207       _DEB("on trouve [%s] => [%s]", Luneimage->code, Lpath);
208       g_hash_table_insert(Lhashimagespath, Luneimage->code, g_strdup(Lpath));
209       Laumoinsun = TRUE;
210     } else {
211       g_warning(_("cannot find file '%s' in any theme directories :-("), Luneimage->code);
212     }
213   }
214 
215   g_slist_free(Lthemesemp);
216 
217   if (!Laumoinsun) {
218     g_hash_table_destroy(Lhashimagespath);
219   }
220 
221   return Laumoinsun ? Lhashimagespath : NULL;
222 }
223 
224 /* remple une liststore avec tous les themes */
managelistethemes(GtkComboBox * Acombo,gboolean Aadddef,gchar * Asel)225 void managelistethemes(GtkComboBox *Acombo, gboolean Aadddef, gchar *Asel)
226 {
227   GtkListStore *Lmodel = GTK_LIST_STORE(gtk_combo_box_get_model(Acombo));
228   GtkTreeIter Liter;
229   GSList *Lthemesemp = get_data_path();
230   GSList *Lcur;
231   gchar Lpath[MAXPATHLEN];
232   gchar Ltestpath[MAXPATHLEN];
233   const gchar *Lcurnom;
234   GDir *Ldir;
235   Ttheme *Luntheme = NULL;
236   gint i = 0;
237   gint Ltosel = -1, Ldefault = 0;
238 
239   gtk_list_store_clear(Lmodel);
240 
241   for (Lcur = Lthemesemp; Lcur && Lcur->data; Lcur = Lcur->next) {
242     g_snprintf(Lpath, sizeof(Lpath)-1, "%s/themes", (gchar *)Lcur->data);
243     if (g_file_test(Lpath, G_FILE_TEST_IS_DIR)==FALSE) continue;
244 
245     if (!(Ldir = g_dir_open(Lpath, 0, NULL))) continue;
246 
247     while ((Lcurnom = g_dir_read_name(Ldir))) {
248       g_snprintf(Ltestpath, sizeof(Ltestpath)-1, "%s/%s/graveman", Lpath, Lcurnom);
249 
250       if (g_file_test(Ltestpath, G_FILE_TEST_IS_REGULAR)) {
251         if ((Luntheme = read_theme_desc(Ltestpath, NULL))) {
252 
253           if (Ltosel == -1 && !strcmp(Lcurnom, _THEME_DEFAULT)) Ldefault = i;
254 
255           gtk_list_store_append(Lmodel, &Liter);
256 
257           gtk_list_store_set(Lmodel, &Liter, 0, i, 1, get_image("Themes"), 2,
258             Luntheme->desc, 3, Lcurnom, -1);
259           if (Asel && Ltosel == -1 && !strcmp(Asel, Lcurnom)) {
260             Ltosel = i;
261           }
262 
263           Luntheme = free_theme(Luntheme);
264 
265           i++;
266         }
267       }
268     }
269 
270     g_dir_close(Ldir);
271   }
272 
273   g_slist_free(Lthemesemp);
274 
275   if (Ltosel == -1) Ltosel = Ldefault;
276   if (i) gtk_combo_box_set_active(Acombo, Ltosel);
277 }
278 
279 /*
280  * vim:et:ts=8:sts=2:sw=2
281  */
282