yakov pushed a commit to branch master.

http://git.enlightenment.org/tools/erigo.git/commit/?id=108ad7f09cb1eebbe2f5fc9ae96861705dc0dd53

commit 108ad7f09cb1eebbe2f5fc9ae96861705dc0dd53
Author: Yakov Goldberg <yako...@samsung.com>
Date:   Mon Nov 16 15:37:15 2015 +0200

    Refactoring new/open project flow.
    
    Combine, separate, add wrappers for appropriate functions.
---
 src/bin/gui/editor.c             |  11 +--
 src/bin/gui/egui_logic.c         | 198 +++++++++++++++++++--------------------
 src/bin/gui/egui_logic_private.h |   2 +-
 3 files changed, 102 insertions(+), 109 deletions(-)

diff --git a/src/bin/gui/editor.c b/src/bin/gui/editor.c
index 875195b..b659758 100644
--- a/src/bin/gui/editor.c
+++ b/src/bin/gui/editor.c
@@ -3947,15 +3947,14 @@ _observers_init(const Gui_Context *ctx)
    return EINA_TRUE;
 }
 
-static Eina_Bool
+static Gui_Context *
 _project_new(const char *filename)
 {
-   Gui_Context *ctx;
-   ctx = gui_context_new();
+   Gui_Context *ctx = gui_context_new();
    if (!ctx)
      {
         ERR("Can not create context: %p", ctx);
-        return EINA_FALSE;
+        return NULL;
      }
 
    if (filename)
@@ -3963,7 +3962,7 @@ _project_new(const char *filename)
         if (!gui_parser_json_file_read(ctx, filename))
           {
              gui_context_del(ctx);
-             return EINA_FALSE;
+             return NULL;
           }
      }
 
@@ -3989,7 +3988,7 @@ _project_new(const char *filename)
    eo_do(g->main_win->context_toolbar, glit = 
elm_obj_toolbar_item_append(NULL, projname, _context_toolbar_clicked, ctx));
    elm_object_item_data_set(glit, ctx);
    eo_do(glit, elm_obj_toolbar_item_selected_set(EINA_TRUE));
-   return EINA_TRUE;
+   return ctx;
 }
 
 Eina_Bool
diff --git a/src/bin/gui/egui_logic.c b/src/bin/gui/egui_logic.c
index 3961a56..879812b 100644
--- a/src/bin/gui/egui_logic.c
+++ b/src/bin/gui/egui_logic.c
@@ -176,10 +176,67 @@ _popup_cb(Popup_Button_Type button_type, void *data)
    eina_stringshare_del(path);
 }
 
+/* Helper to open/create project and update UI. */
+static Gui_Context*
+_project_open_create_internal(const char *filename, int fs_mode, const char 
*dir, const char *file_name, const char *project_name)
+{
+   Eina_Bool post_open = EINA_FALSE;
+   Gui_Context *ctx = NULL;
+   switch (fs_mode)
+     {
+      case ITEM_OPEN:
+           {
+              ctx = _guilogic_cbs->_project_new(filename);
+              if (ctx) post_open = EINA_TRUE;
+              break;
+           }
+      case ITEM_NEW:
+           {
+              ctx = _guilogic_cbs->_project_new(NULL);
+              if (ctx) post_open = EINA_TRUE;
+
+              gui_context_project_name_set((Gui_Context *) ctx, project_name);
+              gui_context_project_path_set((Gui_Context *) ctx, dir);
+              gui_context_project_filename_set((Gui_Context *) ctx, file_name);
+              _context_save(ctx);
+              break;
+           }
+     }
+
+   if (post_open)
+     {
+        _canvas_name_update(ctx, EINA_FALSE);
+        /* If no more free contexts, disable items. */
+        if (!gui_context_free_is())
+          {
+             _menu_item_disabled_set(project_menu, ITEM_OPEN, EINA_TRUE);
+             _menu_item_disabled_set(project_menu, ITEM_NEW, EINA_TRUE);
+          }
+        _menu_item_disabled_set(project_menu, ITEM_CLOSE, EINA_FALSE);
+        _menu_item_disabled_set(project_menu, ITEM_SAVE, EINA_FALSE);
+        _menu_item_disabled_set(project_menu, ITEM_SAVEAS, EINA_FALSE);
+        _menu_item_disabled_set(project_menu, ITEM_EXPORT, EINA_FALSE);
+        _guilogic_cbs->_theme_hoversel_fill();
+     }
+   return ctx;
+}
+
+void
+project_open_create(const char *filename, int fs_mode, const char *dir, const 
char *file_name, const char *project_name)
+{
+   if (fs_mode == ITEM_OPEN)
+     {
+        _project_new_mode_internal(filename, fs_mode, NULL, NULL, NULL);
+     }
+   else if (fs_mode == ITEM_NEW)
+     {
+        _project_open_create_internal(NULL, fs_mode, dir, file_name, 
project_name);
+     }
+}
+
 static void
 _on_fs_done(void *data, Evas_Object *obj EINA_UNUSED, void *event)
 {
-   const Gui_Context *ctx = _active_context_get();
    const char *path = event;
    Eina_Bool path_exists;
    Eina_Bool is_dir;
@@ -202,22 +259,7 @@ _on_fs_done(void *data, Evas_Object *obj EINA_UNUSED, void 
*event)
       case ITEM_OPEN:
            {
               if ((!path_exists) || (path_exists && is_dir)) goto end;
-              if (_guilogic_cbs->_project_new(path))
-                {
-                   ctx = _active_context_get();
-                   _canvas_name_update(ctx, EINA_FALSE);
-                   /* If no more free contexts, disable items. */
-                   if (!gui_context_free_is())
-                     {
-                        _menu_item_disabled_set(project_menu, ITEM_OPEN, 
EINA_TRUE);
-                        _menu_item_disabled_set(project_menu, ITEM_NEW, 
EINA_TRUE);
-                     }
-                   _menu_item_disabled_set(project_menu, ITEM_CLOSE, 
EINA_FALSE);
-                   _menu_item_disabled_set(project_menu, ITEM_SAVE, 
EINA_FALSE);
-                   _menu_item_disabled_set(project_menu, ITEM_SAVEAS, 
EINA_FALSE);
-                   _menu_item_disabled_set(project_menu, ITEM_EXPORT, 
EINA_FALSE);
-                   _guilogic_cbs->_theme_hoversel_fill();
-                }
+              project_open_create(path, fs_mode, NULL, NULL, NULL);
               break;
            }
       case ITEM_NEW:
@@ -233,51 +275,33 @@ _on_fs_done(void *data, Evas_Object *obj EINA_UNUSED, 
void *event)
                    break;
                 }
 
-              if (_guilogic_cbs->_project_new(NULL))
+              const char *project_name = NULL;
+              eo_do(fs_win->fs_project_name_entry, project_name = 
elm_obj_widget_part_text_get(NULL));
+              if (!strlen(project_name)) project_name = "New Project";
+
+              char *default_filename = strdup(project_name), *p;
+              eina_str_tolower(&default_filename);
+              p = default_filename;
+              while (*p)
                 {
-                   ctx = _active_context_get();
-                   /* If no more free contexts, disable items. */
-                   if (!gui_context_free_is())
-                     {
-                        _menu_item_disabled_set(project_menu, ITEM_OPEN, 
EINA_TRUE);
-                        _menu_item_disabled_set(project_menu, ITEM_NEW, 
EINA_TRUE);
-                     }
-                   _menu_item_disabled_set(project_menu, ITEM_CLOSE, 
EINA_FALSE);
-                   _menu_item_disabled_set(project_menu, ITEM_SAVE, 
EINA_FALSE);
-                   _menu_item_disabled_set(project_menu, ITEM_SAVEAS, 
EINA_FALSE);
-                   _menu_item_disabled_set(project_menu, ITEM_EXPORT, 
EINA_FALSE);
-                   _guilogic_cbs->_theme_hoversel_fill();
-                   const char *project_name = NULL;
-                   eo_do(fs_win->fs_project_name_entry, project_name = 
elm_obj_widget_part_text_get(NULL));
-                   if (!strlen(project_name)) project_name = "New Project";
-
-                   char *default_filename = strdup(project_name), *p;
-                   eina_str_tolower(&default_filename);
-                   p = default_filename;
-                   while (*p)
-                     {
-                        if ((*p) == ' ') (*p) = '_';
-                        p++;
-                     }
+                   if ((*p) == ' ') (*p) = '_';
+                   p++;
+                }
 
-                   char full_path[PATH_MAX] = {'\0'};
-                   char full_filename[PATH_MAX] = {'\0'};
-                   int i = 1;
-                   sprintf(full_filename, "%s", default_filename);
+              char full_path[PATH_MAX] = {'\0'};
+              char full_filename[PATH_MAX] = {'\0'};
+              int i = 1;
+              sprintf(full_filename, "%s", default_filename);
+              sprintf(full_path, "%s/%s", path, full_filename);
+              /* If file exists, add number in the end of file. */
+              while (ecore_file_exists(full_path))
+                {
+                   sprintf(full_filename, "%s%d", default_filename, i++);
                    sprintf(full_path, "%s/%s", path, full_filename);
-                   /* If file exists, add number in the end of file. */
-                   while (ecore_file_exists(full_path))
-                     {
-                        sprintf(full_filename, "%s%d", default_filename, i++);
-                        sprintf(full_path, "%s/%s", path, full_filename);
-                     }
-
-                   gui_context_project_name_set((Gui_Context *) ctx, 
project_name);
-                   gui_context_project_path_set((Gui_Context *) ctx, path);
-                   gui_context_project_filename_set((Gui_Context *) ctx, 
full_filename);
-                   _context_save(ctx);
-                   free(default_filename);
                 }
+              project_open_create(NULL, fs_mode, path, full_filename, 
project_name);
+              free(default_filename);
+
               break;
            }
       case ITEM_EXPORT:
@@ -289,6 +313,7 @@ _on_fs_done(void *data, Evas_Object *obj EINA_UNUSED, void 
*event)
                    ERR("can save, but file exists, show confirmation window");
                 }
               char ch_file[PATH_MAX];
+              const Gui_Context *ctx = _active_context_get();
               gui_context_export_filename_set((Gui_Context *) ctx, filename);
               sprintf(ch_file, "%s.c", filename);
               gui_context_export_c_filename_set((Gui_Context *) ctx, ch_file);
@@ -326,6 +351,7 @@ _on_fs_done(void *data, Evas_Object *obj EINA_UNUSED, void 
*event)
                 }
               else
                 {
+                   const Gui_Context *ctx = _active_context_get();
                    gui_context_project_filename_set((Gui_Context *) ctx, 
filename);
                    gui_context_project_path_set((Gui_Context *) ctx, (const 
char *) parent_dir);
                    _context_save(ctx);
@@ -782,52 +808,20 @@ _key_binding_save_cb(void *data)
 static void
 _egui_project_open(const char *path)
 {
-   if (path)
+   if (!path) return;
+   /* If file exists try open it. */
+   if (ecore_file_exists(path))
      {
-        Eina_Bool post_open = EINA_FALSE;
-        /* If file exists try open it. */
-        if (ecore_file_exists(path))
-          {
-             if (_guilogic_cbs->_project_new(path))
-               {
-                  post_open = EINA_TRUE;
-               }
-          }
-        /* If not, create new file. */
-        else
-          {
-             if (_guilogic_cbs->_project_new(NULL))
-               {
-                  const Gui_Context *ctx = _active_context_get();
-
-                  char *_dir = ecore_file_dir_get(path);
-                  const char *filename = ecore_file_file_get(path);
-
-                  gui_context_project_name_set((Gui_Context *) ctx, filename);
-                  gui_context_project_path_set((Gui_Context *) ctx, _dir);
-                  gui_context_project_filename_set((Gui_Context *) ctx, 
filename);
-                  _context_save(ctx);
-
-                  free(_dir);
-                  post_open = EINA_TRUE;
-               }
-          }
+        project_open_create(path, ITEM_OPEN, NULL, NULL, NULL);
+     }
+   /* If not, create new file. */
+   else
+     {
+        char *_dir = ecore_file_dir_get(path);
+        const char *filename = ecore_file_file_get(path);
 
-        if (post_open)
-          {
-             _canvas_name_update(_active_context_get(), EINA_FALSE);
-             /* If no more free contexts, disable items. */
-             if (!gui_context_free_is())
-               {
-                  _menu_item_disabled_set(project_menu, ITEM_OPEN, EINA_TRUE);
-                  _menu_item_disabled_set(project_menu, ITEM_NEW, EINA_TRUE);
-               }
-             _menu_item_disabled_set(project_menu, ITEM_CLOSE, EINA_FALSE);
-             _menu_item_disabled_set(project_menu, ITEM_SAVE, EINA_FALSE);
-             _menu_item_disabled_set(project_menu, ITEM_SAVEAS, EINA_FALSE);
-             _menu_item_disabled_set(project_menu, ITEM_EXPORT, EINA_FALSE);
-             _guilogic_cbs->_theme_hoversel_fill();
-          }
+        project_open_create(NULL, ITEM_NEW, _dir, filename, filename);
+        free(_dir);
      }
 }
 
diff --git a/src/bin/gui/egui_logic_private.h b/src/bin/gui/egui_logic_private.h
index 29a17fa..863cc14 100644
--- a/src/bin/gui/egui_logic_private.h
+++ b/src/bin/gui/egui_logic_private.h
@@ -14,7 +14,7 @@ typedef struct
    const Gui_Widget *(*_editor_selected_wdg_get)();
 
    void (*_project_close)(const Gui_Context *ctx);
-   Eina_Bool (*_project_new)(const char *filename);
+   Gui_Context* (*_project_new)(const char *filename);
    void (*_theme_hoversel_fill)();
 } GuiLogicCbs;
 

-- 


Reply via email to