Adding Menu Items to Plugin
#1
How can i add a menu item to my plugin, lets say if i want when clicking the context menu (where it has Plugin Settings, etc..) i want to add some items (like delete or pause or resume)

sorry i tried to search for it here, but im out of luck with keywords to find what im looking for.

thank you all
Reply
#2
You could use something like:
Code:
commands = []
commands.append(( 'runme',  'XBMC.RunPlugin(plugin://video/myplugin)', ))
commands.append(( 'runother',  'XBMC.RunPlugin(plugin://video/otherplugin)', ))
listitem = xbmcgui.ListItem()
listitem.addContextMenuItems( commands )
Reply
#3
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('', '')
        res = res.replace('', '')
        res = res.replace('', '')
        res = res.replace('', '')
        res = res.replace('', '')

        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
Reply
#4
to clarify a bit ore what i want to do:

when clicking on lets say "Pause" it will set the "mode" to 2 and pass along with it the file id (which is "n") this will call the function doAction for example and then telnet into my mldonkey and pause the file with the id "n" ?!

what i want to know is, like in :
Code:
xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,True)

where i add the item with the arguments for its next action, how can i add the same arguments for:
Code:
li = xbmcgui.ListItem(f + ' [' + r + '/' + p + ']')
                li.addContextMenuItems([
                        ('Resume',[b]'XBMC.RunPlugin(plugin://video/myplugin)'[/b],),
                        ('Pause','XBMC.RunPlugin(plugin://video/myplugin)',),
                        ('Cancel','XBMC.RunPlugin(plugin://video/myplugin)',)
                        ], True )
Reply
#5
figured it out Smile

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('', '')
        res = res.replace('', '')
        res = res.replace('', '')
        res = res.replace('', '')
        res = res.replace('', '')

        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 + ']')
                li.addContextMenuItems([
                        ('Resume',"XBMC.RunPlugin(%s?mode=1&fid=%s)"%(sys.argv[0], n),),
                        ('Pause','XBMC.RunPlugin(plugin://video/mlDonkey Manager)',),
                        ('Cancel','XBMC.RunPlugin(plugin://video/mlDonkey Manager)',)
                        ], True )
                u = sys.argv[0] + "?mode=0&cat=" + urllib.quote_plus(f)
                xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,True)
    
def filePause(fid):
        tn = telnetlib.Telnet(settings[SERVER], settings[PORT])
        tn.read_until("MLdonkey command-line:")
        tn.write("pause "+fid+"\n")
        tn.write("exit\n")
        

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
fid = None

get_settings()                                      


try:
        cat=urllib.unquote_plus(params["cat"])
except:
        pass
try:
        mode=int(params["mode"])
except:
        pass
try:
        fid=urllib.unquote_plus(params["fid"])
except:
        pass



if mode == None:
    showRoot()
elif mode == 0:
        showList(cat)
elif mode == 1:
        filePause(fid)

xbmcplugin.endOfDirectory(int(sys.argv[1]))

still working on code though
Reply
#6
can you help if i wanna run script instead of running plugin
Reply
#7
mirooegypt Wrote:can you help if i wanna run script instead of running plugin

i think it would be the same way, but then again my knowledge to scripting in XBMC is not that great
Reply

Logout Mark Read Team Forum Stats Members Help
Adding Menu Items to Plugin0