diff options
| -rw-r--r-- | pica-client/contactlistwidget.cpp | 21 | ||||
| -rw-r--r-- | pica-client/contactlistwidget.h | 2 | ||||
| -rw-r--r-- | pica-client/dialogs/filetransferdialog.cpp | 187 | ||||
| -rw-r--r-- | pica-client/dialogs/filetransferdialog.h | 63 | ||||
| -rw-r--r-- | pica-client/pica-client.pro | 6 |
5 files changed, 272 insertions, 7 deletions
diff --git a/pica-client/contactlistwidget.cpp b/pica-client/contactlistwidget.cpp index 03ef901..10194e8 100644 --- a/pica-client/contactlistwidget.cpp +++ b/pica-client/contactlistwidget.cpp @@ -2,11 +2,13 @@ #include "globals.h" #include "dialogs/viewcertdialog.h" #include "dialogs/showpicaiddialog.h" +#include "dialogs/filetransferdialog.h" #include "msguirouter.h" #include <QMenu> #include <QMessageBox> #include <QContextMenuEvent> #include <QInputDialog> +#include <QTimer> ContactListWidget::ContactListWidget(QWidget *parent) : QListWidget(parent) @@ -29,6 +31,9 @@ ContactListWidget::ContactListWidget(QWidget *parent) : showidAct = new QAction(tr("Show Pica Pica &ID"), this); connect(showidAct, SIGNAL(triggered()), this, SLOT(show_id())); + sendfileAct = new QAction(tr("Send &File"), this); + connect(sendfileAct, SIGNAL(triggered()), this, SLOT(send_file())); + connect(this, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(start_chat())); setContactsStorage(new Contacts(config_dbname, Accounts::GetCurrentAccount().id)); @@ -50,14 +55,12 @@ void ContactListWidget::contextMenuEvent(QContextMenuEvent *event) menu.addAction(addcontactAct); if (this->itemAt(event->pos())) + { menu.addAction(startchatAct); - - if (this->itemAt(event->pos())) menu.addAction(viewcertAct); - - if (this->itemAt(event->pos())) menu.addAction(showidAct); - + menu.addAction(sendfileAct); + } menu.exec(event->globalPos()); } @@ -202,3 +205,11 @@ void ContactListWidget::Reload() { setContactsStorage(storage); } + +void ContactListWidget::send_file() +{ +//FileTransferDialog *d = new FileTransferDialog("test", 138900713, FileTransferDialog::SENDING, this); + +//d->show(); + +} diff --git a/pica-client/contactlistwidget.h b/pica-client/contactlistwidget.h index 2007d92..c23ac37 100644 --- a/pica-client/contactlistwidget.h +++ b/pica-client/contactlistwidget.h @@ -19,6 +19,7 @@ public: QAction *startchatAct; QAction *viewcertAct; QAction *showidAct; + QAction *sendfileAct; protected: void contextMenuEvent(QContextMenuEvent *event); @@ -41,6 +42,7 @@ private slots: void start_chat(); void view_cert(); void show_id(); + void send_file(); //void debug_onmsginput(QString msg, ChatWindow *sender_window);//debug remove after debug }; 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 diff --git a/pica-client/pica-client.pro b/pica-client/pica-client.pro index 97e7315..cffe5b3 100644 --- a/pica-client/pica-client.pro +++ b/pica-client/pica-client.pro @@ -34,7 +34,8 @@ SOURCES += main.cpp\ history.cpp \ ../PICA_id.c \ dhparam.cpp \ - dialogs/showpicaiddialog.cpp + dialogs/showpicaiddialog.cpp \ + dialogs/filetransferdialog.cpp HEADERS += mainwindow.h \ chatwindow.h \ @@ -62,7 +63,8 @@ HEADERS += mainwindow.h \ history.h \ ../PICA_id.h \ dhparam.h \ - dialogs/showpicaiddialog.h + dialogs/showpicaiddialog.h \ + dialogs/filetransferdialog.h DEFINES = PICA_MULTITHREADED |
