summaryrefslogtreecommitdiff
path: root/src/ui/uploadwidget.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-07-24 08:37:34 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-07-24 08:37:34 +0300
commit7d6ff9d0fadfe281fedf2f5da26ef577cfe07529 (patch)
tree8cb47924f95e47c73b18e6cdff7a36dfa15bac61 /src/ui/uploadwidget.c
parent4824970e2b07fc89d428095ce27113756adbe47a (diff)
Added site-specific configuration; default Titan port
When using the upload shortcut, enable configuring a specific port for Titan via site-specific parameters.
Diffstat (limited to 'src/ui/uploadwidget.c')
-rw-r--r--src/ui/uploadwidget.c61
1 files changed, 53 insertions, 8 deletions
diff --git a/src/ui/uploadwidget.c b/src/ui/uploadwidget.c
index eb8039c3..22c3adf6 100644
--- a/src/ui/uploadwidget.c
+++ b/src/ui/uploadwidget.c
@@ -27,6 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
27#include "color.h" 27#include "color.h"
28#include "command.h" 28#include "command.h"
29#include "gmrequest.h" 29#include "gmrequest.h"
30#include "sitespec.h"
30#include "app.h" 31#include "app.h"
31 32
32#include <the_Foundation/file.h> 33#include <the_Foundation/file.h>
@@ -36,6 +37,7 @@ iDefineObjectConstruction(UploadWidget)
36 37
37struct Impl_UploadWidget { 38struct Impl_UploadWidget {
38 iWidget widget; 39 iWidget widget;
40 iString originalUrl;
39 iString url; 41 iString url;
40 iDocumentWidget *viewer; 42 iDocumentWidget *viewer;
41 iGmRequest * request; 43 iGmRequest * request;
@@ -65,6 +67,7 @@ void init_UploadWidget(iUploadWidget *d) {
65 init_Widget(w); 67 init_Widget(w);
66 setId_Widget(w, "upload"); 68 setId_Widget(w, "upload");
67 useSheetStyle_Widget(w); 69 useSheetStyle_Widget(w);
70 init_String(&d->originalUrl);
68 init_String(&d->url); 71 init_String(&d->url);
69 d->viewer = NULL; 72 d->viewer = NULL;
70 d->request = NULL; 73 d->request = NULL;
@@ -126,15 +129,17 @@ void init_UploadWidget(iUploadWidget *d) {
126 /* Buttons. */ { 129 /* Buttons. */ {
127 addChild_Widget(w, iClob(makePadding_Widget(gap_UI))); 130 addChild_Widget(w, iClob(makePadding_Widget(gap_UI)));
128 iWidget *buttons = 131 iWidget *buttons =
129 makeDialogButtons_Widget((iMenuItem[]){ { "${cancel}", SDLK_ESCAPE, 0, "upload.cancel" }, 132 makeDialogButtons_Widget((iMenuItem[]){ { "${upload.port}", 0, 0, "upload.setport" },
133 { "---", 0, 0, NULL },
134 { "${cancel}", SDLK_ESCAPE, 0, "upload.cancel" },
130 { uiTextAction_ColorEscape "${dlg.upload.send}", 135 { uiTextAction_ColorEscape "${dlg.upload.send}",
131 SDLK_RETURN, 136 SDLK_RETURN,
132 KMOD_PRIMARY, 137 KMOD_PRIMARY,
133 "upload.accept" } }, 138 "upload.accept" } },
134 2); 139 4);
135 setId_Widget(addChildPosFlags_Widget(buttons, 140 setId_Widget(insertChildAfterFlags_Widget(buttons,
136 iClob(d->counter = new_LabelWidget("", NULL)), 141 iClob(d->counter = new_LabelWidget("", NULL)),
137 front_WidgetAddPos, frameless_WidgetFlag), 142 0, frameless_WidgetFlag),
138 "upload.counter"); 143 "upload.counter");
139 addChild_Widget(w, iClob(buttons)); 144 addChild_Widget(w, iClob(buttons));
140 } 145 }
@@ -146,20 +151,44 @@ void init_UploadWidget(iUploadWidget *d) {
146 setBackupFileName_InputWidget(d->input, "uploadbackup.txt"); 151 setBackupFileName_InputWidget(d->input, "uploadbackup.txt");
147} 152}
148 153
149void deinit_UploadWidget(iUploadWidget *d) { 154void deinit_UploadWidget(iUploadWidget *d) {
150 deinit_String(&d->filePath); 155 deinit_String(&d->filePath);
151 deinit_String(&d->url); 156 deinit_String(&d->url);
157 deinit_String(&d->originalUrl);
152 iRelease(d->request); 158 iRelease(d->request);
153} 159}
154 160
155void setUrl_UploadWidget(iUploadWidget *d, const iString *url) { 161static uint16_t titanPortForUrl_(const iString *url) {
162 uint16_t port = 0;
163 const iString *root = collectNewRange_String(urlRoot_String(url));
156 iUrl parts; 164 iUrl parts;
157 init_Url(&parts, url); 165 init_Url(&parts, url);
166 /* If the port is not specified, use the site-specific configuration. */
167 if (isEmpty_Range(&parts.port) || equalCase_Rangecc(parts.scheme, "gemini")) {
168 port = value_SiteSpec(root, titanPort_SiteSpecKey);
169 }
170 else {
171 port = atoi(cstr_Rangecc(parts.port));
172 }
173 return port ? port : GEMINI_DEFAULT_PORT;
174}
175
176static void setUrlPort_UploadWidget_(iUploadWidget *d, const iString *url, uint16_t overridePort) {
177 set_String(&d->originalUrl, url);
178 iUrl parts;
179 const iString *root = collectNewRange_String(urlRoot_String(url));
180 init_Url(&parts, url);
158 setCStr_String(&d->url, "titan"); 181 setCStr_String(&d->url, "titan");
159 appendRange_String(&d->url, (iRangecc){ parts.scheme.end, constEnd_String(url) }); 182 appendRange_String(&d->url, (iRangecc){ parts.scheme.end, parts.host.end });
183 appendFormat_String(&d->url, ":%u", overridePort ? overridePort : titanPortForUrl_(url));
184 appendRange_String(&d->url, (iRangecc){ parts.path.start, constEnd_String(url) });
160 setText_LabelWidget(d->info, &d->url); 185 setText_LabelWidget(d->info, &d->url);
161} 186}
162 187
188void setUrl_UploadWidget(iUploadWidget *d, const iString *url) {
189 setUrlPort_UploadWidget_(d, url, 0);
190}
191
163void setResponseViewer_UploadWidget(iUploadWidget *d, iDocumentWidget *doc) { 192void setResponseViewer_UploadWidget(iUploadWidget *d, iDocumentWidget *doc) {
164 d->viewer = doc; 193 d->viewer = doc;
165} 194}
@@ -182,13 +211,29 @@ static void requestFinished_UploadWidget_(iUploadWidget *d, iGmRequest *req) {
182 211
183static iBool processEvent_UploadWidget_(iUploadWidget *d, const SDL_Event *ev) { 212static iBool processEvent_UploadWidget_(iUploadWidget *d, const SDL_Event *ev) {
184 iWidget *w = as_Widget(d); 213 iWidget *w = as_Widget(d);
214 const char *cmd = command_UserEvent(ev);
185 if (isCommand_Widget(w, ev, "upload.cancel")) { 215 if (isCommand_Widget(w, ev, "upload.cancel")) {
186 /* TODO: If text has been entered, ask for confirmation. */ 216 /* TODO: If text has been entered, ask for confirmation. */
187 setupSheetTransition_Mobile(w, iFalse); 217 setupSheetTransition_Mobile(w, iFalse);
188 destroy_Widget(w); 218 destroy_Widget(w);
189 return iTrue; 219 return iTrue;
190 } 220 }
191 const char *cmd = command_UserEvent(ev); 221 if (isCommand_Widget(w, ev, "upload.setport")) {
222 if (hasLabel_Command(cmd, "value")) {
223 setValue_SiteSpec(collectNewRange_String(urlRoot_String(&d->originalUrl)),
224 titanPort_SiteSpecKey, arg_Command(cmd));
225 setUrlPort_UploadWidget_(d, &d->originalUrl, arg_Command(cmd));
226 }
227 else {
228 makeValueInput_Widget(w,
229 collectNewFormat_String("%u", titanPortForUrl_(&d->originalUrl)),
230 uiHeading_ColorEscape "${heading.uploadport}",
231 "${dlg.uploadport.msg}",
232 "${dlg.uploadport.set}",
233 format_CStr("upload.setport ptr:%p", d));
234 }
235 return iTrue;
236 }
192 if (isCommand_Widget(w, ev, "upload.accept")) { 237 if (isCommand_Widget(w, ev, "upload.accept")) {
193 iWidget * tabs = findChild_Widget(w, "upload.tabs"); 238 iWidget * tabs = findChild_Widget(w, "upload.tabs");
194 const int tabIndex = tabPageIndex_Widget(tabs, currentTabPage_Widget(tabs)); 239 const int tabIndex = tabPageIndex_Widget(tabs, currentTabPage_Widget(tabs));