1 /*
2  * This file is part of TelepathyQt
3  *
4  * Copyright (C) 2011 Collabora Ltd. <http://www.collabora.co.uk/>
5  * Copyright (C) 2011 Nokia Corporation
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library 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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "pending-file-receive.h"
23 #include "_gen/pending-file-receive.moc.hpp"
24 
25 #include <TelepathyQt/Account>
26 #include <TelepathyQt/Channel>
27 #include <TelepathyQt/IncomingFileTransferChannel>
28 
29 #include <QDebug>
30 #include <QFile>
31 
PendingFileReceive(const IncomingFileTransferChannelPtr & chan,const SharedPtr<RefCounted> & object)32 PendingFileReceive::PendingFileReceive(const IncomingFileTransferChannelPtr &chan,
33         const SharedPtr<RefCounted> &object)
34     : PendingFileTransfer(FileTransferChannelPtr::qObjectCast(chan), object),
35       mReceivingFile(false)
36 {
37     // connect/call onTransferStateChanged here as now we are constructed, otherwise doing it in the base
38     // class would only invoke the base class slot
39     connect(chan.data(),
40             SIGNAL(stateChanged(Tp::FileTransferState,Tp::FileTransferStateChangeReason)),
41             SLOT(onTransferStateChanged(Tp::FileTransferState,Tp::FileTransferStateChangeReason)));
42     onTransferStateChanged(chan->state(), chan->stateReason());
43 }
44 
~PendingFileReceive()45 PendingFileReceive::~PendingFileReceive()
46 {
47     mFile.close();
48 }
49 
onTransferStateChanged(FileTransferState state,FileTransferStateChangeReason stateReason)50 void PendingFileReceive::onTransferStateChanged(FileTransferState state,
51         FileTransferStateChangeReason stateReason)
52 {
53     PendingFileTransfer::onTransferStateChanged(state, stateReason);
54 
55     if (state == FileTransferStatePending) {
56         Q_ASSERT(!mReceivingFile);
57         mReceivingFile = true;
58 
59         IncomingFileTransferChannelPtr chan =
60             IncomingFileTransferChannelPtr::qObjectCast(channel());
61         Q_ASSERT(chan);
62 
63         QString fileName(QLatin1String("TpQtExampleFTReceiver_") + chan->fileName());
64         fileName.replace(QLatin1String("/"), QLatin1String("_"));
65         mFile.setFileName(fileName);
66 
67         qDebug() << "Receiving" << chan->fileName() << "from" <<
68             chan->targetId() << ", saving as" << fileName;
69         chan->acceptFile(0, &mFile);
70     }
71 }
72