Use NSUserDefaults for powder.pref storage on OS X, fix initial file open handling on OS X

This commit is contained in:
Simon Robertshaw 2013-11-18 21:54:41 +00:00
parent 1bce4984e3
commit fc25d5a466
2 changed files with 63 additions and 1 deletions

View File

@ -253,6 +253,7 @@ static void CustomApplicationMain (int argc, char **argv)
const char *temparg; const char *temparg;
size_t arglen; size_t arglen;
char *arg; char *arg;
char *openCommandArg;
char **newargv; char **newargv;
if (!gFinderLaunch) /* MacOS is passing command line args. */ if (!gFinderLaunch) /* MacOS is passing command line args. */
@ -267,7 +268,11 @@ static void CustomApplicationMain (int argc, char **argv)
if (arg == NULL) if (arg == NULL)
return FALSE; return FALSE;
newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2)); openCommandArg = (char *) SDL_malloc(5);
if (openCommandArg == NULL)
return FALSE;
newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 3));
if (newargv == NULL) if (newargv == NULL)
{ {
SDL_free(arg); SDL_free(arg);
@ -275,7 +280,9 @@ static void CustomApplicationMain (int argc, char **argv)
} }
gArgv = newargv; gArgv = newargv;
SDL_strlcpy(openCommandArg, "open", 5);
SDL_strlcpy(arg, temparg, arglen); SDL_strlcpy(arg, temparg, arglen);
gArgv[gArgc++] = "open";
gArgv[gArgc++] = arg; gArgv[gArgc++] = arg;
gArgv[gArgc] = NULL; gArgv[gArgc] = NULL;
return TRUE; return TRUE;
@ -344,6 +351,33 @@ static void CustomApplicationMain (int argc, char **argv)
@end @end
char * readUserPreferences() {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *prefDataNSString = [prefs stringForKey:@"powder.pref"];
const char *prefData = [prefDataNSString UTF8String];
if(prefData == NULL)
prefData = "";
char *prefDataCopy = calloc([prefDataNSString length]+1, 1);
SDL_strlcpy(prefDataCopy, prefData, [prefDataNSString length]);
[prefDataNSString release];
[prefs release];
return prefDataCopy;
}
void writeUserPreferences(const char * prefData) {
NSString *prefDataNSString = [NSString stringWithUTF8String:prefData];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:prefDataNSString forKey:@"powder.pref"];
[prefs synchronize];
[prefDataNSString release];
[prefs release];
}
#ifdef main #ifdef main

View File

@ -59,8 +59,13 @@ extern "C"
#else #else
#include <dirent.h> #include <dirent.h>
#endif #endif
#ifdef MACOSX
char * readUserPreferences();
void writeUserPreferences(const char * prefData);
#endif
} }
Client::Client(): Client::Client():
authUser(0, ""), authUser(0, ""),
updateAvailable(false), updateAvailable(false),
@ -80,8 +85,14 @@ Client::Client():
} }
//Read config //Read config
#ifdef MACOSX
char * prefData = readUserPreferences();
std::stringstream configFile(prefData);
free(prefData);
#else
std::ifstream configFile; std::ifstream configFile;
configFile.open("powder.pref", std::ios::binary); configFile.open("powder.pref", std::ios::binary);
#endif
if(configFile) if(configFile)
{ {
int fsize = configFile.tellg(); int fsize = configFile.tellg();
@ -112,7 +123,9 @@ Client::Client():
std::cerr << "Error: Could not read data from prefs: " << e.what() << std::endl; std::cerr << "Error: Could not read data from prefs: " << e.what() << std::endl;
} }
} }
#ifndef MACOSX
configFile.close(); configFile.close();
#endif
} }
} }
@ -805,8 +818,13 @@ void Client::RemoveListener(ClientListener * listener)
void Client::WritePrefs() void Client::WritePrefs()
{ {
#ifdef MACOSX
std::stringstream configFile;
#else
std::ofstream configFile; std::ofstream configFile;
configFile.open("powder.pref", std::ios::trunc); configFile.open("powder.pref", std::ios::trunc);
#endif
if(configFile) if(configFile)
{ {
if(authUser.ID) if(authUser.ID)
@ -827,7 +845,17 @@ void Client::WritePrefs()
configDocument["User"] = json::Null(); configDocument["User"] = json::Null();
} }
json::Writer::Write(configDocument, configFile); json::Writer::Write(configDocument, configFile);
#ifdef MACOSX
std::string prefString = configFile.str();
char prefData[prefString.length()+1];
std::strcpy(prefData, prefString.c_str());
std::cout << prefData << std::endl;
writeUserPreferences(prefData);
#else
configFile.close(); configFile.close();
#endif
} }
} }