File Manager / src / view /

popup.c

  1. /*
  2. * Copyright 2014 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
  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. #include "view/popup.h"
  19. #include "view/list-view.h"
  20. #include "utils/logger.h"
  21. #include "utils/app-types.h"
  22. #include "utils/config.h"
  23. #include "utils/common-utils.h"
  24. #include "main-app.h"
  25.  
  26. #include <efl_extension.h>
  27.  
  28. static const char *const POPUP_BTN_OK = "Ok";
  29. static const char *const POPUP_BTN_RETRY = "Retry";
  30. static const char *const POPUP_BTN_CANCEL = "Cancel";
  31.  
  32. static const char *const POPUP_TEXT_DELETE = "Selected nodes will be deleted. Are you sure?";
  33. static const char *const POPUP_TEXT_COPY_MOVE = "Data saved in clipboard";
  34. static const char *const POPUP_TEXT_FILE_EXISTS = "Folder already exists";
  35. static const char *const POPUP_TEXT_CREATE_FOLDER_FAIL = "Fail to create folder";
  36. static const char *const POPUP_TEXT_INCORRECT_NAME = "Empty folder name";
  37. static const char *const POPUP_TEXT_NEW_FOLDER = "New Folder";
  38. static const char *const POPUP_TEXT_TITLE_NEW_FOLDER = "Add new folder";
  39. static const char *const POPUP_TEXT_EMPTY_EDIT_FIELD = "Enter the folder name";
  40. static const char *const POPUP_TEXT_MOVE_ERROR = "Moving error";
  41. static const char *const POPUP_TEXT_MOVE_RECURSIVE_ERROR = "Cannot move catalog into itself";
  42. static const char *const POPUP_TEXT_COPY_ERROR = "Operation copy failed";
  43. static const char *const POPUP_TEXT_COPY_RECURSIVE_ERROR = "Cannot copy catalog into itself";
  44. static const char *const POPUP_TEXT_ERROR = "Operation failed";
  45.  
  46. static int _popup_delete_type_create(view_data *view);
  47. static int _popup_copy_move_type_create(view_data *view);
  48. static int _popup_new_folder_type_create(view_data *view, const char *folder_name);
  49. static int _popup_incorrect_name_type_create(view_data *view);
  50. static int _popup_copy_move_error_type_create(view_data *view, const char *error_text);
  51. static int _popup_error_type_create(view_data *view);
  52. static int _popup_create_folder_error_type_create(view_data *view, const char *folder_name, const char *error_text);
  53.  
  54. static Evas_Object *_popup_new(Evas_Object *parent, const char *text, const void *data);
  55. static int _popup_button_add(Evas_Object *parent, const char *part, const char *btn_text, Evas_Smart_Cb btn_cb, void *cb_data);
  56. static int _popup_editfield_add(Evas_Object *popup, const char *folder_name);
  57.  
  58. static void _popup_cancel_cb(void *data, Evas_Object *obj, void *event_info);
  59. static void _popup_delete_ok_cb(void *data, Evas_Object *obj, void *event_info);
  60. static void _popup_create_folder_ok_cb(void *data, Evas_Object *obj, void *event_info);
  61. static void _popup_create_folder_error_ok_cb(void *data, Evas_Object *obj, void *event_info);
  62. static void _popup_incorrect_name_ok_cb(void *data, Evas_Object *obj, void *event_info);
  63. static void _popup_back_button_cb(void *data, Evas_Object *obj, void *event_info);
  64.  
  65. int popup_create(view_data *view, popup_type type)
  66. {
  67. RETVM_IF(!view, RESULT_TYPE_INVALID_ARG, "View obj is NULL");
  68.  
  69. int res = RESULT_TYPE_OK;
  70.  
  71. switch (type) {
  72. case POPUP_TYPE_DELETE:
  73. res = _popup_delete_type_create(view);
  74. break;
  75. case POPUP_TYPE_COPY_MOVE:
  76. res = _popup_copy_move_type_create(view);
  77. break;
  78. case POPUP_TYPE_NEW_FOLDER:
  79. res = _popup_new_folder_type_create(view, POPUP_TEXT_NEW_FOLDER);
  80. break;
  81. case POPUP_TYPE_WRONG_FILE_NAME:
  82. res = _popup_incorrect_name_type_create(view);
  83. break;
  84. case POPUP_TYPE_FAIL_TO_MOVE:
  85. res = _popup_copy_move_error_type_create(view, POPUP_TEXT_MOVE_ERROR);
  86. break;
  87. case POPUP_TYPE_MOVE_RECURSIVE_FAIL:
  88. res = _popup_copy_move_error_type_create(view, POPUP_TEXT_MOVE_RECURSIVE_ERROR);
  89. break;
  90. case POPUP_TYPE_FAIL_TO_COPY:
  91. res = _popup_copy_move_error_type_create(view, POPUP_TEXT_COPY_ERROR);
  92. break;
  93. case POPUP_TYPE_COPY_RECURSIVE_FAIL:
  94. res = _popup_copy_move_error_type_create(view, POPUP_TEXT_COPY_RECURSIVE_ERROR);
  95. break;
  96. case POPUP_TYPE_ERROR:
  97. res = _popup_error_type_create(view);
  98. break;
  99. default:
  100. ERR("Incorrect popup type");
  101. res = RESULT_TYPE_FAIL;
  102. break;
  103. }
  104.  
  105. return res;
  106. }
  107.  
  108. static int _popup_delete_type_create(view_data *view)
  109. {
  110. Evas_Object *popup = _popup_new(view->navi, POPUP_TEXT_DELETE, view);
  111. RETVM_IF(!popup, RESULT_TYPE_INVALID_ARG, "Fail to create popup");
  112.  
  113. int res = _popup_button_add(popup, "button1", POPUP_BTN_OK, _popup_delete_ok_cb, popup);
  114. if (res != RESULT_TYPE_OK) {
  115. ERR("Fail to create button");
  116. evas_object_del(popup);
  117. return RESULT_TYPE_FAIL;
  118. }
  119.  
  120. res = _popup_button_add(popup, "button2", POPUP_BTN_CANCEL, _popup_cancel_cb, popup);
  121. if (res != RESULT_TYPE_OK) {
  122. ERR("Fail to create button");
  123. evas_object_del(popup);
  124. return RESULT_TYPE_FAIL;
  125. }
  126.  
  127. return RESULT_TYPE_OK;
  128. }
  129.  
  130. static int _popup_copy_move_type_create(view_data *view)
  131. {
  132. Evas_Object *popup = _popup_new(view->navi, POPUP_TEXT_COPY_MOVE, view);
  133. RETVM_IF(!popup, RESULT_TYPE_INVALID_ARG, "Fail to create popup");
  134.  
  135. int res = _popup_button_add(popup, "button1", POPUP_BTN_OK, _popup_cancel_cb, popup);
  136. if (res != RESULT_TYPE_OK) {
  137. ERR("Fail to create button");
  138. evas_object_del(popup);
  139. return RESULT_TYPE_FAIL;
  140. }
  141.  
  142. return RESULT_TYPE_OK;
  143. }
  144.  
  145. static int _popup_new_folder_type_create(view_data *view, const char *folder_name)
  146. {
  147. RETVM_IF(!folder_name, RESULT_TYPE_INVALID_ARG, "Folder name is NULL");
  148.  
  149. Evas_Object *popup = _popup_new(view->navi, NULL, view);
  150. RETVM_IF(!popup, RESULT_TYPE_INVALID_ARG, "Fail to create popup");
  151.  
  152. elm_object_part_text_set(popup, "title,text", POPUP_TEXT_TITLE_NEW_FOLDER);
  153.  
  154. int res = _popup_editfield_add(popup, folder_name);
  155. RETVM_IF(res != RESULT_TYPE_OK, res, "Fail to add editfield");
  156.  
  157. res = _popup_button_add(popup, "button1", POPUP_BTN_OK, _popup_create_folder_ok_cb, popup);
  158. if (res != RESULT_TYPE_OK) {
  159. ERR("Fail to create button");
  160. evas_object_del(popup);
  161. return RESULT_TYPE_FAIL;
  162. }
  163.  
  164. res = _popup_button_add(popup, "button2", POPUP_BTN_CANCEL, _popup_cancel_cb, popup);
  165. if (res != RESULT_TYPE_OK) {
  166. ERR("Fail to create button");
  167. evas_object_del(popup);
  168. return RESULT_TYPE_FAIL;
  169. }
  170.  
  171. return RESULT_TYPE_OK;
  172. }
  173.  
  174. static int _popup_copy_move_error_type_create(view_data *view, const char *error_text)
  175. {
  176. Evas_Object *popup = _popup_new(view->navi, error_text, view);
  177. RETVM_IF(!popup, RESULT_TYPE_INVALID_ARG, "Fail to create popup");
  178.  
  179. int res = _popup_button_add(popup, "button1", POPUP_BTN_OK, _popup_cancel_cb, popup);
  180. if (res != RESULT_TYPE_OK) {
  181. ERR("Fail to create button");
  182. evas_object_del(popup);
  183. return RESULT_TYPE_FAIL;
  184. }
  185.  
  186. return RESULT_TYPE_OK;
  187. }
  188.  
  189. static int _popup_create_folder_error_type_create(view_data *view, const char *folder_name, const char *error_text)
  190. {
  191. Evas_Object *popup = _popup_new(view->navi, error_text, view);
  192. RETVM_IF(!popup, RESULT_TYPE_INVALID_ARG, "Fail to create popup");
  193.  
  194. int res = _popup_button_add(popup, "button1", POPUP_BTN_RETRY, _popup_create_folder_error_ok_cb, popup);
  195. if (res != RESULT_TYPE_OK) {
  196. ERR("Fail to create button");
  197. evas_object_del(popup);
  198. return RESULT_TYPE_FAIL;
  199. }
  200.  
  201. res = _popup_button_add(popup, "button2", POPUP_BTN_CANCEL, _popup_cancel_cb, popup);
  202. if (res != RESULT_TYPE_OK) {
  203. ERR("Fail to create button");
  204. evas_object_del(popup);
  205. return RESULT_TYPE_FAIL;
  206. }
  207.  
  208. evas_object_data_set(popup, "folder_name", folder_name);
  209. return res;
  210. }
  211.  
  212. static int _popup_error_type_create(view_data *view)
  213. {
  214. Evas_Object *popup = _popup_new(view->navi, POPUP_TEXT_ERROR, view);
  215. RETVM_IF(!popup, RESULT_TYPE_INVALID_ARG, "Fail to create popup");
  216.  
  217. int res = _popup_button_add(popup, "button1", POPUP_BTN_OK, _popup_cancel_cb, popup);
  218. if (res != RESULT_TYPE_OK) {
  219. ERR("Fail to create button");
  220. evas_object_del(popup);
  221. return RESULT_TYPE_FAIL;
  222. }
  223.  
  224. return RESULT_TYPE_OK;
  225. }
  226.  
  227. static int _popup_incorrect_name_type_create(view_data *view)
  228. {
  229. Evas_Object *popup = _popup_new(view->navi, POPUP_TEXT_INCORRECT_NAME, view);
  230. RETVM_IF(!popup, RESULT_TYPE_INVALID_ARG, "Fail to create popup");
  231.  
  232. int res = _popup_button_add(popup, "button1", POPUP_BTN_OK, _popup_incorrect_name_ok_cb, popup);
  233. if (res != RESULT_TYPE_OK) {
  234. ERR("Fail to create button");
  235. evas_object_del(popup);
  236. return RESULT_TYPE_FAIL;
  237. }
  238.  
  239. return RESULT_TYPE_OK;
  240. }
  241.  
  242. static void _popup_back_button_cb(void *data, Evas_Object *obj, void *event_info)
  243. {
  244. evas_object_del(obj);
  245. }
  246.  
  247. static Evas_Object *_popup_new(Evas_Object *parent, const char *text, const void *data)
  248. {
  249. Evas_Object *popup = elm_popup_add(parent);
  250. RETVM_IF(!popup, NULL, "Fail to create popup");
  251.  
  252. elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
  253.  
  254. eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, _popup_back_button_cb, NULL);
  255.  
  256. elm_object_style_set(popup, "default");
  257. elm_object_text_set(popup, text);
  258.  
  259. evas_object_data_set(popup, "view", data);
  260. evas_object_show(popup);
  261.  
  262. return popup;
  263. }
  264.  
  265. static int _popup_editfield_add(Evas_Object *popup, const char *folder_name)
  266. {
  267. Evas_Object *editfield = elm_layout_add(popup);
  268. RETVM_IF(!editfield, RESULT_TYPE_FAIL, "Fail to create editfield");
  269. elm_layout_theme_set(editfield, "layout", "editfield", "singleline");
  270. evas_object_size_hint_align_set(editfield, EVAS_HINT_FILL, 0.0);
  271. evas_object_size_hint_weight_set(editfield, EVAS_HINT_EXPAND, 0.0);
  272.  
  273. Evas_Object *entry = elm_entry_add(editfield);
  274. if (!entry) {
  275. ERR("Fail to create entry");
  276. evas_object_del(editfield);
  277. return RESULT_TYPE_FAIL;
  278. }
  279. evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  280. elm_entry_scrollable_set(entry, EINA_TRUE);
  281. elm_entry_single_line_set(entry, EINA_TRUE);
  282. elm_object_part_text_set(entry, "elm.guide", POPUP_TEXT_EMPTY_EDIT_FIELD);
  283. elm_object_part_text_set(entry, "elm.text", folder_name);
  284. elm_entry_cursor_end_set(entry);
  285. elm_object_focus_set(entry, EINA_TRUE);
  286.  
  287. elm_object_part_content_set(editfield, "elm.swallow.content", entry);
  288. elm_object_content_set(popup, editfield);
  289.  
  290. return RESULT_TYPE_OK;
  291. }
  292.  
  293. static int _popup_button_add(Evas_Object *parent, const char *part, const char *btn_text, Evas_Smart_Cb btn_cb, void *cb_data)
  294. {
  295. Evas_Object *btn = elm_button_add(parent);
  296. RETVM_IF(!btn, RESULT_TYPE_INVALID_ARG, "Fail to create button");
  297.  
  298. evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  299.  
  300. elm_object_text_set(btn, btn_text);
  301. evas_object_smart_callback_add(btn, "clicked", btn_cb, cb_data);
  302.  
  303. elm_object_part_content_set(parent, part, btn);
  304.  
  305. return RESULT_TYPE_OK;
  306. }
  307.  
  308. static void _popup_cancel_cb(void *data, Evas_Object *obj, void *event_info)
  309. {
  310. RETM_IF(!data, "Data is NULL");
  311.  
  312. char *folder_name = evas_object_data_get(data, "folder_name");
  313. free(folder_name);
  314.  
  315. evas_object_del(data);
  316. }
  317.  
  318. static void _popup_delete_ok_cb(void *data, Evas_Object *obj, void *event_info)
  319. {
  320. RETM_IF(!data, "Data is NULL");
  321.  
  322. view_data *view = evas_object_data_get(data, "view");
  323. evas_object_del(data);
  324.  
  325. view->app->status.curr_mode = MODE_DEFAULT;
  326.  
  327. int res = list_view_delete_items(view);
  328. RETM_IF(res != RESULT_TYPE_OK, "Fail to delete file");
  329. }
  330.  
  331. static void _popup_create_folder_ok_cb(void *data, Evas_Object *obj, void *event_info)
  332. {
  333. RETM_IF(!data, "Data is NULL");
  334.  
  335. Evas_Object *popup = data;
  336. view_data *view = evas_object_data_get(popup, "view");
  337.  
  338. Evas_Object *editfield = elm_object_content_get(popup);
  339. Evas_Object *entry = elm_object_part_content_get(editfield, "elm.swallow.content");
  340. const char *folder_name = elm_entry_entry_get(entry);
  341.  
  342. int res = RESULT_TYPE_OK;
  343.  
  344. if (!folder_name || common_util_is_string_empty(folder_name)) {
  345. evas_object_del(data);
  346. res = _popup_incorrect_name_type_create(view);
  347.  
  348. RETM_IF(res != RESULT_TYPE_OK, "Fail to create popup");
  349. return;
  350. }
  351.  
  352. res = list_view_create_folder(view, folder_name);
  353. if (res != RESULT_TYPE_OK) {
  354. char *temp_name = strdup(folder_name);
  355. if (!temp_name) {
  356. ERR("Fail to allocate memory for current folder name");
  357. evas_object_del(data);
  358. return;
  359. }
  360.  
  361. evas_object_del(data);
  362.  
  363. const char *error_text = NULL;
  364. error_text = (res == RESULT_TYPE_DUPLICATED_NAME) ? POPUP_TEXT_FILE_EXISTS
  365. : POPUP_TEXT_CREATE_FOLDER_FAIL;
  366.  
  367. res = _popup_create_folder_error_type_create(view, temp_name, error_text);
  368. if (res != RESULT_TYPE_OK) {
  369. ERR("Fail to create popup");
  370. free(temp_name);
  371. }
  372.  
  373. return;
  374. }
  375.  
  376. res = list_view_update(view->navi_item, UPDATE_TYPE_VIEW | UPDATE_TYPE_CTRL_BAR_MODE);
  377. evas_object_del(data);
  378.  
  379. RETM_IF(res != RESULT_TYPE_OK, "Fail to update view");
  380. }
  381.  
  382. static void _popup_create_folder_error_ok_cb(void *data, Evas_Object *obj, void *event_info)
  383. {
  384. RETM_IF(!data, "Data is NULL");
  385.  
  386. char *folder_name = evas_object_data_get(data, "folder_name");
  387. view_data *view = evas_object_data_get(data, "view");
  388. evas_object_del(data);
  389.  
  390. int res = _popup_new_folder_type_create(view, folder_name);
  391. free(folder_name);
  392. RETM_IF(res != RESULT_TYPE_OK, "Fail to create popup");
  393. }
  394.  
  395. static void _popup_incorrect_name_ok_cb(void *data, Evas_Object *obj, void *event_info)
  396. {
  397. RETM_IF(!data, "Data is NULL");
  398.  
  399. view_data *view = evas_object_data_get(data, "view");
  400. evas_object_del(data);
  401.  
  402. int res = _popup_new_folder_type_create(view, POPUP_TEXT_NEW_FOLDER);
  403. RETM_IF(res != RESULT_TYPE_OK, "Fail to create popup");
  404. }
  405.