Attachment 'cookie-whitelist.py'

Download

   1 # TODO
   2 #  - add whitelist enable checkbox
   3 #  - add padding to the add host dialog
   4 #  - handle the permission entries changing underneath us
   5 
   6 import epiphany
   7 import gtk
   8 
   9 class CookieTreeModel (gtk.GenericTreeModel):
  10 	def __init__ (self):
  11 		gtk.GenericTreeModel.__init__ (self)
  12 		self._s = epiphany.ephy_shell_get_default ().get_embed_single ()
  13 
  14 	def _get_rules (self):
  15 		return [p for p in self._s.list_permissions ('cookie') if p.permission == epiphany.PERMISSION_ALLOWED]
  16 	
  17 	def on_get_flags (self):
  18 		return gtk.TREE_MODEL_LIST_ONLY
  19 	
  20 	def on_get_n_columns (self):
  21 		return 1
  22 	
  23 	def on_get_column_type (self, index):
  24 		assert index == 0
  25 		return str
  26 
  27 	def on_get_path (self, node):
  28 		return (node, )
  29 
  30 	def on_get_iter (self, path):
  31 		i = path[0]
  32 		if i >= 0 and i < len (self._get_rules ()):
  33 			return i
  34 		else:
  35 			return None
  36 
  37 	def on_get_value (self, node, column):
  38 		assert column == 0
  39 		return self._get_rules ()[node].host
  40 
  41 	def on_iter_next (self, node):
  42 		if node + 1 < len (self._get_rules ()):
  43 			return node + 1
  44 		else:
  45 			return None
  46 	
  47 	def on_iter_children (self, node):
  48 		return None
  49 
  50 	def on_iter_has_child (self, node):
  51 		return False
  52 	
  53 	def on_iter_n_children (self, node):
  54 		return 0
  55 	
  56 	def on_iter_nth_child (self, node, n):
  57 		if node == None:
  58 			return (n, ) #huh?
  59 		else:
  60 			return None
  61 	
  62 	def on_iter_parent (self, node):
  63 		return None
  64 	
  65 	def remove (self, iter):
  66 		path = self.get_path (iter)
  67 		index = path[0]
  68 		host = self._get_rules ()[index].host
  69 		self._s.remove_permission (host, 'cookie')
  70 		self.row_deleted (path)
  71 	
  72 	# return an iter for the given host in the list of rules, or None if the
  73 	# host is not present
  74 	def search (self, host):
  75 		i = 0
  76 		for p in self._get_rules ():
  77 			if p.host == host:
  78 				path = (i, )
  79 				return self.get_iter (path)
  80 			i += 1
  81 		return None
  82 	
  83 	def add (self, host):
  84 		# do this check ourselves because weird things happen if we add a host
  85 		# that is already in the list. possibly the order of entries changes
  86 		# or something
  87 		if self.search (host) == None:
  88 			self._s.add_permission ('http://%s' % host, 'cookie', epiphany.PERMISSION_ALLOWED)
  89 			iter = self.search (host)
  90 			self.row_inserted (self.get_path (iter), iter)
  91 
  92 		return self.search (host)
  93 
  94 _ui_str = """
  95 <ui>
  96  <menubar name="menubar">
  97   <menu name="ToolsMenu" action="Tools">
  98    <menuitem name="CookieWhitelist" action="CookieWhitelist"/>
  99   </menu>
 100  </menubar>
 101 </ui>
 102 """
 103 
 104 _cm_w = None
 105 
 106 def _show_cm (action, window):
 107 	global _cm_w
 108 
 109 	if _cm_w:
 110 		_cm_w.present ()
 111 	else:
 112 		ctm = CookieTreeModel ()
 113 		cr = gtk.CellRendererText ()
 114 		tvc = gtk.TreeViewColumn ('Host', cr)
 115 		tvc.add_attribute (cr, 'text', 0)
 116 		tv = gtk.TreeView (ctm)
 117 		tv.append_column (tvc)
 118 
 119 		def row_selected (selection):
 120 			rb.set_sensitive (selection.count_selected_rows ()> 0)
 121 
 122 		tv.get_selection ().connect ('changed', row_selected)
 123 
 124 		sw = gtk.ScrolledWindow ()
 125 		sw.set_policy (gtk.POLICY_NEVER, gtk.POLICY_ALWAYS)
 126 		sw.set_shadow_type (gtk.SHADOW_IN)
 127 		sw.add (tv)
 128 
 129 		def add (button, model):
 130 			l = gtk.Label ('Host')
 131 			e = gtk.Entry (255)
 132 			e.set_activates_default (True)
 133 
 134 			hb = gtk.HBox (False, 12)
 135 			hb.pack_start (l)
 136 			hb.pack_end (e)
 137 			hb.show_all ()
 138 
 139 			d = gtk.Dialog ('Add host', _cm_w, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_ADD, gtk.RESPONSE_OK))
 140 			d.vbox.add (hb)
 141 			d.set_default_response(gtk.RESPONSE_OK)
 142 			
 143 			ret = d.run ()
 144 			if ret == gtk.RESPONSE_OK:
 145 				i = model.add (e.get_text ())
 146 				tv.get_selection ().select_iter (i)
 147 				tv.scroll_to_cell (model.get_path (i))
 148 
 149 			d.destroy ()
 150 
 151 		def remove (button, view):
 152 			(model, iter) = view.get_selection ().get_selected ()
 153 			model.remove (iter)
 154 
 155 		ab = gtk.Button (stock=gtk.STOCK_ADD)
 156 		ab.connect ('clicked', add, ctm)
 157 
 158 		rb = gtk.Button (stock=gtk.STOCK_REMOVE)
 159 		rb.connect ('clicked', remove, tv)
 160 		
 161 		bb = gtk.HButtonBox ()
 162 		bb.add (rb)
 163 		bb.add (ab)
 164 		
 165 		vb = gtk.VBox (False, 12)
 166 		vb.pack_start (sw, True)
 167 		vb.pack_end (bb, False)
 168 
 169 		def hide (window, data):
 170 			window.hide ()
 171 			return True
 172 
 173 		_cm_w = gtk.Window (gtk.WINDOW_TOPLEVEL)
 174 		_cm_w.set_title ('Cookie Whitelist')
 175 		_cm_w.set_border_width (12)
 176 		_cm_w.set_size_request (320,240)
 177 		_cm_w.set_position (gtk.WIN_POS_CENTER)
 178 		_cm_w.add (vb)
 179 		_cm_w.connect ('delete_event', hide)
 180 		_cm_w.show_all ()
 181 
 182 _actions = [('CookieWhitelist', None, 'Cooke Whitelist...', None, None, _show_cm)]
 183 
 184 def attach_window (window):
 185 	group = gtk.ActionGroup('CookieWhitelist')
 186 	group.add_actions (_actions, window)
 187 
 188 	ui_manager = window.get_ui_manager ()
 189 	ui_manager.insert_action_group (group, -1)
 190 	ui_id = ui_manager.add_ui_from_string (_ui_str)
 191 
 192 	window._cookie_manager_data = (group, ui_id)
 193 
 194 def detach_window (window):
 195 	group, ui_id = window._cookie_manager_data
 196 	del window._cookie_manager_data
 197 
 198 	ui_manager = window.get_ui_manager ()
 199 	ui_manager.remove_ui (ui_id)
 200 	ui_manager.remove_action_group (group)
 201 
 202 	ui_manager.ensure_update ()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-07-02 20:36:22, 0.2 KB) [[attachment:cookie-whitelist.ephy-extension]]
  • [get | view] (2008-07-02 20:36:29, 4.8 KB) [[attachment:cookie-whitelist.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

© Sam Morris <sam@robots.org.uk>.
Content may be distributed and modified providing this notice is preserved.