1
# Simple TortoiseSVN-like Bazaar plugin for the Windows Shell
2
# Published under the GNU GPL, v2 or later.
3
# Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
6
from win32com.shell import shell, shellcon
10
"""Windows shell extension that adds context menu items to Bazaar branches."""
11
class BazaarShellExtension:
12
_reg_progid_ = "Bazaar.ShellExtension.ContextMenu"
13
_reg_desc_ = "Bazaar Shell Extension"
14
_reg_clsid_ = "{EEE9936B-73ED-4D45-80C9-AF918354F885}"
15
_com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu]
17
"Initialize", # From IShellExtInit
18
"QueryContextMenu", "InvokeCommand", "GetCommandString" # IContextMenu
21
def Initialize(self, folder, dataobj, hkey):
22
self.dataobj = dataobj
24
def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags):
25
format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL
26
sm = self.dataobj.GetData(format_etc)
27
num_files = shell.DragQueryFile(sm.data_handle, -1)
29
msg = "&Hello from Python (with %d files selected)" % num_files
31
fname = shell.DragQueryFile(sm.data_handle, 0)
32
msg = "&Hello from Python (with '%s' selected)" % fname
35
if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0
38
elif uFlags & shellcon.CMF_VERBSONLY:
39
print "CMF_VERBSONLY..."
40
items.append(msg + " - shortcut")
41
elif uFlags & shellcon.CMF_EXPLORE:
42
print "CMF_EXPLORE..."
43
items.append(msg + " - normal file, right-click in Explorer")
44
elif uFlags & CMF_DEFAULTONLY:
45
print "CMF_DEFAULTONLY...\r\n"
47
print "** unknown flags", uFlags
48
win32gui.InsertMenu(hMenu, indexMenu,
49
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
53
win32gui.InsertMenu(hMenu, indexMenu,
54
win32con.MF_STRING|win32con.MF_BYPOSITION,
59
win32gui.InsertMenu(hMenu, indexMenu,
60
win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
63
return idCmd-idCmdFirst # Must return number of menu items we added.
65
def InvokeCommand(self, ci):
66
mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci
67
# FIXME: Run the actual command
69
def GetCommandString(self, cmd, typ):
70
return "Hello from Python!!"
73
"*\\shellex\\ContextMenuHandlers",
74
"Directory\\Background\\shellex\\ContextMenuHandlers",
75
"Directory\\shellex\\ContextMenuHandlers",
76
"Folder\\shellex\\ContextmenuHandlers"
79
def DllRegisterServer():
81
for keyname in registryKeys:
82
key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, keyname)
83
subkey = _winreg.CreateKey(key, "TortoiseBzr")
84
_winreg.SetValueEx(subkey, None, 0, _winreg.REG_SZ, BazaarShellExtension._reg_clsid_)
85
_winreg.CloseKey(subkey)
88
print BazaarShellExtension._reg_desc_, "registration complete."
90
def DllUnregisterServer():
93
for keyname in registryKeys:
94
_winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
95
"%s\\TortoiseBzr" % keyname)
96
except WindowsError, details:
98
if details.errno != errno.ENOENT:
100
print BazaarShellExtension._reg_desc_, "unregistration complete."
102
if __name__ == '__main__':
103
from win32com.server import register
104
register.UseCommandLine(BazaarShellExtension,
105
finalize_register = DllRegisterServer,
106
finalize_unregister = DllUnregisterServer)