/*
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.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "archivebrowser.h"
#include "archivebrowsermodel.h"
#include "smtreeview.h"
#include "smglobals.h"
#include "delegates.h"
ArchiveBrowser::ArchiveBrowser(QWidget *parent) : QWidget(parent), mSelectedSize(0), mSelectedItems(0){
//prep
mModel = static_cast(SmGlobals::instance()->model("BrowserModel"));
mProxy = new ArchiveBrowserModelProxy;
mProxy->setSourceModel(mModel);
mTree = new SmTreeView("ui/archivebrowserheaders");
mTree->setModel(mProxy);
mTree->setColumnHidden(ArchiveBrowserModel::GenericId, true);
mTree->setColumnHidden(ArchiveBrowserModel::NodeType, true);
mTree->setItemDelegateForColumn(ArchiveBrowserModel::TotalSize, new SizeDelegate(this));
mTree->setItemDelegateForColumn(ArchiveBrowserModel::FileType, new FileTypeDelegate(this));
mTree->setSelectionMode(QAbstractItemView::ExtendedSelection);
//filters
QHBoxLayout *filterLayout = new QHBoxLayout;
mQualityFilter = new QComboBox;
QLabel *filterL = new QLabel(tr("Filters:"));
QLabel *qualityL = new QLabel(tr("Quality"));
filterLayout->addWidget(filterL);
filterLayout->addWidget(qualityL);
setupQualityFilter();
connect(mQualityFilter, SIGNAL(currentIndexChanged(QString)), mProxy, SLOT(setQualityFilter(QString)));
filterLayout->addWidget(mQualityFilter);
mSizeFilter = new QCheckBox(tr("Size Filter"));
connect(mSizeFilter, SIGNAL(stateChanged(int)), mProxy, SLOT(setSizeFilter(int)));
filterLayout->addWidget(mSizeFilter);
filterLayout->addStretch();
//connect
connect(mTree->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(browserSelectionChanged(QItemSelection,QItemSelection)));
//make widget
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(filterLayout);
mainLayout->addWidget(mTree);
setLayout(mainLayout);
mTree->setSortingEnabled(true);
}
void ArchiveBrowser::browserSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
QModelIndexList selectedIdx = selectedRows(selected);
QModelIndexList deselectedIdx = selectedRows(deselected);
foreach(QModelIndex sel, selectedIdx){
mSelectedSize += sel.data(ArchiveBrowserModel::TotalSizeRole).toDouble();
++mSelectedItems;
mModel->setData(sel, true, ArchiveBrowserModel::SelectedRole);
}
foreach(QModelIndex desel, deselectedIdx){
mSelectedSize -= desel.data(ArchiveBrowserModel::TotalSizeRole).toDouble();
--mSelectedItems;
mModel->setData(desel, false, ArchiveBrowserModel::SelectedRole);
}
emit sizeChanged(mSelectedSize);
emit itemCountChanged(mSelectedItems);
qint64 remaining = Q_INT64_C(1024 * 1024 * 1024 * 4) - mSelectedSize;
mProxy->setBytesRemaining(remaining);
}
void ArchiveBrowser::readConfig(){
mTree->readHeaderConfig();
QSettings s;
QString qualFilter = s.value("ui/browserquality", tr("(none)")).toString();
mQualityFilter->setCurrentText(qualFilter);
}
void ArchiveBrowser::writeSettings(){
mTree->writeHeaderConfig();
QSettings s;
s.setValue("ui/browserquality", mQualityFilter->currentText());
}
void ArchiveBrowser::moveToBurn() {
QModelIndexList sel = mTree->selectionModel()->selectedRows();
if(sel.isEmpty()){
return;
}
QSettings s;
QString destDirS = s.value("paths/burn").toString();
QDir burnDir(destDirS);
if(!burnDir.exists()){
QString msg = QString(tr("Destination directory %1 does not exist!\nBailing out!")).arg(destDirS);
QMessageBox::critical(this, tr("Error"), msg);
return;
}
QString msg = QString(tr("This will do the following:
- Move %1 file(s) to %2
- Update the DVD no. for %1 files
Continue?
")).arg(sel.size()).arg(destDirS);
int retval = QMessageBox::question(this, tr("Question"), msg, QMessageBox::Yes | QMessageBox::No);
if(retval == QMessageBox::Yes){
QList filesToUpdate;
foreach(QModelIndex idx, sel){
QString dirName = idx.data(ArchiveBrowserModel::NameRole).toString();
dirName.replace(' ', '.');
burnDir.mkdir(dirName);
QString burnDirS = QString("%1/%2").arg(destDirS).arg(dirName);
QModelIndex real = mProxy->mapToSource(idx);
QModelIndexList children = mModel->children(real);
foreach(QModelIndex child, children){
QFileInfo current(child.data(ArchiveBrowserModel::FullPathRole).toString());
int type = child.data(ArchiveBrowserModel::FileTypeRole).toInt();
QString destination = QString("%1/%2").arg(burnDirS).arg(current.fileName());
if(type == 1){ //movie
filesToUpdate << child.data(ArchiveBrowserModel::GenericIdRole).toInt();
}else{
QFile::copy(current.absoluteFilePath(), destination);
}
}
}
mModel->updateDVDNo(filesToUpdate);
mModel->refresh();
}
}
void ArchiveBrowser::refresh() {
mModel->refresh();
}
void ArchiveBrowser::setupQualityFilter(){
mQualityFilter->clear();
QList qualities = mModel->availableQualities();
qSort(qualities);
QStringList qualityList = QStringList() << tr("(none)");
foreach(int q, qualities){
qualityList << QString::number(q);
}
mQualityFilter->addItems(qualityList);
}
QModelIndexList ArchiveBrowser::selectedRows(const QItemSelection &sel){
QModelIndexList retval;
QModelIndexList selIdx = sel.indexes();
foreach(QModelIndex idx, selIdx){
QModelIndex real = mProxy->mapToSource(idx);
if(real.column() == 0){
retval << real;
}
}
return retval;
}