summaryrefslogtreecommitdiffstats
path: root/fusion-desktop-effects.in
blob: eeab7186706663d09f2a84365a5411db1dbf5a71 (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
#!/usr/bin/env python

import gtk
import gtk.gdk
import sys
import gobject
import gettext
import subprocess
import time
import gconf
import os
import compizconfig
_ = gettext.gettext

gettext.bindtextdomain("fusion-desktop-effects", "@prefix@/share/locale")
gettext.textdomain("fusion-desktop-effects")
COMPIZ_LAUNCHER = "compiz-fusion-gtk"
COMPIZ_WM = "compiz-fusion-gtk"
WM_KEY = "/desktop/gnome/session/required_components/windowmanager"
BANNER = "@datadir@/cf_logo.png"

def get_screens():
    screens = []
    display = gtk.gdk.display_get_default()
    nScreens = display.get_n_screens()
    for i in range(nScreens):
        screens.append(i)
    return screens


def enable_compiz():
    gc = gconf.client_get_default()
    gc.set_string(WM_KEY,COMPIZ_WM)

def compiz_enabled():
    gc = gconf.client_get_default()
    return gc.get_string(WM_KEY) == COMPIZ_WM

def enable_metacity():
    subprocess.Popen(["metacity","--replace"])
    gc = gconf.client_get_default()
    gc.set_string(WM_KEY,"metacity")

def command_exists(cmd):
    for p in os.environ['PATH'].split(':'):
        if os.path.exists(os.path.join(p,cmd)):
           return True
    return False

class MainWindow:
      def __init__(self):
          self.window = gtk.Dialog()

          self.Screens = get_screens()
          self.Context = compizconfig.Context(self.Screens)

          self.window.set_title(_("Compiz Fusion"))
          self.togglebutton = gtk.ToggleButton(_("Enable Compiz Fusion Desktop Effects"))
          self.window.set_resizable(False)
          if compiz_enabled():
             self.togglebutton.set_active(True)
          self.togglebutton.connect('toggled',self.on_toggle)


          framelabel = gtk.Label(_("<b>Desktop Effects</b>"))
          framelabel.set_use_markup(True)
          frame = gtk.Frame()
          frame.set_label_widget(framelabel)
          hbox = gtk.HBox()
          self.window.vbox.pack_start(hbox)
          vbox = gtk.VBox()

          img = gtk.Image()
          img.set_from_file(BANNER)

          integrationbutton = gtk.CheckButton(_('Enable desktop integration'))
          integrationbutton.set_active(self.Context.Integration)
  
          integrationbutton.connect('toggled',self.integration_add_timeout)
  
          allow_integration = False
          available_backends = [b.Name for b in self.Context.Backends.values()]
          if os.environ['DESKTOP_SESSION'] == "gnome" and 'gconf' in available_backends:
             allow_integration = True
          elif os.environ['DESKTOP_SESSION'] == "kde" and 'kconfig' in available_backends:
             allow_integration = True
          
          integrationbutton.set_sensitive(allow_integration)

          vbox.pack_start(img)
          hbox.pack_start(vbox)
          vbox.pack_start(frame)
          vbox2 = gtk.VBox()
          hbox2 = gtk.HBox()
          
          hbox2.pack_start(self.togglebutton,expand=False,fill=False)
          vbox2.pack_start(hbox2)
          vbox2.pack_start(integrationbutton,padding=5)
          alignment = gtk.Alignment(0.5,0.5,1,1)
          alignment.set_padding(0,0,12,0)
          alignment.add(vbox2)
          frame.add(alignment)

          prefimg = gtk.Image()
          prefimg.set_from_icon_name('gtk-preferences',1)

          morebutton = self.window.add_button(_('_Advanced'),0)
          morebutton.set_property('image',prefimg)
        
          expertbutton = self.window.add_button(_('_Expert'),0)
          expertimg = gtk.Image()
          expertimg.set_from_icon_name('preferences-desktop-personal',1)
          expertbutton.set_property('image',expertimg)

          closebutton = self.window.add_button(gtk.STOCK_CLOSE,0)
          closebutton.connect('clicked',self.destroy)

          if not command_exists('simple-ccsm'):
             morebutton.set_sensitive(False)

          if not command_exists('ccsm'):
             expertbutton.set_sensitive(False)

          morebutton.connect('clicked',self.launch_sccsm)
          expertbutton.connect('clicked',self.launch_ccsm)

          self.window.show_all()

          self.window.connect('destroy',self.destroy)

      def destroy(self,widget):
          sys.exit(0)

      def on_toggle(self,widget):
          if widget.get_active():
             process = subprocess.Popen([COMPIZ_LAUNCHER,'--replace'])
             procpoll = None
             for i in range(0,5):
                 time.sleep(1)
                 procpoll = process.poll()
             if not procpoll:
                 td = TimeoutDialog(self.togglebutton)
                 td.run()
          else:
             enable_metacity()

      def launch_sccsm(self,widget):
          subprocess.Popen(['simple-ccsm'])

      def launch_ccsm(self,widget):
          subprocess.Popen(['ccsm'])

      def integration_add_timeout(self,widget):
          gobject.timeout_add(500,self.toggle_integration,widget)
  
      def toggle_integration(self,widget):
          enable = widget.get_active()
          if enable:
              available_backends = [b.Name for b in self.Context.Backends.values()]
              if os.environ['DESKTOP_SESSION'] == "gnome" and 'gconf' in available_backends:
                 if self.Context.CurrentBackend.Name != 'gconf':
                    self.Context.CurrentBackend = self.Context.Backends['gconf']
              elif os.environ['DESKTOP_SESSION'] == "kde" and 'kconfig' in available_backends:
                 if self.Context.CurrentBackend.Name != 'kconfig':
                    self.Context.CurrentBackend = self.Context.Backends['kconfig']
              self.Context.Integration = True
          else:
              self.Context.CurrentBackend = self.Context.Backends['ini']
              self.Context.Integration = False
  

class TimeoutDialog:
      def __init__(self,togglebutton):
          self.togglebutton = togglebutton
          self.window = gtk.Dialog()
          self.window.set_resizable(False)
          self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
          self.window.set_title(_("Keep Settings"))

          mainbox = gtk.HBox()
          self.window.vbox.pack_start(mainbox)

          iconimg = gtk.Image()
          iconimg.set_from_icon_name('gtk-dialog-question',6)
          mainbox.pack_start(iconimg,padding=5)

          vbox = gtk.VBox()
          heading = gtk.Label(_("<b>Do you want to keep these settings?</b>"))
          heading.set_use_markup(True)
          vbox.pack_start(heading)
          self.label = gtk.Label(_("Testing the new settings. If you don't respond in 30 seconds, the previous settings will be restored."))
          self.label.set_line_wrap(True)
          vbox.pack_start(self.label)
          mainbox.pack_start(vbox)

          savebutton = self.window.add_button(_("Keep Settings"),0)
          cancelbutton = self.window.add_button(_("Use Previous Settings"),0)
          savebutton.connect('clicked',self.save)
          cancelbutton.connect('clicked',self.cancel)

          self.window.show_all()

      def save(self,widget):
          self._apply()
          gobject.source_remove(self.timeoutloop)
          self.window.destroy()

      def _apply(self):
          enable_compiz()

      def cancel(self,widget):
          self._revert()
          self.window.destroy()

      def _revert(self):
          enable_metacity()

      def run(self):
          timerdata = dict(timer=0,endtime=30)
          self.timeoutloop = gobject.timeout_add(1000, self.updatelabel,timerdata)

      def updatelabel(self,timerdata):
          timeleft = timerdata['endtime'] - timerdata['timer']
          self.label.set_label(_("Testing the new settings. If you don't respond in %d seconds, the previous settings will be restored." % timeleft))
          timerdata['timer'] += 1
          if timeleft < 0:
             self._revert()
             return False
          else: return True



a = MainWindow()
gtk.main()