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;
size_t arglen;
char *arg;
char *openCommandArg;
char **newargv;
if (!gFinderLaunch) /* MacOS is passing command line args. */
@ -267,7 +268,11 @@ static void CustomApplicationMain (int argc, char **argv)
if (arg == NULL)
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)
{
SDL_free(arg);
@ -275,7 +280,9 @@ static void CustomApplicationMain (int argc, char **argv)
}
gArgv = newargv;
SDL_strlcpy(openCommandArg, "open", 5);
SDL_strlcpy(arg, temparg, arglen);
gArgv[gArgc++] = "open";
gArgv[gArgc++] = arg;
gArgv[gArgc] = NULL;
return TRUE;
@ -344,6 +351,33 @@ static void CustomApplicationMain (int argc, char **argv)
@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

View File

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