bookmarkstorage.cpp

00001 /*
00002   Copyright (c) 2005-2006 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->addAttribute( "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( s, "url" );
00044         i->addAttribute( "name", (*it).name );
00045         i->addAttribute( "url", (*it).url );
00046       }
00047     }
00048 
00049     if( cList.size() )
00050     {
00051       BookmarkHandler::ConferenceList::const_iterator it = cList.begin();
00052       for( ; it != cList.end(); ++it )
00053       {
00054         Tag *i = new Tag( s, "conference" );
00055         i->addAttribute( "name", (*it).name );
00056         i->addAttribute( "jid", (*it).jid );
00057         if( (*it).autojoin )
00058           i->addAttribute( "autojoin", "true" );
00059         else
00060           i->addAttribute( "autojoin", "false" );
00061 
00062         new Tag( i, "nick", (*it).nick );
00063         new Tag( i, "password", (*it).password );
00064       }
00065     }
00066 
00067     storeXML( s, this );
00068   }
00069 
00070   void BookmarkStorage::requestBookmarks()
00071   {
00072     requestXML( "storage", XMLNS_BOOKMARKS, this );
00073   }
00074 
00075   void BookmarkStorage::handlePrivateXML( const std::string& /*tag*/, Tag *xml )
00076   {
00077     BookmarkHandler::BookmarkList bList;
00078     BookmarkHandler::ConferenceList cList;
00079     const Tag::TagList l = xml->children();
00080     Tag::TagList::const_iterator it = l.begin();
00081     for( ; it != l.end(); ++it )
00082     {
00083       if( (*it)->name() == "url" )
00084       {
00085         const std::string url = (*it)->findAttribute( "url" );
00086         const std::string name = (*it)->findAttribute( "name" );
00087 
00088         if( !url.empty() && !name.empty() )
00089         {
00090           BookmarkHandler::bookmarkListItem item;
00091           item.url = url;
00092           item.name = name;
00093           bList.push_back( item );
00094         }
00095       }
00096       else if( (*it)->name() == "conference" )
00097       {
00098         bool autojoin = false;
00099         const std::string jid = (*it)->findAttribute( "jid" );
00100         const std::string name = (*it)->findAttribute( "name" );
00101         const std::string join = (*it)->findAttribute( "autojoin" );
00102         if( ( join == "true" ) || ( join == "1" ) )
00103           autojoin = true;
00104         const std::string nick = (*it)->findChild( "nick" )->cdata();
00105         const std::string pwd = (*it)->findChild( "password" )->cdata();
00106 
00107         if( !jid.empty() && !name.empty() )
00108         {
00109           BookmarkHandler::conferenceListItem item;
00110           item.jid = jid;
00111           item.name = name;
00112           item.nick = nick;
00113           item.password = pwd;
00114           item.autojoin = autojoin;
00115           cList.push_back( item );
00116         }
00117       }
00118     }
00119 
00120     if( m_bookmarkHandler )
00121       m_bookmarkHandler->handleBookmarks( bList, cList );
00122   }
00123 
00124   void BookmarkStorage::handlePrivateXMLResult( const std::string& /*uid*/, PrivateXMLResult /*result*/ )
00125   {
00126   }
00127 
00128   void BookmarkStorage::registerBookmarkHandler( BookmarkHandler *bmh )
00129   {
00130     m_bookmarkHandler = bmh;
00131   }
00132 
00133   void BookmarkStorage::removeBookmarkHandler()
00134   {
00135     m_bookmarkHandler = 0;
00136   }
00137 
00138 }

Generated on Tue May 1 14:20:20 2007 for gloox by  doxygen 1.5.1