gloox  0.9.9.12
lastactivity.cpp
1 /*
2  Copyright (c) 2005-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 "lastactivity.h"
16 #include "disco.h"
17 #include "discohandler.h"
18 #include "client.h"
19 #include "lastactivityhandler.h"
20 
21 #include <cstdlib>
22 
23 namespace gloox
24 {
25 
27  : m_lastActivityHandler( 0 ), m_parent( parent ),
28  m_active( time ( 0 ) )
29  {
30  if( m_parent )
31  m_parent->disco()->addFeature( XMLNS_LAST );
32  }
33 
35  {
36  if( m_parent )
37  {
38  m_parent->disco()->removeFeature( XMLNS_LAST );
39  m_parent->removeIDHandler( this );
40  }
41  }
42 
43  void LastActivity::query( const JID& jid )
44  {
45  const std::string& id = m_parent->getID();
46 
47  Tag *t = new Tag( "iq" );
48  t->addAttribute( "type", "get" );
49  t->addAttribute( "id", id );
50  t->addAttribute( "to", jid.full() );
51  Tag *q = new Tag( t, "query" );
52  q->addAttribute( "xmlns", XMLNS_LAST );
53 
54  m_parent->trackID( this, id, 0 );
55  m_parent->send( t );
56  }
57 
59  {
60  switch( stanza->subtype() )
61  {
62  case StanzaIqGet:
63  {
64  time_t now = time( 0 );
65 
66  Tag *t = new Tag( "iq" );
67  t->addAttribute( "type", "result" );
68  t->addAttribute( "id", stanza->id() );
69  t->addAttribute( "to", stanza->from().full() );
70  Tag *q = new Tag( t, "query" );
71  q->addAttribute( "seconds", (long)( now - m_active ) );
72  q->addAttribute( "xmlns", XMLNS_LAST );
73 
74  m_parent->send( t );
75  break;
76  }
77 
78  case StanzaIqSet:
79  {
80  Tag *t = new Tag( "iq" );
81  t->addAttribute( "id", stanza->id() );
82  t->addAttribute( "to", stanza->from().full() );
83  t->addAttribute( "type", "error" );
84  Tag *e = new Tag( t, "error" );
85  e->addAttribute( "type", "cancel" );
86  Tag *f = new Tag( e, "feature-not-implemented" );
87  f->addAttribute( "xmlns", XMLNS_XMPP_STANZAS );
88 
89  m_parent->send( t );
90  break;
91  }
92 
93  default:
94  break;
95  }
96 
97  return true;
98  }
99 
100  bool LastActivity::handleIqID( Stanza *stanza, int /*context*/ )
101  {
102  if( !m_lastActivityHandler )
103  return false;
104 
105  switch( stanza->subtype() )
106  {
107  case StanzaIqResult:
108  {
109  Tag *q = stanza->findChild( "query" );
110  if( q )
111  {
112  const std::string& seconds = q->findAttribute( "seconds" );
113  if( !seconds.empty() )
114  {
115  int secs = atoi( seconds.c_str() );
116  m_lastActivityHandler->handleLastActivityResult( stanza->from(), secs );
117  }
118  }
119  break;
120  }
121  case StanzaIqError:
122  m_lastActivityHandler->handleLastActivityError( stanza->from(), stanza->error() );
123  break;
124  default:
125  break;
126  }
127 
128  return false;
129  }
130 
132  {
133  m_active = time( 0 );
134  }
135 
136 }