/*
 * * * Compile_AHK SETTINGS BEGIN * * *

[AHK2EXE]
Exe_File=%In_Dir%\..\FramaPrefs.exe
[VERSION]
Set_Version_Info=1
Company_Name=Framakey
File_Description=Editeur de préférences pour Framakey2
File_Version=0.1.0.29
Inc_File_Version=1
Internal_Name=FramaPrefs
Legal_Copyright=(c) 2011 Framakey.org
Original_Filename=FramaPrefs.exe
Product_Name=FramaPrefs
Product_Version=2.0.0.0
Language_ID=56
[ICONS]
Icon_1=%In_Dir%\FramaPrefs.ico
Icon_2=0
Icon_3=0
Icon_4=0
Icon_5=0
Icon_6=0
Icon_7=0

* * * Compile_AHK SETTINGS END * * *
*/

; ----------------------------------------------------
; FramaPrefs
; ----------------------------------------------------
; Par fat115
; License : GPL
; Ce script produit un exécutable gérant les diverses
; préférences utilisées par la Framakey2.
; Compiler: AutoHotkey_L 1.0.92.2+ (http://www.autohotkey.com/).
; $id=FramaPrefs.ahk $date=2011-08-24
; ----------------------------------------------------
;Copyright (C) 2005-2011 Framakey

;Website: http://www.framakey.org

;This software is OSI Certified Open Source Software.
;OSI Certified is a certification mark of the Open Source Initiative.

;This program is free software; you can redistribute it and/or
;modify it under the terms of the GNU General Public License
;as published by the Free Software Foundation; either version 2
;of the License, or (at your option) any later version.

;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
;GNU General Public License for more details.

;You should have received a copy of the GNU General Public License
;along with this program; if not, write to the Free Software
;Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA	02110-1301, USA.
; ----------------------------------------------------

#NoEnv
#Include %A_ScriptDir%\AhkIncludes
#Include Functions.ahk
#Include ini.ahk
#Include ConvBoolNum.ahk
#Include CalcRelPath.ahk
FileInstall, FramaKiosk.png, FramaKiosk.png
FileInstall, Framasuite.png, Framasuite.png
SetWorkingDir, %A_ScriptDir%
FileEncoding, UTF-8
#SingleInstance IGNORE
#NoTrayIcon
; Initialisation
a_ListChemins := Object()
a_ListChemins.Insert("p_FramaStarterExe", "file")
a_ListChemins.Insert("p_AppsDir", "folder")
a_ListChemins.Insert("p_FKUtils", "folder")
a_ListChemins.Insert("p_FKioskExe", "file")
a_ListChemins.Insert("p_KioskPages", "folder")
a_ListChemins.Insert("p_AsuiteExe", "file")
a_ListChemins.Insert("p_AsuiteDir", "folder")
a_ListChemins.Insert("p_CafeExe", "file")
a_ListChemins.Insert("p_SynAppsExe", "file")
a_ListChemins.Insert("p_FKTuneExe", "file")

Gosub ExtractInfos

Gui, Add, Button, x6 y452 w360 h30 gGuiSave, Enregistrer && Quitter
Gui, Add, Button, x376 y452 w360 h30 gGuiClose, Annuler
Gui, Add, Tab, x6 y2 w730 h440 , Démarrage|Options|Chemins
Gui, Tab, Chemins
	{
	Gui, Add, Text, x16 y32 w240 h20 , Lanceur principal de la Framakey :
	Gui, Add, Edit, x266 y32 w310 h20 ReadOnly vp_FramaStarterExe, %p_FramaStarterExe%
	Gui, Add, Button, x586 y32 w30 h20 vbut_FramaStarterExe gSubSelectFile, ...
	Gui, Add, Text, x626 y32 w100 h20 +Center vv_FramaStarterExe, Vérification ...
	Gui, Add, Text, x16 y62 w240 h20 , Dossier des applications portables :
	Gui, Add, Edit, x266 y62 w310 h20 ReadOnly vp_AppsDir, %p_AppsDir%
	Gui, Add, Button, x586 y62 w30 h20 vbut_AppsDir gSubSelectFolder, ...
	Gui, Add, Text, x626 y62 w100 h20 +Center vv_AppsDir, Vérification ...
	Gui, Add, Text, x16 y92 w240 h20 , Dossier des utilitaires Framakey :
	Gui, Add, Edit, x266 y92 w310 h20 ReadOnly vp_FKUtils, %p_FKUtils%
	Gui, Add, Button, x586 y92 w30 h20 vbut_FKUtils gSubSelectFolder, ...
	Gui, Add, Text, x626 y92 w100 h20 +Center vv_FKUtils, Vérification ...
	Gui, Add, Text, x16 y122 w240 h20 , Lanceur portable du FramaKiosk :
	Gui, Add, Edit, x266 y122 w310 h20 ReadOnly vp_FKioskExe, %p_FKioskExe%
	Gui, Add, Button, x586 y122 w30 h20 vbut_FKioskExe gSubSelectFile, ...
	Gui, Add, Text, x626 y122 w100 h20 +Center vv_FKioskExe, Vérification ...
	Gui, Add, Text, x16 y152 w240 h20 , Dossier des pages du Kiosk :
	Gui, Add, Edit, x266 y152 w310 h20 ReadOnly vp_KioskPages, %p_KioskPages%
	Gui, Add, Button, x586 y152 w30 h20 vbut_KioskPages gSubSelectFolder, ...
	Gui, Add, Text, x626 y152 w100 h20 +Center vv_KioskPages, Vérification ...
	Gui, Add, Text, x16 y182 w240 h20 , Lanceur FramASuite :
	Gui, Add, Edit, x266 y182 w310 h20 ReadOnly vp_AsuiteExe, %p_AsuiteExe%
	Gui, Add, Button, x586 y182 w30 h20 vbut_AsuiteExe gSubSelectFile, ...
	Gui, Add, Text, x626 y182 w100 h20 +Center vv_AsuiteExe, Vérification ...
	Gui, Add, Text, x16 y212 w240 h20 , Dossier du menu Asuite :
	Gui, Add, Edit, x266 y212 w310 h20 ReadOnly vp_AsuiteDir, %p_AsuiteDir%
	Gui, Add, Button, x586 y212 w30 h20 vbut_AsuiteDir gSubSelectFolder, ...
	Gui, Add, Text, x626 y212 w100 h20 +Center vv_AsuiteDir, Vérification ...
	Gui, Add, Text, x16 y242 w240 h20 , Lanceur portable de CAFE :
	Gui, Add, Edit, x266 y242 w310 h20 ReadOnly vp_CafeExe, %p_CafeExe%
	Gui, Add, Button, x586 y242 w30 h20 vbut_CafeExe gSubSelectFile, ...
	Gui, Add, Text, x626 y242 w100 h20 +Center vv_CafeExe, Vérification ...
	Gui, Add, Text, x16 y272 w240 h20 , Lanceur portable de SynApps :
	Gui, Add, Edit, x266 y272 w310 h20 ReadOnly vp_SynAppsExe, %p_SynAppsExe%
	Gui, Add, Button, x586 y272 w30 h20 vbut_SynAppsExe gSubSelectFile, ...
	Gui, Add, Text, x626 y272 w100 h20 +Center vv_SynAppsExe, Vérification ...
	Gui, Add, Text, x16 y302 w240 h20 , Lanceur portable de FramaKioskTune :
	Gui, Add, Edit, x266 y302 w310 h20 ReadOnly vp_FKTuneExe, %p_FKTuneExe%
	Gui, Add, Button, x586 y302 w30 h20 vbut_FKTuneExe gSubSelectFile, ...
	Gui, Add, Text, x626 y302 w100 h20 +Center vv_FKTuneExe, Vérification ...
	}
Gui, Tab, Démarrage
	{
	Gui, Add, Text, x16 y32 w440 h20 , Les applications suivantes seront automatiquement lancées au démarrage de la Framakey :
	Gui, Add, CheckBox, x46 y62 w340 h20 Checked%b_LaunchCafe% vb_LaunchCafe, CAFE (association de fichiers avec les applications portables)
	Gui, Add, GroupBox, x26 y92 w690 h330 , Interfaces graphiques
	Gui, Add, CheckBox, x36 y112 w260 h20 Checked%b_LaunchKiosk% vb_LaunchKiosk, FramaKiosk (une interface facilement accessible)
	Gui, Add, Picture, x36 y142 w323 h250 , %A_ScriptDir%\FramaKiosk.png
	Gui, Add, CheckBox, x386 y112 w300 h30 Checked%b_LaunchAsuite% vb_LaunchAsuite, FramASuite (une interface hautement configurable mais qui peut dérouter les débutants au premier abord)
	Gui, Add, Picture, x546 y152 w153 h250 , %A_ScriptDir%\Framasuite.png
	}
Gui, Tab, Options
	{
	Gui, Add, CheckBox, x16 y32 w250 h20 Checked%b_AsuiteDemo% vb_AsuiteDemo, Lancer la démonstration au démarrage d'ASuite
	Gui, Add, CheckBox, x16 y72 w240 h30 Checked%b_SynAppsAvert% vb_SynAppsAvert, Afficher l'avertissement pour la fermeture de SynApps lors des mises à jour des menus
	Gui, Add, CheckBox, x16 y122 w240 h30 Checked%b_AskUBUpdate% vb_AskUBUpdate, Demander confirmation à l'utilisateur avant la mise à jour des menus
	Gui, Add, CheckBox, x16 y172 w260 h70 Checked%b_IsMainProject% vb_IsMainProject, Activer le rangement automatiques des nouvelles applications dans le Kiosk et Asuite`nImportant : peut provoquer des pertes de données si vous n'avez pas une Framakey "standard" !!!
	Gui, Add, CheckBox, x366 y32 w360 h20 Checked%b_SplashStarter% vb_SplashStarter, Désactivation du splashscreen au lancement de la Framakey
	Gui, Add, Text, x366 y62 w250 h20 , Personnaliser le splashscreen de démarrage :
	Gui, Add, Picture, x536 y92 w190 h-1 vpic_Splash, %p_FramaSplash%
	Gui, Add, Button, x386 y122 w130 h50 gChangeSplash, Choisir une image
	Gui, Add, CheckBox, x366 y252 w270 h40 Checked%b_TTipStarter% vb_TTipStarter, Désactivation des informations de la barre des tâches lors du démarrage de la Framakey
	Gui, Add, CheckBox, x366 y302 w280 h20 Checked%b_CustomTTip% vb_CustomTTip, Personnaliser le message d'info de la barre des tâches
	Gui, Add, Edit, x386 y332 w270 h60 vs_CustomTTip, %s_CustomTTip%
	}
; Generated using SmartGUI Creator 4.0
Gui, Show, Center h500 w747, Préférences
Gosub VerifPath
Return

GuiClose:
ExitApp

ExtractInfos:
{
	ini_load(s_MainIni, "Framakey2.ini", "UTF-8")
	p_LaunchIni := ini_getValue(s_MainIni, "Main", "RelMainLauncherName") . ".ini"
	IfNotExist, %p_LaunchIni%
		{
		FileSelectFile, p_LaunchIni, 1, ..\, Veuillez sélectionner le fichier de configuration de Framakey :, Fichier de configuration (*.ini)
		If (ErrorLevel || !p_LaunchIni)
			ExitApp
		p_LaunchIni := CalcRelPath(A_ScriptDir, p_LaunchIni)
		}
	ini_load(s_LaunchIni, p_LaunchIni, "UTF-8")
	; extraction des chemins
	p_FramaStarterExe := StringReplace(p_LaunchIni, ".ini", ".exe")
	p_AppsDir := ini_getValue(s_MainIni, "FramaUpdate", "RelAppsDir")
	p_FKUtils := ini_getValue(s_LaunchIni, "Main", "FramaKeyDir")
	_temp := ini_getValue(s_LaunchIni, "FramaKiosk", "SubDir")
	p_FKioskExe := ".\" . ((!_temp) ? "" : _temp . "\") . ini_getValue(s_LaunchIni, "FramaKiosk", "Exe")
	p_KioskPages := ini_getValue(s_MainIni, "FramaUpdate", "RelKioskPagesDir")
	_temp := ini_getValue(s_LaunchIni, "FramaSuite", "SubDir")
	p_AsuiteExe := ".\" . ((!_temp) ? "" : _temp . "\") . ini_getValue(s_LaunchIni, "FramaSuite", "Exe")
	p_AsuiteDir := ini_getValue(s_MainIni, "Main", "RelASuiteDir")
	_temp := ini_getValue(s_LaunchIni, "Cafe", "SubDir")
	p_CafeExe := ".\" . ((!_temp) ? "" : _temp . "\") . ini_getValue(s_LaunchIni, "Cafe", "Exe")
	p_SynAppsExe := ini_getValue(s_MainIni, "FramaUpdate", "RelSynAppsDir") . "\" . ini_getValue(s_MainIni, "FramaUpdate", "SynAppsLauncher")
	_temp =
	p_FKTuneExe := ini_getValue(s_MainIni, "Main", "RelFKTuneDir") . "\FramaKioskTunePortable.exe"
	; extraction des MainApps à lancer
	b_LaunchCafe := ConvB2N(ini_getValue(s_LaunchIni, "MainApps", "LaunchCafe"))
	b_LaunchKiosk := ConvB2N(ini_getValue(s_LaunchIni, "MainApps", "LaunchFramaKiosk"))
	b_LaunchAsuite := ConvB2N(ini_getValue(s_LaunchIni, "MainApps", "LaunchFramaSuite"))
	; extraction des différentes options
	b_AsuiteDemo := ConvB2N(ini_getValue(s_MainIni, "Tutorials", "FramasuiteDemo"))
	b_SynAppsAvert := ConvB2N(ini_getValue(s_MainIni, "Tutorials", "SynAppsAvert"))
	b_AskUBUpdate := ConvB2N(ini_getValue(s_MainIni, "FramaUpdate", "AskUserBeforeUpdate"))
	b_IsMainProject := ConvB2N(ini_getValue(s_MainIni, "Main", "IsMainProject"))
	b_SplashStarter := ConvB2N(ini_getValue(s_LaunchIni, "SplashScreen", "DisableSplashScreen"))
	b_TTipStarter := ConvB2N(ini_getValue(s_LaunchIni, "SplashScreen", "DisableMainTrayTip"))
	p_FramaSplash := A_ScriptDir . "\" . ini_getValue(s_LaunchIni, "SplashScreen", "SplashScreenDir")
	s_CustomTTip := StringReplace(ini_getValue(s_LaunchIni, "SplashScreen", "MainTrayTipMessage"), "\n", "`n", "All")
	b_CustomTTip := (s_CustomTTip != "") ? 1 : 0
	
Return
}

SubSelectFile:
{
	_path := StringReplace(A_GuiControl, "but_", "p_")
	FileSelectFile, _lanceur, 1, %A_ScriptDir%, Veuillez sélectionner le lanceur :, Exécutables (*.exe)
	If ErrorLevel
		Return
	_lanceur := CalcRelPath(A_ScriptDir, _lanceur)
	GuiControl, , %_path%, %_lanceur%
	Gosub VerifPath
Return
}

SubSelectFolder:
{
	_path := StringReplace(A_GuiControl, "but_", "p_")
	If (_path = "p_FKUtils")
		SplitPath, A_ScriptDir, _lanceur
	Else
		{
		FileSelectFolder, _lanceur, *%A_ScriptDir%, 0, Veuillez sélectionner le dossier :
		If ErrorLevel
			Return
		_lanceur := CalcRelPath(A_ScriptDir, _lanceur)
		}
	GuiControl, , %_path%, %_lanceur%
	Gosub VerifPath
Return
}

VerifPath:
{
	Gui, Submit, NoHide
	For k, v in a_ListChemins
		{
		_verif := StringReplace(k, "p_", "v_")
		_content := %k%
		If (k = "p_FKUtils")
			{
			SplitPath, A_ScriptDir, , _temp
			_content := _temp . "\" . _content
			}
		_exist := FileExist(_content)
		_isfolder := InStr(_exist, "D")
		; si le chemin existe et est du bon type
		If (_exist && (((v = "file") && !_isfolder) || ((v = "folder") && _isfolder)))
			{
			GuiControl, , %_verif%, Valide
			GuiControl, +cLime, %_verif%
			}
		Else
			{
			GuiControl, , %_verif%, Invalide
			GuiControl, +cRed, %_verif%
			}
		GuiControl, MoveDraw, %_verif%
		}

Return
}

ChangeSplash:
{
	FileSelectFile, _splash, 1, %A_ScriptDir%, Sélectionnez le nouveau splashscreen :, Images JPEG (*.jpeg; *.jpg)
	If (_splash = A_ScriptDir . "\FramaSplash.jpg")
		p_FramaSplash := _splash
	Else
		{
		FileCopy, %_splash%, FramaSplashCustom.jpg, 1
		p_FramaSplash := A_ScriptDir . "\FramaSplashCustom.jpg"
		}
	GuiControl, , pic_Splash, %p_FramaSplash%
Return
}

GuiSave:
{
	Gui, Submit, NoHide
	; sauvegarde des chemins
	ini_replaceValue(s_MainIni, "Main", "RelMainLauncherName", StringReplace(p_FramaStarterExe, ".exe"))
	ini_replaceValue(s_MainIni, "FramaUpdate", "RelAppsDir", p_AppsDir)
	ini_replaceValue(s_LaunchIni, "Main", "FramaKeyDir", p_FKUtils)
	SplitPath, p_FKioskExe, _name, _dir
	ini_replaceValue(s_LaunchIni, "FramaKiosk", "SubDir", RegExReplace(_dir, "^\.[\\]?"))
	ini_replaceValue(s_LaunchIni, "FramaKiosk", "Exe", _name)
	SplitPath, p_AsuiteExe, _name, _dir
	ini_replaceValue(s_LaunchIni, "FramaSuite", "SubDir", RegExReplace(_dir, "^\.[\\]?"))
	ini_replaceValue(s_LaunchIni, "FramaSuite", "Exe", _name)
	SplitPath, p_CafeExe, _name, _dir
	ini_replaceValue(s_LaunchIni, "Cafe", "SubDir", RegExReplace(_dir, "^\.[\\]?"))
	ini_replaceValue(s_LaunchIni, "Cafe", "Exe", _name)
	SplitPath, p_SynAppsExe, _name, _dir
	ini_replaceValue(s_MainIni, "FramaUpdate", "RelSynAppsDir", _dir)
	ini_replaceValue(s_MainIni, "FramaUpdate", "SynAppsLauncher", _name)
	ini_replaceValue(s_MainIni, "FramaUpdate", "RelFKTuneDir", StringReplace(p_FKTuneExe, "\FramaKioskTunePortable.exe"))
	; sauvegarde des lancements
	ini_replaceValue(s_LaunchIni, "MainApps", "LaunchCafe", ConvN2B(b_LaunchCafe))
	ini_replaceValue(s_LaunchIni, "MainApps", "LaunchFramaKiosk", ConvN2B(b_LaunchKiosk))
	ini_replaceValue(s_LaunchIni, "MainApps", "LaunchFramaSuite", ConvN2B(b_LaunchAsuite))
	; sauvegarde des différentes options
	ini_replaceValue(s_MainIni, "Tutorials", "FramasuiteDemo", ConvN2B(b_AsuiteDemo))
	ini_replaceValue(s_MainIni, "Tutorials", "SynAppsAvert", ConvN2B(b_SynAppsAvert))
	ini_replaceValue(s_MainIni, "FramaUpdate", "AskUserBeforeUpdate", ConvN2B(b_AskUBUpdate))
	ini_replaceValue(s_MainIni, "Main", "IsMainProject", ConvN2B(b_IsMainProject))
	ini_replaceValue(s_LaunchIni, "SplashScreen", "DisableSplashScreen", ConvN2B(b_SplashStarter))
	ini_replaceValue(s_LaunchIni, "SplashScreen", "DisableMainTrayTip", ConvN2B(b_TTipStarter))
	ini_replaceValue(s_LaunchIni, "SplashScreen", "SplashScreenDir", StringReplace(p_FramaSplash, A_ScriptDir . "\"))
	s_CustomTTip := StringReplace(s_CustomTTip, "`n", "\n", "All")
	If b_CustomTTip
		{
		ini_insertKey(s_LaunchIni, "SplashScreen", "MainTrayTipMessage=" . s_CustomTTip)
		If ErrorLevel
			ini_replaceValue(s_LaunchIni, "SplashScreen", "MainTrayTipMessage", s_CustomTTip)
		}
	Else
		s_LaunchIni := RegExReplace(s_LaunchIni, "m)^MainTrayTipMessage.*")
	ini_save(s_MainIni, "Framakey2.ini", "UTF-8")
	ini_save(s_LaunchIni, p_LaunchIni, "UTF-8")
	Gosub GuiClose

Return
}