1  /*
2     Copyright (c) 2006      by Olivier Goffart  <ogoffart at kde.org>
3 
4     Kopete    (c) 2006 by the Kopete developers <kopete-devel@kde.org>
5 
6     *************************************************************************
7     *                                                                       *
8     * This program is free software; you can redistribute it and/or modify  *
9     * it under the terms of the GNU General Public License as published by  *
10     * the Free Software Foundation; either version 2 of the License, or     *
11     * (at your option) any later version.                                   *
12     *                                                                       *
13     *************************************************************************
14  */
15 
16 #include "jabberbookmarks.h"
17 #include "jabberaccount.h"
18 
19 #include <kopetecontact.h>
20 
21 #include <QPointer>
22 #include "jabber_protocol_debug.h"
23 #include <QAction>
24 #include <kselectaction.h>
25 #include <KLocalizedString>
26 #include <QIcon>
27 
28 #include "tasks/jt_privatestorage.h"
29 #include "ui/dlgjabberbookmarkeditor.h"
30 
JabberBookmark()31 JabberBookmark::JabberBookmark() : m_autoJoin( false )
32 {
33 }
34 
setJId(const QString & jid)35 void JabberBookmark::setJId( const QString &jid )
36 {
37 	m_jId = jid;
38 }
39 
jId() const40 QString JabberBookmark::jId() const
41 {
42 	return m_jId;
43 }
44 
fullJId() const45 QString JabberBookmark::fullJId() const
46 {
47 	if ( !m_nickName.isEmpty() )
48 		return m_jId + '/' + m_nickName;
49 	else
50 		return m_jId;
51 }
52 
setName(const QString & name)53 void JabberBookmark::setName( const QString &name )
54 {
55 	m_name = name;
56 }
57 
name() const58 QString JabberBookmark::name() const
59 {
60 	return m_name;
61 }
62 
setNickName(const QString & name)63 void JabberBookmark::setNickName( const QString &name )
64 {
65 	m_nickName = name;
66 }
67 
nickName() const68 QString JabberBookmark::nickName() const
69 {
70 	return m_nickName;
71 }
72 
setPassword(const QString & password)73 void JabberBookmark::setPassword( const QString &password )
74 {
75 	m_password = password;
76 }
77 
password() const78 QString JabberBookmark::password() const
79 {
80 	return m_password;
81 }
82 
setAutoJoin(bool autoJoin)83 void JabberBookmark::setAutoJoin( bool autoJoin )
84 {
85 	m_autoJoin = autoJoin;
86 }
87 
autoJoin() const88 bool JabberBookmark::autoJoin() const
89 {
90 	return m_autoJoin;
91 }
92 
JabberBookmarks(JabberAccount * parent)93 JabberBookmarks::JabberBookmarks(JabberAccount *parent) : QObject(parent) , m_account(parent)
94 {
95 	connect( m_account , SIGNAL(isConnectedChanged()) , this , SLOT(accountConnected()) );
96 }
97 
accountConnected()98 void JabberBookmarks::accountConnected()
99 {
100 	if(!m_account->isConnected())
101 		return;
102 
103 	JT_PrivateStorage * task = new JT_PrivateStorage ( m_account->client()->rootTask ());
104 	task->get( QStringLiteral("storage") , QStringLiteral("storage:bookmarks") );
105 	QObject::connect ( task, SIGNAL (finished()), this, SLOT (slotReceivedBookmarks()) );
106 	task->go ( true );
107 }
108 
bookmarksFromStorage(const QDomElement & storageElement)109 JabberBookmark::List JabberBookmarks::bookmarksFromStorage( const QDomElement &storageElement )
110 {
111 	JabberBookmark::List bookmarks;
112 	if ( !storageElement.isNull() && storageElement.tagName() == QLatin1String("storage") ) {
113 		for ( QDomElement element = storageElement.firstChildElement(); !element.isNull(); element = element.nextSiblingElement() ) {
114 
115 			if ( element.tagName() == QLatin1String("conference") ) {
116 				JabberBookmark bookmark;
117 
118 				bookmark.setJId( element.attribute( QStringLiteral("jid") ) );
119 				bookmark.setName( element.attribute( QStringLiteral("name") ) );
120 				bookmark.setAutoJoin( element.attribute( QStringLiteral("autojoin"), QStringLiteral("false") ) == QLatin1String("true") );
121 
122 				for ( QDomElement childElement = element.firstChildElement(); !childElement.isNull(); childElement = childElement.nextSiblingElement() ) {
123 					if ( childElement.tagName() == QLatin1String("nick") ) {
124 						bookmark.setNickName( childElement.text() );
125 					} else if ( childElement.tagName() == QLatin1String("password") ) {
126 						bookmark.setPassword( childElement.text() );
127 					}
128 				}
129 
130 				bookmarks += bookmark;
131 			}
132 		}
133 	}
134 
135 	return bookmarks;
136 }
137 
bookmarksToStorage(const JabberBookmark::List & bookmarks,QDomDocument & document)138 QDomElement JabberBookmarks::bookmarksToStorage( const JabberBookmark::List &bookmarks, QDomDocument &document )
139 {
140 	QDomElement storageElement = document.createElement( QStringLiteral("storage") );
141 	storageElement.setAttribute( QStringLiteral("xmlns"), QStringLiteral("storage:bookmarks") );
142 
143 	foreach ( const JabberBookmark &bookmark, bookmarks ) {
144 		QDomElement conferenceElement = document.createElement( QStringLiteral("conference") );
145 		conferenceElement.setAttribute( QStringLiteral("jid"), bookmark.jId() );
146 
147 		if ( !bookmark.name().isEmpty() )
148 			conferenceElement.setAttribute( QStringLiteral("name"), bookmark.name() );
149 
150 		if ( bookmark.autoJoin() )
151 			conferenceElement.setAttribute( QStringLiteral("autojoin"), QStringLiteral("true") );
152 
153 		if ( !bookmark.nickName().isEmpty() ) {
154 			QDomElement element = document.createElement( QStringLiteral("nick") );
155 			element.appendChild( document.createTextNode( bookmark.nickName() ) );
156 			conferenceElement.appendChild( element );
157 		}
158 
159 		if ( !bookmark.password().isEmpty() ) {
160 			QDomElement element = document.createElement( QStringLiteral("password") );
161 			element.appendChild( document.createTextNode( bookmark.password() ) );
162 			conferenceElement.appendChild( element );
163 		}
164 
165 		storageElement.appendChild( conferenceElement );
166 	}
167 
168 	return storageElement;
169 }
170 
slotReceivedBookmarks()171 void JabberBookmarks::slotReceivedBookmarks( )
172 {
173 	JT_PrivateStorage *task = (JT_PrivateStorage*)( sender() );
174 	m_bookmarks.clear();
175 
176 	if ( task->success() ) {
177 		m_bookmarks = bookmarksFromStorage( task->element() );
178 
179 		foreach ( const JabberBookmark &bookmark, m_bookmarks ) {
180 			if ( bookmark.autoJoin() ) {
181 				XMPP::Jid x_jid( bookmark.fullJId() );
182 
183 				QString nickName = x_jid.resource();
184 				if ( nickName.isEmpty() )
185 					nickName = m_account->myself()->displayName();
186 
187 				if ( bookmark.password().isEmpty() )
188 					m_account->client()->joinGroupChat( x_jid.domain(), x_jid.node(), nickName );
189 				else
190 					m_account->client()->joinGroupChat( x_jid.domain(), x_jid.node(), nickName, bookmark.password() );
191 			}
192 		}
193 	}
194 }
195 
insertGroupChat(const XMPP::Jid & jid)196 void JabberBookmarks::insertGroupChat(const XMPP::Jid &jid)
197 {
198 	bool containsConference = false;
199 	foreach ( const JabberBookmark &bookmark, m_bookmarks ) {
200 		if ( bookmark.fullJId() == jid.full() ) {
201 			containsConference = true;
202 			break;
203 		}
204 	}
205 
206 	if ( containsConference || !m_account->isConnected() )
207 		return;
208 
209 	JabberBookmark bookmark;
210 	bookmark.setJId( jid.bare() );
211 	bookmark.setNickName( jid.resource() );
212 	bookmark.setName( jid.full() );
213 
214 	m_bookmarks.append( bookmark );
215 
216 	QDomDocument document( QStringLiteral("storage") );
217 	const QDomElement element = bookmarksToStorage( m_bookmarks, document );
218 
219 	JT_PrivateStorage *task = new JT_PrivateStorage( m_account->client()->rootTask() );
220 	task->set( element );
221 	task->go( true );
222 }
223 
bookmarksAction(QObject * parent)224 QAction * JabberBookmarks::bookmarksAction(QObject *parent)
225 {
226 	Q_UNUSED( parent )
227 
228 	QStringList menuEntries;
229 	foreach ( const JabberBookmark &bookmark, m_bookmarks ) {
230 		menuEntries << bookmark.fullJId();
231 	}
232 
233 	if ( !menuEntries.isEmpty() ) {
234 		menuEntries << QString(); // separator
235 		menuEntries << i18n( "Edit Bookmarks..." );
236 	}
237 
238 	KSelectAction *action = new KSelectAction( this );
239     action->setIcon( QIcon::fromTheme(QStringLiteral("jabber_group")) );
240 	action->setText( i18n( "Groupchat Bookmark" ) );
241 	action->setItems( menuEntries );
242 
243 	connect( action, SIGNAL(triggered(QString)), this, SLOT(slotJoinChatBookmark(QString)) );
244 	return action;
245 }
246 
slotJoinChatBookmark(const QString & _jid)247 void JabberBookmarks::slotJoinChatBookmark( const QString & _jid )
248 {
249 	if ( !m_account->isConnected() )
250 		return;
251 
252 	if ( _jid != i18n( "Edit Bookmarks..." ) ) {
253 		XMPP::Jid jid( _jid );
254 		m_account->client()->joinGroupChat( jid.domain(), jid.node(), jid.resource() );
255 	} else {
256 		QPointer <DlgJabberBookmarkEditor> editor = new DlgJabberBookmarkEditor( m_bookmarks );
257 		if ( editor->exec() && editor ) {
258 			m_bookmarks = editor->bookmarks();
259 
260 			QDomDocument document( QStringLiteral("storage") );
261 			const QDomElement element = bookmarksToStorage( m_bookmarks, document );
262 
263 			JT_PrivateStorage *task = new JT_PrivateStorage( m_account->client()->rootTask() );
264 			task->set( element );
265 			task->go( true );
266 		}
267 		delete editor;
268 	}
269 }
270 
271