File: directorydownloadoperation.cpp

package info (click to toggle)
qflipper 1.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,320 kB
  • sloc: cpp: 18,500; sh: 247; ansic: 191; xml: 38; python: 14; makefile: 5
file content (128 lines) | stat: -rw-r--r-- 4,287 bytes parent folder | download
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
#include "directorydownloadoperation.h"

#include <QUrl>
#include <QFile>
#include <QDebug>

#include "flipperzero/devicestate.h"
#include "flipperzero/protobufsession.h"
#include "flipperzero/rpc/storagereadoperation.h"

#include "getfiletreeoperation.h"

using namespace Flipper;

using namespace Zero;

DirectoryDownloadOperation::DirectoryDownloadOperation(ProtobufSession *rpc, DeviceState *deviceState,
                                                       const QString &targetPath, const QByteArray &remotePath, QObject *parent):
    AbstractUtilityOperation(rpc, deviceState, parent),
    m_targetDir(targetPath),
    m_remotePath(remotePath),
    m_totalSize(0)
{}

const QString DirectoryDownloadOperation::description() const
{
    return QStringLiteral("Download Directory %1").arg(QString(m_remotePath));
}

void DirectoryDownloadOperation::nextStateLogic()
{
    if(operationState() == BasicOperationState::Ready) {
        setOperationState(State::CreatingDirectory);
        createLocalDirectory();

    } else if(operationState() == State::CreatingDirectory) {
        setOperationState(State::GettingFileTree);
        getFileTree();

    } else if(operationState() == State::GettingFileTree) {
        setOperationState(State::ReadingFiles);
        readFiles();

    } else if(operationState() == State::ReadingFiles) {
        finish();
    }
}

void DirectoryDownloadOperation::createLocalDirectory()
{
    const auto subdir = QUrl(m_remotePath).fileName();

    if(m_targetDir.exists(subdir)) {
        const auto success = m_targetDir.cd(subdir) && m_targetDir.removeRecursively() && m_targetDir.cdUp();
        if(!success) {
            finishWithError(BackendError::DiskError, QStringLiteral("Target directory exists, but cannot be removed"));
            return;
        }
    }

    if(!(m_targetDir.mkdir(subdir) && m_targetDir.cd(subdir))) {
        finishWithError(BackendError::DiskError, QStringLiteral("Failed to create directory"));
    } else {
        advanceOperationState();
    }
}

void DirectoryDownloadOperation::getFileTree()
{
    auto *operation = new GetFileTreeOperation(rpc(), deviceState(), m_remotePath, this);

    connect(operation, &AbstractOperation::finished, this, [=]() {
        if(operation->isError()) {
            finishWithError(BackendError::BackupError, operation->errorString());
        } else {
            m_fileList = operation->files();
            m_totalSize = std::accumulate(m_fileList.cbegin(), m_fileList.cend(), 0, [](qint64 total, const FileInfo &arg) {
                return arg.type == FileType::RegularFile ? total + arg.size : total;
            });

            advanceOperationState();
        }

        operation->deleteLater();
    });

    operation->start();
}

void DirectoryDownloadOperation::readFiles()
{
    auto filesRemaining = std::count_if(m_fileList.cbegin(), m_fileList.cend(), [](const FileInfo &arg) {
        return arg.type == FileType::RegularFile;
    });

    auto baseProgress = 0.0;

    for(const auto &fileInfo: qAsConst(m_fileList)) {
        const auto filePath = fileInfo.absolutePath.mid(m_remotePath.size() + 1);

        if(fileInfo.type == FileType::Directory) {
            if(!m_targetDir.mkdir(filePath)) {
                finishWithError(BackendError::DiskError, QStringLiteral("Failed to create directory: %1").arg(QString(filePath)));
                return;
            }

        } else if(fileInfo.type == FileType::RegularFile) {
            auto *file = new QFile(m_targetDir.absoluteFilePath(filePath), this);
            auto *operation = rpc()->storageRead(fileInfo.absolutePath, file);

            const auto isLast = (--filesRemaining == 0);

            connect(operation, &AbstractOperation::progressChanged, this, [=]() {
                setProgress(baseProgress + operation->progress() * fileInfo.size / m_totalSize);
            });

            connect(operation, &AbstractOperation::finished, this, [=]() {
                if(operation->isError()) {
                    finishWithError(BackendError::BackupError, operation->errorString());
                } else if(isLast) {
                    advanceOperationState();
                }
            });

            baseProgress += fileInfo.size * 100.0 / m_totalSize;
        }
    }
}