[SR][Nabi-se][Nabi_SEN_SE-32480] Unable to select more then one file for attachment...
[apps/core/preloaded/ug-myfile-efl.git] / src / common / file-system / mf-ug-fs-oper.c
1 /*
2  * Copyright 2012          Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *  http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19
20 #include <libgen.h>
21 #include <glib.h>
22 #include "mf-ug-fs-util.h"
23 #include "mf-ug-util.h"
24
25 static int __mf_ug_fs_oper_sort_by_date_cb_O2R(const void *d1, const void *d2);
26 static int __mf_ug_fs_oper_sort_by_name_cb_A2Z(const void *d1, const void *d2);
27 static int __mf_ug_fs_oper_sort_by_type_cb_A2Z(const void *d1, const void *d2);
28 static int __mf_ug_fs_oper_sort_by_size_cb_S2L(const void *d1, const void *d2);
29 static int __mf_ug_fs_oper_sort_by_name_cb_Z2A(const void *d1, const void *d2);
30 static int __mf_ug_fs_oper_sort_by_date_cb_R2O(const void *d1, const void *d2);
31 static int __mf_ug_fs_oper_sort_by_type_cb_Z2A(const void *d1, const void *d2);
32 static int __mf_ug_fs_oper_sort_by_size_cb_L2S(const void *d1, const void *d2);
33
34 /*********************
35 **Function name:        __mf_ug_fs_oper_file_system_error
36 **Parameter:
37 **      const char* src:        source path
38 **      const char* dst:        destination path
39 **      int check_option:       check option
40 **
41 **Return value:
42 **      error code
43 **
44 **Action:
45 **      input parameter checking
46 **
47 *********************/
48 static const char *__mf_ug_fs_oper_get_file(const char *path)
49 {
50         char *result = NULL;
51
52         if (!path) {
53                 return NULL;
54         }
55         if ((result = strrchr(path, '/'))) {
56                 result++;
57         } else {
58                 result = (char *)path;
59         }
60         return result;
61 }
62
63
64 static int __mf_ug_fs_oper_file_system_error(const char *src, const char *dst, int check_option)
65 {
66         if ((check_option & MF_ERROR_CHECK_SRC_ARG_VALID) && (src == NULL)) {
67                 return MYFILE_ERR_SRC_ARG_INVALID;
68         }
69         if ((check_option & MF_ERROR_CHECK_SRC_EXIST) && (!ecore_file_exists(src))) {
70                 return MYFILE_ERR_SRC_NOT_EXIST;
71         }
72
73         if (check_option & MF_ERROR_CHECK_SRC_PATH_VALID) {
74                 if (!ecore_file_is_dir(src)) {
75                         if (mf_ug_file_attr_is_right_file_path(src)) {
76                                 return MYFILE_ERR_INVALID_FILE_PATH;
77                         }
78                 } else {
79                         if (mf_ug_file_attr_is_right_dir_path(src)) {
80                                 return MYFILE_ERR_INVALID_DIR_PATH;
81                         }
82                 }
83         }
84
85         if (check_option & MF_ERROR_CHECK_DUPLICATED) {
86                 char *parent_path = NULL;
87
88                 if (!mf_ug_file_attr_get_parent_path(dst, &parent_path)) {
89                         if (mf_ug_file_attr_is_duplicated_name(parent_path, __mf_ug_fs_oper_get_file(dst))) {
90                                 UG_SAFE_FREE_CHAR(parent_path);
91                                 return MYFILE_ERR_DUPLICATED_NAME;
92                         }
93                         UG_SAFE_FREE_CHAR(parent_path);
94                 } else {
95                         UG_SAFE_FREE_CHAR(parent_path);
96                         return MYFILE_ERR_GET_PARENT_PATH_FAIL;
97                 }
98         }
99         return MYFILE_ERR_NONE;
100 }
101
102 /*********************
103 **Function name:        mf_ug_fs_oper_read_dir
104 **Parameter:
105 **      char *path:                             path which we need to read
106 **      Eina_List** dir_list:   output parameter of dir list under specified path
107 **      Eina_List** file_list:  output parameter of file list under specified path
108 **
109 **Return value:
110 **      error code
111 **
112 **Action:
113 **      read element under the specified path
114 **
115 *********************/
116 int mf_ug_fs_oper_read_dir(const char *path, Eina_List **dir_list, Eina_List **file_list)
117 {
118         UG_TRACE_BEGIN;
119         DIR *pDir = NULL;
120         struct dirent *ent;
121
122         ug_mf_retvm_if(path == NULL, MYFILE_ERR_INVALID_ARG, "path is null");
123         ug_mf_retvm_if(dir_list == NULL, MYFILE_ERR_INVALID_ARG, "dir_list is null");
124         ug_mf_retvm_if(file_list == NULL, MYFILE_ERR_INVALID_ARG, "file_list is null");
125
126         int option = MF_ERROR_CHECK_SRC_ARG_VALID | MF_ERROR_CHECK_SRC_EXIST | MF_ERROR_CHECK_SRC_PATH_VALID;
127         int ret = __mf_ug_fs_oper_file_system_error(path, NULL, option);
128
129         if (ret != MYFILE_ERR_NONE) {
130                 return ret;
131         }
132
133         pDir = opendir(path);
134
135         if (pDir == NULL) {
136                 return MYFILE_ERR_DIR_OPEN_FAIL;
137         }
138
139         while ((ent = readdir(pDir)) != NULL) {
140                 GString *childpath = NULL;
141                 ugFsNodeInfo *pNode = NULL;
142
143                 if (strncmp(ent->d_name, ".", 1) == 0 || strcmp(ent->d_name, "..") == 0) {
144                         continue;
145                 }
146
147                 if ((ent->d_type & DT_DIR) == 0 && (ent->d_type & DT_REG) == 0) {
148                         continue;
149                 }
150 #ifdef  UG_DEBUG_FOLDER_OPTION
151                 if ((ent->d_type & DT_DIR) != 0) {
152                         if ((strlen(path) == strlen(PHONE_FOLDER)) && (strcmp(path, PHONE_FOLDER) == 0)
153                             && (strlen(ent->d_name) == strlen(DEBUG_FOLDER)) && (strcmp(ent->d_name, DEBUG_FOLDER) == 0)) {
154                                 continue;
155                         }
156                 }
157 #endif
158                 pNode = (ugFsNodeInfo *) malloc(sizeof(ugFsNodeInfo));
159
160                 if (pNode == NULL) {
161                         continue;
162                 }
163                 memset(pNode, 0, sizeof(ugFsNodeInfo));
164                 snprintf(pNode->path, sizeof(pNode->path), "%s", path);
165                 snprintf(pNode->name, sizeof(pNode->name), "%s", ent->d_name);
166                 if (ent->d_type & DT_DIR) {
167                         pNode->type = UG_FILE_TYPE_DIR;
168                 } else if (ent->d_type & DT_REG) {
169                         mf_ug_file_attr_get_file_category(ent->d_name, &(pNode->type));
170                 }
171                 childpath = g_string_new(path);
172                 if (childpath == NULL) {
173                         free(pNode);
174                         pNode = NULL;
175                         continue;
176                 }
177                 g_string_append_printf(childpath, "/%s", ent->d_name);
178                 mf_ug_file_attr_get_file_stat(childpath->str, &pNode);
179                 if (pNode->type == UG_FILE_TYPE_DIR) {
180                         ug_mf_debug("dir append\n");
181                         *dir_list = eina_list_append(*dir_list, pNode);
182                 } else {
183                         ug_mf_debug("file append\n");
184                         ret = mf_ug_file_attr_get_file_ext(childpath->str, &pNode->ext);
185                         if (ret != MYFILE_ERR_NONE) {
186                                 pNode->ext = NULL;
187                         }
188                         *file_list = eina_list_append(*file_list, pNode);
189                 }
190
191                 g_string_free(childpath, TRUE);
192         }
193         closedir(pDir);
194         UG_TRACE_END;
195
196         return MYFILE_ERR_NONE;
197 }
198
199 /*********************
200 **Function name:        __mf_ug_fs_oper_exec_filter
201 **Parameter:
202 **      ugFsNodeInfo* pnode_info:       the node we need to check for filter
203 **      int option:                             filter
204 **
205 **Return value:
206 **      error code
207 **
208 **Action:
209 **      check if the node satisfied the filter option
210 *********************/
211 static mf_ug_drm_file_mime_type __mf_ug_fs_oper_get_drm_file_type_by_mime(char *mime_type)
212 {
213         gchar **result = NULL;
214         if (mime_type != NULL) {
215                 result = g_strsplit(mime_type, "/", 0);
216                 if (result && (*result)) {
217                         ug_mf_debug("*result is [%s]", *result);
218                         if (g_strcmp0(*result, "audio") == 0) {
219                                 g_strfreev(result);
220                                 return MF_UG_DRM_RINGTONE_FILE;
221                         } else if (g_strcmp0(*result, "image") == 0) {
222                                 g_strfreev(result);
223                                 return MF_UG_DRM_IMAGE_FILE;
224                         } else {
225                                 g_strfreev(result);
226                                 return MF_UG_DRM_UNKNOW_FILE;
227                         }
228                 } else {
229                         g_strfreev(result);
230                         return MF_UG_DRM_UNKNOW_FILE;
231                 }
232         } else {
233                 return MF_UG_DRM_UNKNOW_FILE;
234         }
235 }
236
237 mf_ug_drm_file_mime_type mf_ug_fs_oper_get_drm_type(char *path)
238 {
239         drm_result_e res = 0;
240         drm_content_info_s dcf_content_info;
241
242         res = drm_get_content_info(path, &dcf_content_info);
243
244
245         if (res == DRM_RETURN_SUCCESS) {
246                 mf_ug_drm_file_mime_type drm_mime_type = __mf_ug_fs_oper_get_drm_file_type_by_mime(dcf_content_info.mime_type);
247                 return drm_mime_type;
248         } else {
249                 return MF_UG_DRM_UNKNOW_FILE;
250         }
251 }
252
253 int mf_ug_fs_oper_drm_is_action_allowed(const char *path, drm_action_type_e action, drm_setas_category_e category)
254 {
255         drm_bool_type_e is_allowed = 0;
256         drm_action_allowed_data_s action_data;
257
258         int ret = -1;
259         memset(&action_data,0x0,sizeof(drm_action_allowed_data_s));
260         UG_SAFE_STRCPY(action_data.file_path, path);
261
262         action_data.data = category;
263
264         ret = drm_is_action_allowed(action,&action_data,&is_allowed);
265         if(DRM_RETURN_SUCCESS == ret && DRM_TRUE == is_allowed){
266                 return true;
267         }else{
268                 return false;
269         }
270 }
271
272 bool mf_ug_fs_oper_drm_is_valid(const char *path, drm_permission_type_e perm_type)
273 {
274         int res = DRM_RETURN_SUCCESS;
275         drm_license_status_e licence_status = DRM_LICENSE_STATUS_UNDEFINED;
276         if (path) {
277                 res = drm_get_license_status(path, perm_type, &licence_status);
278         }
279
280         ug_error("res is [%d] licence is [%d]", res, licence_status);
281         if (res == DRM_RETURN_SUCCESS && licence_status == DRM_LICENSE_STATUS_VALID) {
282                 return true;
283         } else {
284                 return false;
285         }
286
287 }
288 static bool __mf_ug_fs_oper_exec_drm_filter(ugFsNodeInfo *pnode_info, int option)
289 {
290         if (pnode_info == NULL) {
291                 return FALSE;
292         }
293
294         int result = -1;
295         char *fullpath = NULL;
296         drm_result_e res = DRM_RETURN_INTERNAL_ERROR;
297         if (option & MF_UG_FILTER_DRM_ALL) {
298                 return TRUE;
299         }
300
301         if (option & MF_UG_FILTER_DRM_WITHOUT_FL) {
302                 fullpath = g_strconcat(pnode_info->path, "/", pnode_info->name, NULL);
303                 res = mf_ug_fs_oper_drm_is_action_allowed(fullpath, DRM_IS_FORWARDING_ALLOWED, DRM_SETAS_NONE);
304                 if (res == DRM_RETURN_SUCCESS) {
305                         return FALSE;
306                 } else {
307                         return TRUE;
308                 }
309         }
310
311         if (option & MF_UG_FILTER_DRM_IMAGE) {
312                 fullpath = g_strconcat(pnode_info->path, "/", pnode_info->name, NULL);
313
314                 result = mf_ug_fs_oper_drm_is_action_allowed(fullpath, DRM_HAS_VALID_SETAS_STATUS, DRM_SETAS_WALLPAPER);
315                 if (result) {
316                         mf_ug_drm_file_mime_type drm_mime_type = mf_ug_fs_oper_get_drm_type(fullpath);
317                         if (drm_mime_type == MF_UG_DRM_IMAGE_FILE)
318                                 return TRUE;
319                         else
320                                 return FALSE;
321                 } else {
322                         return FALSE;
323                 }
324         }
325
326         if (option & MF_UG_FILTER_DRM_RINGTONE) {
327                 fullpath = g_strconcat(pnode_info->path, "/", pnode_info->name, NULL);
328
329                 result = mf_ug_fs_oper_drm_is_action_allowed(fullpath, DRM_HAS_VALID_SETAS_STATUS, DRM_SETAS_RINGTONE);
330                 if (result) {
331                         mf_ug_drm_file_mime_type drm_mime_type = mf_ug_fs_oper_get_drm_type(fullpath);
332                         if (drm_mime_type == MF_UG_DRM_RINGTONE_FILE)
333                                 return TRUE;
334                         else
335                                 return FALSE;
336                 } else {
337                         return FALSE;
338                 }
339
340         }
341
342         return FALSE;
343 }
344
345 static bool __mf_ug_fs_oper_exec_filter(ugFsNodeInfo *pnode_info, int option)
346 {
347         if (pnode_info == NULL) {
348                 return FALSE;
349         }
350         if (option & UG_FILTER_CATEGORY_IMAGE) {
351                 if (pnode_info->type == UG_FILE_TYPE_IMAGE) {
352                         return TRUE;
353                 }
354         }
355         if (option & UG_FILTER_CATEGORY_VIDEO) {
356                 if (pnode_info->type == UG_FILE_TYPE_VIDEO) {
357                         return TRUE;
358                 }
359         }
360         if (option & UG_FILTER_CATEGORY_SOUND) {
361                 if (pnode_info->type == UG_FILE_TYPE_SOUND) {
362                         return TRUE;
363                 }
364         }
365         if (option & UG_FILTER_CATEGORY_VOICE) {
366                 if (pnode_info->type == UG_FILE_TYPE_VOICE) {
367                         return TRUE;
368                 }
369         }
370         if (option & UG_FILTER_CATEGORY_MUSIC) {
371                 if (pnode_info->type == UG_FILE_TYPE_MUSIC) {
372                         return TRUE;
373                 }
374         }
375         if (option & UG_FILTER_CATEGORY_HTML) {
376                 if (pnode_info->type == UG_FILE_TYPE_HTML) {
377                         return TRUE;
378                 }
379         }
380         if (option & UG_FILTER_CATEGORY_FLASH) {
381                 if (pnode_info->type == UG_FILE_TYPE_FLASH) {
382                         return TRUE;
383                 }
384         }
385         if (option & UG_FILTER_CATEGORY_GAME) {
386                 if (pnode_info->type == UG_FILE_TYPE_GAME) {
387                         return TRUE;
388                 }
389         }
390         if (option & UG_FILTER_CATEGORY_APP) {
391                 if (pnode_info->type == UG_FILE_TYPE_APP) {
392                         return TRUE;
393                 }
394         }
395         if (option & UG_FILTER_CATEGORY_THEME) {
396                 if (pnode_info->type == UG_FILE_TYPE_THEME) {
397                         return TRUE;
398                 }
399         }
400         if (option & UG_FILTER_CATEGORY_DOC) {
401                 if (pnode_info->type == UG_FILE_TYPE_DOC) {
402                         return TRUE;
403                 }
404         }
405         if (option & UG_FILTER_CATEGORY_EXCEL) {
406                 if (pnode_info->type == UG_FILE_TYPE_EXCEL) {
407                         return TRUE;
408                 }
409         }
410         if (option & UG_FILTER_CATEGORY_PPT) {
411                 if (pnode_info->type == UG_FILE_TYPE_PPT) {
412                         return TRUE;
413                 }
414         }
415         if (option & UG_FILTER_CATEGORY_PDF) {
416                 if (pnode_info->type == UG_FILE_TYPE_PDF) {
417                         return TRUE;
418                 }
419         }
420         if (option & UG_FILTER_CATEGORY_TXT) {
421                 if (pnode_info->type == UG_FILE_TYPE_TXT) {
422                         return TRUE;
423                 }
424         }
425         if (option & UG_FILTER_CATEGORY_VCONTACT) {
426                 if (pnode_info->type == UG_FILE_TYPE_VCONTACT) {
427                         return TRUE;
428                 }
429         }
430         if (option & UG_FILTER_CATEGORY_VCALENDAR) {
431                 if (pnode_info->type == UG_FILE_TYPE_VCALENDAR) {
432                         return TRUE;
433                 }
434         }
435         if (option & UG_FILTER_CATEGORY_VNOTE) {
436                 if (pnode_info->type == UG_FILE_TYPE_VNOTE) {
437                         return TRUE;
438                 }
439         }
440         if (option & UG_FILTER_CATEGORY_VBOOKMARK) {
441                 if (pnode_info->type == UG_FILE_TYPE_VBOOKMARK) {
442                         return TRUE;
443                 }
444         }
445         if (option & UG_FILTER_CATEGORY_VIDEO_PROJECT) {
446                 if (pnode_info->type == UG_FILE_TYPE_VIDEO_PROJECT) {
447                         return TRUE;
448                 }
449         }
450         if (option & UG_FILTER_CATEGORY_SVG) {
451                 if (pnode_info->type == UG_FILE_TYPE_SVG) {
452                         return TRUE;
453                 }
454         }
455         if (option & UG_FILTER_CATEGORY_RSS) {
456                 if (pnode_info->type == UG_FILE_TYPE_RSS) {
457                         return TRUE;
458                 }
459         }
460         if (option & UG_FILTER_CATEGORY_ETC) {
461                 if (pnode_info->type == UG_FILE_TYPE_ETC) {
462                         return TRUE;
463                 }
464         }
465         return FALSE;
466 }
467
468 int mf_ug_fs_oper_list_filter(Eina_List *in_list, Eina_List **out_list, int option, int drm_opt)
469 {
470         ug_mf_debug();
471         if (in_list == NULL) {
472                 return MYFILE_ERR_SRC_ARG_INVALID;
473         }
474
475         if (out_list == NULL) {
476                 return MYFILE_ERR_DST_ARG_INVALID;
477         }
478
479         if (option == 0) {
480                 *out_list = in_list;
481                 return MYFILE_ERR_NONE;
482         }
483
484         Eina_List *l = NULL;
485         ugFsNodeInfo *data = NULL;
486         EINA_LIST_FOREACH(in_list, l, data) {
487                 if (data->type == UG_FILE_TYPE_DRM && __mf_ug_fs_oper_exec_drm_filter(data, drm_opt)) {
488                         ug_debug("file [%s] is drm file", data->name);
489                         *out_list = eina_list_append(*out_list, data);
490                 } else if (__mf_ug_fs_oper_exec_filter(data, option)) {
491                         *out_list = eina_list_append(*out_list, data);
492                 }
493         }
494         return MYFILE_ERR_NONE;
495 }
496
497 /******************************
498 ** Prototype    : ug_mf_list_filter_by_extention
499 ** Description  : filter from list by extension
500 ** Input        : Eina_List *in_list
501 **                Eina_List **out_list
502 **                char* ext
503 ** Output       : None
504 ** Return Value :
505 ** Calls        :
506 ** Called By    :
507 **
508 **  History        :
509 **  1.Date         : 2010/12/10
510 **    Author       : Samsung
511 **    Modification : Created function
512 **
513 ******************************/
514 int mf_ug_fs_oper_list_filter_by_extension(Eina_List *in_list, Eina_List **out_list, char *ext)
515 {
516         if (in_list == NULL) {
517                 return MYFILE_ERR_SRC_ARG_INVALID;
518         }
519
520         if (out_list == NULL) {
521                 return MYFILE_ERR_DST_ARG_INVALID;
522         }
523
524         if (ext == NULL) {
525                 *out_list = in_list;
526                 return MYFILE_ERR_NONE;
527         }
528
529         Eina_List *l = NULL;
530         ugFsNodeInfo *data = NULL;
531
532         char *seps = ";";
533         char *temp_ext = malloc(strlen(ext) + 1);
534         gchar **result = NULL;
535         gchar **params = NULL;
536
537         EINA_LIST_FOREACH(in_list, l, data) {
538                 memset(temp_ext, 0, strlen(ext) + 1);
539                 strncpy(temp_ext, ext, strlen(ext));
540                 result = g_strsplit(temp_ext, seps, 0);
541                 if (result == NULL) {
542                         continue;
543                 }
544                 for (params = result; *params; params++) {
545                         if (data->ext == NULL)
546                                 break;
547                         if (strcasecmp(data->ext, *params) == 0) {
548                                 *out_list = eina_list_append(*out_list, data);
549                                 break;
550                         }
551                 }
552
553                 g_strfreev(result);
554                 result = NULL;
555         }
556         free(temp_ext);
557         return MYFILE_ERR_NONE;
558 }
559
560 static int __mf_ug_fs_oper_sort_by_priority(const void *d1, const void *d2, int sequence_type)
561 {
562         int ret = 0;
563         switch(sequence_type) {
564         case MF_UG_SORT_BY_PRIORITY_TYPE_A2Z:
565                 ret = __mf_ug_fs_oper_sort_by_date_cb_O2R(d1, d2);
566                 if (ret == 0) {
567                         ret = __mf_ug_fs_oper_sort_by_size_cb_S2L(d1, d2);
568                         if (ret == 0) {
569                                 ret = __mf_ug_fs_oper_sort_by_name_cb_A2Z(d1, d2);
570                         }
571                 }
572                 break;
573         case MF_UG_SORT_BY_PRIORITY_TYPE_Z2A:
574                 ret = __mf_ug_fs_oper_sort_by_date_cb_R2O(d1, d2);
575                 if (ret == 0) {
576                         ret = __mf_ug_fs_oper_sort_by_size_cb_L2S(d1, d2);
577                         if (ret == 0) {
578                                 ret = __mf_ug_fs_oper_sort_by_name_cb_Z2A(d1, d2);
579                         }
580                 }
581                 break;
582         case MF_UG_SORT_BY_PRIORITY_DATE_O2R:
583                 ret = __mf_ug_fs_oper_sort_by_size_cb_S2L(d1, d2);
584                 if (ret == 0) {
585                         ret = __mf_ug_fs_oper_sort_by_name_cb_A2Z(d1, d2);
586                 }
587                 break;
588         case MF_UG_SORT_BY_PRIORITY_DATE_R2O:
589                 ret = __mf_ug_fs_oper_sort_by_size_cb_L2S(d1, d2);
590                 if (ret == 0) {
591                         ret = __mf_ug_fs_oper_sort_by_name_cb_Z2A(d1, d2);
592                 }
593                 break;
594         case MF_UG_SORT_BY_PRIORITY_SIZE_S2L:
595                 ret = __mf_ug_fs_oper_sort_by_name_cb_A2Z(d1, d2);
596                 break;
597         case MF_UG_SORT_BY_PRIORITY_SIZE_L2S:
598                 ret = __mf_ug_fs_oper_sort_by_name_cb_Z2A(d1, d2);
599                 break;
600         default:
601                 break;
602         }
603         return ret;
604 }
605 /*********************
606 **Function name:        __sort_by_name_cb
607 **Parameter:
608 **      const void *d1: node1 to compare
609 **      const void *d2: node2 to compare
610 **
611 **Return value:
612 **      -1      if d1 > d2
613 **      0       if d1 = d2
614 **      1       if d1 > d2
615 **
616 **Action:
617 **      sort the list order by the Assic table
618
619 **
620 *********************/
621 static int __mf_ug_fs_oper_sort_by_name_cb_A2Z(const void *d1, const void *d2)
622 {
623         ugFsNodeInfo *txt1 = (ugFsNodeInfo *) d1;
624         ugFsNodeInfo *txt2 = (ugFsNodeInfo *) d2;
625         gchar *name1 = NULL;
626         gchar *name2 = NULL;
627         int result = 0;
628
629         if (!txt1) {
630                 return (1);
631         }
632         if (!txt2) {
633                 return (-1);
634         }
635
636         name1 = g_ascii_strdown(txt1->name, strlen(txt1->name));
637         if (name1 == NULL) {
638                 return (-1);
639         }
640         name2 = g_ascii_strdown(txt2->name, strlen(txt2->name));
641         if (name2 == NULL) {
642                 g_free(name1);
643                 name1 = NULL;
644                 return (-1);
645         }
646         result = g_strcmp0(name1, name2);
647
648         g_free(name1);
649         name1 = NULL;
650         g_free(name2);
651         name2 = NULL;
652         return result;
653
654 }
655
656 /*********************
657 **Function name:        __sort_by_date_cb
658 **Parameter:
659 **      const void *d1: node1 to compare
660 **      const void *d2: node2 to compare
661 **
662 **Return value:
663 **      -1      if d1 > d2
664 **      0       if d1 = d2
665 **      1       if d1 > d2
666 **
667 **Action:
668 **      sort the list order by the later created the later shown
669 *********************/
670 static int __mf_ug_fs_oper_sort_by_date_cb_O2R(const void *d1, const void *d2)
671 {
672         int ret = 0;
673         ugFsNodeInfo *time1 = (ugFsNodeInfo *) d1;
674         ugFsNodeInfo *time2 = (ugFsNodeInfo *) d2;
675
676         if (!d1) {
677                 return 1;
678         }
679         if (!d2) {
680                 return -1;
681         }
682
683         if (time1->date > time2->date) {
684                 ret = 1;
685         } else if (time1->date < time2->date) {
686                 ret = -1;
687         } else {
688                 ret = 0;
689         }
690
691         if (ret == 0) {
692                 ret = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_DATE_O2R);
693         }
694         return ret;
695 }
696
697 /*********************
698 **Function name:        __sort_by_type_cb
699 **Parameter:
700 **      const void *d1: node1 to compare
701 **      const void *d2: node2 to compare
702 **
703 **Return value:
704 **      -1      if d1 < d2
705 **      0       if d1 = d2
706 **      1       if d1 > d2
707 **
708 **Action:
709 **      sort the list order by the category type value
710 *********************/
711 static int __mf_ug_fs_oper_sort_by_type_cb_A2Z(const void *d1, const void *d2)
712 {
713         ugFsNodeInfo *type1 = (ugFsNodeInfo *) d1;
714         ugFsNodeInfo *type2 = (ugFsNodeInfo *) d2;
715         gchar *ext1 = NULL;
716         gchar *ext2 = NULL;
717         int result = 0;
718
719         if (type1 == NULL || type1->ext == NULL) {
720                 return 1;
721         }
722
723         if (type2 == NULL || type2->ext == NULL) {
724                 return -1;
725         }
726         ext1 = g_ascii_strdown(type1->ext, strlen(type1->ext));
727         if (ext1 == NULL) {
728                 return (-1);
729         }
730         ext2 = g_ascii_strdown(type2->ext, strlen(type2->ext));
731         if (ext2 == NULL) {
732                 g_free(ext1);
733                 ext1 = NULL;
734                 return (-1);
735         }
736         result = g_strcmp0(ext1, ext2);
737
738         g_free(ext1);
739         ext1 = NULL;
740         g_free(ext2);
741         ext2 = NULL;
742
743         if (result == 0) {
744                 result = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_TYPE_A2Z);
745         }
746
747         return result;
748 }
749
750 /*order:        the one with smaller size will be shown earlier*/
751 /*********************
752 **Function name:        __sort_by_name_cb
753 **Parameter:
754 **      const void *d1: node1 to compare
755 **      const void *d2: node2 to compare
756 **
757 **Return value:
758 **      -1      if d1 > d2
759 **      0       if d1 = d2
760 **      1       if d1 > d2
761 **
762 **Action:
763 **      sort the list order by size, rule is the smaller the later shown
764 *********************/
765 static int __mf_ug_fs_oper_sort_by_size_cb_S2L(const void *d1, const void *d2)
766 {
767         int ret = 0;
768         ugFsNodeInfo *size1 = (ugFsNodeInfo *) d1;
769         ugFsNodeInfo *size2 = (ugFsNodeInfo *) d2;
770
771         if (!d1) {
772                 return 1;
773         }
774
775         if (!d2) {
776                 return -1;
777         }
778
779         if (size1->size > size2->size) {
780                 ret = 1;
781         } else if (size1->size < size2->size) {
782                 ret = -1;
783         } else {
784                 ret = 0;
785         }
786
787         if (ret == 0) {
788                 ret = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_SIZE_S2L);
789         }
790         return ret;
791 }
792
793 /*********************
794 **Function name:        __mf_fs_oper_sort_by_name_cb_Z2A
795 **Parameter:
796 **      const void *d1: node1 to compare
797 **      const void *d2: node2 to compare
798 **
799 **Return value:
800 **      1       if d1 > d2
801 **      -1      if d1 <= d2
802 **
803 **Action:
804 **      sort the list order by the Assic table
805
806 **
807 *********************/
808 static int __mf_ug_fs_oper_sort_by_name_cb_Z2A(const void *d1, const void *d2)
809 {
810         ugFsNodeInfo *txt1 = (ugFsNodeInfo *) d1;
811         ugFsNodeInfo *txt2 = (ugFsNodeInfo *) d2;
812
813         int result = 0;
814
815         if (!txt1) {
816                 return (1);
817         }
818         if (!txt2) {
819                 return (-1);
820         }
821         result = strcasecmp(txt1->name, txt2->name);
822
823         if (result < 0) {
824                 return (1);
825         } else {
826                 return (-1);
827         }
828 }
829
830 /*********************
831 **Function name:        __sort_by_date_cb
832 **Parameter:
833 **      const void *d1: node1 to compare
834 **      const void *d2: node2 to compare
835 **
836 **Return value:
837 **      -1      if d1 > d2
838 **      0       if d1 = d2
839 **      1       if d1 < d2
840 **
841 **Action:
842 **      sort the list order by the later created the later shown
843 *********************/
844 static int __mf_ug_fs_oper_sort_by_date_cb_R2O(const void *d1, const void *d2)
845 {
846         int ret = 0;
847         ugFsNodeInfo *time1 = (ugFsNodeInfo *) d1;
848         ugFsNodeInfo *time2 = (ugFsNodeInfo *) d2;
849
850         if (!d1) {
851                 return -1;
852         }
853         if (!d2) {
854                 return 1;
855         }
856         if (time1->date > time2->date) {
857                 ret = -1;
858         } else if (time1->date < time2->date) {
859                 ret = 1;
860         } else {
861                 ret = 0;
862         }
863
864         if (ret == 0) {
865                 ret = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_DATE_R2O);
866         }
867         return ret;
868 }
869
870 /*********************
871 **Function name:        __sort_by_type_cb
872 **Parameter:
873 **      const void *d1: node1 to compare
874 **      const void *d2: node2 to compare
875 **
876 **Return value:
877 **      -1      if d1 > d2
878 **      0       if d1 = d2
879 **      1       if d1 < d2
880 **
881 **Action:
882 **      sort the list order by the category type value
883 *********************/
884 static int __mf_ug_fs_oper_sort_by_type_cb_Z2A(const void *d1, const void *d2)
885 {
886         ugFsNodeInfo *type1 = (ugFsNodeInfo *) d1;
887         ugFsNodeInfo *type2 = (ugFsNodeInfo *) d2;
888         gchar *ext1 = NULL;
889         gchar *ext2 = NULL;
890         int result = 0;
891
892         if (type1 == NULL || type1->ext == NULL) {
893                 return -1;
894         }
895
896         if (type2 == NULL || type2->ext == NULL) {
897                 return 1;
898         }
899
900         ext1 = g_ascii_strdown(type1->ext, strlen(type1->ext));
901         if (ext1 == NULL) {
902                 return (1);
903         }
904         ext2 = g_ascii_strdown(type2->ext, strlen(type2->ext));
905         if (ext2 == NULL) {
906                 g_free(ext1);
907                 ext1 = NULL;
908                 return (-1);
909         }
910         result = g_strcmp0(ext1, ext2);
911         g_free(ext1);
912         ext1 = NULL;
913         g_free(ext2);
914         ext2 = NULL;
915         if (result == 0) {
916                 result = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_TYPE_Z2A);
917         }
918
919         return -result;
920 }
921
922 /*order:        the one with smaller size will be shown earlier*/
923 /*********************
924 **Function name:        __sort_by_name_cb
925 **Parameter:
926 **      const void *d1: node1 to compare
927 **      const void *d2: node2 to compare
928 **
929 **Return value:
930 **      -1      if d1 > d2
931 **      0       if d1 = d2
932 **      1       if d1 < d2
933 **
934 **Action:
935 **      sort the list order by size, rule is the smaller the later shown
936 *********************/
937 static int __mf_ug_fs_oper_sort_by_size_cb_L2S(const void *d1, const void *d2)
938 {
939         int ret = 0;
940         ugFsNodeInfo *size1 = (ugFsNodeInfo *) d1;
941         ugFsNodeInfo *size2 = (ugFsNodeInfo *) d2;
942
943         if (!d1) {
944                 return -1;
945         }
946
947         if (!d2) {
948                 return 1;
949         }
950
951         if (size1->size > size2->size) {
952                 ret = -1;
953         } else if (size1->size < size2->size) {
954                 ret = 1;
955         } else {
956                 ret = 0;
957         }
958
959         if (ret == 0) {
960                 ret = __mf_ug_fs_oper_sort_by_priority(d1, d2, MF_UG_SORT_BY_PRIORITY_SIZE_L2S);
961         }
962         return ret;
963 }
964
965 /*********************
966 **Function name:        mf_fs_oper_sort_list
967 **Parameter:
968 **      Eina_List **list:       the list we need to sort
969 **      int sort_opt:           sort option
970 **
971 **Return value:
972 **      void
973 **
974 **Action:
975 **      sort the list order by sort option with the call back
976 *********************/
977 void mf_ug_fs_oper_sort_list(Eina_List **list, int sort_opt)
978 {
979         Eina_Compare_Cb sort_func = NULL;
980         if (!(*list)) {
981                 return;
982         }
983         switch (sort_opt) {
984         case MF_UG_SORT_BY_NAME_A2Z:
985                 sort_func = __mf_ug_fs_oper_sort_by_name_cb_A2Z;
986                 break;
987         case MF_UG_SORT_BY_TYPE_A2Z:
988                 sort_func = __mf_ug_fs_oper_sort_by_type_cb_A2Z;
989                 break;
990         case MF_UG_SORT_BY_SIZE_S2L:
991                 sort_func = __mf_ug_fs_oper_sort_by_size_cb_S2L;
992                 break;
993         case MF_UG_SORT_BY_DATE_O2R:
994                 sort_func = __mf_ug_fs_oper_sort_by_date_cb_O2R;
995                 break;
996         case MF_UG_SORT_BY_NAME_Z2A:
997                 sort_func = __mf_ug_fs_oper_sort_by_name_cb_Z2A;
998                 break;
999         case MF_UG_SORT_BY_TYPE_Z2A:
1000                 sort_func = __mf_ug_fs_oper_sort_by_type_cb_Z2A;
1001                 break;
1002         case MF_UG_SORT_BY_SIZE_L2S:
1003                 sort_func = __mf_ug_fs_oper_sort_by_size_cb_L2S;
1004                 break;
1005         case MF_UG_SORT_BY_DATE_R2O:
1006                 sort_func = __mf_ug_fs_oper_sort_by_date_cb_R2O;
1007                 break;
1008         default:
1009                 sort_func = __mf_ug_fs_oper_sort_by_type_cb_A2Z;
1010                 break;
1011         }
1012         *list = eina_list_sort(*list, eina_list_count(*list), sort_func);
1013 }
1014
1015 int mf_ug_fs_oper_create_dir(const char *dir)
1016 {
1017         int option = MF_ERROR_CHECK_SRC_ARG_VALID | MF_ERROR_CHECK_DUPLICATED;
1018         int ret = __mf_ug_fs_oper_file_system_error(dir, dir, option);
1019
1020         if (ret != 0) {
1021                 return ret;
1022         }
1023
1024         ret = mf_ug_file_attr_is_right_dir_path(dir);
1025
1026         if (ret != 0) {
1027                 return ret;
1028         }
1029
1030         mode_t default_mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
1031
1032         if (mkdir(dir, default_mode) < 0) {
1033                 return MYFILE_ERR_DIR_CREATE_FAIL;
1034         }
1035         return MYFILE_ERR_NONE;
1036 }
1037