gloox  1.0
lastactivity.cpp
1 /*
2  Copyright (c) 2005-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 "lastactivity.h"
16 #include "disco.h"
17 #include "discohandler.h"
18 #include "clientbase.h"
19 #include "error.h"
20 #include "lastactivityhandler.h"
21 
22 #include <cstdlib>
23 
24 namespace gloox
25 {
26 
27  // ---- LastActivity::Query ----
29  : StanzaExtension( ExtLastActivity ), m_seconds( -1 )
30  {
31  if( !tag || tag->name() != "query" || tag->xmlns() != XMLNS_LAST )
32  return;
33 
34  if( tag->hasAttribute( "seconds" ) )
35  m_seconds = atoi( tag->findAttribute( "seconds" ).c_str() );
36 
37  m_status = tag->cdata();
38  }
39 
40  LastActivity::Query::Query( const std::string& _status, long _seconds )
41  : StanzaExtension( ExtLastActivity ), m_seconds( _seconds ),
42  m_status( _status )
43  {
44  }
45 
47  {
48  }
49 
50  const std::string& LastActivity::Query::filterString() const
51  {
52  static const std::string filter = "/iq/query[@xmlns='" + XMLNS_LAST + "']"
53  "|/presence/query[@xmlns='" + XMLNS_LAST + "']";
54  return filter;
55  }
56 
58  {
59  Tag* t = new Tag( "query" );
60  t->setXmlns( XMLNS_LAST );
61  t->addAttribute( "seconds", m_seconds );
62  t->setCData( m_status );
63  return t;
64  }
65  // ---- ~LastActivity::Query ----
66 
67  // ---- LastActivity ----
69  : m_lastActivityHandler( 0 ), m_parent( parent ),
70  m_active( time ( 0 ) )
71  {
72  if( m_parent )
73  {
74  m_parent->registerStanzaExtension( new Query() );
75  m_parent->registerIqHandler( this, ExtLastActivity );
76  m_parent->disco()->addFeature( XMLNS_LAST );
77  }
78  }
79 
81  {
82  if( m_parent )
83  {
84  m_parent->disco()->removeFeature( XMLNS_LAST );
85  m_parent->removeIqHandler( this, ExtLastActivity );
86  m_parent->removeIDHandler( this );
87  }
88  }
89 
90  void LastActivity::query( const JID& jid )
91  {
92  IQ iq( IQ::Get, jid, m_parent->getID() );
93  iq.addExtension( new Query() );
94  m_parent->send( iq, this, 0 );
95  }
96 
97  bool LastActivity::handleIq( const IQ& iq )
98  {
99  const Query* q = iq.findExtension<Query>( ExtLastActivity );
100  if( !q || iq.subtype() != IQ::Get )
101  return false;
102 
103  IQ re( IQ::Result, iq.from(), iq.id() );
104  re.addExtension( new Query( EmptyString, (long)( time( 0 ) - m_active ) ) );
105  m_parent->send( re );
106 
107  return true;
108  }
109 
110  void LastActivity::handleIqID( const IQ& iq, int /*context*/ )
111  {
112  if( !m_lastActivityHandler )
113  return;
114 
115  if( iq.subtype() == IQ::Result )
116  {
117  const Query* q = iq.findExtension<Query>( ExtLastActivity );
118  if( !q || q->seconds() < 0 )
119  return;
120 
121  m_lastActivityHandler->handleLastActivityResult( iq.from(), q->seconds(), q->status() );
122  }
123  else if( iq.subtype() == IQ::Error && iq.error() )
124  m_lastActivityHandler->handleLastActivityError( iq.from(), iq.error()->error() );
125  }
126 
128  {
129  m_active = time( 0 );
130  }
131 
132 }