summaryrefslogtreecommitdiffstats
path: root/korn/tdeio_count.cpp
blob: 8ad07dcfc028eebed50f3a3cc02810016e4e5afa (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright (C) 2004, Mart Kelder ([email protected])
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "tdeio_count.h"

#include "kio.h"
#include "tdeio_proto.h"
#include "tdeio_single_subject.h"
#include "mailsubject.h"
#include "sortedmailsubject.h"

#include <kdebug.h>
#include <tdeio/jobclasses.h>
#include <tdeio/scheduler.h>
#include <tdeio/slave.h>
#include <tdeio/global.h>
#include <kurl.h>
#include <tqvaluelist.h>

#include <tqstring.h>

KIO_Count::KIO_Count( TQObject * parent, const char * name )
	: TQObject ( parent, name ),
	_kurl( 0 ),
	_metadata( 0 ),
	_protocol( 0 ),
	_valid( true ),
	_new_mailurls( 0 ),
	_subjects_pending( 0 ),
	_total_new_messages( 0 ),
	_popup_subjects( 0 )
{
}

KIO_Count::~KIO_Count()
{
	// Delete copies of urls.
	delete _kurl;
	delete _metadata;
}

void KIO_Count::count( KKioDrop *drop )
{
	if( _new_mailurls )
		return; //A counting is pending, so no new one is started.

	delete _kurl;
	delete _metadata;
	_kio = drop;

	/*
	 * Saving current settings: all actions are asynchroon, so if someone
	 * use slow servers, settings could been changed before this class is
	 * finished with counten. To be able to track back te staring values;
	 * these are saved in the class.
	 */
	_kurl = new KURL( *_kio->_kurl );
	_metadata = new TDEIO::MetaData( *_kio->_metadata );
	_protocol = _kio->_protocol;

	KURL kurl = *_kurl;
	TDEIO::MetaData metadata = *_metadata;

	// Serup a connection
	if( _protocol->connectionBased( ) )
	{
		_protocol->recheckConnectKURL( kurl, metadata );

		if( kurl.port() == 0 )
			kurl.setPort( _protocol->defaultPort( _kio->_ssl ) );

		if( ! ( _slave = TDEIO::Scheduler::getConnectedSlave( kurl, metadata ) ) ) //Forcing reload
		{
			kdWarning() << i18n( "Not able to open a kio slave for %1." ).arg( _protocol->configName() ) << endl;
			_kio->emitShowPassivePopup( i18n( "Not able to open a kio slave for %1." ).arg( _protocol->configName() ) );
			_valid = false;
			_kio->emitValidChanged();
			_slave = 0;
			//delete _new_mailurls; _new_mailurls = 0; //No connection pending
			return;
		}

		connect( _slave, TQT_SIGNAL( error( int, const TQString& ) ), _kio, TQT_SLOT( slotConnectionError( int, const TQString& ) ) );
		connect( _slave, TQT_SIGNAL( warning( const TQString& ) ), _kio, TQT_SLOT( slotConnectionWarning( const TQString& ) ) );
		connect( _slave, TQT_SIGNAL( infoMessage( const TQString& ) ), _kio, TQT_SLOT( slotConnectionInfoMessage( const TQString& ) ) );

		/*
		 * _protocol->recheckConnectKURL could have change kurl and metadata in order to have the right
		 * settings to connect. But some other functions assumed unmodified settings,
		 * so the settings are set back to his originals.
		 */
		kurl = *_kurl;
		metadata = *_metadata;
	}
	else
	{
		_slave = 0; //Prevent disconnecting not-existing slave
	}

	/* Blocking this function: no new counts can be started from now */
	_new_mailurls = new TQValueList< KKioDrop::FileInfo >;

	_protocol->recheckKURL( kurl, metadata );

	if( kurl.port() == 0 )
		kurl.setPort( _protocol->defaultPort( _kio->_ssl ) );

	//Making job to fetch file-list
	
	_job = TDEIO::listDir( kurl, false );
	_job->addMetaData( metadata );

	connect( _job, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( result( TDEIO::Job* ) ) );
	connect( _job, TQT_SIGNAL( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ),
	         this, TQT_SLOT( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ) );

	if( _protocol->connectionBased() )
		TDEIO::Scheduler::assignJobToSlave( _slave, _job );
	else
		TDEIO::Scheduler::scheduleJob( _job );
}

void KIO_Count::stopActiveCount()
{
	if( !_new_mailurls )
		return;

	disconnect( _job, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( result( TDEIO::Job* ) ) );
	disconnect( _job, TQT_SIGNAL( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ),
	            this, TQT_SLOT( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ) );

	TDEIO::Scheduler::cancelJob( _job );

	if( _slave )
	{
		//Slave seems to be disconnected by canceling the last job of the slave
		//TDEIO::Scheduler::disconnectSlave( _slave );
		_slave = 0;
	}

	//Deletings settings
	delete _kurl; _kurl = 0;
	delete _metadata; _metadata = 0;

	delete _new_mailurls; _new_mailurls = 0;
}

void KIO_Count::showPassive( const TQString& id )
{
	KURL kurl = *_kio->_kurl;
	TDEIO::MetaData metadata = *_kio->_metadata;
	kurl = id;
	//TDEIO::Slave *slave = 0;

	_kio->_protocol->readSubjectKURL( kurl, metadata );
	if( kurl.port() == 0 )
		kurl.setPort( _kio->_protocol->defaultPort( _kio->_ssl ) );

	KIO_Single_Subject *subject = new KIO_Single_Subject( this, id.latin1(), kurl, metadata, _kio->_protocol, _slave, id, 0 );

	_subjects_pending++;

	connect( subject, TQT_SIGNAL( readSubject( KornMailSubject* ) ), this, TQT_SLOT( addtoPassivePopup( KornMailSubject* ) ) );
        connect( subject, TQT_SIGNAL( finished( KIO_Single_Subject* ) ), this, TQT_SLOT( deleteSingleSubject( KIO_Single_Subject* ) ) );
}

void KIO_Count::disconnectSlave()
{
	if( _subjects_pending > 0 )
		return; //Still getting data

	if( !_protocol->connectionBased() )
		return; //Protocol doesn't have a connection

	if( !_slave )
		return; //Slave doens't exist

	//Disconnect slave
	TDEIO::Scheduler::disconnectSlave( _slave );
	_slave = 0;
	_protocol = 0;
}

//This function is called when fetching is over
void KIO_Count::result( TDEIO::Job* job )
{
	//job should be the latest job; elsewise: print an error.
	if( job != _job )
		kdError() << i18n( "Got unknown job; something must be wrong..." ) << endl;

	//look of an error occurred. If there is, print the error.
	//This could be very useful by resolving bugs.
	if( job->error() )
	{
		kdError() << i18n( "The next KIO-error occurred by counting: %1" ).arg( job->errorString() ) << endl;
		_kio->emitShowPassivePopup( i18n( "The next KIO-error occurred by counting: %1" ).arg( job->errorString() ) );
		_valid = false;
		_kio->emitValidChanged();
	}

	disconnect( job, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( result( TDEIO::Job* ) ) );
	disconnect( job, TQT_SIGNAL( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ),
	            this, TQT_SLOT( entries( TDEIO::Job*, const TDEIO::UDSEntryList& ) ) );

	disconnectSlave();

	//Deletings settings
	delete _kurl; _kurl = 0;
	delete _metadata; _metadata = 0;

	if( _kio->_mailurls->count() != _new_mailurls->count() )
	{
		*_kio->_mailurls = *_new_mailurls;
		_kio->emitChanged(_kio->_mailurls->count());
	}
	else
	{
		*_kio->_mailurls = *_new_mailurls;
	}
	delete _new_mailurls; _new_mailurls = 0;

	_valid = true;
	_kio->emitValidChanged();
	_kio->emitRechecked();
}

//An file list is ready; now save it in _kio->_mailurls.
void KIO_Count::entries( TDEIO::Job* job, const TDEIO::UDSEntryList &list )
{
	TQStringList old_list;
	TDEIO::UDSEntryListConstIterator  it1 ;
	TDEIO::UDSEntry::ConstIterator it2 ;
	TDEIO::MetaData metadata;
	KURL kurl;
	bool isFile;

	//job should be the latest job
	if( job != _job )
		kdError() << i18n( "Got unknown job; something must be wrong..." ) << endl;

	for( TQValueListConstIterator<KKioDrop::FileInfo> it = _kio->_mailurls->begin(); it != _kio->_mailurls->end(); ++it )
		old_list.append( (*it).name );

	for ( it1 = list.begin() ; it1 != list.end() ; it1++ )
	{
		/*
		 * The list contains multiple objects. Each object could be a file.
		 * Settings about it are saved in this scope until it is added to the list.
		 */
		isFile=false;
		KKioDrop::FileInfo fileinfo;
		fileinfo.name = TQString();
		fileinfo.size = 0;

		for ( it2 = (*it1).begin() ; it2 != (*it1).end() ; it2++ )
		{
			if( (*it2).m_uds == TDEIO::UDS_FILE_TYPE &&
			    ((long)(*it2).m_long & S_IFREG ) )
				isFile=true;
			else if( (*it2).m_uds == TDEIO::UDS_URL )
				fileinfo.name = (*it2).m_str;
			else if( (*it2).m_uds == TDEIO::UDS_NAME )
			{ //The file tdeioslave doesn't return UDS_URL.
				kurl = *_kurl;
				metadata = *_metadata;
				_protocol->recheckKURL( kurl, metadata );
				kurl.setPath ( kurl.path() + '/' + (*it2).m_str );
				fileinfo.name = kurl.url();
			}
			else if( (*it2).m_uds == TDEIO::UDS_SIZE )
			{
				fileinfo.size = (*it2).m_long;
			}
		}

		//Add the entry.
		if( ! fileinfo.name.isNull() && isFile )
		{
			_new_mailurls->append( fileinfo );
			if( ! old_list.contains( fileinfo.name ) && _kio->passivePopup() )
				showPassive( fileinfo.name );
		}
	}
}

void KIO_Count::addtoPassivePopup( KornMailSubject* subject )
{
	if( ! _popup_subjects )
	{
		_popup_subjects = new SortedMailSubject;
		_popup_subjects->setAutoDelete( true );
	}

	_popup_subjects->inSort( subject );
	if( _popup_subjects->count() > 5 )
		_popup_subjects->removeFirst(); //Overhead: subject is downloaded

	_subjects_pending--;
	_total_new_messages++;
	if( _subjects_pending == 0 )
	{
		_kio->emitShowPassivePopup( dynamic_cast< TQPtrList<KornMailSubject>* >( _popup_subjects ), _total_new_messages );
		delete _popup_subjects; _popup_subjects = 0;
		_total_new_messages = 0;

		disconnectSlave();
	}
}

void KIO_Count::deleteSingleSubject( KIO_Single_Subject* single_subject )
{
	delete single_subject;
}

#include "tdeio_count.moc"