xbmc.Keyboard -- addDir ?
#1
Question 
I am having an inconsistency when adding a search by calling keyboard in the addon.
I wish that when the user closes the keyboard without doing any search, it remains in the same directory.

However when doing the search using the code:

Code:
AddDir ( 'My Search - Folder True', '-', 1, 'search.jpg', True)

It returns a new directory with the items I want. But if the user does not search and close the keyboard Kodi advances to a new empty directory. For "isFolder = True"

However, if you search using the code below:

Code:
AddDir ( 'My Search - Folder False', '-', 1, 'search.jpg', False)

It does not return the new directory with the items. But if the user does not search and close the keyboard, Kodi will remain in the same directory. For "isFolder = False"
-----------

I want the code to add a new directory with the items of the "mySearch ()" function only when a text is searched, but when the user closes the keyboard without doing the search I want it to continue in the same directory, without advancing to that empty directory.

How can I fix this problem?


Simple example:

Code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import urllib, urllib2, re, xbmcplugin, xbmcgui, xbmc, xbmcaddon, HTMLParser, os
#=########################################################################################################=#
#                                                   MENU                                                   #
#=########################################################################################################=#
def CATEGORIES():
    addDir('My Search - Folder False', '-', 1, 'search.jpg', False)
    addDir('My Search - Folder True', '-', 1, 'search.jpg', True)
###################################################################################

#=########################################################################################################=#
#                                                 FUNCTIONS                                                #
#=########################################################################################################=#
def mySearch():
    keyb = xbmc.Keyboard('', 'XBMC Search')
    keyb.doModal()
    if keyb.isConfirmed() and len(keyb.getText().strip()) > 0 :
        search = keyb.getText()
        myParam = str(urllib.quote(search)).strip()
        addDir(myParam, 'myUrl', 2, 'other.jpg', False)    
    
def myOther():
    dialog = xbmcgui.Dialog()
    ok = dialog.ok('XBMC', 'Hello World')
    
def addDir(name, url, mode, iconimage, pasta=True, total=1):
    u=sys.argv[0]+'?url='+urllib.quote_plus(url)+'&mode='+str(mode)+'&name='+urllib.quote_plus(name)
    ok = True
    liz = xbmcgui.ListItem(name, iconImage='DefaultFolder.png', thumbnailImage=iconimage)
    ok = xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=u, listitem=liz, isFolder=pasta, totalItems=total)
    return ok
###################################################################################

#=########################################################################################################=#
#                                               GET PARAMS                                                 #
#=########################################################################################################=#    
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()
url = None
name = None
mode = None
iconimage = None

try:
    url = urllib.unquote_plus(params['url'])
except:
    pass
try:
    name = urllib.unquote_plus(params['name'])
except:
    pass
try:
    mode = int(params['mode'])
except:
    pass
try:        
    iconimage = urllib.unquote_plus(params['iconimage'])
except:
    pass
###################################################################################

#=###########################################################################################################=#
#                                                   MODES                                                     #
#=###########################################################################################################=#
if mode == None or url == None or len(url) < 1 :
    CATEGORIES()
elif mode == 1 :
    mySearch()
elif mode == 2 :
    myOther()
###################################################################################
xbmcplugin.endOfDirectory(int(sys.argv[1]))

Thank you.HuhHuhHuhHuhHuh
Reply
#2
Not sure if this will help but this is how I handle it.


PHP Code:
text kb.getText() if kb.isConfirmed() else None;

        if (
text == None or text == ''): 
            return 
None;

        else:
            return 
text


Basically in the else section I would add the AddDir. This might not be the best solution but it works for me. I have not ran into your issue where it sounds like it is always firing isConfirmed
Reply
#3
Thanks for the attention, but it still has not solved my problem!
Take the test, create a new addon with the example code I quoted above:

Then click on: 'My Search - Folder True', it will open the keyboard screen. Just close this screen without typing anything, Kodi will switch to a new empty directory ....

This is what I do not want to happen ...
Reply
#4
(2016-11-30, 03:11)Protocol-X Wrote:
Code:
if (text == None or text == ''):
            return None;

You shouldn't compare to None. CPython implementation guarantees that only one instance of None exists at runtime, so it is strongly recommenced to test identity, not equality. So the correct test is:
Code:
if text is None

As for the OP question, xbmcplugin.endOfDirectory accepts succeeded boolean parameter that can be used to signal to Kodi whether a virtual sub-directory has been resolved correctly or not.
Reply
#5
(2016-11-30, 14:22)Roman_V_M Wrote:
(2016-11-30, 03:11)Protocol-X Wrote:
Code:
if (text == None or text == ''):
            return None;

You shouldn't compare to None. CPython implementation guarantees that only one instance of None exists at runtime, so it is strongly recommenced to test identity, not equality. So the correct test is:
Code:
if text is None

As for the OP question, xbmcplugin.endOfDirectory accepts succeeded boolean parameter that can be used to signal to Kodi whether a virtual sub-directory has been resolved correctly or not.

Thanks for the catch. I normally use is or is not I had this wrong and corrected.
Reply
#6
My problem is not direct return of the keyboard object. As the "addDir" function creates an empty virtual directory, the solution I found was to populate this empty directory with the same information from the menu. In this way, the impression is left to the user that, when closing the keyboard window without doing any searching, it continues on the same screen as before.
Not ideal, but working

PHP Code:
def mySearch():
    
keyb xbmc.Keyboard('''XBMC Search')
    
keyb.doModal() 
    if 
keyb.isConfirmed() and len(keyb.getText().strip()) > 
        
search keyb.getText()
        
myParam str(urllib.quote(search)).strip()
        
addDir(myParam'myUrl'2'other.jpg'False)
    else:
        
#create "new" menu
        
CATEGORIES() 


Code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import urllib, urllib2, re, xbmc, xbmcplugin, xbmcgui, xbmc, xbmcaddon, HTMLParser, os
#=########################################################################################################=#
#                                                   MENU                                                   #
#=########################################################################################################=#
def CATEGORIES():
    addDir('My Search - Folder True', '-', 1, 'search.jpg', True)
###################################################################################
#=########################################################################################################=#
#                                                 FUNCTIONS                                                #
#=########################################################################################################=#
def mySearch():
    keyb = xbmc.Keyboard('', 'XBMC Search')
    keyb.doModal()
    if keyb.isConfirmed() and len(keyb.getText().strip()) > 0 :
        search = keyb.getText()
        myParam = str(urllib.quote(search)).strip()
        addDir(myParam, 'myUrl', 2, 'other.jpg', False)
    else:
        CATEGORIES()
    
def myOther():
    dialog = xbmcgui.Dialog()
    ok = dialog.ok('XBMC', 'Hello World')
    
def addDir(name, url, mode, iconimage, pasta=True, total=1):
    u=sys.argv[0]+'?url='+urllib.quote_plus(url)+'&mode='+str(mode)+'&name='+urllib.quote_plus(name)
    ok = True
    liz = xbmcgui.ListItem(name, iconImage='DefaultFolder.png', thumbnailImage=iconimage)
    ok = xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=u, listitem=liz, isFolder=pasta, totalItems=total)
    return ok
###################################################################################
#=########################################################################################################=#
#                                               GET PARAMS                                                 #
#=########################################################################################################=#    
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()
url = None
name = None
mode = None
iconimage = None

try:
    url = urllib.unquote_plus(params['url'])
except:
    pass
try:
    name = urllib.unquote_plus(params['name'])
except:
    pass
try:
    mode = int(params['mode'])
except:
    pass
try:        
    iconimage = urllib.unquote_plus(params['iconimage'])
except:
    pass
###################################################################################
#=###########################################################################################################=#
#                                                   MODES                                                     #
#=###########################################################################################################=#
if mode == None or url == None or len(url) < 1 :
    CATEGORIES()
elif mode == 1 :
    mySearch()
elif mode == 2 :
    myOther()
###################################################################################
xbmcplugin.endOfDirectory(int(sys.argv[1]))
Reply

Logout Mark Read Team Forum Stats Members Help
xbmc.Keyboard -- addDir ?0