From: daleharvey Date: Thu, 18 May 2006 22:59:44 +0000 (+0000) Subject: added functionality to for users to register jabber accounts within the whiteboard... X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=89e8568d5ae16cfde63d3065a38dc46c6601b62c;p=inkscape.git added functionality to for users to register jabber accounts within the whiteboard->connect dialog --- diff --git a/src/jabber_whiteboard/session-manager.cpp b/src/jabber_whiteboard/session-manager.cpp index d9c9c4613..d39108901 100644 --- a/src/jabber_whiteboard/session-manager.cpp +++ b/src/jabber_whiteboard/session-manager.cpp @@ -180,16 +180,9 @@ SessionManager::setDesktop(::SPDesktop* desktop) } int -SessionManager::connectToServer(Glib::ustring const& server, Glib::ustring const& port, Glib::ustring const& username, Glib::ustring const& pw, bool usessl) +SessionManager::initializeConnection(Glib::ustring const& server, Glib::ustring const& port, bool usessl) { GError* error = NULL; - Glib::ustring jid; - - // JID format is username@server/resource - jid += username + "@" + server + "/" + RESOURCE_NAME; - - LmMessage* m; - LmMessageHandler* mh; if (!this->session_data) { this->session_data = new SessionData(this); @@ -208,8 +201,8 @@ SessionManager::connectToServer(Glib::ustring const& server, Glib::ustring const lm_connection_unref(this->session_data->connection); } - this->session_data->jid = jid; this->session_data->connection = lm_connection_new(server.c_str()); + this->session_data->chat_server = server; lm_connection_set_port(this->session_data->connection, atoi(port.c_str())); @@ -245,9 +238,107 @@ SessionManager::connectToServer(Glib::ustring const& server, Glib::ustring const return FAILED_TO_CONNECT; } - g_log(NULL, G_LOG_LEVEL_DEBUG, "Opened Loudmouth connection in blocking mode."); + //On successful connect, remember info + prefs_set_string_attribute("whiteboard.server", "name", server.c_str()); + prefs_set_string_attribute("whiteboard.server", "port", port.c_str()); + prefs_set_int_attribute("whiteboard.server", "ssl", (usessl) ? 1 : 0); + + return CONNECT_SUCCESS; +} + +std::vector +SessionManager::getRegistrationInfo() +{ + GError* error = NULL; + xmlDoc *doc = NULL; + xmlNode *root_element = NULL; + xmlNode *cur_node = NULL; + + LmMessage *reply,*request; + LmMessageNode *n; + + std::vector registerelements; + + request = lm_message_new_with_sub_type(NULL,LM_MESSAGE_TYPE_IQ,LM_MESSAGE_SUB_TYPE_GET); + n = lm_message_node_add_child (request->node, "query", NULL); + lm_message_node_set_attributes (n, "xmlns", "jabber:iq:register", NULL); + + reply = lm_connection_send_with_reply_and_block(this->session_data->connection, request, &error); + if (error != NULL) { + return registerelements; + } + + n = lm_message_get_node(reply); + + Glib::ustring content = static_cast< Glib::ustring >(lm_message_node_to_string(lm_message_node_get_child(n,"query"))); + doc = xmlReadMemory(content.c_str(),content.size(), "noname.xml", NULL, 0); + + if (doc == NULL) { + g_warning("Failed to parse document\n"); + return registerelements; + } + + root_element = xmlDocGetRootElement(doc); + + for (cur_node = root_element->children; cur_node; cur_node = cur_node->next) { + Glib::ustring name = static_cast< Glib::ustring >((char const *)cur_node->name); + if (cur_node->type == XML_ELEMENT_NODE && name != "instructions" + && name != "username" && name != "password" ) { + registerelements.push_back(name); + } + } + + xmlFreeDoc(doc); + xmlCleanupParser(); + + return registerelements; +} + +int +SessionManager::registerWithServer(Glib::ustring const& username, Glib::ustring const& pw, + std::vector key, std::vector val) +{ + + GError* error = NULL; + + LmMessage *request,*reply; + LmMessageNode *n; + + request = lm_message_new_with_sub_type(NULL,LM_MESSAGE_TYPE_IQ,LM_MESSAGE_SUB_TYPE_SET); + n = lm_message_node_add_child (request->node, "query", NULL); + lm_message_node_set_attributes (n, "xmlns", "jabber:iq:register", NULL); + + lm_message_node_add_child(n,"username",username.c_str()); + lm_message_node_add_child(n,"password",pw.c_str()); + + for(unsigned i=0;isession_data->connection, request, &error); + if (error != NULL || lm_message_get_type(reply) != LM_MESSAGE_SUB_TYPE_RESULT) { + return INVALID_AUTH; + } + + this->session_data->jid = username + "@" + (this->session_data->chat_server.c_str()) + "/" + RESOURCE_NAME; + + prefs_set_string_attribute("whiteboard.server", "username", username.c_str()); + + return this->finaliseConnection(); +} + +int +SessionManager::connectToServer(Glib::ustring const& server, Glib::ustring const& port, + Glib::ustring const& username, Glib::ustring const& pw, bool usessl) +{ + GError* error = NULL; + + initializeConnection(server,port,usessl); + + this->session_data->jid = username + "@" + server + "/" + RESOURCE_NAME; - // Authenticate if (!lm_connection_authenticate_and_block(this->session_data->connection, username.c_str(), pw.c_str(), RESOURCE_NAME, &error)) { if (error != NULL) { g_warning("Failed to authenticate: %s", error->message); @@ -260,6 +351,17 @@ SessionManager::connectToServer(Glib::ustring const& server, Glib::ustring const g_log(NULL, G_LOG_LEVEL_DEBUG, "Successfully authenticated."); + return this->finaliseConnection(); +} + +int +SessionManager::finaliseConnection() +{ + + GError* error = NULL; + LmMessage* m; + LmMessageHandler* mh; + // Register message handler for presence messages mh = lm_message_handler_new((LmHandleMessageFunction)presence_handler, reinterpret_cast< gpointer >(this->_myMessageHandler), NULL); lm_connection_register_message_handler(this->session_data->connection, mh, LM_MESSAGE_TYPE_PRESENCE, LM_HANDLER_PRIORITY_NORMAL); @@ -292,14 +394,6 @@ SessionManager::connectToServer(Glib::ustring const& server, Glib::ustring const this->_setVerbSensitivity(ESTABLISHED_CONNECTION); - //On successful connect, remember info - prefs_set_string_attribute("whiteboard.server", "name", server.c_str()); - prefs_set_string_attribute("whiteboard.server", "port", port.c_str()); - prefs_set_string_attribute("whiteboard.server", "username", username.c_str()); - prefs_set_int_attribute("whiteboard.server", "ssl", (usessl) ? 1 : 0); - //Option to store password here? - - return CONNECT_SUCCESS; } @@ -362,7 +456,8 @@ SessionManager::handleSSLError(LmSSL* ssl, LmSSLStatus status) void SessionManager::disconnectFromServer() { - if (this->session_data->connection) { + if (this->session_data->connection) + { GError* error = NULL; LmMessage *m; diff --git a/src/jabber_whiteboard/session-manager.h b/src/jabber_whiteboard/session-manager.h index a1c4ccc69..06a3cd4c5 100644 --- a/src/jabber_whiteboard/session-manager.h +++ b/src/jabber_whiteboard/session-manager.h @@ -16,6 +16,11 @@ #include #include +#include +#include + +#include + extern "C" { #include } @@ -248,7 +253,38 @@ public: * if authentication invalid */ int connectToServer(Glib::ustring const& server, Glib::ustring const& port, Glib::ustring const& username, Glib::ustring const& pw, bool usessl); + + /** + * Register with a Jabber server. + * + * @param username Jabber username + * @param pw password for Jabber account + * @param key A list of string elements required for registration + * @param val The respective list of value of elements required for registration + * @param usessl use SSL for connection + * + * @return CONNECT_SUCCESS if connection successful; FAILED_TO_CONNECT if connection failed or INVALID_AUTH + * if authentication invalid + */ + int registerWithServer(Glib::ustring const& username,Glib::ustring const& pw, std::vector key, std::vector val); + /** + * Query the Registration information required for a jabber server + * + * @param server Jabber server URL + * @param username Jabber username + * @param pw password for Jabber account + * @param key A list of string elements required for registration + * @param val The respective list of value of elements required for registration + * @param usessl use SSL for connection + * + * @return CONNECT_SUCCESS if connection successful; FAILED_TO_CONNECT if connection failed or INVALID_AUTH + * if authentication invalid + */ + std::vector getRegistrationInfo(); + + int finaliseConnection(); + int initializeConnection(Glib::ustring const& server, Glib::ustring const& port, bool usessl); /** * Handle an SSL error by prompting the user for feedback, and continuing or aborting the connection * process based on that feedback. diff --git a/src/ui/dialog/whiteboard-connect.cpp b/src/ui/dialog/whiteboard-connect.cpp index cb873b904..87bd99944 100644 --- a/src/ui/dialog/whiteboard-connect.cpp +++ b/src/ui/dialog/whiteboard-connect.cpp @@ -41,10 +41,12 @@ WhiteboardConnectDialog::create() } WhiteboardConnectDialogImpl::WhiteboardConnectDialogImpl() : - _layout(4, 4, false), _usessl(_("_Use SSL"), true) + _layout(4, 4, false), _usessl(_("_Use SSL"), true), _register(_("_Register"), true) { this->setSessionManager(); this->_construct(); + //this->set_resize_mode(Gtk::RESIZE_IMMEDIATE); + this->set_resizable(false); this->get_vbox()->show_all_children(); } @@ -87,16 +89,20 @@ WhiteboardConnectDialogImpl::_construct() this->_usessl.set_active((prefs_get_int_attribute("whiteboard.server", "ssl", 0) == 1) ? true : false); this->_layout.attach(this->_labels[0], 0, 1, 0, 1); - this->_layout.attach(this->_labels[3], 2, 3, 0, 1); this->_layout.attach(this->_labels[1], 0, 1, 1, 2); this->_layout.attach(this->_labels[2], 0, 1, 2, 3); + this->_layout.attach(this->_labels[3], 2, 3, 0, 1); this->_layout.attach(this->_server, 1, 2, 0, 1); this->_layout.attach(this->_port, 3, 4, 0, 1); this->_layout.attach(this->_username, 1, 4, 1, 2); this->_layout.attach(this->_password, 1, 4, 2, 3); - this->_layout.attach(this->_usessl, 1, 4, 3, 4); + this->_checkboxes.attach(this->_blank,0,1,0,1); + this->_checkboxes.attach(this->_blank,0,1,1,2); + + this->_checkboxes.attach(this->_usessl, 1, 4, 0, 1); + this->_checkboxes.attach(this->_register, 1, 5, 1, 2); this->_layout.set_col_spacings(1); this->_layout.set_row_spacings(1); @@ -107,22 +113,91 @@ WhiteboardConnectDialogImpl::_construct() // Buttons this->_ok.set_label(_("Connect")); this->_cancel.set_label(_("Cancel")); + this->_ok.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_respCallback), GTK_RESPONSE_OK)); this->_cancel.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_respCallback), GTK_RESPONSE_CANCEL)); + + this->_register.signal_clicked().connect(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_registerCallback)); this->_usessl.signal_clicked().connect(sigc::mem_fun(*this, &WhiteboardConnectDialogImpl::_useSSLClickedCallback)); this->_buttons.pack_start(this->_cancel, true, true, 0); this->_buttons.pack_end(this->_ok, true, true, 0); - + // Pack widgets into main vbox - main->pack_start(this->_layout); - main->pack_end(this->_buttons); + main->pack_start(this->_layout,Gtk::PACK_SHRINK); + main->pack_start(this->_checkboxes,Gtk::PACK_SHRINK); + main->pack_end(this->_buttons,Gtk::PACK_SHRINK); +} + + +void +WhiteboardConnectDialogImpl::_registerCallback() +{ + if (this->_register.get_active()) + { + Glib::ustring server, port; + bool usessl; + + server = this->_server.get_text(); + port = this->_port.get_text(); + usessl = this->_usessl.get_active(); + + Glib::ustring msg = String::ucompose(_("Establishing connection to Jabber server %1"), server); + this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data()); + + if(this->_sm->initializeConnection(server,port,usessl) == CONNECT_SUCCESS) + { + + std::vector entries = this->_sm->getRegistrationInfo(); + + for(unsigned i = 0; i_layout.attach (*label, 0, 1, i+3, i+4, Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK, (Gtk::AttachOptions)0,0,0); + this->_layout.attach (*entry, 1, 4, i+3, i+4, Gtk::FILL|Gtk::EXPAND|Gtk::SHRINK, (Gtk::AttachOptions)0,0,0); + + this->registerlabels.push_back(label); + this->registerentries.push_back(entry); + } + }else{ + Glib::ustring msg = String::ucompose(_("Failed to establish connection to Jabber server %1"), server); + this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data()); + this->_sm->connectionError(msg); + } + + }else{ + + for(unsigned i = 0; i_layout.remove(*registerlabels[i]); + this->_layout.remove(*registerentries[i]); + + delete registerlabels[i]; + delete registerentries[i]; + } + + registerentries.erase(registerentries.begin(), registerentries.end()); + registerlabels.erase(registerlabels.begin(), registerlabels.end()); + } + + this->get_vbox()->show_all_children(); + //this->reshow_with_initial_size(); } void WhiteboardConnectDialogImpl::_respCallback(int resp) { - if (resp == GTK_RESPONSE_OK) { + if (resp == GTK_RESPONSE_OK) + { Glib::ustring server, port, username, password; bool usessl; @@ -135,32 +210,73 @@ WhiteboardConnectDialogImpl::_respCallback(int resp) Glib::ustring msg = String::ucompose(_("Establishing connection to Jabber server %1 as user %2"), server, username); this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data()); - switch (this->_sm->connectToServer(server, port, username, password, usessl)) { - case FAILED_TO_CONNECT: - msg = String::ucompose(_("Failed to establish connection to Jabber server %1"), server); - this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data()); - this->_sm->connectionError(msg); - break; - case INVALID_AUTH: - msg = String::ucompose(_("Authentication failed on Jabber server %1 as %2"), server, username); - this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data()); - this->_sm->connectionError(msg); - break; - case SSL_INITIALIZATION_ERROR: - msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server %1"), server); - this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data()); - this->_sm->connectionError(msg); - break; - - case CONNECT_SUCCESS: - msg = String::ucompose(_("Connected to Jabber server %1 as %2"), server, username); - this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data()); - - // Save preferences - prefs_set_string_attribute(this->_prefs_path, "server", this->_server.get_text().c_str()); - break; - default: - break; + if (!this->_register.get_active()) + { + switch (this->_sm->connectToServer(server, port, username, password, usessl)) { + case FAILED_TO_CONNECT: + msg = String::ucompose(_("Failed to establish connection to Jabber server %1"), server); + this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data()); + this->_sm->connectionError(msg); + break; + case INVALID_AUTH: + msg = String::ucompose(_("Authentication failed on Jabber server %1 as %2"), server, username); + this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data()); + this->_sm->connectionError(msg); + break; + case SSL_INITIALIZATION_ERROR: + msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server %1"), server); + this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data()); + this->_sm->connectionError(msg); + break; + + case CONNECT_SUCCESS: + msg = String::ucompose(_("Connected to Jabber server %1 as %2"), server, username); + this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data()); + + // Save preferences + prefs_set_string_attribute(this->_prefs_path, "server", this->_server.get_text().c_str()); + break; + default: + break; + } + }else{ + + std::vector key,val; + + for(unsigned i = 0; i_sm->registerWithServer(username, password, key, val)) + { + case FAILED_TO_CONNECT: + msg = String::ucompose(_("Failed to establish connection to Jabber server %1"), server); + this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data()); + this->_sm->connectionError(msg); + break; + case INVALID_AUTH: + msg = String::ucompose(_("Registration failed on Jabber server %1 as %2"), server, username); + this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data()); + this->_sm->connectionError(msg); + break; + case SSL_INITIALIZATION_ERROR: + msg = String::ucompose(_("SSL initialization failed when connecting to Jabber server %1"), server); + this->_desktop->messageStack()->flash(WARNING_MESSAGE, msg.data()); + this->_sm->connectionError(msg); + break; + + case CONNECT_SUCCESS: + msg = String::ucompose(_("Connected to Jabber server %1 as %2"), server, username); + this->_desktop->messageStack()->flash(INFORMATION_MESSAGE, msg.data()); + + // Save preferences + prefs_set_string_attribute(this->_prefs_path, "server", this->_server.get_text().c_str()); + break; + default: + break; + } } } diff --git a/src/ui/dialog/whiteboard-connect.h b/src/ui/dialog/whiteboard-connect.h index 3eb86b540..b96f1ce3b 100644 --- a/src/ui/dialog/whiteboard-connect.h +++ b/src/ui/dialog/whiteboard-connect.h @@ -16,6 +16,8 @@ #include "verbs.h" #include "dialog.h" +#include + struct SPDesktop; namespace Inkscape { @@ -53,8 +55,12 @@ public: void setSessionManager(); private: + // GTK+ widgets - Gtk::Table _layout; + std::vector registerlabels; + std::vector registerentries; + + Gtk::Table _layout,_checkboxes; Gtk::HBox _buttons; Gtk::Entry _server; @@ -62,15 +68,17 @@ private: Gtk::Entry _password; Gtk::Entry _port; - Gtk::Label _labels[4]; + Gtk::Label _labels[4],_blank; - Gtk::CheckButton _usessl; + Gtk::CheckButton _usessl,_register; Gtk::Button _ok, _cancel; // Construction and callbacks void _construct(); void _respCallback(int resp); + + void _registerCallback(); void _useSSLClickedCallback(); // SessionManager and SPDesktop pointers