Guest User

Untitled

a guest
Oct 7th, 2010
153
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import gobject
  4. gobject.threads_init()
  5. import gtk
  6. import os
  7. import xml.dom.minidom
  8. import gconf
  9.  
  10. class Wallpaper:
  11.     def __init__(self):
  12.         # Create GTK Window
  13.         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  14.         self.window.set_title("Dynamic Wallpaper")
  15.         self.window.resize(350, 110)
  16.         self.window.set_resizable(False)
  17.        
  18.         # Create window objects
  19.         vbox = gtk.VBox()
  20.  
  21.         label = gtk.Label("Folder:")
  22.         label.set_alignment(0.0, 0.5)
  23.         self.folder_entry = gtk.Entry()
  24.         button = gtk.Button("Select...")
  25.         button.connect("clicked", self.button_clicked)
  26.         hbox = gtk.HBox()
  27.         hbox.pack_start(label, True)
  28.         hbox.pack_start(self.folder_entry, False)
  29.         hbox.pack_start(button, False)
  30.         vbox.pack_start(hbox, False)
  31.        
  32.         label = gtk.Label("Picture duration:")
  33.         label.set_alignment(0.0, 0.5)
  34.         adj = gtk.Adjustment(5.0, 1.0, 60.0, 1.0, 1.0, 0.0)
  35.         self.pdur_spinner = gtk.SpinButton(adj)
  36.         self.pdur_combo = gtk.combo_box_new_text()
  37.         self.pdur_combo.append_text("hr")
  38.         self.pdur_combo.append_text("min")
  39.         self.pdur_combo.append_text("sec")
  40.         self.pdur_combo.set_active(1)
  41.         hbox = gtk.HBox()
  42.         hbox.pack_start(label, True)
  43.         hbox.pack_start(self.pdur_spinner, False)
  44.         hbox.pack_start(self.pdur_combo, False)
  45.         vbox.pack_start(hbox, False)
  46.        
  47.         label = gtk.Label("Transition duration:")
  48.         label.set_alignment(0.0, 0.5)
  49.         adj = gtk.Adjustment(5.0, 0.0, 60.0, 1.0, 1.0, 0.0)
  50.         self.tdur_spinner = gtk.SpinButton(adj)
  51.         self.tdur_combo = gtk.combo_box_new_text()
  52.         self.tdur_combo.append_text("hr")
  53.         self.tdur_combo.append_text("min")
  54.         self.tdur_combo.append_text("sec")
  55.         self.tdur_combo.set_active(2)
  56.         hbox = gtk.HBox()
  57.         hbox.pack_start(label, True)
  58.         hbox.pack_start(self.tdur_spinner, False)
  59.         hbox.pack_start(self.tdur_combo, False)
  60.         vbox.pack_start(hbox, False)
  61.        
  62.         self.cancel_button = gtk.Button(stock=gtk.STOCK_CLOSE)
  63.         self.cancel_button.connect("clicked", self.destroy)
  64.         self.ok_button = gtk.Button(stock=gtk.STOCK_OK)
  65.         self.ok_button.set_sensitive(False)
  66.         self.ok_button.connect("clicked", self.ok_button_clicked)
  67.         hbox = gtk.HBox()
  68.         hbox.pack_end(self.ok_button, False)
  69.         hbox.pack_end(self.cancel_button, False)
  70.         vbox.pack_end(hbox, False, False, 10)
  71.  
  72.         # Connect signal handlers
  73.         self.window.connect("delete-event", self.delete_event)
  74.         self.window.connect("destroy", self.destroy)
  75.  
  76.         # Show entire window
  77.         self.window.add(vbox)
  78.         self.window.show_all()
  79.  
  80.     def destroy(self, widget):
  81.         gtk.main_quit()
  82.        
  83.     def button_clicked(self, button):
  84.         dialog = gtk.FileChooserDialog("Open..", None, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
  85.                                        (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
  86.                                         gtk.STOCK_OK, gtk.RESPONSE_OK))
  87.         dialog.set_default_response(gtk.RESPONSE_OK)
  88.         dialog.set_filename(os.path.expanduser("~/Pictures/*"))
  89.         response = dialog.run()
  90.         filename = dialog.get_filename()
  91.         dialog.destroy()
  92.  
  93.         if response == gtk.RESPONSE_OK:
  94.             self.folder_entry.set_text(filename)
  95.             self.files = []
  96.             accepted = ('.jpg', '.jpeg', '.gif', '.png')
  97.             for file in os.listdir(filename):
  98.                 if os.path.splitext(file)[1] in accepted:
  99.                     self.files.append(os.path.join(filename, file))
  100.            
  101.             if len(self.files):
  102.                 self.ok_button.set_sensitive(True)
  103.             else:
  104.                 dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
  105.                     type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
  106.                     message_format = "Folder contains no images")
  107.                 dialog.set_title('Error')
  108.                 dialog.connect('response', lambda dialog, response: dialog.destroy())
  109.                 dialog.run()
  110.        
  111.     def ok_button_clicked(self, button):
  112.         nfiles = len(self.files)
  113.        
  114.         if nfiles <= 0:
  115.             return False
  116.        
  117.         folder = self.folder_entry.get_text()
  118.  
  119.         picture_duration = int(self.pdur_spinner.get_text())
  120.         if self.pdur_combo.get_active_text() == "min":
  121.             picture_duration = picture_duration * 60
  122.         elif self.pdur_combo.get_active_text() == "hr":
  123.             picture_duration = picture_duration * 60 * 60
  124.        
  125.         transition_duration = int(self.tdur_spinner.get_text())
  126.         if self.tdur_combo.get_active_text() == "min":
  127.             transition_duration = transition_duration * 60
  128.         elif self.tdur_combo.get_active_text() == "hr":
  129.             transition_duration = transition_duration * 60 * 60
  130.                
  131.         document = """<background><starttime><year>2009</year><month>08</month><day>04</day><hour>00</hour><minute>00</minute><second>00</second></starttime></background>"""
  132.         dom = xml.dom.minidom.parseString(document)
  133.        
  134.         for i in range(0, nfiles):
  135.             static = dom.createElement("static")
  136.             duration = dom.createElement("duration")
  137.             duration.appendChild(dom.createTextNode(str(picture_duration)))
  138.             static.appendChild(duration)
  139.             file = dom.createElement("file")
  140.             file.appendChild(dom.createTextNode(self.files[i]))
  141.             static.appendChild(file)
  142.             dom.documentElement.appendChild(static)
  143.  
  144.             if transition_duration <= 0: continue
  145.  
  146.             transition = dom.createElement("transition")
  147.             duration = dom.createElement("duration")
  148.             duration.appendChild(dom.createTextNode(str(transition_duration)))
  149.             transition.appendChild(duration)
  150.             frm = dom.createElement("from")
  151.             frm.appendChild(dom.createTextNode(self.files[i]))
  152.             transition.appendChild(frm)
  153.             to = dom.createElement("to")
  154.             if i < nfiles-1:
  155.                 to.appendChild(dom.createTextNode(self.files[i+1]))
  156.             else:
  157.                 to.appendChild(dom.createTextNode(self.files[0]))
  158.             transition.appendChild(to)
  159.             dom.documentElement.appendChild(transition)
  160.            
  161.         try:
  162.             save_filename = os.path.join(folder, "background-1.xml")
  163.             f = open(save_filename, 'w')
  164.             f.write(dom.toxml("utf-8"))
  165.             f.close()
  166.         except IOError as (errno, strerror):
  167.             try:
  168.                 save_filename = os.path.join(os.path.expanduser("~"), ".dynamic-wallpaper.xml")
  169.                 f = open(save_filename, 'w')
  170.                 f.write(dom.toxml("utf-8"))
  171.                 f.close()
  172.             except Error as (errno, strerror):
  173.                 dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
  174.                     type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
  175.                     message_format = strerror)
  176.                 dialog.set_title('Error')
  177.                 dialog.connect('response', lambda dialog, response: dialog.destroy())
  178.                 dialog.run()
  179.                 return False
  180.         except Error as (errno, strerror):
  181.                 dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
  182.                     type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
  183.                     message_format = strerror)
  184.                 dialog.set_title('Error')
  185.                 dialog.connect('response', lambda dialog, response: dialog.destroy())
  186.                 dialog.run()
  187.                 return False
  188.        
  189.         backgrounds_file = os.path.join(os.path.expanduser("~/.gnome2/"), "backgrounds.xml")
  190.         doc = xml.dom.minidom.parse(backgrounds_file)
  191.        
  192.         wallpaper = dom.createElement("wallpaper")
  193.         wallpaper.setAttribute("deleted", "false")
  194.         name = dom.createElement("name")
  195.         name.appendChild(dom.createTextNode("Dynamic Wallpaper"))
  196.         filename = dom.createElement("filename")
  197.         filename.appendChild(dom.createTextNode(save_filename))
  198.         options = dom.createElement("options")
  199.         options.appendChild(dom.createTextNode("stretched"))
  200.         shade_type = dom.createElement("shade_type")
  201.         shade_type.appendChild(dom.createTextNode("solid"))
  202.         pcolor = dom.createElement("pcolor")
  203.         pcolor.appendChild(dom.createTextNode("#2c2c00001e1e"))
  204.         scolor = dom.createElement("scolor")
  205.         scolor.appendChild(dom.createTextNode("#2c2c00001e1e"))
  206.         wallpaper.appendChild(name)
  207.         wallpaper.appendChild(filename)
  208.         wallpaper.appendChild(options)
  209.         wallpaper.appendChild(shade_type)
  210.         wallpaper.appendChild(pcolor)
  211.         wallpaper.appendChild(scolor)
  212.         doc.documentElement.appendChild(wallpaper)
  213.  
  214.         try:
  215.             f = open(backgrounds_file, 'w')
  216.             f.write(doc.toxml("utf-8"))
  217.             f.close()
  218.         except Error as (errno, strerror):
  219.                 dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
  220.                     type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
  221.                     message_format = strerror)
  222.                 dialog.set_title('Error')
  223.                 dialog.connect('response', lambda dialog, response: dialog.destroy())
  224.                 dialog.run()
  225.                 return False
  226.        
  227.         conf_client = gconf.client_get_default()
  228.         conf_client.add_dir("/desktop/gnome/background", gconf.CLIENT_PRELOAD_NONE)
  229.         conf_client.set_string("/desktop/gnome/background/picture_filename", save_filename)
  230.         conf_client.set_string("/desktop/gnome/background/picture_options", "strecthed")
  231.         conf_client.set_string("/desktop/gnome/background/color_shading_type", "solid")
  232.         conf_client.set_string("/desktop/gnome/background/primary_color", "#2c2c00001e1e")
  233.         conf_client.set_string("/desktop/gnome/background/secondary_color", "#2c2c00001e1e")
  234.  
  235.         gtk.main_quit()
  236.  
  237.     def delete_event(self, widget, event):
  238.         return False
  239.  
  240. if __name__ == "__main__":
  241.     wallpaper = Wallpaper()
  242.     gtk.main()
RAW Paste Data