static void DeserializePrefs(const char *prefsTxt, SerializableGlobalPrefs& globalPrefs, FileHistory& fh, Favorites *favs) { BencObj *obj = BencObj::Decode(prefsTxt); if (!obj || obj->Type() != BT_DICT) goto Exit; BencDict *prefs = static_cast<BencDict *>(obj); BencDict *global = prefs->GetDict(GLOBAL_PREFS_STR); if (!global) goto Exit; DeserializeStruct(gGlobalPrefInfo, dimof(gGlobalPrefInfo), &globalPrefs, global); free(globalPrefs.prevSerialization); globalPrefs.prevSerialization = global->Encode(); int weekDiff = GetWeekCount() - globalPrefs.openCountWeek; globalPrefs.openCountWeek = GetWeekCount(); BencArray *fileHistory = prefs->GetArray(FILE_HISTORY_STR); if (!fileHistory) goto Exit; size_t dlen = fileHistory->Length(); for (size_t i = 0; i < dlen; i++) { BencDict *dict = fileHistory->GetDict(i); assert(dict); if (!dict) continue; DisplayState *state = DeserializeDisplayState(dict, globalPrefs.globalPrefsOnly); if (state) { // "age" openCount statistics (cut in in half after every week) state->openCount >>= weekDiff; fh.Append(state); } } BencArray *favsArr = prefs->GetArray(FAVS_STR); if (!favsArr) goto Exit; for (size_t i = 0; i < favsArr->Length(); i += 2) { BencString *filePathBenc = favsArr->GetString(i); BencArray *favData = favsArr->GetArray(i+1); if (!filePathBenc || !favData) break; ScopedMem<WCHAR> filePath(filePathBenc->Value()); for (size_t j = 0; j < favData->Length(); j += 2) { // we're lenient about errors BencInt *pageNoBenc = favData->GetInt(j); BencString *nameBenc = favData->GetString(j + 1); if (!pageNoBenc || !nameBenc) break; int pageNo = (int)pageNoBenc->Value(); ScopedMem<WCHAR> name(nameBenc->Value()); favs->AddOrReplace(filePath, pageNo, EmptyToNull(name)); } } Exit: delete obj; }
static void Retrieve(BencDict *dict, const char *key, DisplayMode& value) { BencString *string = dict->GetString(key); if (string) { ScopedMem<WCHAR> mode(string->Value()); if (mode) DisplayModeConv::EnumFromName(mode, &value); } }
static void Retrieve(BencDict *dict, const char *key, WCHAR *& value) { BencString *string = dict->GetString(key); if (string) { WCHAR *str = string->Value(); if (str) { free(value); value = str; } } }
static void DeserializePrefs(const char *prefsTxt, SerializableGlobalPrefs& globalPrefs, FileHistory& fh, Favorites *favs) { BencObj *obj = BencObj::Decode(prefsTxt); if (!obj || obj->Type() != BT_DICT) goto Exit; BencDict *prefs = static_cast<BencDict *>(obj); BencDict *global = prefs->GetDict(GLOBAL_PREFS_STR); if (!global) goto Exit; Retrieve(global, TOOLBAR_VISIBLE_STR, globalPrefs.toolbarVisible); Retrieve(global, TOC_VISIBLE_STR, globalPrefs.tocVisible); Retrieve(global, FAV_VISIBLE_STR, globalPrefs.favVisible); Retrieve(global, SIDEBAR_DX_STR, globalPrefs.sidebarDx); Retrieve(global, TOC_DY_STR, globalPrefs.tocDy); Retrieve(global, PDF_ASSOCIATE_DONT_ASK_STR, globalPrefs.pdfAssociateDontAskAgain); Retrieve(global, PDF_ASSOCIATE_ASSOCIATE_STR, globalPrefs.pdfAssociateShouldAssociate); Retrieve(global, ESC_TO_EXIT_STR, globalPrefs.escToExit); Retrieve(global, USE_SYS_COLORS_STR, globalPrefs.useSysColors); Retrieve(global, BG_COLOR_STR, globalPrefs.bgColor); Retrieve(global, ENABLE_AUTO_UPDATE_STR, globalPrefs.enableAutoUpdate); Retrieve(global, REMEMBER_OPENED_FILES_STR, globalPrefs.rememberOpenedFiles); Retrieve(global, GLOBAL_PREFS_ONLY_STR, globalPrefs.globalPrefsOnly); Retrieve(global, SHOW_RECENT_FILES_STR, globalPrefs.showStartPage); Retrieve(global, DISPLAY_MODE_STR, globalPrefs.defaultDisplayMode); Retrieve(global, ZOOM_VIRTUAL_STR, globalPrefs.defaultZoom); Retrieve(global, WINDOW_STATE_STR, globalPrefs.windowState); Retrieve(global, WINDOW_X_STR, globalPrefs.windowPos.x); Retrieve(global, WINDOW_Y_STR, globalPrefs.windowPos.y); Retrieve(global, WINDOW_DX_STR, globalPrefs.windowPos.dx); Retrieve(global, WINDOW_DY_STR, globalPrefs.windowPos.dy); Retrieve(global, INVERSE_SEARCH_COMMANDLINE, globalPrefs.inverseSearchCmdLine); Retrieve(global, ENABLE_TEX_ENHANCEMENTS_STR, globalPrefs.enableTeXEnhancements); Retrieve(global, VERSION_TO_SKIP_STR, globalPrefs.versionToSkip); RetrieveRaw(global, LAST_UPDATE_STR, globalPrefs.lastUpdateTime); const char *lang = GetRawString(global, UI_LANGUAGE_STR); const char *langCode = trans::ValidateLanguageCode(lang); if (langCode) globalPrefs.currentLanguage = langCode; Retrieve(global, FWDSEARCH_OFFSET, globalPrefs.fwdSearch.offset); Retrieve(global, FWDSEARCH_COLOR, globalPrefs.fwdSearch.color); Retrieve(global, FWDSEARCH_WIDTH, globalPrefs.fwdSearch.width); Retrieve(global, FWDSEARCH_PERMANENT, globalPrefs.fwdSearch.permanent); Retrieve(global, CBX_RIGHT2LEFT, globalPrefs.cbxR2L); Retrieve(global, OPEN_COUNT_WEEK_STR, globalPrefs.openCountWeek); int weekDiff = GetWeekCount() - globalPrefs.openCountWeek; globalPrefs.openCountWeek = GetWeekCount(); BencArray *fileHistory = prefs->GetArray(FILE_HISTORY_STR); if (!fileHistory) goto Exit; size_t dlen = fileHistory->Length(); for (size_t i = 0; i < dlen; i++) { BencDict *dict = fileHistory->GetDict(i); assert(dict); if (!dict) continue; DisplayState *state = DeserializeDisplayState(dict, globalPrefs.globalPrefsOnly); if (state) { // "age" openCount statistics (cut in in half after every week) state->openCount >>= weekDiff; fh.Append(state); } } BencArray *favsArr = prefs->GetArray(FAVS_STR); if (!favsArr) goto Exit; for (size_t i = 0; i < favsArr->Length(); i += 2) { BencString *filePathBenc = favsArr->GetString(i); BencArray *favData = favsArr->GetArray(i+1); if (!filePathBenc || !favData) break; ScopedMem<WCHAR> filePath(filePathBenc->Value()); for (size_t j = 0; j < favData->Length(); j += 2) { // we're lenient about errors BencInt *pageNoBenc = favData->GetInt(j); BencString *nameBenc = favData->GetString(j + 1); if (!pageNoBenc || !nameBenc) break; int pageNo = (int)pageNoBenc->Value(); ScopedMem<WCHAR> name(nameBenc->Value()); favs->AddOrReplace(filePath, pageNo, EmptyToNull(name)); } } Exit: delete obj; }
static void DeserializeStruct(PrefInfo *info, size_t count, void *structBase, BencDict *prefs) { char *base = (char *)structBase; BencInt *intObj; BencString *strObj; BencArray *arrObj; for (size_t i = 0; i < count; i++) { PrefInfo& meta = info[i]; switch (meta.type) { case Pref_Bool: if ((intObj = prefs->GetInt(meta.name))) *(bool *)(base + meta.offset) = intObj->Value() != 0; break; case Pref_Int: if ((intObj = prefs->GetInt(meta.name))) *(int *)(base + meta.offset) = (int)intObj->Value(); break; case Pref_Str: if ((strObj = prefs->GetString(meta.name))) str::ReplacePtr((char **)(base + meta.offset), strObj->RawValue()); break; case Pref_WStr: if ((strObj = prefs->GetString(meta.name))) { ScopedMem<WCHAR> str(strObj->Value()); if (str) str::ReplacePtr((WCHAR **)(base + meta.offset), str); } break; case Pref_DisplayMode: if ((strObj = prefs->GetString(meta.name))) { ScopedMem<WCHAR> mode(strObj->Value()); if (mode) DisplayModeConv::EnumFromName(mode, (DisplayMode *)(base + meta.offset)); } break; case Pref_Float: if ((strObj = prefs->GetString(meta.name))) { // note: this might round the value for files produced with versions // prior to 1.6 and on a system where the decimal mark isn't a '.' // (the difference should be hardly notable, though) *(float *)(base + meta.offset) = (float)atof(strObj->RawValue()); } break; case Pref_IntVec: if ((arrObj = prefs->GetArray(meta.name))) { size_t len = arrObj->Length(); Vec<int> *intVec = new Vec<int>(len); if (intVec) { for (size_t idx = 0; idx < len; idx++) { if ((intObj = arrObj->GetInt(idx))) intVec->Append((int)intObj->Value()); } delete *(Vec<int> **)(base + meta.offset); *(Vec<int> **)(base + meta.offset) = intVec; } } break; case Pref_UILang: if ((strObj = prefs->GetString(meta.name))) { // ensure language code is valid const char *langCode = trans::ValidateLangCode(strObj->RawValue()); if (langCode) *(const char **)(base + meta.offset) = langCode; } break; } } }