1 /*
2  * %kadu copyright begin%
3  * Copyright 2011, 2012, 2013 Bartosz Brachaczek (b.brachaczek@gmail.com)
4  * Copyright 2011, 2013, 2014, 2015 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
5  * %kadu copyright end%
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "gadu-file-transfer-service.h"
22 #include "gadu-imtoken-service.h"
23 
24 #include "file-transfer/gadu-outgoing-file-transfer-handler.h"
25 #include "file-transfer/gadu-url-incoming-file-transfer-handler.h"
26 #include "helpers/gadu-protocol-helper.h"
27 #include "gadu-account-details.h"
28 #include "gadu-protocol.h"
29 
30 #include "contacts/contact-manager.h"
31 #include "core/myself.h"
32 #include "file-transfer/file-transfer-direction.h"
33 #include "file-transfer/file-transfer-manager.h"
34 #include "file-transfer/file-transfer-status.h"
35 #include "file-transfer/file-transfer-storage.h"
36 #include "file-transfer/file-transfer-type.h"
37 #include "file-transfer/gui/file-transfer-can-send-result.h"
38 #include "misc/misc.h"
39 
40 #include <QtCore/QUrl>
41 
42 
GaduFileTransferService(GaduProtocol * protocol)43 GaduFileTransferService::GaduFileTransferService(GaduProtocol *protocol) :
44 		FileTransferService(protocol), Protocol(protocol)
45 {
46 	connect(Protocol, SIGNAL(connected(Account)), this, SIGNAL(canSendChanged()));
47 	connect(Protocol, SIGNAL(disconnected(Account)), this, SIGNAL(canSendChanged()));
48 }
49 
~GaduFileTransferService()50 GaduFileTransferService::~GaduFileTransferService()
51 {
52 }
53 
setMyself(Myself * myself)54 void GaduFileTransferService::setMyself(Myself *myself)
55 {
56 	m_myself = myself;
57 }
58 
setFileTransferStorage(FileTransferStorage * fileTransferStorage)59 void GaduFileTransferService::setFileTransferStorage(FileTransferStorage *fileTransferStorage)
60 {
61 	m_fileTransferStorage = fileTransferStorage;
62 }
63 
setGaduIMTokenService(GaduIMTokenService * imTokenService)64 void GaduFileTransferService::setGaduIMTokenService(GaduIMTokenService *imTokenService)
65 {
66 	m_imTokenService = imTokenService;
67 
68 	connect(m_imTokenService, SIGNAL(imTokenChanged(QByteArray)), this, SIGNAL(canSendChanged()));
69 }
70 
createFileTransferHandler(FileTransfer fileTransfer)71 FileTransferHandler * GaduFileTransferService::createFileTransferHandler(FileTransfer fileTransfer)
72 {
73 	if (fileTransfer.transferDirection() == FileTransferDirection::Incoming)
74 		return new GaduUrlIncomingFileTransferHandler{Protocol, fileTransfer};
75 	else
76 		return new GaduOutgoingFileTransferHandler{Protocol, fileTransfer};
77 }
78 
canSend(Contact contact)79 FileTransferCanSendResult GaduFileTransferService::canSend(Contact contact)
80 {
81 	if (m_myself->buddy() == contact.ownerBuddy())
82 		return {false, {}};
83 
84 	if (!Protocol->isConnected())
85 		return {false, tr("Connect before sending files.")};
86 
87 	if (m_imTokenService->imToken().isEmpty())
88 		return {false, tr("Unable to login to GG Drive. Reconnect before sending files.")};
89 
90 	return {true, {}};
91 }
92 
fileTransferReceived(Contact peer,QString downloadId,QString fileName)93 void GaduFileTransferService::fileTransferReceived(Contact peer, QString downloadId, QString fileName)
94 {
95 	auto transfer = m_fileTransferStorage->create();
96 	transfer.setPeer(peer);
97 	transfer.setTransferDirection(FileTransferDirection::Incoming);
98 	transfer.setTransferType(FileTransferType::Url);
99 	transfer.setTransferStatus(FileTransferStatus::ReadyToDownload);
100 	transfer.setRemoteFileName(QUrl::fromPercentEncoding(fileName.toUtf8()));
101 	transfer.setFileSize(0); // we don't know file size yet
102 	transfer.addProperty("gg:downloadId", downloadId, CustomProperties::Storable);
103 	transfer.addProperty("gg:remoteFileName", fileName, CustomProperties::Storable);
104 
105 	emit incomingFileTransfer(transfer);
106 }
107 
108 #include "moc_gadu-file-transfer-service.cpp"
109