-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalPrefs.cpp
148 lines (126 loc) · 4.5 KB
/
GlobalPrefs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Copyright 2021 the SumatraPDF project authors (see AUTHORS file).
License: GPLv3 */
#include "utils/BaseUtil.h"
#include "utils/ScopedWin.h"
#include "utils/WinUtil.h"
#include "utils/FileUtil.h"
#include "utils/SettingsUtil.h"
#include "DisplayMode.h"
#define INCLUDE_SETTINGSSTRUCTS_METADATA
#include "SettingsStructs.h"
#include "GlobalPrefs.h"
#include "utils/Log.h"
GlobalPrefs* gGlobalPrefs = nullptr;
FileState* NewDisplayState(const char* filePath) {
FileState* fs = (FileState*)DeserializeStruct(&gFileStateInfo, nullptr);
SetFileStatePath(fs, filePath);
SetFileStateRelativePath(fs,filePath);
return fs;
}
void DeleteDisplayState(FileState* fs) {
delete fs->thumbnail;
FreeStruct(&gFileStateInfo, fs);
}
Favorite* NewFavorite(int pageNo, const WCHAR* name, const WCHAR* pageLabel) {
Favorite* fav = (Favorite*)DeserializeStruct(&gFavoriteInfo, nullptr);
fav->pageNo = pageNo;
fav->name = strconv::WstrToUtf8(name);
fav->pageLabel = strconv::WstrToUtf8(pageLabel);
return fav;
}
void DeleteFavorite(Favorite* fav) {
FreeStruct(&gFavoriteInfo, fav);
}
GlobalPrefs* NewGlobalPrefs(const char* data) {
return (GlobalPrefs*)DeserializeStruct(&gGlobalPrefsInfo, data);
}
// prevData is used to preserve fields that exists in prevField but not in GlobalPrefs
ByteSlice SerializeGlobalPrefs(GlobalPrefs* prefs, const char* prevData) {
if (!prefs->rememberStatePerDocument || !prefs->rememberOpenedFiles) {
for (FileState* fs : *prefs->fileStates) {
fs->useDefaultState = true;
}
// prevent unnecessary settings from being written out
u16 fieldCount = 0;
while (++fieldCount <= dimof(gFileStateFields)) {
// count the number of fields up to and including useDefaultState
if (gFileStateFields[fieldCount - 1].offset == offsetof(FileState, useDefaultState)) {
break;
}
}
// restore the correct fieldCount ASAP after serialization
gFileStateInfo.fieldCount = fieldCount;
}
ByteSlice serialized = SerializeStruct(&gGlobalPrefsInfo, prefs, prevData);
if (!prefs->rememberStatePerDocument || !prefs->rememberOpenedFiles) {
gFileStateInfo.fieldCount = dimof(gFileStateFields);
}
return serialized;
}
void DeleteGlobalPrefs(GlobalPrefs* gp) {
if (!gp) {
return;
}
for (FileState* ds : *gp->fileStates) {
delete ds->thumbnail;
}
FreeStruct(&gGlobalPrefsInfo, gp);
}
SessionData* NewSessionData() {
return (SessionData*)DeserializeStruct(&gSessionDataInfo, nullptr);
}
TabState* NewTabState(FileState* fs) {
TabState* state = (TabState*)DeserializeStruct(&gTabStateInfo, nullptr);
str::ReplaceWithCopy(&state->filePath, fs->filePath);
state->isRelativePath = fs->isRelativePath;
str::ReplaceWithCopy(&state->fileRelativePath, fs->fileRelativePath);
str::ReplaceWithCopy(&state->displayMode, fs->displayMode);
state->pageNo = fs->pageNo;
str::ReplaceWithCopy(&state->zoom, fs->zoom);
state->rotation = fs->rotation;
state->scrollPos = fs->scrollPos;
state->showToc = fs->showToc;
*state->tocState = *fs->tocState;
return state;
}
void ResetSessionState(Vec<SessionData*>* sessionData) {
CrashIf(!sessionData);
if (!sessionData) {
return;
}
for (SessionData* data : *sessionData) {
FreeStruct(&gSessionDataInfo, data);
}
sessionData->Reset();
}
ParsedColor* GetParsedColor(const char* s, ParsedColor& parsed) {
if (parsed.wasParsed) {
return &parsed;
}
ParseColor(parsed, s);
return &parsed;
}
void SetFileStatePath(FileState* fs, const char* path) {
if (fs->filePath && str::EqI(fs->filePath, path)) {
return;
}
str::ReplaceWithCopy(&fs->filePath, path);
}
void SetFileStateRelativePath(FileState* fs, const char* filepath) {
if (fs->fileRelativePath && str::EqI(fs->fileRelativePath, filepath)) {
return;
}
AutoFreeWstr current = GetExeDir();
AutoFreeWstr path = strconv::Utf8ToWstr(filepath);
WCHAR relativePath[MAX_PATH] = {0};
PathRelativePathToW(relativePath,current.Get(),FILE_ATTRIBUTE_DIRECTORY, path.Get(),FILE_ATTRIBUTE_NORMAL);
// discriminate: .. and .
if (relativePath[1] == L'\\') {
fs->isRelativePath = true;
str::ReplaceWithCopy(&fs->fileRelativePath, strconv::WstrToUtf8(&relativePath[1]));
}
}
void SetFileStatePath(FileState* fs, const WCHAR* path) {
char* pathA = ToUtf8Temp(path);
SetFileStatePath(fs, pathA);
}