~ubuntu-branches/ubuntu/saucy/unity-tweak-tool/saucy-proposed

« back to all changes in this revision

Viewing changes to UnityTweakTool/elements/switch.py

  • Committer: Package Import Robot
  • Author(s): Barneedhar Vigneshwar
  • Date: 2013-02-15 20:33:41 UTC
  • Revision ID: package-import@ubuntu.com-20130215203341-yqrfr9df488k41am
Tags: 0.0.3
* New upstream release
* Closes needs-packaging bug (LP: #1126433)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
# Team:
 
5
#   J Phani Mahesh <phanimahesh@gmail.com>
 
6
#   Barneedhar (jokerdino) <barneedhar@ubuntu.com>
 
7
#   Amith KK <amithkumaran@gmail.com>
 
8
#   Georgi Karavasilev <motorslav@gmail.com>
 
9
#   Sam Tran <samvtran@gmail.com>
 
10
#   Sam Hewitt <hewittsamuel@gmail.com>
 
11
#   Angel Araya <al.arayaq@gmail.com>
 
12
#
 
13
# Description:
 
14
#   A One-stop configuration tool for Unity.
 
15
#
 
16
# Legal Stuff:
 
17
#
 
18
# This file is a part of Unity Tweak Tool
 
19
#
 
20
# Unity Tweak Tool is free software; you can redistribute it and/or modify it under
 
21
# the terms of the GNU General Public License as published by the Free Software
 
22
# Foundation; version 3.
 
23
#
 
24
# Unity Tweak Tool is distributed in the hope that it will be useful, but WITHOUT
 
25
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
26
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
27
# details.
 
28
#
 
29
# You should have received a copy of the GNU General Public License along with
 
30
# this program; if not, see <https://www.gnu.org/licenses/gpl-3.0.txt>
 
31
 
 
32
''' Definitions for Switch element. '''
 
33
from UnityTweakTool.backends import gsettings
 
34
 
 
35
import logging
 
36
logger=logging.getLogger('UnityTweakTool.elements.switch')
 
37
 
 
38
class Switch:
 
39
    def __init__(self,controlObj):
 
40
        ''' Initialise a switch from a controlObj dictionary '''
 
41
        self.id         = controlObj['id']
 
42
        self.builder    = controlObj['builder']
 
43
        self.ui         = controlObj['builder'].get_object(controlObj['id'])
 
44
        self.schema     = controlObj['schema']
 
45
        self.path       = controlObj['path']
 
46
        self.key        = controlObj['key']
 
47
        self.type       = controlObj['type']
 
48
        self.map        = controlObj['map']
 
49
        self.invmap     = dict([ (v,k) for (k,v) in self.map.items() ])
 
50
        self.dependants = controlObj['dependants']
 
51
        assert gsettings.is_valid(
 
52
            schema=self.schema,
 
53
            path=self.path,
 
54
            key=self.key
 
55
            )
 
56
        logger.debug('Initialised a switch with id {self.id} to control key {self.key} of type {self.type} in schema {self.schema} with path {self.path}'.format(self=self))
 
57
 
 
58
    def register(self,handler):
 
59
        ''' Register handler on a handler object '''
 
60
        handler['on_%s_active_notify'% self.id]=self.handler
 
61
        logger.debug('Handler for {self.id} registered'.format(self=self))
 
62
 
 
63
    def refresh(self):
 
64
        ''' Refresh the UI querying the backend '''
 
65
        logger.debug('Refreshing UI display for {self.id}'.format(self=self))
 
66
        self.active=self.map[
 
67
                gsettings.get(
 
68
                    schema=self.schema,
 
69
                    path  =self.path,
 
70
                    key   =self.key,
 
71
                    type  =self.type
 
72
                    )
 
73
                ]
 
74
        self.ui.set_active(self.active)
 
75
        self.handledependants()
 
76
 
 
77
    def handler(self,*args,**kwargs):
 
78
        ''' Handle notify::active signals '''
 
79
        self.active=self.ui.get_active()
 
80
        gsettings.set(
 
81
            schema=self.schema,
 
82
            path=self.path,
 
83
            key=self.key,
 
84
            type=self.type,
 
85
            value=self.invmap[self.active]
 
86
            )
 
87
        self.handledependants()
 
88
        logger.info('Handler for {self.id} executed'.format(self=self))
 
89
 
 
90
    def reset(self):
 
91
        ''' Reset the controlled key '''
 
92
        gsettings.reset(schema=self.schema,path=self.path,key=self.key)
 
93
        logger.debug('Key {self.key} in schema {self.schema} and path {self.path} reset.'.format(self=self))
 
94
        
 
95
    def handledependants(self):
 
96
        for element in self.dependants:
 
97
            self.builder.get_object(element).set_sensitive(self.active)