From 89eca3384af875dc59f2563c33f8e35a923fbfe1 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Sat, 25 Jul 2020 09:12:27 +0300 Subject: Encode spaces in URLs --- src/app.c | 42 ++++++++++++++++++++++++++++++++++++------ src/gmrequest.c | 4 +++- src/gmutil.c | 9 +++++++++ src/gmutil.h | 4 +++- src/ui/inputwidget.c | 1 + src/ui/window.c | 4 +++- 6 files changed, 55 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/app.c b/src/app.c index 83619a9c..e7da4e44 100644 --- a/src/app.c +++ b/src/app.c @@ -45,17 +45,18 @@ void deinit_HistoryItem(iHistoryItem *d) { } #if defined (iPlatformApple) -static const char *dataDir_App_ = "~/Library/Application Support/fi.skyjake.Lagrange"; +static const char *dataDir_App_ = "~/Library/Application Support/fi.skyjake.Lagrange"; #endif #if defined (iPlatformMsys) -static const char *dataDir_App_ = "~/AppData/Roaming/fi.skyjake.Lagrange"; +static const char *dataDir_App_ = "~/AppData/Roaming/fi.skyjake.Lagrange"; #endif #if defined (iPlatformLinux) -static const char *dataDir_App_ = "~/.config/lagrange"; +static const char *dataDir_App_ = "~/.config/lagrange"; #endif -static const char *prefsFileName_App_ = "prefs.cfg"; +static const char *prefsFileName_App_ = "prefs.cfg"; +static const char *historyFileName_App_ = "history.txt"; -static const size_t HISTORY_MAX = 100; +static const size_t historyMax_App_ = 100; struct Impl_App { iCommandLine args; @@ -111,6 +112,10 @@ static const iString *prefsFileName_(void) { return collect_String(concatCStr_Path(&iStringLiteral(dataDir_App_), prefsFileName_App_)); } +static const iString *historyFileName_(void) { + return collect_String(concatCStr_Path(&iStringLiteral(dataDir_App_), historyFileName_App_)); +} + static void loadPrefs_App_(iApp *d) { iUnused(d); /* Create the data dir if it doesn't exist yet. */ @@ -150,6 +155,30 @@ static void savePrefs_App_(const iApp *d) { delete_String(cfg); } +static void saveHistory_App_(const iApp *d) { + iFile *f = new_File(historyFileName_()); + if (open_File(f, writeOnly_FileMode | text_FileMode)) { + iString *line = new_String(); + iConstForEach(Array, i, &d->history) { + const iHistoryItem *item = i.value; + iDate date; + init_Date(&date, &item->when); + format_String(line, + "%04d-%02d-%02dT%02d:%02d:%02d %s\n", + date.year, + date.month, + date.day, + date.hour, + date.minute, + date.second, + cstr_String(&item->url)); + writeData_File(f, cstr_String(line), size_String(line)); + } + delete_String(line); + } + iRelease(f); +} + static void clearHistory_App_(iApp *d) { iForEach(Array, i, &d->history) { deinit_HistoryItem(i.value); @@ -180,6 +209,7 @@ static void init_App_(iApp *d, int argc, char **argv) { static void deinit_App(iApp *d) { savePrefs_App_(d); + saveHistory_App_(d); clearHistory_App_(d); deinit_Array(&d->history); deinit_SortedArray(&d->tickers); @@ -385,7 +415,7 @@ iBool handleCommand_App(const char *cmd) { set_String(&item.url, url); pushBack_Array(&d->history, &item); /* Don't make it too long. */ - if (size_Array(&d->history) > HISTORY_MAX) { + if (size_Array(&d->history) > historyMax_App_) { deinit_HistoryItem(front_Array(&d->history)); remove_Array(&d->history, 0); } diff --git a/src/gmrequest.c b/src/gmrequest.c index ce53abee..6f2c2bc5 100644 --- a/src/gmrequest.c +++ b/src/gmrequest.c @@ -73,6 +73,7 @@ void deinit_GmRequest(iGmRequest *d) { void setUrl_GmRequest(iGmRequest *d, const iString *url) { set_String(&d->url, url); + urlEncodeSpaces_String(&d->url); } static uint32_t timedOutWhileReceivingBody_GmRequest_(uint32_t interval, void *obj) { @@ -174,7 +175,8 @@ void submit_GmRequest(iGmRequest *d) { iUrl url; init_Url(&url, &d->url); if (!cmpCStrSc_Rangecc(&url.protocol, "file", &iCaseInsensitive)) { - iFile *f = new_File(collect_String(newRange_String(url.path))); + iString *path = collect_String(urlDecode_String(collect_String(newRange_String(url.path)))); + iFile * f = new_File(path); if (open_File(f, readOnly_FileMode)) { /* TODO: Check supported file types: images, audio */ d->code = success_GmStatusCode; diff --git a/src/gmutil.c b/src/gmutil.c index 07861523..ce50f015 100644 --- a/src/gmutil.c +++ b/src/gmutil.c @@ -23,3 +23,12 @@ void init_Url(iUrl *d, const iString *text) { } iRelease(pattern); } + +void urlEncodeSpaces_String(iString *d) { + for (;;) { + const size_t pos = indexOfCStr_String(d, " "); + if (pos == iInvalidPos) break; + remove_Block(&d->chars, pos, 1); + insertData_Block(&d->chars, pos, "%20", 3); + } +} diff --git a/src/gmutil.h b/src/gmutil.h index 264ad8a8..41711af7 100644 --- a/src/gmutil.h +++ b/src/gmutil.h @@ -13,4 +13,6 @@ struct Impl_Url { iRangecc query; }; -void init_Url(iUrl *d, const iString *text); +void init_Url (iUrl *, const iString *text); + +void urlEncodeSpaces_String (iString *); diff --git a/src/ui/inputwidget.c b/src/ui/inputwidget.c index 22b30616..e23e5acc 100644 --- a/src/ui/inputwidget.c +++ b/src/ui/inputwidget.c @@ -311,6 +311,7 @@ static void draw_InputWidget_(const iInputWidget *d) { add_I2(topLeft_Rect(bounds), init_I2(xOff, yOff)), white_ColorId, + "%s", cstr_String(&text)); clearClip_Paint(&p); /* Cursor blinking. */ diff --git a/src/ui/window.c b/src/ui/window.c index 896b1e66..e08f6d6c 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -78,7 +78,9 @@ static iBool handleNavBarCommands_(iWidget *navBar, const char *cmd) { if (equal_Command(cmd, "input.ended")) { iInputWidget *url = findChild_Widget(navBar, "url"); if (arg_Command(cmd) && pointer_Command(cmd) == url) { - postCommandf_App("open url:%s", cstr_String(text_InputWidget(url))); + postCommandf_App( + "open url:%s", + cstr_String(text_InputWidget(url))); return iTrue; } } -- cgit v1.2.3