summaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-07-28 09:00:05 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-07-28 09:00:05 +0300
commit0700048de99c4a00094d594dcf321637514f3a6a (patch)
tree7752bab03bc7bed5c20346116772e46e7f70fce0 /src/ui
parent96201f4fab0ac0d97211184d2cf93b1999f8a8b8 (diff)
Plaintext documents; unsupported MIME type error
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/documentwidget.c69
1 files changed, 44 insertions, 25 deletions
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c
index d78a9238..882e029a 100644
--- a/src/ui/documentwidget.c
+++ b/src/ui/documentwidget.c
@@ -191,13 +191,55 @@ static void setSource_DocumentWidget_(iDocumentWidget *d, const iString *source)
191 refresh_Widget(as_Widget(d)); 191 refresh_Widget(as_Widget(d));
192} 192}
193 193
194static void showErrorPage_DocumentWidget_(iDocumentWidget *d, enum iGmStatusCode code) {
195 iString *src = collectNew_String();
196 const iGmError *msg = get_GmError(code);
197 format_String(src,
198 "# %lc %s\n%s",
199 msg->icon ? msg->icon : 0x2327, /* X in a box */
200 msg->title,
201 msg->info);
202 switch (code) {
203 case failedToOpenFile_GmStatusCode:
204 case certificateNotValid_GmStatusCode:
205 appendFormat_String(src, "\n\n%s", cstr_String(meta_GmRequest(d->request)));
206 break;
207 case unsupportedMimeType_GmStatusCode:
208 appendFormat_String(src, "\n```\n%s\n```\n", cstr_String(meta_GmRequest(d->request)));
209 break;
210 case slowDown_GmStatusCode:
211 appendFormat_String(src, "\n\nWait %s seconds before your next request.",
212 cstr_String(meta_GmRequest(d->request)));
213 break;
214 default:
215 break;
216 }
217 setSource_DocumentWidget_(d, src);
218}
219
194static void updateSource_DocumentWidget_(iDocumentWidget *d) { 220static void updateSource_DocumentWidget_(iDocumentWidget *d) {
195 /* TODO: Do this in the background. However, that requires a text metrics calculator 221 /* TODO: Do this in the background. However, that requires a text metrics calculator
196 that does not try to cache the glyph bitmaps. */ 222 that does not try to cache the glyph bitmaps. */
197 if (status_GmRequest(d->request) != input_GmStatusCode && 223 const enum iGmStatusCode statusCode = status_GmRequest(d->request);
198 status_GmRequest(d->request) != sensitiveInput_GmStatusCode) { 224 if (statusCode != input_GmStatusCode &&
225 statusCode != sensitiveInput_GmStatusCode) {
199 iString str; 226 iString str;
200 initBlock_String(&str, body_GmRequest(d->request)); 227 initBlock_String(&str, body_GmRequest(d->request));
228 if (statusCode == success_GmStatusCode) {
229 /* Check the MIME type. */
230 const iString *mime = meta_GmRequest(d->request);
231 if (startsWith_String(mime, "text/plain")) {
232 setFormat_GmDocument(d->doc, plainText_GmDocumentFormat);
233 }
234 else if (startsWith_String(mime, "text/gemini")) {
235 setFormat_GmDocument(d->doc, gemini_GmDocumentFormat);
236 }
237 else {
238 showErrorPage_DocumentWidget_(d, unsupportedMimeType_GmStatusCode);
239 deinit_String(&str);
240 return;
241 }
242 }
201 setSource_DocumentWidget_(d, &str); 243 setSource_DocumentWidget_(d, &str);
202 deinit_String(&str); 244 deinit_String(&str);
203 } 245 }
@@ -317,29 +359,6 @@ static const iString *absoluteUrl_DocumentWidget_(const iDocumentWidget *d, cons
317 return collect_String(absolute); 359 return collect_String(absolute);
318} 360}
319 361
320static void showErrorPage_DocumentWidget_(iDocumentWidget *d, enum iGmStatusCode code) {
321 iString *src = collectNew_String();
322 const iGmError *msg = get_GmError(code);
323 format_String(src,
324 "# %lc %s\n%s",
325 msg->icon ? msg->icon : 0x2327, /* X in a box */
326 msg->title,
327 msg->info);
328 switch (code) {
329 case failedToOpenFile_GmStatusCode:
330 case certificateNotValid_GmStatusCode:
331 appendFormat_String(src, "\n\n%s", cstr_String(meta_GmRequest(d->request)));
332 break;
333 case slowDown_GmStatusCode:
334 appendFormat_String(src, "\n\nWait %s seconds before your next request.",
335 cstr_String(meta_GmRequest(d->request)));
336 break;
337 default:
338 break;
339 }
340 setSource_DocumentWidget_(d, src);
341}
342
343static void checkResponseCode_DocumentWidget_(iDocumentWidget *d) { 362static void checkResponseCode_DocumentWidget_(iDocumentWidget *d) {
344 if (!d->request) { 363 if (!d->request) {
345 return; 364 return;