gloox  1.0.28
rosteritem.cpp
1 /*
2  Copyright (c) 2004-2023 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 "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 }
An abstraction of a JID.
Definition: jid.h:31
Holds resource attributes.
Definition: resource.h:36
A class holding roster item data.
void setGroups(const StringList &groups)
SubscriptionType subscription() const
void setSubscription(const std::string &subscription, const std::string &ask)
void setName(const std::string &name)
GLOOX_DEPRECATED const std::string & jid() const
const std::string & name() const
const StringList & groups() const
const JID & jidJID() const
const JID EmptyJID
Definition: rosteritem.h:53
RosterItem(const std::string &jid, const std::string &name=EmptyString)
Definition: rosteritem.cpp:24
bool changed() const
Definition: rosteritem.cpp:98
void setGroups(const StringList &groups)
Definition: rosteritem.cpp:84
const std::string & name() const
Definition: rosteritem.cpp:46
SubscriptionType subscription() const
Definition: rosteritem.cpp:76
const Resource * highestResource() const
Definition: rosteritem.cpp:136
void setPriority(const std::string &resource, int priority)
Definition: rosteritem.cpp:128
void setData(const RosterItemData &rid)
Definition: rosteritem.cpp:181
void setSubscription(const std::string &subscription, const std::string &ask)
Definition: rosteritem.cpp:70
virtual ~RosterItem()
Definition: rosteritem.cpp:34
const JID & jidJID() const
Definition: rosteritem.cpp:54
void setName(const std::string &name)
Definition: rosteritem.cpp:40
const Resource * resource(const std::string &res) const
Definition: rosteritem.cpp:175
bool online() const
Definition: rosteritem.cpp:170
const StringList groups() const
Definition: rosteritem.cpp:90
void removeResource(const std::string &resource)
Definition: rosteritem.cpp:160
void setStatus(const std::string &resource, const std::string &msg)
Definition: rosteritem.cpp:120
GLOOX_DEPRECATED const std::string & jid() const
Definition: rosteritem.cpp:62
void setExtensions(const std::string &resource, const StanzaExtensionList &exts)
Definition: rosteritem.cpp:152
void setPresence(const std::string &resource, Presence::PresenceType presence)
Definition: rosteritem.cpp:112
void clearMap(std::map< Key, T * > &M)
Definition: util.h:169
The namespace for the gloox library.
Definition: adhoc.cpp:28
std::list< const StanzaExtension * > StanzaExtensionList
Definition: gloox.h:1268
std::list< std::string > StringList
Definition: gloox.h:1251
const std::string EmptyString
Definition: gloox.cpp:124
SubscriptionType
Definition: gloox.h:1224
@ S10nNone
Definition: gloox.h:1225