~jonobacon/ubuntu-accomplishments-daemon/ubuntu-accomplishments-daemon

« back to all changes in this revision

Viewing changes to accomplishments/gui/base/Window.py

  • Committer: Jono Bacon
  • Date: 2012-04-08 22:11:10 UTC
  • Revision ID: jono@ubuntu.com-20120408221110-3ihxpctu5kryp460
Creating ubuntu package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
 
### BEGIN LICENSE
3
 
# This file is in the public domain
4
 
### END LICENSE
5
 
import logging
6
 
 
7
 
from gi.repository import Gio, Gtk # pylint: disable=E0611
8
 
 
9
 
from . helpers import get_builder, show_uri, get_help_uri
10
 
 
11
 
 
12
 
logger = logging.getLogger('trophyinfo')
13
 
 
14
 
 
15
 
# This class is meant to be subclassed by TrophyinfoWindow.  It provides
16
 
# common functions and some boilerplate.
17
 
class Window(Gtk.Window):
18
 
 
19
 
    __gtype_name__ = "Window"
20
 
 
21
 
    # To construct a new instance of this method, the following notable
22
 
    # methods are called in this order:
23
 
    # __new__(cls)
24
 
    # __init__(self)
25
 
    # finish_initializing(self, builder)
26
 
    # __init__(self)
27
 
    #
28
 
    # For this reason, it's recommended you leave __init__ empty and put
29
 
    # your initialization code in finish_initializing
30
 
 
31
 
    def __new__(cls):
32
 
        """Special static method that's automatically called by Python when
33
 
        constructing a new instance of this class.
34
 
 
35
 
        Returns a fully instantiated BaseTrophyinfoWindow object.
36
 
        """
37
 
        builder = get_builder('TrophyinfoWindow')
38
 
        new_object = builder.get_object("trophyinfo_window")
39
 
        new_object.finish_initializing(builder)
40
 
        return new_object
41
 
 
42
 
    def finish_initializing(self, builder):
43
 
        """Called while initializing this instance in __new__
44
 
 
45
 
        finish_initializing should be called after parsing the UI definition
46
 
        and creating a TrophyinfoWindow object with it in order to finish
47
 
        initializing the start of the new TrophyinfoWindow instance.
48
 
        """
49
 
        # Get a reference to the builder and set up the signals.
50
 
        self.builder = builder
51
 
        self.ui = builder.get_ui(self, True)
52
 
        self.PreferencesDialog = None # class
53
 
        self.preferences_dialog = None # instance
54
 
        self.AboutDialog = None # class
55
 
 
56
 
        self.settings = Gio.Settings("net.launchpad.trophyinfo")
57
 
        self.settings.connect('changed', self.on_preferences_changed)
58
 
 
59
 
        # Optional Launchpad integration
60
 
        # This shouldn't crash if not found as it is simply used for bug
61
 
        # reporting. See the following for more information about Launchpad
62
 
        # integration:
63
 
        #
64
 
        # https://wiki.ubuntu.com/UbuntuDevelopment/Internationalisation/Coding
65
 
        try:
66
 
            from gi.repository import LaunchpadIntegration # pylint: disable=E0611
67
 
            LaunchpadIntegration.add_items(self.ui.helpMenu, 1, True, True)
68
 
            LaunchpadIntegration.set_sourcepackagename('trophyinfo')
69
 
        except ImportError:
70
 
            pass
71
 
 
72
 
        # Optional application indicator support
73
 
        # Run 'quickly add indicator' to get started.
74
 
        # More information:
75
 
        #  http://owaislone.org/quickly-add-indicator/
76
 
        #  https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationIndicators
77
 
        try:
78
 
            from accomplishments import indicator
79
 
            # self is passed so methods of this class can be called from indicator.py
80
 
            # Comment this next line out to disable appindicator
81
 
            self.indicator = indicator.new_application_indicator(self)
82
 
        except ImportError:
83
 
            pass
84
 
 
85
 
    def on_mnu_contents_activate(self, widget, data=None):
86
 
        show_uri(self, "ghelp:%s" % get_help_uri())
87
 
 
88
 
    def on_mnu_about_activate(self, widget, data=None):
89
 
        """Display the about box for trophyinfo."""
90
 
        if self.AboutDialog is not None:
91
 
            about = self.AboutDialog() # pylint: disable=E1102
92
 
            response = about.run()
93
 
            about.destroy()
94
 
 
95
 
    def on_mnu_preferences_activate(self, widget, data=None):
96
 
        """Display the preferences window for trophyinfo."""
97
 
 
98
 
        """ From the PyGTK Reference manual
99
 
           Say for example the preferences dialog is currently open,
100
 
           and the user chooses Preferences from the menu a second time;
101
 
           use the present() method to move the already-open dialog
102
 
           where the user can see it."""
103
 
        if self.preferences_dialog is not None:
104
 
            logger.debug('show existing preferences_dialog')
105
 
            self.preferences_dialog.present()
106
 
        elif self.PreferencesDialog is not None:
107
 
            logger.debug('create new preferences_dialog')
108
 
            self.preferences_dialog = self.PreferencesDialog() # pylint: disable=E1102
109
 
            self.preferences_dialog.connect('destroy', self.on_preferences_dialog_destroyed)
110
 
            self.preferences_dialog.show()
111
 
        # destroy command moved into dialog to allow for a help button
112
 
 
113
 
    def on_mnu_close_activate(self, widget, data=None):
114
 
        """Signal handler for closing the TrophyinfoWindow."""
115
 
        self.destroy()
116
 
 
117
 
    def on_destroy(self, widget, data=None):
118
 
        """Called when the TrophyinfoWindow is closed."""
119
 
        # Clean up code for saving application state should be added here.
120
 
        Gtk.main_quit()
121
 
 
122
 
    def on_preferences_changed(self, settings, key, data=None):
123
 
        logger.debug('preference changed: %s = %s' % (key, str(settings.get_value(key))))
124
 
 
125
 
    def on_preferences_dialog_destroyed(self, widget, data=None):
126
 
        '''only affects gui
127
 
 
128
 
        logically there is no difference between the user closing,
129
 
        minimising or ignoring the preferences dialog'''
130
 
        logger.debug('on_preferences_dialog_destroyed')
131
 
        # to determine whether to create or present preferences_dialog
132
 
        self.preferences_dialog = None