2
# -*- coding: utf-8 -*-
4
# Copyright(C) 2012 name <email>
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License along
15
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
from gi.repository import GLib, GObject, Gio, Gtk
18
from gi.repository import Unity, UnityExtras
24
APP_NAME = 'unity-scope-manpages'
25
LOCAL_PATH = '/usr/share/locale/'
26
gettext.bindtextdomain(APP_NAME, LOCAL_PATH)
27
gettext.textdomain(APP_NAME)
30
BUS_NAME = 'com.canonical.Unity.Scope.Development.Manpages'
31
BUS_PATH = '/com/canonical/unity/scope/development/manpages'
33
SVG_DIR = '/usr/share/icons/unity-icon-theme/places/svg/'
34
CAT_0_ICON = Gio.ThemedIcon.new(SVG_DIR + 'group-development.svg')
35
CAT_0_TITLE = _('Manpages')
37
NO_RESULTS_HINT = _('Sorry, there are no Manpages that match your search.')
38
SEARCH_HINT = _('Search Manpages')
44
Manpages Daemon sets up the lens & searches manpages on the system
45
for results matching query
49
self.scope = Unity.Scope.new(BUS_PATH, 'manpages')
50
self.scope.props.search_hint = SEARCH_HINT
51
self.scope.search_in_global = True
53
cats.append(Unity.Category.new('cat_0',
56
Unity.CategoryRenderer.VERTICAL_TILE))
57
self.scope.props.categories = cats
58
self.preferences = Unity.PreferencesManager.get_default()
59
self.preferences.connect('notify::remote-content-search', self._on_preference_changed)
60
self.scope.connect('search-changed', self.on_search_changed)
63
def _on_preference_changed(self, *_):
65
Called when a filter is clicked. Queues a new search
67
self.scope.queue_search_changed(Unity.SearchType.DEFAULT)
69
def on_search_changed(self, scope, search, search_type, *_):
71
Called when the search is changed. Gets the search string and search
72
type and passes it to update_results_model()
74
scope: the scope object
75
search: the search string
76
search_type: the search type (global or local)
78
model = search.props.results_model
80
if self.preferences.props.remote_content_search != Unity.PreferencesManagerRemoteContent.ALL:
83
search_string = search.props.search_string.strip()
84
print 'Search changed to \'%s\'' % search_string
85
self.update_results_model(search_string, model)
86
search.set_reply_hint('no-results-hint',
87
GLib.Variant.new_string(NO_RESULTS_HINT))
90
def update_results_model(self, search, results):
92
Searches for matching manpages and adds them to the results
94
icon_hint = Gio.ThemedIcon.new("text-x-generic").to_string()
96
if len(search) > 2: # don't run apropos for strings shorter than 3 chars
97
apropos = subprocess.Popen(["apropos", search], stdout=subprocess.PIPE)
98
os.waitpid(apropos.pid, 0)
99
out = apropos.communicate()[0]
100
for line in out.split("\n"):
101
regex_apropos = re.compile('^(.+?)\s+\((\d+)\)\s+-\s(.+?)$')
102
m = regex_apropos.match(line)
104
result.append((m.group(1), m.group(2), m.group(3)))
106
uri = 'man:' + res[0] + '(' + res[1] + ')'
107
icon_theme = Gtk.IconTheme()
108
icon_info = icon_theme.lookup_icon(res[0], 128, 0)
110
icon_hint = Gio.ThemedIcon.new(res[0]).to_string()
111
results.append(uri=uri,
114
mimetype='x-scheme-handler/man',
118
result_type=Unity.ResultType.DEFAULT)
120
if __name__ == '__main__':
121
daemon = UnityExtras.dbus_own_name(BUS_NAME, Daemon, None)
123
GLib.unix_signal_add_full(0, 2, lambda x: daemon.quit(), None)