summaryrefslogtreecommitdiffstats
path: root/pica-client/dialogs/filetransferdialog.cpp
blob: 4643bb8e47c393bfbc3d479e96eacaab2810f791 (plain) (blame)
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
184
185
186
187
188
189
190
191
192
193
194
195
#include "filetransferdialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <stdlib.h>

FileTransferDialog::FileTransferDialog(QByteArray peer_id, QString filename, quint64 size,
                                        TransferDirection drct, QWidget *parent) :
    peer_id_(peer_id),
    QDialog(parent),
    dir_(drct),
    filename_(filename),
    filesize_(size)
{
    QVBoxLayout *layout = new QVBoxLayout(this);
    QHBoxLayout *btlayout = new QHBoxLayout(this);

    lbFilename = new QLabel(this);
    lbProgressStatus = new QLabel(this);
    lbTransferSpeed = new QLabel(this);
    lbRemainingTime = new QLabel(this);

    pgbar = new QProgressBar(this);
    leftbutton = new QPushButton(this);
    rightbutton = new QPushButton(this);

    layout->addWidget(lbFilename);
    layout->addWidget(lbProgressStatus);
    layout->addWidget(lbTransferSpeed);
    layout->addWidget(lbRemainingTime);
    layout->addWidget(pgbar);
    layout->addLayout(btlayout);

    btlayout->addWidget(leftbutton);
    btlayout->addWidget(rightbutton);

    if (drct == RECEIVING)
    {
        leftbutton->setText(tr("Accept"));
        rightbutton->setText(tr("Deny"));

        setWindowTitle(tr("Receiving file %1").arg(filename_));
    }
    else if (drct == SENDING)
    {
        leftbutton->setText(tr("Pause"));
        rightbutton->setText(tr("Cancel"));

        setWindowTitle(tr("Sending file %1").arg(filename_));
    }

    lbFilename->setText(filename_);

    pgbar->setMinimum(0);
    pgbar->setMaximum(100);

    update(0);

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
    timer->start(1000);
    progress_ = 0;
    prevprogress_ = 0;

    connect(leftbutton, SIGNAL(clicked()), this, SLOT(leftbuttonclick()));
    connect(rightbutton, SIGNAL(clicked()), this, SLOT(rightbuttonclick()));
}

void FileTransferDialog::update(quint64 progress)
{
    progress_ = progress;
    percents = (double)progress / filesize_ * 100.0;

    lbProgressStatus->setText(bytestoHumanBase2(progress) + " / "
                     + bytestoHumanBase2(filesize_) + QString(" %1 %").arg((int)percents));

    pgbar->setValue(percents);

    if (progress == filesize_)
    {
        leftbutton->setEnabled(false);
        rightbutton->setEnabled(false);
    }
}

void FileTransferDialog::leftbuttonclick()
{
    if (dir_ == RECEIVING)
    {
        if (leftbutton->text() == tr("Accept"))
        {
            leftbutton->setText("Pause");
            rightbutton->setText("Cancel");
            emit acceptedFile(peer_id_);
            return;
        }
    }

    if (leftbutton->text() == tr("Pause"))
    {
        leftbutton->setText(tr("Resume"));
        emit pausedFile(peer_id_, this);
    }
    else if (leftbutton->text() == tr("Resume"))
    {
        leftbutton->setText(tr("Pause"));
        emit resumedFile(peer_id_, this);
    }
}

void FileTransferDialog::rightbuttonclick()
{
    if (dir_ == RECEIVING)
    {
        if (rightbutton->text() == tr("Deny"))
        {
            emit deniedFile(peer_id_);
            return;
        }
    }

    if (rightbutton->text() == tr("Cancel"))
    {
        emit cancelledFile(peer_id_, this);
    }
}

QString FileTransferDialog::bytestoHumanBase2(quint64 bytes)
{
    const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
    int i = 0;
    double size = bytes;

    while (size > 1024)
    {
        size /= 1024;
        i++;
    }

    return QString("%1 %2").arg(size, 6, 'f', i ? 2 : 0).arg(units[i]);
}

QString FileTransferDialog::bytestoHumanBase10(quint64 bytes)
{
    const char* units[] = {"B", "kB", "MB", "GB", "TB", "PB"};
    int i = 0;
    double size = bytes;

    while (size > 1000)
    {
        size /= 1000;
        i++;
    }

    return QString("%1 %2").arg(size, 6, 'f', i ? 2 : 0).arg(units[i]);
}

QString FileTransferDialog::timeLeft(quint64 seconds)
{
    if (seconds < 60)
    {
        return QString(tr("%1 seconds")).arg(seconds);
    }

    int minutes = seconds/60;
    int hours = minutes/60;
    double days = (double)hours/24.0;

    if (days > 1.0 )
        return QString(tr("%1 days")).arg(days, 6, 'f', 1);

    if (hours > 0)
        return QString("%1:%2").arg(hours).arg(minutes % 60);

    return QString("%1:%2").arg(minutes).arg(seconds % 60);
}

void FileTransferDialog::timeout()
{
    if (progress_ >= filesize_)
        return;

    quint64 bps = progress_ - prevprogress_;
    prevprogress_ = progress_;

    if (bps != 0)
    {
        quint64 secondsleft = (filesize_ - progress_)/bps;

        lbRemainingTime->setText(timeLeft(secondsleft));
    }
    else
        lbRemainingTime->setText(QString());

    lbTransferSpeed->setText(bytestoHumanBase10(bps) + tr("/s"));
}