diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-11-04 16:04:50 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-11-04 16:04:50 +0200 |
commit | b20be8d7f701f364d7199a494c25c2694e40864d (patch) | |
tree | 4138cca208aa73eeb3658c3dc8e82b8c03e9b78d /src | |
parent | b17bbaefed9a12046d2c9874cd9b200edacbccf6 (diff) |
Windows: Improvements to file URL/path handling
IssueID #23
Diffstat (limited to 'src')
-rw-r--r-- | src/app.c | 13 | ||||
-rw-r--r-- | src/gmutil.c | 26 | ||||
-rw-r--r-- | src/gmutil.h | 1 |
3 files changed, 31 insertions, 9 deletions
@@ -362,7 +362,7 @@ static void init_App_(iApp *d, int argc, char **argv) { | |||
362 | loadPrefs_App_(d); | 362 | loadPrefs_App_(d); |
363 | load_Keys(dataDir_App_); | 363 | load_Keys(dataDir_App_); |
364 | load_Visited(d->visited, dataDir_App_); | 364 | load_Visited(d->visited, dataDir_App_); |
365 | load_Bookmarks(d->bookmarks, dataDir_App_); | 365 | load_Bookmarks(d->bookmarks, dataDir_App_); |
366 | if (isFirstRun) { | 366 | if (isFirstRun) { |
367 | /* Create the default bookmarks for a quick start. */ | 367 | /* Create the default bookmarks for a quick start. */ |
368 | add_Bookmarks(d->bookmarks, | 368 | add_Bookmarks(d->bookmarks, |
@@ -417,10 +417,10 @@ static void init_App_(iApp *d, int argc, char **argv) { | |||
417 | startsWithCase_String(arg, "gemini:") || startsWithCase_String(arg, "file:") || | 417 | startsWithCase_String(arg, "gemini:") || startsWithCase_String(arg, "file:") || |
418 | startsWithCase_String(arg, "data:") || startsWithCase_String(arg, "about:"); | 418 | startsWithCase_String(arg, "data:") || startsWithCase_String(arg, "about:"); |
419 | if (isKnownScheme || fileExists_FileInfo(arg)) { | 419 | if (isKnownScheme || fileExists_FileInfo(arg)) { |
420 | postCommandf_App("open newtab:%d url:%s%s", | 420 | postCommandf_App("open newtab:%d url:%s", |
421 | newTab, | 421 | newTab, |
422 | isKnownScheme ? "" : "file://", | 422 | isKnownScheme ? cstr_String(arg) |
423 | cstr_String(arg)); | 423 | : cstrCollect_String(makeFileUrl_String(arg))); |
424 | newTab = iTrue; | 424 | newTab = iTrue; |
425 | } | 425 | } |
426 | } | 426 | } |
@@ -501,7 +501,8 @@ void processEvents_App(enum iAppEventMode eventMode) { | |||
501 | postCommandf_App("~open newtab:%d url:%s", newTab, ev.drop.file); | 501 | postCommandf_App("~open newtab:%d url:%s", newTab, ev.drop.file); |
502 | } | 502 | } |
503 | else { | 503 | else { |
504 | postCommandf_App("~open newtab:%d url:file://%s", newTab, ev.drop.file); | 504 | postCommandf_App( |
505 | "~open newtab:%d url:%s", newTab, makeFileUrl_CStr(ev.drop.file)); | ||
505 | } | 506 | } |
506 | break; | 507 | break; |
507 | } | 508 | } |
@@ -908,7 +909,7 @@ iBool handleCommand_App(const char *cmd) { | |||
908 | postCommand_App("font.changed"); | 909 | postCommand_App("font.changed"); |
909 | postCommand_App("window.unfreeze"); | 910 | postCommand_App("window.unfreeze"); |
910 | return iTrue; | 911 | return iTrue; |
911 | } | 912 | } |
912 | else if (equal_Command(cmd, "zoom.set")) { | 913 | else if (equal_Command(cmd, "zoom.set")) { |
913 | setFreezeDraw_Window(get_Window(), iTrue); /* no intermediate draws before docs updated */ | 914 | setFreezeDraw_Window(get_Window(), iTrue); /* no intermediate draws before docs updated */ |
914 | d->prefs.zoomPercent = arg_Command(cmd); | 915 | d->prefs.zoomPercent = arg_Command(cmd); |
diff --git a/src/gmutil.c b/src/gmutil.c index 4b144097..52fe6b28 100644 --- a/src/gmutil.c +++ b/src/gmutil.c | |||
@@ -27,6 +27,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | |||
27 | #include <the_Foundation/path.h> | 27 | #include <the_Foundation/path.h> |
28 | 28 | ||
29 | void init_Url(iUrl *d, const iString *text) { | 29 | void init_Url(iUrl *d, const iString *text) { |
30 | /* Handle "file:" as a special case since it only has the path part. */ | ||
31 | if (startsWithCase_String(text, "file://")) { | ||
32 | iZap(*d); | ||
33 | const char *cstr = constBegin_String(text); | ||
34 | d->scheme = (iRangecc){ cstr, cstr + 4 }; | ||
35 | d->path = (iRangecc){ cstr + 7, constEnd_String(text) }; | ||
36 | return; | ||
37 | } | ||
30 | static iRegExp *absoluteUrlPattern_; | 38 | static iRegExp *absoluteUrlPattern_; |
31 | static iRegExp *relativeUrlPattern_; | 39 | static iRegExp *relativeUrlPattern_; |
32 | if (!absoluteUrlPattern_) { | 40 | if (!absoluteUrlPattern_) { |
@@ -98,8 +106,11 @@ void cleanUrlPath_String(iString *d) { | |||
98 | else if (equal_Rangecc(seg, ".")) { | 106 | else if (equal_Rangecc(seg, ".")) { |
99 | /* Skip it. */ | 107 | /* Skip it. */ |
100 | } | 108 | } |
101 | else { | 109 | else if (!isEmpty_Range(&seg)) { |
102 | appendCStr_String(&clean, "/"); | 110 | /* Ensure the cleaned path starts with a slash if the original does. */ |
111 | if (!isEmpty_String(&clean) || startsWith_Rangecc(parts.path, "/")) { | ||
112 | appendCStr_String(&clean, "/"); | ||
113 | } | ||
103 | appendRange_String(&clean, seg); | 114 | appendRange_String(&clean, seg); |
104 | } | 115 | } |
105 | } | 116 | } |
@@ -127,6 +138,10 @@ iRangecc urlHost_String(const iString *d) { | |||
127 | return url.host; | 138 | return url.host; |
128 | } | 139 | } |
129 | 140 | ||
141 | static iBool isAbsolutePath_(iRangecc path) { | ||
142 | return isAbsolute_Path(collect_String(urlDecode_String(collect_String(newRange_String(path))))); | ||
143 | } | ||
144 | |||
130 | const iString *absoluteUrl_String(const iString *d, const iString *urlMaybeRelative) { | 145 | const iString *absoluteUrl_String(const iString *d, const iString *urlMaybeRelative) { |
131 | iUrl orig; | 146 | iUrl orig; |
132 | iUrl rel; | 147 | iUrl rel; |
@@ -155,7 +170,7 @@ const iString *absoluteUrl_String(const iString *d, const iString *urlMaybeRelat | |||
155 | appendRange_String(absolute, selHost->port); | 170 | appendRange_String(absolute, selHost->port); |
156 | } | 171 | } |
157 | } | 172 | } |
158 | if (isDef_(rel.scheme) || isDef_(rel.host) || startsWith_Rangecc(rel.path, "/")) { | 173 | if (isDef_(rel.scheme) || isDef_(rel.host) || isAbsolutePath_(rel.path)) { |
159 | appendRange_String(absolute, isDef_(rel.path) ? rel.path : range_CStr("/")); /* absolute path */ | 174 | appendRange_String(absolute, isDef_(rel.path) ? rel.path : range_CStr("/")); /* absolute path */ |
160 | } | 175 | } |
161 | else { | 176 | else { |
@@ -180,10 +195,15 @@ const iString *absoluteUrl_String(const iString *d, const iString *urlMaybeRelat | |||
180 | iString *makeFileUrl_String(const iString *localFilePath) { | 195 | iString *makeFileUrl_String(const iString *localFilePath) { |
181 | iString *url = cleaned_Path(localFilePath); | 196 | iString *url = cleaned_Path(localFilePath); |
182 | replace_Block(&url->chars, '\\', '/'); /* in case it's a Windows path */ | 197 | replace_Block(&url->chars, '\\', '/'); /* in case it's a Windows path */ |
198 | set_String(url, collect_String(urlEncodeExclude_String(url, "/"))); | ||
183 | prependCStr_String(url, "file://"); | 199 | prependCStr_String(url, "file://"); |
184 | return url; | 200 | return url; |
185 | } | 201 | } |
186 | 202 | ||
203 | const char *makeFileUrl_CStr(const char *localFilePath) { | ||
204 | return cstrCollect_String(makeFileUrl_String(collectNewCStr_String(localFilePath))); | ||
205 | } | ||
206 | |||
187 | void urlEncodeSpaces_String(iString *d) { | 207 | void urlEncodeSpaces_String(iString *d) { |
188 | for (;;) { | 208 | for (;;) { |
189 | const size_t pos = indexOfCStr_String(d, " "); | 209 | const size_t pos = indexOfCStr_String(d, " "); |
diff --git a/src/gmutil.h b/src/gmutil.h index 8b4f7033..97c2d675 100644 --- a/src/gmutil.h +++ b/src/gmutil.h | |||
@@ -103,4 +103,5 @@ iRangecc urlScheme_String (const iString *); | |||
103 | iRangecc urlHost_String (const iString *); | 103 | iRangecc urlHost_String (const iString *); |
104 | const iString * absoluteUrl_String (const iString *, const iString *urlMaybeRelative); | 104 | const iString * absoluteUrl_String (const iString *, const iString *urlMaybeRelative); |
105 | iString * makeFileUrl_String (const iString *localFilePath); | 105 | iString * makeFileUrl_String (const iString *localFilePath); |
106 | const char * makeFileUrl_CStr (const char *localFilePath); | ||
106 | void urlEncodeSpaces_String (iString *); | 107 | void urlEncodeSpaces_String (iString *); |