Keyset Memory Management Bugfix
[pierogi] / pirkeysetmetadata.cpp
index 5ae2599..e80822c 100644 (file)
@@ -1,17 +1,37 @@
 #include "pirkeysetmetadata.h"
+#include "pirselectdeviceform.h"
+#include "pirkeysetwidgetitem.h"
+#include "protocols/pirprotocol.h"
 
 #include <iostream>
 
+// Global helper object:
+extern PIRMakeMgr makeManager;
+
+// Static member definition:
+PIRDeviceCollection PIRKeysetMetaData::controlledDevices;
+
+// Thread synchronization stuff:
+extern bool commandInFlight;
+
 PIRKeysetMetaData::PIRKeysetMetaData(
   const char *r,
   PIRMakeName m,
   unsigned int i)
-  : keysetName(r),
-    make(m),
-    id(i)
+  : threadableProtocol(NULL),
+    index(i),
+    keysetName(r),
+    make(m)
 {
 }
 
+
+PIRKeysetMetaData::~PIRKeysetMetaData()
+{
+  clearProtocol();
+}
+
+
 bool PIRKeysetMetaData::hasKey(
   PIRKeyName name) const
 {
@@ -19,20 +39,36 @@ bool PIRKeysetMetaData::hasKey(
 }
 
 
-void PIRKeysetMetaData::moveProtocolToThread(
-  QThread &thread)
+void PIRKeysetMetaData::moveToThread(
+  QThread *thread)
 {
-  threadableProtocol->moveToThread(&thread);
+  if (threadableProtocol)
+  {
+    // Do I need some error checking here?
+    threadableProtocol->moveToThread(thread);
+  }
 }
 
 
-void PIRKeysetMetaData::populateDeviceTypes(
-  PIRKeysetWidgetItem *kwi) const
+void PIRKeysetMetaData::populateDevices(
+  PIRSelectDeviceForm *sdf)
 {
-  PIRDeviceTypeCollection::const_iterator i = deviceTypes.begin();
-  while (i != deviceTypes.end())
+  PIRKeysetWidgetItem *kwi;
+
+  PIRDeviceCollection::const_iterator i = controlledDevices.begin();
+
+  while (i != controlledDevices.end())
   {
-    kwi->addDeviceType(*i);
+    // Create a widget for the keyset:
+    QString tempString = makeManager.getMakeString(i->first.make);
+    tempString.append(" ");
+    tempString.append(i->first.model);
+
+    kwi = new PIRKeysetWidgetItem(
+      tempString, i->first.model, i->second, i->first.make, i->first.type);
+
+    sdf->addWidgetItem(kwi);
+
     ++i;
   }
 }
@@ -40,7 +76,7 @@ void PIRKeysetMetaData::populateDeviceTypes(
 
 unsigned int PIRKeysetMetaData::getID() const
 {
-  return id;
+  return index;
 }
 
 
@@ -60,9 +96,10 @@ void PIRKeysetMetaData::addControlledDevice(
   const char *model,
   PIRDeviceTypeName type)
 {
-  controlledDevices.push_back(DeviceInfo(make, model, type));
-
-  deviceTypes.insert(type);
+  controlledDevices.insert(
+    PIRDCPair(
+      DeviceInfo(make, model, type),
+      index));
 }
 
 
@@ -91,7 +128,6 @@ void PIRKeysetMetaData::addSIRC12Key(
   if (key != Unmapped_Key)
   {
     keys[key] = name;
-
     threadableProtocol->addSIRCKey(key, addressData, 5, commandData);
   }
 }
@@ -155,6 +191,20 @@ void PIRKeysetMetaData::addNECKey(
 }
 
 
+void PIRKeysetMetaData::addPanOldKey(
+  const char *name,
+  PIRKeyName key,
+  unsigned int addressData,
+  unsigned int commandData)
+{
+  if (key != Unmapped_Key)
+  {
+    keys[key] = name;
+    threadableProtocol->addPanOldKey(key, addressData, commandData);
+  }
+}
+
+
 void PIRKeysetMetaData::addPioneerKey(
   const char *name,
   PIRKeyName key,
@@ -172,6 +222,65 @@ void PIRKeysetMetaData::addPioneerKey(
 }
 
 
+/*
+void PIRKeysetMetaData::addRCAKey(
+  const char *name,
+  PIRKeyName key,
+  unsigned int addressData,
+  unsigned int commandData)
+{
+  if (key != Unmapped_Key)
+  {
+    keys[key] = name;
+    threadableProtocol->addPioneerKey(
+      key, addressData, commandData);
+  }
+}
+*/
+
+
+void PIRKeysetMetaData::addKaseikyoKey(
+  const char *name,
+  PIRKeyName key,
+  unsigned int addressData,
+  unsigned int commandData)
+{
+  if (key != Unmapped_Key)
+  {
+    keys[key] = name;
+    threadableProtocol->addKaseikyoKey(key, addressData, commandData);
+  }
+}
+
+
+void PIRKeysetMetaData::addDishKey(
+  const char *name,
+  PIRKeyName key,
+  unsigned int firstCommand,
+  unsigned int secondCommand)
+{
+  if (key != Unmapped_Key)
+  {
+    keys[key] = name;
+    threadableProtocol->addDishKey(key, firstCommand, secondCommand);
+  }
+}
+
+
+void PIRKeysetMetaData::addXMPKey(
+  const char *name,
+  PIRKeyName key,
+  unsigned int firstCommand,
+  unsigned int secondCommand)
+{
+  if (key != Unmapped_Key)
+  {
+    keys[key] = name;
+    threadableProtocol->addXMPKey(key, firstCommand, secondCommand);
+  }
+}
+
+
 void PIRKeysetMetaData::setPreData(
   unsigned long data,
   unsigned int bits)
@@ -193,3 +302,20 @@ void PIRKeysetMetaData::setKeysetName(
 {
   keysetName = n;
 }
+
+
+bool PIRKeysetMetaData::clearProtocol()
+{
+  if (!threadableProtocol)
+  {
+    return true;
+  }
+  else if (!commandInFlight)
+  {
+    delete threadableProtocol;
+    threadableProtocol = 0;
+    return true;
+  }
+
+  return false;
+}