Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
abort threads associated to view if view gets hidden.
Browse files Browse the repository at this point in the history
in case of the search view, also abort threads associated to a previous search.
closes #243
  • Loading branch information
mfrasca committed Dec 29, 2015
1 parent 941af80 commit 393b154
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
6 changes: 6 additions & 0 deletions bauble/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ def __init__(self, filename, parent=None, root_widget_name=None):
self.connect(window, 'response', self.on_dialog_response)
self.box = set() # the top level, meant for warnings.

def abort_threads(self):
pass

def update(self):
pass

def run_file_chooser_dialog(
self, text, parent, action, buttons, last_folder, target):
chooser = gtk.FileChooserDialog(text, parent, action, buttons)
Expand Down
6 changes: 6 additions & 0 deletions bauble/pluginmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,12 @@ def __init__(self, *args, **kwargs):
"""
super(View, self).__init__(*args, **kwargs)

def abort_threads(self):
pass

def update(self):
pass


class CommandHandler(object):

Expand Down
1 change: 1 addition & 0 deletions bauble/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ def set_view(self, view=None):
kid.set_visible(True)
else:
kid.set_visible(False)
kid.abort_threads()
if must_add_this_view:
view_box.pack_start(view, True, True, 0)
view.show_all()
Expand Down
21 changes: 18 additions & 3 deletions bauble/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ def __init__(self, klass, ids,
group=group, target=None, name=None, verbose=verbose)
self.klass = klass
self.ids = ids
self.abort = False

def run(self):
session = db.Session()
Expand All @@ -375,6 +376,8 @@ def run(self):
d = {}
for ndx in self.ids:
item = session.query(klass).filter(klass.id == ndx).one()
if self.abort: # check whether caller asks to abort
break
for k, v in item.top_level_count().items():
if isinstance(v, set):
d[k] = v.union(d.get(k, set()))
Expand All @@ -387,14 +390,17 @@ def run(self):
if isinstance(v, set):
v = len(v)
result.append("%s: %d" % (k, v))
if self.abort: # check whether caller asks to abort
break
value = _("top level count: %s") % (", ".join(result))
if bauble.gui:
def callback(text):
statusbar = bauble.gui.widgets.statusbar
sbcontext_id = statusbar.get_context_id('searchview.nresults')
statusbar.pop(sbcontext_id)
statusbar.push(sbcontext_id, text)
gobject.idle_add(callback, value)
if not self.abort: # check whether caller asks to abort
gobject.idle_add(callback, value)
else:
logger.debug("showing text %s", value)
## we should not leave the session around
Expand Down Expand Up @@ -498,6 +504,7 @@ def __init__(self):
# be cleared when we do a new search
self.session = db.Session()
self.add_notes_page_to_bottom_notebook()
self.still_counting = None

def add_notes_page_to_bottom_notebook(self):
'''add notebook page for notes
Expand Down Expand Up @@ -712,6 +719,11 @@ def _impl(*args):
logger.warning(
'Could not parse accelerator: %s' % (action.accelerator))

def abort_threads(self):
if self.still_counting:
self.still_counting.abort = True
self.still_counting = None

nresults_statusbar_context = 'searchview.nresults'

def search(self, text):
Expand All @@ -728,6 +740,8 @@ def search(self, text):
# create a new session for each search...maybe we shouldn't
# even have session as a class attribute
self.session = db.Session()
# stop whatever it might still be doing
self.abort_threads()
bold = '<b>%s</b>'
results = []
try:
Expand Down Expand Up @@ -786,8 +800,9 @@ def search(self, text):
statusbar.pop(sbcontext_id)
statusbar.push(sbcontext_id, _('counting results...'))
if len(set(item.__class__ for item in results)) == 1:
CountResultsTask(results[0].__class__,
[i.id for i in results]).start()
self.still_counting = CountResultsTask(
results[0].__class__, [i.id for i in results])
self.still_counting.start()
else:
statusbar.push(sbcontext_id,
_('size of non homogeneous result: %s') %
Expand Down

0 comments on commit 393b154

Please sign in to comment.