summaryrefslogtreecommitdiffstats
blob: af2831cf6b12267490263f11c6f1cc98d59bd399 (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
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2015-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/Controllers/FileTransfer/FileTransferOverview.h>

#include <boost/bind.hpp>
#include <boost/filesystem.hpp>
#include <boost/signals2.hpp>

#include <Swiften/Base/Log.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/FileTransfer/FileTransferManager.h>

namespace Swift {

FileTransferOverview::FileTransferOverview(FileTransferManager* ftm) : fileTransferManager(ftm) {
    fileTransferManager->onIncomingFileTransfer.connect(boost::bind(&FileTransferOverview::handleIncomingFileTransfer, this, _1));
    onNewFileTransferController.connect(boost::bind(&FileTransferOverview::handleNewFileTransferController, this, _1));
}

FileTransferOverview::~FileTransferOverview() {
    onNewFileTransferController.disconnect(boost::bind(&FileTransferOverview::handleNewFileTransferController, this, _1));
    fileTransferManager->onIncomingFileTransfer.disconnect(boost::bind(&FileTransferOverview::handleIncomingFileTransfer, this, _1));
    foreach(FileTransferController* controller, fileTransfers) {
        controller->onStateChanged.disconnect(boost::bind(&FileTransferOverview::handleFileTransferStateChanged, this));
    }
}

void FileTransferOverview::sendFile(const JID& jid, const std::string& filename) {
    if (boost::filesystem::exists(filename) && boost::filesystem::file_size(filename) > 0) {
        FileTransferController* controller = new FileTransferController(jid, filename, fileTransferManager);
        onNewFileTransferController(controller);
    }
}

void FileTransferOverview::handleIncomingFileTransfer(IncomingFileTransfer::ref transfer) {
    FileTransferController* controller = new FileTransferController(transfer);
    onNewFileTransferController(controller);
}

void FileTransferOverview::handleNewFileTransferController(FileTransferController* controller) {
    fileTransfers.push_back(controller);
    controller->onStateChanged.connect(boost::bind(&FileTransferOverview::handleFileTransferStateChanged, this));
}

void FileTransferOverview::handleFileTransferStateChanged() {
    onFileTransferListChanged();
}

const std::vector<FileTransferController*>& FileTransferOverview::getFileTransfers() const {
    return fileTransfers;
}

void FileTransferOverview::clearFinished() {
    for (std::vector<FileTransferController*>::iterator it = fileTransfers.begin(); it != fileTransfers.end(); ) {
        if((*it)->getState().type == FileTransfer::State::Finished
            || (*it)->getState().type == FileTransfer::State::Failed
            || (*it)->getState().type == FileTransfer::State::Canceled) {
            FileTransferController* controller = *it;
            it = fileTransfers.erase(it);
            controller->onStateChanged.disconnect(boost::bind(&FileTransferOverview::handleFileTransferStateChanged, this));
            delete controller;
        } else {
            ++it;
        }
    }
    onFileTransferListChanged();
}

bool FileTransferOverview::isClearable() const {
    bool isClearable = false;
    foreach (FileTransferController* controller, fileTransfers) {
        if(controller->getState().type == FileTransfer::State::Finished
            || controller->getState().type == FileTransfer::State::Failed
            || controller->getState().type == FileTransfer::State::Canceled) {
            isClearable = true;
            break;
        }
    }
    return isClearable;
}

}