Posted by iggykoopa on Fri 28th Aug 13:58 (modification of post by view diff)
download | new post
- #!/usr/bin/env python
- import pygtk,gtk,gtk.glade,os,sys,tarfile,shutil,commands
- class crunch_themer:
- def __init__(self):
- self.gladefile = "crunch-themer.glade"
- self.wTree = gtk.glade.XML (self.gladefile)
- dic = { "openFileChooser" : self.openFileChooser,
- "chooser_ok_clicked" : self.chooser_ok_clicked,
- "chooser_cancel_clicked" : self.chooser_cancel_clicked,
- "invalid_file_ok_clicked" : self.invalid_file_ok_clicked,
- "applied_ok_clicked" : self.applied_ok_clicked,
- "apply_button_clicked" : self.apply_theme,
- "main_quit" : self.main_quit}
- self.wTree.signal_autoconnect(dic)
- self.main_window = self.wTree.get_widget("main_window")
- self.file_chooser = self.wTree.get_widget("filechooserdialog")
- self.invalid_file_dialog = self.wTree.get_widget("invalid_file_dialog")
- self.theme_applied_dialog = self.wTree.get_widget("theme_applied_dialog")
- self.cb_ob = self.wTree.get_widget("cb_ob")
- self.cb_gtk = self.wTree.get_widget("cb_gtk")
- self.cb_gdm = self.wTree.get_widget("cb_gdm")
- self.cb_mouse = self.wTree.get_widget("cb_mouse")
- self.cb_icon = self.wTree.get_widget("cb_icon")
- self.cb_conky = self.wTree.get_widget("cb_conky")
- self.cb_wallpaper = self.wTree.get_widget("cb_wallpaper")
- self.preview_image = self.wTree.get_widget("preview_image")
- self.theme_selected = False
- self.user = commands.getoutput("logname")
- self.home = "/home/" + self.user
- self.main_window.show()
- def openFileChooser(self,widget,item):
- self.file_chooser.show()
- def chooser_ok_clicked(self,widget):
- self.file_chooser.hide()
- self.file_selected(self.file_chooser.get_filename())
- def applied_ok_clicked(self,widget):
- self.theme_applied_dialog.hide()
- def apply_theme(self,widget):
- if self.cb_ob.get_active():
- if not os.path.exists(self.home + "/.themes/" + self.ob_theme):
- shutil.copytree("/tmp/crunch_theme/" + self.theme_name + "/ob_theme/" + self.ob_theme, self.home + "/.themes/" + self.ob_theme)
- self.rc = open(self.home + "/.config/openbox/rc.xml")
- self.intheme = 0
- self.rc_new = ""
- for line in self.rc:
- if "<theme>" in line:
- self.intheme = 1
- if self.intheme:
- if "<name>" in line:
- line = "<name>" + self.ob_theme + "</name>\n"
- self.intheme = 0
- self.rc_new = self.rc_new + line
- self.rc_old = open(self.home + "/.config/openbox/rc.xml", "w")
- self.rc_old.write(self.rc_new)
- self.theme_applied_dialog.show()
- if self.cb_gtk.get_active():
- if not os.path.exists(self.home + "/.themes/" + self.gtk_theme):
- shutil.copytree("/tmp/crunch_theme/" + self.theme_name + "/gtk_theme/" + self.gtk_theme, self.home + "/.themes/" + self.gtk_theme)
- if self.cb_icon.get_active():
- if not os.path.exists(self.home + "/.icons/" + self.icon_theme):
- shutil.copytree("/tmp/crunch_theme/" + self.theme_name + "/icon_theme/" + self.icon_theme, self.home + "/.icons/" + self.icon_theme)
- if self.cb_gdm.get_active():
- if not os.path.exists("/usr/share/gdm/themes/" + self.gdm_theme):
- shutil.copytree("/tmp/crunch_theme/" + self.theme_name + "/gdm_theme/" + self.gdm_theme, "/usr/share/gdm/themes/" + self.gdm_theme)
- self.gdm_conf = open("/etc/gdm/gdm.conf-custom")
- self.gdm_conf_new = ""
- for line in self.gdm_conf:
- if "GraphicalTheme=" in line:
- line = "GraphicalTheme=" + self.gdm_theme + "\n"
- if "GraphicalThemes=" in line:
- line = "GraphicalThemes=" + self.gdm_theme + "\n"
- self.gdm_conf_new = self.gdm_conf_new + line
- self.gdm_conf_old = open("/etc/gdm/gdm.conf-custom", "w")
- self.gdm_conf_old.write(self.gdm_conf_new)
- if self.cb_mouse.get_active():
- if not os.path.exists(self.home + "/.icons/" + self.mouse_theme):
- shutil.copytree("/tmp/crunch_theme/" + self.theme_name + "/mouse_theme/" + self.mouse_theme, self.home + "/.icons/" + self.mouse_theme)
- if not os.path.exists(self.home + "/.Xdefaults"):
- os.system("touch " + self.home + "/.Xdefaults")
- self.mouse_index = open(self.home + "/.Xdefaults")
- self.new_mouse_index = ""
- cursor_set = 0
- for line in self.mouse_index.read().split("\n"):
- if "Xcursor.theme" in line:
- line = "Xcursor.theme:" + self.mouse_theme + "\n"
- cursor_set = 1
- self.new_mouse_index = self.new_mouse_index + line
- if not cursor_set:
- self.new_mouse_index = self.new_mouse_index + "Xcursor.theme:" + self.mouse_theme
- self.old_mouse_index = open(self.home + "/.Xdefaults", "w")
- self.old_mouse_index.write(self.new_mouse_index)
- if self.cb_wallpaper.get_active():
- bg_cfg_old = open("/tmp/crunch_theme/" + self.theme_name + "/wallpaper_theme/bg-saved.cfg")
- bg_cfg_new = open(self.home + "/.config/nitrogen/bg-saved.cfg", "w")
- bg_cfg = ""
- for line in bg_cfg_old.read().split("\n"):
- if "file" in line:
- line = "file=" + self.home + "/images/wallpaper/wallpaper.png"
- bg_cfg = bg_cfg + line + "\n"
- bg_cfg_new.write(bg_cfg)
- bg_cfg_new.close()
- shutil.copy("/tmp/crunch_theme/" + self.theme_name + "/wallpaper_theme/wallpaper.png", self.home + "/images/wallpaper")
- os.system("nitrogen --restore")
- if self.cb_gtk.get_active() or self.cb_mouse.get_active():
- self.gtk = open("/tmp/crunch_theme/" + self.theme_name + "/.gtkrc-2.0").read()
- self.gtk = self.gtk.replace("*homedir*", self.home)
- self.gtk_old = open(self.home + "/.gtkrc-2.0", "w")
- self.gtk_old.write(self.gtk)
- os.system("chown " + self.user + ":" + self.user + " " + self.home + "/images/wallpaper/*")
- os.system("chown " + self.user + ":" + self.user + " " + self.home + "/.themes/*")
- os.system("chown " + self.user + ":" + self.user + " " + self.home + "/.config/nitrogen/bg-saved.cfg")
- os.system("chown " + self.user + ":" + self.user + " " + self.home + "/.gtkrc-2.0")
- os.system("chown " + self.user + ":" + self.user + " " + self.home + "/.Xdefaults")
- os.system("chown " + self.user + ":" + self.user + " " + self.home + "/.icons/*")
- def check_theme(self,theme_file):
- self.theme_name = theme_file.replace(".tar.bz2","").split("/")
- self.theme_name = self.theme_name[-1]
- self.ob_theme = os.listdir("/tmp/crunch_theme/" + self.theme_name + "/ob_theme")
- if self.ob_theme:
- self.ob_theme = self.ob_theme[0]
- self.gtk_theme = os.listdir("/tmp/crunch_theme/" + self.theme_name + "/gtk_theme")
- if self.gtk_theme:
- self.gtk_theme = self.gtk_theme[0]
- self.icon_theme = os.listdir("/tmp/crunch_theme/" + self.theme_name + "/icon_theme")
- if self.icon_theme:
- self.icon_theme = self.icon_theme[0]
- self.gdm_theme = os.listdir("/tmp/crunch_theme/" + self.theme_name + "/gdm_theme")
- if self.gdm_theme:
- self.gdm_theme = self.gdm_theme[0]
- self.mouse_theme = os.listdir("/tmp/crunch_theme/" + self.theme_name + "/mouse_theme")
- if self.mouse_theme:
- self.mouse_theme = self.mouse_theme[0]
- self.wallpaper_theme = os.listdir("/tmp/crunch_theme/" + self.theme_name + "/wallpaper_theme")
- self.main_dir = os.listdir("/tmp/crunch_theme/" + self.theme_name)
- if self.ob_theme:
- self.cb_ob.set_active(True)
- else:
- self.cb_ob.set_active(False)
- if self.gtk_theme:
- self.cb_gtk.set_active(True)
- else:
- self.cb_gtk.set_active(False)
- if self.icon_theme:
- self.cb_icon.set_active(True)
- else:
- self.cb_icon.set_active(False)
- if self.gdm_theme:
- self.cb_gdm.set_active(True)
- else:
- self.cb_gdm.set_active(False)
- if self.mouse_theme:
- self.cb_mouse.set_active(True)
- else:
- self.cb_mouse.set_active(False)
- if self.wallpaper_theme:
- self.cb_wallpaper.set_active(True)
- else:
- self.cb_wallpaper.set_active(False)
- if "preview.png" in self.main_dir:
- self.preview_image.set_from_file("/tmp/crunch_theme/" + self.theme_name + "/preview.png")
- def file_selected(self,selected_file):
- if ".tar.bz2" in selected_file:
- theme = tarfile.open(selected_file)
- #setup temp directory
- if os.path.exists("/tmp/crunch_theme"):
- shutil.rmtree("/tmp/crunch_theme")
- theme.extractall("/tmp/crunch_theme")
- self.theme_selected = True
- self.check_theme(selected_file)
- else:
- self.invalid_file_dialog.show()
- def invalid_file_ok_clicked(self,widget):
- self.invalid_file_dialog.hide()
- def chooser_cancel_clicked(self,widget):
- self.file_chooser.hide()
- def main_quit(self,widget,item):
- gtk.main_quit()
- if __name__ == "__main__":
- themer = crunch_themer()
- gtk.main()
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.