summaryrefslogtreecommitdiff
path: root/src/sitespec.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-12-01 13:55:11 +0200
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-12-01 13:55:11 +0200
commitf4942e1b4da6dc1334dcdb4f2daae670bfa1f813 (patch)
tree3b0b638d4351311e8322e9c35da11078c27585c0 /src/sitespec.c
parentb3550138da3a669999c06da41720f2be664d5e86 (diff)
Added switching to the identity toolbar menu
One can now use the identity toolbar menu to switch between client certificates that have been used on the current site. The five latest ones are remembered.
Diffstat (limited to 'src/sitespec.c')
-rw-r--r--src/sitespec.c81
1 files changed, 73 insertions, 8 deletions
diff --git a/src/sitespec.c b/src/sitespec.c
index 6f4546f0..f8b77c86 100644
--- a/src/sitespec.c
+++ b/src/sitespec.c
@@ -25,6 +25,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
25#include <the_Foundation/file.h> 25#include <the_Foundation/file.h>
26#include <the_Foundation/path.h> 26#include <the_Foundation/path.h>
27#include <the_Foundation/stringhash.h> 27#include <the_Foundation/stringhash.h>
28#include <the_Foundation/stringarray.h>
28#include <the_Foundation/toml.h> 29#include <the_Foundation/toml.h>
29 30
30iDeclareClass(SiteParams) 31iDeclareClass(SiteParams)
@@ -35,6 +36,7 @@ struct Impl_SiteParams {
35 uint16_t titanPort; 36 uint16_t titanPort;
36 iString titanIdentity; /* fingerprint */ 37 iString titanIdentity; /* fingerprint */
37 int dismissWarnings; 38 int dismissWarnings;
39 iStringArray usedIdentities; /* fingerprints; latest ones at the end */
38 /* TODO: theme seed, style settings */ 40 /* TODO: theme seed, style settings */
39}; 41};
40 42
@@ -42,12 +44,23 @@ void init_SiteParams(iSiteParams *d) {
42 d->titanPort = 0; /* undefined */ 44 d->titanPort = 0; /* undefined */
43 init_String(&d->titanIdentity); 45 init_String(&d->titanIdentity);
44 d->dismissWarnings = 0; 46 d->dismissWarnings = 0;
47 init_StringArray(&d->usedIdentities);
45} 48}
46 49
47void deinit_SiteParams(iSiteParams *d) { 50void deinit_SiteParams(iSiteParams *d) {
51 deinit_StringArray(&d->usedIdentities);
48 deinit_String(&d->titanIdentity); 52 deinit_String(&d->titanIdentity);
49} 53}
50 54
55static size_t findUsedIdentity_SiteParams_(const iSiteParams *d, const iString *fingerprint) {
56 iConstForEach(StringArray, i, &d->usedIdentities) {
57 if (equal_String(i.value, fingerprint)) {
58 return index_StringArrayConstIterator(&i);
59 }
60 }
61 return iInvalidPos;
62}
63
51iDefineClass(SiteParams) 64iDefineClass(SiteParams)
52iDefineObjectConstruction(SiteParams) 65iDefineObjectConstruction(SiteParams)
53 66
@@ -130,6 +143,12 @@ static void handleIniKeyValue_SiteSpec_(void *context, const iString *table, con
130 else if (!cmp_String(key, "dismissWarnings") && value->type == int64_TomlType) { 143 else if (!cmp_String(key, "dismissWarnings") && value->type == int64_TomlType) {
131 d->loadParams->dismissWarnings = value->value.int64; 144 d->loadParams->dismissWarnings = value->value.int64;
132 } 145 }
146 else if (!cmp_String(key, "usedIdentities") && value->type == string_TomlType) {
147 iRangecc seg = iNullRange;
148 while (nextSplit_Rangecc(range_String(value->value.string), " ", &seg)) {
149 pushBack_StringArray(&d->loadParams->usedIdentities, collectNewRange_String(seg));
150 }
151 }
133} 152}
134 153
135static iBool load_SiteSpec_(iSiteSpec *d) { 154static iBool load_SiteSpec_(iSiteSpec *d) {
@@ -151,6 +170,7 @@ static void save_SiteSpec_(iSiteSpec *d) {
151 if (open_File(f, writeOnly_FileMode | text_FileMode)) { 170 if (open_File(f, writeOnly_FileMode | text_FileMode)) {
152 iString *buf = new_String(); 171 iString *buf = new_String();
153 iConstForEach(StringHash, i, &d->sites) { 172 iConstForEach(StringHash, i, &d->sites) {
173 iBeginCollect();
154 const iBlock * key = &i.value->keyBlock; 174 const iBlock * key = &i.value->keyBlock;
155 const iSiteParams *params = i.value->object; 175 const iSiteParams *params = i.value->object;
156 format_String(buf, "[%s]\n", cstr_Block(key)); 176 format_String(buf, "[%s]\n", cstr_Block(key));
@@ -164,8 +184,15 @@ static void save_SiteSpec_(iSiteSpec *d) {
164 if (params->dismissWarnings) { 184 if (params->dismissWarnings) {
165 appendFormat_String(buf, "dismissWarnings = 0x%x\n", params->dismissWarnings); 185 appendFormat_String(buf, "dismissWarnings = 0x%x\n", params->dismissWarnings);
166 } 186 }
187 if (!isEmpty_StringArray(&params->usedIdentities)) {
188 appendFormat_String(
189 buf,
190 "usedIdentities = \"%s\"\n",
191 cstrCollect_String(joinCStr_StringArray(&params->usedIdentities, " ")));
192 }
167 appendCStr_String(buf, "\n"); 193 appendCStr_String(buf, "\n");
168 write_File(f, utf8_String(buf)); 194 write_File(f, utf8_String(buf));
195 iEndCollect();
169 } 196 }
170 delete_String(buf); 197 delete_String(buf);
171 } 198 }
@@ -188,14 +215,19 @@ void deinit_SiteSpec(void) {
188 deinit_String(&d->saveDir); 215 deinit_String(&d->saveDir);
189} 216}
190 217
191void setValue_SiteSpec(const iString *site, enum iSiteSpecKey key, int value) { 218static iSiteParams *findParams_SiteSpec_(iSiteSpec *d, const iString *site) {
192 iSiteSpec *d = &siteSpec_;
193 const iString *hashKey = collect_String(lower_String(site)); 219 const iString *hashKey = collect_String(lower_String(site));
194 iSiteParams *params = value_StringHash(&d->sites, hashKey); 220 iSiteParams *params = value_StringHash(&d->sites, hashKey);
195 if (!params) { 221 if (!params) {
196 params = new_SiteParams(); 222 params = new_SiteParams();
197 insert_StringHash(&d->sites, hashKey, params); 223 insert_StringHash(&d->sites, hashKey, params);
198 } 224 }
225 return params;
226}
227
228void setValue_SiteSpec(const iString *site, enum iSiteSpecKey key, int value) {
229 iSiteSpec *d = &siteSpec_;
230 iSiteParams *params = findParams_SiteSpec_(d, site);
199 iBool needSave = iFalse; 231 iBool needSave = iFalse;
200 switch (key) { 232 switch (key) {
201 case titanPort_SiteSpecKey: 233 case titanPort_SiteSpecKey:
@@ -216,12 +248,7 @@ void setValue_SiteSpec(const iString *site, enum iSiteSpecKey key, int value) {
216 248
217void setValueString_SiteSpec(const iString *site, enum iSiteSpecKey key, const iString *value) { 249void setValueString_SiteSpec(const iString *site, enum iSiteSpecKey key, const iString *value) {
218 iSiteSpec *d = &siteSpec_; 250 iSiteSpec *d = &siteSpec_;
219 const iString *hashKey = collect_String(lower_String(site)); 251 iSiteParams *params = findParams_SiteSpec_(d, site);
220 iSiteParams *params = value_StringHash(&d->sites, hashKey);
221 if (!params) {
222 params = new_SiteParams();
223 insert_StringHash(&d->sites, hashKey, params);
224 }
225 iBool needSave = iFalse; 252 iBool needSave = iFalse;
226 switch (key) { 253 switch (key) {
227 case titanIdentity_SiteSpecKey: 254 case titanIdentity_SiteSpecKey:
@@ -238,6 +265,44 @@ void setValueString_SiteSpec(const iString *site, enum iSiteSpecKey key, const i
238 } 265 }
239} 266}
240 267
268static void insertOrRemoveString_SiteSpec_(iSiteSpec *d, const iString *site, enum iSiteSpecKey key,
269 const iString *value, iBool doInsert) {
270 iSiteParams *params = findParams_SiteSpec_(d, site);
271 iBool needSave = iFalse;
272 switch (key) {
273 case usedIdentities_SiteSpecKey: {
274 const size_t index = findUsedIdentity_SiteParams_(params, value);
275 if (doInsert && index == iInvalidPos) {
276 pushBack_StringArray(&params->usedIdentities, value);
277 needSave = iTrue;
278 }
279 else if (!doInsert && index != iInvalidPos) {
280 remove_StringArray(&params->usedIdentities, index);
281 needSave = iTrue;
282 }
283 break;
284 }
285 default:
286 break;
287 }
288 if (needSave) {
289 save_SiteSpec_(d);
290 }
291}
292
293void insertString_SiteSpec(const iString *site, enum iSiteSpecKey key, const iString *value) {
294 insertOrRemoveString_SiteSpec_(&siteSpec_, site, key, value, iTrue);
295}
296
297void removeString_SiteSpec(const iString *site, enum iSiteSpecKey key, const iString *value) {
298 insertOrRemoveString_SiteSpec_(&siteSpec_, site, key, value, iFalse);
299}
300
301const iStringArray *strings_SiteSpec(const iString *site, enum iSiteSpecKey key) {
302 const iSiteParams *params = findParams_SiteSpec_(&siteSpec_, site);
303 return &params->usedIdentities;
304}
305
241int value_SiteSpec(const iString *site, enum iSiteSpecKey key) { 306int value_SiteSpec(const iString *site, enum iSiteSpecKey key) {
242 iSiteSpec *d = &siteSpec_; 307 iSiteSpec *d = &siteSpec_;
243 const iSiteParams *params = constValue_StringHash(&d->sites, collect_String(lower_String(site))); 308 const iSiteParams *params = constValue_StringHash(&d->sites, collect_String(lower_String(site)));