3 import gettext
; _
= gettext
.gettext
9 from pgw
.DBConnection
import ConnectionParameter
11 #TODO# from pgw.Widgets import LabeledEntry
14 class ListBox(gtk
.ScrolledWindow
):
15 def __init__(self
, title
, liststore
):
16 gtk
.ScrolledWindow
.__init
__(self
)
18 self
.store
= liststore
20 self
.set_policy(gtk
.POLICY_AUTOMATIC
, gtk
.POLICY_ALWAYS
)
21 self
.set_shadow_type(gtk
.SHADOW_NONE
)
23 self
.view
= gtk
.TreeView(liststore
)
25 cell
= gtk
.CellRendererText()
26 column
= gtk
.TreeViewColumn(title
)
27 column
.pack_start(cell
, True)
28 column
.add_attribute(cell
, 'text', 0)
29 column
.set_sort_column_id(0)
30 self
.view
.append_column(column
)
34 def set_array(self
, array
):
37 self
.store
.append(item
)
40 tuple, treeview
= self
.view
.get_cursor()
43 return self
.store
[tuple[0]]
45 class LabeledEntry(gtk
.HBox
):
46 def __init__(self
, caption
):
47 gtk
.HBox
.__init
__(self
)
48 self
.entry
= gtk
.Entry()
49 self
.label
= gtk
.Label(caption
)
53 def connect(self
, event
, connection
):
54 self
.entry
.connect(event
, connection
)
57 return self
.entry
.get_text()
58 def grab_focus(self
, eventFromWidget
=None):
59 self
.entry
.grab_focus()
61 class ConnectDialog(gtk
.Window
):
63 gtk
.Window
.__init
__(self
)
66 self
.set_title(_('Open ConnectionParameter'))
67 self
.set_property("width-request", 620)
68 self
.set_property("height-request", 400)
69 self
.set_property("resizable", False)
71 self
.connection
= ConnectionParameter(name
="", user
="", password
="", db
="", local
=False, host
="localhost", port
="5432")
74 self
.content
= gtk
.VBox()
76 self
.input = gtk
.HBox()
77 self
.input.set_homogeneous(True)
79 self
.bottom
= gtk
.HBox()
81 self
.left
= gtk
.VBox()
83 self
.right
= gtk
.VBox()
87 self
.message
= gtk
.Label(_('Enter Connection details:'))
88 self
.message
.set_line_wrap(True)
90 self
.connect_style_local
= gtk
.RadioButton(group
=None, label
=_('Local Connection'), use_underline
=True)
91 self
.connect_style_network
= gtk
.RadioButton(group
=self
.connect_style_local
, label
=_('Network Connection'), use_underline
=True)
94 vbox
.add(self
.message
)
95 vbox
.add(self
.connect_style_local
)
96 vbox
.add(self
.connect_style_network
)
97 self
.right
.pack_start(vbox
, expand
=False, fill
=False, padding
=5)
99 self
.entry_name
= LabeledEntry(_('Session Name'))
100 self
.entry_name
.entry
.set_text(self
.connection
.name
)
101 self
.entry_user
= LabeledEntry(_('Username'))
102 self
.entry_user
.entry
.set_text(self
.connection
.user
)
103 self
.entry_password
= LabeledEntry(_('Password'))
104 self
.entry_password
.entry
.set_text(self
.connection
.password
)
105 self
.entry_db
= LabeledEntry(_('Database'))
106 self
.entry_db
.entry
.set_text(self
.connection
.db
)
107 self
.entry_host
= LabeledEntry(_('Host'))
108 self
.entry_host
.entry
.set_text(self
.connection
.host
)
109 self
.entry_port
= LabeledEntry(_('Port'))
110 self
.entry_port
.entry
.set_text(self
.connection
.port
)
112 entrys
= [self
.entry_name
, self
.entry_user
, self
.entry_password
, self
.entry_db
, self
.entry_host
, self
.entry_port
]
113 for widget
in entrys
:
114 self
.right
.add(widget
)
115 widget
.connect("key-press-event", self
.on_change
)
117 widget
.connect("activate", entrys
[entrys
.index(widget
) + 1].grab_focus
)
124 self
.connect_style_local
.connect("toggled", self
.on_change
)
125 self
.connect_style_network
.connect("toggled", self
.on_change
)
126 self
.connect_style_local
.set_active(False)
127 self
.connect_style_network
.set_active(True)
131 self
.listbox
= ListBox(_('Last Connections'), gtk
.ListStore( str, pgw
.GObjectContainer
))
132 self
.listbox
.set_array([['Foo', pgw
.GObjectContainer(ConnectionParameter("yvesf", "iii"))],["blafoo", pgw
.GObjectContainer(ConnectionParameter("yvesf", "iii"))]])
133 self
.listbox
.view
.connect('cursor-changed', self
.on_cursor_changed
)
134 self
.left
.pack_start(self
.listbox
,True,True)
137 button_connect
= gtk
.Button(_('Connect'))
138 button_connect
.connect("clicked", self
.on_clicked_ok
)
139 button_connect
.set_property("height-request", 45)
140 button_connect
.set_property("width-request", 150)
142 button_cancel
= gtk
.Button(_('Cancel'))
143 button_cancel
.connect("clicked", self
.on_clicked_cancel
)
144 button_cancel
.set_property("height-request", 45)
145 button_cancel
.set_property("width-request", 150)
147 self
.bottom
.pack_start(button_connect
, True, False)
148 self
.bottom
.pack_start(button_cancel
, True, False)
150 self
.input.add(self
.left
)
151 self
.input.add(self
.right
)
152 self
.content
.pack_start(self
.input)
153 self
.content
.pack_start(self
.bottom
, False, False)
154 self
.add(self
.content
)
156 self
.on_change()#Check UI
157 self
.entry_name
.grab_focus()
159 def set_error(self
, message
):
160 self
.message
.set_markup(message
)
162 def on_clicked_ok(self
, widget
):
163 cp
= ConfigParser
.ConfigParser()
165 cp
.readfp(open(pgw
.get_config_path(), 'r'))
169 if not cp
.has_section("connection"):
170 cp
.add_section("connection")
171 cp
.set("connection", "connection", pickle
.dumps([]))
176 return_value
= self
.action_ok(self
, self
.connection
)
177 if return_value
== True:
182 def on_clicked_cancel(self
, widget
):
185 def on_cursor_changed(self
, widget
=None):
188 def on_change(self
, widget
=None, event
=None):
189 """The User entered some new values in any widget"""
190 self
.connection
.name
= self
.entry_name
.get_text()
191 self
.connection
.user
= self
.entry_user
.get_text()
192 self
.connection
.password
= self
.entry_password
.get_text()
193 self
.connection
.db
= self
.entry_db
.get_text()
194 if self
.connect_style_local
.get_active(): #Lokale Verbindung
195 self
.entry_host
.hide()
196 self
.entry_port
.hide()
197 self
.connection
.local
= True
198 self
.connection
.host
= ""
199 self
.connection
.port
= ""
200 elif self
.connect_style_network
.get_active():
201 self
.entry_host
.show()
202 self
.entry_port
.show()
203 self
.connection
.local
= False
204 self
.connection
.host
= self
.entry_host
.get_text()
205 self
.connection
.port
= self
.entry_port
.get_text()
207 raise Exception("Something strange happen ;)")
213 def connect(self
, event
, conn
):
214 if event
== "clicked-ok":
215 self
.action_ok
= conn
217 super.connect(event
, conn
)
219 def on_dlgconnect_change(self
, treeview
):
220 """Called when the user choose a connection in the connection history list"""
221 # fill the connection dialog with the selected connection parameters
222 model
, iter = treeview
.get_selection().get_selected()
223 host
, port
, db
, user
= string
.split(model
.get(iter, 1)[0], ',')
224 self
.ui
.entry_host
.set_text(host
)
225 self
.ui
.entry_port
.set_text(port
)
226 self
.ui
.entry_database
.set_text(db
)
227 self
.ui
.entry_user
.set_text(user
)
228 self
.ui
.entry_password
.set_text('')
230 def on_dlgconnect_map(self
, widget
):
231 """Called when the connection dialog box is displayed"""
232 # load the connections history from the config file
233 cp
= ConfigParser
.ConfigParser()
235 cp
.readfp(open(pgw
.get_config_path(), 'r'))
239 pickled
= cp
.get("connection", "pickled")
240 old_connections
= pickle
.loads(pickled
)
241 self
.listbox
.set_array(old_connections
)
243 if not cp
.has_section("connection"):
244 cp
.add_section("connection")
245 cp
.set("connection", "connection", pickle
.dumps([]))
250 if __name__
== "__main__":
253 window
= ConnectDialog()
260 except Exception, err
:
266 thread
.start_new_thread(test
, ("foo",))
268 #thread.start_new_thread(gtk.main, ())