<commit_before>#include "e.h"

static void *_create_data(E_Config_Dialog *cfd);
static void  _free_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static int   _apply_cfdata(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static Evas_Object *_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
static void _ilist_fill(E_Config_Dialog_Data *cfdata);
static void _ilist_cb_selected(void *data);
static void _cb_add(void *data, void *data2);
static void _cb_delete(void *data, void *data2);
static void _cb_reset(void *data, void *data2);
static void _cb_dialog_yes(void *data);
static void _cb_dialog_destroy(void *data);

E_Dialog *_dia_new_profile(E_Config_Dialog_Data *cfdata);
static void _new_profile_cb_close(void *data, E_Dialog *dia);
static void _new_profile_cb_ok(void *data, E_Dialog *dia);
static void _new_profile_cb_dia_del(void *obj);


struct _E_Config_Dialog_Data
{
   E_Config_Dialog *cfd;
   Evas_Object *o_list;
   Evas_Object *o_delete;
   Evas_Object *o_reset;
   Evas_Object *o_text;
   const char *sel_profile;

   E_Dialog *dia_new_profile;
   char *new_profile;
};

typedef struct _Del_Profile_Confirm_Data Del_Profile_Confirm_Data;
struct _Del_Profile_Confirm_Data
{
   E_Config_Dialog_Data *cfdata;
};

E_Config_Dialog *
e_int_config_profiles(E_Container *con, const char *params __UNUSED__)
{
   E_Config_Dialog *cfd;
   E_Config_Dialog_View *v;

   if (e_config_dialog_find("E", "settings/profiles")) return NULL;
   v = E_NEW(E_Config_Dialog_View, 1);
   if (!v) return NULL;
   v->create_cfdata = _create_data;
   v->free_cfdata = _free_data;
   v->basic.apply_cfdata = _apply_cfdata;
   v->basic.create_widgets = _create_widgets;

   cfd = e_config_dialog_new(con,
			     _("Profile Selector"),
			     "E", "settings/profiles",
			     "preferences-profiles", 0, v, NULL);
   e_config_dialog_changed_auto_set(cfd, 0);
   return cfd;
}

static void *
_create_data(E_Config_Dialog *cfd)
{
   E_Config_Dialog_Data *cfdata;

   cfdata = E_NEW(E_Config_Dialog_Data, 1);
   cfdata->cfd = cfd;
   return cfdata;
}

static void
_free_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
{
   E_FREE(cfdata);
}

static int
_apply_cfdata(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
{
   const char *cur_profile;
   E_Action *a;

   cur_profile = e_config_profile_get();
   if (strcmp (cur_profile, cfdata->sel_profile) == 0)
     return 1;

   e_config_save_flush();
   e_config_profile_set(cfdata->sel_profile);
   e_config_profile_save();
   e_config_save_block_set(1);

   a = e_action_find("restart");
   if ((a) && (a->func.go)) a->func.go(NULL, NULL);
   return 1;
}

static Evas_Object *
_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata)
{
   Evas_Object *o, *of, *ot, *ob;
   char buf[PATH_MAX];

   o = e_widget_list_add(evas, 0, 0);

   of = e_widget_framelist_add(evas, _("Available Profiles"), 0);
   cfdata->o_list = e_widget_ilist_add(evas, 24, 24, &(cfdata->sel_profile));
   e_widget_size_min_set(cfdata->o_list, 140 * e_scale, 50 * e_scale);
   e_widget_framelist_object_append(of, cfdata->o_list);

   e_widget_list_object_append(o, of, 1, 1, 0.5);

   ob = e_widget_textblock_add(evas);
   e_widget_size_min_set(ob, 140 * e_scale, 50 * e_scale);
   e_widget_textblock_markup_set(ob, _("Select a profile"));
   e_widget_list_object_append(o, ob, 1, 0, 0.5);
   cfdata->o_text = ob;
   
   ot = e_widget_table_add(evas, 0);
   ob = e_widget_button_add(evas, _("Add"), "list-add", _cb_add, cfdata, NULL);
   e_widget_table_object_append(ot, ob, 0, 0, 1, 1, 1, 1, 0, 0);
   cfdata->o_delete = e_widget_button_add(evas, _("Delete"), "list-remove", _cb_delete, cfdata, NULL);
   e_widget_table_object_append(ot, cfdata->o_delete, 1, 0, 1, 1, 1, 1, 0, 0);
   cfdata->o_reset = e_widget_button_add(evas, _("Reset"), "system-restart", _cb_reset, cfdata, NULL);
   e_widget_table_object_align_append(ot, cfdata->o_reset, 2, 0, 1, 1, 0, 1, 1, 1, 1.0, 0.5);

   // if there is a system version of the profile - allow reset
   e_prefix_data_snprintf(buf, sizeof(buf), "data/config/%s/", e_config_profile_get());
   if (ecore_file_is_dir(buf))
     e_widget_disabled_set(cfdata->o_reset, 0);
   else
     e_widget_disabled_set(cfdata->o_reset, 1);
   
   e_widget_disabled_set(cfdata->o_delete, 1);

   e_widget_list_object_append(o, ot, 1, 0, 0.0);

   _ilist_fill(cfdata);

   e_dialog_resizable_set(cfd->dia, 1);
   return o;
}

static void
_ilist_fill(E_Config_Dialog_Data *cfdata)
{
   Evas *evas;
   Eina_List *l, *profiles;
   int selected = -1, i;
   
   if (!cfdata) return;
   if (!cfdata->o_list) return;

   evas = evas_object_evas_get(cfdata->o_list);
   evas_event_freeze(evas);
   edje_freeze();
   e_widget_ilist_freeze(cfdata->o_list);

   e_widget_ilist_clear(cfdata->o_list);
   e_widget_ilist_go(cfdata->o_list);

   profiles = e_config_profile_list();
   for (i = 0, l = profiles; l; l = l->next, i++)
     {
        Efreet_Desktop *desk = NULL;
	Evas_Object *ic;
        char buf[PATH_MAX], *prof, *pdir;
        const char *label;
        
        prof = l->data;
        if (e_config_profile_get())
          {
             if (!strcmp(prof, e_config_profile_get())) selected = i;
          }
        pdir = e_config_profile_dir_get(prof);
        snprintf(buf, sizeof(buf), "%s/profile.desktop", pdir);
        desk = efreet_desktop_get(buf);
        if (!desk)
          {
             e_prefix_data_snprintf(buf, sizeof(buf), "data/config/%s/", prof);
             pdir = strdup(buf);
             if (pdir)
               {
                  snprintf(buf, sizeof(buf), "%s/profile.desktop", pdir);
                  desk = efreet_desktop_get(buf);
               }
          }
        label = prof;
        if ((desk) && (desk->name)) label = desk->name;
        buf[0] = 0;
        if (pdir)
          snprintf(buf, sizeof(buf), "%s/icon.edj", pdir);
        if ((desk) && (desk->icon) && (pdir))
          snprintf(buf, sizeof(buf), "%s/%s", pdir, desk->icon);
        else
          e_prefix_data_concat_static(buf, "data/images/enlightenment.png");
        ic = e_util_icon_add(buf, evas);
        e_widget_ilist_append(cfdata->o_list, ic, label, _ilist_cb_selected, cfdata, prof);
        if (pdir) free(pdir);
        free(prof);
        if (desk) efreet_desktop_free(desk);
     }
   if (profiles) eina_list_free(profiles);
   if (selected >= 0)
     e_widget_ilist_selected_set(cfdata->o_list, selected);   
   e_widget_size_min_set(cfdata->o_list, 155, 250);
   e_widget_ilist_go(cfdata->o_list);

   e_widget_ilist_thaw(cfdata->o_list);
   edje_thaw();
   evas_event_thaw(evas);
}

static void
_ilist_cb_selected(void *data)
{
   E_Config_Dialog_Data *cfdata;
   const char *cur_profile;
   unsigned char v;
   Efreet_Desktop *desk = NULL;
   char *pdir, buf[PATH_MAX];
   
   cfdata = data;
   if (!cfdata) return;

   cur_profile = e_config_profile_get();

   v = (strcmp(cur_profile, cfdata->sel_profile) == 0);
   e_widget_disabled_set(cfdata->o_delete, v);
   e_config_dialog_changed_set(cfdata->cfd, !v);

   pdir = e_config_profile_dir_get(cfdata->sel_profile);
   snprintf(buf, sizeof(buf), "%s/profile.desktop", pdir);
   desk = efreet_desktop_get(buf);
   if (!desk)
     {
        e_prefix_data_snprintf(buf, sizeof(buf), "data/config/%s/", cfdata->sel_profile);
        pdir = strdup(buf);
        if (pdir)
          {
             snprintf(buf, sizeof(buf), "%s/profile.desktop", pdir);
             desk = efreet_desktop_get(buf);
          }
     }
   if (desk)
     e_widget_textblock_markup_set(cfdata->o_text, desk->comment);
   else<commit_msg>bit better profile dialogs, at least it is aligned now.<commit_after>#include "e.h"

static void *_create_data(E_Config_Dialog *cfd);
static void  _free_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static int   _apply_cfdata(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static Evas_Object *_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
static void _ilist_fill(E_Config_Dialog_Data *cfdata);
static void _ilist_cb_selected(void *data);
static void _cb_add(void *data, void *data2);
static void _cb_delete(void *data, void *data2);
static void _cb_reset(void *data, void *data2);
static void _cb_dialog_yes(void *data);
static void _cb_dialog_destroy(void *data);

E_Dialog *_dia_new_profile(E_Config_Dialog_Data *cfdata);
static void _new_profile_cb_close(void *data, E_Dialog *dia);
static void _new_profile_cb_ok(void *data, E_Dialog *dia);
static void _new_profile_cb_dia_del(void *obj);


struct _E_Config_Dialog_Data
{
   E_Config_Dialog *cfd;
   Evas_Object *o_list;
   Evas_Object *o_delete;
   Evas_Object *o_reset;
   Evas_Object *o_text;
   Evas_Object *o_textlabel;
   const char *sel_profile;

   E_Dialog *dia_new_profile;
   char *new_profile;
};

typedef struct _Del_Profile_Confirm_Data Del_Profile_Confirm_Data;
struct _Del_Profile_Confirm_Data
{
   E_Config_Dialog_Data *cfdata;
};

E_Config_Dialog *
e_int_config_profiles(E_Container *con, const char *params __UNUSED__)
{
   E_Config_Dialog *cfd;
   E_Config_Dialog_View *v;

   if (e_config_dialog_find("E", "settings/profiles")) return NULL;
   v = E_NEW(E_Config_Dialog_View, 1);
   if (!v) return NULL;
   v->create_cfdata = _create_data;
   v->free_cfdata = _free_data;
   v->basic.apply_cfdata = _apply_cfdata;
   v->basic.create_widgets = _create_widgets;

   cfd = e_config_dialog_new(con,
			     _("Profile Selector"),
			     "E", "settings/profiles",
			     "preferences-profiles", 0, v, NULL);
   e_config_dialog_changed_auto_set(cfd, 0);
   return cfd;
}

static void *
_create_data(E_Config_Dialog *cfd)
{
   E_Config_Dialog_Data *cfdata;

   cfdata = E_NEW(E_Config_Dialog_Data, 1);
   cfdata->cfd = cfd;
   return cfdata;
}

static void
_free_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
{
   E_FREE(cfdata);
}

static int
_apply_cfdata(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
{
   const char *cur_profile;
   E_Action *a;

   cur_profile = e_config_profile_get();
   if (strcmp (cur_profile, cfdata->sel_profile) == 0)
     return 1;

   e_config_save_flush();
   e_config_profile_set(cfdata->sel_profile);
   e_config_profile_save();
   e_config_save_block_set(1);

   a = e_action_find("restart");
   if ((a) && (a->func.go)) a->func.go(NULL, NULL);
   return 1;
}

static Evas_Object *
_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata)
{
   Evas_Object *o, *of, *ot, *ob;
   Evas_Coord mw, mh;
   char buf[PATH_MAX];

   o = e_widget_list_add(evas, 0, 0);

   of = e_widget_framelist_add(evas, _("Available Profiles"), 0);
   cfdata->o_list = e_widget_ilist_add(evas, 24, 24, &(cfdata->sel_profile));
   e_widget_size_min_set(cfdata->o_list, 140 * e_scale, 100 * e_scale);
   e_widget_framelist_object_append(of, cfdata->o_list);

   cfdata->o_textlabel = e_widget_label_add(evas, "");
   e_widget_size_min_get(cfdata->o_textlabel, &mw, &mh);
   e_widget_framelist_object_append_full(of, cfdata->o_textlabel,
					 1, 1, /* fill */
					 1, 0, /* expand */
					 0.5, 0.5, /* align */
					 mw, mh, /* min */
					 99999, 99999 /* max */
					 );

   ob = e_widget_textblock_add(evas);
   e_widget_textblock_markup_set(ob, _("Select a profile"));
   cfdata->o_text = ob;
   e_widget_framelist_object_append_full(of, cfdata->o_text,
					 1, 1, /* fill */
					 1, 0, /* expand */
					 0.5, 0.5, /* align */
					 140 * e_scale, 80 * e_scale, /* min */
					 99999, 99999 /* max */
					 );

   ot = e_widget_table_add(evas, 0);
   ob = e_widget_button_add(evas, _("Add"), "list-add", _cb_add, cfdata, NULL);
   e_widget_table_object_append(ot, ob, 0, 0, 1, 1, 1, 1, 0, 0);
   cfdata->o_delete = e_widget_button_add(evas, _("Delete"), "list-remove", _cb_delete, cfdata, NULL);
   e_widget_table_object_append(ot, cfdata->o_delete, 1, 0, 1, 1, 1, 1, 0, 0);
   cfdata->o_reset = e_widget_button_add(evas, _("Reset"), "system-restart", _cb_reset, cfdata, NULL);
   e_widget_table_object_align_append(ot, cfdata->o_reset, 2, 0, 1, 1, 0, 1, 1, 1, 1.0, 0.5);

   e_widget_list_object_append(o, of, 1, 1, 0.5);

   // if there is a system version of the profile - allow reset
   e_prefix_data_snprintf(buf, sizeof(buf), "data/config/%s/", e_config_profile_get());
   if (ecore_file_is_dir(buf))
     e_widget_disabled_set(cfdata->o_reset, 0);
   else
     e_widget_disabled_set(cfdata->o_reset, 1);
   
   e_widget_disabled_set(cfdata->o_delete, 1);

   e_widget_list_object_append(o, ot, 1, 0, 0.0);

   _ilist_fill(cfdata);

   e_dialog_resizable_set(cfd->dia, 1);
   return o;
}

static void
_ilist_fill(E_Config_Dialog_Data *cfdata)
{
   Evas *evas;
   Eina_List *l, *profiles;
   int selected = -1, i;
   
   if (!cfdata) return;
   if (!cfdata->o_list) return;

   evas = evas_object_evas_get(cfdata->o_list);
   evas_event_freeze(evas);
   edje_freeze();
   e_widget_ilist_freeze(cfdata->o_list);

   e_widget_ilist_clear(cfdata->o_list);
   e_widget_ilist_go(cfdata->o_list);

   profiles = e_config_profile_list();
   for (i = 0, l = profiles; l; l = l->next, i++)
     {
        Efreet_Desktop *desk = NULL;
	Evas_Object *ic;
        char buf[PATH_MAX], *prof, *pdir;
        const char *label;
        
        prof = l->data;
        if (e_config_profile_get())
          {
             if (!strcmp(prof, e_config_profile_get())) selected = i;
          }
        pdir = e_config_profile_dir_get(prof);
        snprintf(buf, sizeof(buf), "%s/profile.desktop", pdir);
        desk = efreet_desktop_get(buf);
        if (!desk)
          {
             e_prefix_data_snprintf(buf, sizeof(buf), "data/config/%s/", prof);
             pdir = strdup(buf);
             if (pdir)
               {
                  snprintf(buf, sizeof(buf), "%s/profile.desktop", pdir);
                  desk = efreet_desktop_get(buf);
               }
          }
        label = prof;
        if ((desk) && (desk->name)) label = desk->name;
        buf[0] = 0;
        if (pdir)
          snprintf(buf, sizeof(buf), "%s/icon.edj", pdir);
        if ((desk) && (desk->icon) && (pdir))
          snprintf(buf, sizeof(buf), "%s/%s", pdir, desk->icon);
        else
          e_prefix_data_concat_static(buf, "data/images/enlightenment.png");
        ic = e_util_icon_add(buf, evas);
        e_widget_ilist_append(cfdata->o_list, ic, label, _ilist_cb_selected, cfdata, prof);
        if (pdir) free(pdir);
        free(prof);
        if (desk) efreet_desktop_free(desk);
     }
   if (profiles) eina_list_free(profiles);
   if (selected >= 0)
     e_widget_ilist_selected_set(cfdata->o_list, selected);   
   e_widget_size_min_set(cfdata->o_list, 155, 250);
   e_widget_ilist_go(cfdata->o_list);

   e_widget_ilist_thaw(cfdata->o_list);
   edje_thaw();
   evas_event_thaw(evas);
}

static void
_ilist_cb_selected(void *data)
{
   E_Config_Dialog_Data *cfdata;
   const char *cur_profile;
   unsigned char v;
   Efreet_Desktop *desk = NULL;
   char *pdir, buf[PATH_MAX];
   const char *name;

   cfdata = data;
   if (!cfdata) return;

   cur_profile = e_config_profile_get();

   v = (strcmp(cur_profile, cfdata->sel_profile) == 0);
   e_widget_disabled_set(cfdata->o_delete, v);
   e_config_dialog_changed_set(cfdata->cfd, !v);

   pdir = e_config_profile_dir_get(cfdata->sel_profile);
   snprintf(buf, sizeof(buf), "%s/profile.desktop", pdir);
   desk = efreet_desktop_get(buf);
   if (!desk)
     {
        e_prefix_data_snprintf(buf, sizeof(buf), "data/config/%s/", cfdata->sel_profile);
        pdir = strdup(buf);
        if (pdir)
          {
             snprintf(buf, sizeof(buf), "%s/profile.desktop", pdir);
             desk = efreet_desktop_get(buf);
          }
     }

   if ((desk) && (desk->name))
     name = desk->name;
   else
     name = cur_profile;
   snprintf(buf, sizeof(buf), _("Selected profile: %s"), name);
   e_widget_label_text_set(cfdata->o_textlabel, buf);

   if (desk)
     e_widget_textblock_markup_set(cfdata->o_text, desk->comment);
   else
{'fraction_non_alphanumeric': 0.13150153345434062, 'fraction_numerical': 0.012705764536521249, 'mean_word_length': 3.678770131771596, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4663201', 'n_tokens_mistral': 6529, 'n_tokens_neox': 6168, 'n_words': 1173}