87bd9994467f961c853f8c8ad1212ee0ccb93597
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 = SP_ACTIVE_DESKTOP;
66 this->_sm = SP_ACTIVE_DESKTOP->whiteboard_session_manager();
68 }
70 void
71 WhiteboardConnectDialogImpl::_construct()
72 {
73 Gtk::VBox* main = this->get_vbox();
75 // Construct dialog interface
76 this->_labels[0].set_markup_with_mnemonic(_("_Server:"));
77 this->_labels[1].set_markup_with_mnemonic(_("_Username:"));
78 this->_labels[2].set_markup_with_mnemonic(_("_Password:"));
79 this->_labels[3].set_markup_with_mnemonic(_("P_ort:"));
81 this->_labels[0].set_mnemonic_widget(this->_server);
82 this->_labels[1].set_mnemonic_widget(this->_username);
83 this->_labels[2].set_mnemonic_widget(this->_password);
84 this->_labels[3].set_mnemonic_widget(this->_port);
86 this->_server.set_text(prefs_get_string_attribute("whiteboard.server", "name"));
87 this->_port.set_text(prefs_get_string_attribute("whiteboard.server", "port"));
88 this->_username.set_text(prefs_get_string_attribute("whiteboard.server", "username"));
89 this->_usessl.set_active((prefs_get_int_attribute("whiteboard.server", "ssl", 0) == 1) ? true : false);
91 this->_layout.attach(this->_labels[0], 0, 1, 0, 1);
92 this->_layout.attach(this->_labels[1], 0, 1, 1, 2);
93 this->_layout.attach(this->_labels[2], 0, 1, 2, 3);
94 this->_layout.attach(this->_labels[3], 2, 3, 0, 1);
96 this->_layout.attach(this->_server, 1, 2, 0, 1);
97 this->_layout.attach(this->_port, 3, 4, 0, 1);
98 this->_layout.attach(this->_username, 1, 4, 1, 2);
99 this->_layout.attach(this->_password, 1, 4, 2, 3);
101 this->_checkboxes.attach(this->_blank,0,1,0,1);
102 this->_checkboxes.attach(this->_blank,0,1,1,2);
104 this->_checkboxes.attach(this->_usessl, 1, 4, 0, 1);
105 this->_checkboxes.attach(this->_register, 1, 5, 1, 2);
107 this->_layout.set_col_spacings(1);
108 this->_layout.set_row_spacings(1);
110 this->_password.set_visibility(false);
111 this->_password.set_invisible_char('*');
113 // Buttons
114 this->_ok.set_label(_("Connect"));
115 this->_cancel.set_label(_("Cancel"));
117 this->_ok.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_respCallback), GTK_RESPONSE_OK));
118 this->_cancel.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_respCallback), GTK_RESPONSE_CANCEL));
120 this->_register.signal_clicked().connect(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_registerCallback));
121 this->_usessl.signal_clicked().connect(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_useSSLClickedCallback));
123 this->_buttons.pack_start(this->_cancel, true, true, 0);
124 this->_buttons.pack_end(this->_ok, true, true, 0);
126 // Pack widgets into main vbox
127 main->pack_start(this->_layout,Gtk::PACK_SHRINK);
128 main->pack_start(this->_checkboxes,Gtk::PACK_SHRINK);
129 main->pack_end(this->_buttons,Gtk::PACK_SHRINK);
130 }
133 void
134 WhiteboardConnectDialogImpl::_registerCallback()
135 {
136 if (this->_register.get_active())
137 {
138 Glib::ustring server, port;
139 bool usessl;
141 server = this->_server.get_text();
142 port = this->_port.get_text();
143 usessl = this->_usessl.get_active();
145 Glib::ustring msg = String::ucompose(_("Establishing connection to Jabber server <b>%1</b>"), server);
146 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
148 if(this->_sm->initializeConnection(server,port,usessl) == CONNECT_SUCCESS)
149 {
151 std::vector<Glib::ustring> entries = this->_sm->getRegistrationInfo();
153 for(unsigned i = 0; i<entries.size();i++)
154 {
156 Gtk::Entry *entry = manage (new Gtk::Entry);
157 Gtk::Label *label = manage (new Gtk::Label);
159 Glib::ustring::size_type zero=0,one=1;
160 Glib::ustring LabelText = entries[i].replace(zero,one,one,Glib::Unicode::toupper(entries[i].at(0)));
162 (*label).set_markup_with_mnemonic(LabelText.c_str());
163 (*label).set_mnemonic_widget(*entry);
165 this->_layout.attach (*label, 0, 1, i+3, i+4, Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK, (Gtk::AttachOptions)0,0,0);
166 this->_layout.attach (*entry, 1, 4, i+3, i+4, Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK, (Gtk::AttachOptions)0,0,0);
168 this->registerlabels.push_back(label);
169 this->registerentries.push_back(entry);
170 }
171 }else{
172 Glib::ustring msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
173 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
174 this->_sm->connectionError(msg);
175 }
177 }else{
179 for(unsigned i = 0; i<registerlabels.size();i++)
180 {
181 this->_layout.remove(*registerlabels[i]);
182 this->_layout.remove(*registerentries[i]);
184 delete registerlabels[i];
185 delete registerentries[i];
186 }
188 registerentries.erase(registerentries.begin(), registerentries.end());
189 registerlabels.erase(registerlabels.begin(), registerlabels.end());
190 }
192 this->get_vbox()->show_all_children();
193 //this->reshow_with_initial_size();
194 }
196 void
197 WhiteboardConnectDialogImpl::_respCallback(int resp)
198 {
199 if (resp == GTK_RESPONSE_OK)
200 {
201 Glib::ustring server, port, username, password;
202 bool usessl;
204 server = this->_server.get_text();
205 port = this->_port.get_text();
206 username = this->_username.get_text();
207 password = this->_password.get_text();
208 usessl = this->_usessl.get_active();
210 Glib::ustring msg = String::ucompose(_("Establishing connection to Jabber server <b>%1</b> as user <b>%2</b>"), server, username);
211 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
213 if (!this->_register.get_active())
214 {
215 switch (this->_sm->connectToServer(server, port, username, password, usessl)) {
216 case FAILED_TO_CONNECT:
217 msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
218 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
219 this->_sm->connectionError(msg);
220 break;
221 case INVALID_AUTH:
222 msg = String::ucompose(_("Authentication failed on Jabber server <b>%1</b> as <b>%2</b>"), server, username);
223 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
224 this->_sm->connectionError(msg);
225 break;
226 case SSL_INITIALIZATION_ERROR:
227 msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server <b>%1</b>"), server);
228 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
229 this->_sm->connectionError(msg);
230 break;
232 case CONNECT_SUCCESS:
233 msg = String::ucompose(_("Connected to Jabber server <b>%1</b> as <b>%2</b>"), server, username);
234 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
236 // Save preferences
237 prefs_set_string_attribute(this->_prefs_path, "server", this->_server.get_text().c_str());
238 break;
239 default:
240 break;
241 }
242 }else{
244 std::vector<Glib::ustring> key,val;
246 for(unsigned i = 0; i<registerlabels.size();i++)
247 {
248 key.push_back((*registerlabels[i]).get_text());
249 val.push_back((*registerentries[i]).get_text());
250 }
252 switch (this->_sm->registerWithServer(username, password, key, val))
253 {
254 case FAILED_TO_CONNECT:
255 msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
256 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
257 this->_sm->connectionError(msg);
258 break;
259 case INVALID_AUTH:
260 msg = String::ucompose(_("Registration failed on Jabber server <b>%1</b> as <b>%2</b>"), server, username);
261 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
262 this->_sm->connectionError(msg);
263 break;
264 case SSL_INITIALIZATION_ERROR:
265 msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server <b>%1</b>"), server);
266 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
267 this->_sm->connectionError(msg);
268 break;
270 case CONNECT_SUCCESS:
271 msg = String::ucompose(_("Connected to Jabber server <b>%1</b> as <b>%2</b>"), server, username);
272 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
274 // Save preferences
275 prefs_set_string_attribute(this->_prefs_path, "server", this->_server.get_text().c_str());
276 break;
277 default:
278 break;
279 }
280 }
281 }
283 this->_password.set_text("");
284 this->hide();
285 }
287 void
288 WhiteboardConnectDialogImpl::_useSSLClickedCallback()
289 {
290 if (this->_usessl.get_active()) {
291 this->_port.set_text("5223");
293 // String::ucompose seems to format numbers according to locale; unfortunately,
294 // I'm not yet sure how to turn that off
295 //this->_port.set_text(String::ucompose("%1", LM_CONNECTION_DEFAULT_PORT_SSL));
296 } else {
297 this->_port.set_text("5222");
298 }
299 }
301 }
303 }
305 }
307 /*
308 Local Variables:
309 mode:c++
310 c-file-style:"stroustrup"
311 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
312 indent-tabs-mode:nil
313 fill-column:99
314 End:
315 */
316 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :