Added missing model.h and some tests for remotelist
[irwi] / src / remotelist.cpp
1 #include "remotelist.h"
2 #include <QFile> 
3 #include <QDomElement> 
4 #include <QDomNodeList> 
5 #include <QDebug> 
6
7 RemoteList::RemoteList()
8     : valid(false)
9 {
10 }
11
12 RemoteList::RemoteList(const RemoteList &r)
13     : valid(r.valid)
14     , mfgMap(r.mfgMap)
15     , modelMap(r.modelMap)
16 {
17 }
18
19 RemoteList::RemoteList(QDomDocument &doc)
20 {
21     parse(doc);
22 }
23
24 RemoteList::RemoteList(const QString &xmlFile)
25 {
26     parse(xmlFile);   
27 }
28
29 RemoteList::RemoteList(QIODevice &in)
30 {
31     QDomDocument doc;
32     if (doc.setContent(&in))
33         parse(doc);
34 }
35
36 RemoteList::~RemoteList()
37 {
38 }
39
40 void RemoteList::setContent(QDomDocument &doc)
41 {
42     mfgMap.clear();
43     modelMap.clear();
44     parse(doc);
45 }
46
47 void RemoteList::setContent(const QString &xmlFile)
48 {
49     mfgMap.clear();
50     modelMap.clear();
51     parse(xmlFile);
52 }
53
54 bool RemoteList::isValid()
55 {
56     return valid;
57 }
58
59 QStringList RemoteList::letters()
60 {
61     return mfgMap.keys();
62 }
63
64 QStringList RemoteList::manufacturers(const QString &letter)
65 {
66     return mfgMap.value(letter);
67 }
68
69 QList<Model> RemoteList::models(const QString &manufacturer)
70 {
71     return modelMap.value(manufacturer);
72 }
73
74 void RemoteList::parse(const QString &xmlFile)
75 {
76     QFile file(xmlFile);
77     QDomDocument doc;
78
79     if (!file.open(QIODevice::ReadOnly))
80     {
81         valid = false;
82         return;
83     }
84
85     if (!doc.setContent(&file))
86     {
87         file.close();
88         valid = false;
89         return;
90     }
91     file.close();   
92
93     parse(doc);
94 }
95
96 void RemoteList::parse(QDomDocument &doc)
97 {
98     QDomNodeList chars = doc.elementsByTagName("char");
99
100     for (int i = 0; i < chars.size(); ++i)
101     {
102         QDomElement charEl = chars.at(i).toElement();
103         if (!charEl.isNull())
104         {
105             if (charEl.hasAttribute("id"))
106                 mfgMap.insert(charEl.attribute("id").at(0), parseMfgs(charEl));
107         }
108     }
109
110     // TODO: more error handling
111     valid = (mfgMap.size() > 0 &&
112              modelMap.size() > 0);
113 }
114
115 QStringList RemoteList::parseMfgs(QDomElement &charEl)
116 {
117     QStringList mfgStrings;
118     QDomNodeList mfgs = charEl.elementsByTagName("mfg");
119
120     for (int i = 0; i < mfgs.size(); ++i)
121     {
122         QDomElement mfgEl = mfgs.at(i).toElement();
123         if (!mfgEl.isNull())
124         {
125             if (mfgEl.hasAttribute("id"))
126             {
127                 mfgStrings.append(mfgEl.attribute("id"));
128                 modelMap.insert(mfgStrings.last(), parseModels(mfgEl));
129             }
130         }
131     }
132     return mfgStrings;
133 }
134
135 QList<Model> RemoteList::parseModels(QDomElement &mfgEl)
136 {
137     QList<Model> modelList;
138     QDomNodeList models = mfgEl.elementsByTagName("model");
139
140     for (int i = 0; i < models.size(); ++i)
141     {
142         QDomElement modelEl = models.at(i).toElement();
143         if (!modelEl.isNull())
144         {
145             if (modelEl.hasAttribute("name") && modelEl.hasAttribute("file"))
146             {
147                 modelList.append(Model(modelEl.attribute("name"),
148                                        modelEl.attribute("file")));
149             }
150         }
151     }
152     return modelList;
153 }
154