gloox  1.0.1
rosteritem.cpp
1 /*
2  Copyright (c) 2004-2012 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 "rosteritem.h"
16 #include "rosteritemdata.h"
17 #include "util.h"
18 
19 namespace gloox
20 {
21 
22  JID EmptyJID();
23 
24  RosterItem::RosterItem( const std::string& jid, const std::string& name )
25  : m_data( new RosterItemData( JID( jid ), name, StringList() ) )
26  {
27  }
28 
30  : m_data( new RosterItemData( data ) )
31  {
32  }
33 
35  {
36  delete m_data;
37  util::clearMap( m_resources );
38  }
39 
40  void RosterItem::setName( const std::string& name )
41  {
42  if( m_data )
43  m_data->setName( name );
44  }
45 
46  const std::string& RosterItem::name() const
47  {
48  if( m_data )
49  return m_data->name();
50  else
51  return EmptyString;
52  }
53 
54  const JID& RosterItem::jidJID() const
55  {
56  if( m_data )
57  return m_data->jidJID();
58  else
59  return EmptyJID;
60  }
61 
62  const std::string& RosterItem::jid() const
63  {
64  if( m_data )
65  return m_data->jid();
66  else
67  return EmptyString;
68  }
69 
70  void RosterItem::setSubscription( const std::string& subscription, const std::string& ask )
71  {
72  if( m_data )
73  m_data->setSubscription( subscription, ask );
74  }
75 
77  {
78  if( m_data )
79  return m_data->subscription();
80  else
81  return S10nNone;
82  }
83 
84  void RosterItem::setGroups( const StringList& groups )
85  {
86  if( m_data )
87  m_data->setGroups( groups );
88  }
89 
91  {
92  if( m_data )
93  return m_data->groups();
94  else
95  return StringList();
96  }
97 
98  bool RosterItem::changed() const
99  {
100  if( m_data )
101  return m_data->changed();
102  else
103  return false;
104  }
105 
107  {
108  if( m_data )
109  m_data->setSynchronized();
110  }
111 
112  void RosterItem::setPresence( const std::string& resource, Presence::PresenceType presence )
113  {
114  if( m_resources.find( resource ) == m_resources.end() )
115  m_resources[resource] = new Resource( 0, EmptyString, presence );
116  else
117  m_resources[resource]->setStatus( presence );
118  }
119 
120  void RosterItem::setStatus( const std::string& resource, const std::string& msg )
121  {
122  if( m_resources.find( resource ) == m_resources.end() )
123  m_resources[resource] = new Resource( 0, msg, Presence::Unavailable );
124  else
125  m_resources[resource]->setMessage( msg );
126  }
127 
128  void RosterItem::setPriority( const std::string& resource, int priority )
129  {
130  if( m_resources.find( resource ) == m_resources.end() )
131  m_resources[resource] = new Resource( priority, EmptyString, Presence::Unavailable );
132  else
133  m_resources[resource]->setPriority( priority );
134  }
135 
137  {
138  int highestPriority = -255;
140  ResourceMap::const_iterator it = m_resources.begin();
141  for( ; it != m_resources.end() ; ++it )
142  {
143  if( (*it).second->priority() > highestPriority )
144  {
145  highestPriority = (*it).second->priority();
146  highestResource = (*it).second;
147  }
148  }
149  return highestResource;
150  }
151 
152  void RosterItem::setExtensions( const std::string& resource, const StanzaExtensionList& exts )
153  {
154  if( m_resources.find( resource ) == m_resources.end() )
155  m_resources[resource] = new Resource( 0, EmptyString, Presence::Unavailable );
156 
157  m_resources[resource]->setExtensions( exts );
158  }
159 
160  void RosterItem::removeResource( const std::string& resource )
161  {
162  ResourceMap::iterator it = m_resources.find( resource );
163  if( it != m_resources.end() )
164  {
165  delete (*it).second;
166  m_resources.erase( it );
167  }
168  }
169 
170  bool RosterItem::online() const
171  {
172  return !m_resources.empty();
173  }
174 
175  const Resource* RosterItem::resource( const std::string& res ) const
176  {
177  ResourceMap::const_iterator it = m_resources.find( res );
178  return it != m_resources.end() ? (*it).second : 0;
179  }
180 
182  {
183  delete m_data;
184  m_data = new RosterItemData( rid );
185  }
186 
187 }