From 85568337e3cebc6ce867bceba43b62671fcab059 Mon Sep 17 00:00:00 2001 From: krepa098 Date: Sun, 11 Jan 2015 11:58:50 +0100 Subject: [PATCH] cleanup --- qtox.pro | 2 - src/chatlog/chatlog.cpp | 1 - src/filetransferinstance.cpp | 507 ----------------------------------- src/filetransferinstance.h | 87 ------ src/widget/form/chatform.cpp | 1 - 5 files changed, 598 deletions(-) delete mode 100644 src/filetransferinstance.cpp delete mode 100644 src/filetransferinstance.h diff --git a/qtox.pro b/qtox.pro index d8c76f360..69a37c204 100644 --- a/qtox.pro +++ b/qtox.pro @@ -157,7 +157,6 @@ HEADERS += src/widget/form/addfriendform.h \ src/widget/friendlistwidget.h \ src/widget/genericchatroomwidget.h \ src/widget/form/genericchatform.h \ - src/filetransferinstance.h \ src/corestructs.h \ src/coredefines.h \ src/coreav.h \ @@ -229,7 +228,6 @@ SOURCES += \ src/coreav.cpp \ src/widget/genericchatroomwidget.cpp \ src/widget/form/genericchatform.cpp \ - src/filetransferinstance.cpp \ src/corestructs.cpp \ src/widget/maskablepixmapwidget.cpp \ src/video/cameraworker.cpp \ diff --git a/src/chatlog/chatlog.cpp b/src/chatlog/chatlog.cpp index 0c1d7e731..15f62a806 100644 --- a/src/chatlog/chatlog.cpp +++ b/src/chatlog/chatlog.cpp @@ -645,7 +645,6 @@ void ChatLog::resizeEvent(QResizeEvent* ev) updateMultiSelectionRect(); - qDebug() << "starting worker"; lastWorkerIndex = 0; workerDy = 0.0; workerTimer->start(); diff --git a/src/filetransferinstance.cpp b/src/filetransferinstance.cpp deleted file mode 100644 index bf690cdd1..000000000 --- a/src/filetransferinstance.cpp +++ /dev/null @@ -1,507 +0,0 @@ -/* - Copyright (C) 2014 by Project Tox - - This file is part of qTox, a Qt-based graphical interface for Tox. - - This program is libre software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - - See the COPYING file for more details. -*/ - -#include "filetransferinstance.h" -#include "core.h" -#include "misc/settings.h" -#include "misc/style.h" -#include "src/friendlist.h" -#include "src/friend.h" -#include -#include -#include -#include -#include -#include - -#define MAX_CONTENT_WIDTH 250 -#define MAX_PREVIEW_SIZE 25*1024*1024 - -uint FileTransferInstance::Idconter = 0; - -FileTransferInstance::FileTransferInstance(ToxFile File) - : lastBytesSent{0}, - fileNum{File.fileNum}, friendId{File.friendId}, direction{File.direction} -{ - id = Idconter++; - state = tsPending; - remotePaused = false; - lastUpdateTime = QDateTime::currentDateTime(); - - filename = File.fileName; - - // update this whenever you change the font in innerStyle.css - QFontMetrics fm(Style::getFont(Style::Small)); - - filenameElided = fm.elidedText(filename, Qt::ElideRight, MAX_CONTENT_WIDTH); - size = getHumanReadableSize(File.filesize); - contentPrefWidth = std::max(fm.width(filenameElided), fm.width(size)) + fm.leading(); - - speed = "0B/s"; - eta = "00:00"; - - if (File.direction == ToxFile::SENDING) - { - if (File.file->size() <= MAX_PREVIEW_SIZE) - { - QImage preview; - File.file->seek(0); - if (preview.loadFromData(File.file->readAll())) - { - pic = preview.scaled(100, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); - } - } - File.file->seek(0); - } -} - -QString FileTransferInstance::getHumanReadableSize(unsigned long long size) -{ - static const char* suffix[] = {"B","kiB","MiB","GiB","TiB"}; - int exp = 0; - if (size) - exp = std::min( (int) (log(size) / log(1024)), (int) (sizeof(suffix) / sizeof(suffix[0]) - 1)); - return QString().setNum(size / pow(1024, exp),'f',2).append(suffix[exp]); -} - -void FileTransferInstance::onFileTransferInfo(int FriendId, int FileNum, int64_t Filesize, int64_t BytesSent, ToxFile::FileDirection Direction) -{ - if (FileNum != fileNum || FriendId != friendId || Direction != direction) - return; - -// state = tsProcessing; - QDateTime now = QDateTime::currentDateTime(); - long recenttimediff = lastUpdateTime.msecsTo(now); - if (recenttimediff < 1000) //update every 1s - return; - - long timediff = effStartTime.msecsTo(now); - if (timediff <= 0) - return; - - long avgspeed = (BytesSent - previousBytesSent) / timediff * 1000; - long recentspeed = (BytesSent - lastBytesSent) / recenttimediff * 1000; - - speed = getHumanReadableSize(recentspeed)+"/s"; - size = getHumanReadableSize(Filesize); - totalBytes = Filesize; - - if (!avgspeed) - return; - int etaSecs = (Filesize - BytesSent) / avgspeed; - QTime etaTime(0,0); - etaTime = etaTime.addSecs(etaSecs); - eta = etaTime.toString("mm:ss"); - - lastBytesSent = BytesSent; - lastUpdateTime = now; - emit stateUpdated(); -} - -void FileTransferInstance::onFileTransferCancelled(int FriendId, int FileNum, ToxFile::FileDirection Direction) -{ - if (FileNum != fileNum || FriendId != friendId || Direction != direction) - return; - disconnect(Core::getInstance(),0,this,0); - state = tsCanceled; - - emit stateUpdated(); -} - -void FileTransferInstance::onFileTransferFinished(ToxFile File) -{ - if (File.fileNum != fileNum || File.friendId != friendId || File.direction != direction) - return; - disconnect(Core::getInstance(),0,this,0); - - if (File.direction == ToxFile::RECEIVING) - { - QImage preview; - QFile previewFile(File.filePath); - if (previewFile.open(QIODevice::ReadOnly) && previewFile.size() <= MAX_PREVIEW_SIZE) // Don't preview big (>25MiB) images - { - if (preview.loadFromData(previewFile.readAll())) - { - pic = preview.scaled(100, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); - } - previewFile.close(); - } - } - - state = tsFinished; - - emit stateUpdated(); -} - -void FileTransferInstance::onFileTransferAccepted(ToxFile File) -{ - if (File.fileNum != fileNum || File.friendId != friendId || File.direction != direction) - return; - - remotePaused = false; - state = tsProcessing; - effStartTime = QDateTime::currentDateTime(); - - emit stateUpdated(); -} - -void FileTransferInstance::onFileTransferRemotePausedUnpaused(ToxFile File, bool paused) -{ - if (File.fileNum != fileNum || File.friendId != friendId || File.direction != direction) - return; - - remotePaused = paused; - - emit stateUpdated(); -} - -void FileTransferInstance::onFileTransferPaused(int FriendId, int FileNum, ToxFile::FileDirection Direction) -{ - if (FileNum != fileNum || FriendId != friendId || Direction != direction) - return; - - state = tsPaused; - - emit stateUpdated(); -} - -void FileTransferInstance::cancelTransfer() -{ - Core::getInstance()->cancelFileSend(friendId, fileNum); - state = tsCanceled; - emit stateUpdated(); -} - -void FileTransferInstance::rejectRecvRequest() -{ - Core::getInstance()->rejectFileRecvRequest(friendId, fileNum); - onFileTransferCancelled(friendId, fileNum, direction); - state = tsCanceled; - emit stateUpdated(); -} - -// for whatever the fuck reason, QFileInfo::isWritable() always fails for files that don't exist -// which makes it useless for our case -// since QDir doesn't have an isWritable(), the only option I can think of is to make/delete the file -// surely this is a common problem that has a qt-implemented solution? -bool isFileWritable(QString& path) -{ - QFile file(path); - bool exists = file.exists(); - bool out = file.open(QIODevice::WriteOnly); - file.close(); - if (!exists) - file.remove(); - return out; -} - -void FileTransferInstance::acceptRecvRequest() -{ - QString path = Settings::getInstance().getAutoAcceptDir(FriendList::findFriend(friendId)->getToxID()); - - if (path.isEmpty() && Settings::getInstance().getAutoSaveEnabled()) - path = Settings::getInstance().getGlobalAutoAcceptDir(); - - if (!path.isEmpty()) - { - QDir dir(path); - path = dir.filePath(filename); - QFileInfo info(path); - if (info.exists()) // emulate chrome - { - QString name = info.baseName(), ext = info.completeSuffix(); - int i = 0; - do - { - path = dir.filePath(name + QString(" (%1)").arg(++i) + "." + ext); - } - while (QFileInfo(path).exists()); - } - qDebug() << "File: auto saving to" << path; - } - else - { - while (true) - { - path = QFileDialog::getSaveFileName(0, tr("Save a file","Title of the file saving dialog"), QDir::home().filePath(filename)); - if (path.isEmpty()) - return; - else - { - if (isFileWritable(path)) - break; - else - QMessageBox::warning(0, - tr("Location not writable","Title of permissions popup"), - tr("You do not have permission to write that location. Choose another, or cancel the save dialog.", "text of permissions popup")); - } - } - } - - savePath = path; - - Core::getInstance()->acceptFileRecvRequest(friendId, fileNum, path); - state = tsProcessing; - - effStartTime = QDateTime::currentDateTime(); - - emit stateUpdated(); -} - -void FileTransferInstance::pauseResumeRecv() -{ - if (!(state == tsProcessing || state == tsPaused)) - return; - - if (remotePaused) - return; - - Core::getInstance()->pauseResumeFileRecv(friendId, fileNum); - - if (state == tsPaused) - { - effStartTime = QDateTime::currentDateTime(); - previousBytesSent = lastBytesSent; - } - - emit stateUpdated(); -} - -void FileTransferInstance::pauseResumeSend() -{ - if (!(state == tsProcessing || state == tsPaused)) - return; - - if (remotePaused) - return; - - Core::getInstance()->pauseResumeFileSend(friendId, fileNum); - - if (state == tsPaused) - { - effStartTime = QDateTime::currentDateTime(); - previousBytesSent = lastBytesSent; - } - - emit stateUpdated(); -} - -QString FileTransferInstance::QImage2base64(const QImage &img) -{ - QByteArray ba; - QBuffer buffer(&ba); - buffer.open(QIODevice::WriteOnly); - img.save(&buffer, "PNG"); // writes image into ba in PNG format - return ba.toBase64(); -} - -QString FileTransferInstance::getHtmlImage() -{ - - QString res; - if (state == tsPending || state == tsProcessing || state == tsPaused) - { - QImage leftBtnImg(":/ui/fileTransferInstance/stopFileButton.png"); - QImage rightBtnImg; - if (state == tsProcessing) - rightBtnImg = QImage(":/ui/fileTransferInstance/pauseFileButton.png"); - else if (state == tsPaused) - rightBtnImg = QImage(":/ui/fileTransferInstance/resumeFileButton.png"); - else - { - if (direction == ToxFile::SENDING) - rightBtnImg = QImage(":/ui/fileTransferInstance/pauseGreyFileButton.png"); - else - rightBtnImg = QImage(":/ui/fileTransferInstance/acceptFileButton.png"); - } - - if (remotePaused) - rightBtnImg = QImage(":/ui/fileTransferInstance/pauseGreyFileButton.png"); - - res = draw2ButtonsForm("silver", leftBtnImg, rightBtnImg); - } else if (state == tsBroken) - { - QImage leftBtnImg(":/ui/fileTransferInstance/stopFileButton.png"); - QImage rightBtnImg(":/ui/fileTransferInstance/pauseGreyFileButton.png"); - - res = draw2ButtonsForm("red", leftBtnImg, rightBtnImg); - } else if (state == tsCanceled) - { - res = drawButtonlessForm("red"); - } else if (state == tsFinished) - { - res = drawButtonlessForm("green"); - } - - return res; -} - -void FileTransferInstance::pressFromHtml(QString code) -{ - if (state == tsFinished || state == tsCanceled) - return; - - if (direction == ToxFile::SENDING) - { - if (code == "btnA") - cancelTransfer(); - else if (code == "btnB") - pauseResumeSend(); - } else { - if (code == "btnA") - rejectRecvRequest(); - else if (code == "btnB") - { - if (state == tsPending) - acceptRecvRequest(); - else - pauseResumeRecv(); - } - } -} - -QString FileTransferInstance::drawButtonlessForm(const QString &type) -{ - QString imgAStr; - QString imgBStr; - - if (type == "red") - { - imgAStr = ""; - imgBStr = ""; - } else { - imgAStr = ""; - imgBStr = ""; - } - - QString content = "

" + filenameElided + "

" + size + "

"; - - return wrapIntoForm(content, type, imgAStr, imgBStr); -} - -QString FileTransferInstance::insertMiniature(const QString &type) -{ - if (pic == QImage()) - return QString(); - - QString widgetId = QString::number(getId()); - - QString res; - res = "
\n"; - res += ""; - res += "
\n"; - res += "
\n"; - res += "
\n"; - return res; -} - -QString FileTransferInstance::draw2ButtonsForm(const QString &type, const QImage &imgA, const QImage &imgB) -{ - QString widgetId = QString::number(getId()); - QString imgAstr = ""; - QString imgBstr = ""; - - QString content; - QString progrBar = ""; - - content = "

" + filenameElided + "

"; - content += ""; - content += ""; - content += ""; - content += ""; - content += "
" + size + "" + speed + "" + tr("ETA") + ": " + eta + "
"; - content += progrBar; - content += "
"; - - return wrapIntoForm(content, type, imgAstr, imgBstr); -} - -QString FileTransferInstance::wrapIntoForm(const QString& content, const QString &type, const QString &imgAstr, const QString &imgBstr) -{ - QString w = QString::number(QImage(":/ui/fileTransferInstance/emptyLRedFileButton.png").size().width()); - QString imgLeftA, imgLeftB; - - if (type == "green") - { - imgLeftA = ""; - imgLeftB = ""; - } - - if (type == "silver") - { - imgLeftA = ""; - imgLeftB = ""; - } - - if (type == "red") - { - imgLeftA = ""; - imgLeftB = ""; - } - - QString res; - res = "\n"; - res += "\n"; - res += "\n"; - res += insertMiniature(type); - res += "\n"; - res += "\n"; - res += "\n"; - res += "\n"; - res += "
\n"; - res += "
" + imgLeftA + "
" + imgLeftB + "
\n"; - res += "
\n"; - res += "
"; - res += content; - res += "
\n"; - res += "
\n"; - res += "
\n"; - res += "
\n"; - res += "
" + imgAstr + "
" + imgBstr + "
\n"; - res += "
\n"; - - return res; -} - -QImage FileTransferInstance::drawProgressBarImg(const double &part, int w, int h) -{ - QImage progressBar(w, h, QImage::Format_Mono); - - QPainter qPainter(&progressBar); - qPainter.setBrush(Qt::NoBrush); - qPainter.setPen(Qt::black); - qPainter.drawRect(0, 0, w - 1, h - 1); - - qPainter.setBrush(Qt::SolidPattern); - qPainter.setPen(Qt::black); - qPainter.drawRect(1, 0, (w - 2) * (part), h - 1); - - return progressBar; -} - -void FileTransferInstance::onFileTransferBrokenUnbroken(ToxFile File, bool broken) -{ - if (File.fileNum != fileNum || File.friendId != friendId || File.direction != direction) - return; - - if (broken) - state = tsBroken; - else - state = tsProcessing; - - emit stateUpdated(); -} diff --git a/src/filetransferinstance.h b/src/filetransferinstance.h deleted file mode 100644 index 386e8bd01..000000000 --- a/src/filetransferinstance.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - Copyright (C) 2014 by Project Tox - - This file is part of qTox, a Qt-based graphical interface for Tox. - - This program is libre software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - - See the COPYING file for more details. -*/ -#ifndef FILETRANSFERINSTANCE_H -#define FILETRANSFERINSTANCE_H - -#include -#include -#include - -#include "corestructs.h" - -struct ToxFile; - -class FileTransferInstance : public QObject -{ - Q_OBJECT -public: - enum TransfState {tsPending, tsProcessing, tsPaused, tsFinished, tsCanceled, tsBroken}; - -public: - explicit FileTransferInstance(ToxFile File); - QString getHtmlImage(); - uint getId(){return id;} - TransfState getState() {return state;} - -public slots: - void onFileTransferInfo(int FriendId, int FileNum, int64_t Filesize, int64_t BytesSent, ToxFile::FileDirection Direction); - void onFileTransferCancelled(int FriendId, int FileNum, ToxFile::FileDirection Direction); - void onFileTransferFinished(ToxFile File); - void onFileTransferAccepted(ToxFile File); - void onFileTransferPaused(int FriendId, int FileNum, ToxFile::FileDirection Direction); - void onFileTransferRemotePausedUnpaused(ToxFile File, bool paused); - void onFileTransferBrokenUnbroken(ToxFile File, bool broken); - void pressFromHtml(QString); - -signals: - void stateUpdated(); - -private slots: - void cancelTransfer(); - void rejectRecvRequest(); - void acceptRecvRequest(); - void pauseResumeRecv(); - void pauseResumeSend(); - -private: - QString getHumanReadableSize(unsigned long long size); - QString QImage2base64(const QImage &img); - QString drawButtonlessForm(const QString &type); - QString draw2ButtonsForm(const QString &type, const QImage &imgA, const QImage &imgB); - QString insertMiniature(const QString &type); - QString wrapIntoForm(const QString &content, const QString &type, const QString &imgAstr, const QString &imgBstr); - QImage drawProgressBarImg(const double &part, int w, int h); - -private: - static uint Idconter; - uint id; - - TransfState state; - bool remotePaused; - QImage pic; - QString filename, size, speed, eta; - QString filenameElided; - QDateTime effStartTime, lastUpdateTime; - long long lastBytesSent, totalBytes, previousBytesSent = 0; // last var used for eta on resumes - int fileNum; - int friendId; - int contentPrefWidth; - QString savePath; - ToxFile::FileDirection direction; - QString stopFileButtonStylesheet, pauseFileButtonStylesheet, acceptFileButtonStylesheet; -}; - -#endif // FILETRANSFERINSTANCE_H diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 99b481170..44ffc4b81 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -28,7 +28,6 @@ #include "src/widget/form/loadhistorydialog.h" #include "src/friend.h" #include "src/widget/friendwidget.h" -#include "src/filetransferinstance.h" #include "src/widget/netcamview.h" #include "src/widget/tool/chattextedit.h" #include "src/core.h"