summaryrefslogtreecommitdiff
path: root/CompizManager.py
blob: 2b1e983a711296b3d9f479f752e84d827fccf07d (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
#
# Copyright Eyemagnet Limited
# 
# 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 2 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., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# 
# Author: Kristian F. Erikson <kristian@erikson.dk>
#

import dbus
import xml.etree.ElementTree
import time

from CompizPlugin import CompizPlugin   # Import the local modules
from CompizConnection import CompizConnection
from CompizTools import *

class CompizManager(object):
    """
    Instances of this class are intended to represent an abstraction for managing Compiz plugins.
    Compiz Plugins can be managed interactively in Python using this module.
    """
    
    def __init__(self):
        """ Build a compiz manager instance """
        self.connection = CompizConnection()
        self.pluginDict = {}
        self.pluginNameList = []
        self.activePluginList = []
        
        self.loadPlugins()
        self.buildPlugins()
        self.listActivePlugins()
        
    def active(self):
        """ Returns true if Compiz is active and accessible, otherwise false -- Not implemented yet """
        # TODO: implement
        pass
            
    def loadPlugins(self):              # Connects to compiz and gets the list of plugins
        """ Internal function that gets the list of availiable plugins from Compiz """
        pluginTuple = self.connection.getPlugins()
        plugins = convertFromDbusToPython(pluginTuple)
        self.pluginNameList = sorted(plugins)
        
    def buildPlugins(self):             # Creates new plugin objects for each of the plugins
        """ Internal function that builds the plugins and their options and actions """
        tree = xml.etree.ElementTree.parse("plugins.xml")
        for pluginName in self.pluginNameList:
            plugin = CompizPlugin(pluginName, self)
            actionsAndArgs = tree.find(pluginName)
            plugin.setupActions(actionsAndArgs)     # Further parse XML data
            self.pluginDict[pluginName] = plugin    # Save once parsed from XML
        
    def getPlugin(self, pluginName):    # Returns specified plugin object
        """ Returns a representation of a Compiz plugin """
        return self.pluginDict[pluginName]
        
    def listActivePlugins(self):        # List all currently active plugins
        """ Returns a list of currently active Compiz plugins """
        activePluginTuple = self.connection.get('/core/allscreens/active_plugins')
        activePlugins = convertFromDbusToPython(activePluginTuple)
        self.activePluginList = sorted(activePlugins)
        return self.activePluginList

    def listAllPlugins(self):           # List all avaible plugins
        """ Returns a list of all availiable Compiz plugins (active and inactive) """
        return self.pluginNameList
    
    def listInactivePlugins(self):      # List inactive plugins = pluginNameList - activePluginList
        """ Returns a list of currently inactive Compiz plugins """
        self.listActivePlugins()
        self.inactivePluginList = []
        for pluginName in self.pluginNameList:
            if pluginName in self.activePluginList:
                pass
            else:
                self.inactivePluginList.append(pluginName)
        return self.inactivePluginList
        
    def activatePlugin(self, pluginNames):      # takes a tuple of names (strings) or a single name
        """ Activates plugins, either takes a single name or tuple of names """
        if type(pluginNames) == type('s'):       # if it's a string
            if pluginNames in self.pluginNameList:
                if pluginNames not in self.activePluginList:
                    self.activePluginList.append(pluginNames)
            # Convert and send off list
            dbusArray = convertFromPythonToDbus(self.activePluginList)
            self.connection.setActivePlugins(dbusArray)
        elif type(pluginNames) == type([]):      # if it's a list
            check = 0
            for pluginName in pluginNames:      # check all plugin names given are valid
                if pluginName in self.pluginNameList:
                    check += 1
            if check != len(pluginNames):
                raise CompizPluginNameErr
            for plugin in pluginNames:
                if plugin not in self.activePluginList:
                    self.activePluginList.append(plugin)
            # Convert and send off list
            dbusArray = convertFromPythonToDbus(self.activePluginList)
            self.connection.setActivePlugins(dbusArray)
        else:
            raise CompizPluginTypeArgError
            
    def deactivatePlugin(self, pluginNames):      # takes a tuple of names (strings) or a single name
        """ Deactivates plugins, takes either a single name or tuple of names """
        if type(pluginNames) == type('s'):       # if it's a string
            if pluginNames in self.pluginNameList:
                if pluginNames in self.activePluginList:
                    self.activePluginList.remove(pluginNames)
            # Convert and send off list
            dbusArray = convertFromPythonToDbus(self.activePluginList)
            self.connection.setActivePlugins(dbusArray)
        elif type(pluginNames) == type([]):      # if it's a list
            check = 0
            for pluginName in pluginNames:      # check all plugin names given are valid
                if pluginName in self.pluginNameList:
                    check += 1
            if check != len(pluginNames):
                raise CompizPluginNameErr
            for plugin in pluginNames:
                if plugin in self.activePluginList:
                    self.activePluginList.remove(plugin)
            # Convert and send off list
            dbusArray = convertFromPythonToDbus(self.activePluginList)
            self.connection.setActivePlugins(dbusArray)
        else:
            raise CompizPluginTypeArgError

    def setActivePluginList(self, pluginList):
        """ Lets you set the list of currently active plugins. Proceed with caution. """
        dbusArray = convertFromPythonToDbus(pluginList)
        self.connection.setActivePlugins(dbusArray)
        
    def pluginDoAction(self, pluginName, action, parameters=None):
        """ Get a plugin to do an action with parameters without having to get the plugin first -- Not implemented """
        # TODO: Implement
        pass
        
    def listAllWindows()
        """ Return a dictionary with attributes for each window, including info "xwininfo" returns as well as 
        Compiz Fusion specific details like which viewport or face of the cube, etc. -- Not implemented """
        # TODO: Implement
        pass
    
        


def AllToScreen():
    """ Prints all Plugins with their actions and options to the screen """
    listofPlugins = manager.listAllPlugins()
    for plugin in listofPlugins:
        tempPlugin = manager.getPlugin(plugin)
        print("Plugin: " + str(tempPlugin.name) + ' active=' + str(tempPlugin.active))
        print("Options:\n" + str(tempPlugin.listOptions()))
        print("Actions:\n" + str(tempPlugin.listActions()) + '\n')
    

def AllToFile(fileName):
    """ Prints all Plugins with their actions and options to a file """
    listofPlugins = manager.listAllPlugins()
    output = open(fileName, 'w')
    for plugin in listofPlugins:
        tempPlugin = manager.getPlugin(plugin)
        output.write("Plugin: " + str(tempPlugin.name) + ' active=' + str(tempPlugin.active) +  '\n')
        output.write("Options:\n" + str(tempPlugin.listOptions())  + '\n')
        output.write("Actions:\n" + str(tempPlugin.listActions()) + '\n\n')
        

if __name__ == '__main__':
    # Set a global manager for the tests:
    manager = CompizManager()
    
    from CompizTests import *
    # Pre test setup
    preTestSetup()
    # Run the tests
    suite = unittest.TestLoader().loadTestsFromTestCase(TestCompizManagerClasses)
    unittest.TextTestRunner(verbosity=2).run(suite)
    # Run the test cleanup
    postTestCleanup()
    
    # Print a file with actions and options listed in it
    AllToFile('Plugins with options and actions.txt')