summaryrefslogtreecommitdiffstats
path: root/pica-client/dialogs/filetransferdialog.cpp
diff options
context:
space:
mode:
authorme_desk <me_desk@fb96df88-216d-11de-830c-f3023f12d8ac>2014-01-02 10:12:35 +0000
committerme_desk <me_desk@fb96df88-216d-11de-830c-f3023f12d8ac>2014-01-02 10:12:35 +0000
commit72076cbe42730602b02afed5032d1be41d5eb36c (patch)
tree8cd6c43775c15b8f3fc493220ba97d3502280cfb /pica-client/dialogs/filetransferdialog.cpp
parentde121d0f66a66295f43844443a619db8b2baad3c (diff)
downloadpica-pica-72076cbe42730602b02afed5032d1be41d5eb36c.tar.gz
pica-pica-72076cbe42730602b02afed5032d1be41d5eb36c.zip
implemented FileTransferDialog and added "Send File" menu item in contactlistwidget
git-svn-id: svn://localhost/project_picapica/trunk@304 fb96df88-216d-11de-830c-f3023f12d8ac
Diffstat (limited to 'pica-client/dialogs/filetransferdialog.cpp')
-rw-r--r--pica-client/dialogs/filetransferdialog.cpp187
1 files changed, 187 insertions, 0 deletions
diff --git a/pica-client/dialogs/filetransferdialog.cpp b/pica-client/dialogs/filetransferdialog.cpp
new file mode 100644
index 0000000..c7f145d
--- /dev/null
+++ b/pica-client/dialogs/filetransferdialog.cpp
@@ -0,0 +1,187 @@
+#include "filetransferdialog.h"
+#include <QVBoxLayout>
+#include <QHBoxLayout>
+#include <stdlib.h>
+
+FileTransferDialog::FileTransferDialog(QString filename, quint64 size, TransferDirection drct, QWidget *parent) :
+ 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);
+}
+
+void FileTransferDialog::leftbuttonclick()
+{
+ if (dir_ == RECEIVING)
+ {
+ if (leftbutton->text() == tr("Accept"))
+ {
+ leftbutton->setText("Pause");
+ rightbutton->setText("Cancel");
+ emit acceptedFile();
+ return;
+ }
+ }
+
+ if (leftbutton->text() == tr("Pause"))
+ {
+ leftbutton->setText(tr("Resume"));
+ emit pausedFile();
+ }
+ else if (leftbutton->text() == tr("Resume"))
+ {
+ leftbutton->setText(tr("Pause"));
+ emit resumedFile();
+ }
+}
+
+void FileTransferDialog::rightbuttonclick()
+{
+ if (dir_ == RECEIVING)
+ {
+ if (rightbutton->text() == tr("Deny"))
+ {
+ emit deniedFile();
+ return;
+ }
+ }
+
+ if (rightbutton->text() == tr("Cancel"))
+ {
+ emit cancelledFile();
+ }
+}
+
+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"));
+}