2011-03-20 17:43:58 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2011 Christophe Dumez
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
2010-12-26 12:51:37 +03:00
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
#include <libtorrent/version.hpp>
|
|
|
|
#include <libtorrent/session.hpp>
|
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "previewlistdelegate.h"
|
|
|
|
#include "previewselect.h"
|
2013-02-05 04:46:00 +04:00
|
|
|
#include "fs_utils.h"
|
2013-09-14 16:17:50 +04:00
|
|
|
#include "preferences.h"
|
2010-12-26 12:51:37 +03:00
|
|
|
|
2012-02-20 21:56:07 +04:00
|
|
|
PreviewSelect::PreviewSelect(QWidget* parent, QTorrentHandle h): QDialog(parent), h(h) {
|
2010-12-26 12:51:37 +03:00
|
|
|
setupUi(this);
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2013-09-14 16:17:50 +04:00
|
|
|
Preferences pref;
|
2010-12-26 12:51:37 +03:00
|
|
|
// Preview list
|
2013-09-14 16:17:50 +04:00
|
|
|
previewListModel = new QStandardItemModel(0, NB_COLUMNS);
|
2010-12-26 12:51:37 +03:00
|
|
|
previewListModel->setHeaderData(NAME, Qt::Horizontal, tr("Name"));
|
|
|
|
previewListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size"));
|
|
|
|
previewListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress"));
|
|
|
|
previewList->setModel(previewListModel);
|
2014-01-02 20:21:19 +04:00
|
|
|
previewList->hideColumn(FILE_INDEX);
|
2010-12-26 12:51:37 +03:00
|
|
|
listDelegate = new PreviewListDelegate(this);
|
|
|
|
previewList->setItemDelegate(listDelegate);
|
|
|
|
previewList->header()->resizeSection(0, 200);
|
2013-09-14 16:17:50 +04:00
|
|
|
previewList->setAlternatingRowColors(pref.useAlternatingRowColors());
|
2010-12-26 12:51:37 +03:00
|
|
|
// Fill list in
|
|
|
|
std::vector<libtorrent::size_type> fp;
|
|
|
|
h.file_progress(fp);
|
|
|
|
unsigned int nbFiles = h.num_files();
|
2012-02-20 21:56:07 +04:00
|
|
|
for (unsigned int i=0; i<nbFiles; ++i) {
|
2010-12-26 12:51:37 +03:00
|
|
|
QString fileName = h.filename_at(i);
|
2013-02-09 21:55:10 +04:00
|
|
|
if (fileName.endsWith(".!qB"))
|
|
|
|
fileName.chop(4);
|
2013-02-05 04:46:00 +04:00
|
|
|
QString extension = fsutils::fileExtension(fileName).toUpper();
|
2012-02-20 21:30:53 +04:00
|
|
|
if (misc::isPreviewable(extension)) {
|
2010-12-26 12:51:37 +03:00
|
|
|
int row = previewListModel->rowCount();
|
|
|
|
previewListModel->insertRow(row);
|
|
|
|
previewListModel->setData(previewListModel->index(row, NAME), QVariant(fileName));
|
|
|
|
previewListModel->setData(previewListModel->index(row, SIZE), QVariant((qlonglong)h.filesize_at(i)));
|
|
|
|
previewListModel->setData(previewListModel->index(row, PROGRESS), QVariant((double)fp[i]/h.filesize_at(i)));
|
2014-01-02 20:21:19 +04:00
|
|
|
previewListModel->setData(previewListModel->index(row, FILE_INDEX), QVariant(i));
|
2010-12-26 12:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
2013-09-14 16:17:50 +04:00
|
|
|
|
2013-10-15 01:39:36 +04:00
|
|
|
previewList->selectionModel()->select(previewListModel->index(0, NAME), QItemSelectionModel::Select);
|
|
|
|
previewList->selectionModel()->select(previewListModel->index(0, SIZE), QItemSelectionModel::Select);
|
|
|
|
previewList->selectionModel()->select(previewListModel->index(0, PROGRESS), QItemSelectionModel::Select);
|
2012-02-20 21:56:07 +04:00
|
|
|
if (!previewListModel->rowCount()) {
|
2010-12-26 12:51:37 +03:00
|
|
|
QMessageBox::critical(0, tr("Preview impossible"), tr("Sorry, we can't preview this file"));
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
connect(this, SIGNAL(readyToPreviewFile(QString)), parent, SLOT(previewFile(QString)));
|
2013-09-14 16:17:50 +04:00
|
|
|
previewListModel->sort(NAME);
|
|
|
|
previewList->header()->setSortIndicator(0, Qt::AscendingOrder);
|
|
|
|
|
2012-02-20 21:56:07 +04:00
|
|
|
if (previewListModel->rowCount() == 1) {
|
2010-12-26 12:51:37 +03:00
|
|
|
qDebug("Torrent file only contains one file, no need to display selection dialog before preview");
|
|
|
|
// Only one file : no choice
|
|
|
|
on_previewButton_clicked();
|
|
|
|
}else{
|
|
|
|
qDebug("Displaying media file selection dialog for preview");
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-20 21:56:07 +04:00
|
|
|
PreviewSelect::~PreviewSelect() {
|
2010-12-26 12:51:37 +03:00
|
|
|
delete previewListModel;
|
|
|
|
delete listDelegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-20 21:56:07 +04:00
|
|
|
void PreviewSelect::on_previewButton_clicked() {
|
2010-12-26 12:51:37 +03:00
|
|
|
QModelIndex index;
|
2014-01-02 20:21:19 +04:00
|
|
|
QModelIndexList selectedIndexes = previewList->selectionModel()->selectedRows(FILE_INDEX);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (selectedIndexes.size() == 0) return;
|
2010-12-26 12:51:37 +03:00
|
|
|
// Flush data
|
|
|
|
h.flush_cache();
|
2012-02-18 18:44:20 +04:00
|
|
|
|
2012-12-26 22:00:31 +04:00
|
|
|
QStringList absolute_paths(h.absolute_files_path());
|
2014-01-02 20:21:19 +04:00
|
|
|
//only one file should be selected
|
|
|
|
QString path = absolute_paths.at(selectedIndexes.at(0).data().toInt());
|
|
|
|
// File
|
|
|
|
if (QFile::exists(path))
|
|
|
|
emit readyToPreviewFile(path);
|
|
|
|
else
|
|
|
|
QMessageBox::critical(0, tr("Preview impossible"), tr("Sorry, we can't preview this file"));
|
2010-12-26 12:51:37 +03:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2012-02-20 21:56:07 +04:00
|
|
|
void PreviewSelect::on_cancelButton_clicked() {
|
2010-12-26 12:51:37 +03:00
|
|
|
close();
|
|
|
|
}
|