summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/groupwise/libgroupwise/tasks/searchchattask.cpp
blob: 4ee355499080900fcd372d0ab4e0af120d78e341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
    Kopete Groupwise Protocol
    searchchattask.cpp - high level search for users on the server - spawns PollSearchResultsTasks

    Copyright (c) 2005      SUSE Linux Products GmbH	 	 http://www.suse.com
    
    Based on Iris, Copyright (C) 2003  Justin Karneges

    Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>
 
    *************************************************************************
    *                                                                       *
    * This library is free software; you can redistribute it and/or         *
    * modify it under the terms of the GNU Lesser General Public            *
    * License as published by the Free Software Foundation; either          *
    * version 2 of the License, or (at your option) any later version.      *
    *                                                                       *
    *************************************************************************
*/

#include <qdatetime.h>
#include <qtimer.h>

#include "client.h"
#include "gwerror.h"
#include "gwfield.h"
#include "response.h"

#include "getchatsearchresultstask.h"

#include "searchchattask.h"


// the delay we allow the server to initially do the search
#define GW_POLL_INITIAL_DELAY 1000
// the maximum number of times to poll the server
#define GW_POLL_MAXIMUM 5
// the frequency between subsequent polls
#define GW_POLL_FREQUENCY_MS 8000

using namespace GroupWise;

SearchChatTask::SearchChatTask(Task* parent): RequestTask(parent), m_polls( 0 )
{
}


SearchChatTask::~SearchChatTask()
{
}

void SearchChatTask::search( SearchType type )
{
	Field::FieldList lst;
	// object Id identifies the search for later reference
	lst.append( new Field::SingleField( NM_A_B_ONLY_MODIFIED, 0, NMFIELD_TYPE_BOOL, ( type == FetchAll ? 0 : 1 ) ) );
	createTransfer( "chatsearch", lst );
}

bool SearchChatTask::take( Transfer * transfer )
{
	if ( !forMe( transfer ) )
		return false;
	Response * response = dynamic_cast<Response *>( transfer );
	if ( !response )
		return false;
	if ( response->resultCode() )
	{
		kdDebug( GROUPWISE_DEBUG_GLOBAL ) << k_funcinfo << "got return code in response << " << response->resultCode() << endl;
		setError( response->resultCode() );
		return true;
	}
	Field::FieldList responseFields = response->fields();
	Field::SingleField * sf = responseFields.findSingleField( NM_A_UD_OBJECT_ID );
	m_objectId = sf->value().toInt();

	// now start the results poll timer
	QTimer::singleShot( GW_POLL_INITIAL_DELAY, this, SLOT( slotPollForResults() ) );
	return true;
}

void SearchChatTask::slotPollForResults()
{
	//create a PollSearchResultsTask
	GetChatSearchResultsTask * gcsrt = new GetChatSearchResultsTask( client()->rootTask() );
	gcsrt->poll( m_objectId );
	connect( gcsrt, SIGNAL( finished() ), SLOT( slotGotPollResults() ) );
	gcsrt->go( true );
}

void SearchChatTask::slotGotPollResults()
{
	GetChatSearchResultsTask * gcsrt = (GetChatSearchResultsTask *)sender();
	kdDebug( GROUPWISE_DEBUG_GLOBAL ) << k_funcinfo << "status code is " << gcsrt->queryStatus() << endl;
	m_polls++;
	switch ( gcsrt->queryStatus() )
	{
		case GetChatSearchResultsTask::GettingData:
			if ( m_polls < GW_POLL_MAXIMUM ) // restart timer
				QTimer::singleShot( GW_POLL_FREQUENCY_MS, this, SLOT( slotPollForResults() ) );
			else
				setSuccess( gcsrt->statusCode() );
			break;
		case GetChatSearchResultsTask::DataRetrieved:
			// got some results, there may be more.
			m_results += gcsrt->results();
            QTimer::singleShot( 0, this, SLOT( slotPollForResults() ) );
			break;
		case GetChatSearchResultsTask::Completed:
			m_results += gcsrt->results();
			setSuccess();
			break;
		case GetChatSearchResultsTask::Cancelled:
			setError(gcsrt->statusCode() );
			break;
		case GetChatSearchResultsTask::Error:
			setError( gcsrt->statusCode() );
			break;
	}
}

QValueList< GroupWise::ChatroomSearchResult > SearchChatTask::results()
{
	return m_results;
}

#include "searchchattask.moc"