summaryrefslogtreecommitdiff
path: root/src/MenuConfigurationRepository.cpp
blob: 647bc88f652d8ee0f2eafe487de59b4b1383089a (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
/*
 * 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 "MenuConfigurationRepository.h"
#include <exception>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <libxml++/libxml++.h>
#include <glibmm/convert.h>
#include "SystemMenuNode.h"
#include "SubMenuNode.h"
#include "PluginMenuNode.h"
#include "CommandMenuNode.h"
#include "SeparatorMenuNode.h"
#include "XmlConfigurationFactory.h"

using namespace xmlpp;
using namespace std;

const string MenuConfigurationRepository::_dtd = 
  "<!ELEMENT menus ((menu | system | plugin | command | separator)+)>\n"
  "<!ELEMENT menu ((menu | system | plugin | command | separator)+)>\n"
  "<!ATTLIST menu name CDATA #REQUIRED>\n"
  "<!ELEMENT system EMPTY>\n"
  "<!ATTLIST system\n"
  " name CDATA #REQUIRED\n"
  " title CDATA #IMPLIED>\n"
  "<!ELEMENT plugin EMPTY>\n"
  "<!ATTLIST plugin\n"
  " name CDATA #REQUIRED\n"
  " title CDATA #IMPLIED>\n"
  "<!ELEMENT command EMPTY>\n"
  "<!ATTLIST command\n"
  " name CDATA #REQUIRED\n"
  " execute CDATA #REQUIRED\n"
  " confirm (yes|no) #IMPLIED>"
  "<!ELEMENT separator EMPTY>\n"
  "<!ATTLIST separator\n"
  " title CDATA #IMPLIED>\n";

MenuConfigurationRepository::MenuConfigurationRepository(string menuFileName)
{
    _configurationFileName = menuFileName;
    _cachedMenuConfiguration = NULL;
    _lastConfigFileModificationTime = 0;
}

MenuConfigurationRepository::~MenuConfigurationRepository()
{
    delete _cachedMenuConfiguration;
}

SubMenuNode* MenuConfigurationRepository::Load()
{ 
    if (ConfigFileHasBeenChange())
    {
        try
        {
            dsyslog("loading menuorg config file from %s", _configurationFileName.c_str()); 

            DomParser _parser;

            _parser.set_substitute_entities();
            _parser.parse_file(_configurationFileName);

            DtdValidator validator;
            validator.parse_memory(_dtd);

            Document *document = _parser.get_document();
            validator.validate( document );

            if (document)
            {
                _lastConfigFileModificationTime = CurrentConfigFileModificationTime();
                delete _cachedMenuConfiguration;
                _cachedMenuConfiguration = new SubMenuNode("root");
                CreateMenuTree(document->get_root_node(), _cachedMenuConfiguration);
            }

        }
        catch(const std::exception& ex)
        {
            cerr << "menuorg: Exception caught when parsing xml configuration: " << ex.what();
            esyslog("Exception caught when parsing xml configuration. See stderr output for details.");
        }        
    }
    return _cachedMenuConfiguration;
}

void MenuConfigurationRepository::Save(SubMenuNode& menuConfiguration)
{
    Document* doc = XmlConfigurationFactory::CreateXmlConfig(&menuConfiguration);
    doc->write_to_file_formatted(_configurationFileName);
}

bool MenuConfigurationRepository::ConfigFileHasBeenChange()
{
    return (CurrentConfigFileModificationTime() != _lastConfigFileModificationTime);  
}

time_t MenuConfigurationRepository::CurrentConfigFileModificationTime()
{
    struct stat fileState;
    if (stat(_configurationFileName.c_str(), &fileState) == 0)
    {
        return fileState.st_mtim.tv_sec;
    }
    return 0;
}

void MenuConfigurationRepository::CreateMenuTree(const Element* menuRoot, SubMenuNode* menuNode)
{
    Node::NodeList children = menuRoot->get_children();
    for (Node::NodeList::iterator i = children.begin(); i != children.end(); i++)
    {   
        const Element* childElement = dynamic_cast<const Element*>(*i);

        if (childElement)
        {
            string type = childElement->get_name();
            string name = GetAttributeValue(childElement, "name", "");

            if ( type == "menu")
            {
                SubMenuNode* subMenu = new SubMenuNode(name);
                subMenu->AddToParent(menuNode);
                CreateMenuTree(childElement, subMenu);
            }
            else if (type == "system")
            {
                menuNode->AddChild(new SystemMenuNode(VdrState::ByName(name), GetTitle(childElement, "")));
            }
            else if (type == "plugin")
            {
                menuNode->AddChild(new PluginMenuNode(name, GetTitle(childElement, "")));
            }
            else if (type == "command")
            {
                string execute = childElement->get_attribute("execute")->get_value();
                bool confirm = GetAttributeValue(childElement, "confirm", "false") == "yes";
                menuNode->AddChild(new CommandMenuNode(name, execute, confirm));
            }
            else if (type == "separator")
            {
                menuNode->AddChild(new SeparatorMenuNode(GetTitle(childElement, "")));
            }
        }
    }
}

string MenuConfigurationRepository::UnicodeToLocaleOrIso8859(Glib::ustring unicodeString)
{
    try
    {
        return Glib::locale_from_utf8(unicodeString);
    }
    catch (Glib::ConvertError)
    {
        return Glib::convert_with_fallback(unicodeString, "ISO8859-15", "UTF-8");
    }
}

string MenuConfigurationRepository::GetTitle(const Element* node, string defaultValue)
{
    return GetAttributeValue(node, "title", defaultValue);
}

string MenuConfigurationRepository::GetAttributeValue(const Element* node, string name, string defaultValue)
{
    const Attribute* attribute = node->get_attribute(name);
    return attribute ? (string) UnicodeToLocaleOrIso8859(attribute->get_value()) : defaultValue;
}