~mhall119/onehundredscopes/loco-team-portal

« back to all changes in this revision

Viewing changes to ltp-lens

  • Committer: Michael Hall
  • Date: 2011-12-21 02:40:57 UTC
  • Revision ID: mhall119@ubuntu.com-20111221024057-mzv7rwg776kw5jk1
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/python
 
2
 
 
3
#    Copyright (c) 2011 David Calle <davidc@framli.eu>
 
4
#    Copyright (c) 2011 Michael Hall <mhall119@gmail.com>
 
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
 
 
19
import sys
 
20
import os
 
21
import datetime
 
22
import locodir
 
23
 
 
24
from gi.repository import GLib, GObject, Gio
 
25
from gi.repository import Dee
 
26
# FIXME: Some weird bug in Dee or PyGI makes Dee fail unless we probe
 
27
#        it *before* we import the Unity module... ?!
 
28
_m = dir(Dee.SequenceModel)
 
29
from gi.repository import Unity
 
30
 
 
31
BUS_NAME = "net.launchpad.lens.ltp"
 
32
 
 
33
LTP_TEAMS = 0
 
34
LTP_EVENTS = 1
 
35
LTP_MEETINGS = 2
 
36
LTP_VENUES = 3
 
37
 
 
38
class Daemon:
 
39
 
 
40
    def __init__ (self):
 
41
        # The path for the Lens *must* also match the one in our .lens file
 
42
        self._lens = Unity.Lens.new ("/net/launchpad/lens/ltp", "ltp")
 
43
        self._scope = Unity.Scope.new ("/net/launchpad/lens/ltp/main")
 
44
        self._scope.connect ("notify::active-search", self.on_search_changed)
 
45
        self._scope.connect ("filters-changed", self.on_search_changed);
 
46
        self._scope.connect ("notify::active", self.on_search_changed);
 
47
        #self._scope.connect ("activate-uri", self.on_activate_uri);
 
48
        self._scope.export()
 
49
 
 
50
        self._lens.props.search_hint = "Search LoCo Teams & Activity"
 
51
        self._lens.props.visible = True;
 
52
        self._lens.props.search_in_global = False;
 
53
        
 
54
        # Populate categories
 
55
        cats = []
 
56
        cats.append (Unity.Category.new ("Teams",
 
57
                                         Gio.ThemedIcon.new("ubuntu-logo"),
 
58
                                         Unity.CategoryRenderer.VERTICAL_TILE))
 
59
 
 
60
        cats.append (Unity.Category.new ("Events",
 
61
                                         Gio.ThemedIcon.new("calendar"),
 
62
                                         Unity.CategoryRenderer.HORIZONTAL_TILE))
 
63
 
 
64
        cats.append (Unity.Category.new ("Meetings",
 
65
                                         Gio.ThemedIcon.new("applications-chat"),
 
66
                                         Unity.CategoryRenderer.HORIZONTAL_TILE))
 
67
 
 
68
        cats.append (Unity.Category.new ("Venues",
 
69
                                         Gio.ThemedIcon.new("stock_home"),
 
70
                                         Unity.CategoryRenderer.HORIZONTAL_TILE))
 
71
 
 
72
        self._lens.props.categories = cats
 
73
        
 
74
        # Populate filters
 
75
        filters = []
 
76
        self._lens.props.filters = filters
 
77
 
 
78
        self._lens.add_local_scope (self._scope);
 
79
        self._lens.export ()
 
80
 
 
81
        self._ltp = locodir.LocoDirectory()
 
82
 
 
83
    def thaw(self, entry):
 
84
        entry.thaw_notify()
 
85
        return False
 
86
 
 
87
    def on_search_changed (self, entry, *args):
 
88
        print "on_search_changed"
 
89
        # Prevent concurrent searches and concurrent updates of our models,
 
90
        # by preventing any notify signals from propagating to us.
 
91
        # Important: Remember to thaw the notifys again! 
 
92
        entry.freeze_notify ()
 
93
        search = self._scope.props.active_search or None
 
94
        if search:
 
95
            search_string = search.props.search_string or None
 
96
        else:
 
97
            search_string = None
 
98
        results = self._scope.props.results_model
 
99
        self.update_results_model (search_string, results)
 
100
        GObject.idle_add(self.thaw, entry)
 
101
 
 
102
#
 
103
# Update results model
 
104
#
 
105
    def update_results_model(self, search, model):
 
106
        print "update_results_model"
 
107
        model.clear()
 
108
        if search != '' and search:
 
109
 
 
110
            # Teams by name
 
111
            for team in self._ltp.getTeams(name__icontains=search).values():
 
112
                model.append('http://loco.ubuntu.com/teams/%s/' % team['lp_name'],
 
113
                             team['mugshot_url'],
 
114
                             LTP_TEAMS,
 
115
                             "text/html",
 
116
                             team['name'], team['lp_name'], '')
 
117
 
 
118
                # Events by team
 
119
                for tevent in self._ltp.getTeamEvents(teams=team['id'], date_end__gte=datetime.datetime.utcnow()).values():
 
120
                    model.append('http://loco.ubuntu.com/events/%s/%s/detail/' % (team['lp_name'], tevent['id']),
 
121
                                 team['mugshot_url'],
 
122
                                 LTP_EVENTS,
 
123
                                 "text/html",
 
124
                                 tevent['name'], '%s\n%s' % (tevent['date_begin'], tevent['description']), '')
 
125
 
 
126
                # Meetings by team
 
127
                print "Looking for meetings for %s" % team['name']
 
128
                for tmeeting in self._ltp.getTeamMeetings(teams=team['id'], date_end__gte=datetime.datetime.utcnow()).values():
 
129
                    model.append('http://loco.ubuntu.com/meeting/%s/%s/detail/' % (team['lp_name'], tmeeting['id']),
 
130
                                 team['mugshot_url'],
 
131
                                 LTP_MEETINGS,
 
132
                                 "text/html",
 
133
                                 tmeeting['name'], tmeeting['date_begin'], '')
 
134
                 
 
135
            
 
136
            # Events by name
 
137
            for tevent in self._ltp.getTeamEvents(name__icontains=search, date_end__gte=datetime.datetime.utcnow()).values():
 
138
                team = self._ltp.getTeam(tevent['teams'][0])
 
139
                model.append('http://loco.ubuntu.com/events/%s/%s/detail/' % (team['lp_name'], tevent['id']),
 
140
                             team['mugshot_url'],
 
141
                             LTP_EVENTS,
 
142
                             "text/html",
 
143
                             tevent['name'], '%s\n%s' % (tevent['date_begin'], tevent['description']), '')
 
144
            
 
145
            # Meetings by name
 
146
            for tmeeting in self._ltp.getTeamMeetings(name__icontains=search, date_end__gte=datetime.datetime.utcnow()).values():
 
147
                team = self._ltp.getTeam(tevent['teams'][0])
 
148
                model.append('http://loco.ubuntu.com/meeting/%s/%s/detail/' % (team['lp_name'], tmeeting['id']),
 
149
                             team['mugshot_url'],
 
150
                             LTP_MEETINGS,
 
151
                             "text/html",
 
152
                             tmeeting['name'], tmeeting['date_begin'], '')
 
153
 
 
154
        else:
 
155
            # Display all teams
 
156
            for team in self._ltp.getTeams().values():
 
157
                model.append('http://loco.ubuntu.com/teams/%s/' % team['lp_name'],
 
158
                             team['mugshot_url'],
 
159
                             LTP_TEAMS,
 
160
                             "text/html",
 
161
                             team['name'], team['lp_name'], '')
 
162
 
 
163
if __name__ == "__main__":
 
164
    session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
 
165
    session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None,
 
166
                                          'org.freedesktop.DBus',
 
167
                                          '/org/freedesktop/DBus',
 
168
                                          'org.freedesktop.DBus', None)
 
169
    result = session_bus.call_sync('RequestName',
 
170
                                   GLib.Variant ("(su)", (BUS_NAME, 0x4)),
 
171
                                   0, -1, None)
 
172
                                   
 
173
    # Unpack variant response with signature "(u)". 1 means we got it.
 
174
    result = result.unpack()[0]
 
175
    
 
176
    if result != 1 :
 
177
        print >> sys.stderr, "Failed to own name %s. Bailing out." % BUS_NAME
 
178
        raise SystemExit (1)
 
179
    
 
180
    daemon = Daemon()
 
181
    GObject.MainLoop().run()