summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/kopetetask.cpp
blob: b748411693e08bc8ce26671ae6ef05482bebe1c0 (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
/*
    kopetetask.cpp - Kopete Task

    Copyright (c) 2004      by Richard Smith         <kde@metafoo.co.uk>
    Kopete    (c) 2002-2004 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 "kopetetask.h"

#include <klocale.h>

#include <qptrlist.h>

namespace Kopete
{

class Task::Private
{
public:
	Private()
	 : result( ResultFailed )
	{
		errorMessage = i18n( "The operation has not finished yet" );
	}

	Task::Result result;
	QString errorMessage;
	QPtrList<Task> subtasks;
};

Task::Task()
 : d( new Private )
{
}

Task::~Task()
{
	delete d;
}

bool Task::succeeded() const
{
	return d->result == ResultSucceeded;
}

const QString &Task::errorString() const
{
	return d->errorMessage;
}

void Task::abort( int flags )
{
	int childFlags = flags & ~AbortEmitResult;
	for ( Task *task = d->subtasks.first(); task; task = d->subtasks.next() )
		task->abort( childFlags );

	if ( flags & AbortEmitResult )
		emitResult( ResultFailed, i18n( "Aborted" ) );
	else
		delete this;
}

void Task::addSubtask( Task *task )
{
	d->subtasks.append( task );
	connect( task, SIGNAL( result( Kopete::Task* ) ),
	         this, SLOT( slotResult( Kopete::Task* ) ) );
	connect( task, SIGNAL( statusMessage( Kopete::Task*, const QString & ) ),
	         this, SIGNAL( statusMessage( Kopete::Task*, const QString & ) ) );
}

void Task::removeSubtask( Task *task, RemoveSubtaskIfLast actionIfLast )
{
	disconnect( task, SIGNAL( result( Kopete::Task* ) ),
	            this, SLOT( slotResult( Kopete::Task* ) ) );
	disconnect( task, SIGNAL( statusMessage( Kopete::Task*, const QString & ) ),
	            this, SIGNAL( statusMessage( Kopete::Task*, const QString & ) ) );
	d->subtasks.remove( task );
	if ( d->subtasks.isEmpty() && actionIfLast == IfLastEmitResult )
		emitResult( task->succeeded() ? ResultSucceeded : ResultFailed, task->errorString() );
}

void Task::emitResult( Result res, const QString &errorMessage )
{
	d->result = res;
	d->errorMessage = errorMessage;
	emit result( this );
	delete this;
}

void Task::slotResult( Kopete::Task *task )
{
	removeSubtask( task );
}

}

#include "kopetetask.moc"
// vim: set noet ts=4 sts=4 sw=4: