bookmarkstorage.cpp

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

Generated on Sat Nov 10 08:50:26 2007 for gloox by  doxygen 1.5.3-20071008