38 #include "davcontroller.h" |
38 #include "davcontroller.h" |
39 |
39 |
40 static DavContext* davctx; |
40 static DavContext* davctx; |
41 |
41 |
42 void application_init(void) { |
42 void application_init(void) { |
43 davctx = dav_context_new(); |
43 davctx = dav_context_new(); |
44 dav_context_set_mtsafe(davctx, true); |
44 dav_context_set_mtsafe(davctx, true); |
45 |
45 |
46 application_create_menu(); |
46 application_create_menu(); |
47 } |
47 } |
48 |
48 |
49 |
49 |
50 void application_startup(UiEvent* event, void* data) { |
50 void application_startup(UiEvent* event, void* data) { |
51 if (load_config(davctx)) { |
51 if (load_config(davctx)) { |
52 // TODO: error |
52 // TODO: error |
53 exit(-1); |
53 exit(-1); |
54 } |
54 } |
55 |
55 |
56 window_init(); |
56 window_init(); |
57 |
57 |
58 // create document for global settings (repolist, ...) |
58 // create document for global settings (repolist, ...) |
59 DavApp *app = application_create_app_document(); |
59 DavApp *app = application_create_app_document(); |
60 UiContext *global = ui_global_context(); |
60 UiContext *global = ui_global_context(); |
61 ui_attach_document(global, app); |
61 ui_attach_document(global, app); |
62 |
62 |
63 // create new window |
63 // create new window |
64 UiObject *win = window_create(); |
64 UiObject *win = window_create(); |
65 // create new browser document and attach it to the main window |
65 // create new browser document and attach it to the main window |
66 DavBrowser *doc = davbrowser_create(win); |
66 DavBrowser *doc = davbrowser_create(win); |
67 ui_attach_document(win->ctx, doc); |
67 ui_attach_document(win->ctx, doc); |
68 |
68 |
69 ui_show(win); |
69 ui_show(win); |
70 } |
70 } |
71 |
71 |
72 static void* davrepo_getname(void *elm, int unused) { |
72 static void* davrepo_getname(void *elm, int unused) { |
73 DavCfgRepository *repo = elm; |
73 DavCfgRepository *repo = elm; |
74 return repo->name.value.ptr; |
74 return repo->name.value.ptr; |
75 } |
75 } |
76 |
76 |
77 void application_create_menu(void) { |
77 void application_create_menu(void) { |
78 // menu |
78 // menu |
79 |
79 |
113 } |
113 } |
114 |
114 |
115 |
115 |
116 |
116 |
117 DavApp* application_create_app_document(void) { |
117 DavApp* application_create_app_document(void) { |
118 DavApp *doc = ui_document_new(sizeof(DavApp)); |
118 DavApp *doc = ui_document_new(sizeof(DavApp)); |
119 UiContext *ctx = ui_document_context(doc); |
119 UiContext *ctx = ui_document_context(doc); |
120 doc->repos = ui_list_new(ctx, "repolist"); |
120 doc->repos = ui_list_new(ctx, "repolist"); |
121 |
121 |
122 // create repo list |
122 // create repo list |
123 application_update_repolist(doc); |
123 application_update_repolist(doc); |
124 |
124 |
125 return doc; |
125 return doc; |
126 } |
126 } |
127 |
127 |
128 void application_update_repolist(DavApp *app) { |
128 void application_update_repolist(DavApp *app) { |
129 DavConfig *config = get_config(); |
129 DavConfig *config = get_config(); |
130 DavCfgRepository *repo = config->repositories; |
130 DavCfgRepository *repo = config->repositories; |
131 |
131 |
132 ui_list_clear(app->repos); |
132 ui_list_clear(app->repos); |
133 |
133 |
134 for (DavCfgRepository *repo = config->repositories; repo; repo = repo->next) { |
134 for (DavCfgRepository *repo = config->repositories; repo; repo = repo->next) { |
135 ui_list_append(app->repos, repo); |
135 ui_list_append(app->repos, repo); |
136 } |
136 } |
137 } |
137 } |
138 |
138 |
139 |
139 |
140 DavContext* application_dav_context(void) { |
140 DavContext* application_dav_context(void) { |
141 return davctx; |
141 return davctx; |
142 } |
142 } |
143 |
143 |
144 |
144 |
145 void action_window_new(UiEvent *event, void *data) { |
145 void action_window_new(UiEvent *event, void *data) { |
146 UiObject *win = window_create(); |
146 UiObject *win = window_create(); |
147 // create new browser document and attach it to the main window |
147 // create new browser document and attach it to the main window |
148 DavBrowser *doc = davbrowser_create(win); |
148 DavBrowser *doc = davbrowser_create(win); |
149 ui_attach_document(win->ctx, doc); |
149 ui_attach_document(win->ctx, doc); |
150 |
150 |
151 ui_show(win); |
151 ui_show(win); |
152 } |
152 } |
153 |
153 |
154 void action_refresh(UiEvent *event, void *data) { |
154 void action_refresh(UiEvent *event, void *data) { |
155 DavBrowser *browser = event->document; |
155 DavBrowser *browser = event->document; |
156 if (browser->current) { |
156 if (browser->current) { |
157 davbrowser_query_path(event->obj, browser, browser->current->path); |
157 davbrowser_query_path(event->obj, browser, browser->current->path); |
158 } |
158 } |
159 } |
159 } |
160 |
160 |
161 void action_repo_selected(UiEvent *event, void *data) { |
161 void action_repo_selected(UiEvent *event, void *data) { |
162 DavCfgRepository *repo = event->eventdata; |
162 DavCfgRepository *repo = event->eventdata; |
163 DavBrowser *browser = event->document; |
163 DavBrowser *browser = event->document; |
164 davbrowser_connect2repo(event->obj, browser, repo, ""); |
164 davbrowser_connect2repo(event->obj, browser, repo, ""); |
165 } |
165 } |
166 |
166 |
167 static void file_selected(UiEvent *event, void *data) { |
167 static void file_selected(UiEvent *event, void *data) { |
168 UiFileList *files = event->eventdata; |
168 UiFileList *files = event->eventdata; |
169 if (files && files->nfiles > 0) { |
169 if (files && files->nfiles > 0) { |
170 // files will be freed by the filedialog handler, therefore we need a copy |
170 // files will be freed by the filedialog handler, therefore we need a copy |
171 UiFileList uploadList = ui_filelist_copy(*files); // uploadList will be freed by davbrowser_upload_files |
171 UiFileList uploadList = ui_filelist_copy(*files); // uploadList will be freed by davbrowser_upload_files |
172 davbrowser_upload_files(event->obj, event->document, uploadList); |
172 davbrowser_upload_files(event->obj, event->document, uploadList); |
173 } |
173 } |
174 |
174 |
175 } |
175 } |
176 |
176 |
177 |
177 |
178 |
178 |
179 static void download_location_selected(UiEvent *event, void *data) { |
179 static void download_location_selected(UiEvent *event, void *data) { |
180 DavBrowser *browser = event->document; |
180 DavBrowser *browser = event->document; |
181 DavResource *reslist = data; |
181 DavResource *reslist = data; |
182 UiFileList *flist = event->eventdata; |
182 UiFileList *flist = event->eventdata; |
183 |
183 |
184 if (flist && flist->nfiles > 0) { |
184 if (flist && flist->nfiles > 0) { |
185 davbrowser_download(event->obj, browser, reslist, flist->files[0]); |
185 davbrowser_download(event->obj, browser, reslist, flist->files[0]); |
186 } else { |
186 } else { |
187 dav_session_destroy(reslist->session); |
187 dav_session_destroy(reslist->session); |
188 } |
188 } |
189 } |
189 } |
190 |
190 |
191 void action_download(UiEvent *event, void *data) { |
191 void action_download(UiEvent *event, void *data) { |
192 DavBrowser *browser = event->document; |
192 DavBrowser *browser = event->document; |
193 UiListSelection sel = ui_list_getselection(browser->resources); |
193 UiListSelection sel = ui_list_getselection(browser->resources); |
194 if (sel.count > 0) {; |
194 if (sel.count > 0) {; |
195 const char *initialFileName = NULL; |
195 const char *initialFileName = NULL; |
196 if (sel.count == 1) { |
196 if (sel.count == 1) { |
197 DavResource *res = ui_list_get(browser->resources, sel.rows[0]); |
197 DavResource *res = ui_list_get(browser->resources, sel.rows[0]); |
198 if (res && !res->iscollection) { |
198 if (res && !res->iscollection) { |
199 initialFileName = res->name; |
199 initialFileName = res->name; |
200 } |
200 } |
201 } |
201 } |
202 |
202 |
203 // create a copy of the current session and all selected resources |
203 // create a copy of the current session and all selected resources |
204 DavSession *sn = dav_session_clone(browser->sn); |
204 DavSession *sn = dav_session_clone(browser->sn); |
205 DavResource *first = NULL; |
205 DavResource *first = NULL; |
206 DavResource *last = NULL; |
206 DavResource *last = NULL; |
207 for (int i = 0; i < sel.count; i++) { |
207 for (int i = 0; i < sel.count; i++) { |
208 // get selected resource |
208 // get selected resource |
209 DavResource *res = ui_list_get(browser->resources, sel.rows[i]); |
209 DavResource *res = ui_list_get(browser->resources, sel.rows[i]); |
210 // copy resource |
210 // copy resource |
211 DavResource *res_copy = dav_resource_new(sn, res->path); |
211 DavResource *res_copy = dav_resource_new(sn, res->path); |
212 res_copy->iscollection = res->iscollection; |
212 res_copy->iscollection = res->iscollection; |
213 res_copy->contentlength = res->contentlength; |
213 res_copy->contentlength = res->contentlength; |
214 res_copy->lastmodified = res->lastmodified; |
214 res_copy->lastmodified = res->lastmodified; |
215 res_copy->creationdate = res->creationdate; |
215 res_copy->creationdate = res->creationdate; |
216 |
216 |
217 // link resources |
217 // link resources |
218 if (!first) { |
218 if (!first) { |
219 first = res_copy; |
219 first = res_copy; |
220 } |
220 } |
221 if (last) { |
221 if (last) { |
222 res_copy->prev = last; |
222 res_copy->prev = last; |
223 last->next = res_copy; |
223 last->next = res_copy; |
224 } |
224 } |
225 last = res_copy; |
225 last = res_copy; |
226 } |
226 } |
227 |
227 |
228 if (initialFileName) { |
228 if (initialFileName) { |
229 ui_savefiledialog(event->obj, initialFileName, download_location_selected, first); |
229 ui_savefiledialog(event->obj, initialFileName, download_location_selected, first); |
230 } else { |
230 } else { |
231 ui_openfiledialog(event->obj, UI_FILEDIALOG_SELECT_FOLDER, download_location_selected, first); |
231 ui_openfiledialog(event->obj, UI_FILEDIALOG_SELECT_FOLDER, download_location_selected, first); |
232 } |
232 } |
233 } |
233 } |
234 } |
234 } |
235 |
235 |
236 void action_upload_file(UiEvent *event, void *data) { |
236 void action_upload_file(UiEvent *event, void *data) { |
237 ui_openfiledialog(event->obj, UI_FILEDIALOG_SELECT_MULTI, file_selected, NULL); |
237 ui_openfiledialog(event->obj, UI_FILEDIALOG_SELECT_MULTI, file_selected, NULL); |
238 } |
238 } |
239 |
239 |
240 void action_delete(UiEvent *event, void *data) { |
240 void action_delete(UiEvent *event, void *data) { |
241 DavBrowser *browser = event->document; |
241 DavBrowser *browser = event->document; |
242 UiListSelection sel = ui_list_getselection(browser->resources); |
242 UiListSelection sel = ui_list_getselection(browser->resources); |
243 if (sel.count > 0) { |
243 if (sel.count > 0) { |
244 davbrowser_delete(event->obj, browser, sel); |
244 davbrowser_delete(event->obj, browser, sel); |
245 } |
245 } |
246 ui_listselection_free(sel); |
246 ui_listselection_free(sel); |
247 } |
247 } |
248 |
248 |
249 void action_newfile(UiEvent *event, void *data) { |
249 void action_newfile(UiEvent *event, void *data) { |
250 |
250 |
251 } |
251 } |
252 |
252 |
253 |
253 |
254 static void newfolderdialog_result(UiEvent *event, void *data) { |
254 static void newfolderdialog_result(UiEvent *event, void *data) { |
255 DavBrowser *browser = event->document; |
255 DavBrowser *browser = event->document; |
256 char *path = event->eventdata; |
256 char *path = event->eventdata; |
257 if (event->intval == 1) { |
257 if (event->intval == 1) { |
258 davbrowser_mkcol(event->obj, browser, path); |
258 davbrowser_mkcol(event->obj, browser, path); |
259 } |
259 } |
260 } |
260 } |
261 |
261 |
262 void action_mkcol(UiEvent *event, void *data) { |
262 void action_mkcol(UiEvent *event, void *data) { |
263 ui_dialog(event->obj, .content = "New Folder", .input = TRUE, .button1_label = "Create Folder", .button2_label = "Close", .result = newfolderdialog_result); |
263 ui_dialog(event->obj, .content = "New Folder", .input = TRUE, .button1_label = "Create Folder", .button2_label = "Close", .result = newfolderdialog_result); |
264 } |
264 } |