I got the items added to context menu, but now how can i add functionality to it, in my case i want to issue a certain command over telnet.
here is the code:
Code:
import xbmc, xbmcgui, xbmcplugin, urllib2, urllib, telnetlib, os
settings = {}
SERVER, PORT = range(2)
def get_settings():
settings[SERVER] = xbmcplugin.getSetting( "server" )
settings[PORT] = xbmcplugin.getSetting( "port" )
def save_settings():
xbmcplugin.setSetting( "server", settings[SERVER] )
xbmcplugin.setSetting( "port", settings[PORT] )
def showRoot():
ri=[
("Downloading"),
("Queued"),
("Paused")
]
## add root list in xbmc gui
for cat in ri:
li = xbmcgui.ListItem(cat)
u = sys.argv[0] + "?mode=0&cat=" + urllib.quote_plus(cat)
xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,True)
def showList(cat):
tn = telnetlib.Telnet(settings[SERVER], settings[PORT])
tn.read_until("MLdonkey command-line:")
if cat == "Downloading":
tn.write("vd downloading\n")
elif cat == "Paused":
tn.write("vd paused\n")
elif cat == "Queued":
tn.write("vd queued\n")
tn.write("exit\n")
res = tn.read_all()
res = res.replace('[2;37;0m', '')
res = res.replace('[36m', '')
res = res.replace('[32m', '')
res = res.replace('[31m', '')
res = res.replace('[7m', '')
r = res.splitlines()
for l in r:
if l[0:1] == '[':
t = l[1] ## file type
n = l[3:6] ## file no.
f = l[33:83] ## file name
p = l[84:89] + '%' ## % done
s = l[101:108] ## file size
r = l[132:138] ## rate
i = l[140:143] ## priority
li = xbmcgui.ListItem(f + ' [' + r + '/' + p + ']')
[b] li.addContextMenuItems([
('Resume','XBMC.RunPlugin(plugin://video/myplugin)',),
('Pause','XBMC.RunPlugin(plugin://video/myplugin)',),
('Cancel','XBMC.RunPlugin(plugin://video/myplugin)',)
])[/b]
u = sys.argv[0] + "?mode=0&cat=" + urllib.quote_plus(f)
xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,True)
def get_params():
param=[]
paramstring=sys.argv[2]
if len(paramstring)>=2:
params=sys.argv[2]
cleanedparams=params.replace('?','')
if (params[len(params)-1]=='/'):
params=params[0:len(params)-2]
pairsofparams=cleanedparams.split('&')
param={}
for i in range(len(pairsofparams)):
splitparams={}
splitparams=pairsofparams[i].split('=')
if (len(splitparams))==2:
param[splitparams[0]]=splitparams[1]
return param
params = get_params()
mode = None
cat = None
get_settings()
try:
cat=urllib.unquote_plus(params["cat"])
except:
pass
try:
mode=int(params["mode"])
except:
pass
if mode == None:
showRoot()
elif mode == 0:
showList(cat)
xbmcplugin.endOfDirectory(int(sys.argv[1]))
btw, this is something im working on to control my mldonkey