1 /** @file
2 * Whiteboard share with user dialog - implementation
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>
15 #include <sigc++/sigc++.h>
16 #include <gtk/gtkdialog.h>
18 #include "message-stack.h"
19 #include "message-context.h"
20 #include "inkscape.h"
21 #include "desktop.h"
23 #include "jabber_whiteboard/typedefs.h"
24 #include "jabber_whiteboard/session-manager.h"
25 #include "jabber_whiteboard/buddy-list-manager.h"
27 #include "jabber_whiteboard/session-file-selector.h"
29 #include "ui/dialog/whiteboard-sharewithuser.h"
31 #include "util/ucompose.hpp"
33 namespace Inkscape {
35 namespace UI {
37 namespace Dialog {
39 WhiteboardShareWithUserDialog*
40 WhiteboardShareWithUserDialog::create()
41 {
42 return new WhiteboardShareWithUserDialogImpl();
43 }
45 WhiteboardShareWithUserDialogImpl::WhiteboardShareWithUserDialogImpl()
46 {
47 this->setSessionManager();
48 this->_construct();
49 this->get_vbox()->show_all_children();
51 this->_sm->session_data->buddyList.addInsertListener(sigc::mem_fun(this, &WhiteboardShareWithUserDialogImpl::_insertBuddy));
52 this->_sm->session_data->buddyList.addEraseListener(sigc::mem_fun(this, &WhiteboardShareWithUserDialogImpl::_eraseBuddy));
54 }
56 WhiteboardShareWithUserDialogImpl::~WhiteboardShareWithUserDialogImpl()
57 {
59 }
61 void
62 WhiteboardShareWithUserDialogImpl::setSessionManager()
63 {
64 this->_desktop = this->getDesktop();
65 this->_sm = this->_desktop->whiteboard_session_manager();
67 }
70 void
71 WhiteboardShareWithUserDialogImpl::_construct()
72 {
73 Gtk::VBox* main = this->get_vbox();
75 // Construct dialog interface
76 this->_labels[0].set_markup_with_mnemonic(_("_User's Jabber ID:"));
77 this->_labels[0].set_mnemonic_widget(this->_jid);
79 // Buttons
80 this->_share.set_label(_("_Invite user"));
81 this->_cancel.set_label(_("_Cancel"));
82 this->_share.set_use_underline(true);
83 this->_cancel.set_use_underline(true);
85 // Button callbacks
86 this->_share.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardShareWithUserDialogImpl::_respCallback), SHARE));
87 this->_cancel.signal_clicked().connect(sigc::bind< 0 >(sigc::mem_fun(*this, &WhiteboardShareWithUserDialogImpl::_respCallback), CANCEL));
89 // Construct ListStore for buddy list information
90 this->_buddylistdata = Gtk::ListStore::create(this->_blm);
91 this->_buddylist.set_model(this->_buddylistdata);
92 this->_buddylist.append_column(_("Buddy List"), this->_blm.jid);
94 // Fill buddy list
95 this->_fillBuddyList();
97 // Buddy list onclick callback
98 this->_buddylist.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &WhiteboardShareWithUserDialogImpl::_listCallback));
100 // Pack widgets into boxes
101 this->_listwindow.add(this->_buddylist);
102 this->_listwindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
103 this->_buddylistbox.pack_start(this->_listwindow);
105 this->_connecttojidbox.pack_start(this->_labels[0]);
106 this->_connecttojidbox.pack_end(this->_jid);
108 this->_buttons.pack_start(this->_cancel);
109 this->_buttons.pack_end(this->_share);
111 // Pack boxes into main box
112 main->pack_start(this->_buddylistbox);
113 main->pack_start(this->_connecttojidbox);
114 main->pack_start(this->_sfsbox);
115 main->pack_end(this->_buttons);
116 }
119 void
120 WhiteboardShareWithUserDialogImpl::_fillBuddyList()
121 {
122 Whiteboard::BuddyList& bl = this->_sm->session_data->buddyList.getList();
124 for(Whiteboard::BuddyList::iterator i = bl.begin(); i != bl.end(); i++) {
125 this->_insertBuddy(*i);
126 }
127 // std::for_each(bl.begin(), bl.end(), std::mem_fun(&WhiteboardShareWithUserDialogImpl::_insertBuddy));
128 }
130 void
131 WhiteboardShareWithUserDialogImpl::_insertBuddy(std::string const& jid)
132 {
133 // FIXME: need a better way to avoid inserting duplicate rows in the case
134 // of duplicate Jabber presence messages
135 typedef Gtk::TreeModel::Children type_children;
136 type_children children = this->_buddylistdata->children();
137 for(type_children::iterator i = children.begin(); i != children.end(); i++) {
138 if ((*i).get_value(this->_blm.jid) == jid) {
139 return;
140 }
141 }
143 Gtk::TreeModel::Row row = *(this->_buddylistdata->append());
144 row[this->_blm.jid] = jid;
145 }
147 void
148 WhiteboardShareWithUserDialogImpl::_eraseBuddy(std::string const& jid)
149 {
150 // FIXME: Doesn't gtkmm provide a better way to erase rows from a ListStore?
151 typedef Gtk::TreeModel::Children type_children;
152 type_children children = this->_buddylistdata->children();
153 for(type_children::iterator i = children.begin(); i != children.end(); i++) {
154 if ((*i).get_value(this->_blm.jid) == jid) {
155 this->_buddylistdata->erase(i);
156 return;
157 }
158 }
159 }
161 void
162 WhiteboardShareWithUserDialogImpl::_respCallback(int resp)
163 {
164 switch (resp) {
165 case SHARE:
166 {
167 Glib::ustring jid = this->_jid.get_text();
169 // Check that the JID is in the format user@host/resource
170 if (jid.find("@", 0) == Glib::ustring::npos) {
171 jid += "@";
172 jid += lm_connection_get_server(this->_sm->session_data->connection);
173 }
175 if (jid.find("/", 0) == Glib::ustring::npos) {
176 jid += "/" + static_cast< Glib::ustring >(RESOURCE_NAME);
177 }
179 g_log(NULL, G_LOG_LEVEL_DEBUG, "Full JID is %s", jid.c_str());
181 Glib::ustring msg = String::ucompose(_("Sending whiteboard invitation to <b>%1</b>"), jid);
182 this->_sm->desktop()->messageStack()->flash(Inkscape::NORMAL_MESSAGE, msg.data());
183 if (this->_sfsbox.isSelected()) {
184 this->_sm->session_data->sessionFile = this->_sfsbox.getFilename();
185 } else {
186 this->_sm->session_data->sessionFile.clear();
187 }
188 this->_sm->sendRequestToUser(jid);
189 this->hide();
190 break;
191 }
193 case CANCEL:
194 this->hide();
195 break;
197 default:
198 break;
199 }
200 }
202 void
203 WhiteboardShareWithUserDialogImpl::_listCallback()
204 {
205 Glib::RefPtr< Gtk::TreeSelection > sel = this->_buddylist.get_selection();
207 typedef Gtk::TreeModel::Children type_children;
208 type_children::iterator row = sel->get_selected();
209 this->_jid.set_text((*row).get_value(this->_blm.jid));
210 }
212 }
214 }
216 }
218 /*
219 Local Variables:
220 mode:c++
221 c-file-style:"stroustrup"
222 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
223 indent-tabs-mode:nil
224 fill-column:99
225 End:
226 */
227 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :