application/davcontroller.c

changeset 46
31bc86844659
parent 45
ab71409644b0
child 48
ae61523bce20
equal deleted inserted replaced
45:ab71409644b0 46:31bc86844659
950 950
951 // ------------------------------------- Path Operation (DELETE, MKCOL) ------------------------------------- 951 // ------------------------------------- Path Operation (DELETE, MKCOL) -------------------------------------
952 952
953 enum DavPathOpType { 953 enum DavPathOpType {
954 DAV_PATH_OP_DELETE = 0, 954 DAV_PATH_OP_DELETE = 0,
955 DAV_PATH_OP_MKCOL 955 DAV_PATH_OP_CREATE
956 }; 956 };
957 957
958 typedef struct DavPathOp { 958 typedef struct DavPathOp {
959 UiObject *ui; 959 UiObject *ui;
960 DavBrowser *browser; 960 DavBrowser *browser;
967 char **path; 967 char **path;
968 // browser->resources indices 968 // browser->resources indices
969 size_t *list_indices; 969 size_t *list_indices;
970 // number of path/list_indices elements 970 // number of path/list_indices elements
971 size_t nelm; 971 size_t nelm;
972 // path is collection
973 DavBool iscollection;
972 974
973 // browser->current ptr when the PathOp started 975 // browser->current ptr when the PathOp started
974 // used in combination with collection_ctn to check if the browser list changed 976 // used in combination with collection_ctn to check if the browser list changed
975 DavResource *collection; 977 DavResource *collection;
976 int64_t collection_ctn; 978 int64_t collection_ctn;
979 typedef struct DavPathOpResult { 981 typedef struct DavPathOpResult {
980 UiObject *ui; 982 UiObject *ui;
981 DavBrowser *browser; 983 DavBrowser *browser;
982 DavResource *collection; 984 DavResource *collection;
983 int64_t collection_ctn; 985 int64_t collection_ctn;
986 enum DavPathOpType op;
987 DavBool iscollection;
984 988
985 char *path; 989 char *path;
986 int res_index; 990 int res_index;
987 int result; 991 int result;
988 char *errormsg; 992 char *errormsg;
1014 free(result->path); 1018 free(result->path);
1015 free(result); 1019 free(result);
1016 return 0; 1020 return 0;
1017 } 1021 }
1018 1022
1019 static int uithr_pathop_mkcol_error(void *data) { 1023 static int uithr_pathop_create_resource_error(void *data) {
1020 DavPathOpResult *result = data; 1024 DavPathOpResult *result = data;
1021 1025
1022 cxmutstr msg = cx_asprintf("Cannot create collection %s", result->path); 1026 cxmutstr msg = cx_asprintf("Cannot create %s %s", result->iscollection ? "collection" : "resource", result->path);
1023 ui_dialog(result->ui, .title = "Error", .content = msg.ptr, .button1_label = "OK"); 1027 ui_dialog(result->ui, .title = "Error", .content = msg.ptr, .button1_label = "OK");
1024 free(msg.ptr); 1028 free(msg.ptr);
1025 1029
1026 if (result->errormsg) { 1030 if (result->errormsg) {
1027 free(result->errormsg); 1031 free(result->errormsg);
1029 free(result->path); 1033 free(result->path);
1030 free(result); 1034 free(result);
1031 return 0; 1035 return 0;
1032 } 1036 }
1033 1037
1034 static int uithr_pathop_mkcol_sucess(void *data) { 1038 static int uithr_pathop_create_resource_sucess(void *data) {
1035 DavPathOpResult *result = data; 1039 DavPathOpResult *result = data;
1036 1040
1037 if (result->browser->current == result->collection && result->browser->res_counter == result->collection_ctn) { 1041 if (result->browser->current == result->collection && result->browser->res_counter == result->collection_ctn) {
1038 DavResource *res = dav_resource_new(result->browser->sn, result->path); 1042 DavResource *res = dav_resource_new(result->browser->sn, result->path);
1039 res->iscollection = TRUE; 1043 res->iscollection = result->iscollection;
1040 // TODO: add the resource at the correct position or sort the list after append 1044 // TODO: add the resource at the correct position or sort the list after append
1041 ui_list_append(result->browser->resources, res); 1045 ui_list_append(result->browser->resources, res);
1042 result->browser->resources->update(result->browser->resources, 0); 1046 result->browser->resources->update(result->browser->resources, 0);
1043 } 1047 }
1044 1048
1057 DavPathOpResult *result = malloc(sizeof(DavPathOpResult)); 1061 DavPathOpResult *result = malloc(sizeof(DavPathOpResult));
1058 result->ui = op->ui; 1062 result->ui = op->ui;
1059 result->browser = op->browser; 1063 result->browser = op->browser;
1060 result->collection = op->collection; 1064 result->collection = op->collection;
1061 result->collection_ctn = op->collection_ctn; 1065 result->collection_ctn = op->collection_ctn;
1066 result->op = op->op;
1062 result->path = strdup(res->path); 1067 result->path = strdup(res->path);
1063 result->result = 0; 1068 result->result = 0;
1064 result->res_index = op->list_indices[i]; 1069 result->res_index = op->list_indices[i];
1065 result->errormsg = NULL; 1070 result->errormsg = NULL;
1071 result->iscollection = op->iscollection;
1066 1072
1067 if (op->op == DAV_PATH_OP_DELETE) { 1073 if (op->op == DAV_PATH_OP_DELETE) {
1068 ui_threadfunc result_callback = uithr_pathop_delete_sucess; 1074 ui_threadfunc result_callback = uithr_pathop_delete_sucess;
1069 if (dav_delete(res)) { 1075 if (dav_delete(res)) {
1070 result->errormsg = op->sn->errorstr ? strdup(op->sn->errorstr) : NULL; 1076 result->errormsg = op->sn->errorstr ? strdup(op->sn->errorstr) : NULL;
1071 result_callback = uithr_pathop_delete_error; 1077 result_callback = uithr_pathop_delete_error;
1072 } 1078 }
1073 ui_call_mainthread(result_callback, result); 1079 ui_call_mainthread(result_callback, result);
1074 } else if (op->op == DAV_PATH_OP_MKCOL) { 1080 } else if (op->op == DAV_PATH_OP_CREATE) {
1075 res->iscollection = TRUE; 1081 res->iscollection = op->iscollection;
1076 ui_threadfunc result_callback = uithr_pathop_mkcol_sucess; 1082 ui_threadfunc result_callback = uithr_pathop_create_resource_sucess;
1077 if (dav_create(res)) { 1083 if (dav_create(res)) {
1078 result->errormsg = op->sn->errorstr ? strdup(op->sn->errorstr) : NULL; 1084 result->errormsg = op->sn->errorstr ? strdup(op->sn->errorstr) : NULL;
1079 result_callback = uithr_pathop_mkcol_error; 1085 result_callback = uithr_pathop_create_resource_error;
1080 } 1086 }
1081 ui_call_mainthread(result_callback, result); 1087 ui_call_mainthread(result_callback, result);
1082 } 1088 }
1083 1089
1084 dav_resource_free(res); 1090 dav_resource_free(res);
1085 free(op->path[i]); 1091 free(op->path[i]);
1086 } 1092 }
1087 } 1093 }
1116 } 1122 }
1117 1123
1118 ui_job(ui, jobthr_path_op, op, NULL, NULL); 1124 ui_job(ui, jobthr_path_op, op, NULL, NULL);
1119 } 1125 }
1120 1126
1121 void davbrowser_mkcol(UiObject *ui, DavBrowser *browser, const char *name) { 1127 void davbrowser_create_resource(UiObject *ui, DavBrowser *browser, const char *name, DavBool iscollection) {
1122 DavPathOp *op = malloc(sizeof(DavPathOp)); 1128 DavPathOp *op = malloc(sizeof(DavPathOp));
1123 op->ui = ui; 1129 op->ui = ui;
1124 op->browser = browser; 1130 op->browser = browser;
1125 op->op = DAV_PATH_OP_MKCOL; 1131 op->op = DAV_PATH_OP_CREATE;
1126 op->sn = dav_session_clone(browser->sn); 1132 op->sn = dav_session_clone(browser->sn);
1127 op->path = calloc(1, sizeof(char*)); 1133 op->path = calloc(1, sizeof(char*));
1128 op->list_indices = calloc(1, sizeof(size_t)); 1134 op->list_indices = calloc(1, sizeof(size_t));
1129 op->nelm = 1; 1135 op->nelm = 1;
1136 op->iscollection = iscollection;
1130 1137
1131 op->path[0] = util_concat_path(browser->current->path, name); 1138 op->path[0] = util_concat_path(browser->current->path, name);
1132 1139
1133 op->collection = browser->current; 1140 op->collection = browser->current;
1134 op->collection_ctn = browser->res_counter; 1141 op->collection_ctn = browser->res_counter;
1135 1142
1136 ui_job(ui, jobthr_path_op, op, NULL, NULL); 1143 ui_job(ui, jobthr_path_op, op, NULL, NULL);
1137 } 1144 }
1145
1146 void davbrowser_mkcol(UiObject *ui, DavBrowser *browser, const char *name) {
1147 davbrowser_create_resource(ui, browser, name, TRUE);
1148 }
1149
1150 void davbrowser_newfile(UiObject *ui, DavBrowser *browser, const char *name) {
1151 davbrowser_create_resource(ui, browser, name, FALSE);
1152 }

mercurial