void aui_cfg_save()
{
	char o[256];
	snprintf(o, 256, "%i", auic()->tooltext);
	aarray_set(aui_cfg_array, "tooltext", o);

	snprintf(o, 256, "%i", auic()->showhidden);
	aarray_set(aui_cfg_array, "showhidden", o);

	snprintf(o, 256, "%i", auic()->fontsize);
	aarray_set(aui_cfg_array, "fontsize", o);

	snprintf(o, 256, "%i", auic()->automount);
	aarray_set(aui_cfg_array, "automount", o);

	snprintf(o, 256, "%i", acfg()->fadeframes);
	aarray_set(aui_cfg_array, "fadeframes", o);

	aarray_set(aui_cfg_array, "fontfamily", auicv.fontfamily);
	aarray_set(aui_cfg_array, "iconset", auicv.iconset);
	aarray_set(aui_cfg_array, "language", auicv.language);
	aarray_set(aui_cfg_array, "theme", auicv.theme);

	//aui_cfg_savecalib();
	aui_cfg_savechange();
}
void aui_cfg_fromarray()
{
	char *o;

	o = aarray_get(aui_cfg_array, "automount");
	if (o != NULL)
		auic()->automount = atoi(o);

	o = aarray_get(aui_cfg_array, "tooltext");
	if (o != NULL)
		auic()->tooltext = atoi(o);

	o = aarray_get(aui_cfg_array, "fontsize");
	if (o != NULL)
		auic()->fontsize = atoi(o);

	o = aarray_get(aui_cfg_array, "fadeframes");
	if (o != NULL)
		acfg()->fadeframes = atoi(o);

	o = aarray_get(aui_cfg_array, "showhidden");
	if (o != NULL)
		auic()->showhidden = atoi(o);

	o = aarray_get(aui_cfg_array, "fontfamily");
	if (o != NULL)
		snprintf(auicv.fontfamily, 128, o);

	o = aarray_get(aui_cfg_array, "iconset");
	if (o != NULL)
		snprintf(auicv.iconset, 128, o);

	o = aarray_get(aui_cfg_array, "language");
	if (o != NULL)
		snprintf(auicv.language, 128, o);

	o = aarray_get(aui_cfg_array, "theme");
	if (o != NULL)
		snprintf(auicv.theme, 128, o);
}
//* 
//* GET TOOL TITLE
//*
char *aui_tbtitle(int id)
{
	if (!auic()->tooltext)
		return NULL;

	char *titlekey[11] = {
		"tools.newfolder",	// 0
		"tools.favorite",	// 1
		"tools.selectall",	// 2
		"tools.unselect",	// 3
		"tools.copy",	// 4
		"tools.cut",	// 5
		"tools.paste",	// 6
		"tools.menu",	// 7
		"cancel",	// 8
		"done",		// 9
		"settings.reset"	// 10
	};
	return alang_get(titlekey[id]);
}
int aui_cfg_btnFH()
{
	if (!auic()->tooltext)
		return 0;
	return ag_fontheight(0);
}
//*
//* SHOW MENU
//*
byte auia_menu(AUI_VARSP v) {
  byte common_cmd = 0;
  byte cstate = *v->state;
  
  if ((cstate == 1) || (cstate == 2)) {
    AWMENUITEM mi[5];
    int cp = 0;
    
    if (cstate == 1) {
      aw_menuset(mi, cp++, "tools.unselect", 19);
    }
    
    aw_menuset(mi, cp++, "tools.delete", 4);
    aw_menuset(mi, cp++, "settings", 17);
    aw_menuset(mi, cp++, "about", 9);
    aw_menuset(mi, cp++, "exit", 0);
    byte ret = aw_menu(v->hWin, NULL, mi, cp);
    
    if ((cstate == 2) && (ret > 0)) {
      ret++;
    }
    
    if (ret == 5) {
      common_cmd = 1;
    }
    else if (ret == 3) {
      common_cmd = 2;
    }
    else if (ret == 4) {
      common_cmd = 3;
    }
    else if (ret == 2) {
      //-- Delete
      auia_setclipboard(v, 3);
      return auia_do_delete(v);
    }
    else if (ret == 1) {
      afbox_setcheckall(v->hFile, 0);
    }
  }
  else {
    int cp = 0;
    AWMENUITEM mi[7];
    
    if (cstate == 3) {
      aw_menuset(mi, cp++, "tools.newfolder", 11);
    }
    
    byte isonfav = auifav_isfav(v->path);
    
    if (isonfav) {
      aw_menuset(mi, cp++, "tools.favorite.del", 8);
    }
    else {
      aw_menuset(mi, cp++, "tools.favorite.add", 7);
    }
    
    aw_menuset(mi, cp++,
               (auic()->showhidden ? "tools.hiddenfile" :
                "tools.hiddenfile.show"), 18);
    aw_menuset(mi, cp++, "tools.terminal", 40);
    aw_menuset(mi, cp++, "settings", 17);
    aw_menuset(mi, cp++, "about", 9);
    aw_menuset(mi, cp++, "exit", 0);
    byte ret = aw_menu(v->hWin, NULL, mi, cp);
    
    if ((cstate != 3) && (ret > 0)) {
      ret++;
    }
    
    if (ret == 7) { /* EXIT */
      common_cmd = 1;
    }
    else if (ret == 6) { /* ABOUT */
      common_cmd = 3;
    }
    else if (ret == 4) { /* TERMINAL */
      common_cmd = 4;
    }
    else if (ret == 5) { /* SETTING */
      common_cmd = 2;
    }
    else if (ret == 3) {
      auic()->showhidden = (auic()->showhidden ? 0 : 1);
      aui_cfg_save();
      v->reshow = 1;
      return 0;
    }
    else if (ret == 2) {
      //-- Add Remove Favorite
      if (isonfav) {
        auifav_del(v->path);
      }
      else {
        auifav_add(v->path);
      }
    }
    else if (ret == 1) {
      //-- Show New Folder
      return auia_newfolder(v);
    }
  }
  
  //-- Common command
  if (common_cmd == 1) {  //-- Exit
    AWMENUITEM mi[2];
    aw_menuset(mi, 0, "yes", 33);
    aw_menuset(mi, 1, "no", 0);
    byte ovr = aw_multiconfirm_ex(v->hWin, alang_get("exit"),
                                  alang_get("exit.message"),
                                  aui_icons(39), mi, 2,
                                  1);
                                  
    if (ovr == 0) {
      return 0;
    }
  }
  else if (common_cmd == 2) { //-- Settings
    v->reshow = 2;
    return 0;
  }
  else if (common_cmd == 3) { //-- About
    auido_about_dialog(v->hWin);
  }
  else if (common_cmd == 4) { //-- Terminal
    v->reshow = 6;
    return 0;
  }
  
  return 1;
}
//*
//* SHOW MAIN DIRECTORY LIST UI
//*
void aui_show_setting() {
  printf("SHOW SETTINGS\n");
  //-- REDRAW BG
  aui_setbg(NULL);
  //-- Main Variable
  AUI_VARS v;
  //-- Return Value
  v.reshow = 0;
  //-- Init Drawing Data
  v.selfile = "";
  v.path = "";
  v.state = 0;
  v.pad = agdp() * 2;
  v.btnH = agdp() * 20;
  v.boxH = agh() - (aui_minY + (v.btnH) + (v.pad * 2));
  v.boxY = aui_minY;
  v.btnY = v.boxY + v.boxH + v.pad;
  v.navW = agw() - (v.pad * 2);
  v.pthW = v.navW;
  v.navY = aui_minY + v.pad;
  v.btnW = floor(v.navW / 3);
  int btnHlv = v.navW - (v.pad / 2);
  v.hFileType = 0;
  v.btnFH = aui_cfg_btnFH();
  //-- Draw Navigation Bar
  // aui_drawtopbar(&aui_win_bg, 0, aui_minY, agw(), v.btnH + (v.pad * 2));
  aui_drawnav(&aui_win_bg, 0, v.btnY - (v.pad + v.btnFH), agw(),
              v.btnH + (v.pad * 2) + v.btnFH);
  //-- Create Main Window
  v.hWin = aw(&aui_win_bg);
  //****************************[ INIT WINDOW CONTROLS ]****************************/
  // imgbtn(v.hWin, v.pad, v.navY, btnHlv, v.btnH, &UI_ICONS[20], alang_get("settings.mount"), 3, 22);
  /*imgbtn(v.hWin, v.pad, v.navY, btnHlv, v.btnH, &UI_ICONS[17],
         alang_get("settings.calib"), 3, 21);
  */
  v.hFile = acopt(v.hWin, 0, v.boxY, agw(), (v.boxH - v.btnFH));
  //-- Toolbar style : 1
  acopt_addgroup(v.hFile, alang_get("settings.toolbar"), "");
  acopt_add(v.hFile, alang_get("settings.toolbar.icontext"), "",
            auic()->tooltext ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.toolbar.icononly"), "",
            auic()->tooltext ? 0 : 1);
  //-- Font size : 2
  acopt_addgroup(v.hFile, alang_get("settings.fontsize"), "");
  acopt_add(v.hFile, alang_get("settings.fontsize.small"), "",
            (auic()->fontsize == 1) ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.fontsize.medium"), "",
            (auic()->fontsize == 2) ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.fontsize.big"), "",
            (auic()->fontsize == 3) ? 1 : 0);
  //-- Font Family : 3
  acopt_addgroup(v.hFile, alang_get("settings.fontfamily"), "");
  
  if (1) {
    AZREADDIRP rz_fonts = az_readdir(AROMA_DIR "/fonts/");
    
    if (rz_fonts != NULL) {
      int i = 0;
      
      for (i = 0; i < rz_fonts->n; i++) {
        if (rz_fonts->t[i] == 1) {
          char desc[256];
          auis_readinfo(desc, AROMA_DIR "/fonts/",
                        rz_fonts->f[i]);
          acopt_add(v.hFile, rz_fonts->f[i], desc,
                    (strcmp
                     (auic()->fontfamily,
                      rz_fonts->f[i]) ==
                     0) ? 1 : 0);
        }
      }
      
      az_readdir_free(rz_fonts);
    }
  }
  
  //-- Icon set : 4
  acopt_addgroup(v.hFile, alang_get("settings.iconset"), "");
  
  if (1) {
    AZREADDIRP rz_icons = az_readdir(AROMA_DIR "/icons/");
    
    if (rz_icons != NULL) {
      int i = 0;
      
      for (i = 0; i < rz_icons->n; i++) {
        if (rz_icons->t[i] == 1) {
          char desc[256];
          auis_readinfo(desc, AROMA_DIR "/icons/",
                        rz_icons->f[i]);
          acopt_add(v.hFile, rz_icons->f[i], desc,
                    (strcmp
                     (auic()->iconset,
                      rz_icons->f[i]) ==
                     0) ? 1 : 0);
        }
      }
      
      az_readdir_free(rz_icons);
    }
  }
  
  //-- Theme : 5
  acopt_addgroup(v.hFile, alang_get("settings.themes"), "");
  acopt_add(v.hFile, "Generic", "Unskinned AROMA File Manager",
            (strcmp(auic()->theme, "") == 0) ? 1 : 0);
            
  if (1) {
    AZREADDIRP rz_theme = az_readdir(AROMA_DIR "/themes/");
    
    if (rz_theme != NULL) {
      int i = 0;
      
      for (i = 0; i < rz_theme->n; i++) {
        if (rz_theme->t[i] == 1) {
          char desc[256];
          auis_readinfo(desc,
                        AROMA_DIR "/themes/",
                        rz_theme->f[i]);
          acopt_add(v.hFile, rz_theme->f[i], desc,
                    (strcmp
                     (auic()->theme,
                      rz_theme->f[i]) ==
                     0) ? 1 : 0);
        }
      }
      
      az_readdir_free(rz_theme);
    }
  }
  
  //-- Languages : 6
  acopt_addgroup(v.hFile, alang_get("settings.lang"), "");
  
  if (1) {
    AZREADDIRP rz_langs = az_readdir(AROMA_DIR "/langs/");
    
    if (rz_langs != NULL) {
      int i = 0;
      
      for (i = 0; i < rz_langs->n; i++) {
        if (rz_langs->t[i] == 1) {
          char desc[256];
          auis_readinfo(desc, AROMA_DIR "/langs/",
                        rz_langs->f[i]);
          acopt_add(v.hFile, rz_langs->f[i], desc,
                    (strcmp
                     (auic()->language,
                      rz_langs->f[i]) ==
                     0) ? 1 : 0);
        }
      }
      
      az_readdir_free(rz_langs);
    }
  }
  
  //-- Font size : 7
  acopt_addgroup(v.hFile, alang_get("settings.trans"), "");
  acopt_add(v.hFile, alang_get("settings.trans.0"), "",
            (acfg()->fadeframes == 0) ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.trans.1"), "",
            (acfg()->fadeframes == 2) ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.trans.2"), "",
            (acfg()->fadeframes == 4) ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.trans.3"), "",
            (acfg()->fadeframes == 6) ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.trans.4"), "",
            (acfg()->fadeframes == 8) ? 1 : 0);
  //-- Automount : 8
  acopt_addgroup(v.hFile, alang_get("settings.mount"), "");
  acopt_add(v.hFile, alang_get("settings.mount.all"), "",
            auic()->automount ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.mount.none"), "",
            auic()->automount ? 0 : 1);
            
   
  //-- colorspace : 9
  acopt_addgroup(v.hFile, alang_get("settings.colorspace"), "");
  acopt_add(v.hFile, alang_get("settings.colorspace.rgba"), "",
            (auic()->colorspace == 1) ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.colorspace.abgr"), "",
            (auic()->colorspace == 2) ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.colorspace.argb"), "",
            (auic()->colorspace == 3) ? 1 : 0);
  acopt_add(v.hFile, alang_get("settings.colorspace.bgra"), "",
            (auic()->colorspace == 4) ? 1 : 0);           
            
            
  //-- Font size : 8
  /*
     acopt_addgroup(v.hFile,"Automount Partitions","");
     acopt_add(v.hFile,"All partitions","",0);
     acopt_add(v.hFile,"None","",1);
   */
  //-- TOOLS
  v.b1 = imgbtn(v.hWin, v.pad, v.btnY - v.btnFH, v.btnW, v.btnH + v.btnFH, &UI_ICONS[33], aui_tbtitle(9), 1, 12);	//-- Done
  v.b2 = imgbtn(v.hWin, v.pad + v.btnW, v.btnY - v.btnFH, v.btnW, v.btnH + v.btnFH, &UI_ICONS[14], aui_tbtitle(10), 1, 22);	//-- Reset
  v.b3 = imgbtn(v.hWin, v.pad + v.btnW * 2, v.btnY - v.btnFH, v.btnW, v.btnH + v.btnFH, &UI_ICONS[0], aui_tbtitle(8), 1, 20);	//-- Cancel
  //-- Show Window
  aw_show_ex(v.hWin, 2, 0, v.hFile);
  // aw_setfocus(v.hWin,v.hFile);
  aui_changetitle(&v, alang_get("settings"));
  byte ondispatch = 1;
  byte saveconfig = 1;
  byte save_to_file = 0;
  
  do {
    dword msg = aw_dispatch(v.hWin);
    
    switch (aw_gm(msg)) {
      case 12: {
          //-- DONE
          ondispatch = 0;
        }
        break;
        
      case 20: {
          //-- CANCEL
          ondispatch = 0;
          saveconfig = 0;
        }
        break;
        
      case 22: {
          //-- RESET
          if (aw_confirm(v.hWin,
                         alang_get("settings.reset"),
                         alang_get("settings.reset.msg"),
                         aui_icons(39), NULL, NULL)) {
            ondispatch = 0;
            saveconfig = 0;
            aui_cfg_init();
            aui_load_icons();
            aui_cfg_reloadfonts();
            aui_langreload();
            aui_themereload();
            save_to_file = 1;
          }
        }
        break;
        
      case 21: {
          //-- CALIB
          if (aw_calibtools(NULL)) {
            aui_cfg_save();
          }
        }
        break;
    }
  }
  while (ondispatch);
  
  if (saveconfig) {
    byte font_reloaded = 0;
    byte colorspace_refreshed = 0;
    //-- Text on toolbar
    auic()->tooltext = (acopt_getvalue(v.hFile, 1) == 1) ? 1 : 0;
    auic()->automount = (acopt_getvalue(v.hFile, 8) == 1) ? 1 : 0;
    //-- Font Size
    byte newfontsz = (byte) acopt_getvalue(v.hFile, 2);
    
    if (newfontsz != auic()->fontsize) {
      auic()->fontsize = newfontsz;
      font_reloaded = 1;
    }
    
    byte newcolorspace = (byte) acopt_getvalue(v.hFile, 9);
    
    if (newcolorspace != auic()->colorspace) {
      auic()->colorspace = newcolorspace;
      colorspace_refreshed = 1;
    }
    
    //-- Font Family
    char * fontsel = acopt_getseltitle(v.hFile, 3);
    
    if ((fontsel != NULL)
        && (strcmp(fontsel, auic()->fontfamily) != 0)) {
      snprintf(auic()->fontfamily, 256, "%s", fontsel);
      font_reloaded = 1;
    }
    
    //-- Iconset
    fontsel = acopt_getseltitle(v.hFile, 4);
    
    if ((fontsel != NULL)
        && (strcmp(fontsel, auic()->iconset) != 0)) {
      snprintf(auic()->iconset, 256, "%s", fontsel);
      aui_unload_icons();
      aui_load_icons();
    }
    
    //-- Theme
    fontsel = acopt_getseltitle(v.hFile, 5);
    
    if ((fontsel != NULL) && (strcmp(fontsel, auic()->theme) != 0)) {
      int newtheme = acopt_getvalue(v.hFile, 5);
      
      if (newtheme == 1) {
        if (strcmp(auic()->theme, "") != 0) {
          snprintf(auic()->theme, 256, "");
          aui_themereload();
        }
      }
      else {
        snprintf(auic()->theme, 256, "%s", fontsel);
        aui_themereload();
      }
    }
    
    //-- Language
    fontsel = acopt_getseltitle(v.hFile, 6);
    
    if ((fontsel != NULL)
        && (strcmp(fontsel, auic()->language) != 0)) {
      snprintf(auic()->language, 256, "%s", fontsel);
      aui_langreload();
    }
    
    //-- Transition
    int transition = acopt_getvalue(v.hFile, 7);
    acfg()->fadeframes = (transition - 1) * 2;
    
    //-- Reload Font
    if (font_reloaded) {
      aui_cfg_reloadfonts();
    }
    
    // set new color space
    if (colorspace_refreshed) {
      aui_cfg_setcolorspace();
    }
    
    save_to_file = 1;
  }
  
  //-- Window
  aw_destroy(v.hWin);
  
  //-- Save
  if (save_to_file) {
    aui_cfg_save();
  }
}
int aui_fetch(char *path, ACONTROLP FB, char *selfile)
{
	struct dirent **files;
	aui_dir_active_path = path;
	int n = scandir(path, &files, 0, *aui_fsort);

	if (n > 0) {
		while (n--) {
			char *dname = files[n]->d_name;
			if (!strcmp(dname, ".") || !strcmp(dname, "..")) {
				free(files[n]);
				continue;
			}

			if ((dname[0] == '.') && (auic()->showhidden == 0)) {
				free(files[n]);
				continue;
			}
			//-- GET TYPE
			byte dtype = aui_dtcheck(dname, files[n]->d_type, 20);

			char *full_path = NULL;
			aui_setpath(&full_path, aui_dir_active_path, dname, 0);

			if (full_path) {
				if (auic()->showhidden == 0) {
					if (aui_ishidden_file(full_path)) {
						free(full_path);
						free(files[n]);
						continue;
					}
				}

				byte selectedDefault =
				    strcmp(selfile, dname) ? 0 : 1;
				//-- DIR
				if (dtype == 4) {
					int cnt = aui_getdircount(full_path);
					char desc[256];
					char perm[10];
					aui_fileperm(perm, full_path);

					char formats[256];
					char formats2[256];
					snprintf(formats, 256,
						 "<#selectbg_g>%i</#>", cnt);
					snprintf(formats2, 256,
						 alang_get((cnt >
							    1) ?
							   "dir.filecounts" :
							   "dir.filecount"),
						 formats);
					snprintf(desc, 256, "<@right>%s</@>",
						 formats2);
					afbox_add(FB, dname, desc, 0,
						  &UI_ICONS[22], dtype, perm, 0,
						  selectedDefault);
				}
				//-- FILE
				else if (dtype == 8) {
					char desc[256];
					char dsz[256];
					char perm[10];
					aui_fileperm(perm, full_path);
					aui_filesize(dsz, full_path);
					snprintf(desc, 256, "<@right>%s</@>",
						 dsz);
					afbox_add(FB, dname, desc, 0,
						  &UI_ICONS[21], dtype, perm, 0,
						  selectedDefault);
				}
				//-- LINK FILE / DIR
				else if ((dtype == 28) || (dtype == 24)) {
					char *desc = NULL;
					char buf[4096];
					int len =
					    readlink(full_path, buf,
						     sizeof(buf));
					if (len >= 0) {
						buf[len] = 0;
						desc =
						    aui_strip(buf,
							      agw() -
							      (agdp() * 46), 0);
					}
					afbox_add(FB, dname,
						  ((desc ==
						    NULL) ?
						   alang_get("symlink.error") :
						   desc), 0,
						  &UI_ICONS[(dtype ==
							     24) ? 25 : 24],
						  dtype, "", 0,
						  selectedDefault);
					if (desc != NULL)
						free(desc);
				}
				//-- ERROR LINK
				else if (dtype == 10) {
					afbox_add(FB, dname,
						  alang_get("symlink.error"), 0,
						  &UI_ICONS[23], dtype, "", 0,
						  selectedDefault);
				}

				free(full_path);
			}
			free(files[n]);
		}
		free(files);
	}
	return 1;
}