Package flumotion :: Package admin :: Package gtk :: Module connections
[hide private]

Source Code for Module flumotion.admin.gtk.connections

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3   
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  """connection widgets and dialogs""" 
 19   
 20  import os 
 21  import gettext 
 22   
 23  import gobject 
 24  import gtk 
 25  from kiwi.ui.objectlist import Column 
 26  from pango import ELLIPSIZE_MIDDLE, ELLIPSIZE_END 
 27   
 28  from flumotion.admin.connections import getRecentConnections, \ 
 29       hasRecentConnections 
 30  from flumotion.common.pygobject import gsignal, gproperty 
 31  from flumotion.ui.glade import GladeWidget, GladeWindow 
 32   
 33  __version__ = "$Rev$" 
 34  _ = gettext.gettext 
 35   
 36   
37 -def format_timestamp(stamp):
38 return stamp.strftime('%x')
39 40
41 -class Connections(GladeWidget):
42 gladeFile = 'connections.glade' 43 44 gsignal('have-connection', bool) 45 gsignal('connection-activated', object) 46 gsignal('connections-cleared') 47
48 - def __init__(self):
49 GladeWidget.__init__(self) 50 51 self.connections.set_columns( 52 [Column("host", title=_("Hostname"), searchable=True, 53 ellipsize=ELLIPSIZE_MIDDLE, expand=True, width=100), 54 Column("manager", title=_("Manager"), searchable=True, 55 ellipsize=ELLIPSIZE_END, expand=True, width=50), 56 Column("timestamp", title=_("Last used"), 57 sorted=True, 58 order=gtk.SORT_DESCENDING, 59 format_func=format_timestamp), 60 ]) 61 self.connections.add_list(getRecentConnections()) 62 self.connections.get_treeview().set_search_equal_func( 63 self._searchEqual) 64 self.connections.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) 65 self.connections.set_property('selection-mode', gtk.SELECTION_SINGLE) 66 self.connections.set_size_request(-1, 160) 67 68 self._updateButtons()
69
70 - def _updateButtons(self):
71 canClear = hasRecentConnections() 72 self.button_clear.set_sensitive(canClear) 73 self.button_clear_all.set_sensitive(canClear) 74 if not canClear: 75 self.emit('connections-cleared')
76
77 - def _searchEqual(self, model, column, key, iter):
78 connection = model.get(iter, 0)[0] 79 if key in connection.manager: 80 return False 81 82 # True means doesn't match 83 return True
84
85 - def _clear_all(self):
86 for conn in self.connections: 87 os.unlink(conn.filename) 88 self.connections.clear()
89
90 - def _clear(self, conn):
91 self.connections.remove(conn) 92 os.unlink(conn.filename)
93 94 # Public API 95
96 - def grab_focus(self):
97 if len(self.connections): 98 self.connections.select(self.connections[0]) 99 self.connections.grab_focus()
100
101 - def get_selected(self):
102 return self.connections.get_selected()
103
104 - def update(self, connection):
105 os.utime(connection.filename, None)
106 107 # Callbacks 108
109 - def on_button_clear_clicked(self, button):
110 conn = self.connections.get_selected() 111 if conn: 112 self._clear(conn) 113 self._updateButtons()
114
115 - def on_button_clear_all_clicked(self, button):
116 self._clear_all() 117 self._updateButtons()
118
119 - def _on_connections_row_activated(self, connections, connection):
120 self.emit('connection-activated', connection)
121
122 - def _on_connections_selection_changed(self, connections, connection):
123 self.emit('have-connection', bool(connection))
124 125 gobject.type_register(Connections) 126 127
128 -class ConnectionsDialog(GladeWindow):
129 gladeFile = 'connection-dialog.glade' 130 131 gsignal('have-connection', object) 132
133 - def on_connection_activated(self, widget, state):
134 self.emit('have-connection', state)
135
136 - def on_cancel(self, button):
137 self.destroy()
138
139 - def on_ok(self, button):
140 self.emit('have-connection', 141 self.widgets['connections'].get_selected())
142
143 - def on_delete_event(self, dialog, event):
144 self.destroy()
145
146 - def on_connections_cleared(self, widget):
147 self.button_ok.set_sensitive(False)
148 149 gobject.type_register(ConnectionsDialog) 150 151
152 -class OpenConnection(GladeWidget):
153 gladeFile = 'open-connection.glade' 154 155 gproperty(bool, 'can-activate', 'If the state of the widget is complete', 156 False) 157
158 - def __init__(self):
159 self.host_entry = self.port_entry = self.ssl_check = None 160 GladeWidget.__init__(self) 161 self.set_property('can-activate', False) 162 self.on_entries_changed() 163 self.connect('grab-focus', self.on_grab_focus)
164
165 - def on_grab_focus(self, *args):
166 self.host_entry.grab_focus() 167 return True
168
169 - def on_entries_changed(self, *args):
170 old_can_act = self.get_property('can-activate') 171 can_act = self.host_entry.get_text() and self.port_entry.get_text() 172 # fixme: validate input 173 if old_can_act != can_act: 174 self.set_property('can-activate', can_act)
175
176 - def on_ssl_check_toggled(self, button):
177 if button.get_active(): 178 self.port_entry.set_text('7531') 179 else: 180 self.port_entry.set_text('8642')
181
182 - def set_state(self, state):
183 self.host_entry.set_text(state['host']) 184 self.port_entry.set_text(str(state['port'])) 185 self.ssl_check.set_active(not state['use_insecure'])
186
187 - def get_state(self):
188 return {'host': self.host_entry.get_text(), 189 'port': int(self.port_entry.get_text()), 190 'use_insecure': not self.ssl_check.get_active()}
191 gobject.type_register(OpenConnection) 192 193
194 -class Authenticate(GladeWidget):
195 gladeFile = 'authenticate.glade' 196 197 gproperty(bool, 'can-activate', 'If the state of the widget is complete', 198 False) 199 200 # pychecker sacrifices 201 auth_method_combo = None 202 user_entry = None 203 passwd_entry = None 204
205 - def __init__(self, *args):
206 GladeWidget.__init__(self, *args) 207 self.set_property('can-activate', False) 208 self.user_entry.connect('activate', 209 lambda *x: self.passwd_entry.grab_focus()) 210 self.connect('grab-focus', self.on_grab_focus)
211
212 - def on_passwd_entry_activate(self, entry):
213 toplevel = self.get_toplevel() 214 toplevel.wizard.next()
215
216 - def on_grab_focus(self, *args):
218
219 - def on_entries_changed(self, *args):
220 can_act = self.user_entry.get_text() and self.passwd_entry.get_text() 221 self.set_property('can-activate', can_act)
222
223 - def set_state(self, state):
224 if state and 'user' in state: 225 self.user_entry.set_text(state['user']) 226 self.passwd_entry.set_text(state['passwd']) 227 else: 228 self.user_entry.set_text('') 229 self.passwd_entry.set_text('')
230
231 - def get_state(self):
232 return {'user': self.user_entry.get_text(), 233 'passwd': self.passwd_entry.get_text()}
234 gobject.type_register(Authenticate) 235