summaryrefslogtreecommitdiff
path: root/GstDebugViewer/GUI/window.py
blob: a6700d321ecad075d71e0b2c7fcba875ea342069 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
# -*- coding: utf-8; mode: python; -*-
#
#  GStreamer Debug Viewer - View and analyze GStreamer debug log files
#
#  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 Debug Viewer GUI module."""

def _ (s):
    return s

import os.path
from bisect import bisect_right, bisect_left
import logging

import gobject
import gtk

from GstDebugViewer import Common, Data, Main
from GstDebugViewer.GUI.columns import LineViewColumnManager, ViewColumnManager
from GstDebugViewer.GUI.filters import (CategoryFilter,
                                        DebugLevelFilter,
                                        FilenameFilter,
                                        ObjectFilter)
from GstDebugViewer.GUI.models import (FilteredLogModel,
                                       LazyLogModel,
                                       LineViewLogModel,
                                       LogModelBase,
                                       RangeFilteredLogModel)

class LineView (object):

    def __init__ (self):

        self.column_manager = LineViewColumnManager ()

    def attach (self, window):

        self.clear_action = window.actions.clear_line_view
        handler = self.handle_clear_line_view_action_activate
        self.clear_action.connect ("activate", handler)

        self.line_view = window.widgets.line_view
        self.line_view.connect ("row-activated", self.handle_line_view_row_activated)

        ui = window.ui_manager
        self.popup = ui.get_widget ("/ui/context/LineViewContextMenu").get_submenu ()
        Common.GUI.widget_add_popup_menu (self.line_view, self.popup)

        self.log_view = log_view = window.log_view
        log_view.connect ("row-activated", self.handle_log_view_row_activated)
        sel = log_view.get_selection ()
        sel.connect ("changed", self.handle_log_view_selection_changed)

        self.clear_action.props.sensitive = False
        self.column_manager.attach (window)

    def clear (self):

        model = self.line_view.get_model ()

        if len (model) == 0:
            return

        for i in range (1, len (model)):
            model.remove_line (1)

        self.clear_action.props.sensitive = False

    def handle_attach_log_file (self, window):

        self.line_view.set_model (LineViewLogModel (window.log_model))

    def handle_line_view_row_activated (self, view, path, column):

        line_index = path[0]
        line_model = view.get_model ()
        log_model = self.log_view.get_model ()
        top_index = line_model.line_index_to_top (line_index)
        log_index = log_model.line_index_from_top (top_index)
        path = (log_index,)
        self.log_view.scroll_to_cell (path, use_align = True, row_align = .5)
        sel = self.log_view.get_selection ()
        sel.select_path (path)

    def handle_log_view_row_activated (self, view, path, column):

        log_model = view.get_model ()
        line_index = path[0]

        top_line_index = log_model.line_index_to_top (line_index)
        line_model = self.line_view.get_model ()
        if line_model is None:
            return

        if len (line_model):
            timestamps = [row[line_model.COL_TIME] for row in line_model]
            row = log_model[(line_index,)]
            position = bisect_right (timestamps, row[line_model.COL_TIME])
        else:
            position = 0
        if len (line_model) > 1:
            other_index = line_model.line_index_to_top (position - 1)
        else:
            other_index = -1
        if other_index == top_line_index and position != 1:
            # Already have the line.
            pass
        else:
            line_model.insert_line (position, top_line_index)
            self.clear_action.props.sensitive = True

    def handle_log_view_selection_changed (self, selection):

        line_model = self.line_view.get_model ()
        if line_model is None:
            return

        model, tree_iter = selection.get_selected ()

        if tree_iter is None:
            return

        path = model.get_path (tree_iter)
        line_index = model.line_index_to_top (path[0])

        if len (line_model) == 0:
            line_model.insert_line (0, line_index)
        else:
            line_model.replace_line (0, line_index)

    def handle_clear_line_view_action_activate (self, action):

        self.clear ()

class ProgressDialog (object):

    def __init__ (self, window, title = ""):

        widgets = window.widget_factory.make ("progress-dialog.ui", "progress_dialog")
        dialog = widgets.progress_dialog
        dialog.connect ("response", self.__handle_dialog_response)

        self.__dialog = dialog
        self.__progress_bar = widgets.progress_bar
        self.__progress_bar.props.text = title

        dialog.set_transient_for (window.gtk_window)
        dialog.show ()

    def __handle_dialog_response (self, dialog, resp):

        self.handle_cancel ()

    def handle_cancel (self):

        pass

    def update (self, progress):

        if self.__progress_bar is None:
            return

        self.__progress_bar.props.fraction = progress

    def destroy (self):

        if self.__dialog is None:
            return
        self.__dialog.destroy ()
        self.__dialog = None
        self.__progress_bar = None

class Window (object):

    def __init__ (self, app):

        self.logger = logging.getLogger ("ui.window")
        self.app = app

        self.dispatcher = None
        self.progress_dialog = None
        self.update_progress_id = None

        self.window_state = Common.GUI.WindowState ()
        self.column_manager = ViewColumnManager (app.state_section)

        self.actions = Common.GUI.Actions ()

        group = gtk.ActionGroup ("MenuActions")
        group.add_actions ([("FileMenuAction", None, _("_File")),
                            ("ViewMenuAction", None, _("_View")),
                            ("ViewColumnsMenuAction", None, _("_Columns")),
                            ("HelpMenuAction", None, _("_Help")),
                            ("LineViewContextMenuAction", None, "")])
        self.actions.add_group (group)

        group = gtk.ActionGroup ("WindowActions")
        group.add_actions ([("new-window", gtk.STOCK_NEW, _("_New Window"), "<Ctrl>N"),
                            ("open-file", gtk.STOCK_OPEN, _("_Open File"), "<Ctrl>O"),
                            ("reload-file", gtk.STOCK_REFRESH, _("_Reload File"), "<Ctrl>R"),
                            ("close-window", gtk.STOCK_CLOSE, _("Close _Window"), "<Ctrl>W"),
                            ("cancel-load", gtk.STOCK_CANCEL, None,),
                            ("clear-line-view", gtk.STOCK_CLEAR, None),
                            ("show-about", gtk.STOCK_ABOUT, None)])
        self.actions.add_group (group)
        self.actions.reload_file.props.sensitive = False

        group = gtk.ActionGroup ("RowActions")
        group.add_actions ([("hide-before-line", None, _("Hide lines before this one")),
                            ("hide-after-line", None, _("Hide lines after this one")),
                            ("show-hidden-lines", None, _("Show hidden lines")),
                            ("edit-copy-line", gtk.STOCK_COPY, _("Copy line"), "<Ctrl>C"),
                            ("edit-copy-message", gtk.STOCK_COPY, _("Copy message"), ""),
                            ("set-base-time", None, _("Set base time")),
                            ("hide-log-level", None, _("Hide log level")),
                            ("hide-log-category", None, _("Hide log category")),
                            ("hide-log-object", None, _("Hide object")),
                            ("hide-filename", None, _("Hide filename"))])
        group.props.sensitive = False
        self.actions.add_group (group)

        self.actions.add_group (self.column_manager.action_group)

        self.log_file = None
        self.setup_model (LazyLogModel ())

        self.widget_factory = Common.GUI.WidgetFactory (Main.Paths.data_dir)
        self.widgets = self.widget_factory.make ("main-window.ui", "main_window")

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

        self.ui_manager = ui = self.ui_factory.make ()
        menubar = ui.get_widget ("/ui/menubar")
        self.widgets.vbox_main.pack_start (menubar, False, False, 0)

        self.gtk_window = self.widgets.main_window
        self.gtk_window.add_accel_group (ui.get_accel_group ())
        self.log_view = self.widgets.log_view
        self.log_view.drag_dest_unset ()
        self.log_view.set_search_column (-1)
        sel = self.log_view.get_selection ()
        sel.connect ("changed", self.handle_log_view_selection_changed)

        self.view_popup = ui.get_widget ("/ui/context/LogViewContextMenu").get_submenu ()
        Common.GUI.widget_add_popup_menu (self.log_view, self.view_popup)

        self.line_view = LineView ()

        self.attach ()
        self.column_manager.attach (self.log_view)

    def setup_model (self, model, filter = False):

        self.log_model = model
        self.log_range = RangeFilteredLogModel (self.log_model)
        if filter:
            self.log_filter = FilteredLogModel (self.log_range)
            self.log_filter.handle_process_finished = self.handle_log_filter_process_finished
        else:
            self.log_filter = None

    def get_top_attach_point (self):

        return self.widgets.vbox_main

    def get_side_attach_point (self):

        return self.widgets.hbox_view

    def attach (self):

        self.window_state.attach (window = self.gtk_window,
                                  state = self.app.state_section)

        self.clipboard = gtk.Clipboard (self.gtk_window.get_display (),
                                        gtk.gdk.SELECTION_CLIPBOARD)

        for action_name in ("new-window", "open-file", "reload-file",
                            "close-window", "cancel-load",
                            "hide-before-line", "hide-after-line", "show-hidden-lines",
                            "edit-copy-line", "edit-copy-message", "set-base-time",
                            "hide-log-level", "hide-log-category", "hide-log-object",
                            "hide-filename", "show-about",):
            name = action_name.replace ("-", "_")
            action = getattr (self.actions, name)
            handler = getattr (self, "handle_%s_action_activate" % (name,))
            action.connect ("activate", handler)

        self.gtk_window.connect ("delete-event", self.handle_window_delete_event)

        self.features = []

        for plugin_feature in self.app.iter_plugin_features ():
            feature = plugin_feature (self.app)
            self.features.append (feature)

        for feature in self.features:
            feature.handle_attach_window (self)

        # FIXME: With multiple selection mode, browsing the list with key
        # up/down slows to a crawl! WTF is wrong with this stupid widget???
        sel = self.log_view.get_selection ()
        sel.set_mode (gtk.SELECTION_BROWSE)

        self.line_view.attach (self)

        self.gtk_window.show ()

    def detach (self):

        self.set_log_file (None)
        for feature in self.features:
            feature.handle_detach_window (self)

        self.window_state.detach ()
        self.column_manager.detach ()

    def get_active_line_index (self):

        selection = self.log_view.get_selection ()
        model, tree_iter = selection.get_selected ()
        if tree_iter is None:
            raise ValueError ("no line selected")
        path = model.get_path (tree_iter)
        return path[0]

    def get_active_line (self):

        selection = self.log_view.get_selection ()
        model, tree_iter = selection.get_selected ()
        if tree_iter is None:
            raise ValueError ("no line selected")
        model = self.log_view.get_model ()
        return model.get (tree_iter, *LogModelBase.column_ids)

    def close (self, *a, **kw):

        self.logger.debug ("closing window, detaching")
        self.detach ()
        self.gtk_window.hide ()
        self.logger.debug ("requesting close from app")
        self.app.close_window (self)

    def push_view_state (self):

        self.default_index = None
        self.default_start_index = None

        model = self.log_view.get_model ()
        if model is None:
            return

        try:
            line_index = self.get_active_line_index ()
        except ValueError:
            super_index = None
            self.logger.debug ("no line selected")
        else:
            super_index = model.line_index_to_top (line_index)
            self.logger.debug ("pushing selected line %i (abs %i)",
                               line_index, super_index)

        self.default_index = super_index

        vis_range = self.log_view.get_visible_range ()
        if vis_range is not None:
            start_path, end_path = vis_range
            start_index = start_path[0]
            self.default_start_index = model.line_index_to_top (start_index)

    def update_model (self, model = None):

        if model is None:
            model = self.log_view.get_model ()

        previous_model = self.log_view.get_model ()

        if previous_model == model:
            # Force update.
            self.log_view.set_model (None)
        self.log_view.set_model (model)

    def pop_view_state (self, scroll_to_selection = False):

        model = self.log_view.get_model ()
        if model is None:
            return

        selected_index = self.default_index
        start_index = self.default_start_index

        if selected_index is not None:

            try:
                select_index = model.line_index_from_top (selected_index)
            except IndexError, exc:
                self.logger.debug ("abs line index %i filtered out, not reselecting",
                                   selected_index)
            else:
                assert select_index >= 0
                sel = self.log_view.get_selection ()
                path = (select_index,)
                sel.select_path (path)

                if start_index is None or scroll_to_selection:
                    self.log_view.scroll_to_cell (path, use_align = True, row_align = .5)

        if start_index is not None and not scroll_to_selection:

            def traverse ():
                for i in xrange (start_index, len (model)):
                    yield i
                for i in xrange (start_index - 1, 0, -1):
                    yield i
            for current_index in traverse ():
                try:
                    target_index = model.line_index_from_top (current_index)
                except IndexError:
                    continue
                else:
                    path = (target_index,)
                    self.log_view.scroll_to_cell (path, use_align = True, row_align = 0.)
                    break

    def update_view (self):

        view = self.log_view
        model = view.get_model ()

        start_path, end_path = view.get_visible_range ()
        start_index, end_index = start_path[0], end_path[0]

        for line_index in range (start_index, end_index + 1):
            path = (line_index,)
            tree_iter = model.get_iter (path)
            model.row_changed (path, tree_iter)

    def handle_log_view_selection_changed (self, selection):

        try:
            line_index = self.get_active_line_index ()
        except ValueError:
            first_selected = True
            last_selected = True
        else:
            first_selected = (line_index == 0)
            last_selected = (line_index == len (self.log_view.get_model ()) - 1)

        self.actions.hide_before_line.props.sensitive = not first_selected
        self.actions.hide_after_line.props.sensitive = not last_selected

    def handle_window_delete_event (self, window, event):

        self.actions.close_window.activate ()

    def handle_new_window_action_activate (self, action):

        self.app.open_window ()

    def handle_open_file_action_activate (self, action):

        dialog = gtk.FileChooserDialog (None, self.gtk_window,
                                        gtk.FILE_CHOOSER_ACTION_OPEN,
                                        (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                         gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT,))
        response = dialog.run ()
        dialog.hide ()
        if response == gtk.RESPONSE_ACCEPT:
            self.set_log_file (dialog.get_filename ())
        dialog.destroy ()

    def handle_reload_file_action_activate (self, action):

        if self.log_file is None:
            return

        self.set_log_file (self.log_file.path)

    def handle_cancel_load_action_activate (self, action):

        self.logger.debug ("cancelling data load")

        self.set_log_file (None)

        if self.progress_dialog:
            self.progress_dialog.destroy ()
            self.progress_dialog = None
        if self.update_progress_id is not None:
            gobject.source_remove (self.update_progress_id)
            self.update_progress_id = None

    def handle_close_window_action_activate (self, action):

        self.close ()

    def handle_hide_after_line_action_activate (self, action):

        self.hide_range (after = True)

    def handle_hide_before_line_action_activate (self, action):

        self.hide_range (after = False)

    def hide_range (self, after):

        model = self.log_view.get_model ()
        try:
            filtered_line_index = self.get_active_line_index ()
        except ValueError:
            return

        if after:
            first_index = model.line_index_to_top (0)
            last_index = model.line_index_to_top (filtered_line_index)

            self.logger.info ("hiding lines after %i (abs %i), first line is abs %i",
                              filtered_line_index,
                              last_index,
                              first_index)
        else:
            first_index = model.line_index_to_top (filtered_line_index)
            last_index = model.line_index_to_top (len (model) - 1)

            self.logger.info ("hiding lines before %i (abs %i), last line is abs %i",
                              filtered_line_index,
                              first_index,
                              last_index)

        self.push_view_state ()
        start_index = first_index
        stop_index = last_index + 1
        self.log_range.set_range (start_index, stop_index)
        if self.log_filter:
            self.log_filter.super_model_changed_range ()
        self.update_model ()
        self.pop_view_state ()
        self.actions.show_hidden_lines.props.sensitive = True

    def handle_show_hidden_lines_action_activate (self, action):

        self.logger.info ("restoring model filter to show all lines")
        self.push_view_state ()
        self.log_range.reset ()
        self.log_filter = None
        self.update_model (self.log_range)
        self.pop_view_state (scroll_to_selection = True)
        self.actions.show_hidden_lines.props.sensitive = False

    def handle_edit_copy_line_action_activate (self, action):

        # TODO: Should probably copy the _exact_ line as taken from the file.

        line = self.get_active_line ()
        log_line = Data.LogLine (line)
        self.clipboard.set_text (log_line.line_string ())

    def handle_edit_copy_message_action_activate (self, action):

        col_id = LogModelBase.COL_MESSAGE
        self.clipboard.set_text (self.get_active_line ()[col_id])

    def add_model_filter (self, filter):

        self.progress_dialog = ProgressDialog (self, _("Filtering"))
        self.progress_dialog.handle_cancel = self.handle_filter_progress_dialog_cancel
        dispatcher = Common.Data.GSourceDispatcher ()
        self.filter_dispatcher = dispatcher

        # FIXME: Unsetting the model to keep e.g. the dispatched timeline
        # sentinel from collecting data while we filter idly, which slows
        # things down for nothing.
        self.push_view_state ()
        self.log_view.set_model (None)
        if self.log_filter is None:
            self.log_filter = FilteredLogModel (self.log_range)
            self.log_filter.handle_process_finished = self.handle_log_filter_process_finished
        self.log_filter.add_filter (filter, dispatcher = dispatcher)

        gobject.timeout_add (250, self.update_filter_progress)

    def update_filter_progress (self):

        if self.progress_dialog is None:
            return False

        try:
            progress = self.log_filter.get_filter_progress ()
        except ValueError:
            self.logger.warning ("no filter process running")
            return False

        self.progress_dialog.update (progress)

        return True

    def handle_filter_progress_dialog_cancel (self):

        self.progress_dialog.destroy ()
        self.progress_dialog = None

        self.log_filter.abort_process ()
        self.log_view.set_model (self.log_filter)
        self.pop_view_state ()

    def handle_log_filter_process_finished (self):

        self.progress_dialog.destroy ()
        self.progress_dialog = None

        # No push_view_state here, did this in add_model_filter.
        self.update_model (self.log_filter)
        self.pop_view_state ()

        self.actions.show_hidden_lines.props.sensitive = True

    def handle_set_base_time_action_activate (self, action):

        row = self.get_active_line ()
        time_column = self.column_manager.find_item (name = "time")
        time_column.set_base_time (row[LogModelBase.COL_TIME])

    def handle_hide_log_level_action_activate (self, action):

        row = self.get_active_line ()
        debug_level = row[LogModelBase.COL_LEVEL]
        self.add_model_filter (DebugLevelFilter (debug_level))

    def handle_hide_log_category_action_activate (self, action):

        row = self.get_active_line ()
        category = row[LogModelBase.COL_CATEGORY]
        self.add_model_filter (CategoryFilter (category))

    def handle_hide_log_object_action_activate (self, action):

        row = self.get_active_line ()
        object_ = row[LogModelBase.COL_OBJECT]
        self.add_model_filter (ObjectFilter (object_))

    def handle_hide_filename_action_activate (self, action):

        row = self.get_active_line ()
        filename = row[LogModelBase.COL_FILENAME]
        self.add_model_filter (FilenameFilter (filename))

    def handle_show_about_action_activate (self, action):

        from GstDebugViewer import version

        dialog = self.widget_factory.make_one ("about-dialog.ui", "about_dialog")
        dialog.props.version = version
        dialog.run ()
        dialog.destroy ()

    @staticmethod
    def _timestamp_cell_data_func (column, renderer, model, tree_iter):

        ts = model.get_value (tree_iter, LogModel.COL_TIME)
        renderer.props.text = Data.time_args (ts)

    def _message_cell_data_func (self, column, renderer, model, tree_iter):

        offset = model.get_value (tree_iter, LogModel.COL_MESSAGE_OFFSET)
        self.log_file.seek (offset)
        renderer.props.text = strip_escape (self.log_file.readline ().strip ())

    def set_log_file (self, filename):

        if self.log_file is not None:
            for feature in self.features:
                feature.handle_detach_log_file (self, self.log_file)

        if filename is None:
            if self.dispatcher is not None:
                self.dispatcher.cancel ()
            self.dispatcher = None
            self.log_file = None
            self.actions.groups["RowActions"].props.sensitive = False
        else:
            self.logger.debug ("setting log file %r", filename)

            try:
                self.setup_model (LazyLogModel ())

                self.dispatcher = Common.Data.GSourceDispatcher ()
                self.log_file = Data.LogFile (filename, self.dispatcher)
            except EnvironmentError, exc:
                try:
                    file_size = os.path.getsize (filename)
                except EnvironmentError:
                    pass
                else:
                    if file_size == 0:
                        # Trying to mmap an empty file results in an invalid
                        # argument error.
                        self.show_error (_("Could not open file"),
                                         _("The selected file is empty"))
                        return
                self.handle_environment_error (exc, filename)
                return

            basename = os.path.basename (filename)
            self.gtk_window.props.title = _("%s - GStreamer Debug Viewer") % (basename,)

            self.log_file.consumers.append (self)
            self.log_file.start_loading ()

    def handle_environment_error (self, exc, filename):

        self.show_error (_("Could not open file"), str (exc))

    def show_error (self, message1, message2):

        dialog = gtk.MessageDialog (self.gtk_window, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR,
                                    gtk.BUTTONS_OK, message1)
        # The property for secondary text is new in 2.10, so we use this clunky
        # method instead.
        dialog.format_secondary_text (message2)
        dialog.set_default_response (0)
        dialog.run ()
        dialog.destroy ()

    def handle_load_started (self):

        self.logger.debug ("load has started")

        self.progress_dialog = ProgressDialog (self, _("Loading log file"))
        self.progress_dialog.handle_cancel = self.handle_load_progress_dialog_cancel
        self.update_progress_id = gobject.timeout_add (250, self.update_load_progress)

    def handle_load_progress_dialog_cancel (self):

        self.actions.cancel_load.activate ()

    def update_load_progress (self):

        if self.progress_dialog is None:
            self.logger.debug ("progress dialog is gone, removing progress update timeout")
            self.update_progress_id = None
            return False

        progress = self.log_file.get_load_progress ()
        self.progress_dialog.update (progress)

        return True

    def handle_load_finished (self):

        self.logger.debug ("load has finshed")

        self.progress_dialog.destroy ()
        self.progress_dialog = None

        self.log_model.set_log (self.log_file)
        self.log_range.reset ()
        self.log_filter = None

        self.actions.reload_file.props.sensitive = True
        self.actions.groups["RowActions"].props.sensitive = True
        self.actions.show_hidden_lines.props.sensitive = False

        def idle_set ():
            self.log_view.set_model (self.log_range)

            self.line_view.handle_attach_log_file (self)
            for feature in self.features:
                feature.handle_attach_log_file (self, self.log_file)
            if len (self.log_range):
                sel = self.log_view.get_selection ()
                sel.select_path ((0,))
            return False

        gobject.idle_add (idle_set)