#@+leo-ver=5-thin
#@+node:maphew.20130611235500.2658: * @file register-leo.leox
#@@language python
#@@tabwidth -4
#@+others
#@+node:maphew.20130510011223.1645: ** docstring
'''Tell Windows how to handle .leo files, enables double clicking on them to open.

To run: in Leo make this the active node and press [Ctrl-B] (execute-script)
        
Requires elevated User Account Control (UAC) privileges.
See http://superuser.com/questions/88491/force-cmd-exe-to-run-as-admin/

Resources used:
   @url  http://stackoverflow.com/questions/2331690/how-to-set-a-icon-file-while-creating-file
   @url http://stackoverflow.com/questions/771689/how-can-i-set-an-icon-for-my-own-file-extension
'''
#@+node:maphew.20130509185752.1607: ** register_leo
def register_leo(pyexe, launchLeo, icon):
    '''Create registry key for Leo file type, set default icon and launch command    
    '''    
    g.es("\nAttempting to register leo filetype with Windows...")
    g.es("Python:", pyexe)
    g.es("Leo:", launchLeo)
    
    if g.os_path_exists(icon):
        g.es("Found:", icon)
        #@+<<filetype>>
        #@+node:maphew.20130515233516.1659: *3* <<filetype>>
        try:
            leoKey = CreateKey(HKEY_CLASSES_ROOT, "Leo.File")
            iconKey = CreateKey(leoKey, "DefaultIcon")
            shellKey = CreateKey(leoKey, "Shell")
            openKey = CreateKey(shellKey, "Open")
            cmdKey = CreateKey(openKey, "Command")

            SetValue(leoKey, None, REG_SZ, "Leo Editor File")
            SetValue(iconKey, None, REG_SZ, icon)
            SetValueEx(cmdKey, None, None, REG_EXPAND_SZ, '"{0}" "{1}" "%1" %*'.format(pyexe, launchLeo))
            
            for k in (leoKey, iconKey, shellKey, openKey, cmdKey):
                CloseKey(k)
                
            g.es("Registered!")

        except WindowsError as e:
            g.es("Failed:", e)
        #@-<<filetype>>
        #@+<<association>>
        #@+node:maphew.20130515233516.1660: *3* <<association>>
        try:
            leoKey = CreateKey(HKEY_CLASSES_ROOT, ".leo")
            SetValue(leoKey, None, REG_SZ, "Leo.File")
            # SetValue(leoKey, "Content Type", REG_SZ, "text/xml")  #creates a sub_key instead of a value :(
            CloseKey(leoKey)
            
            leoKey = CreateKey(HKEY_CLASSES_ROOT, ".leox")
            SetValue(leoKey, None, REG_SZ, "Leo.File")
            CloseKey(leoKey)
            
            g.es(".leo, .leox files associated as 'Leo.File'")        

        except WindowsError as e:
            g.es("Failed:", e)
        #@-<<association>>
    else:
        g.es("LeoDoc.ico not in expected location, can't continue.")  
#@+node:maphew.20130510011223.1646: *3* sample registry outcome
'''This is what the resulting registry key should look like, once exported from
regedit. The translated hex code is:
    
    "d:\Python27\python.exe" "C:\Users\Matt\Dropbox\apps\leo-editor\launchLeo.py" "%1" %*
    
'''
#@+at
# Windows Registry Editor Version 5.00
# 
# [HKEY_CLASSES_ROOT\.leo]
# @="Leo.File"
# 
# [HKEY_CLASSES_ROOT\.leox]
# @="Leo.File"
# 
# [HKEY_CLASSES_ROOT\Leo.File]
# @="Leo Editor File"
# 
# [HKEY_CLASSES_ROOT\Leo.File\DefaultIcon]
# @="C:\\Users\\Matt\\Dropbox\\apps\\leo-editor\\leo\\Icons\\LeoDoc.ico"
# 
# [HKEY_CLASSES_ROOT\Leo.File\Shell]
# 
# [HKEY_CLASSES_ROOT\Leo.File\Shell\Open]
# 
# [HKEY_CLASSES_ROOT\Leo.File\Shell\Open\Command]
# @=hex(2):22,00,64,00,3a,00,5c,00,50,00,79,00,74,00,68,00,6f,00,6e,00,32,00,37,\
#   00,5c,00,70,00,79,00,74,00,68,00,6f,00,6e,00,2e,00,65,00,78,00,65,00,22,00,\
#   20,00,22,00,43,00,3a,00,5c,00,55,00,73,00,65,00,72,00,73,00,5c,00,4d,00,61,\
#   00,74,00,74,00,5c,00,44,00,72,00,6f,00,70,00,62,00,6f,00,78,00,5c,00,61,00,\
#   70,00,70,00,73,00,5c,00,6c,00,65,00,6f,00,2d,00,65,00,64,00,69,00,74,00,6f,\
#   00,72,00,5c,00,6c,00,61,00,75,00,6e,00,63,00,68,00,4c,00,65,00,6f,00,2e,00,\
#   70,00,79,00,22,00,20,00,22,00,25,00,31,00,22,00,20,00,25,00,2a,00,00,00
#@-others
from _winreg import *

# g.es("\n--- Running register-leo Leo script")

pyexe = g.sys.executable
pywexe = pyexe.replace('python.exe', 'pythonw.exe')
launchLeo = g.os_path_finalize_join(g.computeLeoDir(), '../launchLeo.py')
icon = "%s\Icons\LeoDoc.ico" % g.computeLeoDir()

# use 'pywexe' instead to not open a console window when launching Leo
register_leo(pywexe, launchLeo, icon)
#@-leo
