This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

ob-autostart by AdComp

By: pablokal on May 14th, 2011  |  syntax: Python  |  size: 14.20 KB  |  views: 1,023  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python2
  2. # -*- coding: utf-8 -*-
  3.  
  4. ##
  5. #   OpenBox Autostart Control
  6. #
  7. #   Created by
  8. #       ADcomp <david.madbox@gmail.com>
  9. #       http://www.ad-comp.be/
  10. #
  11. #   This program is distributed under the terms of the GNU General Public License
  12. #   For more info see http://www.gnu.org/licenses/gpl.txt
  13. ##
  14.  
  15. try:
  16.     import os
  17.     import sys
  18.     import gtk
  19.     import gobject
  20.     import string
  21.     import pwd
  22.     import locale
  23.     import gettext
  24. except:
  25.     print("ERROR : All required python dependencies were not found!")
  26.     sys.exit()
  27.  
  28. ## Not yet !
  29. gettext.install('ob-autostart', './locale', unicode=1)
  30.  
  31. LAUNCH_LIST = []
  32.  
  33. # position in list
  34. ID_ACTIVE = 0
  35. ID_DESC = 1
  36. ID_CMD = 2
  37. ID_TIMER = 3
  38.  
  39. # Find the Name / Command / Icon from .desktop file
  40. def info_desktop_file(file):
  41.     name, cmd = None, None
  42.     try:
  43.         cfile = open(file,"r")
  44.  
  45.         for line in cfile:
  46.             if '=' in line:
  47.                 words = line.split('=')
  48.                 if words[0] == 'Name':
  49.                     name = words[1].replace('\n','')
  50.  
  51.                 if words[0] == 'Exec':
  52.                     cmd = words[1].replace('\n','')
  53.                     cmd = cmd.split(' ')[0]
  54.         cfile.close()
  55.     except:
  56.         print "Error : .desktop file info"
  57.     return (name, cmd)
  58.  
  59. #----------------------------------------------------------------------------
  60. class Conf:
  61. #----------------------------------------------------------------------------
  62.     def __init__(self):
  63.         # Create window
  64.         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  65.         self.window.set_title(_("OB Autostart"))
  66.         self.window.set_position(gtk.WIN_POS_CENTER)
  67.         self.window.connect("destroy", self.destroy)
  68.  
  69.         # Containers
  70.         BoxBase = gtk.VBox()
  71.         BoxBase.set_spacing(10)
  72.         BoxBase.set_border_width(5)
  73.  
  74.         BoxList = gtk.VBox()
  75.         BoxList.set_spacing(5)
  76.         BoxList.set_border_width(5)
  77.  
  78.         BoxListControls = gtk.HButtonBox()
  79.  
  80.         BoxControls = gtk.HButtonBox()
  81.         BoxControls.set_spacing(5)
  82.         BoxControls.set_layout(gtk.BUTTONBOX_END)
  83.  
  84.         #Icon ListStore
  85.         self.treeview = gtk.TreeView()
  86.         model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
  87.         self.filtremodele = model.filter_new()
  88.         self.treeview.set_model(model)
  89.  
  90.         cell_text = gtk.CellRendererText()
  91.         col_text = gtk.TreeViewColumn(_("A"), cell_text, text=ID_ACTIVE)
  92.         self.treeview.append_column(col_text)
  93.  
  94.         cell_text = gtk.CellRendererText()
  95.         col_text = gtk.TreeViewColumn(_("Description"), cell_text, text=ID_DESC)
  96.         self.treeview.append_column(col_text)
  97.  
  98.         cell_command = gtk.CellRendererText()
  99.         col_command = gtk.TreeViewColumn(_("command"), cell_command, text=ID_CMD)
  100.         #~ col_command.set_visible(False)
  101.         self.treeview.append_column(col_command)
  102.  
  103.         cell_icon = gtk.CellRendererText()
  104.         col_icon = gtk.TreeViewColumn(_("Time"), cell_icon, text=ID_TIMER)
  105.         #~ col_icon.set_visible(True)
  106.         self.treeview.append_column(col_icon)
  107.  
  108.         for my_item in LAUNCH_LIST:
  109.             item = model.append(None)
  110.             desc , cmd , timer, isactive = my_item
  111.  
  112.             model.set_value(item, ID_ACTIVE, str(isactive))
  113.             model.set_value(item, ID_DESC, str(desc))
  114.             model.set_value(item, ID_CMD, str(cmd))
  115.             model.set_value(item, ID_TIMER, str(timer))
  116.  
  117.         self.treeview.connect("row-activated", self.EditItem)
  118.         self.treeview.set_reorderable(True)
  119.         self.treeview.get_selection().set_mode(gtk.SELECTION_SINGLE)
  120.  
  121.         ## Control buttons for list
  122.         # Add
  123.         button = gtk.Button(stock=gtk.STOCK_ADD)
  124.         button.connect("clicked", self.button_control_new_clicked)
  125.         BoxListControls.pack_start(button)
  126.         # Delete
  127.         button = gtk.Button(stock=gtk.STOCK_REMOVE)
  128.         button.connect("clicked", self.button_control_delete_clicked)
  129.         BoxListControls.pack_start(button)
  130.  
  131.         ## Main Controls
  132.         # Save
  133.         button_save = gtk.Button(stock=gtk.STOCK_SAVE)
  134.         button_save.connect("clicked", self.saveconf)
  135.         BoxControls.pack_end(button_save, False, False)
  136.         # Exit
  137.         button_exit = gtk.Button(stock=gtk.STOCK_CLOSE)
  138.         button_exit.connect("clicked", self.destroy)
  139.         BoxControls.pack_end(button_exit, False, False)
  140.  
  141.         BoxList.pack_start(self.treeview, True, True)
  142.         BoxList.pack_end(BoxListControls, False, True)
  143.  
  144.         BoxBase.pack_start(BoxList, True)
  145.         BoxBase.pack_start(gtk.HSeparator(), True)
  146.         BoxBase.pack_end(BoxControls, False)
  147.  
  148.         self.window.add(BoxBase)
  149.  
  150.         ## Show main window frame and all content
  151.         self.window.show_all()
  152.  
  153.     def saveconf(self, widget=None, event=None):
  154.         '''Save configuration'''
  155.         model = self.treeview.get_model()
  156.         iter = model.get_iter_root()
  157.         launch_list = []
  158.  
  159.         while (iter):
  160.             tmp_data = (
  161.                 model.get_value(iter, ID_DESC),
  162.                 model.get_value(iter, ID_CMD),
  163.                 model.get_value(iter, ID_TIMER),
  164.                 model.get_value(iter, ID_ACTIVE)
  165.                 )
  166.             launch_list.append(tmp_data)
  167.             iter = model.iter_next(iter)
  168.  
  169.         home = pwd.getpwuid(os.getuid())[5]
  170.         if not os.path.exists("%s/.config/ob-autostart" % home):
  171.             os.makedirs("%s/.config/ob-autostart" % home)
  172.  
  173.         src = "%s/.config/ob-autostart/config" % home
  174.         cfile = open(src,"w")
  175.         cfile.write("# OB_Autostart config\n")
  176.         cfile.write("LAUNCH_LIST = [ \n")
  177.         for item in launch_list:
  178.             desc = item[0]
  179.             timer = str(item[2])
  180.             isactive = item[3]
  181.             cmd = item[1].replace("\"","\\\"")
  182.             output = '\t ("%s","%s","%s","%s"),\n' % (desc,cmd,timer,isactive)
  183.             cfile.write(output)
  184.         cfile.write("\t]\n")
  185.  
  186.         if (cfile.closed == False):
  187.             cfile.close()
  188.  
  189.     def EditItem(self, treeview, path=None, view_column=None):
  190.         nsel = treeview.get_cursor()
  191.         selection = nsel[0][0]
  192.         if (selection is not None):
  193.             item = {}
  194.             model   = self.treeview.get_model()
  195.             item['desc'] = model.get_value(model.get_iter(selection), ID_DESC)
  196.             item['cmd'] = model.get_value(model.get_iter(selection), ID_CMD)
  197.             item['timer'] = model.get_value( model.get_iter(selection), ID_TIMER )
  198.             item['is_active'] = model.get_value( model.get_iter(selection), ID_ACTIVE )
  199.             Edit_Item(self.window, item, model, selection)
  200.  
  201.     # Add new treeview object, position=-1 inserts  into cursor's position
  202.     def new_treeview_entry(self, position=-1, insertAfter=True):
  203.         self.treeview.grab_focus()
  204.         model = self.treeview.get_model()
  205.         try:
  206.             position = self.treeview.get_cursor()[0][0]
  207.             try:
  208.                 iter = model.get_iter(position)
  209.             except ValueError:
  210.                 print("> Empty list ?")
  211.                 iter = model.get_iter()
  212.  
  213.             if (insertAfter == True):
  214.                 item = model.insert_after(iter)
  215.             else:
  216.                 item = model.insert_before(iter)
  217.         except TypeError:
  218.             print "typeError _ treeview"
  219.             item = model.append(None)
  220.             self.treeview.grab_focus()
  221.  
  222.         model.set_value(item, ID_DESC, '')
  223.         model.set_value(item, ID_CMD, '')
  224.         model.set_value(item, ID_TIMER, '0')
  225.         model.set_value(item, ID_ACTIVE, '')
  226.  
  227.         ## Set focus to new entry and edit
  228.         path = model.get_path(model.get_iter(position+1))
  229.         self.treeview.set_cursor(path)
  230.         self.EditItem(self.treeview)
  231.  
  232.     def button_control_new_clicked(self, n):
  233.         self.new_treeview_entry()
  234.         return
  235.  
  236.     def button_control_delete_clicked(self, n):
  237.         # remove item from configuration
  238.         try:
  239.             self.treeview.grab_focus()
  240.             pos = self.treeview.get_cursor()[0][0]
  241.             model = self.treeview.get_model()
  242.             self.treeview.set_cursor(pos-1)
  243.             self.treeview.grab_focus()
  244.             model.remove(model.get_iter(pos))
  245.             if pos < 0:
  246.                 pos = 0
  247.             # Set the focus and cursor correctly
  248.             self.treeview.set_cursor(pos);
  249.             self.treeview.grab_focus()
  250.         except TypeError:
  251.             print( "> nothing to delete !?" )
  252.  
  253.     def main(self):
  254.         gtk.main()
  255.  
  256.     def destroy(self, widget=None, data=None):
  257.         self.window.hide()
  258.         gtk.main_quit()
  259.  
  260. #----------------------------------------------------------------------------
  261. class Edit_Item:
  262. #----------------------------------------------------------------------------
  263.     def __init__(self, master, item, model, selection):
  264.         self.model = model
  265.         self.selection = selection
  266.  
  267.         # Create window
  268.         self.master = master
  269.         self.window = gtk.Dialog(_("Edit Item"), master, gtk.DIALOG_MODAL, buttons=None)
  270.         self.window.set_default_size(400, 100)
  271.         self.window.set_position(gtk.WIN_POS_CENTER)
  272.  
  273.         self.frame = gtk.Frame()
  274.         self.frame.set_border_width(5)
  275.  
  276.         BoxOptions = gtk.VBox()
  277.         BoxOptions.set_border_width(10)
  278.         self.frame.add(BoxOptions)
  279.         BoxOptions.set_spacing(10)
  280.  
  281.         #Command options
  282.         label = gtk.Label(_("Description:"))
  283.  
  284.         self.description = gtk.Entry()
  285.         self.description.set_text(item['desc'])
  286.  
  287.         box = gtk.HBox()
  288.         box.pack_start(label, False, False)
  289.         box.pack_end(self.description, True, True)
  290.         BoxOptions.pack_start(box, False, True)
  291.  
  292.         #Command options
  293.         label = gtk.Label(_("Command:"))
  294.  
  295.         self.text_command = gtk.Entry()
  296.         command = item['cmd'].replace("\\\"", "\"")
  297.         self.text_command.set_text(command)
  298.  
  299.         self.button_command = gtk.Button("...")
  300.         self.button_command.connect("clicked", self.button_command_clicked)
  301.  
  302.         box = gtk.HBox()
  303.         box.pack_start(label, False, False)
  304.         box.pack_start(self.text_command, True, True)
  305.         box.pack_end(self.button_command, False, True)
  306.         BoxOptions.pack_start(box, False, True)
  307.  
  308.  
  309.         self.is_active_chk = gtk.CheckButton('is active')
  310.         self.is_active_chk.show()
  311.         if item['is_active']=="*":
  312.             self.is_active_chk.set_active(1)
  313.  
  314.         sep = gtk.VSeparator()
  315.  
  316.         #Icon options
  317.         label = gtk.Label("Sleep:")
  318.  
  319.         ## Timer _ Sleep
  320.         timer = float(item['timer'])
  321.         adjustment = gtk.Adjustment(value=timer, lower=0, upper=600, step_incr=1, page_incr=10, page_size=0)
  322.         self.sleep_time = gtk.SpinButton(adjustment=adjustment, climb_rate=0.0, digits=0)
  323.  
  324.         box = gtk.HBox()
  325.         box.pack_start(self.is_active_chk, False, False)
  326.         box.pack_start(sep, True, True)
  327.         box.pack_start(label, False, False)
  328.         box.pack_end(self.sleep_time, False, False)
  329.         BoxOptions.pack_start(box, False, True)
  330.  
  331.         self.window.vbox.pack_start(self.frame)
  332.  
  333.         bouton = gtk.Button(stock=gtk.STOCK_CANCEL)
  334.         bouton.connect("clicked", self.close)
  335.         self.window.action_area.pack_start(bouton, True, True, 0)
  336.         bouton.show()
  337.  
  338.         bouton = gtk.Button(stock=gtk.STOCK_OK)
  339.         bouton.connect("clicked", self.change_item)
  340.         self.window.action_area.pack_start(bouton, True, True, 0)
  341.         bouton.show()
  342.  
  343.         #Show main window frame and all content
  344.         self.window.show_all()
  345.         self.window.run()
  346.  
  347.     def change_item(self, data):
  348.         command = self.text_command.get_text()
  349.         desc = self.description.get_text()
  350.         timer = int(self.sleep_time.get_value())
  351.         isactive = ''
  352.         if self.is_active_chk.get_active():
  353.             isactive = "*"
  354.         item    = self.model.get_iter(self.selection)
  355.  
  356.         self.model.set_value(item, ID_DESC, desc)
  357.         self.model.set_value(item, ID_CMD, command)
  358.         self.model.set_value(item, ID_TIMER, timer)
  359.         self.model.set_value(item, ID_ACTIVE, isactive)
  360.         self.close()
  361.  
  362.     def close(self, data=None):
  363.         self.window.hide()
  364.         self.window.destroy()
  365.  
  366.     def button_command_clicked(self, n):
  367.         dialog = gtk.FileChooserDialog(_("Select command.."),
  368.                                         None,
  369.                                         gtk.FILE_CHOOSER_ACTION_OPEN,
  370.                                         (gtk.STOCK_CANCEL,
  371.                                         gtk.RESPONSE_CANCEL,
  372.                                         gtk.STOCK_OPEN,
  373.                                         gtk.RESPONSE_OK))
  374.         dialog.set_default_response(gtk.RESPONSE_OK)
  375.  
  376.         filter = gtk.FileFilter()
  377.         filter.set_name(_("All files"))
  378.         filter.add_pattern("*")
  379.         dialog.add_filter(filter)
  380.         dialog.set_current_folder('/usr/share/applications')
  381.  
  382.         response = dialog.run()
  383.         if response == gtk.RESPONSE_OK:
  384.             ## .desktop file selected ?
  385.             filename = dialog.get_filename()
  386.             if '.desktop' in filename:
  387.                 name, cmd = info_desktop_file(filename)
  388.                 self.text_command.set_text(cmd)
  389.                 self.description.set_text(name)
  390.             else:
  391.                 self.text_command.set_text(filename)
  392.         dialog.destroy()
  393.  
  394. if __name__ == "__main__":
  395.     home = pwd.getpwuid(os.getuid())[5]
  396.     os.chdir(home)
  397.     cfg_file = None
  398.  
  399.     if os.access("%s/.config/ob-autostart/config" % home, os.F_OK|os.R_OK):
  400.         execfile("%s/.config/ob-autostart/config" % home)
  401.     del home
  402.  
  403.     if len(sys.argv) == 2:
  404.         if sys.argv[1] == "-d":
  405.             for item in LAUNCH_LIST:
  406.                 cmd = item[1]
  407.                 timer = int(item[2])
  408.                 is_active = item[3]
  409.                 if is_active == '*' and cmd != "":
  410.                     if timer > 0:
  411.                         cmd = "sleep %ss && %s" % (timer,cmd)
  412.                     print cmd
  413.                     os.spawnl(os.P_NOWAIT, "/bin/sh", "/bin/sh", "-c", cmd)
  414.     else:
  415.         conf = Conf()
  416.         conf.main()
clone this paste RAW Paste Data