~davidc3/onehundredscopes/scummvm

« back to all changes in this revision

Viewing changes to src/unity-scope-scummvm

  • Committer: David Call√©
  • Date: 2011-11-19 19:51:46 UTC
  • Revision ID: davidc@framli.eu-20111119195146-3k1124q830rlds68
Branch init

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.games.scummvm"
 
27
 
 
28
class Daemon:
 
29
        def __init__ (self):
 
30
                self.scope = Unity.Scope.new ("/net/launchpad/scope/games/scummvm")
 
31
                self.scope.search_in_global = False
 
32
                self.scope.connect ("notify::active-search", self.on_search_changed)
 
33
                self.scope.connect ("notify::active", self.on_search_changed)
 
34
                self.scope.connect ("filters-changed", self.on_search_changed);
 
35
                self.scope.connect ("activate-uri", self.on_click);
 
36
                self.scope.export()
 
37
 
 
38
        def on_click (self, scope, uri):
 
39
                from subprocess import call
 
40
                print uri
 
41
#               GLib.spawn_async([uri.split('---')[0],uri.split('---')[1]], flags=(GLib.SPAWN_SEARCH_PATH | GLib.SPAWN_DO_NOT_REAP_CHILD))
 
42
                call("scummvm %s" % uri , shell=True)
 
43
                return Unity.ActivationResponse.new(Unity.HandledType.HIDE_DASH, uri)
 
44
 
 
45
        def get_search_string (self):
 
46
                search = self.scope.props.active_search
 
47
                return search.props.search_string if search else None
 
48
 
 
49
        def on_search_changed (self, scope, param_spec=None):
 
50
                        search = self.get_search_string()
 
51
                        print "Search changed to: '%s'" % search
 
52
                        results = self.scope.props.results_model
 
53
                        results.clear()
 
54
                        self.update_results_model (search, results)
 
55
                        results.flush_revision_queue ()
 
56
 
 
57
        def update_results_model(self, search, model):
 
58
                if not self.check_filters("gameshelf") or self.check_filters("gameshelf") == "scummvm":
 
59
                        for i in self.scummvm(search):
 
60
                                title = i[0]
 
61
                                comment = ""
 
62
                                uri = i[1]
 
63
#                               uri = GLib.spawn_async(i[1], flags=GLib.SPAWN_SEARCH_PATH)
 
64
                                icon_hint = i[2]
 
65
                                model.append (uri, icon_hint, 0,"application-x-desktop", title, comment, uri)
 
66
#                               model.append (uri, icon_hint, 1,"text/html", title, "A game of fun and mayhem...", uri)
 
67
                                if self.scope.props.active_search:
 
68
                                        self.scope.props.active_search.emit("finished")
 
69
 
 
70
        def check_filters(self, filter_name):
 
71
                try:
 
72
                        filter = self.scope.get_filter(filter_name).get_active_option().props.id
 
73
                except (AttributeError):
 
74
                        filter = None
 
75
                return filter
 
76
 
 
77
        def scummvm(self, search):
 
78
                id_list = []
 
79
                description_list = []
 
80
                game_list = []
 
81
                data_list = []
 
82
                rc = "/home/"+ os.getenv('USER') +"/.scummvmrc"
 
83
                if os.path.exists(rc):
 
84
                        for line in open(rc).xreadlines(): 
 
85
                                if "gameid=" in line:
 
86
                                        game_id = line.split("=")[1].replace("\n", "")
 
87
                                        id_list.append(game_id)
 
88
                                if "description=" in line:
 
89
                                        game_description = line.split("=")[1].replace("\n", "").split("(")[0]
 
90
                                        
 
91
                                        description_list.append(game_description)
 
92
                i = 0
 
93
                for id in id_list:
 
94
                        if search is None or search is '' or description_list[i].lower().find(search.lower())  > -1:
 
95
                                item_list = []
 
96
                                icon_hint = ''
 
97
                                for image in self.google_image_search("cover %s" % description_list[i]):
 
98
                                        icon_hint = image['tbUrl']
 
99
                                item_list.append(description_list[i])
 
100
                                print description_list[i]
 
101
                                item_list.append(id_list[i])
 
102
                                item_list.append(icon_hint)
 
103
                                i = i+1
 
104
                                data_list.append(item_list)
 
105
                return data_list
 
106
 
 
107
        # Google Images search
 
108
        # <http://code.google.com/apis/imagesearch/v1/jsondevguide.html>
 
109
        #
 
110
        def google_image_search(self, q):
 
111
                import urllib, urllib2, simplejson
 
112
                try:
 
113
                        q = q.replace(" ", "+")
 
114
                        query = urllib.urlencode({'q': q.encode('utf-8') })
 
115
                        if query != '' or query != 'q=' or query != 'q':
 
116
                                url = ('https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=1&safe=active&%s'
 
117
                                  % (query))
 
118
                                print url
 
119
                                f = urllib2.urlopen(url)
 
120
                                content = f.read()
 
121
                                results = simplejson.loads(content)
 
122
                                print "google_image_search " + str(results['responseStatus'])
 
123
                                if results['responseStatus'] == 200:
 
124
                                        return results['responseData']['results']
 
125
                                else:
 
126
                                        pass
 
127
                except (IOError, urllib2.URLError, urllib2.HTTPError, simplejson.JSONDecodeError):
 
128
                        print "google_image_search_is_error"
 
129
 
 
130
 
 
131
if __name__ == "__main__":
 
132
        session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
 
133
        session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None,
 
134
          'org.freedesktop.DBus',
 
135
          '/org/freedesktop/DBus',
 
136
          'org.freedesktop.DBus', None)
 
137
        result = session_bus.call_sync('RequestName',
 
138
           GLib.Variant ("(su)", (BUS_NAME, 0x4)),
 
139
           0, -1, None)
 
140
           
 
141
        # Unpack variant response with signature "(u)". 1 means we got it.
 
142
        result = result.unpack()[0]
 
143
        
 
144
        if result != 1 :
 
145
                print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
 
146
                raise SystemExit (1)
 
147
        
 
148
        daemon = Daemon()
 
149
        GObject.MainLoop().run()