Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/model/homemodel.py
blob: f4fd3eec3f6c92f3e73cffc048a7a6a166d5e536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (C) 2006, Owen Williams.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import logging

import gobject
import wnck

from model.homeactivity import HomeActivity

class HomeModel(gobject.GObject):

    __gsignals__ = {
        'activity-added':          (gobject.SIGNAL_RUN_FIRST,
                                    gobject.TYPE_NONE, 
                                   ([gobject.TYPE_PYOBJECT])),
        'activity-removed':        (gobject.SIGNAL_RUN_FIRST,
                                    gobject.TYPE_NONE,
                                   ([gobject.TYPE_PYOBJECT])),
        'active-activity-changed': (gobject.SIGNAL_RUN_FIRST,
                                    gobject.TYPE_NONE,
                                   ([gobject.TYPE_PYOBJECT]))
    }
    
    def __init__(self, bundle_registry):
        gobject.GObject.__init__(self)

        self._activities = {}
        self._bundle_registry = bundle_registry
        self._current_activity = None

        screen = wnck.screen_get_default()
        screen.connect('window-opened', self._window_opened_cb)
        screen.connect('window-closed', self._window_closed_cb)
        screen.connect('active-window-changed',
                       self._active_window_changed_cb)

    def get_current_activity(self):
        return self._current_activity

    def __iter__(self): 
        return iter(self._activities)
        
    def __len__(self):
        return len(self._activities)
        
    def __getitem__(self, i):
        return self._activities[i]

    def _window_opened_cb(self, screen, window):
        if window.get_window_type() == wnck.WINDOW_NORMAL:
            self._add_activity(window)

    def _window_closed_cb(self, screen, window):
        if window.get_window_type() == wnck.WINDOW_NORMAL:
            self._remove_activity(window.get_xid())

    def _active_window_changed_cb(self, screen):
        window = screen.get_active_window()
        if window == None:
            self.emit('active-activity-changed', None)
            return
        if window.get_window_type() != wnck.WINDOW_NORMAL:
            return

        xid = window.get_xid()
        if self._activities.has_key(xid):
            self._current_activity = self._activities[xid]
        else:
            self._current_activity = None
            logging.error('Model for window %d does not exist.' % xid)

        self.emit('active-activity-changed', self._current_activity)
        
    def _add_activity(self, window):
        activity = HomeActivity(self._bundle_registry, window)
        self._activities[window.get_xid()] = activity
        self.emit('activity-added', activity)

    def _remove_activity(self, xid):
        if self._activities.has_key(xid):
            self.emit('activity-removed', self._activities[xid])
            del self._activities[xid]
        else:
            logging.error('Model for window %d does not exist.' % xid)