/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to search.py

  • Committer: Jelmer Vernooij
  • Date: 2011-04-10 18:44:39 UTC
  • mto: This revision was merged to the branch mainline in revision 730.
  • Revision ID: jelmer@samba.org-20110410184439-g7hqaacexqtviq13
Move i18n support to a separate file, so gettext files aren't loaded unless bzr-gtk is used.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 by Jelmer Vernooij <jelmer@samba.org>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
try:
 
18
    import pygtk
 
19
    pygtk.require("2.0")
 
20
except:
 
21
    pass
 
22
 
 
23
import gobject
 
24
import gtk
 
25
 
 
26
from bzrlib.plugins.search import index as _mod_index
 
27
 
 
28
from bzrlib.plugins.gtk.i18n import _i18n
 
29
 
 
30
class SearchDialog(gtk.Dialog):
 
31
    """Search dialog."""
 
32
    def __init__(self, index, parent=None):
 
33
        gtk.Dialog.__init__(self, title="Search Revisions",
 
34
                                  parent=parent,
 
35
                                  flags=gtk.DIALOG_MODAL,
 
36
                                  buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
 
37
                                           gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
 
38
        pixbuf = self.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_MENU)
 
39
        self.set_icon(pixbuf)
 
40
        
 
41
        # Get arguments
 
42
        self.index = index
 
43
 
 
44
        self.searchbar = gtk.HBox()
 
45
        searchbar_label = gtk.Label(_i18n("Search for:"))
 
46
        self.searchbar.pack_start(searchbar_label, False, False, 0)
 
47
        self.searchentry = gtk.Entry()
 
48
        self.searchentry.connect('activate', self._searchentry_activate)
 
49
        # TODO: Completion using the bzr-search suggests functionality
 
50
        self.searchbar.add(self.searchentry)
 
51
        self.vbox.pack_start(self.searchbar, expand=False, fill=False)
 
52
 
 
53
        self.results_model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
 
54
        self.results_treeview = gtk.TreeView(self.results_model)
 
55
        self.results_treeview.connect("row-activated", self._searchresult_row_activated)
 
56
 
 
57
        documentname_column = gtk.TreeViewColumn(_i18n("Document"), gtk.CellRendererText(), text=0)
 
58
        self.results_treeview.append_column(documentname_column)
 
59
 
 
60
        summary_column = gtk.TreeViewColumn(_i18n("Summary"), gtk.CellRendererText(), text=1)
 
61
        self.results_treeview.append_column(summary_column)
 
62
 
 
63
        results_scrolledwindow = gtk.ScrolledWindow()
 
64
        results_scrolledwindow.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
 
65
        results_scrolledwindow.add(self.results_treeview)
 
66
 
 
67
        self.vbox.pack_start(results_scrolledwindow, expand=True, fill=True)
 
68
 
 
69
        self.set_default_size(600, 400)
 
70
        # Show the dialog
 
71
        self.show_all()
 
72
 
 
73
    def get_revision(self):
 
74
        (path, focus) = self.results_treeview.get_cursor()
 
75
        if path is None:
 
76
            return None
 
77
        iter = self.results_model.get_iter(path)
 
78
        return self.results_model.get_value(iter, 2)
 
79
 
 
80
    def _searchentry_activate(self, entry):
 
81
        self.results_model.clear()
 
82
        self.index._branch.lock_read()
 
83
        try:
 
84
            query = [(query_item,) for query_item in self.searchentry.get_text().split(" ")]
 
85
            for result in self.index.search(query):
 
86
                if isinstance(result, _mod_index.FileTextHit):
 
87
                    revid = result.text_key[-1]
 
88
                elif isinstance(result, _mod_index.RevisionHit):
 
89
                    revid = result.revision_key[0]
 
90
                else:
 
91
                    raise AssertionError()
 
92
                self.results_model.append([result.document_name(), result.summary(), revid])
 
93
        finally:
 
94
            self.index._branch.unlock()
 
95
    
 
96
    def _searchresult_row_activated(self, treeview, path, view_column):
 
97
        self.response(gtk.RESPONSE_OK)