Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ka_widget.py
blob: b7c23611c70a9a6455ccc6fd9fc637822cd8dab8 (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
# coding: UTF-8
# Copyright 2009, 2010 Thomas Jourdan
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gtk
import os

import ka_debug
import ka_extensionpoint

class WidgetList():
    """ """
    def __init__(self):
        self._list = {}

    def remember(self, widget_name, widget):
        """
        pre: widget_name is not None
        pre: widget is not None
        pre: widget_name not in self._list
        """
        widget.set_name(widget_name)
        self._list[widget_name] = widget

    def get_widget(self, widget_name):
        """
        pre: widget_name in self._list
        post: __return__ is not None
        """
        if widget_name in self._list:
            return self._list[widget_name]
        ka_debug.err('missing: ' + widget_name)
        return None


class KandidWidget(object):
    """ """

    def __init__(self):
        self.widget_list = WidgetList()
        self._notebook = None

    def get_widget_tree(self):
        """
        post: __return__ is not None
        """
        if self._notebook is None:
            # Create GUI
            self._notebook = gtk.Notebook()
            self.widget_list.remember('kandidNotebook', self._notebook)
            self._notebook.set_tab_pos(gtk.POS_TOP)
        return self._notebook

    def get_widget(self, widget_name):
        return self.widget_list.get_widget(widget_name)
             
    @staticmethod
    def get_localization():
        """ returns localization and territory
        post: len(__return__[0]) >= 2
        """
        languages = []
        territory, locale= '', ''
        for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
            val = os.environ.get(envar)
            if val:
                languages = val.split(':')
                for lang in languages:
                    pos = lang.find('.')
                    if pos >= 0:
                        lang = lang[:pos]
                    pos = lang.find('_')
                    if pos >= 0:
                        locale = lang[:pos]
                        territory = lang[pos+1:]
                    else:
                        locale = lang
                break
        ka_debug.info('languages: %s "%s" "%s"' % (languages, locale, territory)) 
        return locale, territory

    @staticmethod
    def get_localized_path():
        """Try to find the LOCALE folder. Default folder is locale/en/"""
        locale, territory = KandidWidget.get_localization()
        bundle_path = ka_extensionpoint.get_bundle_path()
        path_to_file = os.path.join(bundle_path,
                                 'locale',
                                 locale + '_' + territory,
                                 '')
        if not os.path.isfile(path_to_file):
            path_to_file = os.path.join(bundle_path,
                                     'locale',
                                     locale,
                                     '')
        if not os.path.isfile(path_to_file):
            path_to_file = os.path.join(bundle_path,
                                     'locale',
                                     'en',
                                     '')
        return path_to_file

    @staticmethod
    def get_introimage_path():
        """Try to locate the intro folder"""
        bundle_path = ka_extensionpoint.get_bundle_path()
        path_to_file = os.path.join(bundle_path,
                                 'intro',
                                 '')
        return path_to_file