2
# -*- coding: latin-1 -*-
4
#Copyright (c) 2011 David Calle <davidc@framli.eu>
6
#This program is free software: you can redistribute it and/or modify
7
#it under the terms of the GNU General Public License as published by
8
#the Free Software Foundation, either version 3 of the License, or
9
#(at your option) any later version.
11
#This program is distributed in the hope that it will be useful,
12
#but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
#GNU General Public License for more details.
16
#You should have received a copy of the GNU General Public License
17
#along with this program. If not, see <http://www.gnu.org/licenses/>.
19
from gi.repository import GLib, GObject, Gio
20
from gi.repository import Dee
21
_m = dir(Dee.SequenceModel)
22
from gi.repository import Unity
25
socket.setdefaulttimeout(5)
26
BUS_NAME = "net.launchpad.scope.math.calculator"
30
self.scope = Unity.Scope.new ("/net/launchpad/scope/math/calculator")
31
self.scope.search_in_global = False
32
self.scope.connect ("notify::active-search", self.on_search_changed)
33
self.scope.connect ("notify::active-global-search", self.on_search_changed)
34
self.scope.connect ("notify::active", self.on_search_changed)
35
self.scope.connect ("filters-changed", self.on_search_changed);
38
def get_search_string (self):
39
search = self.scope.props.active_global_search
41
search = self.scope.props.active_search
42
return search.props.search_string if search else None
44
def on_search_changed (self, scope, param_spec=None):
45
search = self.get_search_string()
46
print "Search changed to: '%s'" % search
47
results_global = self.scope.props.global_results_model
48
results_global.clear()
49
self.update_results_model (search, results_global)
50
results_global.flush_revision_queue ()
52
def update_results_model(self, search, model):
53
self.calc(search,model)
55
def calc(self, search, model):
59
icon = "gnome-calculator"
61
search = search.replace('(','\(')
62
search = search.replace(')','\)')
63
search = search.replace(',','.')
64
search = search.replace('pi','π')
65
search = search.replace('"','')
66
search = search.replace('\'','')
67
search = search.replace('&','')
68
search = search.replace('|','')
71
hint = hint.replace('\(','(')
72
hint = hint.replace('\)',')')
73
hint = hint.replace('sqrt','√')
75
calc_result=commands.getoutput('gcalctool -s %s' % str(search))
79
if not (calc_result.startswith("Argument")) and not (calc_result.startswith("Error")):
80
model.append('', icon, 0, "text/html", calc_result, hint, '')
83
if __name__ == "__main__":
84
session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
85
session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None,
86
'org.freedesktop.DBus',
87
'/org/freedesktop/DBus',
88
'org.freedesktop.DBus', None)
89
result = session_bus.call_sync('RequestName',
90
GLib.Variant ("(su)", (BUS_NAME, 0x4)),
93
# Unpack variant response with signature "(u)". 1 means we got it.
94
result = result.unpack()[0]
97
print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
101
GObject.MainLoop().run()