summaryrefslogtreecommitdiff
path: root/src/history.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-08-12 10:28:02 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-08-12 10:28:02 +0300
commit1d54f7b990ea7f676403681577fc4df984cab0be (patch)
tree94a8ac83159f021dbbb354a6d76ecbaf83a63aad /src/history.c
parent38e09f15ff3e9c4781236016bfbb0b0f9062590b (diff)
Save and load app state (tabs, history)
Diffstat (limited to 'src/history.c')
-rw-r--r--src/history.c66
1 files changed, 37 insertions, 29 deletions
diff --git a/src/history.c b/src/history.c
index 5be24608..9779795a 100644
--- a/src/history.c
+++ b/src/history.c
@@ -55,39 +55,38 @@ iHistory *copy_History(const iHistory *d) {
55 return copy; 55 return copy;
56} 56}
57 57
58void save_History(const iHistory *d, const char *dirPath) { 58void serialize_History(const iHistory *d, iStream *outs) {
59 iString *line = new_String(); 59 writeU16_Stream(outs, d->recentPos);
60 iFile *f = newCStr_File(concatPath_CStr(dirPath, "recent.txt")); 60 writeU16_Stream(outs, size_Array(&d->recent));
61 if (open_File(f, writeOnly_FileMode | text_FileMode)) { 61 iConstForEach(Array, i, &d->recent) {
62 iConstForEach(Array, i, &d->recent) { 62 const iRecentUrl *item = i.value;
63 const iRecentUrl *item = i.value; 63 serialize_String(&item->url, outs);
64 format_String(line, "%04x %s\n", item->scrollY, cstr_String(&item->url)); 64 write32_Stream(outs, item->scrollY);
65 writeData_File(f, cstr_String(line), size_String(line)); 65 if (item->cachedResponse) {
66 write8_Stream(outs, 1);
67 serialize_GmResponse(item->cachedResponse, outs);
68 }
69 else {
70 write8_Stream(outs, 0);
66 } 71 }
67 } 72 }
68 iRelease(f); 73}
69 delete_String(line); 74
70} 75void deserialize_History(iHistory *d, iStream *ins) {
71 76 clear_History(d);
72void load_History(iHistory *d, const char *dirPath) { 77 d->recentPos = readU16_Stream(ins);
73 iFile *f = newCStr_File(concatPath_CStr(dirPath, "recent.txt")); 78 size_t count = readU16_Stream(ins);
74 if (open_File(f, readOnly_FileMode | text_FileMode)) { 79 while (count--) {
75 const iRangecc src = range_Block(collect_Block(readAll_File(f))); 80 iRecentUrl item;
76 iRangecc line = iNullRange; 81 init_RecentUrl(&item);
77 while (nextSplit_Rangecc(&src, "\n", &line)) { 82 deserialize_String(&item.url, ins);
78 iRangecc nonwhite = line; 83 item.scrollY = read32_Stream(ins);
79 trim_Rangecc(&nonwhite); 84 if (read8_Stream(ins)) {
80 if (isEmpty_Range(&nonwhite)) continue; 85 item.cachedResponse = new_GmResponse();
81 int scroll = 0; 86 deserialize_GmResponse(item.cachedResponse, ins);
82 sscanf(nonwhite.start, "%04x", &scroll);
83 iRecentUrl item;
84 init_RecentUrl(&item);
85 item.scrollY = scroll;
86 initRange_String(&item.url, (iRangecc){ nonwhite.start + 5, nonwhite.end });
87 pushBack_Array(&d->recent, &item);
88 } 87 }
88 pushBack_Array(&d->recent, &item);
89 } 89 }
90 iRelease(f);
91} 90}
92 91
93void clear_History(iHistory *d) { 92void clear_History(iHistory *d) {
@@ -123,6 +122,15 @@ const iString *url_History(const iHistory *d, size_t pos) {
123 return collectNew_String(); 122 return collectNew_String();
124} 123}
125 124
125iRecentUrl *findUrl_History(iHistory *d, const iString *url) {
126 iReverseForEach(Array, i, &d->recent) {
127 if (cmpStringCase_String(url, &((iRecentUrl *) i.value)->url) == 0) {
128 return i.value;
129 }
130 }
131 return NULL;
132}
133
126void replace_History(iHistory *d, const iString *url) { 134void replace_History(iHistory *d, const iString *url) {
127 /* Update in the history. */ 135 /* Update in the history. */
128 iRecentUrl *item = mostRecentUrl_History(d); 136 iRecentUrl *item = mostRecentUrl_History(d);