gloox  1.0.23
bookmarkstorage.cpp
1 /*
2  Copyright (c) 2005-2019 by Jakob Schröter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 
15 #include "bookmarkstorage.h"
16 #include "clientbase.h"
17 
18 
19 namespace gloox
20 {
21 
23  : PrivateXML( parent ),
24  m_bookmarkHandler( 0 )
25  {
26  }
27 
29  {
30  }
31 
32  void BookmarkStorage::storeBookmarks( const BookmarkList& bList, const ConferenceList& cList )
33  {
34  Tag* s = new Tag( "storage" );
36 
37  BookmarkList::const_iterator itb = bList.begin();
38  for( ; itb != bList.end(); ++itb )
39  {
40  Tag* i = new Tag( s, "url", "name", (*itb).name );
41  i->addAttribute( "url", (*itb).url );
42  }
43 
44  ConferenceList::const_iterator itc = cList.begin();
45  for( ; itc != cList.end(); ++itc )
46  {
47  Tag* i = new Tag( s, "conference", "name", (*itc).name );
48  i->addAttribute( "jid", (*itc).jid );
49  i->addAttribute( "autojoin", (*itc).autojoin ? "true" : "false" );
50 
51  new Tag( i, "nick", (*itc).nick );
52  new Tag( i, "password", (*itc).password );
53  }
54 
55  storeXML( s, this );
56  }
57 
59  {
60  requestXML( "storage", XMLNS_BOOKMARKS, this );
61  }
62 
64  {
65  if( !xml )
66  return;
67 
68  BookmarkList bList;
69  ConferenceList cList;
70  const TagList& l = xml->children();
71  TagList::const_iterator it = l.begin();
72  for( ; it != l.end(); ++it )
73  {
74  if( (*it)->name() == "url" )
75  {
76  const std::string& url = (*it)->findAttribute( "url" );
77  const std::string& name = (*it)->findAttribute( "name" );
78 
79  if( !url.empty() && !name.empty() )
80  {
81  BookmarkListItem item;
82  item.url = url;
83  item.name = name;
84  bList.push_back( item );
85  }
86  }
87  else if( (*it)->name() == "conference" )
88  {
89  const std::string& jid = (*it)->findAttribute( "jid" );
90  const std::string& name = (*it)->findAttribute( "name" );
91 
92  if( !jid.empty() && !name.empty() )
93  {
94  const std::string& join = (*it)->findAttribute( "autojoin" );
95  ConferenceListItem item;
96  item.jid = jid;
97  item.name = name;
98  const Tag* nick = (*it)->findChild( "nick" );
99  if( nick )
100  item.nick = nick->cdata();
101  const Tag* pwd = (*it)->findChild( "password" );
102  if( pwd )
103  item.password = pwd->cdata();
104  item.autojoin = ( join == "true" || join == "1" );
105  cList.push_back( item );
106  }
107  }
108  }
109 
110  if( m_bookmarkHandler )
111  m_bookmarkHandler->handleBookmarks( bList, cList );
112  }
113 
114  void BookmarkStorage::handlePrivateXMLResult( const std::string& /*uid*/, PrivateXMLResult /*result*/ )
115  {
116  }
117 
118 }
std::string requestXML(const std::string &tag, const std::string &xmlns, PrivateXMLHandler *pxh)
Definition: privatexml.cpp:74
std::string storeXML(const Tag *tag, PrivateXMLHandler *pxh)
Definition: privatexml.cpp:88
const std::string XMLNS
Definition: gloox.cpp:122
virtual void handlePrivateXML(const Tag *xml)
std::list< Tag * > TagList
Definition: tag.h:26
const std::string & name() const
Definition: tag.h:394
Tag * findChild(const std::string &name) const
Definition: tag.cpp:624
virtual void handleBookmarks(const BookmarkList &bList, const ConferenceList &cList)=0
const std::string XMLNS_BOOKMARKS
Definition: gloox.cpp:59
The namespace for the gloox library.
Definition: adhoc.cpp:27
This class implements XEP-0049 (Private XML Storage).
Definition: privatexml.h:37
bool addAttribute(Attribute *attr)
Definition: tag.cpp:354
BookmarkStorage(ClientBase *parent)
std::list< ConferenceListItem > ConferenceList
const TagList & children() const
Definition: tag.cpp:510
const std::string cdata() const
Definition: tag.cpp:497
virtual void handlePrivateXMLResult(const std::string &uid, PrivateXMLResult pxResult)
void storeBookmarks(const BookmarkList &bList, const ConferenceList &cList)
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition: clientbase.h:76
This is an abstraction of an XML element.
Definition: tag.h:46
std::list< BookmarkListItem > BookmarkList