File indexing completed on 2024-04-21 04:04:40

0001  /*
0002     Copyright (c) 2006      by Olivier Goffart  <ogoffart at kde.org>
0003 
0004     Kopete    (c) 2006 by the Kopete developers <kopete-devel@kde.org>
0005 
0006     *************************************************************************
0007     *                                                                       *
0008     * This program is free software; you can redistribute it and/or modify  *
0009     * it under the terms of the GNU General Public License as published by  *
0010     * the Free Software Foundation; either either version 2
0011    of the License, or (at your option) any later version.of the License, or     *
0012     * (at your option) any later version.                                   *
0013     *                                                                       *
0014     *************************************************************************
0015  */
0016 
0017 #include "jabberbookmarks.h"
0018 #include "jabberaccount.h"
0019 
0020 #include <kopetecontact.h>
0021 
0022 
0023 #include <QAction>
0024 #include <KSelectAction>
0025 #include <KLocalizedString>
0026 #include <QIcon>
0027 
0028 #include "tasks/jt_privatestorage.h"
0029 
0030 
0031 JabberBookmarks::JabberBookmarks(JabberAccount *parent) : QObject(parent) , m_account(parent) 
0032 {
0033     connect( m_account , SIGNAL(isConnectedChanged()) , this , SLOT(accountConnected()) );
0034 }
0035 
0036 void JabberBookmarks::accountConnected()
0037 {
0038     if(!m_account->isConnected())
0039         return;
0040     
0041     JT_PrivateStorage * task = new JT_PrivateStorage ( m_account->client()->rootTask ());
0042     task->get( "storage" , "storage:bookmarks" );
0043     QObject::connect ( task, SIGNAL (finished()), this, SLOT (slotReceivedBookmarks()) );
0044     task->go ( true );
0045 }
0046 
0047 void JabberBookmarks::slotReceivedBookmarks( )
0048 {
0049     JT_PrivateStorage * task = (JT_PrivateStorage*)(sender());
0050     m_storage=QDomDocument("storage");
0051     m_conferencesJID.clear();
0052     if(task->success())
0053     {
0054         QDomElement storage_e=task->element();
0055         if(!storage_e.isNull() && storage_e.tagName() == "storage")
0056         {
0057             storage_e=m_storage.importNode(storage_e,true).toElement();
0058             m_storage.appendChild(storage_e);
0059 
0060             for(QDomNode n = storage_e.firstChild(); !n.isNull(); n = n.nextSibling()) 
0061             {
0062                 QDomElement i = n.toElement();
0063                 if(i.isNull())
0064                     continue;
0065                 if(i.tagName() == "conference")
0066                 {
0067                     QString jid=i.attribute("jid");
0068                     QString password;
0069                     for(QDomNode n = i.firstChild(); !n.isNull(); n = n.nextSibling()) {
0070                         QDomElement e = n.toElement();
0071                         if(e.isNull())
0072                             continue;
0073                         else if(e.tagName() == "nick")
0074                             jid+='/'+e.text();
0075                         else if(e.tagName() == "password")
0076                             password=e.text();
0077                         
0078                     }
0079                     m_conferencesJID += jid;
0080                     if(i.attribute("autojoin") == "true")
0081                     {
0082                         XMPP::Jid x_jid(jid);
0083                         QString nick=x_jid.resource();
0084                         if(nick.isEmpty())
0085                             nick=m_account->myself()->nickName();
0086 
0087                         if(password.isEmpty())
0088                             m_account->client()->joinGroupChat(x_jid.host() , x_jid.user() , nick );
0089                         else
0090                             m_account->client()->joinGroupChat(x_jid.host() , x_jid.user() , nick , password);
0091                     }
0092                 }
0093             }
0094         }
0095     }
0096 }
0097 
0098 
0099 void JabberBookmarks::insertGroupChat(const XMPP::Jid &jid)
0100 {
0101     if(m_conferencesJID.contains(jid.full()) || !m_account->isConnected())
0102     {
0103         return;
0104     }
0105 
0106     QDomElement storage_e=m_storage.documentElement();
0107     if(storage_e.isNull())
0108     {
0109         storage_e=m_storage.createElement("storage");
0110         m_storage.appendChild(storage_e);
0111         storage_e.setAttribute("xmlns","storage:bookmarks");
0112     }
0113     
0114     QDomElement conference=m_storage.createElement("conference");
0115     storage_e.appendChild(conference);
0116     conference.setAttribute("jid",jid.userHost());
0117     QDomElement nick=m_storage.createElement("nick");
0118     conference.appendChild(nick);
0119     nick.appendChild(m_storage.createTextNode(jid.resource()));
0120     QDomElement name=m_storage.createElement("name");
0121     conference.appendChild(name);
0122     name.appendChild(m_storage.createTextNode(jid.full()));
0123 
0124     JT_PrivateStorage * task = new JT_PrivateStorage ( m_account->client()->rootTask ());
0125     task->set( storage_e );
0126     task->go ( true );
0127     
0128     m_conferencesJID += jid.full();
0129 }
0130 
0131 QAction * JabberBookmarks::bookmarksAction(QObject * /*parent*/)
0132 {
0133     KSelectAction *groupchatBM = new KSelectAction( this );
0134     groupchatBM->setIcon( QIcon::fromTheme( QLatin1String( "jabber_group" )) );
0135     groupchatBM->setText( i18n("Groupchat bookmark") );
0136     groupchatBM->setItems(m_conferencesJID);
0137     QObject::connect(groupchatBM, SIGNAL(triggered(QString)) , this , SLOT(slotJoinChatBookmark(QString)));
0138     return groupchatBM;
0139 }
0140 
0141 void JabberBookmarks::slotJoinChatBookmark( const QString & _jid )
0142 {
0143     if(!m_account->isConnected())
0144         return;
0145     XMPP::Jid jid(_jid);
0146     m_account->client()->joinGroupChat( jid.host() , jid.user() , jid.resource() );
0147 }
0148 
0149 #include "moc_jabberbookmarks.cpp"