gloox 1.0
bookmarkstorage.cpp
00001 /*
00002   Copyright (c) 2005-2009 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 BookmarkList& bList, const ConferenceList& cList )
00033   {
00034     Tag* s = new Tag( "storage" );
00035     s->addAttribute( XMLNS, XMLNS_BOOKMARKS );
00036 
00037     BookmarkList::const_iterator itb = bList.begin();
00038     for( ; itb != bList.end(); ++itb )
00039     {
00040       Tag* i = new Tag( s, "url", "name", (*itb).name );
00041       i->addAttribute( "url", (*itb).url );
00042     }
00043 
00044     ConferenceList::const_iterator itc = cList.begin();
00045     for( ; itc != cList.end(); ++itc )
00046     {
00047       Tag* i = new Tag( s, "conference", "name", (*itc).name );
00048       i->addAttribute( "jid", (*itc).jid );
00049       i->addAttribute( "autojoin", (*itc).autojoin ? "true" : "false" );
00050 
00051       new Tag( i, "nick", (*itc).nick );
00052       new Tag( i, "password", (*itc).password );
00053     }
00054 
00055     storeXML( s, this );
00056   }
00057 
00058   void BookmarkStorage::requestBookmarks()
00059   {
00060     requestXML( "storage", XMLNS_BOOKMARKS, this );
00061   }
00062 
00063   void BookmarkStorage::handlePrivateXML( const Tag* xml )
00064   {
00065     if( !xml )
00066       return;
00067 
00068     BookmarkList bList;
00069     ConferenceList cList;
00070     const TagList& l = xml->children();
00071     TagList::const_iterator it = l.begin();
00072     for( ; it != l.end(); ++it )
00073     {
00074       if( (*it)->name() == "url" )
00075       {
00076         const std::string& url = (*it)->findAttribute( "url" );
00077         const std::string& name = (*it)->findAttribute( "name" );
00078 
00079         if( !url.empty() && !name.empty() )
00080         {
00081           BookmarkListItem item;
00082           item.url = url;
00083           item.name = name;
00084           bList.push_back( item );
00085         }
00086       }
00087       else if( (*it)->name() == "conference" )
00088       {
00089         const std::string& jid = (*it)->findAttribute( "jid" );
00090         const std::string& name = (*it)->findAttribute( "name" );
00091 
00092         if( !jid.empty() && !name.empty() )
00093         {
00094           const std::string& join = (*it)->findAttribute( "autojoin" );
00095           ConferenceListItem item;
00096           item.jid = jid;
00097           item.name = name;
00098           const Tag* nick = (*it)->findChild( "nick" );
00099           if( nick )
00100             item.nick = nick->cdata();
00101           const Tag* pwd = (*it)->findChild( "password" );
00102           if( pwd )
00103             item.password = pwd->cdata();
00104           item.autojoin = ( join == "true" || join == "1" );
00105           cList.push_back( item );
00106         }
00107       }
00108     }
00109 
00110     if( m_bookmarkHandler )
00111       m_bookmarkHandler->handleBookmarks( bList, cList );
00112   }
00113 
00114   void BookmarkStorage::handlePrivateXMLResult( const std::string& /*uid*/, PrivateXMLResult /*result*/ )
00115   {
00116   }
00117 
00118 }