bookmarkstorage.cpp

00001 /*
00002   Copyright (c) 2005 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 
00015 #include "bookmarkstorage.h"
00016 #include "clientbase.h"
00017 
00018 
00019 namespace gloox
00020 {
00021 
00022   BookmarkStorage::BookmarkStorage( ClientBase *parent )
00023     : PrivateXML( parent ),
00024     m_bookmarkHandler( 0 )
00025   {
00026   }
00027 
00028   BookmarkStorage::~BookmarkStorage()
00029   {
00030   }
00031 
00032   void BookmarkStorage::storeBookmarks( const BookmarkHandler::BookmarkList& bList,
00033                                         const BookmarkHandler::ConferenceList& cList )
00034   {
00035     Tag *s = new Tag( "storage" );
00036     s->addAttrib( "xmlns", XMLNS_BOOKMARKS );
00037 
00038     if( bList.size() )
00039     {
00040       BookmarkHandler::BookmarkList::const_iterator it = bList.begin();
00041       for( ; it != bList.end(); ++it )
00042       {
00043         Tag *i = new Tag( "url" );
00044         i->addAttrib( "name", (*it).name );
00045         i->addAttrib( "url", (*it).url );
00046         s->addChild( i );
00047       }
00048     }
00049 
00050     if( cList.size() )
00051     {
00052       BookmarkHandler::ConferenceList::const_iterator it = cList.begin();
00053       for( ; it != cList.end(); ++it )
00054       {
00055         Tag *i = new Tag( "conference" );
00056         i->addAttrib( "name", (*it).name );
00057         i->addAttrib( "jid", (*it).jid );
00058         if( (*it).autojoin )
00059           i->addAttrib( "autojoin", "true" );
00060         else
00061           i->addAttrib( "autojoin", "false" );
00062         i->addChild( new Tag( "nick", (*it).nick ) );
00063         i->addChild( new Tag( "password", (*it).password ) );
00064         s->addChild( i );
00065       }
00066     }
00067 
00068     storeXML( s, this );
00069   }
00070 
00071   void BookmarkStorage::requestBookmarks()
00072   {
00073     requestXML( "storage", XMLNS_BOOKMARKS, this );
00074   }
00075 
00076   void BookmarkStorage::handlePrivateXML( const std::string& /*tag*/, Tag *xml )
00077   {
00078     BookmarkHandler::BookmarkList bList;
00079     BookmarkHandler::ConferenceList cList;
00080     const Tag::TagList l = xml->children();
00081     Tag::TagList::const_iterator it = l.begin();
00082     for( ; it != l.end(); ++it )
00083     {
00084       if( (*it)->name() == "url" )
00085       {
00086         const std::string url = (*it)->findAttribute( "url" );
00087         const std::string name = (*it)->findAttribute( "name" );
00088 
00089         if( !url.empty() && !name.empty() )
00090         {
00091           BookmarkHandler::bookmarkListItem item;
00092           item.url = url;
00093           item.name = name;
00094           bList.push_back( item );
00095         }
00096       }
00097       else if( (*it)->name() == "conference" )
00098       {
00099         bool autojoin = false;
00100         const std::string jid = (*it)->findAttribute( "jid" );
00101         const std::string name = (*it)->findAttribute( "name" );
00102         const std::string join = (*it)->findAttribute( "autojoin" );
00103         if( ( join == "true" ) || ( join == "1" ) )
00104           autojoin = true;
00105         const std::string nick = (*it)->findChild( "nick" )->cdata();
00106         const std::string pwd = (*it)->findChild( "password" )->cdata();
00107 
00108         if( !jid.empty() && !name.empty() )
00109         {
00110           BookmarkHandler::conferenceListItem item;
00111           item.jid = jid;
00112           item.name = name;
00113           item.nick = nick;
00114           item.password = pwd;
00115           item.autojoin = autojoin;
00116           cList.push_back( item );
00117         }
00118       }
00119     }
00120 
00121     if( m_bookmarkHandler )
00122       m_bookmarkHandler->handleBookmarks( bList, cList );
00123   }
00124 
00125   void BookmarkStorage::registerBookmarkHandler( BookmarkHandler *bmh )
00126   {
00127     m_bookmarkHandler = bmh;
00128   }
00129 
00130   void BookmarkStorage::removeBookmarkHandler()
00131   {
00132     m_bookmarkHandler = 0;
00133   }
00134 
00135 }

Generated on Mon Jan 16 16:19:54 2006 for gloox by  doxygen 1.4.6