summaryrefslogtreecommitdiff
path: root/GstInspector/GUI/app.py
blob: afe12e4ce82be7cbbfd240a6920256b6af7c4f91 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8; mode: python; -*-
#
#  GStreamer Inspector - Multimedia system plugin introspection
#
#  Copyright (C) 2007 René Stadler <mail@renestadler.de>
#
#  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 3 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, see <http://www.gnu.org/licenses/>.

"""GStreamer Inspector GUI.app module."""

import os
import logging

import gtk

from GstInspector import Data, main

from GstInspector.GUI.actions import Actions
from GstInspector.GUI.filters import FilterManager
from GstInspector.GUI.models import ElementModel
from GstInspector.GUI.state import InspectorAppState
from GstInspector.GUI.utils import UIFactory, WidgetFactory
from GstInspector.GUI.window import InspectorWindow

class InspectorApp (Data.Consumer):

    def __init__ (self):

        self.logger = logging.getLogger ("app")
        self.windows = []

        self.state = InspectorAppState ()
        self.element_model = ElementModel ()

        ui_filter_classes = FilterManager.iter_item_classes ()
        model_factories = set ((cls.get_model_factory ()
                                for cls in ui_filter_classes))
        if None in model_factories:
            model_factories.remove (None)

        self.data_producer = Data.Producer (Data.GSourceDispatcher (),
                                            Data.Policy (long_running = True))
        self.data_producer.consumers += model_factories
        self.data_producer.consumers += [self.element_model, self]

        gtk.window_set_default_icon_name ("gst-inspector")

        # Keep startup notification spinning until we have filled the view.
        gtk.window_set_auto_startup_notification (False)

        menu_group = gtk.ActionGroup ("MenuActions")
        menu_group.add_actions ([("FileMenuAction", None, _("_File")),
                                 ("ViewMenuAction", None, _("_View")),
                                 ("ViewColumnsMenuAction", None, _("_Columns")),
                                 ("HelpMenuAction", None, _("_Help")),
                                 ("NameValueContextMenuAction", None, "")])

        self.actions = Actions ()
        self.actions.add_group (menu_group)

        self.widget_factory = WidgetFactory (main.Paths.data_dir)

        ui_filename = os.path.join (main.Paths.data_dir, "menus.ui")
        self.ui_factory = UIFactory (ui_filename, self.actions)

        self.new_window ()

    def handle_load_started_after (self):

        self.logger.info ("data load has started")

    def handle_data_error (self, error):

        """Data.Consumer method."""

        if error.exc_type_name == ImportError.__name__:
            raise ImportError (error.error)
        else:
            import sys
            print >> sys.stderr, "Exception in child process:"
            print >> sys.stderr, error.traceback,
            raise RuntimeError ("error from child process: %s"
                                % (error.error,))

    def handle_load_finished (self):

        """Data.Consumer method."""

        gtk.gdk.notify_startup_complete ()

        self.logger.info ("data load has finished")

    def new_window (self):

        window = InspectorWindow (self)
        self.logger.info ("created new window (%i)", window.count)
        self.data_producer.consumers.append (window)
        self.windows.append (window)
        self.logger.debug ("window list is now %r", self.windows)

        action = window.actions.new_window
        handler = self.handle_new_window_action_activate
        action.connect ("activate", handler)

        action = window.actions.reload_data
        handler = self.handle_reload_data_action_activate
        action.connect ("activate", handler)

    def close_window (self, window):

        try:
            window.detach ()
        finally:
            self.state.save ()
            self.logger.info ("window (%i) closed", window.count)
            self.data_producer.consumers.remove (window)
            self.windows.remove (window)

            if self.windows:
                self.logger.debug ("window list is now %r", self.windows)

            if not self.windows:
                self.logger.info ("last window closed, exiting main loop")
                self.state.save (now = True)

                gtk.main_quit ()

    def update_data (self):

        """Trigger a refresh of all introspection data."""

        self.logger.info ("reloading data")

        policy = Data.Policy (update = True)
        self.data_producer.policy.modify (policy)
        self.data_producer.start ()

    def handle_new_window_action_activate (self, action):

        self.new_window ()

    def handle_reload_data_action_activate (self, action):

        self.update_data ()

    def run (self):

        try:
            self.data_producer.start ()

            main_loop_wrapper = main.MainLoopWrapper (enterfunc = gtk.main,
                                                      exitfunc = gtk.main_quit)
            main_loop_wrapper.run ()
        finally:
            gtk.gdk.notify_startup_complete ()

from gettext import gettext as _