From fc25d5a466d793c2069d451cb16e800787dccc95 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Mon, 18 Nov 2013 21:54:41 +0000 Subject: [PATCH] Use NSUserDefaults for powder.pref storage on OS X, fix initial file open handling on OS X --- SDLMain.m | 36 +++++++++++++++++++++++++++++++++++- src/client/Client.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/SDLMain.m b/SDLMain.m index 2434f81aa..083900aab 100644 --- a/SDLMain.m +++ b/SDLMain.m @@ -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 diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 3869fb182..1ee1cc73b 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -59,8 +59,13 @@ extern "C" #else #include #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 } }