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

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

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

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.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)


          heading = gtk.Label(_("<b>Desktop Effects</b>"))
          heading.set_use_markup(True)
          heading.set_alignment(0,0)
          hbox = gtk.HBox(homogeneous=False,spacing=0)
          self.window.vbox.pack_start(hbox,padding=10)
          vbox = gtk.VBox(homogeneous=False,spacing=0)

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

          vbox.pack_start(img)
          hbox.pack_start(vbox,padding=10)
          vbox.pack_start(heading)
          vbox.pack_start(self.togglebutton)

          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'])


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(homogeneous=False,spacing=0)
          self.window.vbox.pack_start(mainbox,padding=10)

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

          vbox = gtk.VBox(homogeneous=False,spacing=0)
          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()