NB. browser

cocurrent 'jbrowser'

cd=: 15!:0
deb=: #~ (+. (1: |. (> </\)))@(' '&~:)

NB. =========================================================
NB. launch
NB.
NB. launch file in browser
NB.
NB. y. is a filename
launch=: 3 : 0
browser=. BROWSER_j_
if. 0 = #browser do.
  browser=. dfltbrowser''
end.
cmd=. deb y.
cmd=. '/' (I. cmd='\') } cmd
cmd=. 'file://',cmd
cmd=. '"',cmd,'"'
cmd=. browser,' ',cmd
try. wd 'winexec *',cmd
catch.
  msg=. 'Could not run the browser with the command:',LF,LF
  msg=. msg, cmd,LF,LF
  msg=. msg,'You can change the browser definition in Edit|Configure|Browser.',LF,LF
  msg=. msg,'You can also access the Help directly from system\extras\help\index.htm'
  wdinfo 'Run Browser';msg
end.
)

NB. =========================================================
NB.  Read registry and return name of default browser
NB. dfltbrowser ''
NB.     return default browser, or '' if we cannot
dfltbrowser=: verb define
if. UNAME -: 'Darwin' do. 'open' return. end.
if. IFUNIX do.
  try.
    2!:0'which netscape'
    'netscape' return. catch. end.
  try.
    2!:0'which konqueror' NB. kde
    'konqueror' return. catch. end.
  '' return.
end.
'' if. 1~:>{.r=. querykeyvalue HKEY_CLASSES_ROOT;'.htm';'' do. return. end.
if. 1~:>{.r=. querykeyvalue HKEY_CLASSES_ROOT;((}:>{:r),'\shell\open\command');'' do. return. end.
r=. deb >{:r
r {.~ 1 i.~ (r=' ') > ~:/\r='"'
)

NB. =========================================================
RegOpenKeyExA=: 'advapi32 RegOpenKeyExA i i *c i i *i'&cd
RegQueryValueExA=: 'advapi32 RegQueryValueExA i i *c i *i *c *i'&cd
RegCloseKey=: 'advapi32 RegCloseKey i i'&cd
HKEY_CLASSES_ROOT=: _2147483648

NB. =========================================================
NB. STANDARD_RIGHTS_ALL,KEY_QUERY_VALUE,KEY_SET_VALUE
NB. KEY_CREATE_SUB_KEY,KEY_ENUMERATE_SUB_KEYS
NB. KEY_NOTIFY,KEY_CREATE_LINK
NB. and not SYNCHRONIZE
fullaccess=: 983103

NB. 'type value'=. querykeyvalue hkey;subkey;value
querykeyvalue=: verb define
2000 querykeyvalue y.
:
'hkey subkey value'=. y.
'rc handle'=. regopen hkey;subkey
if. rc~:0 do. (-rc);'' return. end.
'rc x x x type data len'=. RegQueryValueExA handle;value;0;(,0);(x.${.a.);,x.
'unable to close' assertrc regclose handle
if. rc~:0 do. (-rc);'' return. end.
({.type);({.len){.data
)


NB. =========================================================
NB. regopen key;subkey
regopen=: verb define
'hkey subkey'=. y.
'rc x x x x handle'=. RegOpenKeyExA hkey;subkey;0;fullaccess;,0
rc;{.handle
)


NB. =========================================================
regclose=: verb define
>{.RegCloseKey ,<y.
)

NB. =========================================================
assertrc=: ([ , ' ('"_ , ":@] , ')'"_) assert ]=0:
