# K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)

# ---------- Xul Tester Extension ----------------------------------------------------------------------------------
#
# Dependencies	: main.kmm (OpenURL), 7-zip
# Resources	: -
# Preferences	: -
#
# ------------------------------------------------------------------------------------------------------------------
#
# In case of manual install, registry keys can't be found and menu is grayed
# To Bypass it, just fill the content of this variable the example value adapted
# to your config
# Example :
# $_7Zip_Path="\"C:\\Program Files\\7-Zip\\7z.exe"
# You must restart K-Meleon to take the modifications
# You have two shortcuts to modify this value:
#  SHIFT+ALT+J when The command line is empty

$_7Zip_Path="";

# ---------- General -----------------------------------------------------------------------------------------------

$_zipper="";
$jarfile="";
$xulfile="";
$JSfile="";
$_lines="";

$_jarfileXUL=getpref(STRING,"extensions.xultester.XUL.jarfile");
$_xulfileXUL=getpref(STRING,"extensions.xultester.XUL.file2test");
$_jarfileJS=getpref(STRING,"extensions.xultester.JS.jarfile");
$_xulfileJS=getpref(STRING,"extensions.xultester.JS.file2test");

# ------------------------------------------------------------------------------------------------------------------


_XULfile{
$_type="XUL";
&_JarName;
$xulfile=$_xulfileXUL;
$jarfile==""?0:&_SaveXUL;
$jarfile==""?0:$_zipper==""?0:&_FileList;
$jarfile==""?0:&_File2Test;
$jarfile==""?$OpenURL="":$OpenURL=($xulfile==""?"":"chrome://".$jarfile."/content/".$xulfile.".xul");
$OpenURL==""?0:&OpenURL_InNewWindow;
}

_JSfile{
$_type="JS";
&_JarName;
$xulfile=$_xulfileJS;
$jarfile==""?0:&_SaveJS;
$jarfile==""?0:$_zipper==""?0:&_FileList;
$jarfile==""?0:&_File2Test;
$jarfile==""?$OpenURL="":$OpenURL=($xulfile==""?$"":"chrome://".$jarfile."/content/".$xulfile.".js");
$OpenURL==""?0:injectJS($OpenURL);
}

_JarName{
$jarfile=$_type=="XUL"?$_jarfileXUL:$_jarfileJS;
$_file="xxx";
$_rep=$jarfile==""?"1":"0";
$_rep=="1"?0:$_message=_("Enter the name without the extension").".\n\n"._("Current file is : '").$jarfile."'\n\n     "._("Yes = Edit the name")."\n     "._("No  = Browse for file")."\n     "._("Cancel or Esc = Cancel the test")
$_rep=$_rep=="1"?"NO":confirm($_message, _("Name of the jar file to test"), YESNOCANCEL);
# Browse
$_rep=="NO"?&_Browse:0;
# Edit
$_rep=="YES"?&_Edit:0;
# Cancel
$_rep=="0"?$jarfile="":0;
$jarfile==""?0:setpref(STRING,"extensions.xultester.".$_type.".jarfile",$jarfile);
}

_SaveXUL{
$_jarfileXUL==$jarfile?0:$xulfile="";
$_jarfileXUL=$jarfile;
}

_SaveJS{
$_jarfileJS==$jarfile?0:$xulfile="";
$_jarfileJS=$jarfile;
}

_FileList{
$_echo="@echo off\n";
$_zipper="\"c:\\Program Files\\7-Zip\\7z.exe\"";
$_command=" l -r \"";
$_jar=getfolder(RootFolder)."\\chrome\\".$jarfile.".jar\"";
$_files=$_type=="XUL"?" *.xul>":" *.js>";
$_target="\"".getfolder(ProfileFolder)."\\jarfiles.txt\"";
$_commandLine=getfolder(ProfileFolder)."\\jarfiles.bat";;
pluginmsg(utils,"write",$_commandLine,$_echo.$_zipper.$_command.$_jar.$_files.$_target);
pluginmsg(utils,"exec",$_commandLine,9999);
$_liste=pluginmsgex(utils,"read",getfolder(ProfileFolder)."\\jarfiles.txt",STRING);
$_type=="XUL"?$__type=".xul":$__type=".js";
$_index=index($_liste,$__type);
$_lines="";$_line="";$_separator=0;
while($_index>-1) &_addListe;
alert(_("Here is a list of the files avalaible for test:")."\n\n".$_lines,($_type=="XUL"._("XUL files of"):_("JS files of"))." ".$jarfile.".jar");
}

_addListe{
$_endofline=index($_liste,"\n");
$_endofline>-1?$_line=substr($_liste,0,$_endofline-1):0;
$_endofline>-1?$_liste=substr($_liste,$_endofline+1):0;
$_line==""?$_content=-1:$_content=index($_line,"content\\");
$_content>-1?0:$_line="";
$_line==""?0:$_line=gsub($__type,"",substr($_line,$_content+8));
index($_line,$jarfile."\\")==0?$_line=substr($_line,length($jarfile)+1):0;
$_line==""?0:$_lines=$_lines.$_line."\n";
$_index=index($_liste,$__type);
}

_Browse{
$_file=pluginmsgex(utils, "GetOpenFileName",getfolder(RootFolder)."\\chrome\\*.jar" , STRING);
index($_file,"chrome")>-1?$_file=substr($_file,index($_file,"chrome")+7):0;
$_file == ""?$_rep="0":$jarfile=substr($_file,0,index($_file,"."));
}

_Edit{
$jarfile=prompt(_("Enter the name without the extension"),_("Name of the jar file to test"),$jarfile);
}

_File2Test{
$xulfile=prompt(_("Enter the name without the extension"),$_type=="XUL"?_("Name of the XUL file to test"):_("Name of the JS file to test"),$xulfile);
$xulfile==""?$jarfile="":setpref(STRING,"extensions.xultester.".$_type.".file2test",$xulfile);
$xulfile==""?0:($_type=="XUL"?$_xulfileXUL=$xulfile:$_xulfileJS=$xulfile);
}

JarAbout{
$_helpTitle=_("How to use Jar Tester");
$_help1=_("Required:")."\n\n";
$_help2=_("1) Jarfile must be located in chrome folder.")."\n";
$_help3=_("2) Its own manifest file must be located in chrome folder")."\n";
$_help4=_("3) Js file test doesn't work for now.")."\n";
$_help5=_("4) 7-Zip is automatically uses if found.")."\n";
$_help6=_("5) 7-Zip path can be manually set by using Alt+Shift+J");
alert($_help1.$_help2.$_help3.$_help4.$_help5.$_help6,$_helpTitle);
}

# ----- PRIVATE

_JarTester_CommandLine{
# try to get 7-Zip's command line from the Windows Registry
$__data=readreg("HKLM","Software\\7-Zip\\Path");
$_zipper=$__data;
$_zipper==""?$_zipper=$_7Zip_Path:0;
$_zipper==""?setaccel("SHIFT ALT J","macros(_JarTester_Path)"):0;
}

_JarTester_Path{
$ext="kmm"; &getExtensionHandler; exec(sub("%1",getfolder(MacroFolder)."\\JarTester.kmm",$cmdline));
}

_JarTester_BuildMenu{
setmenu("Development",popup,"&JAR Tester",0);
setmenu("&JAR Tester",macro,"&XUL file",_XULfile);
setmenu("&JAR Tester",macro,"&JS file",_JSfile);
setmenu("&JAR Tester",separator,-1);
setmenu("&JAR Tester",macro,"&Help",JarAbout);
}

$OnInit=$OnInit."_JarTester_BuildMenu;";
$OnStartup=$OnStartup."_JarTester_CommandLine;";
# ------------------------------------------------------------------------------------------------------------------
$macroModules=$macroModules."JarTester;";
