summaryrefslogtreecommitdiffstats
path: root/pica-client/dialogs
diff options
context:
space:
mode:
Diffstat (limited to 'pica-client/dialogs')
-rw-r--r--pica-client/dialogs/filetransferdialog.cpp187
-rw-r--r--pica-client/dialogs/filetransferdialog.h63
2 files changed, 250 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"));
+}
diff --git a/pica-client/dialogs/filetransferdialog.h b/pica-client/dialogs/filetransferdialog.h
new file mode 100644
index 0000000..a5a4783
--- /dev/null
+++ b/pica-client/dialogs/filetransferdialog.h
@@ -0,0 +1,63 @@
+#ifndef FILETRANSFERDIALOG_H
+#define FILETRANSFERDIALOG_H
+
+#include <QDialog>
+#include <QProgressBar>
+#include <QLabel>
+#include <QPushButton>
+#include <QTimer>
+
+class FileTransferDialog : public QDialog
+{
+ Q_OBJECT
+public:
+ enum TransferDirection
+ {
+ SENDING,
+ RECEIVING
+ };
+
+ explicit FileTransferDialog(QString filename, quint64 size, TransferDirection drct, QWidget *parent = 0);
+
+
+
+private:
+ QLabel *lbFilename;
+ QLabel *lbProgressStatus;
+ QLabel *lbTransferSpeed;
+ QLabel *lbRemainingTime;
+ QProgressBar *pgbar;
+ QPushButton *leftbutton;
+ QPushButton *rightbutton;
+
+ TransferDirection dir_;
+ QString filename_;
+ quint64 filesize_;
+
+ double percents;
+ quint64 progress_;
+ quint64 prevprogress_;
+
+ static QString bytestoHumanBase2(quint64 bytes);
+ static QString bytestoHumanBase10(quint64 bytes);
+ static QString timeLeft(quint64 seconds);
+
+ QTimer *timer;
+signals:
+ void pausedFile();
+ void resumedFile();
+ void cancelledFile();
+ void acceptedFile();
+ void deniedFile();
+
+public slots:
+ void update(quint64 progress);
+
+private slots:
+ void timeout();
+ void leftbuttonclick();
+ void rightbuttonclick();
+
+};
+
+#endif // FILETRANSFERDIALOG_H