summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-07-22 07:21:02 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-07-22 07:21:02 +0300
commit2c514c6842c612814fbbf5eef591eb591c1345c3 (patch)
tree80359bd4656aa60da2d3da9108e0a76ea8cc6ebc
parentcd8b26d5707edd573aa18686ef2aa2b664dc0591 (diff)
DocumentWidget: Reading from file://
-rw-r--r--src/ui/documentwidget.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c
index aab010c9..7318e4cb 100644
--- a/src/ui/documentwidget.c
+++ b/src/ui/documentwidget.c
@@ -4,6 +4,7 @@
4#include "../gemini.h" 4#include "../gemini.h"
5#include "../gmdocument.h" 5#include "../gmdocument.h"
6 6
7#include <the_Foundation/file.h>
7#include <the_Foundation/regexp.h> 8#include <the_Foundation/regexp.h>
8#include <the_Foundation/tlsrequest.h> 9#include <the_Foundation/tlsrequest.h>
9 10
@@ -22,6 +23,7 @@ struct Impl_DocumentWidget {
22 int statusCode; 23 int statusCode;
23 iString *newSource; 24 iString *newSource;
24 iGmDocument *doc; 25 iGmDocument *doc;
26 int pageMargin;
25}; 27};
26 28
27iDeclareType(Url) 29iDeclareType(Url)
@@ -36,7 +38,7 @@ struct Impl_Url {
36 38
37void init_Url(iUrl *d, const iString *text) { 39void init_Url(iUrl *d, const iString *text) {
38 iRegExp *pattern = 40 iRegExp *pattern =
39 new_RegExp("(.+)://([^/:?]+)(:[0-9]+)?([^?]*)(\\?.*)?", caseInsensitive_RegExpOption); 41 new_RegExp("(.+)://([^/:?]*)(:[0-9]+)?([^?]*)(\\?.*)?", caseInsensitive_RegExpOption);
40 iRegExpMatch m; 42 iRegExpMatch m;
41 if (matchString_RegExp(pattern, text, &m)) { 43 if (matchString_RegExp(pattern, text, &m)) {
42 capturedRange_RegExpMatch(&m, 1, &d->protocol); 44 capturedRange_RegExpMatch(&m, 1, &d->protocol);
@@ -67,7 +69,8 @@ void init_DocumentWidget(iDocumentWidget *d) {
67 d->request = NULL; 69 d->request = NULL;
68 d->newSource = new_String(); 70 d->newSource = new_String();
69 d->doc = new_GmDocument(); 71 d->doc = new_GmDocument();
70 setUrl_DocumentWidget(d, collectNewCStr_String("gemini.circumlunar.space/")); 72 d->pageMargin = 5;
73 setUrl_DocumentWidget(d, collectNewCStr_String("file:///Users/jaakko/test.gmi"));
71} 74}
72 75
73void deinit_DocumentWidget(iDocumentWidget *d) { 76void deinit_DocumentWidget(iDocumentWidget *d) {
@@ -79,7 +82,7 @@ void deinit_DocumentWidget(iDocumentWidget *d) {
79static int documentWidth_DocumentWidget_(const iDocumentWidget *d) { 82static int documentWidth_DocumentWidget_(const iDocumentWidget *d) {
80 const iWidget *w = constAs_Widget(d); 83 const iWidget *w = constAs_Widget(d);
81 const iRect bounds = bounds_Widget(w); 84 const iRect bounds = bounds_Widget(w);
82 return bounds.size.x; 85 return bounds.size.x - gap_UI * d->pageMargin * 2;
83} 86}
84 87
85void setSource_DocumentWidget(iDocumentWidget *d, const iString *source) { 88void setSource_DocumentWidget(iDocumentWidget *d, const iString *source) {
@@ -121,6 +124,15 @@ static void fetch_DocumentWidget_(iDocumentWidget *d) {
121 d->statusCode = 0; 124 d->statusCode = 0;
122 iUrl url; 125 iUrl url;
123 init_Url(&url, d->url); 126 init_Url(&url, d->url);
127 if (!cmpCStrSc_Rangecc(&url.protocol, "file", &iCaseInsensitive)) {
128 iFile *f = new_File(collect_String(newRange_String(url.path)));
129 if (open_File(f, readOnly_FileMode)) {
130 setBlock_String(d->newSource, collect_Block(readAll_File(f)));
131 }
132 iRelease(f);
133 d->state = ready_DocumentState;
134 return;
135 }
124 d->request = new_TlsRequest(); 136 d->request = new_TlsRequest();
125 uint16_t port = toInt_String(collect_String(newRange_String(url.port))); 137 uint16_t port = toInt_String(collect_String(newRange_String(url.port)));
126 if (port == 0) { 138 if (port == 0) {
@@ -184,6 +196,7 @@ static void draw_DocumentWidget_(const iDocumentWidget *d) {
184 } 196 }
185 if (d->state != ready_DocumentState) return; 197 if (d->state != ready_DocumentState) return;
186 iDrawContext ctx = {.d = d, .bounds = bounds_Widget(w) }; 198 iDrawContext ctx = {.d = d, .bounds = bounds_Widget(w) };
199 shrink_Rect(&ctx.bounds, init1_I2(gap_UI * d->pageMargin));
187 init_Paint(&ctx.paint); 200 init_Paint(&ctx.paint);
188 render_GmDocument(d->doc, (iRangei){ 0, height_Rect(ctx.bounds) }, drawRun_DrawContext_, &ctx); 201 render_GmDocument(d->doc, (iRangei){ 0, height_Rect(ctx.bounds) }, drawRun_DrawContext_, &ctx);
189} 202}