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

#include <string.h>
#include <stdlib.h>
#include <vdr/tools.h>
#include "menudata.h"
#include "common.h"
#include "history.h"

// --- cQueryData ----------------------------------------------------------

cQueryData::cQueryData(const char *Name) {
  name = Name ? strdup(Name) : NULL;
}

cQueryData::~cQueryData() {
  if (name)
    free(name);
}

// --- cSimpleLink ---------------------------------------------------------

cSimpleLink::cSimpleLink(const char *reference) {
  ref = reference ? strdup(reference) : NULL;
}

cSimpleLink::~cSimpleLink() {
  if (ref) {
    free(ref);
  }
}

char *cSimpleLink::GetURL() {
  return ref;
}

// --- cTextFieldData ------------------------------------------------------

cTextFieldData::cTextFieldData(const char *Name, int Length) 
: cQueryData(Name)
{
  valuebufferlength = Length;
  valuebuffer = (char *)malloc(Length*sizeof(char));
  *valuebuffer = '\0';
}

cTextFieldData::~cTextFieldData() {
  if(valuebuffer)
    free(valuebuffer);
}

char *cTextFieldData::GetQueryFragment() {
  const char *name = GetName();

  if (name && *name && valuebuffer) {
    char *encoded = URLencode(valuebuffer);
    cString tmp = cString::sprintf("%s,%s", name, encoded);
    free(encoded);
    return strdup(tmp);
  }

  return NULL;
}

char *cTextFieldData::GetValue() {
  return valuebuffer;
}

int cTextFieldData::GetLength() {
  return valuebufferlength;
}

// --- cItemListData -------------------------------------------------------

cItemListData::cItemListData(const char *Name, char **Strings, char **StringValues, int NumStrings) 
: cQueryData(Name)
{
  strings = Strings;
  stringvalues = StringValues;
  numstrings = NumStrings;
  value = 0;
}

cItemListData::~cItemListData() {
  for (int i=0; i < numstrings; i++) {
    free(strings[i]);
    free(stringvalues[i]);
  }
  if (strings)
    free(strings);
  if (stringvalues)
    free(stringvalues);
}

char *cItemListData::GetQueryFragment() {
  const char *name = GetName();

  if (name && *name) {
    cString tmp = cString::sprintf("%s,%s", name, stringvalues[value]);
    return strdup(tmp);
  }

  return NULL;
}

char **cItemListData::GetStrings() {
  return strings;
}

char **cItemListData::GetStringValues() {
  return stringvalues;
}

int cItemListData::GetNumStrings() {
  return numstrings;
}

int *cItemListData::GetValuePtr() {
  return &value;
}

// --- cSubmissionButtonData -----------------------------------------------

cSubmissionButtonData::cSubmissionButtonData(
        const char *queryUrl, const cHistoryObject *currentPage)
{
  querybase = queryUrl ? strdup(queryUrl) : NULL;
  page = currentPage;
}

cSubmissionButtonData::~cSubmissionButtonData() {
  if (querybase)
    free(querybase);
  // do not free page
}

char *cSubmissionButtonData::GetURL() {
  if (!querybase)
    return NULL;

  char *querystr = (char *)malloc(sizeof(char)*(strlen(querybase)+2));
  strcpy(querystr, querybase);

  if (!page)
    return querystr;

  if (strchr(querystr, '?'))
    strcat(querystr, "&");
  else
    strcat(querystr, "?");

  int numparameters = 0;
  for (int i=0; i<page->QuerySize(); i++) {
    char *parameter = page->GetQueryFragment(i);
    if (parameter) {
      querystr = (char *)realloc(querystr, (strlen(querystr)+strlen(parameter)+8)*sizeof(char));
      if (i > 0)
	strcat(querystr, "&");
      strcat(querystr, "subst=");
      strcat(querystr, parameter);
      numparameters++;

      free(parameter);
    }
  }

  if (numparameters == 0) {
    // remove the '?' or '&' because no parameters were added to the url
    querystr[strlen(querystr)-1] = '\0';
  }

  return querystr;
}