Advertisement
Guest User

Untitled

a guest
Jan 25th, 2013
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. import re, os
  2. from gi.repository import GLib, Gio, GObject
  3. import shutil
  4. import gconf
  5. import time
  6.  
  7. USER = os.getenv('USER')
  8. BG = Gio.Settings.new("org.gnome.desktop.background")
  9. GSETTINGS = Gio.Settings.new("org.gnome.desktop.interface")
  10. GCONF = gconf.client_get_default()
  11.  
  12. class Daemon:
  13.     def __init__ (self):
  14.         BG.connect("changed::picture-uri", self.change_theme)
  15.  
  16.     def change_theme (self, *_):
  17.         print "Waiting a few seconds before applying the changes..."
  18.         time.sleep(3)
  19.         self.write_changes(self.get_color ())
  20.         GCONF.set_string("/apps/metacity/general/theme", "Ambiance")
  21.         GSETTINGS.set_string("gtk-theme", "Ambiance")
  22.         GSETTINGS.set_string("gtk-theme", "Ambiance-chameleon")
  23.  
  24.     def get_color (self):
  25.         xprop_value = GLib.spawn_command_line_sync("/usr/bin/xprop -root _GNOME_BACKGROUND_REPRESENTATIVE_COLORS")
  26.         rgb_set = re.match(r".*\((\d+)\,(\d+),(\d+).*", xprop_value[1]).groups ()
  27.         color_hex = '%02x%02x%02x' % (int(rgb_set[0]), int(rgb_set[1]), int(rgb_set[2]))
  28.         return str(color_hex)
  29.  
  30.     def write_changes (self, color_hex):
  31.         try:
  32.             shutil.copytree("/usr/share/themes/Ambiance", "/home/%s/.themes/Ambiance-chameleon" % USER)
  33.         except:
  34.             pass
  35.         print "Changing the highlight to #"+color_hex
  36.         with open("/home/%s/.themes/Ambiance-chameleon/gtk-3.0/gtk.css" % USER, "r") as sources:
  37.             lines = sources.readlines()
  38.         with open("/home/%s/.themes/Ambiance-chameleon/gtk-3.0/gtk.css" % USER, "w") as sources:
  39.             for line in lines:
  40.                 if line.startswith('@define-color selected_bg_color '):
  41.                     line = re.sub(r'#.+',r'#'+color_hex+';', line)
  42.                 sources.write(line)
  43.  
  44.         with open("/home/%s/.themes/Ambiance-chameleon/gtk-2.0/gtkrc" % USER, "r") as sources:
  45.             lines = sources.readlines()
  46.         with open("/home/%s/.themes/Ambiance-chameleon/gtk-2.0/gtkrc" % USER, "w") as sources:
  47.             for line in lines:
  48.                 if line.startswith('gtk-color-scheme = '):
  49.                     line = re.sub(r'selected_bg_color:#[\w]+',r'selected_bg_color:#'+color_hex, line)
  50.                 sources.write(line)
  51.  
  52. if __name__ == "__main__":
  53.     daemon = Daemon()
  54.     GObject.MainLoop().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement