Improved Keyset Selection Window
[pierogi] / pirkeysetmanager.cpp
1 #include "pirkeysetmanager.h"
2 #include "pirkeysetmetadata.h"
3 #include "keysets/apple.h"
4 #include "keysets/ei.h"
5 #include "keysets/goldstar.h"
6 #include "keysets/hauppauge.h"
7 #include "keysets/jvc.h"
8 #include "keysets/lg.h"
9 #include "keysets/mitsubishi.h"
10 #include "keysets/nokia.h"
11 #include "keysets/panasonic.h"
12 #include "keysets/rca.h"
13 #include "keysets/samsung.h"
14 #include "keysets/sanyo.h"
15 #include "keysets/sharp.h"
16 #include "keysets/sony.h"
17 #include "keysets/toshiba.h"
18 #include "keysets/westinghouse.h"
19 #include "keysets/zenith.h"
20 //#include "protocol.h"
21 #include "pirmakenames.h"
22
23 #include "pirexception.h"
24
25 #include <QObject>
26 //#include <QMetaType>
27
28 // I'll be handling the threading of the keyset commands in this object:
29 #include <QMutex>
30
31 // This file defines some global stuff!  First, threading globals:
32
33 // The stopRepeatingFlag boolean is the method used to tell running commands
34 // in the worker thread to stop:
35 bool stopRepeatingFlag;
36 QMutex stopRepeatingMutex;
37
38 // Global helper objects:
39 PIRMakeMgr makeManager;
40 PIRDeviceTypeMgr deviceManager;
41
42 // Now, on to the actual method definitions:
43
44 PIRKeysetManager::PIRKeysetManager(
45   QObject *guiObject)
46   : counter(0)
47 {
48   // Create the keysets.  Ugly!  This needs to be worked on!
49   populateKeyset(new AppleWhiteRemote(guiObject, counter++));
50
51   populateKeyset(new EiKeyset(guiObject, counter++));
52
53   populateKeyset(new GoldStarTV1(guiObject, counter++));
54   populateKeyset(new GoldStarTV2(guiObject, counter++));
55   populateKeyset(new GoldStarVCR1(guiObject, counter++));
56   populateKeyset(new GoldStarVCR1a(guiObject, counter++));
57   populateKeyset(new GoldStarVCR1b(guiObject, counter++));
58   populateKeyset(new GoldStarVCR1c(guiObject, counter++));
59   populateKeyset(new GoldStarVCR1d(guiObject, counter++));
60   populateKeyset(new GoldStarCD1(guiObject, counter++));
61
62   populateKeyset(new HauppaugeGeneric(guiObject, counter++));
63   populateKeyset(new HauppaugeTV1(guiObject, counter++));
64
65   populateKeyset(new JVCSat1(guiObject, counter++));
66   populateKeyset(new JVCSat2(guiObject, counter++));
67   populateKeyset(new JVCVCR1(guiObject, counter++));
68   populateKeyset(new JVCVCR1a(guiObject, counter++));
69   populateKeyset(new JVCVCR1b(guiObject, counter++));
70   populateKeyset(new JVCVCR1c(guiObject, counter++));
71   populateKeyset(new JVCVCR2(guiObject, counter++));
72   populateKeyset(new JVCVCR2a(guiObject, counter++));
73   populateKeyset(new JVCVCR2b(guiObject, counter++));
74   populateKeyset(new JVCVCR2c(guiObject, counter++));
75   populateKeyset(new JVCTV1(guiObject, counter++));
76   populateKeyset(new JVCTV1a(guiObject, counter++));
77   populateKeyset(new JVCTV1b(guiObject, counter++));
78   populateKeyset(new JVCTV1c(guiObject, counter++));
79   populateKeyset(new JVCTV1d(guiObject, counter++));
80   populateKeyset(new JVCTV2(guiObject, counter++));
81   populateKeyset(new JVCTV2a(guiObject, counter++));
82   populateKeyset(new JVCTV2b(guiObject, counter++));
83   populateKeyset(new JVCTV2c(guiObject, counter++));
84   populateKeyset(new JVCTV2d(guiObject, counter++));
85   populateKeyset(new JVCDAT1(guiObject, counter++));
86   populateKeyset(new JVCCarDeck1(guiObject, counter++));
87   populateKeyset(new JVCPortableAudio1(guiObject, counter++));
88   populateKeyset(new JVCPortableAudio1a(guiObject, counter++));
89   populateKeyset(new JVCPortableAudio1b(guiObject, counter++));
90   populateKeyset(new JVCPortableAudio2(guiObject, counter++));
91   populateKeyset(new JVCDVD1(guiObject, counter++));
92
93   populateKeyset(new LGTV1(guiObject, counter++));
94   populateKeyset(new LGTV1a(guiObject, counter++));
95   populateKeyset(new LGTV1b(guiObject, counter++));
96   populateKeyset(new LGTV1c(guiObject, counter++));
97   populateKeyset(new LGTV2(guiObject, counter++));
98   populateKeyset(new LGTV2a(guiObject, counter++));
99   populateKeyset(new LGTV2b(guiObject, counter++));
100   populateKeyset(new LGDisc1(guiObject, counter++));
101   populateKeyset(new LGDisc2(guiObject, counter++));
102   populateKeyset(new LGDisc2a(guiObject, counter++));
103   populateKeyset(new LGVCR1(guiObject, counter++));
104   populateKeyset(new LGVCR1a(guiObject, counter++));
105   populateKeyset(new LGVCR1b(guiObject, counter++));
106
107   populateKeyset(new MitsubishiTV1(guiObject, counter++));
108   populateKeyset(new MitsubishiTV1a(guiObject, counter++));
109   populateKeyset(new MitsubishiVCR1(guiObject, counter++));
110   populateKeyset(new MitsubishiVCR1a(guiObject, counter++));
111
112   populateKeyset(new NokiaGenericVCR(guiObject, counter++));
113
114   populateKeyset(new PanasonicAmp(guiObject, counter++));
115   populateKeyset(new PanasonicCarAudio(guiObject, counter++));
116   populateKeyset(new PanasonicSat1(guiObject, counter++));
117   populateKeyset(new PanasonicSat1a(guiObject, counter++));
118   populateKeyset(new PanasonicTV1(guiObject, counter++));
119   populateKeyset(new PanasonicTV1a(guiObject, counter++));
120   populateKeyset(new PanasonicTV1b(guiObject, counter++));
121   populateKeyset(new PanasonicVCR1(guiObject, counter++));
122   populateKeyset(new PanasonicVCR1a(guiObject, counter++));
123   populateKeyset(new PanasonicVCR1b(guiObject, counter++));
124   populateKeyset(new PanasonicVCR1c(guiObject, counter++));
125   populateKeyset(new PanasonicDVD1(guiObject, counter++));
126   populateKeyset(new PanasonicDVD1a(guiObject, counter++));
127   populateKeyset(new PanasonicAudio(guiObject, counter++));
128
129   populateKeyset(new RCATV1(guiObject, counter++));
130   populateKeyset(new RCATV1a(guiObject, counter++));
131   populateKeyset(new RCATV2(guiObject, counter++));
132   populateKeyset(new RCAAux1(guiObject, counter++));
133   populateKeyset(new RCAAux2(guiObject, counter++));
134   populateKeyset(new RCAAux2a(guiObject, counter++));
135   populateKeyset(new RCAVCR1(guiObject, counter++));
136   populateKeyset(new RCAVCR2(guiObject, counter++));
137   populateKeyset(new RCADVD1(guiObject, counter++));
138   populateKeyset(new RCADVD1a(guiObject, counter++));
139   populateKeyset(new RCASat1(guiObject, counter++));
140   populateKeyset(new RCASat2(guiObject, counter++));
141
142   populateKeyset(new SamsungTV1(guiObject, counter++));
143   populateKeyset(new SamsungTV1a(guiObject, counter++));
144   populateKeyset(new SamsungTV1b(guiObject, counter++));
145   populateKeyset(new SamsungTV1c(guiObject, counter++));
146   populateKeyset(new SamsungTV1d(guiObject, counter++));
147   populateKeyset(new SamsungTV1e(guiObject, counter++));
148   populateKeyset(new SamsungTV1f(guiObject, counter++));
149   populateKeyset(new SamsungTV2(guiObject, counter++));
150   populateKeyset(new SamsungTV2a(guiObject, counter++));
151   populateKeyset(new SamsungVCR1(guiObject, counter++));
152   populateKeyset(new SamsungVCR1a(guiObject, counter++));
153   populateKeyset(new SamsungVCR1b(guiObject, counter++));
154   populateKeyset(new SamsungVCR1c(guiObject, counter++));
155   populateKeyset(new SamsungVCR1d(guiObject, counter++));
156   populateKeyset(new SamsungVCR1e(guiObject, counter++));
157   populateKeyset(new SamsungDVD1(guiObject, counter++));
158   populateKeyset(new SamsungDVD1a(guiObject, counter++));
159   populateKeyset(new SamsungDVD1b(guiObject, counter++));
160   populateKeyset(new SamsungDVD2(guiObject, counter++));
161   populateKeyset(new SamsungAC1(guiObject, counter++));
162
163   populateKeyset(new SanyoTV1(guiObject, counter++));
164   populateKeyset(new SanyoTV2(guiObject, counter++));
165   populateKeyset(new SanyoVCR1(guiObject, counter++));
166
167   populateKeyset(new SharpTV1(guiObject, counter++));
168   populateKeyset(new SharpTV1a(guiObject, counter++));
169   populateKeyset(new SharpTV1b(guiObject, counter++));
170   populateKeyset(new SharpTV1c(guiObject, counter++));
171   populateKeyset(new SharpTV1d(guiObject, counter++));
172   populateKeyset(new SharpTV1e(guiObject, counter++));
173   populateKeyset(new SharpVCR1(guiObject, counter++));
174   populateKeyset(new SharpReceiver1(guiObject, counter++));
175
176   populateKeyset(new SonyTV1(guiObject, counter++));
177   populateKeyset(new SonyTV1a(guiObject, counter++));
178   populateKeyset(new SonyTV1b(guiObject, counter++));
179   populateKeyset(new SonyTV1d(guiObject, counter++));
180   populateKeyset(new SonyAmp1(guiObject, counter++));
181   populateKeyset(new SonyAmp2(guiObject, counter++));
182   populateKeyset(new SonyCD1(guiObject, counter++));
183   populateKeyset(new SonyCD1a(guiObject, counter++));
184   populateKeyset(new SonyCD2(guiObject, counter++));
185   populateKeyset(new SonyCD3(guiObject, counter++));
186   populateKeyset(new SonyDAT1(guiObject, counter++));
187   populateKeyset(new SonyDVD1(guiObject, counter++));
188   populateKeyset(new SonyDVD1a(guiObject, counter++));
189   populateKeyset(new SonyDVD1b(guiObject, counter++));
190   populateKeyset(new SonyDVD1c(guiObject, counter++));
191   populateKeyset(new SonyVCR1(guiObject, counter++));
192   populateKeyset(new SonyVCR1a(guiObject, counter++));
193   populateKeyset(new SonyReceiver1(guiObject, counter++));
194
195   populateKeyset(new ToshibaTV1(guiObject, counter++));
196   populateKeyset(new ToshibaTV1a(guiObject, counter++));
197   populateKeyset(new ToshibaTV1b(guiObject, counter++));
198   populateKeyset(new ToshibaTV1c(guiObject, counter++));
199   populateKeyset(new ToshibaTV1d(guiObject, counter++));
200   populateKeyset(new ToshibaTV1e(guiObject, counter++));
201   populateKeyset(new ToshibaVCR1(guiObject, counter++));
202   populateKeyset(new ToshibaVCR1a(guiObject, counter++));
203   populateKeyset(new ToshibaDisc1(guiObject, counter++));
204   populateKeyset(new ToshibaDisc1a(guiObject, counter++));
205   populateKeyset(new ToshibaDisc1b(guiObject, counter++));
206   populateKeyset(new ToshibaDisc1c(guiObject, counter++));
207   populateKeyset(new ToshibaDisc1d(guiObject, counter++));
208
209   populateKeyset(new WestinghouseTV1(guiObject, counter++));
210   populateKeyset(new WestinghouseTV2(guiObject, counter++));
211
212   populateKeyset(new ZenithC32V37(guiObject, counter++));
213
214   // Start the thread running:
215   commandThread.start();
216 }
217
218
219 PIRKeysetManager::~PIRKeysetManager()
220 {
221   // Tell the keysets to stop doing any work:
222   stopRepeating();
223
224   // Tell the thread that we want it to stop:
225   commandThread.exit();
226
227   // Wait for the thread to stop:
228   commandThread.wait();
229
230   // Delete all the keysets:
231   PIRKeysetCollection::iterator i = keysetsInfo.begin();
232   while (i != keysetsInfo.end())
233   {
234     if ((*i).second) delete (*i).second;
235     ++i;
236   }
237 }
238
239
240 void PIRKeysetManager::stopRepeating()
241 {
242   QMutexLocker locker(&stopRepeatingMutex);
243   stopRepeatingFlag = true;
244 }
245
246
247 bool PIRKeysetManager::hasKey(
248   int keysetID,
249   PIRKeyName name)
250 {
251   PIRKeysetMetaData *meta = keysetsInfo[keysetID];
252
253   if (!meta) return false;
254
255   return meta->hasKey(name);
256 }
257
258
259 PIRMakeName PIRKeysetManager::getMake(
260   int keysetID)
261 {
262   PIRKeysetMetaData *meta = keysetsInfo[keysetID];
263
264   if (!meta) return Any_Make; // Work on this!
265
266   return meta->getMake();
267 }
268
269
270 QString PIRKeysetManager::getDisplayName(
271   int keysetID)
272 {
273   PIRKeysetMetaData *meta = keysetsInfo[keysetID];
274
275   if (!meta) return QString("Database Error");
276
277   return QString(meta->getKeysetName());
278 }
279
280
281 void PIRKeysetManager::populateKeyset(
282   PIRKeysetMetaData *keyset)
283 {
284   keysetsInfo[keyset->getID()] = keyset;
285
286   keyset->moveProtocolToThread(commandThread);
287 }
288
289
290 void PIRKeysetManager::populateGuiWidget(
291   PIRSelectKeysetForm *skf)
292 {
293   PIRKeysetCollection::const_iterator i = keysetsInfo.begin();
294
295   PIRMakeName make;
296
297   while (i != keysetsInfo.end())
298   {
299     make = i->second->getMake();
300     QString tempString = makeManager.getMakeString(make);
301     tempString.append(" ");
302     tempString.append(i->second->getKeysetName());
303     skf->addNameToList(tempString, i->first, make);
304     ++i;
305   }
306 }
307
308
309 void PIRKeysetManager::populateDeviceTypes(
310   PIRKeysetWidgetItem *kwi,
311   int keysetID)
312 {
313   // Pass the buck:  (Need to check for bad keysetID here?)
314   keysetsInfo[keysetID]->populateDeviceTypes(kwi);
315 }