summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/irc/irctransferhandler.cpp
blob: fd004985db76386c078472d3d510ab4c375dc53b (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
/*
    irctransferhandler.cpp - IRC transfer.

    Copyright (c) 2004      by Michel Hermier <[email protected]>

    Kopete    (c) 2004      by the Kopete developers <[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.                                   *
    *                                                                       *
    *************************************************************************
*/

#include <kdebug.h>

#include <kopetetransfermanager.h>

#include "libkirc/kirctransfer.h"
#include "libkirc/kirctransferhandler.h"

#include "kopetemetacontact.h"
#include "irccontact.h"
#include "irccontactmanager.h"

#include "irctransferhandler.h"

IRCTransferHandler *IRCTransferHandler::self()
{
	static IRCTransferHandler sm_self;
	return &sm_self;
}

KIRC::TransferHandler *IRCTransferHandler::handler()
{
	return KIRC::TransferHandler::self();
}

IRCTransferHandler::IRCTransferHandler()
{
	connect(handler(), TQT_SIGNAL(transferCreated(KIRC::Transfer *)),
		this, TQT_SLOT(transferCreated(KIRC::Transfer *)));

	connect(Kopete::TransferManager::transferManager(), TQT_SIGNAL(accepted(Kopete::Transfer *, const TQString &)),
		this, TQT_SLOT(transferAccepted(Kopete::Transfer *, const TQString&)));
	connect( Kopete::TransferManager::transferManager(), TQT_SIGNAL(refused(const Kopete::FileTransferInfo &)),
		this, TQT_SLOT(transferRefused(const Kopete::FileTransferInfo &)));
}

void IRCTransferHandler::transferCreated(KIRC::Transfer *t)
{
	kdDebug(14120) << k_funcinfo << endl;

	IRCContact *contact = IRCContactManager::existContact(t->engine(), t->nick());
	TQString fileName = t->fileName();
	unsigned long fileSize = t->fileSize();

	if(!contact)
	{
		kdDebug(14120) << k_funcinfo << "Trying to create transfer for a non existing contact(" << t->nick() << ")." << endl;
		return;
	}

	switch(t->type())
	{
//	case KIRC::Transfer::Chat:
	case KIRC::Transfer::FileOutgoing:
		{
			Kopete::Transfer *kt = Kopete::TransferManager::transferManager()->addTransfer(
				contact, fileName, fileSize, contact->metaContact()->displayName(),
				Kopete::FileTransferInfo::Outgoing);
			connectKopeteTransfer(kt, t);
		}
		break;
	case KIRC::Transfer::FileIncoming:
		{
			int ID = Kopete::TransferManager::transferManager()->askIncomingTransfer(
				contact , fileName, fileSize);
			m_idMap.insert(ID, t);
		}
		break;
	default:
		kdDebug(14120) << k_funcinfo << "Unknown transfer type" << endl;
		t->deleteLater();
	}
}

void IRCTransferHandler::transferAccepted(Kopete::Transfer *kt, const TQString &file)
{
	kdDebug(14120) << k_funcinfo << endl;

	KIRC::Transfer *t = getKIRCTransfer(kt->info());
	if(t)
	{
		t->setFileName(file);
		connectKopeteTransfer(kt, t);
	}
}
void IRCTransferHandler::transferRefused(const Kopete::FileTransferInfo &info)
{
	kdDebug(14120) << k_funcinfo << endl;

	KIRC::Transfer *t = getKIRCTransfer(info);
	if(t)
	{
		t->deleteLater();
	}
}

void IRCTransferHandler::connectKopeteTransfer(Kopete::Transfer *kt, KIRC::Transfer *t)
{
	kdDebug(14120) << k_funcinfo << endl;

	if(kt && t)
	{
		switch(t->type())
		{
//		case KIRC::Transfer::Chat:
		case KIRC::Transfer::FileOutgoing:
		case KIRC::Transfer::FileIncoming:
			connect(t , TQT_SIGNAL(fileSizeAcknowledge(unsigned int)),
				kt, TQT_SLOT(slotProcessed(unsigned int)));
			break;
		default:
			kdDebug(14120) << k_funcinfo << "Unknown transfer connections for type" << endl;
			t->deleteLater();
			return;
		}

		connect(t , TQT_SIGNAL(complete()),
			kt, TQT_SLOT(slotComplete()));

//		connect(kt , TQT_SIGNAL(transferCanceled()),
//			t, TQT_SLOT(abort()));
//		connect(kt,  TQT_SIGNAL(destroyed()),
//			t, TQT_SLOT(slotKopeteTransferDestroyed()));

		connect(kt, TQT_SIGNAL(result(TDEIO::Job *)),
			this , TQT_SLOT(tdeioresult(TDEIO::Job *)));

		t->initiate();
	}
}

void IRCTransferHandler::tdeioresult(TDEIO::Job *job)
{
	Kopete::Transfer *kt= (Kopete::Transfer *)job; // FIXME: move to *_cast
	if(!kt)
	{
		kdDebug(14120) << k_funcinfo << "Kopete::Transfer not found from tdeio:" << job << endl;
		return;
	}

	switch(kt->error())
	{
		case 0:	// 0 means no error
			break;
		case TDEIO::ERR_USER_CANCELED:
			kdDebug(14120) << k_funcinfo << "User canceled transfer." << endl;
			// TDEIO::buildErrorString form error don't provide a result string ...
//			if (t->)
//				kt->userAbort(i18n("User canceled transfer."));
//			else
//				kt->userAbort(i18n("User canceled transfer for file:%1").arg(t->fileName()));
			break;
		default:
			kdDebug(14120) << k_funcinfo << "Transfer halted:" << kt->error() << endl;
//			kt->userAbort(TDEIO::buildErrorString(kt->error(), kt->fileName()));
			break;
	}
}

KIRC::Transfer *IRCTransferHandler::getKIRCTransfer(const Kopete::FileTransferInfo &info)
{
	KIRC::Transfer *t = m_idMap[info.transferId()];
	m_idMap.remove(info.transferId());
	return t;
}

#include "irctransferhandler.moc"