Spelleng fix.
[uzbl-mobile] / examples / data / uzbl / scripts / uzbl_tabbed.py
index a0500e7..485adcc 100755 (executable)
@@ -1,9 +1,9 @@
 #!/usr/bin/python
 
 # Uzbl tabbing wrapper using a fifo socket interface
-# Copywrite (c) 2009, Tom Adams <tom@holizz.com>
-# Copywrite (c) 2009, quigybo <?>
-# Copywrite (c) 2009, Mason Larobina <mason.larobina@gmail.com>
+# Copyright (c) 2009, Tom Adams <tom@holizz.com>
+# Copyright (c) 2009, quigybo <?>
+# Copyright (c) 2009, Mason Larobina <mason.larobina@gmail.com>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -32,7 +32,8 @@
 #       and inherit configuration options from the user's uzbl config.
 #
 # Contributor(s):
-#   (None yet)
+#   mxey <mxey@ghosthacking.net>
+#       uzbl_config path now honors XDG_CONFIG_HOME if it exists.
 
 
 # Issues: 
@@ -85,7 +86,10 @@ else:
 # === Default Configuration ====================================================
 
 # Location of your uzbl configuration file.
-uzbl_config = os.path.join(os.environ['HOME'],'.config/uzbl/config')
+if 'XDG_CONFIG_HOME' in os.environ.keys() and os.environ['XDG_CONFIG_HOME']:
+    uzbl_config = os.path.join(os.environ['XDG_CONFIG_HOME'], 'uzbl/config')
+else:
+    uzbl_config = os.path.join(os.environ['HOME'],'.config/uzbl/config')
 
 # All of these settings can be inherited from your uzbl config file.
 config = {'show_tabs': True,
@@ -95,8 +99,10 @@ config = {'show_tabs': True,
   'fifo_dir': '/tmp',
   'icon_path': os.path.join(data_dir, 'uzbl.png'),
   'session_file': os.path.join(data_dir, 'session'),
-  'tab_colours': 'foreground = "#000000"',
-  'selected_tab': 'foreground = "#000000" background="#bbbbbb"',
+  'tab_colours': 'foreground = "#999"',
+  'tab_text_colours': 'foreground = "#444"',
+  'selected_tab': 'foreground = "#aaa" background="#303030"',
+  'selected_tab_text': 'foreground = "green"',
   'window_size': "800,800",
   'monospace_size': 10, 
   'bind_new_tab': 'gn',
@@ -174,9 +180,18 @@ class UzblTabbed:
             self._outgoing = []
             self._configured = False
 
+            # Probe commands
+            self._probeurl = 'sh \'echo "url %s $6" > "%s"\'' % (self.pid,\
+              self.parent.fifo_socket)
+            
+            # As soon as the variable expansion bug is fixed in uzbl
+            # I can start using this command to fetch the winow title
+            self._probetitle = 'sh \'echo "title %s @window_title" > "%s"\'' \
+              % (self.pid, self.parent.fifo_socket)
+
             # When notebook tab deleted the kill switch is raised.
             self._kill = False
-            
+             
             # Queue binds for uzbl child
             self.parent.config_uzbl(self)
 
@@ -226,19 +241,20 @@ class UzblTabbed:
             
             # Ugly way of getting the socket path. Screwed if fifo is in any
             # other part of the fifo socket path.
-
             socket = 'socket'.join(self.fifo.split('fifo'))
-            
-            # I feel so dirty
+            # Hackish & wasteful way of getting the window title. 
             subcmd = 'print title %s @<document.title>@' % self.pid
             cmd = 'uzblctrl -s "%s" -c "%s" > "%s" &' % (socket, subcmd, \
               self.parent.fifo_socket)
-
             subprocess.Popen([cmd], shell=True)
+            self.send(self._probeurl)
+            
+            # Wont work yet.
+            #self.send(self._probetitle)
 
             self._lastprobe = time.time()
-            
-        
+
+
         def send(self, msg):
             '''Child fifo write function.'''
 
@@ -292,7 +308,7 @@ class UzblTabbed:
             self.tablist.set_justify(gtk.JUSTIFY_LEFT)
             self.tablist.set_line_wrap(False)
             self.tablist.set_selectable(False)
-            self.tablist.set_padding(2,2)
+            self.tablist.set_padding(0,2)
             self.tablist.set_alignment(0,0)
             self.tablist.set_ellipsize(pango.ELLIPSIZE_END)
             self.tablist.set_text(" ")
@@ -506,7 +522,11 @@ class UzblTabbed:
             if len(cmd) > 2:
                 uzbl = self.get_uzbl_by_pid(int(cmd[1]))
                 if uzbl:
-                    setattr(uzbl, cmd[0], ' '.join(cmd[2:]))
+                    old = getattr(uzbl, cmd[0])
+                    new = ' '.join(cmd[2:])
+                    setattr(uzbl, cmd[0], new)
+                    if old != new:
+                       self.update_tablist()
                 else:
                     error("Cannot find uzbl instance with pid %r" % int(cmd[1]))
         else:
@@ -549,7 +569,9 @@ class UzblTabbed:
         # has been created. 
         timerid = gobject.timeout_add(100, uzbl.flush, "flush-initial-config")
         uzbl.timers['flush-initial-config'] = timerid
-       
+    
+        self.update_tablist()
+
 
     def config_uzbl(self, uzbl):
         '''Send bind commands for tab new/close/next/prev to a uzbl 
@@ -583,10 +605,12 @@ class UzblTabbed:
             page = notebook[n]
             i = notebook.index(page)
             self.notebook.set_current_page(i)
-
+        
         except IndexError:
             pass
 
+        self.update_tablist()
+
 
     def next_tab(self, n=1):
         '''Switch to next tab or n tabs right.'''
@@ -596,6 +620,8 @@ class UzblTabbed:
             pagen = self.notebook.get_current_page() + n
             self.notebook.set_current_page( pagen % numofpages ) 
 
+        self.update_tablist()
+
 
     def prev_tab(self, n=1):
         '''Switch to prev tab or n tabs left.'''
@@ -607,6 +633,8 @@ class UzblTabbed:
                 pagen += numofpages
             self.notebook.set_current_page(pagen)
 
+        self.update_tablist()
+
 
     def close_tab(self, tabid=None):
         '''Closes current tab. Supports negative indexing.'''
@@ -632,6 +660,8 @@ class UzblTabbed:
         del self.pages[socket]
         self.notebook.remove_page(tabid)
 
+        self.update_tablist()
+
 
     def tab_closed(self, notebook, socket, page_num):
         '''Close the window if no tabs are left. Called by page-removed 
@@ -648,14 +678,14 @@ class UzblTabbed:
             del self.pages[socket]
         
         if self.notebook.get_n_pages() == 0:
-            gtk.main_quit()
+            self.quit()
+
+        self.update_tablist()
 
 
     def tab_changed(self, notebook, page, page_num):
         '''Refresh tab list. Called by switch-page signal.'''
 
-        self.tablist.set_text(str(list(self.notebook)))
-
         self.update_tablist()
 
 
@@ -664,9 +694,13 @@ class UzblTabbed:
 
         pango = ""
 
-        normal, selected = config['tab_colours'], config['selected_tab']
-        tab_format = "<span %s> [ %d %s ] </span>"
+        normal = (config['tab_colours'], config['tab_text_colours'])
+        selected = (config['selected_tab'], config['selected_tab_text'])
+        
+        tab_format = "<span %s> [ %d <span %s> %s</span> ] </span>"
         
+        title_format = "%s - Uzbl Browser"
+
         uzblkeys = self.pages.keys()
         curpage = self.notebook.get_current_page()
 
@@ -679,17 +713,20 @@ class UzblTabbed:
             
             if index == curpage:
                 colours = selected
+                self.window.set_title(title_format % uzbl.title)
+
             else:
                 colours = normal
             
-            pango += tab_format % (colours, index, uzbl.title)
+            pango += tab_format % (colours[0], index, colours[1], uzbl.title)
 
         self.tablist.set_markup(pango)
 
         return True
 
 
-    def quit(self, window, event):
+    #def quit(self, window, event):
+    def quit(self, *args):
         '''Cleanup the application and quit. Called by delete-event signal.'''
 
         for fd in self._watchers.keys():
@@ -707,21 +744,28 @@ class UzblTabbed:
         
         if config['save_session']:
             session_file = os.path.expandvars(config['session_file'])
-            if not os.path.isfile(session_file):
-                dirname = os.path.dirname(session_file)
-                rmkdir(dirname)
-            h = open(session_file, 'w')
-            h.write('current = %s\n' % self.notebook.get_current_page())
-            h.close()
-            for socket in list(self.notebook):
-                if socket not in self.pages.keys(): continue
-                uzbl = self.pages[socket]
-                uzbl.send('sh "echo $6 >> %s"' % session_file)
-                time.sleep(0.05)
-
-        gtk.main_quit() 
+            if self.notebook.get_n_pages():
+                if not os.path.isfile(session_file):
+                    dirname = os.path.dirname(session_file)
+                    if not os.path.isdir(dirname):
+                        rmkdir(dirname)
+
+                h = open(session_file, 'w')
+                h.write('current = %s\n' % self.notebook.get_current_page())
+                h.close()
+                for socket in list(self.notebook):
+                    if socket not in self.pages.keys(): continue
+                    uzbl = self.pages[socket]
+                    uzbl.send('sh "echo $6 >> %s"' % session_file)
+                    time.sleep(0.05)
 
+            else:
+                # Notebook has no pages so delete session file if it exists.
+                # Its better to not exist than be blank IMO. 
+                if os.path.isfile(session_file):
+                    os.remove(session_file)
 
+        gtk.main_quit() 
 
 
 if __name__ == "__main__":