00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "flexoff.h"
00015 #include "dataform.h"
00016 #include "disco.h"
00017 #include "error.h"
00018
00019 #include <cstdlib>
00020
00021 namespace gloox
00022 {
00023
00024 FlexibleOffline::FlexibleOffline( ClientBase* parent )
00025 : m_parent( parent ), m_flexibleOfflineHandler( 0 )
00026 {
00027 }
00028
00029 FlexibleOffline::~FlexibleOffline()
00030 {
00031 m_parent->removeIDHandler( this );
00032 }
00033
00034 void FlexibleOffline::checkSupport()
00035 {
00036 m_parent->disco()->getDiscoInfo( m_parent->jid().server(), EmptyString, this, FOCheckSupport );
00037 }
00038
00039 void FlexibleOffline::getMsgCount()
00040 {
00041 m_parent->disco()->getDiscoInfo( m_parent->jid().server(), XMLNS_OFFLINE, this, FORequestNum );
00042 }
00043
00044 void FlexibleOffline::fetchHeaders()
00045 {
00046 m_parent->disco()->getDiscoItems( m_parent->jid().server(), XMLNS_OFFLINE, this, FORequestHeaders );
00047 }
00048
00049 void FlexibleOffline::messageOperation( int context, const StringList& msgs )
00050 {
00051 const std::string& id = m_parent->getID();
00052 IQ::IqType iqType = context == FORequestMsgs ? IQ::Get : IQ::Set;
00053 IQ iq( iqType, JID(), id, XMLNS_OFFLINE, "offline" );
00054 Tag* o = iq.query();
00055
00056 if( msgs.empty() )
00057 new Tag( o, context == FORequestMsgs ? "fetch" : "purge" );
00058 else
00059 {
00060 const std::string action = context == FORequestMsgs ? "view" : "remove";
00061 StringList::const_iterator it = msgs.begin();
00062 for( ; it != msgs.end(); ++it )
00063 {
00064 Tag* i = new Tag( o, "item", "action", action );
00065 i->addAttribute( "node", (*it) );
00066 }
00067 }
00068
00069 m_parent->send( iq, this, context );
00070 }
00071
00072 void FlexibleOffline::registerFlexibleOfflineHandler( FlexibleOfflineHandler* foh )
00073 {
00074 m_flexibleOfflineHandler = foh;
00075 }
00076
00077 void FlexibleOffline::removeFlexibleOfflineHandler()
00078 {
00079 m_flexibleOfflineHandler = 0;
00080 }
00081
00082 void FlexibleOffline::handleDiscoInfo( const JID& , const Disco::Info& info, int context )
00083 {
00084 if( !m_flexibleOfflineHandler )
00085 return;
00086
00087 switch( context )
00088 {
00089 case FOCheckSupport:
00090 m_flexibleOfflineHandler->handleFlexibleOfflineSupport( info.hasFeature( XMLNS_OFFLINE ) );
00091 break;
00092
00093 case FORequestNum:
00094 int num = -1;
00095 if( info.form() && info.form()->hasField( "number_of_messages" ) )
00096 num = atoi( info.form()->field( "number_of_messages" )->value().c_str() );
00097
00098 m_flexibleOfflineHandler->handleFlexibleOfflineMsgNum( num );
00099 break;
00100 }
00101 }
00102
00103 void FlexibleOffline::handleDiscoItems( const JID& , const Disco::Items& items, int context )
00104 {
00105 if( context == FORequestHeaders && m_flexibleOfflineHandler )
00106 {
00107 if( items.node() == XMLNS_OFFLINE )
00108 m_flexibleOfflineHandler->handleFlexibleOfflineMessageHeaders( items.items() );
00109 }
00110 }
00111
00112 void FlexibleOffline::handleDiscoError( const JID& , const Error* , int )
00113 {
00114 }
00115
00116 void FlexibleOffline::handleIqID( const IQ& iq, int context )
00117 {
00118 if( !m_flexibleOfflineHandler )
00119 return;
00120
00121 switch( context )
00122 {
00123 case FORequestMsgs:
00124 switch( iq.subtype() )
00125 {
00126 case IQ::Result:
00127 m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrRequestSuccess );
00128 break;
00129 case IQ::Error:
00130 switch( iq.error()->error() )
00131 {
00132 case StanzaErrorForbidden:
00133 m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrForbidden );
00134 break;
00135 case StanzaErrorItemNotFound:
00136 m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrItemNotFound );
00137 break;
00138 default:
00139 m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrUnknownError );
00140 break;
00141 }
00142 break;
00143 default:
00144 break;
00145 }
00146 break;
00147 case FORemoveMsgs:
00148 switch( iq.subtype() )
00149 {
00150 case IQ::Result:
00151 m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrRemoveSuccess );
00152 break;
00153 case IQ::Error:
00154 switch( iq.error()->error() )
00155 {
00156 case StanzaErrorForbidden:
00157 m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrForbidden );
00158 break;
00159 case StanzaErrorItemNotFound:
00160 m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrItemNotFound );
00161 break;
00162 default:
00163 m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrUnknownError );
00164 break;
00165 }
00166 break;
00167 default:
00168 break;
00169 }
00170 break;
00171 }
00172 }
00173
00174 }