summaryrefslogtreecommitdiff
path: root/src/xmlmenu.cpp
blob: 6893038b5c3af0a1a916321d36994a34fe0c7da8 (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
#include "xmlmenu.h"
#include <iostream>
#include <libxml++/libxml++.h>
#include <exception>
#include <vdr/plugin.h>
#include "vdrmenuitem.h"
#include "submenuitem.h"
#include "pluginmenuitem.h"

using namespace xmlpp;
using namespace std;

void XmlMenu::LoadXmlMenu()
{ 
    // TODO: show how vdr handels the path vars (developer doc)
    // and change code for dynamic path vars
    const char *File = "/var/lib/vdr/plugins/vdr-menu.xml";

    try
    {
        DomParser parser;

        //TODO: patch the xmlfile with the xsd definition for validate the schema
        //parser.set_validate();
        parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
        parser.parse_file(File);
        if(parser)
        {
            //Walk the tree:
            MenuCount=0;
            const Element* rootElement = parser.get_document()->get_root_node(); //deleted by DomParser.
            ParseElement(rootElement, &_rootMenuNode);
            _xmlLoadError=false;
        }
    }
    catch(const std::exception& ex)
    {
        //TODO: print output to syslog (isyslog or dsyslog?)
        cout << "Exception caught: " << ex.what() << endl;
        isyslog("Exception caught: %s", ex.what());
        //TODO: display message on osd
        _xmlLoadError=true;
    }
}

MenuNode* XmlMenu::GetMenuTree()
{
    return &_rootMenuNode;
}

void XmlMenu::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");

            if (nameAttribute)
            {
                string type = childElement->get_name();
                string name = nameAttribute->get_value();
                
                if ( type == "menu")
                {
                    MenuNode* subMenu = AddSubMenuItem(name, menuNode);
                    ParseElement(childElement, subMenu);
                }
                else if (type == "system")
                {
                    AddSystemMenuItem(name, menuNode);
                }
                else if (type == "plugin")
                {
                    AddPluginMenuItem(name, menuNode);
                }
            }
        }
    }
}

MenuNode* XmlMenu::AddSubMenuItem(string name, MenuNode* menu)
{
    return menu->AddChild(new SubMenuItem(name));
}

void XmlMenu::AddSystemMenuItem(string name, MenuNode* menu)
{
    menu->AddChild(new VdrMenuItem(name, MenuTextToVdrState(name)));
}

void XmlMenu::AddPluginMenuItem(string pluginName, MenuNode* menu)
{
    const char* pluginMainMenuEntry;
    int pluginIndex;

    if (FindPluginByName(pluginName, &pluginMainMenuEntry, pluginIndex))
    {
        menu->AddChild(new PluginMenuItem(pluginMainMenuEntry, pluginIndex));
    }
}

eOSState XmlMenu::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 XmlMenu::FindPluginByName(string name, const char** mainMenuEntry, int& pluginIndex)
{
    int i = 0;

    while (cPlugin *plugin = cPluginManager::GetPlugin(i))
    {
        if (name == plugin->Name()) 
        {
            if (const char *item = plugin->MainMenuEntry())
            {
                pluginIndex = i;
                *mainMenuEntry = item;
                return true;
            }
        }
        i++;
    }
    return false;
}

bool XmlMenu::getErrorStatus()
{
    return _xmlLoadError;
}