Code

Refactored preferences handling into a new version of
[inkscape.git] / src / ui / dialog / whiteboard-connect.cpp
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 "preferences.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         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
86         this->_server.set_text(prefs->getString("whiteboard.server", "name"));
87         /// @todo Convert port to an integer preference?
88         this->_port.set_text(prefs->getString("whiteboard.server", "port"));
89         this->_username.set_text(prefs->getString("whiteboard.server", "username"));
90         this->_usessl.set_active(prefs->getBool("whiteboard.server", "ssl", false);
92         this->_layout.attach(this->_labels[0], 0, 1, 0, 1);
93         this->_layout.attach(this->_labels[1], 0, 1, 1, 2);
94         this->_layout.attach(this->_labels[2], 0, 1, 2, 3);
95         this->_layout.attach(this->_labels[3], 2, 3, 0, 1);
97         this->_layout.attach(this->_server, 1, 2, 0, 1);
98         this->_layout.attach(this->_port, 3, 4, 0, 1);
99         this->_layout.attach(this->_username, 1, 4, 1, 2);
100         this->_layout.attach(this->_password, 1, 4, 2, 3);
102         this->_checkboxes.attach(this->_blank,0,1,0,1);
103         this->_checkboxes.attach(this->_blank,0,1,1,2);
105         this->_checkboxes.attach(this->_usessl, 1, 4, 0, 1);
106         this->_checkboxes.attach(this->_register, 1, 5, 1, 2);
108         this->_layout.set_col_spacings(1);
109         this->_layout.set_row_spacings(1);
111         this->_password.set_visibility(false);
112         this->_password.set_invisible_char('*');
114         // Buttons
115         this->_ok.set_label(_("Connect"));
116         this->_cancel.set_label(_("Cancel"));
118         this->_ok.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_respCallback), GTK_RESPONSE_OK));
119         this->_cancel.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_respCallback), GTK_RESPONSE_CANCEL));
120         
121         this->_register.signal_clicked().connect(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_registerCallback));
122         this->_usessl.signal_clicked().connect(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_useSSLClickedCallback));
124         this->_buttons.pack_start(this->_cancel, true, true, 0);
125         this->_buttons.pack_end(this->_ok, true, true, 0);
126         
127         // Pack widgets into main vbox
128         main->pack_start(this->_layout,Gtk::PACK_SHRINK);
129         main->pack_start(this->_checkboxes,Gtk::PACK_SHRINK);
130         main->pack_end(this->_buttons,Gtk::PACK_SHRINK);
134 void
135 WhiteboardConnectDialogImpl::_registerCallback()
137         if (this->_register.get_active()) 
138         {
139                 Glib::ustring server, port;
140                 bool usessl;
142                 server = this->_server.get_text();
143                 port = this->_port.get_text();
144                 usessl = this->_usessl.get_active();
146                 Glib::ustring msg = String::ucompose(_("Establishing connection to Jabber server <b>%1</b>"), server);
147                 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
149                 if(this->_sm->initializeConnection(server,port,usessl) == CONNECT_SUCCESS)
150                 {
152                         std::vector<Glib::ustring> entries = this->_sm->getRegistrationInfo();
154                         for(unsigned i = 0; i<entries.size();i++)
155                         {
157                                 Gtk::Entry *entry = manage (new Gtk::Entry);
158                                 Gtk::Label *label = manage (new Gtk::Label);
160                                 Glib::ustring::size_type zero=0,one=1;
161                                 Glib::ustring LabelText = entries[i].replace(zero,one,one,Glib::Unicode::toupper(entries[i].at(0)));
163                                 (*label).set_markup_with_mnemonic(LabelText.c_str());
164                                 (*label).set_mnemonic_widget(*entry);
166                                 this->_layout.attach (*label, 0, 1, i+3, i+4, Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK, (Gtk::AttachOptions)0,0,0);            
167                                 this->_layout.attach (*entry, 1, 4, i+3, i+4, Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK, (Gtk::AttachOptions)0,0,0);
169                                 this->registerlabels.push_back(label);
170                                 this->registerentries.push_back(entry);
171                         }
172                 }else{
173                         Glib::ustring msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
174                         this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
175                         this->_sm->connectionError(msg);
176                 }
178         }else{
180                 for(unsigned i = 0; i<registerlabels.size();i++)
181                 {
182                         this->_layout.remove(*registerlabels[i]);
183                         this->_layout.remove(*registerentries[i]);
185                         delete registerlabels[i];
186                         delete registerentries[i];
187                 }
189                 registerentries.erase(registerentries.begin(), registerentries.end());
190                 registerlabels.erase(registerlabels.begin(), registerlabels.end());
191         }
193         this->get_vbox()->show_all_children();
194         //this->reshow_with_initial_size();
197 void
198 WhiteboardConnectDialogImpl::_respCallback(int resp)
200     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
201         if (resp == GTK_RESPONSE_OK) 
202         {
203                 Glib::ustring server, port, username, password;
204                 bool usessl;
206                 server = this->_server.get_text();
207                 port = this->_port.get_text();
208                 username = this->_username.get_text();
209                 password = this->_password.get_text();
210                 usessl = this->_usessl.get_active();
212                 Glib::ustring msg = String::ucompose(_("Establishing connection to Jabber server <b>%1</b> as user <b>%2</b>"), server, username);
213                 this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
215                 if (!this->_register.get_active()) 
216                 {
217                         switch (this->_sm->connectToServer(server, port, username, password, usessl)) {
218                                 case FAILED_TO_CONNECT:
219                                         msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
220                                         this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
221                                         this->_sm->connectionError(msg);
222                                         break;
223                                 case INVALID_AUTH:
224                                         msg = String::ucompose(_("Authentication failed on Jabber server <b>%1</b> as <b>%2</b>"), server, username);
225                                         this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
226                                         this->_sm->connectionError(msg);
227                                         break;
228                                 case SSL_INITIALIZATION_ERROR:
229                                         msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server <b>%1</b>"), server);
230                                         this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
231                                         this->_sm->connectionError(msg);
232                                         break;
233                                         
234                                 case CONNECT_SUCCESS:
235                                         msg = String::ucompose(_("Connected to Jabber server <b>%1</b> as <b>%2</b>"), server, username);
236                                         this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
237         
238                                         // Save preferences
239                                         prefs->setString(this->_prefs_path, "server", this->_server.get_text());
240                                         break;
241                                 default:
242                                         break;
243                         }
244                 }else{
246                         std::vector<Glib::ustring> key,val;     
248                         for(unsigned i = 0; i<registerlabels.size();i++)
249                         {
250                                 key.push_back((*registerlabels[i]).get_text());
251                                 val.push_back((*registerentries[i]).get_text());
252                         }
254                         switch (this->_sm->registerWithServer(username, password, key, val)) 
255                         {
256                                 case FAILED_TO_CONNECT:
257                                         msg = String::ucompose(_("Failed to establish connection to Jabber server <b>%1</b>"), server);
258                                         this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
259                                         this->_sm->connectionError(msg);
260                                         break;
261                                 case INVALID_AUTH:
262                                         msg = String::ucompose(_("Registration failed on Jabber server <b>%1</b> as <b>%2</b>"), server, username);
263                                         this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
264                                         this->_sm->connectionError(msg);
265                                         break;
266                                 case SSL_INITIALIZATION_ERROR:
267                                         msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server <b>%1</b>"), server);
268                                         this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data());
269                                         this->_sm->connectionError(msg);
270                                         break;
271                                         
272                                 case CONNECT_SUCCESS:
273                                         msg = String::ucompose(_("Connected to Jabber server <b>%1</b> as <b>%2</b>"), server, username);
274                                         this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data());
275         
276                                         // Save preferences
277                                         prefs->setString(this->_prefs_path, "server", this->_server.get_text());
278                                         break;
279                                 default:
280                                         break;
281                         }
282                 }
283         }
285         this->_password.set_text("");
286         this->hide();
289 void
290 WhiteboardConnectDialogImpl::_useSSLClickedCallback()
292         if (this->_usessl.get_active()) {
293                 this->_port.set_text("5223");
294         
295                 // String::ucompose seems to format numbers according to locale; unfortunately,
296                 // I'm not yet sure how to turn that off
297                 //this->_port.set_text(String::ucompose("%1", LM_CONNECTION_DEFAULT_PORT_SSL));
298         } else {
299                 this->_port.set_text("5222");
300         }
303 } // namespace Dialog
305 } // namespace UI
307 } // namespace Inkscape
309 /*
310   Local Variables:
311   mode:c++
312   c-file-style:"stroustrup"
313   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
314   indent-tabs-mode:nil
315   fill-column:99
316   End:
317 */
318 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :