application/davcontroller.c

changeset 27
c254ed644edf
parent 26
40d6af793c1a
child 28
1ecc1183f046
equal deleted inserted replaced
26:40d6af793c1a 27:c254ed644edf
667 667
668 668
669 // ------------------------------------- Path Operation (DELETE, MKCOL) ------------------------------------- 669 // ------------------------------------- Path Operation (DELETE, MKCOL) -------------------------------------
670 670
671 enum DavPathOpType { 671 enum DavPathOpType {
672 DAV_PATH_OP_DELETE = 0 672 DAV_PATH_OP_DELETE = 0,
673 DAV_PATH_OP_MKCOL
673 }; 674 };
674 675
675 typedef struct DavPathOp { 676 typedef struct DavPathOp {
677 UiObject *ui;
678 DavBrowser *browser;
679
680 // operation type (delete, mkcol, ...)
676 enum DavPathOpType op; 681 enum DavPathOpType op;
682 // clone of the browser's DavSession
677 DavSession *sn; 683 DavSession *sn;
684 // path array
678 char **path; 685 char **path;
686 // browser->resources indices
679 size_t *list_indices; 687 size_t *list_indices;
688 // number of path/list_indices elements
680 size_t nelm; 689 size_t nelm;
690
691 // browser->current ptr when the PathOp started
692 // used in combination with collection_ctn to check if the browser list changed
681 DavResource *collection; 693 DavResource *collection;
682 int64_t collection_ctn; 694 int64_t collection_ctn;
683 } DavPathOp; 695 } DavPathOp;
684 696
697 typedef struct DavPathOpResult {
698 UiObject *ui;
699 DavBrowser *browser;
700 DavResource *collection;
701 int64_t collection_ctn;
702
703 char *path;
704 int res_index;
705 int result;
706 char *errormsg;
707 } DavPathOpResult;
708
709 static int uithr_pathop_delete_error(void *data) {
710 DavPathOpResult *result = data;
711
712 cxmutstr msg = cx_asprintf("Cannot delete resource %s", result->path);
713 ui_dialog(result->ui, .title = "Error", .content = msg.ptr, .button1_label = "OK");
714 free(msg.ptr);
715
716 if (result->errormsg) {
717 free(result->errormsg);
718 }
719 free(result->path);
720 free(result);
721 return 0;
722 }
723
724 static int uithr_pathop_delete_sucess(void *data) {
725 DavPathOpResult *result = data;
726
727 if (result->browser->current == result->collection && result->browser->res_counter == result->collection_ctn) {
728 ui_list_remove(result->browser->resources, result->res_index);
729 result->browser->resources->update(result->browser->resources, 0);
730 }
731
732 free(result->path);
733 free(result);
734 return 0;
735 }
736
685 static int jobthr_path_op(void *data) { 737 static int jobthr_path_op(void *data) {
686 DavPathOp *op = data; 738 DavPathOp *op = data;
687 739
688 for (int i = 0; i < op->nelm; i++) { 740
741 for (int i = op->nelm-1; i >= 0; i--) {
689 if (op->path[i]) { 742 if (op->path[i]) {
690 DavResource *res = dav_resource_new(op->sn, op->path[i]); 743 DavResource *res = dav_resource_new(op->sn, op->path[i]);
691 744
692 int ret = 0; 745 DavPathOpResult *result = malloc(sizeof(DavPathOpResult));
746 result->ui = op->ui;
747 result->browser = op->browser;
748 result->collection = op->collection;
749 result->collection_ctn = op->collection_ctn;
750 result->path = strdup(res->path);
751 result->result = 0;
752 result->res_index = op->list_indices[i];
753 result->errormsg = NULL;
754
693 if (op->op == DAV_PATH_OP_DELETE) { 755 if (op->op == DAV_PATH_OP_DELETE) {
694 ret = dav_delete(res); 756 ui_threadfunc result_callback = uithr_pathop_delete_sucess;
757 if (dav_delete(res)) {
758 result->errormsg = op->sn->errorstr ? strdup(op->sn->errorstr) : NULL;
759 result_callback = uithr_pathop_delete_error;
760 }
761 ui_call_mainthread(result_callback, result);
695 } 762 }
696 763
697 dav_resource_free(res); 764 dav_resource_free(res);
698 } 765 free(op->path[i]);
699 } 766 }
700 767 }
701 768
702 return 0; 769 dav_session_destroy(op->sn);
703 } 770 free(op->path);
771 free(op->list_indices);
772 free(op);
773
774 return 0;
775 }
776
777
704 778
705 void davbrowser_delete(UiObject *ui, DavBrowser *browser, UiListSelection selection) { 779 void davbrowser_delete(UiObject *ui, DavBrowser *browser, UiListSelection selection) {
706 DavPathOp *op = malloc(sizeof(DavPathOp)); 780 DavPathOp *op = malloc(sizeof(DavPathOp));
781 op->ui = ui;
782 op->browser = browser;
707 op->op = DAV_PATH_OP_DELETE; 783 op->op = DAV_PATH_OP_DELETE;
708 op->sn = dav_session_clone(browser->sn); 784 op->sn = dav_session_clone(browser->sn);
709 op->path = calloc(selection.count, sizeof(char*)); 785 op->path = calloc(selection.count, sizeof(char*));
710 op->list_indices = calloc(selection.count, sizeof(size_t)); 786 op->list_indices = calloc(selection.count, sizeof(size_t));
711 op->nelm = selection.count; 787 op->nelm = selection.count;

mercurial