~davidc3/onehundredscopes/calculator-raring

« back to all changes in this revision

Viewing changes to src/unity-scope-calculator

  • Committer: David Callé
  • Date: 2011-11-20 20:07:56 UTC
  • Revision ID: davidc@framli.eu-20111120200756-d9sie94zx0hfcfb5
Branch init + packaging

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
# -*- coding: latin-1 -*-
 
3
 
 
4
#Copyright (c) 2011 David Calle <davidc@framli.eu>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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/>.
 
18
import sys, os
 
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
 
23
import socket
 
24
 
 
25
socket.setdefaulttimeout(5)
 
26
BUS_NAME = "net.launchpad.scope.math.calculator"
 
27
 
 
28
class Daemon:
 
29
        def __init__ (self):
 
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);
 
36
                self.scope.export()
 
37
 
 
38
        def get_search_string (self):
 
39
                search = self.scope.props.active_global_search
 
40
                if search is None:
 
41
                        search = self.scope.props.active_search
 
42
                return search.props.search_string if search else None
 
43
 
 
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 ()
 
51
 
 
52
        def update_results_model(self, search, model):
 
53
                self.calc(search,model)
 
54
 
 
55
        def calc(self, search, model):
 
56
                import commands
 
57
                print "calc!"
 
58
 
 
59
                icon = "gnome-calculator"
 
60
 
 
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('|','')
 
69
 
 
70
                hint = search
 
71
                hint = hint.replace('\(','(')
 
72
                hint = hint.replace('\)',')')
 
73
                hint = hint.replace('sqrt','√')
 
74
 
 
75
                calc_result=commands.getoutput('gcalctool -s %s' % str(search))
 
76
                print search
 
77
                print calc_result
 
78
                if search:
 
79
                        if not (calc_result.startswith("Argument")) and not (calc_result.startswith("Error")):
 
80
                                model.append('', icon, 0, "text/html", calc_result, hint, '')
 
81
 
 
82
 
 
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)),
 
91
           0, -1, None)
 
92
           
 
93
        # Unpack variant response with signature "(u)". 1 means we got it.
 
94
        result = result.unpack()[0]
 
95
        
 
96
        if result != 1 :
 
97
                print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
 
98
                raise SystemExit (1)
 
99
        
 
100
        daemon = Daemon()
 
101
        GObject.MainLoop().run()