diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-09-15 14:05:42 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-09-15 14:05:42 +0300 |
commit | 6580b4e2c396f2fdfb0fb017ec4249baa2fba5ff (patch) | |
tree | 1a0db802aa76dbf9f8b5c595e8b1a687ecdf4667 /src/ui/uploadwidget.c | |
parent | 5119a72481a2c752972470ea7b488a5c9e58b720 (diff) |
iOS: Picking files for upload
Diffstat (limited to 'src/ui/uploadwidget.c')
-rw-r--r-- | src/ui/uploadwidget.c | 69 |
1 files changed, 54 insertions, 15 deletions
diff --git a/src/ui/uploadwidget.c b/src/ui/uploadwidget.c index 34cace08..3f8b78dc 100644 --- a/src/ui/uploadwidget.c +++ b/src/ui/uploadwidget.c | |||
@@ -33,8 +33,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | |||
33 | #include "gmcerts.h" | 33 | #include "gmcerts.h" |
34 | #include "app.h" | 34 | #include "app.h" |
35 | 35 | ||
36 | #if defined (iPlatformAppleMobile) | ||
37 | # include "ios.h" | ||
38 | #endif | ||
39 | |||
36 | #include <the_Foundation/file.h> | 40 | #include <the_Foundation/file.h> |
37 | #include <the_Foundation/fileinfo.h> | 41 | #include <the_Foundation/fileinfo.h> |
42 | #include <the_Foundation/path.h> | ||
38 | 43 | ||
39 | iDefineObjectConstruction(UploadWidget) | 44 | iDefineObjectConstruction(UploadWidget) |
40 | 45 | ||
@@ -64,6 +69,16 @@ struct Impl_UploadWidget { | |||
64 | iAtomicInt isRequestUpdated; | 69 | iAtomicInt isRequestUpdated; |
65 | }; | 70 | }; |
66 | 71 | ||
72 | static void releaseFile_UploadWidget_(iUploadWidget *d) { | ||
73 | #if defined (iPlatformAppleMobile) | ||
74 | if (!isEmpty_String(&d->filePath)) { | ||
75 | /* Delete the temporary file that was copied for uploading. */ | ||
76 | remove(cstr_String(&d->filePath)); | ||
77 | } | ||
78 | #endif | ||
79 | clear_String(&d->filePath); | ||
80 | } | ||
81 | |||
67 | static void updateProgress_UploadWidget_(iGmRequest *request, size_t current, size_t total) { | 82 | static void updateProgress_UploadWidget_(iGmRequest *request, size_t current, size_t total) { |
68 | iUploadWidget *d = userData_Object(request); | 83 | iUploadWidget *d = userData_Object(request); |
69 | postCommand_Widget(d, | 84 | postCommand_Widget(d, |
@@ -157,8 +172,8 @@ void init_UploadWidget(iUploadWidget *d) { | |||
157 | }, actions, iElemCount(actions)); | 172 | }, actions, iElemCount(actions)); |
158 | d->info = findChild_Widget(w, "upload.info"); | 173 | d->info = findChild_Widget(w, "upload.info"); |
159 | d->input = findChild_Widget(w, "upload.text"); | 174 | d->input = findChild_Widget(w, "upload.text"); |
160 | d->filePathLabel = findChild_Widget(w, "upload.file.name"); | 175 | d->filePathLabel = findChild_Widget(w, "upload.filepathlabel"); |
161 | d->fileSizeLabel = findChild_Widget(w, "upload.file.size"); | 176 | d->fileSizeLabel = findChild_Widget(w, "upload.filesizelabel"); |
162 | d->mime = findChild_Widget(w, "upload.mime"); | 177 | d->mime = findChild_Widget(w, "upload.mime"); |
163 | d->token = findChild_Widget(w, "upload.token"); | 178 | d->token = findChild_Widget(w, "upload.token"); |
164 | d->counter = findChild_Widget(w, "upload.counter"); | 179 | d->counter = findChild_Widget(w, "upload.counter"); |
@@ -237,6 +252,7 @@ void init_UploadWidget(iUploadWidget *d) { | |||
237 | } | 252 | } |
238 | 253 | ||
239 | void deinit_UploadWidget(iUploadWidget *d) { | 254 | void deinit_UploadWidget(iUploadWidget *d) { |
255 | releaseFile_UploadWidget_(d); | ||
240 | deinit_Block(&d->idFingerprint); | 256 | deinit_Block(&d->idFingerprint); |
241 | deinit_String(&d->filePath); | 257 | deinit_String(&d->filePath); |
242 | deinit_String(&d->url); | 258 | deinit_String(&d->url); |
@@ -304,6 +320,26 @@ static void updateIdentityDropdown_UploadWidget_(iUploadWidget *d) { | |||
304 | : format_CStr(" fp:%s", cstrCollect_String(hexEncode_Block(&d->idFingerprint)))); | 320 | : format_CStr(" fp:%s", cstrCollect_String(hexEncode_Block(&d->idFingerprint)))); |
305 | } | 321 | } |
306 | 322 | ||
323 | static void updateFileInfo_UploadWidget_(iUploadWidget *d) { | ||
324 | iFileInfo *info = iClob(new_FileInfo(&d->filePath)); | ||
325 | if (isDirectory_FileInfo(info)) { | ||
326 | makeMessage_Widget("${heading.upload.error.file}", | ||
327 | "${upload.error.directory}", | ||
328 | (iMenuItem[]){ "${dlg.message.ok}", 0, 0, "message.ok" }, 1); | ||
329 | clear_String(&d->filePath); | ||
330 | d->fileSize = 0; | ||
331 | return iTrue; | ||
332 | } | ||
333 | d->fileSize = size_FileInfo(info); | ||
334 | #if defined (iPlatformMobile) | ||
335 | setTextCStr_LabelWidget(d->filePathLabel, cstr_Rangecc(baseName_Path(&d->filePath))); | ||
336 | #else | ||
337 | setText_LabelWidget(d->filePathLabel, &d->filePath); | ||
338 | #endif | ||
339 | setTextCStr_LabelWidget(d->fileSizeLabel, formatCStrs_Lang("num.bytes.n", d->fileSize)); | ||
340 | setTextCStr_InputWidget(d->mime, mediaType_Path(&d->filePath)); | ||
341 | } | ||
342 | |||
307 | static iBool processEvent_UploadWidget_(iUploadWidget *d, const SDL_Event *ev) { | 343 | static iBool processEvent_UploadWidget_(iUploadWidget *d, const SDL_Event *ev) { |
308 | iWidget *w = as_Widget(d); | 344 | iWidget *w = as_Widget(d); |
309 | const char *cmd = command_UserEvent(ev); | 345 | const char *cmd = command_UserEvent(ev); |
@@ -430,6 +466,7 @@ static iBool processEvent_UploadWidget_(iUploadWidget *d, const SDL_Event *ev) { | |||
430 | d->request = NULL; /* DocumentWidget has it now. */ | 466 | d->request = NULL; /* DocumentWidget has it now. */ |
431 | } | 467 | } |
432 | setupSheetTransition_Mobile(w, iFalse); | 468 | setupSheetTransition_Mobile(w, iFalse); |
469 | releaseFile_UploadWidget_(d); | ||
433 | destroy_Widget(w); | 470 | destroy_Widget(w); |
434 | return iTrue; | 471 | return iTrue; |
435 | } | 472 | } |
@@ -439,24 +476,26 @@ static iBool processEvent_UploadWidget_(iUploadWidget *d, const SDL_Event *ev) { | |||
439 | refresh_Widget(w); | 476 | refresh_Widget(w); |
440 | return iTrue; | 477 | return iTrue; |
441 | } | 478 | } |
479 | else if (isCommand_Widget(w, ev, "upload.pickfile")) { | ||
480 | #if defined (iPlatformAppleMobile) | ||
481 | if (hasLabel_Command(cmd, "path")) { | ||
482 | releaseFile_UploadWidget_(d); | ||
483 | set_String(&d->filePath, collect_String(suffix_Command(cmd, "path"))); | ||
484 | updateFileInfo_UploadWidget_(d); | ||
485 | } | ||
486 | else { | ||
487 | pickFile_iOS(format_CStr("upload.pickfile ptr:%p", d)); | ||
488 | } | ||
489 | #endif | ||
490 | return iTrue; | ||
491 | } | ||
442 | if (ev->type == SDL_DROPFILE) { | 492 | if (ev->type == SDL_DROPFILE) { |
443 | /* Switch to File tab. */ | 493 | /* Switch to File tab. */ |
444 | iWidget *tabs = findChild_Widget(w, "upload.tabs"); | 494 | iWidget *tabs = findChild_Widget(w, "upload.tabs"); |
445 | showTabPage_Widget(tabs, tabPage_Widget(tabs, 1)); | 495 | showTabPage_Widget(tabs, tabPage_Widget(tabs, 1)); |
496 | releaseFile_UploadWidget_(d); | ||
446 | setCStr_String(&d->filePath, ev->drop.file); | 497 | setCStr_String(&d->filePath, ev->drop.file); |
447 | iFileInfo *info = iClob(new_FileInfo(&d->filePath)); | 498 | updateFileInfo_UploadWidget_(d); |
448 | if (isDirectory_FileInfo(info)) { | ||
449 | makeMessage_Widget("${heading.upload.error.file}", | ||
450 | "${upload.error.directory}", | ||
451 | (iMenuItem[]){ "${dlg.message.ok}", 0, 0, "message.ok" }, 1); | ||
452 | clear_String(&d->filePath); | ||
453 | d->fileSize = 0; | ||
454 | return iTrue; | ||
455 | } | ||
456 | d->fileSize = size_FileInfo(info); | ||
457 | setText_LabelWidget(d->filePathLabel, &d->filePath); | ||
458 | setTextCStr_LabelWidget(d->fileSizeLabel, formatCStrs_Lang("num.bytes.n", d->fileSize)); | ||
459 | setTextCStr_InputWidget(d->mime, mediaType_Path(&d->filePath)); | ||
460 | return iTrue; | 499 | return iTrue; |
461 | } | 500 | } |
462 | return processEvent_Widget(w, ev); | 501 | return processEvent_Widget(w, ev); |