themes.c 8.59 KB
Newer Older
Klaus Schmidinger's avatar
Klaus Schmidinger committed
1 2 3 4 5 6
/*
 * themes.c: Color themes used by skins
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
7
 * $Id: themes.c 4.0 2012/02/17 13:57:32 kls Exp $
Klaus Schmidinger's avatar
Klaus Schmidinger committed
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
 */

#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)
{
Klaus Schmidinger's avatar
Klaus Schmidinger committed
77
  char *s = descriptions[I18nCurrentLanguage()];
Klaus Schmidinger's avatar
Klaus Schmidinger committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  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;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
96 97
     cReadLine ReadLine;
     while ((s = ReadLine.Read(f)) != NULL) {
Klaus Schmidinger's avatar
Klaus Schmidinger committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
           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);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
115 116
                    if (lang >= 0) {
                       free(descriptions[lang]);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
117
                       descriptions[lang] = strdup(v);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
118
                       }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
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
                    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)
Klaus Schmidinger's avatar
Klaus Schmidinger committed
152
        esyslog("ERROR: error in %s, line %d%s%s", FileName, line, error ? ": " : "", error ? error : "");
Klaus Schmidinger's avatar
Klaus Schmidinger committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166
     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()) {
Klaus Schmidinger's avatar
Klaus Schmidinger committed
167
     for (int i = 0; i < I18nLanguages()->Size(); i++) {
Klaus Schmidinger's avatar
Klaus Schmidinger committed
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
         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) {
Klaus Schmidinger's avatar
Klaus Schmidinger committed
243 244 245
     cReadDir d(themesDirectory);
     struct dirent *e;
     while ((e = d.Next()) != NULL) {
Klaus Schmidinger's avatar
Klaus Schmidinger committed
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
           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;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
273
                    }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
274
                 numThemes++;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
275 276
                 }
              }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
277 278
           }
     return numThemes > 0;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
279 280 281 282 283 284 285 286 287 288 289
     }
  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)
Klaus Schmidinger's avatar
Klaus Schmidinger committed
290
         index = i;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
291 292 293 294 295 296 297 298 299 300 301 302 303
      }
  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)
{
Klaus Schmidinger's avatar
Klaus Schmidinger committed
304
  cString FileName = cString::sprintf("%s/%s-%s.theme", themesDirectory, SkinName, ThemeName);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
305 306 307 308 309 310
  if (access(FileName, F_OK) == 0) // the file exists
     Theme->Load(FileName);
}

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