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
|
/*
* vdr-menuorg - A plugin for the Linux Video Disk Recorder
* Copyright (C) 2007 Thomas Creutz, Tobias Grimm
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id$
*
*/
#include "menuconfiguration.h"
#include <libxml++/libxml++.h>
#include <exception>
#include <iostream>
#include <vdr/plugin.h>
#include "systemmenunode.h"
#include "submenunode.h"
#include "pluginmenunode.h"
#include "commandmenunode.h"
using namespace xmlpp;
using namespace std;
const string MenuConfiguration::_dtd =
"<!ELEMENT menus ((menu | system | plugin | command)+)>\n"
"<!ELEMENT menu ((menu | system | plugin | command)+)>\n"
"<!ATTLIST menu name CDATA #REQUIRED>\n"
"<!ELEMENT system EMPTY>\n"
"<!ATTLIST system name CDATA #REQUIRED>\n"
"<!ELEMENT plugin EMPTY>\n"
"<!ATTLIST plugin name CDATA #REQUIRED>\n"
"<!ELEMENT command EMPTY>\n"
"<!ATTLIST command\n"
" name CDATA #REQUIRED\n"
" execute CDATA #REQUIRED\n"
" confirm (yes|no) #IMPLIED>";
MenuNode* MenuConfiguration::LoadMenu(string menuFileName)
{
MenuNode* menuRoot = new MenuNode();
try
{
dsyslog("loading menuorg config file from %s", menuFileName.c_str());
DomParser parser;
parser.set_substitute_entities();
parser.parse_file(menuFileName);
DtdValidator validator;
validator.parse_memory(_dtd);
Document *pDoc = parser.get_document();
validator.validate( pDoc );
const Element* rootElement = parser.get_document()->get_root_node();
ParseElement(rootElement, menuRoot);
AddUnconfiguredPlugins(menuRoot);
}
catch(const std::exception& ex)
{
delete menuRoot;
menuRoot = NULL;
cerr << "menuorg: Exception caught when parsing xml configuration: " << ex.what();
esyslog("Exception caught when parsing xml configuration. See stderr output for details.");
}
return menuRoot;
}
void MenuConfiguration::ParseElement(const Element* element, MenuNode* menuNode)
{
Node::NodeList children = element->get_children();
for (Node::NodeList::iterator i = children.begin(); i != children.end(); i++)
{
const xmlpp::Element* childElement = dynamic_cast<const xmlpp::Element*>(*i);
if (childElement)
{
const xmlpp::Attribute* nameAttribute = childElement->get_attribute("name");
string type = childElement->get_name();
string name = nameAttribute->get_value();
if ( type == "menu")
{
MenuNode* subMenu = AddSubMenuNode(name, menuNode);
ParseElement(childElement, subMenu);
}
else if (type == "system")
{
AddSystemMenuNode(name, menuNode);
}
else if (type == "plugin")
{
AddPluginMenuNode(name, menuNode);
}
else if (type == "command")
{
string execute = childElement->get_attribute("execute")->get_value();
const xmlpp::Attribute* confirmAttribute = childElement->get_attribute("confirm");
bool confirm = confirmAttribute ? (confirmAttribute->get_value() == "yes") : false;
AddCommandMenuNode(name, execute, confirm, menuNode);
}
}
}
}
MenuNode* MenuConfiguration::AddSubMenuNode(string name, MenuNode* menu)
{
return menu->AddChild(new SubMenuNode(name));
}
void MenuConfiguration::AddSystemMenuNode(string name, MenuNode* menu)
{
menu->AddChild(new SystemMenuNode(name, MenuTextToVdrState(name)));
}
void MenuConfiguration::AddPluginMenuNode(string pluginName, MenuNode* menu)
{
int pluginIndex;
cPlugin* plugin;
if (FindPluginByName(pluginName, plugin, pluginIndex))
{
_configuredPlugins.push_back(pluginName);
menu->AddChild(new PluginMenuNode(plugin, pluginIndex));
}
}
eOSState MenuConfiguration::MenuTextToVdrState(string menuText)
{
if (menuText == "Schedule")
{
return osSchedule;
}
else if (menuText == "Channels")
{
return osChannels;
}
else if (menuText == "Timers")
{
return osTimers;
}
else if (menuText == "Recordings")
{
return osRecordings;
}
else if (menuText == "Setup")
{
return osSetup;
}
else if (menuText == "Commands")
{
return osCommands;
}
else
return osContinue;
}
bool MenuConfiguration::FindPluginByName(string name, cPlugin*& plugin, int& pluginIndex)
{
int i = 0;
while (cPlugin *currentPlugin = cPluginManager::GetPlugin(i))
{
if (name == currentPlugin->Name())
{
plugin = currentPlugin;
pluginIndex = i;
return true;
}
i++;
}
return false;
}
void MenuConfiguration::AddUnconfiguredPlugins(MenuNode* menu)
{
int i = 0;
while (cPlugin *plugin = cPluginManager::GetPlugin(i))
{
if (find(_configuredPlugins.begin(), _configuredPlugins.end(), plugin->Name()) == _configuredPlugins.end())
{
menu->AddChild(new PluginMenuNode(plugin, i));
}
i++;
}
}
void MenuConfiguration::AddCommandMenuNode(string name, string command, bool confirm, MenuNode* menu)
{
menu->AddChild(new CommandMenuNode(name, command, confirm));
}
|