Added new widget for hashing files.

Added new common class FilesDefs to handle informations of file types like icons and names.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4713 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2011-12-07 13:08:12 +00:00
parent c9fc77f2d1
commit 0ee35b55f4
35 changed files with 932 additions and 1031 deletions

View file

@ -0,0 +1,85 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2011, RetroShare Team
*
* This program is free 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 2
* 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include <QApplication>
#include <QFileInfo>
#include "FilesDefs.h"
#include "RsCollectionFile.h"
static QString getInfoFromFilename(const QString& filename, bool anyForUnknown, bool image)
{
QString ext = QFileInfo(filename).suffix().toLower();
if (ext == "jpg" || ext == "jpeg" || ext == "tif" || ext == "tiff" || ext == "png" || ext == "gif" || ext == "bmp" || ext == "ico" || ext == "svg") {
return image ? ":/images/FileTypePicture.png" : QApplication::translate("FilesDefs", "Picture");
} else if (ext == "avi" || ext == "mpg" || ext == "mpeg" || ext == "wmv" || ext == "divx" || ext == "ts" ||
ext == "mkv" || ext == "mp4" || ext == "flv" || ext == "mov" || ext == "asf" || ext == "xvid" ||
ext == "vob" || ext == "qt" || ext == "rm" || ext == "3gp" || ext == "ogm") {
return image ? ":/images/FileTypeVideo.png" : QApplication::translate("FilesDefs", "Video");
} else if (ext == "ogg" || ext == "mp3" || ext == "mp1" || ext == "mp2" || ext == "wav" || ext == "wma" || ext == "m4a" || ext == "flac" ||ext == "xpm") {
return image ? ":/images/FileTypeAudio.png" : QApplication::translate("FilesDefs", "Audio");
} else if (ext == "tar" || ext == "bz2" || ext == "zip" || ext == "gz" || ext == "7z" || ext == "msi" ||
ext == "rar" || ext == "rpm" || ext == "ace" || ext == "jar" || ext == "tgz" || ext == "lha" ||
ext == "cab" || ext == "cbz"|| ext == "cbr" || ext == "alz" || ext == "sit" || ext == "arj" || ext == "deb") {
return image ? ":/images/FileTypeArchive.png" : QApplication::translate("FilesDefs", "Archive");
} else if (ext == "app" || ext == "bat" || ext == "cgi" || ext == "com" ||
ext == "exe" || ext == "js" || ext == "pif" ||
ext == "py" || ext == "pl" || ext == "sh" || ext == "vb" || ext == "ws") {
return image ? ":/images/FileTypeProgram.png" : QApplication::translate("FilesDefs", "Program");
} else if (ext == "iso" || ext == "nrg" || ext == "mdf" || ext == "img" || ext == "dmg" || ext == "bin" || ext == "uif") {
return image ? ":/images/FileTypeCDImage.png" : QApplication::translate("FilesDefs", "CD/DVD-Image");
} else if (ext == "txt" || ext == "cpp" || ext == "c" || ext == "h" || ext == "ui" ||
ext == "doc" || ext == "rtf" || ext == "sxw" || ext == "xls" || ext == "pps" || ext == "xml" || ext == "nfo" ||
ext == "reg" || ext == "sxc" || ext == "odt" || ext == "ods" || ext == "dot" || ext == "ppt" || ext == "css" || ext == "crt" ||
ext == "html" || ext == "htm" || ext == "php") {
return image ? ":/images/FileTypeDocument.png" : QApplication::translate("FilesDefs", "Document");
} else if (ext == "pdf") {
return image ? ":/images/mimetypes/pdf.png" : QApplication::translate("FilesDefs", "Document");
} else if (ext == RsCollectionFile::ExtensionString) {
return image ? ":/images/mimetypes/rscollection-16.png" : QApplication::translate("FilesDefs", "RetroShare collection file");
} else if (ext == "sub" || ext == "srt") {
return image ? ":/images/FileTypeAny.png" : QApplication::translate("FilesDefs", "Subtitles");
} else if (ext == "nds") {
return image ? ":/images/FileTypeAny.png" : QApplication::translate("FilesDefs", "Nintendo DS Rom");
}
if (anyForUnknown) {
return image ? ":/images/FileTypeAny.png" : "";
}
return "";
}
QString FilesDefs::getImageFromFilename(const QString& filename, bool anyForUnknown)
{
return getInfoFromFilename(filename, anyForUnknown, true);
}
QIcon FilesDefs::getIconFromFilename(const QString& filename)
{
return QIcon(getInfoFromFilename(filename, true, true));
}
QString FilesDefs::getNameFromFilename(const QString &filename)
{
return getInfoFromFilename(filename, false, false);
}

View file

@ -0,0 +1,37 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2011, RetroShare Team
*
* This program is free 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 2
* 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef _FILESDEFS_H
#define _FILESDEFS_H
#include <QString>
#include <QIcon>
class FilesDefs
{
public:
static QString getImageFromFilename(const QString& filename, bool anyForUnknown);
static QIcon getIconFromFilename(const QString& filename);
static QString getNameFromFilename(const QString& filename);
};
#endif

View file

@ -0,0 +1,259 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2011, RetroShare Team
*
* This program is free 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 2
* 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include <QMessageBox>
#include <QDir>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QUrl>
#include <QTimer>
#include <iostream>
#include "gui/feeds/AttachFileItem.h"
#include "HashBox.h"
#include "ui_HashBox.h"
HashedFile::HashedFile()
{
size = 0;
flag = NoFlag;
}
HashBox::HashBox(QWidget *parent) :
QScrollArea(parent),
ui(new Ui::HashBox)
{
ui->setupUi(this);
mAutoHide = false;
dropWidget = NULL;
}
HashBox::~HashBox()
{
delete ui;
}
void HashBox::setAutoHide(bool autoHide)
{
mAutoHide = autoHide;
setVisible(!mAutoHide);
}
void HashBox::setDropWidget(QWidget* widget)
{
if (dropWidget) {
dropWidget->removeEventFilter(this);
}
dropWidget = widget;
if (dropWidget) {
widget->installEventFilter(this);
}
}
static void showFormats(const std::string& event, const QStringList& formats)
{
std::cerr << event << "() Formats" << std::endl;
QStringList::const_iterator it;
for (it = formats.begin(); it != formats.end(); ++it) {
std::cerr << "Format: " << (*it).toStdString();
std::cerr << std::endl;
}
}
bool HashBox::eventFilter(QObject* object, QEvent* event)
{
if (object == dropWidget) {
if (event->type() == QEvent::DragEnter) {
QDragEnterEvent* dragEnterEvent = static_cast<QDragEnterEvent*>(event);
if (dragEnterEvent) {
/* print out mimeType */
showFormats("HashBox::dragEnterEvent", dragEnterEvent->mimeData()->formats());
if (dragEnterEvent->mimeData()->hasUrls()) {
std::cerr << "HashBox::dragEnterEvent() Accepting Urls" << std::endl;
dragEnterEvent->acceptProposedAction();
} else {
std::cerr << "HashBox::dragEnterEvent() No Urls" << std::endl;
}
}
} else if (event->type() == QEvent::Drop) {
QDropEvent* dropEvent = static_cast<QDropEvent*>(event);
if (dropEvent) {
if (Qt::CopyAction & dropEvent->possibleActions()) {
/* print out mimeType */
showFormats("HashBox::dropEvent", dropEvent->mimeData()->formats());
QStringList files;
if (dropEvent->mimeData()->hasUrls()) {
std::cerr << "HashBox::dropEvent() Urls:" << std::endl;
QList<QUrl> urls = dropEvent->mimeData()->urls();
QList<QUrl>::iterator uit;
for (uit = urls.begin(); uit != urls.end(); ++uit) {
QString localpath = uit->toLocalFile();
std::cerr << "Whole URL: " << uit->toString().toStdString() << std::endl;
std::cerr << "or As Local File: " << localpath.toStdString() << std::endl;
if (localpath.isEmpty() == false) {
//Check that the file does exist and is not a directory
QDir dir(localpath);
if (dir.exists()) {
std::cerr << "HashBox::dropEvent() directory not accepted." << std::endl;
QMessageBox mb(tr("Drop file error."), tr("Directory can't be dropped, only files are accepted."), QMessageBox::Information, QMessageBox::Ok, 0, 0, this);
mb.exec();
} else if (QFile::exists(localpath)) {
files.push_back(localpath);
} else {
std::cerr << "HashBox::dropEvent() file does not exists."<< std::endl;
QMessageBox mb(tr("Drop file error."), tr("File not found or file name not accepted."), QMessageBox::Information, QMessageBox::Ok, 0, 0, this);
mb.exec();
}
}
}
}
addAttachments(files);
dropEvent->setDropAction(Qt::CopyAction);
dropEvent->accept();
} else {
std::cerr << "HashBox::dropEvent() Rejecting uncopyable DropAction" << std::endl;
}
}
}
}
// pass the event on to the parent class
return QScrollArea::eventFilter(object, event);
}
void HashBox::addAttachments(const QStringList& files, HashedFile::Flags flag)
{
/* add a AttachFileItem to the attachment section */
std::cerr << "PopupChatDialog::addExtraFile() hashing file." << std::endl;
if (files.isEmpty()) {
return;
}
if (mAutoHide) {
show();
}
QStringList::ConstIterator it;
for (it = files.constBegin(); it != files.constEnd(); ++it) {
/* add widget in for new destination */
AttachFileItem* file = new AttachFileItem(*it);
QObject::connect(file, SIGNAL(fileFinished(AttachFileItem*)), this, SLOT(fileFinished(AttachFileItem*)));
HashingInfo hashingInfo;
hashingInfo.item = file;
hashingInfo.flag = flag;
mHashingInfos.push_back(hashingInfo);
ui->verticalLayout->addWidget(file, 1, 0);
}
QApplication::processEvents();
// workaround for Qt bug, the size from the first call to QScrollArea::sizeHint() is stored in QWidgetItemV2 and
// QScrollArea::sizeHint() is never called again so that widgetResizable of QScrollArea doesn't work
// the next line clears the member QScrollArea::widgetSize for recalculation of the added children in QScrollArea::sizeHint()
setWidget(takeWidget());
// the next line set the cache to dirty
updateGeometry();
emit fileHashingStarted();
checkAttachmentReady();
}
void HashBox::fileFinished(AttachFileItem* file)
{
std::cerr << "HashBox::fileHashingFinished() started." << std::endl;
//check that the file is ok
if (file->getState() == AFI_STATE_ERROR) {
std::cerr << "HashBox::fileHashingFinished error file is not hashed.";
return;
}
checkAttachmentReady();
}
void HashBox::checkAttachmentReady()
{
if (mHashingInfos.isEmpty()) {
return;
}
QList<HashingInfo>::iterator it;
for (it = mHashingInfos.begin(); it != mHashingInfos.end(); ++it) {
if (it->item->isHidden()) {
continue;
}
if (!it->item->done()) {
break;
}
}
if (it != mHashingInfos.end()) {
/* repeat... */
QTimer::singleShot(500, this, SLOT(checkAttachmentReady()));
return;
}
if (mAutoHide) {
hide();
}
QList<HashedFile> hashedFiles;
for (it = mHashingInfos.begin(); it != mHashingInfos.end(); ++it) {
HashingInfo& hashingInfo = *it;
if (hashingInfo.item->done()) {
HashedFile hashedFile;
hashedFile.filename = hashingInfo.item->FileName();
hashedFile.filepath = hashingInfo.item->FilePath();
hashedFile.hash = hashingInfo.item->FileHash();
hashedFile.size = hashingInfo.item->FileSize();
hashedFile.flag = hashingInfo.flag;
hashedFiles.push_back(hashedFile);
ui->verticalLayout->removeWidget(hashingInfo.item);
hashingInfo.item->deleteLater();
}
}
mHashingInfos.clear();
QApplication::processEvents();
// workaround for Qt bug, the size from the first call to QScrollArea::sizeHint() is stored in QWidgetItemV2 and
// QScrollArea::sizeHint() is never called again so that widgetResizable of QScrollArea doesn't work
// the next line clears the member QScrollArea::widgetSize for recalculation of the removed children in QScrollArea::sizeHint()
setWidget(takeWidget());
// the next line set the cache to dirty
updateGeometry();
emit fileHashingFinished(hashedFiles);
}

View file

@ -0,0 +1,91 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2011, RetroShare Team
*
* This program is free 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 2
* 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef HASHBOX_H
#define HASHBOX_H
#include <QScrollArea>
namespace Ui {
class HashBox;
}
class AttachFileItem;
class QVBoxLayout;
class HashedFile
{
public:
enum Flags {
NoFlag = 0,
Picture = 1
};
public:
QString filename;
QString filepath;
uint64_t size;
std::string hash;
Flags flag;
public:
HashedFile();
};
class HashBox : public QScrollArea
{
Q_OBJECT
public:
explicit HashBox(QWidget *parent = 0);
~HashBox();
void setAutoHide(bool autoHide);
void addAttachments(const QStringList& files, HashedFile::Flags flag = HashedFile::NoFlag);
void setDropWidget(QWidget* widget);
protected:
bool eventFilter(QObject *object, QEvent *event);
private slots:
void fileFinished(AttachFileItem* file);
void checkAttachmentReady();
signals:
void fileHashingStarted();
void fileHashingFinished(QList<HashedFile> hashedFiles);
private:
class HashingInfo
{
public:
AttachFileItem* item;
HashedFile::Flags flag;
};
QList<HashingInfo> mHashingInfos;
bool mAutoHide;
QWidget* dropWidget;
Ui::HashBox *ui;
};
#endif // HASHBOX_H

View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>HashBox</class>
<widget class="QScrollArea" name="HashBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>35</height>
</size>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<number>0</number>
</property>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>