Move code for sorting from view file to sort file 45/47345/3
authorHyojung Jo <hj903.jo@samsung.com>
Wed, 2 Sep 2015 11:47:12 +0000 (20:47 +0900)
committerHyojung Jo <hj903.jo@samsung.com>
Thu, 3 Sep 2015 02:22:27 +0000 (11:22 +0900)
Change-Id: Id5dd42f1c7503f39b96bad01b72184f39e138ff1
Signed-off-by: Hyojung Jo <hj903.jo@samsung.com>
include/data/app.h
include/define.h
include/utils.h
src/data/app.c
src/grid/grid.c
src/utils.c
src/view/view_action_menu.c
src/view/view_base.c

index 2752d69..9ba812c 100644 (file)
 
 #include <Elementary.h>
 
+enum sort_type {
+       SORT_INVALID = -1,
+       SORT_RECENT = 0,
+       SORT_A_TO_Z,
+       SORT_Z_TO_A,
+       SORT_MAX,
+};
+
 struct app_data;
 
-Eina_List *get_app_list(void);
 char *get_app_id(struct app_data *adata);
 char *get_pkg_id(struct app_data *adata);
 char *get_app_name(struct app_data *adata);
@@ -33,7 +40,9 @@ bool get_app_icon_bg_color(struct app_data *adata,
                int *r, int *g, int *b, int *a);
 bool get_app_text_bg_color(struct app_data *adata,
                int *r, int *g, int *b, int *a);
-void free_app_list(Eina_List *list);
+Eina_List *get_app_list(void);
 Eina_List *get_app_favorite_list(void);
+bool get_sorted_app_list(Eina_List **list, enum sort_type type);
+void free_app_list(Eina_List *list);
 
 #endif /* __AIR_APPS_APP_H__ */
index 7b6f1e8..3882da2 100644 (file)
 #define STR_Z_TO_A "Z-A"
 #define STR_NULL "(NULL)"
 #define STR_NO_CONTENTS "No Contents"
-#define STR_BROWSER_ID "org.tizen.browser"
 #define STR_BROWSER "Web Browser"
 #define STR_NOT_SUPPORTED "Not Supported"
 #define STR_LIVETV "Live TV"
 #define STR_FAVORITE "Favorite"
 #define STR_UNINSTALL "Uninstall"
-#define STR_LIVETV_ID "org.tizen.live-tv"
 #define STR_NO_FAVORITE "No favorite contents"
 #define STR_ADD_FAVORITE "Added to your favorite."
 #define STR_REMOVE_FAVORITE "Removed from your favorite."
index 0d4aaf5..068b2a6 100644 (file)
@@ -35,7 +35,7 @@ Evas_Object *utils_add_notify(Evas_Object *parent, const char *text,
                int timeout);
 Evas_Object *utils_add_popup(Evas_Object *parent, const char *style,
                const char *popup_title, const char *popup_text);
-Evas_Object *utils_add_ctxpopup(Evas_Object *parent, int opt_size,
+Evas_Object *utils_add_ctxpopup(Evas_Object *parent, int opt_size, int *opt_id,
                const char **opt_text, input_handler *opt_handler, void *data);
 bool utils_launch_app(const char *appid);
 bool utils_uninstall_app(const char *pkgid, const char *pkgtype);
index a2035bd..5233d7d 100644 (file)
  * limitations under the License.
  */
 
+#include <glib.h>
 #include <pkgmgr-info.h>
 #include <app_contents.h>
+#include <app_define.h>
 #include <app_debug.h>
 
 #include "define.h"
@@ -27,7 +29,6 @@ struct app_data {
        char *name;
        char *icon;
        char *pkgtype;
-       bool is_locked;
        struct color_data icon_bg;
        struct color_data text_bg;
 };
@@ -98,7 +99,7 @@ static int _get_app_data_foreach(pkgmgrinfo_appinfo_h handle, void *data)
        if (pkgtype)
                adata->pkgtype = strdup(pkgtype);
 
-       if (!strcmp(appid, STR_BROWSER_ID)) {
+       if (!strcmp(appid, APP_ID_BROWSER)) {
                _set_default_color(&adata->icon_bg);
                _set_default_color(&adata->text_bg);
        } else {
@@ -274,3 +275,119 @@ Eina_List *get_app_favorite_list(void)
 
        return fav_list;
 }
+
+static int _sort_by_a_to_z(const void *first_data, const void *second_data)
+{
+       struct app_data *adata1, *adata2;
+
+       adata1 = (struct app_data *)first_data;
+       adata2 = (struct app_data *)second_data;
+
+       if (!adata1 || !get_app_name(adata1))
+               return 1;
+
+       if (!adata2 || !get_app_name(adata2))
+               return -1;
+
+       return strcmp(get_app_name(adata1), get_app_name(adata2));
+}
+
+static int _sort_by_z_to_a(const void *first_data, const void *second_data)
+{
+       struct app_data *adata1, *adata2;
+
+       adata1 = (struct app_data *)first_data;
+       adata2 = (struct app_data *)second_data;
+
+       if (!adata1 || !get_app_name(adata1))
+               return -1;
+
+       if (!adata2 || !get_app_name(adata2))
+               return 1;
+
+       return strcmp(get_app_name(adata2), get_app_name(adata1));
+}
+
+static void _recent_list_foreach(gpointer data, gpointer user_data)
+{
+       struct recent_data *rdata;
+       struct app_data *adata;
+       Eina_List **list;
+       Eina_List *l = NULL;
+
+       if (!data || !user_data) {
+               _ERR("Data is NULL.");
+               return;
+       }
+
+       list = user_data;
+       rdata = data;
+
+       if (!rdata->id)
+               return;
+
+       EINA_LIST_FOREACH(*list, l, adata) {
+               if (adata && get_app_id(adata) &&
+                               !strcmp(get_app_id(adata), rdata->id)) {
+                       *list = eina_list_promote_list(*list, l);
+                       break;
+               }
+       }
+}
+
+static void _sort_by_recently_used(Eina_List **list)
+{
+       GList *recent_l = NULL;
+       struct app_data *adata;
+       Eina_List *l = NULL;
+       int r;
+
+       r = app_contents_get_recent_list(CONTENTS_APP, -1, &recent_l);
+       if (r != APP_CONTENTS_ERROR_NONE) {
+               _ERR("Get recent list failed.");
+               return;
+       }
+
+       if (!recent_l)
+               return;
+
+       recent_l = g_list_reverse(recent_l);
+       g_list_foreach(recent_l, _recent_list_foreach, list);
+
+       /* The 'Web Browser' should be at first when 'Recent' option selected */
+       EINA_LIST_FOREACH(*list, l, adata) {
+               if (adata && get_app_id(adata) &&
+                               !strcmp(get_app_id(adata), APP_ID_BROWSER)) {
+                       *list = eina_list_promote_list(*list, l);
+                       break;
+               }
+       }
+}
+
+bool get_sorted_app_list(Eina_List **list, enum sort_type type)
+{
+       if (!list) {
+               _ERR("Invalid argument.");
+               return false;
+       }
+
+       switch (type) {
+       case SORT_RECENT:
+               _sort_by_recently_used(list);
+               break;
+
+       case SORT_A_TO_Z:
+               *list = eina_list_sort(*list, 0, _sort_by_a_to_z);
+               break;
+
+       case SORT_Z_TO_A:
+               *list = eina_list_sort(*list, 0, _sort_by_z_to_a);
+               break;
+
+       default:
+               _ERR("Unhandled sort type.");
+               return false;
+       }
+
+       return true;
+}
index 5245133..83c4f56 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <Elementary.h>
 #include <gridmgr.h>
+#include <app_define.h>
 #include <app_debug.h>
 
 #include "define.h"
@@ -31,10 +32,10 @@ static char *_text_get(void *data, Evas_Object *obj, const char *part)
        }
        adata = data;
 
-       if (!strcmp(get_app_id(adata), STR_BROWSER_ID) &&
+       if (!strcmp(get_app_id(adata), APP_ID_BROWSER) &&
                        !strcmp(part, PART_THUMB_TEXT_BROWSER))
                return strdup(STR_BROWSER);
-       else if (strcmp(get_app_id(adata), STR_BROWSER_ID) &&
+       else if (strcmp(get_app_id(adata), APP_ID_BROWSER) &&
                        !strcmp(part, PART_THUMB_TEXT))
                return strdup(get_app_name(adata));
 
@@ -65,7 +66,7 @@ static Evas_Object *_content_get(void *data, Evas_Object *obj, const char *part)
 
                if (!icon || !strcmp(icon, STR_NULL))
                        img_path = DEFAULT_APP_ICON_PNG;
-               else if (!strcmp(get_app_id(adata), STR_BROWSER_ID))
+               else if (!strcmp(get_app_id(adata), APP_ID_BROWSER))
                        img_path = BROWSER_ICON_PNG;
                else
                        img_path = icon;
index e42b957..ac1064e 100644 (file)
@@ -273,7 +273,7 @@ Evas_Object *utils_add_popup(Evas_Object *parent, const char *style,
        return popup;
 }
 
-Evas_Object *utils_add_ctxpopup(Evas_Object *parent, int opt_size,
+Evas_Object *utils_add_ctxpopup(Evas_Object *parent, int opt_size, int *opt_id,
                const char **opt_text, input_handler *opt_handler, void *data)
 {
        Evas_Object *ctxpopup, *box, *btn = NULL, *firstbtn = NULL;
@@ -314,7 +314,8 @@ Evas_Object *utils_add_ctxpopup(Evas_Object *parent, int opt_size,
                elm_box_pack_end(box, btn);
 
                if (opt_handler)
-                       inputmgr_add_callback(btn, i, opt_handler, data);
+                       inputmgr_add_callback(btn, opt_id[i],
+                                       opt_handler, data);
 
                if (i == 0)
                        firstbtn = btn;
index cc84802..8eecd38 100644 (file)
@@ -18,6 +18,7 @@
 #include <viewmgr.h>
 #include <inputmgr.h>
 #include <gridmgr.h>
+#include <app_define.h>
 #include <app_debug.h>
 
 #include "define.h"
@@ -82,7 +83,7 @@ static void _mouse_move_cb(int id, void *data, Evas *e, Evas_Object *obj,
 
 static void _livetv_selected(void)
 {
-       if (!utils_launch_app(STR_LIVETV_ID)) {
+       if (!utils_launch_app(APP_ID_LIVETV)) {
                _ERR("Launch live tv failed.");
                return;
        }
@@ -303,7 +304,7 @@ static void _grid_realized_cb(int id, void *data, Evas_Object *obj,
        elm_object_item_signal_callback_add(item, SIG_ITEM_SELECTED,
                        SIG_SOURCE_EDC, _grid_item_selected_cb, data);
 
-       if (!strcmp(get_app_id(adata), STR_BROWSER_ID))
+       if (!strcmp(get_app_id(adata), APP_ID_BROWSER))
                elm_object_item_signal_emit(item, SIG_VISIBLE,
                                PART_INSIDE_LINE);
 }
index f83d65f..b01fcf6 100644 (file)
@@ -20,6 +20,7 @@
 #include <gridmgr.h>
 #include <inputmgr.h>
 #include <app_contents.h>
+#include <app_define.h>
 #include <app_debug.h>
 
 #include "define.h"
@@ -33,19 +34,19 @@ enum menu_type {
        MENU_APPSTORE,
 };
 
-enum sort_type {
-       SORT_RECENT = 0,
-       SORT_A_TO_Z,
-       SORT_Z_TO_A,
-};
-
 const char *str_menu[] = {
        STR_MYAPPS,
        STR_APPSTORE,
        NULL
 };
 
-const char *str_sort[] = {
+static int id_sort_opt[] = {
+       SORT_RECENT,
+       SORT_A_TO_Z,
+       SORT_Z_TO_A,
+};
+
+const char *str_sort_opt[] = {
        STR_RECENT,
        STR_A_TO_Z,
        STR_Z_TO_A,
@@ -196,121 +197,19 @@ static void _destroy_sort_ctxpopup(struct _priv *priv)
        priv->sort_popup = NULL;
 }
 
-static int _sort_by_a_to_z(const void *first_data, const void *second_data)
-{
-       struct app_data *adata1, *adata2;
-
-       adata1 = (struct app_data *)first_data;
-       adata2 = (struct app_data *)second_data;
-
-       if (!adata1 || !get_app_name(adata1))
-               return 1;
-
-       if (!adata2 || !get_app_name(adata2))
-               return -1;
-
-       return strcmp(get_app_name(adata1), get_app_name(adata2));
-}
-
-static int _sort_by_z_to_a(const void *first_data, const void *second_data)
-{
-       struct app_data *adata1, *adata2;
-
-       adata1 = (struct app_data *)first_data;
-       adata2 = (struct app_data *)second_data;
-
-       if (!adata1 || !get_app_name(adata1))
-               return -1;
-
-       if (!adata2 || !get_app_name(adata2))
-               return 1;
-
-       return strcmp(get_app_name(adata2), get_app_name(adata1));
-}
-
-static void _recent_list_foreach(gpointer data, gpointer user_data)
-{
-       struct _priv *priv;
-       struct recent_data *rdata;
-       struct app_data *adata;
-       Eina_List *l = NULL;
-
-       if (!data || !user_data) {
-               _ERR("Data is NULL.");
-               return;
-       }
-       priv = user_data;
-       rdata = data;
-
-       if (!priv->app_list || !rdata->id)
-               return;
-
-       EINA_LIST_FOREACH(priv->app_list, l, adata) {
-               if (adata && get_app_id(adata) &&
-                               !strcmp(get_app_id(adata), rdata->id)) {
-                       priv->app_list =
-                               eina_list_promote_list(priv->app_list, l);
-                       break;
-               }
-       }
-}
-
-static void _sort_by_recently_used(struct _priv *priv)
+static void _sort_option_selected(struct _priv *priv, int id, Evas_Object *obj)
 {
-       GList *recent_l = NULL;
-       struct app_data *adata;
-       Eina_List *l = NULL;
-       int r;
-
-       r = app_contents_get_recent_list(CONTENTS_APP, -1, &recent_l);
-       if (r != APP_CONTENTS_ERROR_NONE) {
-               _ERR("Get recent list failed.");
+       if (!priv->app_list || id <= SORT_INVALID || id >= SORT_MAX) {
+               _ERR("Cannot sort");
                return;
        }
 
-       if (!recent_l)
+       if (!get_sorted_app_list(&priv->app_list, id)) {
+               _ERR("Sort failed.");
                return;
-
-       recent_l = g_list_reverse(recent_l);
-       g_list_foreach(recent_l, _recent_list_foreach, priv);
-
-       /* The 'Web Browser' should be at first when 'Recent' option selected */
-       EINA_LIST_FOREACH(priv->app_list, l, adata) {
-               if (adata && get_app_id(adata) &&
-                               !strcmp(get_app_id(adata), STR_BROWSER_ID)) {
-                       priv->app_list =
-                               eina_list_promote_list(priv->app_list, l);
-                       break;
-               }
        }
-}
 
-static void _sort_option_selected(struct _priv *priv, int id, Evas_Object *obj)
-{
-       if (!priv->app_list)
-               return;
-
-       switch (id) {
-       case SORT_RECENT:
-               _sort_by_recently_used(priv);
-               priv->sort_type = SORT_RECENT;
-               break;
-
-       case SORT_A_TO_Z:
-               priv->app_list = eina_list_sort(priv->app_list, 0,
-                               _sort_by_a_to_z);
-               priv->sort_type = SORT_A_TO_Z;
-               break;
-
-       case SORT_Z_TO_A:
-               priv->app_list = eina_list_sort(priv->app_list, 0,
-                               _sort_by_z_to_a);
-               priv->sort_type = SORT_Z_TO_A;
-               break;
-
-       default:
-               _ERR("Invalid sort type.");
-       }
+       priv->sort_type = id;
 
        _destroy_sort_ctxpopup(priv);
        elm_object_text_set(priv->sort_btn, elm_object_text_get(obj));
@@ -365,8 +264,8 @@ static bool _draw_sort_option(struct _priv *priv)
 
        elm_object_signal_emit(priv->sort_btn, SIG_SELECTED, SRC_SORT_BTN);
 
-       ctxpopup = utils_add_ctxpopup(priv->base, SORT_COUNT, str_sort,
-                       &_option_input_handler, priv);
+       ctxpopup = utils_add_ctxpopup(priv->base, SORT_COUNT, id_sort_opt,
+                       str_sort_opt, &_option_input_handler, priv);
        if (!ctxpopup) {
                _ERR("Add ctxpopup failed.");
                return false;
@@ -511,7 +410,9 @@ static void _grid_item_selected_cb(void *data, Elm_Object_Item *it,
        elm_gengrid_item_selected_set(it, EINA_FALSE);
 
        if (priv->sort_type == SORT_RECENT) {
-               _sort_by_recently_used(priv);
+               if (!get_sorted_app_list(&priv->app_list, SORT_RECENT))
+                       return;
+
                gridmgr_append_list(priv->gmgr, STR_MYAPPS, priv->app_list);
 
                item = elm_gengrid_first_item_get(priv->grid);
@@ -547,7 +448,7 @@ static void _grid_realized_cb(int id, void *data, Evas_Object *obj,
        elm_object_item_signal_callback_add(item, SIG_ITEM_SELECTED,
                        SIG_SOURCE_EDC, _grid_item_selected_cb, data);
 
-       if (!strcmp(get_app_id(adata), STR_BROWSER_ID))
+       if (!strcmp(get_app_id(adata), APP_ID_BROWSER))
                elm_object_item_signal_emit(item, SIG_VISIBLE,
                                PART_INSIDE_LINE);
 
@@ -665,7 +566,7 @@ static bool _draw_sort_area(struct _priv *priv)
        Evas_Object *btn;
 
        btn = utils_add_button(priv->base, PART_SORT,
-                       str_sort[priv->sort_type], STYLE_SORT_BTN);
+                       str_sort_opt[priv->sort_type], STYLE_SORT_BTN);
        if (!btn) {
                _ERR("Add button failed.");
                return false;
@@ -774,7 +675,8 @@ static Evas_Object *_create(Evas_Object *win, void *data)
 
        priv->grid = grid;
        priv->app_list = get_app_list();
-       _sort_by_recently_used(priv);
+
+       get_sorted_app_list(&priv->app_list, SORT_RECENT);
 
        if (!viewmgr_set_view_data(VIEW_BASE, priv)) {
                _ERR("Set view data failed.");