File Manager / src /

main-app.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 "main-app.h"
  19. #include "view/window.h"
  20. #include "view/main-view.h"
  21. #include "view/list-view.h"
  22. #include "view/navigator.h"
  23. #include "utils/ui-utils.h"
  24. #include "utils/logger.h"
  25. #include "model/fs-manager.h"
  26. #include "model/clipboard.h"
  27. #include "model/navi-path-storage.h"
  28.  
  29. #include <stdlib.h>
  30. #include <Elementary.h>
  31. #include <app.h>
  32. #include <elm_app.h>
  33.  
  34. #define BASE_DISPLAY_SCALE 2.6
  35.  
  36. /* app event callbacks */
  37. static bool _on_create_cb(void *user_data);
  38. static void _on_terminate_cb(void *user_data);
  39. static void _on_pause_cb(void *user_data);
  40. static void _on_resume_cb(void *user_data);
  41. static void _on_app_control_cb(app_control_h app_control, void *user_data);
  42.  
  43. static void _app_clear_data(app_data *app);
  44. static Evas_Object *_app_add_naviframe(app_data *app);
  45. static void _app_naviframe_backbutton_cb(void *data, Evas_Object *obj, void *event_info);
  46.  
  47. app_data *app_create()
  48. {
  49. app_data *app = calloc(1, sizeof(app_data));
  50. return app;
  51. }
  52.  
  53. void app_destroy(app_data *app)
  54. {
  55. free(app);
  56. }
  57.  
  58. int app_run(app_data *app, int argc, char **argv)
  59. {
  60. if (!app) {
  61. return -1;
  62. }
  63.  
  64. ui_app_lifecycle_callback_s cbs = {
  65. .create = _on_create_cb,
  66. .terminate = _on_terminate_cb,
  67. .pause = _on_pause_cb,
  68. .resume = _on_resume_cb,
  69. .app_control = _on_app_control_cb
  70. };
  71.  
  72. return ui_app_main(argc, argv, &cbs, app);
  73. }
  74.  
  75. static bool _on_create_cb(void *user_data)
  76. {
  77. app_data *app = user_data;
  78.  
  79. if (!app) {
  80. return false;
  81. }
  82.  
  83. elm_app_base_scale_set(BASE_DISPLAY_SCALE);
  84.  
  85. /* Create file system manager */
  86. app->manager = fs_manager_create();
  87. RETVM_IF(!app->manager, false, "Failed to create file system manager");
  88.  
  89. /* Create clipboard */
  90. app->clipboard = clipboard_create();
  91. if (!app->clipboard) {
  92. ERR("Failed to create clipboard");
  93. fs_manager_destroy(app->manager);
  94. return false;
  95. }
  96.  
  97. /* Create main window */
  98. app->win = win_create();
  99. if (!app->win) {
  100. ERR("Failed to create main window");
  101. _app_clear_data(app);
  102. return false;
  103. }
  104.  
  105. /* Create naviframe */
  106. app->navi = _app_add_naviframe(app);
  107. if (!app->navi) {
  108. ERR("Failed to create naviframe");
  109. _app_clear_data(app);
  110. return false;
  111. }
  112.  
  113. /* Create navigation path storage */
  114. app->path_storage = navi_path_storage_create();
  115. if (!app->path_storage) {
  116. ERR("Failed to create navigation path storage");
  117. _app_clear_data(app);
  118. return false;
  119. }
  120.  
  121. /* Create navigator */
  122. app->navigator = navigator_create(app);
  123. if (!app->navigator) {
  124. ERR("Failed to create navigator");
  125. _app_clear_data(app);
  126. return false;
  127. }
  128.  
  129. /* Create main view */
  130. int res = main_view_add(app, app->navi);
  131. if (res != RESULT_TYPE_OK) {
  132. ERR("Failed to create main view");
  133. _app_clear_data(app);
  134. return false;
  135. }
  136.  
  137. return true;
  138. }
  139.  
  140. static void _on_terminate_cb(void *user_data)
  141. {
  142. app_data *app = user_data;
  143. _app_clear_data(app);
  144. }
  145.  
  146. static void _on_pause_cb(void *user_data)
  147. {
  148. }
  149.  
  150. static void _on_resume_cb(void *user_data)
  151. {
  152. }
  153.  
  154. static void _on_app_control_cb(app_control_h app_control, void *user_data)
  155. {
  156. }
  157.  
  158. static void _app_clear_data(app_data *app)
  159. {
  160. if (app) {
  161. fs_manager_destroy(app->manager);
  162. app->manager = NULL;
  163. clipboard_destroy(app->clipboard);
  164. app->clipboard = NULL;
  165. win_destroy(app->win);
  166. app->win = NULL;
  167. navigator_destroy(app->navigator);
  168. app->navigator = NULL;
  169. navi_path_storage_destroy(app->path_storage);
  170. app->path_storage = NULL;
  171. }
  172. }
  173.  
  174. static Evas_Object *_app_add_naviframe(app_data *app)
  175. {
  176. Evas_Object *naviframe = NULL;
  177. Evas_Object *parent = win_get_host_layout(app->win);
  178. if (parent) {
  179. naviframe = ui_utils_navi_add(parent, _app_naviframe_backbutton_cb, app);
  180. if (naviframe) {
  181. win_set_layout(app->win, naviframe);
  182. }
  183. }
  184. return naviframe;
  185. }
  186.  
  187. static void _app_naviframe_backbutton_cb(void *data, Evas_Object *obj, void *event_info)
  188. {
  189. app_data *app = (app_data *)data;
  190.  
  191. if (app->status.curr_mode != MODE_DEFAULT) {
  192. app->status.curr_mode = MODE_DEFAULT;
  193. if (app->status.is_mainview) {
  194. RETM_IF(main_view_update(
  195. elm_naviframe_top_item_get(obj)) != RESULT_TYPE_OK,
  196. "Failed to navigate to previous view");
  197. } else {
  198. RETM_IF(list_view_update(
  199. elm_naviframe_top_item_get(obj), UPDATE_TYPE_GENLIST |
  200. UPDATE_TYPE_CTRL_BAR_MODE) != RESULT_TYPE_OK,
  201. "Failed to navigate to previous view");
  202. }
  203. return;
  204. }
  205.  
  206. if (elm_naviframe_top_item_get(obj) == elm_naviframe_bottom_item_get(obj)) {
  207. if (app) {
  208. win_lower(app->win);
  209. }
  210. } else {
  211. int res = navigator_goto_previous_view(app->navigator);
  212. RETM_IF(res != RESULT_TYPE_OK, "Failed to navigate to previous view");
  213. }
  214. }