/* 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 #include #include "coverarchiveeditor.h" #include "moviemodel.h" #include "coveritem.h" #include "helper.h" CoverArchiveEditor::CoverArchiveEditor(MovieModel *model, QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f), mModel(model){ QGridLayout *grid = new QGridLayout; //front cover QLabel *l1 = new QLabel(tr("Set &front cover")); mFront = new QComboBox; l1->setBuddy(mFront); grid->addWidget(l1, 0, 0); grid->addWidget(mFront, 0, 1); //back cover QLabel *l2 = new QLabel(tr("Set &back cover")); mBack = new QComboBox; l2->setBuddy(mBack); grid->addWidget(l2, 1, 0); grid->addWidget(mBack, 1, 1); //general cover QLabel *l3 = new QLabel(tr("Set &general cover")); mCovers = new QComboBox; l3->setBuddy(mCovers); grid->addWidget(l3, 2, 0); grid->addWidget(mCovers, 2, 1); //buttons QHBoxLayout *buttonLayout = new QHBoxLayout; mAddCover = new QPushButton(tr("Add cover...")); connect(mAddCover, SIGNAL(clicked()), this, SLOT(addCover())); mUpdate = new QPushButton(tr("Update")); connect(mUpdate, SIGNAL(clicked()), this, SLOT(accept())); mClose = new QPushButton(tr("Close")); connect(mClose, SIGNAL(clicked()), this, SLOT(reject())); buttonLayout->addStretch(); buttonLayout->addWidget(mAddCover); buttonLayout->addWidget(mUpdate); buttonLayout->addWidget(mClose); //main layout QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(grid); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); } void CoverArchiveEditor::setMovie(const QModelIndex &movie){ mCoverList = mModel->data(movie, MovieModel::CoverRole).toList(); mMovId = mModel->data(movie, MovieModel::IdRole).toInt(); QString title = QString(tr("%1 - Cover edit for %2")).arg(qApp->applicationName()).arg(movie.data().toString()); setWindowTitle(title); QStringList files("-"); foreach(QVariant c, mCoverList){ CoverItem item = c.value(); files << item.fileName(); } mFront->addItems(files); QList::const_iterator it; int idx(-1); it = std::find_if(mCoverList.constBegin(), mCoverList.constEnd(), std::bind2nd(CoverItem::findType(), "front")); QString fn; if(it != mCoverList.constEnd()){ fn = it->value().fileName(); idx = mFront->findText(fn); if(idx != -1){ mFront->setCurrentIndex(idx); } } mBack->addItems(files); it = std::find_if(mCoverList.constBegin(), mCoverList.constEnd(), std::bind2nd(CoverItem::findType(), "back")); if(it != mCoverList.constEnd()){ fn = it->value().fileName(); idx = mBack->findText(fn); if(idx != -1){ mBack->setCurrentIndex(idx); } } mCovers->addItems(files); it = std::find_if(mCoverList.constBegin(), mCoverList.constEnd(), std::bind2nd(CoverItem::findType(), "general")); if(it != mCoverList.constEnd()){ fn = it->value().fileName(); idx = mCovers->findText(fn); if(idx != -1){ mCovers->setCurrentIndex(idx); } } } void CoverArchiveEditor::accept(){ QList items; QString fn; CoverItem item; fn = mFront->currentText(); item = realItem(fn, "front"); if(item != CoverItem()){ items << item; } fn = mBack->currentText(); item = realItem(fn, "back"); if(item != CoverItem()){ items << item; } fn = mCovers->currentText(); item = realItem(fn, "general"); if(item != CoverItem()){ items << item; } mModel->setCovers(mMovId, items); } void CoverArchiveEditor::addCover(){ QSettings s; QString startDir = s.value("ui/selectstartup").toString(); QStringList files = QFileDialog::getOpenFileNames(this, "Select covers to add...", startDir); QStringList addItems; foreach(QString f, files){ mAddedCovers << f; QFileInfo info(f); addItems << info.fileName(); } mFront->addItems(addItems); mBack->addItems(addItems); mCovers->addItems(addItems); } CoverItem CoverArchiveEditor::coverItem(const QString &path, const QString &type) const{ QString md5 = Helper::md5Sum(path); QString newPath = Helper::moveToArchive(path, md5); return CoverItem(newPath, type, md5); } CoverItem CoverArchiveEditor::realItem(const QString &filename, const QString &type) const{ QList::const_iterator initCit = std::find_if(mCoverList.constBegin(), mCoverList.constEnd(), std::bind2nd(CoverItem::findFilename(), filename)); //got it, have to convert it to the right type if(initCit != mCoverList.constEnd()){ CoverItem i = initCit->value(); i.setType(type); return i; //an added item }else{ QList::const_iterator ait = std::find_if(mAddedCovers.constBegin(), mAddedCovers.constEnd(), std::bind2nd(Helper::StringListContains(), filename)); //maybe an unset item if(ait == mAddedCovers.constEnd()){ QList::const_iterator oit = std::find_if(mCoverList.constBegin(), mCoverList.constEnd(), std::bind2nd(CoverItem::findType(), type)); if(oit != mCoverList.constEnd()){ CoverItem oldItem = oit->value(); //remove it from fs Helper::removeFromArchive(oldItem.fileName(), oldItem.md5()); } return CoverItem(); } CoverItem i = coverItem((*ait), type); return i; } return CoverItem(); }