3 # Uzbl tabbing wrapper using a fifo socket interface
4 # Copyright (c) 2009, Tom Adams <tom@holizz.com>
5 # Copyright (c) 2009, Chris van Dijk <cn.vandijk@hotmail.com>
6 # Copyright (c) 2009, Mason Larobina <mason.larobina@gmail.com>
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # Tom Adams <tom@holizz.com>
24 # Wrote the original uzbl_tabbed.py as a proof of concept.
26 # Chris van Dijk (quigybo) <cn.vandijk@hotmail.com>
27 # Made signifigant headway on the old uzbl_tabbing.py script on the
28 # uzbl wiki <http://www.uzbl.org/wiki/uzbl_tabbed>
30 # Mason Larobina <mason.larobina@gmail.com>
31 # Rewrite of the uzbl_tabbing.py script to use a fifo socket interface
32 # and inherit configuration options from the user's uzbl config.
35 # mxey <mxey@ghosthacking.net>
36 # uzbl_config path now honors XDG_CONFIG_HOME if it exists.
40 # Because this version of uzbl_tabbed is able to inherit options from your main
41 # uzbl configuration file you may wish to configure uzbl tabbed from there.
42 # Here is a list of configuration options that can be customised and some
43 # example values for each:
45 # General tabbing options:
49 # gtk_tab_pos = (top|left|bottom|right)
50 # switch_to_new_tabs = 1
54 # new_tab_title = Loading
62 # icon_path = $HOME/.local/share/uzbl/uzbl.png
63 # session_file = $HOME/.local/share/uzbl/session
66 # status_background = #303030
67 # window_size = 800,800
69 # And the key bindings:
71 # bind_tab_from_clip = gY
72 # bind_tab_from_uri = go _
77 # bind_goto_first = g<
80 # And uzbl_tabbed.py takes care of the actual binding of the commands via each
81 # instances fifo socket.
84 # tab_colours = foreground = "#888" background = "#303030"
85 # tab_text_colours = foreground = "#bbb"
86 # selected_tab = foreground = "#fff"
87 # selected_tab_text = foreground = "green"
88 # tab_indicate_https = 1
89 # https_colours = foreground = "#888"
90 # https_text_colours = foreground = "#9c8e2d"
91 # selected_https = foreground = "#fff"
92 # selected_https_text = foreground = "gold"
94 # How these styling values are used are soley defined by the syling policy
95 # handler below (the function in the config section). So you can for example
96 # turn the tab text colour Firetruck-Red in the event "error" appears in the
97 # tab title or some other arbitrary event. You may wish to make a trusted
98 # hosts file and turn tab titles of tabs visiting trusted hosts purple.
102 # - new windows are not caught and opened in a new tab.
103 # - when uzbl_tabbed.py crashes it takes all the children with it.
104 # - when a new tab is opened when using gtk tabs the tab button itself
105 # grabs focus from its child for a few seconds.
106 # - when switch_to_new_tabs is not selected the notebook page is
107 # maintained but the new window grabs focus (try as I might to stop it).
111 # - add command line options to use a different session file, not use a
112 # session file and or open a uri on starup.
113 # - ellipsize individual tab titles when the tab-list becomes over-crowded
114 # - add "<" & ">" arrows to tablist to indicate that only a subset of the
115 # currently open tabs are being displayed on the tablist.
116 # - add the small tab-list display when both gtk tabs and text vim-like
117 # tablist are hidden (I.e. [ 1 2 3 4 5 ])
119 # - pass a uzbl socketid to uzbl_tabbed.py and have it assimilated into
120 # the collective. Resistance is futile!
121 # - on demand store the session to file (need binding & command for that)
142 sys.stderr.write("%s\n"%msg)
145 # ============================================================================
146 # ::: Default configuration section ::::::::::::::::::::::::::::::::::::::::::
147 # ============================================================================
149 # Location of your uzbl data directory.
150 if 'XDG_DATA_HOME' in os.environ.keys() and os.environ['XDG_DATA_HOME']:
151 data_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'uzbl/')
153 data_dir = os.path.join(os.environ['HOME'], '.local/share/uzbl/')
154 if not os.path.exists(data_dir):
155 error("Warning: uzbl data_dir does not exist: %r" % data_dir)
157 # Location of your uzbl configuration file.
158 if 'XDG_CONFIG_HOME' in os.environ.keys() and os.environ['XDG_CONFIG_HOME']:
159 uzbl_config = os.path.join(os.environ['XDG_CONFIG_HOME'], 'uzbl/config')
161 uzbl_config = os.path.join(os.environ['HOME'],'.config/uzbl/config')
162 if not os.path.exists(uzbl_config):
163 error("Warning: Cannot locate your uzbl_config file %r" % uzbl_config)
165 # All of these settings can be inherited from your uzbl config file.
168 'show_tablist': True, # Show text uzbl like statusbar tab-list
169 'show_gtk_tabs': False, # Show gtk notebook tabs
170 'tablist_top': True, # Display tab-list at top of window
171 'gtk_tab_pos': 'top', # Gtk tab position (top|left|bottom|right)
172 'switch_to_new_tabs': True, # Upon opening a new tab switch to it
175 'tab_titles': True, # Display tab titles (else only tab-nums)
176 'new_tab_title': 'Loading', # New tab title
177 'max_title_len': 50, # Truncate title at n characters
178 'show_ellipsis': True, # Show ellipsis when truncating titles
181 'save_session': True, # Save session in file when quit
182 'fifo_dir': '/tmp', # Path to look for uzbl fifo
183 'socket_dir': '/tmp', # Path to look for uzbl socket
184 'icon_path': os.path.join(data_dir, 'uzbl.png'),
185 'session_file': os.path.join(data_dir, 'session'),
188 'status_background': "#303030", # Default background for all panels
189 'window_size': "800,800", # width,height in pixels
192 'bind_new_tab': 'gn', # Open new tab.
193 'bind_tab_from_clip': 'gY', # Open tab from clipboard.
194 'bind_tab_from_uri': 'go _', # Open new tab and goto entered uri.
195 'bind_close_tab': 'gC', # Close tab.
196 'bind_next_tab': 'gt', # Next tab.
197 'bind_prev_tab': 'gT', # Prev tab.
198 'bind_goto_tab': 'gi_', # Goto tab by tab-number (in title)
199 'bind_goto_first': 'g<', # Goto first tab
200 'bind_goto_last': 'g>', # Goto last tab
202 # Add custom tab style definitions to be used by the tab colour policy
203 # handler here. Because these are added to the config dictionary like
204 # any other uzbl_tabbed configuration option remember that they can
205 # be superseeded from your main uzbl config file.
206 'tab_colours': 'foreground = "#888" background = "#303030"',
207 'tab_text_colours': 'foreground = "#bbb"',
208 'selected_tab': 'foreground = "#fff"',
209 'selected_tab_text': 'foreground = "green"',
210 'tab_indicate_https': True,
211 'https_colours': 'foreground = "#888"',
212 'https_text_colours': 'foreground = "#9c8e2d"',
213 'selected_https': 'foreground = "#fff"',
214 'selected_https_text': 'foreground = "gold"',
216 } # End of config dict.
218 # This is the tab style policy handler. Every time the tablist is updated
219 # this function is called to determine how to colourise that specific tab
220 # according the simple/complex rules as defined here. You may even wish to
221 # move this function into another python script and import it using:
222 # from mycustomtabbingconfig import colour_selector
223 # Remember to rename, delete or comment out this function if you do that.
225 def colour_selector(tabindex, currentpage, uzbl):
226 '''Tablist styling policy handler. This function must return a tuple of
227 the form (tab style, text style).'''
229 # Just as an example:
230 # if 'error' in uzbl.title:
231 # if tabindex == currentpage:
232 # return ('foreground="#fff"', 'foreground="red"')
233 # return ('foreground="#888"', 'foreground="red"')
235 # Style tabs to indicate connected via https.
236 if config['tab_indicate_https'] and uzbl.uri.startswith("https://"):
237 if tabindex == currentpage:
238 return (config['selected_https'], config['selected_https_text'])
239 return (config['https_colours'], config['https_text_colours'])
241 # Style to indicate selected.
242 if tabindex == currentpage:
243 return (config['selected_tab'], config['selected_tab_text'])
246 return (config['tab_colours'], config['tab_text_colours'])
249 # ============================================================================
250 # ::: End of configuration section :::::::::::::::::::::::::::::::::::::::::::
251 # ============================================================================
254 def readconfig(uzbl_config, config):
255 '''Loads relevant config from the users uzbl config file into the global
256 config dictionary.'''
258 if not os.path.exists(uzbl_config):
259 error("Unable to load config %r" % uzbl_config)
262 # Define parsing regular expressions
263 isint = re.compile("^(\-|)[0-9]+$").match
264 findsets = re.compile("^set\s+([^\=]+)\s*\=\s*(.+)$",\
265 re.MULTILINE).findall
267 h = open(os.path.expandvars(uzbl_config), 'r')
271 for (key, value) in findsets(rawconfig):
272 key, value = key.strip(), value.strip()
273 if key not in config.keys(): continue
274 if isint(value): value = int(value)
277 # Ensure that config keys that relate to paths are expanded.
278 expand = ['fifo_dir', 'socket_dir', 'session_file', 'icon_path']
280 config[key] = os.path.expandvars(config[key])
284 '''Recursively make directories.
285 I.e. `mkdir -p /some/nonexistant/path/`'''
287 path, sep = os.path.realpath(path), os.path.sep
288 dirs = path.split(sep)
289 for i in range(2,len(dirs)+1):
290 dir = os.path.join(sep,sep.join(dirs[:i]))
291 if not os.path.exists(dir):
296 '''To infinity and beyond!'''
305 '''Generates a random md5 for socket message-termination endmarkers.'''
307 return hashlib.md5(str(random.random()*time.time())).hexdigest()
311 '''A tabbed version of uzbl using gtk.Notebook'''
314 '''Uzbl instance meta-data/meta-action object.'''
316 def __init__(self, parent, tab, fifo_socket, socket_file, pid,\
321 self.fifo_socket = fifo_socket
322 self.socket_file = socket_file
324 self.title = config['new_tab_title']
332 # Switch to tab after loading
333 self._switch = switch
334 # fifo/socket files exists and socket connected.
335 self._connected = False
339 # Message termination endmarker.
340 self._marker = gen_endmarker()
342 # Gen probe commands string
344 probe = probes.append
345 probe('print uri %d @uri %s' % (self.pid, self._marker))
346 probe('print title %d @<document.title>@ %s' % (self.pid,\
348 self._probecmds = '\n'.join(probes)
350 # Enqueue keybinding config for child uzbl instance
351 self.parent.config_uzbl(self)
354 def flush(self, timer_call=False):
355 '''Flush messages from the socket-out and fifo-out queues.'''
362 error("Flush called on dead tab.")
365 if len(self._fifoout):
366 if os.path.exists(self.fifo_socket):
367 h = open(self.fifo_socket, 'w')
368 while len(self._fifoout):
369 msg = self._fifoout.pop(0)
373 if len(self._socketout):
374 if not self._socket and os.path.exists(self.socket_file):
375 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
376 sock.connect(self.socket_file)
380 while len(self._socketout):
381 msg = self._socketout.pop(0)
382 self._socket.send("%s\n"%msg)
384 if not self._connected and timer_call:
385 if not len(self._fifoout + self._socketout):
386 self._connected = True
388 if timer_call in self.timers.keys():
389 gobject.source_remove(self.timers[timer_call])
390 del self.timers[timer_call]
395 return len(self._fifoout + self._socketout)
399 '''Steal parent focus and switch the notebook to my own tab.'''
401 tabs = list(self.parent.notebook)
402 tabid = tabs.index(self.tab)
403 self.parent.goto_tab(tabid)
407 '''Probes the client for information about its self.'''
410 self.send(self._probecmds)
411 self._lastprobe = time.time()
414 def write(self, msg):
415 '''Child fifo write function.'''
417 self._fifoout.append(msg)
418 # Flush messages from the queue if able.
423 '''Child socket send function.'''
425 self._socketout.append(msg)
426 # Flush messages from queue if able.
431 '''Create tablist, window and notebook.'''
437 # Once a second is updated with the latest tabs' uris so that when the
438 # window is killed the session is saved.
440 # And index of current page in self._tabsuris
443 # Holds metadata on the uzbl childen open.
446 # Generates a unique id for uzbl socket filenames.
447 self.next_pid = counter().next
450 self.window = gtk.Window()
452 window_size = map(int, config['window_size'].split(','))
453 self.window.set_default_size(*window_size)
456 error("Invalid value for default_size in config file.")
458 self.window.set_title("Uzbl Browser")
459 self.window.set_border_width(0)
461 # Set main window icon
462 icon_path = config['icon_path']
463 if os.path.exists(icon_path):
464 self.window.set_icon(gtk.gdk.pixbuf_new_from_file(icon_path))
467 icon_path = '/usr/share/uzbl/examples/data/uzbl/uzbl.png'
468 if os.path.exists(icon_path):
469 self.window.set_icon(gtk.gdk.pixbuf_new_from_file(icon_path))
471 # Attach main window event handlers
472 self.window.connect("delete-event", self.quit)
475 if config['show_tablist']:
477 self.window.add(vbox)
478 ebox = gtk.EventBox()
479 self.tablist = gtk.Label()
480 self.tablist.set_use_markup(True)
481 self.tablist.set_justify(gtk.JUSTIFY_LEFT)
482 self.tablist.set_line_wrap(False)
483 self.tablist.set_selectable(False)
484 self.tablist.set_padding(2,2)
485 self.tablist.set_alignment(0,0)
486 self.tablist.set_ellipsize(pango.ELLIPSIZE_END)
487 self.tablist.set_text(" ")
489 ebox.add(self.tablist)
491 bgcolor = gtk.gdk.color_parse(config['status_background'])
492 ebox.modify_bg(gtk.STATE_NORMAL, bgcolor)
495 self.notebook = gtk.Notebook()
496 self.notebook.set_show_tabs(config['show_gtk_tabs'])
499 allposes = {'left': gtk.POS_LEFT, 'right':gtk.POS_RIGHT,
500 'top':gtk.POS_TOP, 'bottom':gtk.POS_BOTTOM}
501 if config['gtk_tab_pos'] in allposes.keys():
502 self.notebook.set_tab_pos(allposes[config['gtk_tab_pos']])
504 self.notebook.set_show_border(False)
505 self.notebook.set_scrollable(True)
506 self.notebook.set_border_width(0)
508 self.notebook.connect("page-removed", self.tab_closed)
509 self.notebook.connect("switch-page", self.tab_changed)
510 self.notebook.connect("page-added", self.tab_opened)
513 if config['show_tablist']:
514 if config['tablist_top']:
515 vbox.pack_start(ebox, False, False, 0)
516 vbox.pack_end(self.notebook, True, True, 0)
519 vbox.pack_start(self.notebook, True, True, 0)
520 vbox.pack_end(ebox, False, False, 0)
525 self.window.add(self.notebook)
528 self.wid = self.notebook.window.xid
530 # Create the uzbl_tabbed fifo
531 fifo_filename = 'uzbltabbed_%d' % os.getpid()
532 self.fifo_socket = os.path.join(config['fifo_dir'], fifo_filename)
533 self._create_fifo_socket(self.fifo_socket)
534 self._setup_fifo_watcher(self.fifo_socket)
537 def _create_fifo_socket(self, fifo_socket):
538 '''Create interprocess communication fifo socket.'''
540 if os.path.exists(fifo_socket):
541 if not os.access(fifo_socket, os.F_OK | os.R_OK | os.W_OK):
542 os.mkfifo(fifo_socket)
545 basedir = os.path.dirname(self.fifo_socket)
546 if not os.path.exists(basedir):
548 os.mkfifo(self.fifo_socket)
550 print "Listening on %s" % self.fifo_socket
553 def _setup_fifo_watcher(self, fifo_socket):
554 '''Open fifo socket fd and setup gobject IO_IN & IO_HUP watchers.
555 Also log the creation of a fd and store the the internal
556 self._watchers dictionary along with the filename of the fd.'''
558 if fifo_socket in self._fifos.keys():
559 fd, watchers = self._fifos[fifo_socket]
561 for watcherid in watchers.keys():
562 gobject.source_remove(watchers[watcherid])
563 del watchers[watcherid]
565 del self._fifos[fifo_socket]
567 # Re-open fifo and add listeners.
568 fd = os.open(fifo_socket, os.O_RDONLY | os.O_NONBLOCK)
570 self._fifos[fifo_socket] = (fd, watchers)
571 watcher = lambda key, id: watchers.__setitem__(key, id)
573 # Watch for incoming data.
574 gid = gobject.io_add_watch(fd, gobject.IO_IN, self.main_fifo_read)
575 watcher('main-fifo-read', gid)
577 # Watch for fifo hangups.
578 gid = gobject.io_add_watch(fd, gobject.IO_HUP, self.main_fifo_hangup)
579 watcher('main-fifo-hangup', gid)
583 '''UzblTabbed main function that calls the gtk loop.'''
585 # Update tablist timer
586 #timer = "update-tablist"
587 #timerid = gobject.timeout_add(500, self.update_tablist,timer)
588 #self._timers[timer] = timerid
590 # Probe clients every second for window titles and location
591 timer = "probe-clients"
592 timerid = gobject.timeout_add(1000, self.probe_clients, timer)
593 self._timers[timer] = timerid
598 def probe_clients(self, timer_call):
599 '''Probe all uzbl clients for up-to-date window titles and uri's.'''
603 tabskeys = self.tabs.keys()
604 notebooklist = list(self.notebook)
606 for tab in notebooklist:
607 if tab not in tabskeys: continue
608 uzbl = self.tabs[tab]
609 uriinventory.append(uzbl.uri)
612 sockd[uzbl._socket] = uzbl
614 self._tabsuris = uriinventory
615 self._curpage = self.notebook.get_current_page()
617 sockets = sockd.keys()
618 (reading, _, errors) = select.select(sockets, [], sockets, 0)
622 uzbl._buffer = sock.recv(1024).replace('\n',' ')
623 temp = uzbl._buffer.split(uzbl._marker)
624 self._buffer = temp.pop()
625 cmds = [s.strip().split() for s in temp if len(s.strip())]
629 self.parse_command(cmd)
632 error("parse_command: invalid command %s" % ' '.join(cmd))
638 def main_fifo_hangup(self, fd, cb_condition):
639 '''Handle main fifo socket hangups.'''
641 # Close fd, re-open fifo_socket and watch.
642 self._setup_fifo_watcher(self.fifo_socket)
644 # And to kill any gobject event handlers calling this function:
648 def main_fifo_read(self, fd, cb_condition):
649 '''Read from main fifo socket.'''
651 self._buffer = os.read(fd, 1024)
652 temp = self._buffer.split("\n")
653 self._buffer = temp.pop()
654 cmds = [s.strip().split() for s in temp if len(s.strip())]
659 self.parse_command(cmd)
662 error("parse_command: invalid command %s" % ' '.join(cmd))
668 def parse_command(self, cmd):
669 '''Parse instructions from uzbl child processes.'''
671 # Commands ( [] = optional, {} = required )
673 # open new tab and head to optional uri.
675 # close current tab or close via tab id.
677 # open next tab or n tabs down. Supports negative indexing.
679 # open prev tab or n tabs down. Supports negative indexing.
686 # title {pid} {document-title}
687 # updates tablist title.
688 # uri {pid} {document-location}
697 elif cmd[0] == "newfromclip":
698 uri = subprocess.Popen(['xclip','-selection','clipboard','-o'],\
699 stdout=subprocess.PIPE).communicate()[0]
703 elif cmd[0] == "close":
705 self.close_tab(int(cmd[1]))
710 elif cmd[0] == "next":
712 self.next_tab(int(cmd[1]))
717 elif cmd[0] == "prev":
719 self.prev_tab(int(cmd[1]))
724 elif cmd[0] == "goto":
725 self.goto_tab(int(cmd[1]))
727 elif cmd[0] == "first":
730 elif cmd[0] == "last":
733 elif cmd[0] in ["title", "uri"]:
735 uzbl = self.get_tab_by_pid(int(cmd[1]))
737 old = getattr(uzbl, cmd[0])
738 new = ' '.join(cmd[2:])
739 setattr(uzbl, cmd[0], new)
741 self.update_tablist()
743 error("parse_command: no uzbl with pid %r" % int(cmd[1]))
745 error("parse_command: unknown command %r" % ' '.join(cmd))
748 def get_tab_by_pid(self, pid):
749 '''Return uzbl instance by pid.'''
751 for tab in self.tabs.keys():
752 if self.tabs[tab].pid == pid:
753 return self.tabs[tab]
758 def new_tab(self, uri='', switch=None):
759 '''Add a new tab to the notebook and start a new instance of uzbl.
760 Use the switch option to negate config['switch_to_new_tabs'] option
761 when you need to load multiple tabs at a time (I.e. like when
762 restoring a session from a file).'''
764 pid = self.next_pid()
767 self.notebook.append_page(tab)
770 fifo_filename = 'uzbl_fifo_%s_%0.2d' % (self.wid, pid)
771 fifo_socket = os.path.join(config['fifo_dir'], fifo_filename)
772 socket_filename = 'uzbl_socket_%s_%0.2d' % (self.wid, pid)
773 socket_file = os.path.join(config['socket_dir'], socket_filename)
776 switch = config['switch_to_new_tabs']
779 # Create meta-instance and spawn child
781 uri = '--uri %s' % uri
783 uzbl = self.UzblInstance(self, tab, fifo_socket, socket_file, pid,\
785 self.tabs[tab] = uzbl
786 cmd = 'uzbl -s %s -n %s_%0.2d %s &' % (sid, self.wid, pid, uri)
787 subprocess.Popen([cmd], shell=True) # TODO: do i need close_fds=True ?
789 # Add gobject timer to make sure the config is pushed when fifo socket
791 timerid = gobject.timeout_add(100, uzbl.flush, "flush-initial-config")
792 uzbl.timers['flush-initial-config'] = timerid
794 self.update_tablist()
797 def config_uzbl(self, uzbl):
798 '''Send bind commands for tab new/close/next/prev to a uzbl
802 bind_format = 'bind %s = sh "echo \\\"%s\\\" > \\\"%s\\\""'
803 bind = lambda key, action: binds.append(bind_format % (key, action, \
806 # Keys are defined in the config section
807 # bind ( key , command back to fifo )
808 bind(config['bind_new_tab'], 'new')
809 bind(config['bind_tab_from_clip'], 'newfromclip')
810 bind(config['bind_tab_from_uri'], 'new %s')
811 bind(config['bind_close_tab'], 'close')
812 bind(config['bind_next_tab'], 'next')
813 bind(config['bind_prev_tab'], 'prev')
814 bind(config['bind_goto_tab'], 'goto %s')
815 bind(config['bind_goto_first'], 'goto 0')
816 bind(config['bind_goto_last'], 'goto -1')
818 # uzbl.send via socket or uzbl.write via fifo, I'll try send.
819 uzbl.send("\n".join(binds))
822 def goto_tab(self, index):
823 '''Goto tab n (supports negative indexing).'''
825 tabs = list(self.notebook)
826 if 0 <= index < len(tabs):
827 self.notebook.set_current_page(index)
828 self.update_tablist()
833 # Update index because index might have previously been a
835 index = tabs.index(tab)
836 self.notebook.set_current_page(index)
837 self.update_tablist()
843 def next_tab(self, step=1):
844 '''Switch to next tab or n tabs right.'''
847 error("next_tab: invalid step %r" % step)
850 ntabs = self.notebook.get_n_pages()
851 tabn = (self.notebook.get_current_page() + step) % ntabs
852 self.notebook.set_current_page(tabn)
853 self.update_tablist()
856 def prev_tab(self, step=1):
857 '''Switch to prev tab or n tabs left.'''
860 error("prev_tab: invalid step %r" % step)
863 ntabs = self.notebook.get_n_pages()
864 tabn = self.notebook.get_current_page() - step
865 while tabn < 0: tabn += ntabs
866 self.notebook.set_current_page(tabn)
867 self.update_tablist()
870 def close_tab(self, tabn=None):
871 '''Closes current tab. Supports negative indexing.'''
874 tabn = self.notebook.get_current_page()
878 tab = list(self.notebook)[tabn]
881 error("close_tab: invalid index %r" % tabn)
884 self.notebook.remove_page(tabn)
887 def tab_opened(self, notebook, tab, index):
888 '''Called upon tab creation. Called by page-added signal.'''
890 if config['switch_to_new_tabs']:
891 self.notebook.set_focus_child(tab)
894 oldindex = self.notebook.get_current_page()
895 oldtab = self.notebook.get_nth_page(oldindex)
896 self.notebook.set_focus_child(oldtab)
899 def tab_closed(self, notebook, tab, index):
900 '''Close the window if no tabs are left. Called by page-removed
903 if tab in self.tabs.keys():
904 uzbl = self.tabs[tab]
905 for timer in uzbl.timers.keys():
906 error("tab_closed: removing timer %r" % timer)
907 gobject.source_remove(uzbl.timers[timer])
918 if self.notebook.get_n_pages() == 0:
921 self.update_tablist()
926 def tab_changed(self, notebook, page, index):
927 '''Refresh tab list. Called by switch-page signal.'''
929 tab = self.notebook.get_nth_page(index)
930 self.notebook.set_focus_child(tab)
931 self.update_tablist(index)
935 def update_tablist(self, curpage=None):
936 '''Upate tablist status bar.'''
938 show_tablist = config['show_tablist']
939 show_gtk_tabs = config['show_gtk_tabs']
940 tab_titles = config['tab_titles']
941 show_ellipsis = config['show_ellipsis']
942 if not show_tablist and not show_gtk_tabs:
945 tabs = self.tabs.keys()
947 curpage = self.notebook.get_current_page()
949 title_format = "%s - Uzbl Browser"
950 max_title_len = config['max_title_len']
954 normal = (config['tab_colours'], config['tab_text_colours'])
955 selected = (config['selected_tab'], config['selected_tab_text'])
957 tab_format = "<span %s> [ %d <span %s> %s</span> ] </span>"
959 tab_format = "<span %s> [ <span %s>%d</span> ] </span>"
962 gtk_tab_format = "%d %s"
964 for index, tab in enumerate(self.notebook):
965 if tab not in tabs: continue
966 uzbl = self.tabs[tab]
969 self.window.set_title(title_format % uzbl.title)
971 tabtitle = uzbl.title[:max_title_len]
972 if show_ellipsis and len(tabtitle) != len(uzbl.title):
973 tabtitle = "%s\xe2\x80\xa6" % tabtitle[:-1] # Show Ellipsis
977 self.notebook.set_tab_label_text(tab,\
978 gtk_tab_format % (index, tabtitle))
980 self.notebook.set_tab_label_text(tab, str(index))
983 style = colour_selector(index, curpage, uzbl)
984 (tabc, textc) = style
987 pango += tab_format % (tabc, index, textc, tabtitle)
989 pango += tab_format % (tabc, textc, index)
992 self.tablist.set_markup(pango)
997 def quit(self, *args):
998 '''Cleanup the application and quit. Called by delete-event signal.'''
1000 for fifo_socket in self._fifos.keys():
1001 fd, watchers = self._fifos[fifo_socket]
1003 for watcherid in watchers.keys():
1004 gobject.source_remove(watchers[watcherid])
1005 del watchers[watcherid]
1007 del self._fifos[fifo_socket]
1009 for timerid in self._timers.keys():
1010 gobject.source_remove(self._timers[timerid])
1011 del self._timers[timerid]
1013 if os.path.exists(self.fifo_socket):
1014 os.unlink(self.fifo_socket)
1015 print "Unlinked %s" % self.fifo_socket
1017 if config['save_session']:
1018 session_file = config['session_file']
1019 if len(self._tabsuris):
1020 if not os.path.isfile(session_file):
1021 dirname = os.path.dirname(session_file)
1022 if not os.path.isdir(dirname):
1023 # Recursive mkdir not rmdir.
1026 sessionstr = '\n'.join(self._tabsuris)
1027 h = open(session_file, 'w')
1028 h.write('current = %s\n%s' % (self._curpage, sessionstr))
1032 # Notebook has no pages so delete session file if it exists.
1033 if os.path.isfile(session_file):
1034 os.remove(session_file)
1039 if __name__ == "__main__":
1041 # Read from the uzbl config into the global config dictionary.
1042 readconfig(uzbl_config, config)
1046 if os.path.isfile(os.path.expandvars(config['session_file'])):
1047 h = open(os.path.expandvars(config['session_file']),'r')
1048 lines = [line.strip() for line in h.readlines()]
1053 if line.startswith("current"):
1054 current = int(line.split()[-1])
1057 urls.append(line.strip())
1059 for (index, url) in enumerate(urls):
1060 if current == index:
1061 uzbl.new_tab(line, True)
1064 uzbl.new_tab(line, False)