summaryrefslogtreecommitdiff
path: root/src/vdr-plugin/config.c
blob: f294e6044924b6ce4ebfb4f794c2f79dbfac7200 (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
/*
 * config.c: Web video plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "dictionary.h"
#include "iniparser.h"
#include "common.h"

// --- cDownloadQuality ---------------------------------------------------

cDownloadQuality::cDownloadQuality(const char *sitename)
: min(NULL), max(NULL) {
  site = sitename ? strdup(sitename) : NULL;
}

cDownloadQuality::~cDownloadQuality() {
  if (site)
    free(site);
  if (min)
    free(min);
  if (max)
    free(max);
}

void cDownloadQuality::SetMin(const char *val) {
  if (min)
    free(min);

  min = val ? strdup(val) : NULL;
}

void cDownloadQuality::SetMax(const char *val) {
  if (max)
    free(max);

  max = val ? strdup(val) : NULL;
}

const char *cDownloadQuality::GetSite() {
  return site;
}

const char *cDownloadQuality::GetMin() {
  return min;
}

const char *cDownloadQuality::GetMax() {
  return max;
}

// --- cWebvideoConfig -----------------------------------------------------

cWebvideoConfig *webvideoConfig = new cWebvideoConfig();

cWebvideoConfig::cWebvideoConfig() {
  downloadPath = NULL;
  templatePath = NULL;
  preferXine = true;
}

cWebvideoConfig::~cWebvideoConfig() {
  if (downloadPath)
    free(downloadPath);
  if (templatePath)
    free(templatePath);
}

void cWebvideoConfig::SetDownloadPath(const char *path) {
  if (downloadPath)
    free(downloadPath);
  downloadPath = path ? strdup(path) : NULL;
}

const char *cWebvideoConfig::GetDownloadPath() {
  return downloadPath;
}

void cWebvideoConfig::SetTemplatePath(const char *path) {
  if (templatePath)
    free(templatePath);
  templatePath = path ? strdup(path) : NULL;
}

const char *cWebvideoConfig::GetTemplatePath() {
  return templatePath;
}

void cWebvideoConfig::SetPreferXineliboutput(bool pref) {
  preferXine = pref;
}

bool cWebvideoConfig::GetPreferXineliboutput() {
  return preferXine;
}

bool cWebvideoConfig::ReadConfigFile(const char *inifile) {
  dictionary *conf = iniparser_load(inifile);

  if (!conf)
    return false;

  info("loading config file %s", inifile);

  const char *templatepath = iniparser_getstring(conf, "webvi:templatepath", NULL);
  if (templatepath) {
    debug("templatepath = %s (from %s)", templatepath, inifile);
    SetTemplatePath(templatepath);
  }

  for (int i=0; i<iniparser_getnsec(conf); i++) {
    const char *section = iniparser_getsecname(conf, i);

    if (strncmp(section, "site-", 5) == 0) {
      const char *sitename = section+5;
      const int maxsectionlen = 40;
      char key[64];
      char *keyname;

      strncpy(key, section, maxsectionlen);
      key[maxsectionlen] = '\0';
      strcat(key, ":");
      keyname = key+strlen(key);

      strcpy(keyname, "download-min-quality");
      const char *download_min = iniparser_getstring(conf, key, NULL);

      strcpy(keyname, "download-max-quality");
      const char *download_max = iniparser_getstring(conf, key, NULL);

      strcpy(keyname, "stream-min-quality");
      const char *stream_min = iniparser_getstring(conf, key, NULL);

      strcpy(keyname, "stream-max-quality");
      const char *stream_max = iniparser_getstring(conf, key, NULL);

      if (download_min || download_max) {
        cDownloadQuality *limits = new cDownloadQuality(sitename);
        limits->SetMin(download_min);
        limits->SetMax(download_max);
        downloadLimits.Add(limits);

	debug("download priorities for %s (from %s): min = %s, max = %s",
	      sitename, inifile, download_min, download_max);
      }

      if (stream_min || stream_max) {
        cDownloadQuality *limits = new cDownloadQuality(sitename);
        limits->SetMin(stream_min);
        limits->SetMax(stream_max);
        streamLimits.Add(limits);

	debug("streaming priorities for %s (from %s): min = %s, max = %s",
	      sitename, inifile, stream_min, stream_max);
      }
    }
  }

  iniparser_freedict(conf);

  return true;
}

const char *cWebvideoConfig::GetQuality(const char *site, eRequestType type, int limit) {
  if (type != REQT_FILE && type != REQT_STREAM)
    return NULL;

  cList<cDownloadQuality>& priorlist = downloadLimits;
  if (type == REQT_STREAM)
    priorlist = streamLimits;

  cDownloadQuality *node = priorlist.First();

  while (node && (strcmp(site, node->GetSite()) != 0)) {
    node = priorlist.Next(node);
  }

  if (!node)
    return NULL;

  if (limit == 0)
    return node->GetMin();
  else
    return node->GetMax();
}

const char *cWebvideoConfig::GetMinQuality(const char *site, eRequestType type) {
  return GetQuality(site, type, 0);
}

const char *cWebvideoConfig::GetMaxQuality(const char *site, eRequestType type) {
  return GetQuality(site, type, 1);
}