Multithreaded search with multiple keywords

This commit is contained in:
Clement Lefebvre 2010-09-12 12:21:47 +01:00
parent 18e9c6616a
commit dc39d0783e
2 changed files with 148 additions and 52 deletions

7
debian/changelog vendored
View File

@ -1,3 +1,10 @@
mintmenu (5.0.5) julia; urgency=low
* Multi-threaded search
* Multiple keywords search
-- Clement Lefebvre <root@linuxmint.com> Sun, 12 Sep 2010 12:21:00 +0000
mintmenu (5.0.4) julia; urgency=low mintmenu (5.0.4) julia; urgency=low
* Don't break menu if apt fails to initialize * Don't break menu if apt fails to initialize

View File

@ -29,6 +29,8 @@ import apt
from user import home from user import home
gtk.gdk.threads_init()
# i18n # i18n
gettext.install("mintmenu", "/usr/share/linuxmint/locale") gettext.install("mintmenu", "/usr/share/linuxmint/locale")
@ -649,20 +651,134 @@ class pluginclass( object ):
self.applicationsBox.add(suggestionButton) self.applicationsBox.add(suggestionButton)
self.suggestions.append(suggestionButton) self.suggestions.append(suggestionButton)
self.last_separator = gtk.EventBox() #self.last_separator = gtk.EventBox()
self.last_separator.add(gtk.HSeparator()) #self.last_separator.add(gtk.HSeparator())
self.last_separator.set_size_request(-1, 20) #self.last_separator.set_size_request(-1, 20)
self.last_separator.type = "separator" #self.last_separator.type = "separator"
self.mintMenuWin.SetPaneColors( [ self.last_separator ] ) #self.mintMenuWin.SetPaneColors( [ self.last_separator ] )
self.last_separator.show_all() #self.last_separator.show_all()
self.applicationsBox.add(self.last_separator) #self.applicationsBox.add(self.last_separator)
self.suggestions.append(self.last_separator) #self.suggestions.append(self.last_separator)
def add_apt_filter_results(self, cache, keyword):
try:
# Wait to see if the keyword has changed.. before doing anything
time.sleep(0.3)
current_keyword = keyword
gtk.gdk.threads_enter()
try:
current_keyword = self.searchEntry.get_text()
finally:
gtk.gdk.threads_leave()
if keyword != current_keyword:
return
found_packages = []
keywords = keyword.split(" ")
i = 0
if cache is not None:
for pkg in cache:
i = i +1
if i%1000==0:
time.sleep(0.01)
some_found = False
some_not_found = False
for word in keywords:
if word in pkg.name:
some_found = True
else:
some_not_found = True
if some_found and not some_not_found:
found_packages.append(pkg)
gtk.gdk.threads_enter()
try:
if keyword == self.searchEntry.get_text() and len(found_packages) > 0:
last_separator = gtk.EventBox()
last_separator.add(gtk.HSeparator())
last_separator.set_size_request(-1, 20)
last_separator.type = "separator"
self.mintMenuWin.SetPaneColors( [ last_separator ] )
last_separator.show_all()
self.applicationsBox.add(last_separator)
self.suggestions.append(last_separator)
for pkg in found_packages:
name = pkg.name
for word in keywords:
if word != "":
name = name.replace(word, "<b>%s</b>" % word);
suggestionButton = SuggestionButton(gtk.STOCK_ADD, self.iconSize, "")
suggestionButton.connect("clicked", self.apturl_install, pkg.name)
suggestionButton.set_text(_("Install package '%s'") % name)
suggestionButton.set_tooltip_text("%s\n\n%s\n\n%s" % (pkg.name, pkg.summary.capitalize(), pkg.description))
suggestionButton.set_icon_size(self.iconSize)
self.applicationsBox.add(suggestionButton)
self.suggestions.append(suggestionButton)
if cache != self.current_results:
self.current_results.append(pkg)
finally:
gtk.gdk.threads_leave()
#if len(found_packages) == 0:
# gtk.gdk.threads_enter()
# try:
# self.applicationsBox.remove(self.last_separator)
# self.suggestions.remove(self.last_separator)
# finally:
# gtk.gdk.threads_leave()
except Exception, detail:
print detail
def add_apt_filter_results_sync(self, cache, keyword):
try:
found_packages = []
keywords = keyword.split(" ")
if cache is not None:
for pkg in cache:
some_found = False
some_not_found = False
for word in keywords:
if word in pkg.name:
some_found = True
else:
some_not_found = True
if some_found and not some_not_found:
found_packages.append(pkg)
if len(found_packages) > 0:
last_separator = gtk.EventBox()
last_separator.add(gtk.HSeparator())
last_separator.set_size_request(-1, 20)
last_separator.type = "separator"
self.mintMenuWin.SetPaneColors( [ last_separator ] )
last_separator.show_all()
self.applicationsBox.add(last_separator)
self.suggestions.append(last_separator)
for pkg in found_packages:
name = pkg.name
for word in keywords:
if word != "":
name = name.replace(word, "<b>%s</b>" % word);
suggestionButton = SuggestionButton(gtk.STOCK_ADD, self.iconSize, "")
suggestionButton.connect("clicked", self.apturl_install, pkg.name)
suggestionButton.set_text(_("Install package '%s'") % name)
suggestionButton.set_tooltip_text("%s\n\n%s\n\n%s" % (pkg.name, pkg.summary.capitalize(), pkg.description))
suggestionButton.set_icon_size(self.iconSize)
self.applicationsBox.add(suggestionButton)
self.suggestions.append(suggestionButton)
#if len(found_packages) == 0:
# self.applicationsBox.remove(self.last_separator)
# self.suggestions.remove(self.last_separator)
except Exception, detail:
print detail
def Filter( self, widget, category = None ): def Filter( self, widget, category = None ):
self.filterTimer = None self.filterTimer = None
start = time.time()
#print "FILTER"
for suggestion in self.suggestions: for suggestion in self.suggestions:
self.applicationsBox.remove(suggestion) self.applicationsBox.remove(suggestion)
self.suggestions = [] self.suggestions = []
@ -684,42 +800,16 @@ class pluginclass( object ):
if self.current_suggestion is not None and self.current_suggestion in text: if self.current_suggestion is not None and self.current_suggestion in text:
# We're restricting our search... # We're restricting our search...
self.add_search_suggestions(text) self.add_search_suggestions(text)
found_packages = 0 if (len(self.current_results) > 0):
for pkg in self.current_results: self.add_apt_filter_results_sync(self.current_results, text)
if text in pkg.name: else:
found_packages+=1 thr = threading.Thread(name="mint-menu-apt-filter", group=None, target=self.add_apt_filter_results, args=(self.apt_cache, text), kwargs={})
name = pkg.name.replace(text, "<b>%s</b>" % text); thr.start()
suggestionButton = SuggestionButton(gtk.STOCK_ADD, self.iconSize, "")
suggestionButton.connect("clicked", self.apturl_install, pkg.name)
suggestionButton.set_text(_("Install package '%s'") % name)
suggestionButton.set_tooltip_text("%s\n\n%s\n\n\n%s" % (pkg.name, pkg.summary.capitalize(), pkg.description))
suggestionButton.set_icon_size(self.iconSize)
self.applicationsBox.add(suggestionButton)
self.suggestions.append(suggestionButton)
if found_packages == 0:
self.applicationsBox.remove(self.last_separator)
self.suggestions.remove(self.last_separator)
else: else:
self.current_results = [] self.current_results = []
self.add_search_suggestions(text) self.add_search_suggestions(text)
found_packages = 0 thr = threading.Thread(name="mint-menu-apt-filter", group=None, target=self.add_apt_filter_results, args=(self.apt_cache, text), kwargs={})
if self.apt_cache is not None: thr.start()
for pkg in self.apt_cache:
if text in pkg.name:
found_packages+=1
name = pkg.name.replace(text, "<b>%s</b>" % text);
suggestionButton = SuggestionButton(gtk.STOCK_ADD, self.iconSize, "")
suggestionButton.connect("clicked", self.apturl_install, pkg.name)
suggestionButton.set_text(_("Install package '%s'") % name)
suggestionButton.set_tooltip_text("%s\n\n%s\n\n%s" % (pkg.name, pkg.summary.capitalize(), pkg.description))
suggestionButton.set_icon_size(self.iconSize)
self.applicationsBox.add(suggestionButton)
self.suggestions.append(suggestionButton)
self.current_results.append(pkg)
if found_packages == 0:
self.applicationsBox.remove(self.last_separator)
self.suggestions.remove(self.last_separator)
self.current_suggestion = text self.current_suggestion = text
else: else:
self.current_suggestion = None self.current_suggestion = None
@ -754,7 +844,6 @@ class pluginclass( object ):
self.searchEntry.set_text( "" ) self.searchEntry.set_text( "" )
self.applicationsScrolledWindow.get_vadjustment().set_value( 0 ) self.applicationsScrolledWindow.get_vadjustment().set_value( 0 )
#print u"Filtertime: ", (time.time() - start), "s"
# Forward all text to the search box # Forward all text to the search box
def keyPress( self, widget, event ): def keyPress( self, widget, event ):