~keith-penguin/kdegames/trunk

« back to all changes in this revision

Viewing changes to ksirk/ksirk/Jabber/jabberbookmarks.cpp

  • Committer: Keith Worrell
  • Date: 2009-03-18 05:35:28 UTC
  • Revision ID: keith.worrell@gmail.com-20090318053528-mx6x9c0ngmg0kg6p
imported project

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
22
#include <kdebug.h>
 
23
#include <kaction.h>
 
24
#include <kselectaction.h>
 
25
#include <klocale.h>
 
26
#include <kicon.h>
 
27
 
 
28
#include "tasks/jt_privatestorage.h"
 
29
 
 
30
 
 
31
JabberBookmarks::JabberBookmarks(JabberAccount *parent) : QObject(parent) , m_account(parent) 
 
32
{
 
33
        connect( m_account , SIGNAL( isConnectedChanged() ) , this , SLOT( accountConnected() ) );
 
34
}
 
35
 
 
36
void JabberBookmarks::accountConnected()
 
37
{
 
38
        if(!m_account->isConnected())
 
39
                return;
 
40
        
 
41
        JT_PrivateStorage * task = new JT_PrivateStorage ( m_account->client()->rootTask ());
 
42
        task->get( "storage" , "storage:bookmarks" );
 
43
        QObject::connect ( task, SIGNAL ( finished () ), this, SLOT ( slotReceivedBookmarks() ) );
 
44
        task->go ( true );
 
45
}
 
46
 
 
47
void JabberBookmarks::slotReceivedBookmarks( )
 
48
{
 
49
        JT_PrivateStorage * task = (JT_PrivateStorage*)(sender());
 
50
        m_storage=QDomDocument("storage");
 
51
        m_conferencesJID.clear();
 
52
        if(task->success())
 
53
        {
 
54
                QDomElement storage_e=task->element();
 
55
                if(!storage_e.isNull() && storage_e.tagName() == "storage")
 
56
                {
 
57
                        storage_e=m_storage.importNode(storage_e,true).toElement();
 
58
                        m_storage.appendChild(storage_e);
 
59
 
 
60
                        for(QDomNode n = storage_e.firstChild(); !n.isNull(); n = n.nextSibling()) 
 
61
                        {
 
62
                                QDomElement i = n.toElement();
 
63
                                if(i.isNull())
 
64
                                        continue;
 
65
                                if(i.tagName() == "conference")
 
66
                                {
 
67
                                        QString jid=i.attribute("jid");
 
68
                                        QString password;
 
69
                                        for(QDomNode n = i.firstChild(); !n.isNull(); n = n.nextSibling()) {
 
70
                                                QDomElement e = n.toElement();
 
71
                                                if(e.isNull())
 
72
                                                        continue;
 
73
                                                else if(e.tagName() == "nick")
 
74
                                                        jid+='/'+e.text();
 
75
                                                else if(e.tagName() == "password")
 
76
                                                        password=e.text();
 
77
                                                
 
78
                                        }
 
79
                                        m_conferencesJID += jid;
 
80
                                        if(i.attribute("autojoin") == "true")
 
81
                                        {
 
82
                                                XMPP::Jid x_jid(jid);
 
83
                                                QString nick=x_jid.resource();
 
84
                                                if(nick.isEmpty())
 
85
                                                        nick=m_account->myself()->nickName();
 
86
 
 
87
                                                if(password.isEmpty())
 
88
                                                        m_account->client()->joinGroupChat(x_jid.host() , x_jid.user() , nick );
 
89
                                                else
 
90
                                                        m_account->client()->joinGroupChat(x_jid.host() , x_jid.user() , nick , password);
 
91
                                        }
 
92
                                }
 
93
                        }
 
94
                }
 
95
        }
 
96
}
 
97
 
 
98
 
 
99
void JabberBookmarks::insertGroupChat(const XMPP::Jid &jid)
 
100
{
 
101
        if(m_conferencesJID.contains(jid.full()) || !m_account->isConnected())
 
102
        {
 
103
                return;
 
104
        }
 
105
 
 
106
        QDomElement storage_e=m_storage.documentElement();
 
107
        if(storage_e.isNull())
 
108
        {
 
109
                storage_e=m_storage.createElement("storage");
 
110
                m_storage.appendChild(storage_e);
 
111
                storage_e.setAttribute("xmlns","storage:bookmarks");
 
112
        }
 
113
        
 
114
        QDomElement conference=m_storage.createElement("conference");
 
115
        storage_e.appendChild(conference);
 
116
        conference.setAttribute("jid",jid.userHost());
 
117
        QDomElement nick=m_storage.createElement("nick");
 
118
        conference.appendChild(nick);
 
119
        nick.appendChild(m_storage.createTextNode(jid.resource()));
 
120
        QDomElement name=m_storage.createElement("name");
 
121
        conference.appendChild(name);
 
122
        name.appendChild(m_storage.createTextNode(jid.full()));
 
123
 
 
124
        JT_PrivateStorage * task = new JT_PrivateStorage ( m_account->client()->rootTask ());
 
125
        task->set( storage_e );
 
126
        task->go ( true );
 
127
        
 
128
        m_conferencesJID += jid.full();
 
129
}
 
130
 
 
131
KAction * JabberBookmarks::bookmarksAction(QObject * /*parent*/)
 
132
{
 
133
        KSelectAction *groupchatBM = new KSelectAction( this );
 
134
        groupchatBM->setIcon( KIcon("jabber_group") );
 
135
        groupchatBM->setText( i18n("Groupchat bookmark") );
 
136
        groupchatBM->setItems(m_conferencesJID);
 
137
        QObject::connect(groupchatBM, SIGNAL(triggered(const QString&)) , this , SLOT(slotJoinChatBookmark(const QString&)));
 
138
        return groupchatBM;
 
139
}
 
140
 
 
141
void JabberBookmarks::slotJoinChatBookmark( const QString & _jid )
 
142
{
 
143
        if(!m_account->isConnected())
 
144
                return;
 
145
        XMPP::Jid jid(_jid);
 
146
        m_account->client()->joinGroupChat( jid.host() , jid.user() , jid.resource() );
 
147
}
 
148
 
 
149
#include "jabberbookmarks.moc"