summaryrefslogtreecommitdiff
path: root/alot/commands/search.py
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2011-10-08 21:49:40 +0100
committerPatrick Totzke <patricktotzke@gmail.com>2011-10-08 21:49:40 +0100
commitdf3119df31b720219394c449454f373aa900bcb9 (patch)
tree1fefb24975af3aebce7175c699762fa3b73db600 /alot/commands/search.py
parentacd0a5689c57ad29c1eeec4e5699e8eefb826a85 (diff)
sorted commands into separate files
Diffstat (limited to 'alot/commands/search.py')
-rw-r--r--alot/commands/search.py135
1 files changed, 135 insertions, 0 deletions
diff --git a/alot/commands/search.py b/alot/commands/search.py
new file mode 100644
index 00000000..e30eb30f
--- /dev/null
+++ b/alot/commands/search.py
@@ -0,0 +1,135 @@
+from commands import Command, registerCommand
+from twisted.internet import defer
+
+class RetagPromptCommand(Command):
+ """start a commandprompt to retag selected threads' tags
+ this is needed to fill the prompt with the current tags..
+ """
+ def apply(self, ui):
+ thread = ui.current_buffer.get_selected_thread()
+ if not thread:
+ return
+ initial_tagstring = ','.join(thread.get_tags())
+ ui.commandprompt('retag ' + initial_tagstring)
+
+
+class RetagCommand(Command):
+ """tag selected thread"""
+ def __init__(self, tagsstring=u'', thread=None, **kwargs):
+ self.tagsstring = tagsstring
+ self.thread = thread
+ Command.__init__(self, **kwargs)
+
+ def apply(self, ui):
+ if not self.thread:
+ self.thread = ui.current_buffer.get_selected_thread()
+ if not self.thread:
+ return
+ tags = filter(lambda x: x, self.tagsstring.split(','))
+ ui.logger.info("got %s:%s" % (self.tagsstring, tags))
+ try:
+ self.thread.set_tags(tags)
+ except DatabaseROError:
+ ui.notify('index in read-only mode', priority='error')
+ return
+
+ # flush index
+ ui.apply_command(FlushCommand())
+
+ # refresh selected threadline
+ sbuffer = ui.current_buffer
+ threadwidget = sbuffer.get_selected_threadline()
+ threadwidget.rebuild() # rebuild and redraw the line
+
+
+class RefineCommand(Command):
+ """refine the query of the currently open searchbuffer"""
+ def __init__(self, query=None, **kwargs):
+ self.querystring = query
+ Command.__init__(self, **kwargs)
+
+ @defer.inlineCallbacks
+ def apply(self, ui):
+ if self.querystring:
+ if self.querystring == '*':
+ s = 'really search for all threads? This takes a while..'
+ if (yield ui.choice(s, select='yes', cancel='no')) == 'no':
+ return
+ sbuffer = ui.current_buffer
+ oldquery = sbuffer.querystring
+ if self.querystring not in [None, oldquery]:
+ sbuffer.querystring = self.querystring
+ sbuffer = ui.current_buffer
+ sbuffer.rebuild()
+ ui.update()
+ else:
+ ui.notify('empty query string')
+
+
+class RefinePromptCommand(Command):
+ """prompt to change current search buffers query"""
+ def apply(self, ui):
+ sbuffer = ui.current_buffer
+ oldquery = sbuffer.querystring
+ ui.commandprompt('refine ' + oldquery)
+
+
+class ToggleThreadTagCommand(Command):
+ """toggles tag in given or currently selected thread"""
+ def __init__(self, tags, thread=None, **kwargs):
+ assert tags
+ self.thread = thread
+ self.tags = set(tags)
+ Command.__init__(self, **kwargs)
+
+ def apply(self, ui):
+ if not self.thread:
+ self.thread = ui.current_buffer.get_selected_thread()
+ if not self.thread:
+ return
+ try:
+ self.thread.set_tags(set(self.thread.get_tags()) ^ self.tags)
+ except DatabaseROError:
+ ui.notify('index in read-only mode', priority='error')
+ return
+
+ # flush index
+ ui.apply_command(FlushCommand())
+
+ # update current buffer
+ # TODO: what if changes not yet flushed?
+ cb = ui.current_buffer
+ if isinstance(cb, buffer.SearchBuffer):
+ # refresh selected threadline
+ threadwidget = cb.get_selected_threadline()
+ threadwidget.rebuild() # rebuild and redraw the line
+ #remove line from searchlist if thread doesn't match the query
+ qs = "(%s) AND thread:%s" % (cb.querystring,
+ self.thread.get_thread_id())
+ if ui.dbman.count_messages(qs) == 0:
+ ui.logger.debug('remove: %s' % self.thread)
+ cb.threadlist.remove(threadwidget)
+ cb.result_count -= self.thread.get_total_messages()
+ ui.update()
+ elif isinstance(cb, buffer.ThreadBuffer):
+ pass
+
+
+class OpenThreadCommand(Command):
+ """open a new thread-view buffer"""
+ def __init__(self, thread=None, **kwargs):
+ self.thread = thread
+ Command.__init__(self, **kwargs)
+
+ def apply(self, ui):
+ if not self.thread:
+ self.thread = ui.current_buffer.get_selected_thread()
+ if self.thread:
+ query = ui.current_buffer.querystring
+ ui.logger.info('open thread view for %s' % self.thread)
+
+ sb = buffer.ThreadBuffer(ui, self.thread)
+ ui.buffer_open(sb)
+ sb.unfold_matching(query)
+
+