gloox  0.9.9.12
bookmarkstorage.cpp
1 /*
2  Copyright (c) 2005-2008 by Jakob Schroeter <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" );
35  s->addAttribute( "xmlns", XMLNS_BOOKMARKS );
36 
37  if( bList.size() )
38  {
39  BookmarkList::const_iterator it = bList.begin();
40  for( ; it != bList.end(); ++it )
41  {
42  Tag *i = new Tag( s, "url" );
43  i->addAttribute( "name", (*it).name );
44  i->addAttribute( "url", (*it).url );
45  }
46  }
47 
48  if( cList.size() )
49  {
50  ConferenceList::const_iterator it = cList.begin();
51  for( ; it != cList.end(); ++it )
52  {
53  Tag *i = new Tag( s, "conference" );
54  i->addAttribute( "name", (*it).name );
55  i->addAttribute( "jid", (*it).jid );
56  if( (*it).autojoin )
57  i->addAttribute( "autojoin", "true" );
58  else
59  i->addAttribute( "autojoin", "false" );
60 
61  new Tag( i, "nick", (*it).nick );
62  new Tag( i, "password", (*it).password );
63  }
64  }
65 
66  storeXML( s, this );
67  }
68 
70  {
71  requestXML( "storage", XMLNS_BOOKMARKS, this );
72  }
73 
74  void BookmarkStorage::handlePrivateXML( const std::string& /*tag*/, Tag *xml )
75  {
76  BookmarkList bList;
77  ConferenceList cList;
78  const Tag::TagList& l = xml->children();
79  Tag::TagList::const_iterator it = l.begin();
80  for( ; it != l.end(); ++it )
81  {
82  if( (*it)->name() == "url" )
83  {
84  const std::string& url = (*it)->findAttribute( "url" );
85  const std::string& name = (*it)->findAttribute( "name" );
86 
87  if( !url.empty() && !name.empty() )
88  {
89  BookmarkListItem item;
90  item.url = url;
91  item.name = name;
92  bList.push_back( item );
93  }
94  }
95  else if( (*it)->name() == "conference" )
96  {
97  bool autojoin = false;
98  const std::string& jid = (*it)->findAttribute( "jid" );
99  const std::string& name = (*it)->findAttribute( "name" );
100  const std::string& join = (*it)->findAttribute( "autojoin" );
101  if( ( join == "true" ) || ( join == "1" ) )
102  autojoin = true;
103  std::string nick;
104  const Tag* nickname = (*it)->findChild( "nick" );
105  if( nickname )
106  nick = nickname->cdata();
107  std::string pwd;
108  const Tag* password = (*it)->findChild( "password" );
109  if( password )
110  pwd = password->cdata();
111 
112  if( !jid.empty() && !name.empty() )
113  {
114  ConferenceListItem item;
115  item.jid = jid;
116  item.name = name;
117  item.nick = nick;
118  item.password = pwd;
119  item.autojoin = autojoin;
120  cList.push_back( item );
121  }
122  }
123  }
124 
125  if( m_bookmarkHandler )
126  m_bookmarkHandler->handleBookmarks( bList, cList );
127  }
128 
129  void BookmarkStorage::handlePrivateXMLResult( const std::string& /*uid*/, PrivateXMLResult /*result*/ )
130  {
131  }
132 
134  {
135  m_bookmarkHandler = bmh;
136  }
137 
139  {
140  m_bookmarkHandler = 0;
141  }
142 
143 }