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

# ---------- User Agent --------------------------------------------------------------------------------------------
#
# Dependencies	: main.kmm ($macroPrefBranch)
# Resources	: -
# Preferences	: kmeleon.privacy.useragent[1,2,...].name
#		  kmeleon.privacy.useragent[1,2,...].string
#
# ------------------------------------------------------------------------------------------------------------------

UserAgent{
macroinfo=_("Set the specified User Agent string");
menuchecked=$ARG==""?(getpref(STRING,$_UserAgent_Active)==""):(getpref(STRING,$_UserAgent_Active)==getpref(STRING,$_UserAgent_Custom.$ARG.".string"));
$ARG==""?delpref($_UserAgent_Active):$_string=getpref(STRING,$_UserAgent_Custom.$ARG.".string");
$ARG==""?&_UserAgent_SyncMenu:&_UserAgent;
}
UserAgent_Custom{
macroinfo=_("Set a custom User Agent string");
$_string=prompt(_("Enter the User Agent string to identify K-Meleon as:"),_("Custom User Agent"),getpref(STRING,$_UserAgent_Active));
$_string==""?0:&_UserAgent;
}

UserAgent_AlertString{
macroinfo=_("View the current User Agent string");
injectJS("alert('"._("Current User Agent String:")."\\n\\n'+navigator.userAgent)");
}
UserAgent_ToggleReset{
macroinfo=_("Toggle resetting the default User Agent string when exiting");
menuchecked=getpref(BOOL,$_UserAgent_Reset);
togglepref(BOOL,$_UserAgent_Reset);
}

# ----- PRIVATE

$_UserAgent_Active="general.useragent.override";
$_UserAgent_Custom="kmeleon.privacy.useragent";
$_UserAgent_Reset=$macroPrefBranch."useragent.reset_on_exit";

_UserAgent{
$_string==""?delpref($_UserAgent_Active):setpref(STRING,$_UserAgent_Active,$_string); &_UserAgent_SyncMenu;
}
_UserAgent_SyncMenu{
$_data=getpref(STRING,$_UserAgent_Active);
setcheck("macros(UserAgent_Custom)",($_data!=""));
$_j=1; while($_j>0) &_UserAgent_SyncMenuLoop;
}
_UserAgent_SyncMenuLoop{
$__data=getpref(STRING,$_UserAgent_Custom.$_j.".string");
$__data==$_data?setcheck("macros(UserAgent_Custom)",false):0;
$_j=$__data==""?0:$_j+1;
}
$OnStartup=$OnStartup."_UserAgent_SyncMenu;";

_UserAgent_BuildMenu{
# tools menu
$_u="&User Agent";
setmenu(PrivacySecurity,popup,$_u);
setmenu($_u,macro,"Default","UserAgent()");
$_i=1; while($_i>0) &_UserAgent_BuildMenuLoop;
setmenu($_u,macro,"&Custom...",UserAgent_Custom);
setmenu($_u,macro,"&Reset To Default On Exit",UserAgent_ToggleReset);
setmenu($_u,separator,"&Reset To Default On Exit");
}
_UserAgent_BuildMenuLoop{
$__data=getpref(STRING,$_UserAgent_Custom.$_i.".name");
$__data==""?0:setmenu($_u,macro,$__data,"UserAgent(".$_i.")");
$_i=$__data==""?0:$_i+1;
}
$OnInit=$OnInit."_UserAgent_BuildMenu;";

_UserAgent_Reset{
getpref(BOOL,$_UserAgent_Reset)?delpref($_UserAgent_Active):0;
}
$OnQuit=$OnQuit."_UserAgent_Reset;";

# ------------------------------------------------------------------------------------------------------------------
$macroModules=$macroModules."UserAgent;";