1 /**
2 * Whiteboard connection establishment dialog
3 *
4 * Authors:
5 * David Yip <yipdw@rose-hulman.edu>
6 * Jason Segal, Jonas Collaros, Stephen Montgomery, Brandi Soggs, Matthew Weinstock (original C/Gtk version)
7 *
8 * Copyright (c) 2004-2005 Authors
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #include <glibmm/i18n.h>
14 #include <gtk/gtkdialog.h>
15 #include <gtkmm/entry.h>
16 #include <gtkmm/checkbutton.h>
17 #include <gtkmm/table.h>
19 #include "inkscape.h"
20 #include "desktop.h"
21 #include "message-stack.h"
22 #include "prefs-utils.h"
24 #include "jabber_whiteboard/session-manager.h"
26 #include "message-context.h"
27 #include "ui/dialog/whiteboard-connect.h"
29 #include "util/ucompose.hpp"
31 namespace Inkscape {
33 namespace UI {
35 namespace Dialog {
37 WhiteboardConnectDialog*
38 WhiteboardConnectDialog::create()
39 {
40 return new WhiteboardConnectDialogImpl();
41 }
43 WhiteboardConnectDialogImpl::WhiteboardConnectDialogImpl() :
44 _layout(4, 4, false), _usessl(_("_Use SSL"), true), _register(_("_Register"), true)
45 {
46 this->setSessionManager();
47 this->_construct();
48 //this->set_resize_mode(Gtk::RESIZE_IMMEDIATE);
49 this->set_resizable(false);
50 this->get_vbox()->show_all_children();
51 }
53 WhiteboardConnectDialogImpl::~WhiteboardConnectDialogImpl()
54 {
55 }
57 void WhiteboardConnectDialogImpl::present()
58 {
59 Dialog::present();
60 }
62 void
63 WhiteboardConnectDialogImpl::setSessionManager()
64 {
65 this->_desktop = this->getDesktop();
66 this->_sm = this->_desktop->whiteboard_session_manager();
67 }
69 void
70 WhiteboardConnectDialogImpl::_construct()
71 {
72 Gtk::VBox* main = this->get_vbox();
74 // Construct dialog interface
75 this->_labels[0].set_markup_with_mnemonic(_("_Server:"));
76 this->_labels[1].set_markup_with_mnemonic(_("_Username:"));
77 this->_labels[2].set_markup_with_mnemonic(_("_Password:"));
78 this->_labels[3].set_markup_with_mnemonic(_("P_ort:"));
80 this->_labels[0].set_mnemonic_widget(this->_server);
81 this->_labels[1].set_mnemonic_widget(this->_username);
82 this->_labels[2].set_mnemonic_widget(this->_password);
83 this->_labels[3].set_mnemonic_widget(this->_port);
85 this->_server.set_text(prefs_get_string_attribute("whiteboard.server", "name"));
86 this->_port.set_text(prefs_get_string_attribute("whiteboard.server", "port"));
87 this->_username.set_text(prefs_get_string_attribute("whiteboard.server", "username"));
88 this->_usessl.set_active((prefs_get_int_attribute("whiteboard.server", "ssl", 0) == 1) ? true : false);
90 this->_layout.attach(this->_labels[0], 0, 1, 0, 1);
91 this->_layout.attach(this->_labels[1], 0, 1, 1, 2);
92 this->_layout.attach(this->_labels[2], 0, 1, 2, 3);
93 this->_layout.attach(this->_labels[3], 2, 3, 0, 1);
95 this->_layout.attach(this->_server, 1, 2, 0, 1);
96 this->_layout.attach(this->_port, 3, 4, 0, 1);
97 this->_layout.attach(this->_username, 1, 4, 1, 2);
98 this->_layout.attach(this->_password, 1, 4, 2, 3);
100 this->_checkboxes.attach(this->_blank,0,1,0,1);
101 this->_checkboxes.attach(this->_blank,0,1,1,2);
103 this->_checkboxes.attach(this->_usessl, 1, 4, 0, 1);
104 this->_checkboxes.attach(this->_register, 1, 5, 1, 2);
106 this->_layout.set_col_spacings(1);
107 this->_layout.set_row_spacings(1);
109 this->_password.set_visibility(false);
110 this->_password.set_invisible_char('*');
112 // Buttons
113 this->_ok.set_label(_("Connect"));
114 this->_cancel.set_label(_("Cancel"));
116 this->_ok.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_respCallback), GTK_RESPONSE_OK));
117 this->_cancel.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_respCallback), GTK_RESPONSE_CANCEL));
119 this->_register.signal_clicked().connect(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_registerCallback));
120 this->_usessl.signal_clicked().connect(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_useSSLClickedCallback));
122 this->_buttons.pack_start(this->_cancel, true, true, 0);
123 this->_buttons.pack_end(this->_ok, true, true, 0);
125 // Pack widgets into main vbox
126 main->pack_start(this->_layout,Gtk::PACK_SHRINK);
127 main->pack_start(this->_checkboxes,Gtk::PACK_SHRINK);
128 main->pack_end(this->_buttons,Gtk::PACK_SHRINK);
129 }
132 void
133 WhiteboardConnectDialogImpl::_registerCallback()
134 {
135 if (this->_register.get_active())
136 {
137 Glib::ustring server, port;
138 bool usessl;
140 server = this->_server.get_text();
141 port = this->_port.get_text();
142 usessl = this->_usessl.get_active();
144 Glib::ustring msg = String::ucompose(_("Establishing connection to Jabber server <b>%1</b>"), server);
145 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
147 if(this->_sm->initializeConnection(server,port,usessl) == CONNECT_SUCCESS)
148 {
150 std::vector<Glib::ustring> entries = this->_sm->getRegistrationInfo();
152 for(unsigned i = 0; i<entries.size();i++)
153 {
155 Gtk::Entry *entry = manage (new Gtk::Entry);
156 Gtk::Label *label = manage (new Gtk::Label);
158 Glib::ustring::size_type zero=0,one=1;
159 Glib::ustring LabelText = entries[i].replace(zero,one,one,Glib::Unicode::toupper(entries[i].at(0)));
161 (*label).set_markup_with_mnemonic(LabelText.c_str());
162 (*label).set_mnemonic_widget(*entry);
164 this->_layout.attach (*label, 0, 1, i+3, i+4, Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK, (Gtk::AttachOptions)0,0,0);
165 this->_layout.attach (*entry, 1, 4, i+3, i+4, Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK, (Gtk::AttachOptions)0,0,0);
167 this->registerlabels.push_back(label);
168 this->registerentries.push_back(entry);
169 }
170 }else{
171 Glib::ustring msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
172 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
173 this->_sm->connectionError(msg);
174 }
176 }else{
178 for(unsigned i = 0; i<registerlabels.size();i++)
179 {
180 this->_layout.remove(*registerlabels[i]);
181 this->_layout.remove(*registerentries[i]);
183 delete registerlabels[i];
184 delete registerentries[i];
185 }
187 registerentries.erase(registerentries.begin(), registerentries.end());
188 registerlabels.erase(registerlabels.begin(), registerlabels.end());
189 }
191 this->get_vbox()->show_all_children();
192 //this->reshow_with_initial_size();
193 }
195 void
196 WhiteboardConnectDialogImpl::_respCallback(int resp)
197 {
198 if (resp == GTK_RESPONSE_OK)
199 {
200 Glib::ustring server, port, username, password;
201 bool usessl;
203 server = this->_server.get_text();
204 port = this->_port.get_text();
205 username = this->_username.get_text();
206 password = this->_password.get_text();
207 usessl = this->_usessl.get_active();
209 Glib::ustring msg = String::ucompose(_("Establishing connection to Jabber server <b>%1</b> as user <b>%2</b>"), server, username);
210 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
212 if (!this->_register.get_active())
213 {
214 switch (this->_sm->connectToServer(server, port, username, password, usessl)) {
215 case FAILED_TO_CONNECT:
216 msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
217 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
218 this->_sm->connectionError(msg);
219 break;
220 case INVALID_AUTH:
221 msg = String::ucompose(_("Authentication failed on Jabber server <b>%1</b> as <b>%2</b>"), server, username);
222 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
223 this->_sm->connectionError(msg);
224 break;
225 case SSL_INITIALIZATION_ERROR:
226 msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server <b>%1</b>"), server);
227 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
228 this->_sm->connectionError(msg);
229 break;
231 case CONNECT_SUCCESS:
232 msg = String::ucompose(_("Connected to Jabber server <b>%1</b> as <b>%2</b>"), server, username);
233 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
235 // Save preferences
236 prefs_set_string_attribute(this->_prefs_path, "server", this->_server.get_text().c_str());
237 break;
238 default:
239 break;
240 }
241 }else{
243 std::vector<Glib::ustring> key,val;
245 for(unsigned i = 0; i<registerlabels.size();i++)
246 {
247 key.push_back((*registerlabels[i]).get_text());
248 val.push_back((*registerentries[i]).get_text());
249 }
251 switch (this->_sm->registerWithServer(username, password, key, val))
252 {
253 case FAILED_TO_CONNECT:
254 msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
255 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
256 this->_sm->connectionError(msg);
257 break;
258 case INVALID_AUTH:
259 msg = String::ucompose(_("Registration failed on Jabber server <b>%1</b> as <b>%2</b>"), server, username);
260 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
261 this->_sm->connectionError(msg);
262 break;
263 case SSL_INITIALIZATION_ERROR:
264 msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server <b>%1</b>"), server);
265 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
266 this->_sm->connectionError(msg);
267 break;
269 case CONNECT_SUCCESS:
270 msg = String::ucompose(_("Connected to Jabber server <b>%1</b> as <b>%2</b>"), server, username);
271 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
273 // Save preferences
274 prefs_set_string_attribute(this->_prefs_path, "server", this->_server.get_text().c_str());
275 break;
276 default:
277 break;
278 }
279 }
280 }
282 this->_password.set_text("");
283 this->hide();
284 }
286 void
287 WhiteboardConnectDialogImpl::_useSSLClickedCallback()
288 {
289 if (this->_usessl.get_active()) {
290 this->_port.set_text("5223");
292 // String::ucompose seems to format numbers according to locale; unfortunately,
293 // I'm not yet sure how to turn that off
294 //this->_port.set_text(String::ucompose("%1", LM_CONNECTION_DEFAULT_PORT_SSL));
295 } else {
296 this->_port.set_text("5222");
297 }
298 }
300 }
302 }
304 }
306 /*
307 Local Variables:
308 mode:c++
309 c-file-style:"stroustrup"
310 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
311 indent-tabs-mode:nil
312 fill-column:99
313 End:
314 */
315 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :