#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=send-1.ico
#AutoIt3Wrapper_Outfile=..\..\..\Desktop\moveyourpop.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.16.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <AutoItConstants.au3>
#include <GuiEdit.au3>
#include <ScrollBarsConstants.au3>
#include <StaticConstants.au3>
#include <GuiListView.au3>
#include <ButtonConstants.au3>
#include <misc.au3>
#include <ListViewConstants.au3>


_Singleton(@ScriptName)

$bypassSauvegarde = 0
Global $allServer, $profileDirectory, $dstSave, $profileSizef, $prefsToArray
$color = "0x4A90E2"
$color2 = "0x4A90E2"
$colorbk = "0x1E1E2E"

; thunderbird doit être fermé pour lancer le script
if ProcessExists("thunderbird.exe") Then
	MsgBox(16, "MoveYourPOP", "Veuillez fermer Thunderbird avant de lancer le script.")
	Exit
EndIf

;gui principal
$hGUI = GUICreate("MoveYourPOP", 800, 570)
GUISetBkColor(0xf2f2f2)

GUISetFont(10, 500, default, "Segoe UI")
GUICtrlCreateLabel("moveYourPOP© by jop - Version 1.2", 0, 553, 800, 15, $SS_CENTER)
GUICtrlSetFont(-1, 8, 500, 2, "Segoe UI")	
GUICtrlSetBkColor(-1, 0xf2f2f2)

GUICtrlCreateGroup("", 10, 5, 780, 80)
GUICtrlCreateLabel("👤 Profil Thunderbird : ", 20, 23, 200, 30)
GUICtrlSetColor(-1, $color)
GUIctrlSetFont(-1,12, 600, Default, "Segoe UI")
$labSizeProfile = GUICtrlCreateLabel("", 220, 25, 150, 30)
GUICtrlSetColor(-1, $color)
GUIctrlSetFont(-1,11, 600, Default, "Segoe UI")
;~ GUICtrlSetBkColor(-1, 0xff0000)
;~ GUICtrlCreateLabel("Domaine(s) :", 390, 25, 75, 30)
;~ GUICtrlSetColor(-1, $color)
;~ GUICtrlSetTip(-1, "Séparez les domaines par un |", "Domaines à rechercher")
;~ GUICtrlSetCursor(-1, 4)
;~ $searchDoamin = GUICtrlCreateInput("ac-nice.fr|ac-aix-marseille.fr", 470, 21, 140, 25)
;~ GUICtrlSetFont(-1, 10, 500, Default, "Segoe UI")
;~ $refresh = GUICtrlCreateLabel("🔄", 610, 20, 30, 33)
;~ GUICtrlSetFont(-1, 15, 500, Default, "Segoe UI")
;~ GUICtrlSetColor(-1, $color)
;~ GUICtrlSetCursor(-1, 0)
;~ GUICtrlSetTip(-1, "Rafraichir la liste des comptes")
$modifProFileDirectory = GUICtrlCreateButton("📂 Modifier", 660, 17, 120, 33)
GUICtrlSetBkColor(-1, $color2)
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetFont(-1, 11, 600, Default, "Segoe UI")
GUICtrlSetTip(-1, "Sélectionnez le dossier du profil Thunderbird")
$labProfile = GUICtrlCreateLabel("", 20, 52, 760, 25, $SS_SUNKEN+$SS_CENTER)
GUICtrlSetBkColor(-1, 0xffffff)
GUICtrlSetFont(-1, 11, 500, Default, "Segoe UI")
GUICtrlSetTip(-1, "Cliquez pour ouvrir le dossier")
guictrlsetcursor(-1, 0)

GUICtrlCreateGroup("", 10, 90, 780, 80)
GUICtrlCreateLabel("💾 Dossier de sauvegarde :", 20, 105, 230, 25)
GUICtrlSetColor(-1, $color)
GUIctrlSetFont(-1,12, 600, Default, "Segoe UI")
$labSizeSave = GUICtrlCreateLabel("", 250, 107, 250, 25)
GUICtrlSetColor(-1, $color)
GUIctrlSetFont(-1,11, 600, Default, "Segoe UI")

$modifSaveDirectory = GUICtrlCreateButton("📂 Modifier", 660, 101, 120, 33)
GUICtrlSetBkColor(-1, $color2)
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetFont(-1, 11, 600, Default, "Segoe UI")
GUICtrlSetTip(-1, "Sélectionnez le dossier de sauvegarde")
GUICtrlCreateContextMenu($modifSaveDirectory)
$bypassSave = GUICtrlCreateMenuItem("Bypasser la sauvegarde", $modifSaveDirectory)
$dstSave = @AppDataDir&"\Thunderbird\Profiles"
GUICtrlSetData($labSizeSave, "("&Round(DriveSpaceFree($dstSave)/1024, 0)&" Go disponible)")
$labSave = GUICtrlCreateLabel($dstSave, 20, 136, 760, 25, $SS_SUNKEN+$SS_CENTER)
GUICtrlSetBkColor(-1, 0xffffff)
GUICtrlSetFont(-1, 11, 500, Default, "Segoe UI")
GUICtrlSetTip(-1, "Cliquez pour ouvrir le dossier")
guictrlsetcursor(-1, 0)

GUICtrlCreateGroup("", 10, 175, 780, 310)
GUICtrlCreateLabel("✉️ Compte(s) POP trouvé(s) :", 20, 190, 240, 25)
GUICtrlSetColor(-1, $color)
GUIctrlSetFont(-1,12, 600, Default, "Segoe UI")

$listview = GUICtrlCreateListView("Username|Account|ID|ServerID|Dossier de stockage|smtpID|Adresse du POP", 20, 220, 760, 250)
GUICtrlSetFont(-1, 11, 500, Default, "Segoe UI")
GUICtrlSetBkColor(-1, 0xff0000)
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, -1) ; $LVSCW_AUTOSIZE
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 1, -1) ; $LVSCW_AUTOSIZE
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 2, -1) ; $LVSCW_AUTOSIZE
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 3, -1) ; $LVSCW_AUTOSIZE
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 4, -1) ; $LVSCW_AUTOSIZE
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 5, -1) ; $LVSCW_AUTOSIZE
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 6, -1) ; $LVSCW_AUTOSIZE
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 7, -1) ; $LVSCW_AUTOSIZE
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, -2) ; $LVSCW_AUTOSIZE_USEHEADER
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 1, -2) ; $LVSCW_AUTOSIZE_USEHEADER
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 2, -2) ; $LVSCW_AUTOSIZE_USEHEADER
GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 3, -2) ; $LVSCW_AUTOSIZE_USEHEADER
guictrlsendmsg($ListView, $LVM_SETCOLUMNWIDTH, 4, -2) ; $LVSCW_AUTOSIZE_USEHEADER
guictrlsendmsg($ListView, $LVM_SETCOLUMNWIDTH, 5, -2) ; $LVSCW_AUTOSIZE_USEHEADER
guictrlsendmsg($ListView, $LVM_SETCOLUMNWIDTH, 6, -2) ; $LVSCW_AUTOSIZE_USEHEADER
guictrlsendmsg($ListView, $LVM_SETCOLUMNWIDTH, 7, -2) ; $LVSCW_AUTOSIZE_USEHEADER


$go = GUICtrlCreateButton("🚀 Exécuter", 10, 490, 780, 60)
GUICtrlSetBkColor(-1, $color2)
GUICtrlSetColor(-1, 0xffffff)
GUICtrlSetFont(-1, 14, 600, Default, "Segoe UI")

$proFileDirectory = FindLatestPrefs(@AppDataDir&"\Thunderbird\Profiles")

_getAllServer($profileDirectory)

GUISetState()

While 1
	Sleep(10)
	Switch GUIGetMsg()
		Case $GUI_EVENT_CLOSE
			Exit
		Case $bypassSave
			$byppasMsg = MsgBox(52, "MoveYourPOP", "La sauvegarde sera bypassée."&@CRLF&@CRLF&"Vous savez ce que vous faites ..."&@CRLF&@CRLF&"Etes-vous sûr de vouloir continuer sans la sauvegrade ?"&@CRLF&@crlf&"[Oui] pour continuer sans la sauvegarde."&@CRLF&"[Non] ré-activera la sauvegarde.")
			if $byppasMsg = 6 Then
				$bypassSauvegarde = 1
			Else
				$bypassSauvegarde = 0
			EndIf
		Case $modifProFileDirectory
			$newProfileFolder = FileSelectFolder("Séléctionnez le dossier du profil", @AppDataDir&"\Thunderbird\Profiles")
			If Not @error Then
				$profileDirectory = $newProfileFolder
				GUICtrlSetData($labProfile, $profileDirectory)
				$dstSave = $newProfileFolder
				$dstSave = StringRegExpReplace($dstSave, "\\[^\\]+$", "")
				GUICtrlSetData($labSave, $dstSave)
				GUICtrlSetData($labSizeSave, "("&Round(DriveSpaceFree($dstSave)/1024, 0)&" Go disponible)")
				_getAllServer($profileDirectory)
			EndIf
		;~ Case $refresh
		;~ 	_getAllServer($profileDirectory)
		Case $modifSaveDirectory
			$newSaveFolder = FileSelectFolder("Séléctionnez le dossier de sauvegarde", @AppDataDir&"\Thunderbird\Profiles")
			If Not @error Then
				$dstSave = $newSaveFolder
				GUICtrlSetData($labSave, $dstSave)
				GUICtrlSetData($labSizeSave, "("&Round(DriveSpaceFree($dstSave)/1024, 0)&" Go disponible)")
			EndIf
		Case $labProfile
			ShellExecute($profileDirectory)
		case $labSave
			ShellExecute($dstSave)
		Case $go
			if $bypassSauvegarde = 0 Then
				_checkSpace($profileDirectory, $dstSave)
			EndIf
			If @error Then
				$msg = MsgBox(48,"MoveYourPOP",$dstSave&@CRLF&" n'a pas assez d'espace disponible pour la sauvegarde."&@CRLF&@CRLF&"Sélectionnez un autre lecteur.")
			Else
				if $bypassSauvegarde = 0 Then
					$backup = _backup($profileDirectory, $dstSave)
					$saveSize = Round(DirGetSize($dstSave&"\"&$backup)/1024/1024, 2)&" Mo"
					$msg = MsgBox(68, "MoveYoutPop", "Taille du dossier du profil : "&$profileSizef&@crlf&"Taille de la sauvegdarde : "&$saveSize&@CRLF&@CRLF&"Continuer ?")
					if $msg = 7 then Exit
				EndIf
				
				$msg = 6
				If $msg = 6 Then
					$userjs = $profileDirectory&"\user.js"
					FileDelete($userjs)
					$serverLocalFolder = _searchValueInPrefsJsArray($prefsToArray, "mail.accountmanager.localfoldersserver")
					$localFolderDirectory = _searchValueInPrefsJsArray($prefsToArray, "mail.server."&$serverLocalFolder&".directory-rel")
					$localFolderDirectory = StringReplace($localFolderDirectory, "[ProfD]", $profileDirectory&"\")
					$localFolderDirectory = StringReplace($localFolderDirectory, "/", "\")
					
					for $c = 0 to ubound($allServer) - 1
						;~ _ArrayDisplay($allServer[$c])
						$accountName = $allServer[$c][0]
						$identity = $allServer[$c][2]
						$servID = $allServer[$c][3]
						$directory = $allServer[$c][4]
						$smtpID = $allServer[$c][5]
						$popAdress = $allServer[$c][6]
						If StringInStr($popAdress, "ac-nice.fr") Then
							$domain = ".ac-nice.fr"
							$smtpAddress = "smtps.ac-nice.fr"
						ElseIf StringInStr($popAdress, "ac-aix-marseille.fr") Then
							$domain = ".ac-aix-marseille.fr"
							$smtpAddress = "smtp.ac-aix-marseille.fr"
						EndIf
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.identity.'&$identity&'.archive_folder", "imap://'&$accountName&'@imap'&$domain&'/Archives");'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.identity.'&$identity&'.draft_folder", "imap://'&$accountName&'@imap'&$domain&'/Drafts");'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.identity.'&$identity&'.fcc_folder", "imap://'&$accountName&'@imap'&$domain&'/Sent");'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.identity.'&$identity&'.stationery_folder", "imap://'&$accountName&'@imap'&$domain&'/Templates");'&@CRLF)

						$randomUID = Random(10000, 99999, 1)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.server.'&$servID&'.directory", "'&StringReplace($proFileDirectory, "\", "\\")&'\\ImapMail\\imap'&$domain&'-'&$randomUID&'");'&@CRLF);'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.server.'&$servID&'.directory-rel", "[ProfD]ImapMail/imap'&$domain&'-'&$randomUID&'");'&@CRLF);'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.server.'&$servID&'.hostname", "imap'&$domain&'");'&@CRLF);'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.server.'&$servID&'.type", "imap");'&@CRLF);'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.server.'&$servID&'.port", 993);'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.server.'&$servID&'.socketType", 3);'&@CRLF);'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.server.'&$servID&'.spamActionTargetAccount", "imap://'&$accountName&'@imap'&$domain&'");'&@CRLF);'&@CRLF) 
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.server.'&$servID&'.namespace.personal", "\"\"");'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.server.'&$servID&'.namespace.public", "\"Shared Folders/Public/\"");'&@CRLF)
						
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.smtpserver.'&$smtpID&'.authMethod", 3);'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.smtpserver.'&$smtpID&'.description", "");'&@CRLF)

						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.smtpserver.'&$smtpID&'.hostname", "'&$smtpAddress&'");'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.smtpserver.'&$smtpID&'.port", 465);'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.smtpserver.'&$smtpID&'.try_ssl", 3);'&@CRLF)
						FileWrite($profileDirectory&"\user.js", 'user_pref("mail.smtpserver.'&$smtpID&'.type", "smtp");'&@CRLF)

						DirCreate($localFolderDirectory&"\_Archives de "&$accountName&".sbd")
						$dst = $localFolderDirectory&"\_Archives de "&$accountName&".sbd"
						FileWrite($localFolderDirectory&"\_Archives de "&$accountName, "")
						$cmdMove = RunWait(@ComSpec & ' /c powershell.exe -Command "Move-Item -Path ''' & $directory & '\*'' -Destination ''' & $dst & '''"', "")
						;~ MsgBox(16, "MoveYourPOP", "Erreur lors du déplacement des mails de "&$accountName&" vers "&$dst&@CRLF&@CRLF&"le programme va s'arrêter."&@CRLF&"Aucun changement n'a été effectué.")
						;~ MsgBox(16, "MoveYourPOP", "Erreur lors du déplacement des mails de "&$accountName&" vers "&$dst&@CRLF&@CRLF&"le programme va s'arrêter."&@CRLF&"Aucun changement n'a été effectué.")
						If $cmdMove <> 0 Then
							MsgBox(16, "MoveYourPOP", "Erreur lors du déplacement des mails de "&$accountName&" vers "&$dst&@CRLF&@CRLF&"le programme va s'arrêter."&@CRLF&"Aucun changement n'a été effectué.")
							FileDelete($profileDirectory&"\user.js")
							Exit
						EndIf
					Next
				EndIf
				
				GUICtrlSetData($go, "🚀 Terminé")
				GUICtrlSetState($go, $GUI_DISABLE)
				GUICtrlSetBkColor($go, 0x3CB371)
				MsgBox(64, "MoveYourPOP", "Terminé ✅")
				GUIDelete($hGUI)
				$thunderbirdPath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\thunderbird.exe", "")
				If FileExists($thunderbirdPath) Then
					Run($thunderbirdPath)
					Sleep(20000)
					FileMove($profileDirectory&"\user.js", $profileDirectory&"\user.js.bak", 1)
					Exit
				Else
					MsgBox(16, "Erreur", "Thunderbird non trouvé !"&@CRLF&"Veuillez le lancer manuellement."&@crlf&"Pensez à supprimer le fichier user.js dans le dossier du profil Thunderbird.")
					Exit
				EndIf
			EndIf
			;~ Exit
	EndSwitch
WEnd

;~ Func _getAllServer($profileDirectory)
; teste si le prefs.js existe
; teste si un compte POP existe
; récupère les infos des serveurs POP
Func _getAllServer($profileDirectory)
	GUICtrlDelete($listview)
	$listview = GUICtrlCreateListView("Username|Account|ID|ServerID|Dossier de stockage|smtpID|Size", 20, 220, 760, 250)
	GUICtrlSetData($go, "🚀 Exécuter")
	GUICtrlSetBkColor($go, $color2)
	GUICtrlSetState($go, $GUI_ENABLE)
	GUICtrlSetBkColor($labProfile, 0xffffff)
	GUICtrlSetColor($labProfile, 0x000000)
	GUICtrlSetBkColor($labSave, 0xffffff)
	GUICtrlSetColor($labSave, 0x000000)
	
	$jsFile = $proFileDirectory&"\prefs.js"
	if Not FileExists($jsFile) Then
		GUICtrlSetData($labProfile, "Pas de fichier prefs.js trouvé dans "&$profileDirectory)
		GUICtrlSetBkColor($labProfile, 0xE24141)
		GUICtrlSetColor($labProfile, 0xffffff)
		GUICtrlSetState($go, $GUI_DISABLE)
		GUICtrlSetData($go, "🛠️ Pas Prêt")
		Return SetError(13)
	EndIf
	$aArray = FileReadToArray($jsFile)
	$prefsToArray = _prefsjsToArray($aArray)
	;on cherche les serveur pop
	$searchForServer = _searchKeyInPrefsJsArrayRegex($prefsToArray, "^pop3$")

	If $searchForServer[0] = 0 Then
		GUICtrlSetData($labProfile, "Pas de compte POP trouvé dans "&$profileDirectory)
		GUICtrlSetBkColor($labProfile, 0xE24141)
		GUICtrlSetColor($labProfile, 0xffffff)
		GUICtrlSetBkColor($go, 0xE24141)
		GUICtrlSetState($go, $GUI_DISABLE)
		GUICtrlSetData($go, "🛠️ Pas Prêt")
		return SetError(14)
	EndIf

	$profileSizef = Round(DirGetSize($profileDirectory)/1024/1024, 2)&" Mo"
	GUICtrlSetData($labProfile, $profileDirectory)
	GUICtrlSetData($labSizeProfile, "("&$profileSizef&")")
	
	Global $allServer[1][7]
	Local $count = 0
	For $i = 1 to $searchForServer[0]
		$serverID = StringSplit($searchForServer[$i], ".")
		$serverID = $serverID[3]
		$searchForAcNice = _searchValueInPrefsJsArrayRegex($prefsToArray, "mail\.server\."&$serverID&"\.hostname$")
		For $p = 1 To $searchForAcNice[0]
			$domainToSearch = "ac-nice.fr|ac-aix-marseille.fr"
			$domainToSearch = StringReplace($domainToSearch, ".", "\.")
			$domainToSearch = StringReplace($domainToSearch, " ", "")
			if StringRegExp($searchForAcNice[$p], $domainToSearch) Then
				$infoServeur = _getInfosAboutServer($prefsToArray, $serverID)
				$username = $infoServeur[0]
				$IDAccount = $infoServeur[1]
				$IDIdentity = $infoServeur[2]
				$directory = StringReplace($infoServeur[3], "/", "\")
				$directory = StringReplace($directory, "[ProfD]", $profileDirectory&"\")
				$smtpID = _searchValueInPrefsJsArray($prefsToArray, "mail.identity."&$IDIdentity&".smtpServer")
				;~ ConsoleWrite("mail.identity."&$IDIdentity&".smtpServer"&@CRLF&$smtpID&@CRLF)
				if $smtpID = "0" Then
					$smtpID = _searchValueInPrefsJsArray($prefsToArray, "mail.smtp.defaultserver")
				EndIf
				$popHostname = $searchForAcNice[$p]

				$allServer[$count][0] = $username
				$allServer[$count][1] = $IDAccount
				$allServer[$count][2] = $IDIdentity
				$allServer[$count][3] = $serverID
				$allServer[$count][4] = $directory
				$allServer[$count][5] = $smtpID
				$allServer[$count][6] = $popHostname

				$count += 1
				Redim $allServer[$count + 1][7]
			EndIf
		Next
	Next
	if $count = 0 then GUICtrlSetState($go, $GUI_DISABLE)
	Redim $allServer[$count][7]
	;~ _ArrayDisplay($allServer)
	;~ GUICtrlDelete($listview)
	;~ $listview = GUICtrlCreateListView("Username|Account|ID|ServerID|Dossier de stockage|smtpID|Adresse du POP", 20, 220, 760, 250)
	;~ GUICtrlSetFont(-1, 11, 500, Default, "Segoe UI")
	_GUICtrlListView_AddArray($listview, $allServer)
	;~ Redim $allServer[$count][6]
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, -1) ; $LVSCW_AUTOSIZE
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 1, -1) ; $LVSCW_AUTOSIZE
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 2, -1) ; $LVSCW_AUTOSIZE
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 3, -1) ; $LVSCW_AUTOSIZE
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 4, -1) ; $LVSCW_AUTOSIZE
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 5, -1) ; $LVSCW_AUTOSIZE
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 6, -1) ; $LVSCW_AUTOSIZE
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 7, -1) ; $LVSCW_AUTOSIZE
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, -2) ; $LVSCW_AUTOSIZE_USEHEADER
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 1, -2) ; $LVSCW_AUTOSIZE_USEHEADER
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 2, -2) ; $LVSCW_AUTOSIZE_USEHEADER
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 3, -2) ; $LVSCW_AUTOSIZE_USEHEADER
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 4, -2) ; $LVSCW_AUTOSIZE_USEHEADER
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 5, -2) ; $LVSCW_AUTOSIZE_USEHEADER
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 6, -2) ; $LVSCW_AUTOSIZE_USEHEADER
	GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 7, -2) ; $LVSCW_AUTOSIZE_USEHEADER
EndFunc

;fonction de sauvegarde
Func _backup($source, $destination)
	$dateHeure = StringFormat("%02d_%02d_%04d_%02dh%02dm%02ds", @MDAY, @MON, @YEAR, @HOUR, @MIN, @SEC)
	$name = StringSplit($source, "\")
	$name = $name[$name[0]]&"_backup_"&$dateHeure
	$cmdSave = RunWait('robocopy.exe "' & $source & '" "' & $destination & '\' & $name & '" /E /COPY:DAT /MT:16 /R:3 /W:5')
	if $cmdSave >= 8 Then
		MsgBox(16, "MoveYourPOP", "Il y a eu des erreurs lors de la sauvegarde")
	EndIf
	Return $name
EndFunc

;vérifie l'espace disque
func _checkSpace($profileDirectory, $dstSave)
	Local $free, $profileSize
	$free = DriveSpaceFree($dstSave)
	$profileSize = DirGetSize($profileDirectory) /1024/1024
	If $profileSize > $free Then
		Return SetError(666)
	EndIf
EndFunc

;récupère les infos des serveurs POP
Func _getInfosAboutServer($prefsArray, $serverID)
	Local $aArray[4]
	Local $accountID
	$aArray[0] = _searchValueInPrefsJsArray($prefsArray, "mail.server."&$serverID&".userName")
	$accountID = _searchKeyInPrefsJsArray($prefsArray, $serverID)
	$accountID = StringSplit($accountID, ".")
	$accountID = $accountID[3]
	$aArray[1] = $accountID
	$aArray[2] = _searchValueInPrefsJsArray($prefsArray, "mail.account."&$accountID&".identities")
	$aArray[3] = _searchValueInPrefsJsArray($prefsArray, "mail.server."&$serverID&".directory-rel")
	Return $aArray
EndFunc

; on met le contenu du prefs.js sous la forme d'un tableau clé => valeur
Func _prefsjsToArray($readFile)
	Local $value = ""
	Local $arrayLength = UBound($readFile) - 1
	If $arrayLength < 1 Then
		Return SetError(13)
	EndIf
	Local $array[$arrayLength][2]
	Local $x = 0

	For $i = 1 to $arrayLength
		If StringRegExp($readFile[$i], '^(?!\s*//)(?!\s*$).+') Then
			$x = $x + 1
			$value = ""
			$string = StringRegExpReplace($readFile[$i], '^user_pref\(\"', "")
			$string = StringRegExpReplace($string, '\"\)\;$', "")
			$string = StringRegExpReplace($string, '\)\;$', "")
			$string = StringReplace($string, '", "', ",")
			$string = StringReplace($string, '", ', ",")
			$split = StringSplit($string, ",")
			If $split[0] > 2 Then
				For $p = 2 to $split[0]
					$value &= $split[$p]&","
				Next
				$value = StringRegExpReplace($value, ",$", "")
			Else
				$value = $split[2]
			EndIf
			$array[$x][0] = $split[1]
			$array[$x][1] = $value
		EndIf
	Next

	$array[0][0] = $x
	ReDim $array[$x + 1][2]
	Return $array
EndFunc

;recherche dans le tableau (prefs.js)
;reverse 0 => on cherche clé
;reverse 1 => on cherche une valeur
Func _searchValueInPrefsJsArrayRegex($array, $string)
	Local $key, $value
	Local $x = 0
	Local $result[1]
	For $p = 1 to $array[0][0]
		$key = $array[$p][0]
		$value = $array[$p][1]
		If StringRegExp($key, $string) Then
			_ArrayAdd($result, $value)
			$x = $x + 1
		EndIf
	Next
	$result[0] = $x
	Return $result
EndFunc

Func _searchKeyInPrefsJsArrayRegex($array, $string)
	Local $key, $value
	Local $x = 0
	Local $result[1]
	For $p = 1 to $array[0][0]
		$key = $array[$p][0]
		$value = $array[$p][1]
		If StringRegExp($value, $string) Then
			_ArrayAdd($result, $key)
			$x = $x + 1
		EndIf
	Next
	$result[0] = $x
	Return $result
EndFunc

Func _searchValueInPrefsJsArray($array, $string)
	Local $key, $value
	;~ Local $x = 0
	For $p = 1 to $array[0][0]
		$key = $array[$p][0]
		$value = $array[$p][1]
		If $key = $string Then
			Return $value
		EndIf
	Next
EndFunc

Func _searchKeyInPrefsJsArray($array, $string)
	Local $key, $value
	;~ Local $x = 0
	For $p = 1 to $array[0][0]
		$key = $array[$p][0]
		$value = $array[$p][1]
		If $value = $string Then
			Return $key
		EndIf
	Next
EndFunc

Func FindLatestPrefs($sBaseDir)
    Local $aFileList = _FileListToArrayRec($sBaseDir, "prefs.js", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT)
    If @error Then
        Return SetError(666)
    EndIf

    Local $sLatestFile = "", $iLatestTime = 0

    For $i = 1 To $aFileList[0]
        Local $sFilePath = $sBaseDir & "\" & $aFileList[$i]
		If Not StringInStr($sFilePath, "backup") Then
        Local $sModTime = FileGetTime($sFilePath, $FT_MODIFIED, 1)
			If $sModTime > $iLatestTime Then
				$iLatestTime = $sModTime
				$sLatestFile = $sFilePath
			EndIf
		EndIf
    Next
	Local $latestDirectory = StringRegExpReplace($sLatestFile, "\\[^\\]+$", "")
    Return $latestDirectory
EndFunc