gloox  0.9.9.12
rosteritem.cpp
1 /*
2  Copyright (c) 2004-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 "rosteritem.h"
16 
17 namespace gloox
18 {
19 
20  RosterItem::RosterItem( const JID& jid, const std::string& name )
21  : m_subscription( S10nNone ), m_name( name ), m_jid( jid.bare() ), m_changed( false )
22  {
23  }
24 
26  {
27  ResourceMap::iterator it = m_resources.begin();
28  for( ; it != m_resources.end(); ++it )
29  {
30  delete (*it).second;
31  (*it).second = 0;
32  }
33  }
34 
35  void RosterItem::setName( const std::string& name )
36  {
37  m_name = name;
38  m_changed = true;
39  }
40 
41  void RosterItem::setPresence( const std::string& resource, Presence presence )
42  {
43  if( m_resources.find( resource ) == m_resources.end() )
44  {
45  m_resources[resource] = new Resource( 0, std::string(), presence );
46  }
47  else
48  m_resources[resource]->setStatus( presence );
49  }
50 
51  void RosterItem::setStatus( const std::string& resource, const std::string& msg )
52  {
53  if( m_resources.find( resource ) == m_resources.end() )
54  {
55  m_resources[resource] = new Resource( 0, msg, PresenceUnavailable );
56  }
57  else
58  m_resources[resource]->setMessage( msg );
59  }
60 
61  void RosterItem::setPriority( const std::string& resource, int priority )
62  {
63  if( m_resources.find( resource ) == m_resources.end() )
64  {
65  m_resources[resource] = new Resource( priority, std::string(), PresenceUnavailable );
66  }
67  else
68  m_resources[resource]->setPriority( priority );
69  }
70 
71  void RosterItem::setSubscription( const std::string& subscription, bool ask )
72  {
73  if( subscription == "from" && !ask )
74  m_subscription = S10nFrom;
75  else if( subscription == "from" && ask )
76  m_subscription = S10nFromOut;
77  else if( subscription == "to" && !ask )
78  m_subscription = S10nTo;
79  else if( subscription == "to" && ask )
80  m_subscription = S10nToIn;
81  else if( subscription == "none" && !ask )
82  m_subscription = S10nNone;
83  else if( subscription == "none" && ask )
84  m_subscription = S10nNoneOut;
85  else if( subscription == "both" )
86  m_subscription = S10nBoth;
87  }
88 
89  void RosterItem::setGroups( const StringList& groups )
90  {
91  m_groups = groups;
92  m_changed = true;
93  }
94 
95  void RosterItem::removeResource( const std::string& resource )
96  {
97  ResourceMap::iterator it = m_resources.find( resource );
98  if( it != m_resources.end() )
99  {
100  delete (*it).second;
101  m_resources.erase( it );
102  }
103  }
104 
105  bool RosterItem::online() const
106  {
107  return !m_resources.empty();
108  }
109 
110  const Resource* RosterItem::resource( const std::string& res ) const
111  {
112  ResourceMap::const_iterator it = m_resources.find( res );
113  return it != m_resources.end() ? (*it).second : 0;
114  }
115 
116 }