Update CoreFoundationPreerences settings engine to handle both controllers and devices

This commit is contained in:
Micke Prag 2012-02-27 16:46:16 +01:00
parent 5dd66bfb03
commit 5fcc391c95

View file

@ -60,22 +60,26 @@ std::wstring Settings::getSetting(const std::wstring &strName) const {
/* /*
* Return the number of stored devices * Return the number of stored devices
*/ */
int Settings::getNumberOfDevices(void) const { int Settings::getNumberOfNodes(Node type) const {
CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName ); CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName );
if (!cfarray) return 0; if (!cfarray) return 0;
CFIndex size = CFArrayGetCount( cfarray ); CFIndex size = CFArrayGetCount( cfarray );
int devices = 0; int nodes = 0;
for (CFIndex k = 0; k < size; ++k) { for (CFIndex k = 0; k < size; ++k) {
CFStringRef key = (CFStringRef) CFArrayGetValueAtIndex(cfarray, k); CFStringRef key = (CFStringRef) CFArrayGetValueAtIndex(cfarray, k);
if (CFStringHasPrefix( key, CFSTR("devices.") ) && if (!CFStringHasSuffix( key, CFSTR(".name") )) {
CFStringHasSuffix( key, CFSTR(".name") ) ) { continue;
devices++; }
if (type == Device && CFStringHasPrefix( key, CFSTR("devices.") )) {
++nodes;
} else if (type == Controller && CFStringHasPrefix( key, CFSTR("controllers.") )) {
++nodes;
} }
} }
return devices; return nodes;
} }
int Settings::getDeviceId(int intDeviceIndex) const { int Settings::getNodeId(Node type, int intNodeIndex) const {
CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName ); CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName );
if (!cfarray) return 0; if (!cfarray) return 0;
CFIndex size = CFArrayGetCount( cfarray ); CFIndex size = CFArrayGetCount( cfarray );
@ -83,10 +87,19 @@ int Settings::getDeviceId(int intDeviceIndex) const {
int id = 0; int id = 0;
for (CFIndex k = 0; k < size; ++k) { for (CFIndex k = 0; k < size; ++k) {
CFStringRef key = (CFStringRef) CFArrayGetValueAtIndex(cfarray, k); CFStringRef key = (CFStringRef) CFArrayGetValueAtIndex(cfarray, k);
if (CFStringHasPrefix( key, CFSTR("devices.") ) && if (!CFStringHasSuffix( key, CFSTR(".name") )) {
CFStringHasSuffix( key, CFSTR(".name") ) ) { CFRelease( key );
continue;
if (index == intDeviceIndex) { }
if ( type == Device && !CFStringHasPrefix(key, CFSTR("devices.")) ) {
CFRelease( key );
continue;
}
if ( type == Controller && !CFStringHasPrefix(key, CFSTR("controllers.")) ) {
CFRelease( key );
continue;
}
if (index == intNodeIndex) {
CFArrayRef split = CFStringCreateArrayBySeparatingStrings( 0, key, CFSTR(".") ); CFArrayRef split = CFStringCreateArrayBySeparatingStrings( 0, key, CFSTR(".") );
if ( !split || CFArrayGetCount( split ) != 3 ) continue; if ( !split || CFArrayGetCount( split ) != 3 ) continue;
@ -117,29 +130,30 @@ int Settings::getDeviceId(int intDeviceIndex) const {
} }
index++; index++;
} }
CFRelease( key ); return id;
}
/*
* Add a new node
*/
int Settings::addNode(Node type) {
int id = getNextNodeId(type);
setStringSetting( type, id, L"name", L"", false ); //Create a empty name so the node has an entry
if (type == Device) {
//Is there a reason we do this?
setStringSetting( type, id, L"model", L"", false );
} }
return id; return id;
} }
/* /*
* Add a new device * Get next available node id
*/ */
int Settings::addDevice() { int Settings::getNextNodeId(Node type) const {
int id = getNextDeviceId();
setStringSetting( id, L"name", L"", false ); //Create a empty name so the device has an entry
setStringSetting( id, L"model", L"", false );
return id;
}
/*
* Get next available device id
*/
int Settings::getNextDeviceId() const {
int id = 0, max = 0; int id = 0, max = 0;
int numberOfDevices = getNumberOfDevices(); int numberOfNodes = getNumberOfNodes(type);
for( int i = 0; i < numberOfDevices; i++) { for( int i = 0; i < numberOfNodes; i++) {
id = getDeviceId( i ); id = getNodeId( type, i );
if (id > max) { if (id > max) {
max = id; max = id;
} }
@ -151,9 +165,9 @@ int Settings::getNextDeviceId() const {
/* /*
* Remove a device * Remove a device
*/ */
int Settings::removeDevice(int intDeviceId){ int Settings::removeNode(Node type, int intNodeId){
int ret = TELLSTICK_ERROR_DEVICE_NOT_FOUND; int ret = TELLSTICK_ERROR_DEVICE_NOT_FOUND;
CFStringRef filterKey = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d."), intDeviceId); // The key to search for CFStringRef filterKey = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d."), getNodeString(type).c_str(), intNodeId); // The key to search for
CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName ); CFArrayRef cfarray = CFPreferencesCopyKeyList( d->app_ID, d->userName, d->hostName );
if (!cfarray) { if (!cfarray) {
@ -172,15 +186,15 @@ int Settings::removeDevice(int intDeviceId){
return ret; return ret;
} }
std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &wname, bool parameter) const { std::wstring Settings::getStringSetting(Node type, int intNodeId, const std::wstring &wname, bool parameter) const {
std::string name(TelldusCore::wideToString(wname)); std::string name(TelldusCore::wideToString(wname));
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 ); CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
CFStringRef key; CFStringRef key;
if (parameter) { if (parameter) {
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.parameters.%@"), intDeviceId, cfname); key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.parameters.%@"), getNodeString(type).c_str(), intNodeId, cfname);
} else { } else {
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.%@"), intDeviceId, cfname); key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.%@"), getNodeString(type).c_str(), intNodeId, cfname);
} }
CFStringRef value; CFStringRef value;
@ -209,7 +223,7 @@ std::wstring Settings::getStringSetting(int intDeviceId, const std::wstring &wna
return retval; return retval;
} }
int Settings::setStringSetting(int intDeviceId, const std::wstring &wname, const std::wstring &wvalue, bool parameter) { int Settings::setStringSetting(Node type, int intNodeId, const std::wstring &wname, const std::wstring &wvalue, bool parameter) {
std::string name(TelldusCore::wideToString(wname)); std::string name(TelldusCore::wideToString(wname));
std::string value(TelldusCore::wideToString(wvalue)); std::string value(TelldusCore::wideToString(wvalue));
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 ); CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
@ -217,9 +231,9 @@ int Settings::setStringSetting(int intDeviceId, const std::wstring &wname, const
CFStringRef key; CFStringRef key;
if (parameter) { if (parameter) {
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.parameters.%@"), intDeviceId, cfname); key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.parameters.%@"), getNodeString(type).c_str(), intNodeId, cfname);
} else { } else {
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.%@"), intDeviceId, cfname); key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.%@"), getNodeString(type).c_str(), intNodeId, cfname);
} }
CFPreferencesSetValue( key, cfvalue, d->app_ID, d->userName, d->hostName ); CFPreferencesSetValue( key, cfvalue, d->app_ID, d->userName, d->hostName );
@ -227,7 +241,7 @@ int Settings::setStringSetting(int intDeviceId, const std::wstring &wname, const
return TELLSTICK_SUCCESS; return TELLSTICK_SUCCESS;
} }
int Settings::getIntSetting(int intDeviceId, const std::wstring &wname, bool parameter) const { int Settings::getIntSetting(Node type, int intNodeId, const std::wstring &wname, bool parameter) const {
int retval = 0; int retval = 0;
std::string name(TelldusCore::wideToString(wname)); std::string name(TelldusCore::wideToString(wname));
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 ); CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
@ -235,9 +249,9 @@ int Settings::getIntSetting(int intDeviceId, const std::wstring &wname, bool par
CFStringRef key; CFStringRef key;
if (parameter) { if (parameter) {
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.parameters.%@"), intDeviceId, cfname); key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.parameters.%@"), getNodeString(type).c_str(), intNodeId, cfname);
} else { } else {
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.%@"), intDeviceId, cfname); key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.%@"), getNodeString(type).c_str(), intNodeId, cfname);
} }
cfvalue = (CFNumberRef)CFPreferencesCopyValue(key, d->app_ID, d->userName, d->hostName); cfvalue = (CFNumberRef)CFPreferencesCopyValue(key, d->app_ID, d->userName, d->hostName);
@ -258,16 +272,16 @@ int Settings::getIntSetting(int intDeviceId, const std::wstring &wname, bool par
return retval; return retval;
} }
int Settings::setIntSetting(int intDeviceId, const std::wstring &wname, int value, bool parameter) { int Settings::setIntSetting(Node type, int intNodeId, const std::wstring &wname, int value, bool parameter) {
std::string name(TelldusCore::wideToString(wname)); std::string name(TelldusCore::wideToString(wname));
CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 ); CFStringRef cfname = CFStringCreateWithCString( 0, name.c_str(), kCFStringEncodingUTF8 );
CFNumberRef cfvalue = CFNumberCreate(NULL, kCFNumberIntType, &value); CFNumberRef cfvalue = CFNumberCreate(NULL, kCFNumberIntType, &value);
CFStringRef key; CFStringRef key;
if (parameter) { if (parameter) {
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.parameters.%@"), intDeviceId, cfname); key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.parameters.%@"), getNodeString(type).c_str(), intNodeId, cfname);
} else { } else {
key = CFStringCreateWithFormat(0, NULL, CFSTR("devices.%d.%@"), intDeviceId, cfname); key = CFStringCreateWithFormat(0, NULL, CFSTR("%ss.%d.%@"), getNodeString(type).c_str(), intNodeId, cfname);
} }
CFPreferencesSetValue( key, cfvalue, d->app_ID, d->userName, d->hostName ); CFPreferencesSetValue( key, cfvalue, d->app_ID, d->userName, d->hostName );