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