1 /** @file
2 * @brief Whiteboard connection dialog - implementation
3 */
4 /* Authors:
5 * David Yip <yipdw@rose-hulman.edu>
6 * Jason Segal
7 * Jonas Collaros
8 * Stephen Montgomery
9 * Brandi Soggs
10 * Matthew Weinstock (original C/Gtk version)
11 *
12 * Copyright (c) 2004-2005 Authors
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include <glibmm/i18n.h>
17 #include <gtk/gtkdialog.h>
18 #include <gtkmm/entry.h>
19 #include <gtkmm/checkbutton.h>
20 #include <gtkmm/table.h>
22 #include "inkscape.h"
23 #include "desktop.h"
24 #include "message-stack.h"
25 #include "preferences.h"
27 #include "jabber_whiteboard/session-manager.h"
29 #include "message-context.h"
30 #include "ui/dialog/whiteboard-connect.h"
32 #include "util/ucompose.hpp"
34 namespace Inkscape {
36 namespace UI {
38 namespace Dialog {
40 WhiteboardConnectDialog*
41 WhiteboardConnectDialog::create()
42 {
43 return new WhiteboardConnectDialogImpl();
44 }
46 WhiteboardConnectDialogImpl::WhiteboardConnectDialogImpl() :
47 _layout(4, 4, false), _usessl(_("_Use SSL"), true), _register(_("_Register"), true)
48 {
49 this->setSessionManager();
50 this->_construct();
51 //this->set_resize_mode(Gtk::RESIZE_IMMEDIATE);
52 this->set_resizable(false);
53 this->get_vbox()->show_all_children();
54 }
56 WhiteboardConnectDialogImpl::~WhiteboardConnectDialogImpl()
57 {
58 }
60 void WhiteboardConnectDialogImpl::present()
61 {
62 Dialog::present();
63 }
65 void
66 WhiteboardConnectDialogImpl::setSessionManager()
67 {
68 this->_desktop = this->getDesktop();
69 this->_sm = this->_desktop->whiteboard_session_manager();
70 }
72 void
73 WhiteboardConnectDialogImpl::_construct()
74 {
75 Gtk::VBox* main = this->get_vbox();
77 // Construct dialog interface
78 this->_labels[0].set_markup_with_mnemonic(_("_Server:"));
79 this->_labels[1].set_markup_with_mnemonic(_("_Username:"));
80 this->_labels[2].set_markup_with_mnemonic(_("_Password:"));
81 this->_labels[3].set_markup_with_mnemonic(_("P_ort:"));
83 this->_labels[0].set_mnemonic_widget(this->_server);
84 this->_labels[1].set_mnemonic_widget(this->_username);
85 this->_labels[2].set_mnemonic_widget(this->_password);
86 this->_labels[3].set_mnemonic_widget(this->_port);
88 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
89 this->_server.set_text(prefs->getString("/whiteboard/server/name"));
90 /// @todo Convert port to an integer preference?
91 this->_port.set_text(prefs->getString("/whiteboard/server/port"));
92 this->_username.set_text(prefs->getString("/whiteboard/server/username"));
93 this->_usessl.set_active(prefs->getBool("/whiteboard/server/ssl", false);
95 this->_layout.attach(this->_labels[0], 0, 1, 0, 1);
96 this->_layout.attach(this->_labels[1], 0, 1, 1, 2);
97 this->_layout.attach(this->_labels[2], 0, 1, 2, 3);
98 this->_layout.attach(this->_labels[3], 2, 3, 0, 1);
100 this->_layout.attach(this->_server, 1, 2, 0, 1);
101 this->_layout.attach(this->_port, 3, 4, 0, 1);
102 this->_layout.attach(this->_username, 1, 4, 1, 2);
103 this->_layout.attach(this->_password, 1, 4, 2, 3);
105 this->_checkboxes.attach(this->_blank,0,1,0,1);
106 this->_checkboxes.attach(this->_blank,0,1,1,2);
108 this->_checkboxes.attach(this->_usessl, 1, 4, 0, 1);
109 this->_checkboxes.attach(this->_register, 1, 5, 1, 2);
111 this->_layout.set_col_spacings(1);
112 this->_layout.set_row_spacings(1);
114 this->_password.set_visibility(false);
115 this->_password.set_invisible_char('*');
117 // Buttons
118 this->_ok.set_label(_("Connect"));
119 this->_cancel.set_label(_("Cancel"));
121 this->_ok.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_respCallback), GTK_RESPONSE_OK));
122 this->_cancel.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_respCallback), GTK_RESPONSE_CANCEL));
124 this->_register.signal_clicked().connect(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_registerCallback));
125 this->_usessl.signal_clicked().connect(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_useSSLClickedCallback));
127 this->_buttons.pack_start(this->_cancel, true, true, 0);
128 this->_buttons.pack_end(this->_ok, true, true, 0);
130 // Pack widgets into main vbox
131 main->pack_start(this->_layout,Gtk::PACK_SHRINK);
132 main->pack_start(this->_checkboxes,Gtk::PACK_SHRINK);
133 main->pack_end(this->_buttons,Gtk::PACK_SHRINK);
134 }
137 void
138 WhiteboardConnectDialogImpl::_registerCallback()
139 {
140 if (this->_register.get_active())
141 {
142 Glib::ustring server, port;
143 bool usessl;
145 server = this->_server.get_text();
146 port = this->_port.get_text();
147 usessl = this->_usessl.get_active();
149 Glib::ustring msg = String::ucompose(_("Establishing connection to Jabber server <b>%1</b>"), server);
150 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
152 if(this->_sm->initializeConnection(server,port,usessl) == CONNECT_SUCCESS)
153 {
155 std::vector<Glib::ustring> entries = this->_sm->getRegistrationInfo();
157 for(unsigned i = 0; i<entries.size();i++)
158 {
160 Gtk::Entry *entry = manage (new Gtk::Entry);
161 Gtk::Label *label = manage (new Gtk::Label);
163 Glib::ustring::size_type zero=0,one=1;
164 Glib::ustring LabelText = entries[i].replace(zero,one,one,Glib::Unicode::toupper(entries[i].at(0)));
166 (*label).set_markup_with_mnemonic(LabelText.c_str());
167 (*label).set_mnemonic_widget(*entry);
169 this->_layout.attach (*label, 0, 1, i+3, i+4, Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK, (Gtk::AttachOptions)0,0,0);
170 this->_layout.attach (*entry, 1, 4, i+3, i+4, Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK, (Gtk::AttachOptions)0,0,0);
172 this->registerlabels.push_back(label);
173 this->registerentries.push_back(entry);
174 }
175 }else{
176 Glib::ustring msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
177 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
178 this->_sm->connectionError(msg);
179 }
181 }else{
183 for(unsigned i = 0; i<registerlabels.size();i++)
184 {
185 this->_layout.remove(*registerlabels[i]);
186 this->_layout.remove(*registerentries[i]);
188 delete registerlabels[i];
189 delete registerentries[i];
190 }
192 registerentries.erase(registerentries.begin(), registerentries.end());
193 registerlabels.erase(registerlabels.begin(), registerlabels.end());
194 }
196 this->get_vbox()->show_all_children();
197 //this->reshow_with_initial_size();
198 }
200 void
201 WhiteboardConnectDialogImpl::_respCallback(int resp)
202 {
203 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
204 if (resp == GTK_RESPONSE_OK)
205 {
206 Glib::ustring server, port, username, password;
207 bool usessl;
209 server = this->_server.get_text();
210 port = this->_port.get_text();
211 username = this->_username.get_text();
212 password = this->_password.get_text();
213 usessl = this->_usessl.get_active();
215 Glib::ustring msg = String::ucompose(_("Establishing connection to Jabber server <b>%1</b> as user <b>%2</b>"), server, username);
216 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
218 if (!this->_register.get_active())
219 {
220 switch (this->_sm->connectToServer(server, port, username, password, usessl)) {
221 case FAILED_TO_CONNECT:
222 msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
223 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
224 this->_sm->connectionError(msg);
225 break;
226 case INVALID_AUTH:
227 msg = String::ucompose(_("Authentication failed on Jabber server <b>%1</b> as <b>%2</b>"), server, username);
228 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
229 this->_sm->connectionError(msg);
230 break;
231 case SSL_INITIALIZATION_ERROR:
232 msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server <b>%1</b>"), server);
233 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
234 this->_sm->connectionError(msg);
235 break;
237 case CONNECT_SUCCESS:
238 msg = String::ucompose(_("Connected to Jabber server <b>%1</b> as <b>%2</b>"), server, username);
239 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
241 // Save preferences
242 prefs->setString(this->_prefs_path + "/server", this->_server.get_text());
243 break;
244 default:
245 break;
246 }
247 }else{
249 std::vector<Glib::ustring> key,val;
251 for(unsigned i = 0; i<registerlabels.size();i++)
252 {
253 key.push_back((*registerlabels[i]).get_text());
254 val.push_back((*registerentries[i]).get_text());
255 }
257 switch (this->_sm->registerWithServer(username, password, key, val))
258 {
259 case FAILED_TO_CONNECT:
260 msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
261 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
262 this->_sm->connectionError(msg);
263 break;
264 case INVALID_AUTH:
265 msg = String::ucompose(_("Registration failed on Jabber server <b>%1</b> as <b>%2</b>"), server, username);
266 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
267 this->_sm->connectionError(msg);
268 break;
269 case SSL_INITIALIZATION_ERROR:
270 msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server <b>%1</b>"), server);
271 this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
272 this->_sm->connectionError(msg);
273 break;
275 case CONNECT_SUCCESS:
276 msg = String::ucompose(_("Connected to Jabber server <b>%1</b> as <b>%2</b>"), server, username);
277 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
279 // Save preferences
280 prefs->setString(this->_prefs_path + "/server", this->_server.get_text());
281 break;
282 default:
283 break;
284 }
285 }
286 }
288 this->_password.set_text("");
289 this->hide();
290 }
292 void
293 WhiteboardConnectDialogImpl::_useSSLClickedCallback()
294 {
295 if (this->_usessl.get_active()) {
296 this->_port.set_text("5223");
298 // String::ucompose seems to format numbers according to locale; unfortunately,
299 // I'm not yet sure how to turn that off
300 //this->_port.set_text(String::ucompose("%1", LM_CONNECTION_DEFAULT_PORT_SSL));
301 } else {
302 this->_port.set_text("5222");
303 }
304 }
306 } // namespace Dialog
308 } // namespace UI
310 } // namespace Inkscape
312 /*
313 Local Variables:
314 mode:c++
315 c-file-style:"stroustrup"
316 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
317 indent-tabs-mode:nil
318 fill-column:99
319 End:
320 */
321 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :