summaryrefslogtreecommitdiff
path: root/themes.c
blob: 88c7005e52c96a4b0592eda391e320022abba806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * themes.c: Color themes used by skins
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: themes.c 2.2 2012/02/17 13:57:32 kls Exp $
 */

#include "themes.h"
#include <dirent.h>
#include <string.h>
#include "config.h"
#include "tools.h"

// --- cTheme ----------------------------------------------------------------

cTheme::cTheme(void)
{
  name = strdup("default");
  memset(colorNames, 0, sizeof(colorNames));
  memset(colorValues, 0, sizeof(colorValues));
  descriptions[0] = strdup("Default");
}

cTheme::~cTheme()
{
  free(name);
  for (int i = 0; i < MaxThemeColors; i++)
      free(colorNames[i]);
}

bool cTheme::FileNameOk(const char *FileName, bool SetName)
{
  const char *error = NULL;
  if (!isempty(FileName)) {
     const char *d = strrchr(FileName, '/');
     if (d)
        FileName = d + 1;
     const char *n = strchr(FileName, '-');
     if (n) {
        if (n > FileName) {
           if (!strchr(++n, '-')) {
              const char *e = strchr(n, '.');
              if (e && strcmp(e, ".theme") == 0) {
                 if (e - n >= 1) {
                    // FileName is ok
                    if (SetName) {
                       free(name);
                       name = strndup(n, e - n);
                       }
                    }
                 else
                    error = "missing theme name";
                 }
              else
                 error = "invalid extension";
              }
           else
              error = "too many '-'";
           }
        else
           error = "missing skin name";
        }
     else
        error = "missing '-'";
     }
  else
     error = "empty";
  if (error)
     esyslog("ERROR: invalid theme file name (%s): '%s'", error, FileName);
  return !error;
}

const char *cTheme::Description(void)
{
  char *s = descriptions[I18nCurrentLanguage()];
  if (!s)
     s = descriptions[0];
  return s ? s : name;
}

bool cTheme::Load(const char *FileName, bool OnlyDescriptions)
{
  if (!FileNameOk(FileName, true))
     return false;
  bool result = false;
  if (!OnlyDescriptions)
     isyslog("loading %s", FileName);
  FILE *f = fopen(FileName, "r");
  if (f) {
     int line = 0;
     result = true;
     char *s;
     const char *error = NULL;
     cReadLine ReadLine;
     while ((s = ReadLine.Read(f)) != NULL) {
           line++;
           char *p = strchr(s, '#');
           if (p)
              *p = 0;
           s = stripspace(skipspace(s));
           if (!isempty(s)) {
              char *n = s;
              char *v = strchr(s, '=');
              if (v) {
                 *v++ = 0;
                 n = stripspace(skipspace(n));
                 v = stripspace(skipspace(v));
                 if (strstr(n, "Description") == n) {
                    int lang = 0;
                    char *l = strchr(n, '.');
                    if (l)
                       lang = I18nLanguageIndex(++l);
                    if (lang >= 0) {
                       free(descriptions[lang]);
                       descriptions[lang] = strdup(v);
                       }
                    else
                       error = "invalid language code";
                    }
                 else if (!OnlyDescriptions) {
                    for (int i = 0; i < MaxThemeColors; i++) {
                        if (colorNames[i]) {
                           if (strcmp(n, colorNames[i]) == 0) {
                              char *p = NULL;
                              errno = 0;
                              tColor c = strtoul(v, &p, 16);
                              if (!errno && !*p)
                                 colorValues[i] = c;
                              else
                                 error = "invalid color value";
                              break;
                              }
                           }
                        else {
                           error = "unknown color name";
                           break;
                           }
                        }
                    }
                 }
              else
                 error = "missing value";
              }
           if (error) {
              result = false;
              break;
              }
           }
     if (!result)
        esyslog("ERROR: error in %s, line %d%s%s", FileName, line, error ? ": " : "", error ? error : "");
     fclose(f);
     }
  else
     LOG_ERROR_STR(FileName);
  return result;
}

bool cTheme::Save(const char *FileName)
{
  if (!FileNameOk(FileName))
     return false;
  bool result = true;
  cSafeFile f(FileName);
  if (f.Open()) {
     for (int i = 0; i < I18nLanguages()->Size(); i++) {
         if (descriptions[i])
            fprintf(f, "Description%s%.*s = %s\n", i ? "." : "", 3, i ? I18nLanguageCode(i) : "", descriptions[i]);
         }
     for (int i = 0; i < MaxThemeColors; i++) {
         if (colorNames[i])
            fprintf(f, "%s = %08X\n", colorNames[i], colorValues[i]);
         }
     if (!f.Close())
        result = false;
     }
  else
     result = false;
  return result;
}

int cTheme::AddColor(const char *Name, tColor Color)
{
  for (int i = 0; i < MaxThemeColors; i++) {
      if (colorNames[i]) {
         if (strcmp(Name, colorNames[i]) == 0) {
            colorValues[i] = Color;
            return i;
            }
         }
      else {
         colorNames[i] = strdup(Name);
         colorValues[i] = Color;
         return i;
         }
      }
  return -1;
}

tColor cTheme::Color(int Subject)
{
  return (Subject >= 0 && Subject < MaxThemeColors) ? colorValues[Subject] : 0;
}

// --- cThemes ---------------------------------------------------------------

char *cThemes::themesDirectory = NULL;

cThemes::cThemes(void)
{
  numThemes = 0;
  names = 0;
  fileNames = NULL;
  descriptions = NULL;
}

cThemes::~cThemes()
{
  Clear();
}

void cThemes::Clear(void)
{
  for (int i = 0; i < numThemes; i++) {
      free(names[i]);
      free(fileNames[i]);
      free(descriptions[i]);
      }
  free(names);
  free(fileNames);
  free(descriptions);
  numThemes = 0;
  names = 0;
  fileNames = NULL;
  descriptions = NULL;
}

bool cThemes::Load(const char *SkinName)
{
  Clear();
  if (themesDirectory) {
     cReadDir d(themesDirectory);
     struct dirent *e;
     while ((e = d.Next()) != NULL) {
           if (strstr(e->d_name, SkinName) == e->d_name && e->d_name[strlen(SkinName)] == '-') {
              cString FileName = AddDirectory(themesDirectory, e->d_name);
              cTheme Theme;
              if (Theme.Load(*FileName, true)) {
                 if (char **NewBuffer = (char **)realloc(names, (numThemes + 1) * sizeof(char *))) {
                    names = NewBuffer;
                    names[numThemes] = strdup(Theme.Name());
                    }
                 else {
                    esyslog("ERROR: out of memory");
                    break;
                    }
                 if (char **NewBuffer = (char **)realloc(fileNames, (numThemes + 1) * sizeof(char *))) {
                    fileNames = NewBuffer;
                    fileNames[numThemes] = strdup(*FileName);
                    }
                 else {
                    esyslog("ERROR: out of memory");
                    break;
                    }
                 if (char **NewBuffer = (char **)realloc(descriptions, (numThemes + 1) * sizeof(char *))) {
                    descriptions = NewBuffer;
                    descriptions[numThemes] = strdup(Theme.Description());
                    }
                 else {
                    esyslog("ERROR: out of memory");
                    break;
                    }
                 numThemes++;
                 }
              }
           }
     return numThemes > 0;
     }
  return false;
}

int cThemes::GetThemeIndex(const char *Description)
{
  int index = 0;
  for (int i = 0; i < numThemes; i++) {
      if (strcmp(descriptions[i], Description) == 0)
         return i;
      if (strcmp(descriptions[i], "Default") == 0)
         index = i;
      }
  return index;
}

void cThemes::SetThemesDirectory(const char *ThemesDirectory)
{
  free(themesDirectory);
  themesDirectory = strdup(ThemesDirectory);
  MakeDirs(themesDirectory, true);
}

void cThemes::Load(const char *SkinName, const char *ThemeName, cTheme *Theme)
{
  cString FileName = cString::sprintf("%s/%s-%s.theme", themesDirectory, SkinName, ThemeName);
  if (access(FileName, F_OK) == 0) // the file exists
     Theme->Load(FileName);
}

void cThemes::Save(const char *SkinName, cTheme *Theme)
{
  cString FileName = cString::sprintf("%s/%s-%s.theme", themesDirectory, SkinName, Theme->Name());
  if (access(FileName, F_OK) != 0) // the file does not exist
     Theme->Save(FileName);
}