#!/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(_("Desktop Effects")) 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(_("Do you want to keep these settings?")) 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()