summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jabberbookmarks.cpp
blob: 720705b23b4fcaf8193fea3cb698d229ef2f2030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 /*
    Copyright (c) 2006      by Olivier Goffart  <ogoffart at kde.org>

    Kopete    (c) 2006 by the Kopete developers <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
 */

#include "jabberbookmarks.h"
#include "jabberaccount.h"

#include <kopetecontact.h>


#include <kdebug.h>
#include <kaction.h>
#include <klocale.h>

#include "xmpp_tasks.h"


JabberBookmarks::JabberBookmarks(JabberAccount *parent) : QObject(parent) , m_account(parent) 
{
	connect( m_account , SIGNAL( isConnectedChanged() ) , this , SLOT( accountConnected() ) );
}

void JabberBookmarks::accountConnected()
{
	if(!m_account->isConnected())
		return;
	
	XMPP::JT_PrivateStorage * task = new XMPP::JT_PrivateStorage ( m_account->client()->rootTask ());
	task->get( "storage" , "storage:bookmarks" );
	QObject::connect ( task, SIGNAL ( finished () ), this, SLOT ( slotReceivedBookmarks() ) );
	task->go ( true );
}

void JabberBookmarks::slotReceivedBookmarks( )
{
	XMPP::JT_PrivateStorage * task = (XMPP::JT_PrivateStorage*)(sender());
	m_storage=QDomDocument("storage");
	m_conferencesJID.clear();
	if(task->success())
	{
		QDomElement storage_e=task->element();
		if(!storage_e.isNull() && storage_e.tagName() == "storage")
		{
			storage_e=m_storage.importNode(storage_e,true).toElement();
			m_storage.appendChild(storage_e);

			for(QDomNode n = storage_e.firstChild(); !n.isNull(); n = n.nextSibling()) 
			{
				QDomElement i = n.toElement();
				if(i.isNull())
					continue;
				if(i.tagName() == "conference")
				{
					QString jid=i.attribute("jid");
					QString password;
					for(QDomNode n = i.firstChild(); !n.isNull(); n = n.nextSibling()) {
						QDomElement e = n.toElement();
						if(e.isNull())
							continue;
						else if(e.tagName() == "nick")
							jid+="/"+e.text();
						else if(e.tagName() == "password")
							password=e.text();
						
					}
					m_conferencesJID += jid;
					if(i.attribute("autojoin") == "true")
					{
						XMPP::Jid x_jid(jid);
						QString nick=x_jid.resource();
						if(nick.isEmpty())
							nick=m_account->myself()->nickName();

						if(password.isEmpty())
							m_account->client()->joinGroupChat(x_jid.host() , x_jid.user() , nick );
						else
							m_account->client()->joinGroupChat(x_jid.host() , x_jid.user() , nick , password);
					}
				}
			}
		}
	}
}


void JabberBookmarks::insertGroupChat(const XMPP::Jid &jid)
{
	if(m_conferencesJID.contains(jid.full()) || !m_account->isConnected())
	{
		return;
	}

	QDomElement storage_e=m_storage.documentElement();
	if(storage_e.isNull())
	{
		storage_e=m_storage.createElement("storage");
		m_storage.appendChild(storage_e);
		storage_e.setAttribute("xmlns","storage:bookmarks");
	}
	
	QDomElement conference=m_storage.createElement("conference");
	storage_e.appendChild(conference);
	conference.setAttribute("jid",jid.userHost());
	QDomElement nick=m_storage.createElement("nick");
	conference.appendChild(nick);
	nick.appendChild(m_storage.createTextNode(jid.resource()));
	QDomElement name=m_storage.createElement("name");
	conference.appendChild(name);
	name.appendChild(m_storage.createTextNode(jid.full()));

		
	XMPP::JT_PrivateStorage * task = new XMPP::JT_PrivateStorage ( m_account->client()->rootTask ());
	task->set( storage_e );
	task->go ( true );
	
	m_conferencesJID += jid.full();
}

KAction * JabberBookmarks::bookmarksAction(QObject *parent)
{
	KSelectAction *groupchatBM = new KSelectAction( i18n("Groupchat bookmark") , "jabber_group" , 0 , parent , "actionBookMark" );
	groupchatBM->setItems(m_conferencesJID);
	QObject::connect(groupchatBM, SIGNAL(activated (const QString&)) , this, SLOT(slotJoinChatBookmark(const QString&)));
	return groupchatBM;
}

void JabberBookmarks::slotJoinChatBookmark( const QString & _jid )
{
	if(!m_account->isConnected())
		return;
	XMPP::Jid jid(_jid);
	m_account->client()->joinGroupChat( jid.host() , jid.user() , jid.resource() );
}



#include "jabberbookmarks.moc"