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