6fc7cd1c0ac9aad9175ec1038c18f74fc28f3e0f
[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()
30 {
31 }
32
33 void RemoteList::setContent(QDomDocument &doc)
34 {
35     mfgMap.clear();
36     modelMap.clear();
37     parse(doc);
38 }
39
40 void RemoteList::setContent(const QString &xmlFile)
41 {
42     mfgMap.clear();
43     modelMap.clear();
44     parse(xmlFile);
45 }
46
47 bool RemoteList::isValid()
48 {
49     return valid;
50 }
51
52 QList<QChar> RemoteList::letters()
53 {
54     return mfgMap.keys();
55 }
56
57 QStringList RemoteList::manufacturers(const QChar &letter)
58 {
59     return mfgMap.value(letter);
60 }
61
62 QList<Model> RemoteList::models(const QString &manufacturer)
63 {
64     return modelMap.value(manufacturer);
65 }
66
67 void RemoteList::parse(const QString &xmlFile)
68 {
69     QFile file(xmlFile);
70     QDomDocument doc;
71
72     if (!file.open(QIODevice::ReadOnly))
73     {
74         valid = false;
75         return;
76     }
77
78     if (!doc.setContent(&file))
79     {
80         file.close();
81         valid = false;
82         return;
83     }
84     file.close();   
85
86     parse(doc);
87 }
88
89 void RemoteList::parse(QDomDocument &doc)
90 {
91     QDomNodeList chars = doc.elementsByTagName("char");
92
93     for (int i = 0; i < chars.size(); ++i)
94     {
95         QDomElement charEl = chars.at(i).toElement();
96         if (!charEl.isNull())
97         {
98             if (charEl.hasAttribute("id"))
99                 mfgMap.insert(charEl.attribute("id").at(0), parseMfgs(charEl));
100         }
101     }
102
103     valid = (mfgMap.size() > 0 &&
104              modelMap.size() > 0 &&
105              mfgMap.values().size() == modelMap.keys().size());
106 }
107
108 QStringList RemoteList::parseMfgs(QDomElement &charEl)
109 {
110     QStringList mfgStrings;
111     QDomNodeList mfgs = charEl.elementsByTagName("mfg");
112
113     for (int i = 0; i < mfgs.size(); ++i)
114     {
115         QDomElement mfgEl = mfgs.at(i).toElement();
116         if (!mfgEl.isNull())
117         {
118             if (mfgEl.hasAttribute("id"))
119             {
120                 mfgStrings.append(mfgEl.attribute("id"));
121                 modelMap.insert(mfgStrings.last(), parseModels(mfgEl));
122             }
123         }
124     }
125     return mfgStrings;
126 }
127
128 QList<Model> RemoteList::parseModels(QDomElement &mfgEl)
129 {
130     QList<Model> modelList;
131     QDomNodeList models = mfgEl.elementsByTagName("model");
132
133     for (int i = 0; i < models.size(); ++i)
134     {
135         QDomElement modelEl = models.at(i).toElement();
136         if (!modelEl.isNull())
137         {
138             if (modelEl.hasAttribute("name") && modelEl.hasAttribute("file"))
139             {
140                 modelList.append(Model(modelEl.attribute("name"),
141                                        modelEl.attribute("file")));
142             }
143         }
144     }
145     return modelList;
146 }
147