summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--coveritem.cpp20
-rw-r--r--coveritem.h28
-rw-r--r--moviemodel.cpp62
-rw-r--r--moviemodel.h12
-rw-r--r--shemov.pro6
5 files changed, 119 insertions, 9 deletions
diff --git a/coveritem.cpp b/coveritem.cpp
new file mode 100644
index 0000000..658c630
--- /dev/null
+++ b/coveritem.cpp
@@ -0,0 +1,20 @@
+/*
+ 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 <QString>
+
+#include "coveritem.h"
+
+CoverItem::CoverItem(const QString &filename, const QString &type, const QString &md5) : mFilename(filename), mMd5(md5) {
+ bool valid = false;
+ if((type == "front") || (type == "back")){
+ valid = true;
+ }
+ if(!valid){
+ qWarning("Invalid covertype: %s", qPrintable(type));
+ }
+}
diff --git a/coveritem.h b/coveritem.h
new file mode 100644
index 0000000..a435f90
--- /dev/null
+++ b/coveritem.h
@@ -0,0 +1,28 @@
+/*
+ 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.
+*/
+
+#ifndef COVERITEM_H
+#define COVERITEM_H
+
+class QString;
+
+class CoverItem {
+ public:
+ CoverItem(const QString &filename, const QString &type, const QString &md5);
+ ~CoverItem() {};
+ const QString fileName() const { return mFilename; };
+ const QString type() const { return mType; };
+ const QString md5() const { return mMd5; };
+
+ private:
+ QString mFilename;
+ QString mType;
+ QString mMd5;
+};
+
+#endif
+
diff --git a/moviemodel.cpp b/moviemodel.cpp
index a4f47dc..53849cc 100644
--- a/moviemodel.cpp
+++ b/moviemodel.cpp
@@ -8,9 +8,15 @@
#include <QSqlQuery>
#include "moviemodel.h"
+#include "coveritem.h"
MovieModel::MovieModel(QObject *parent) : QAbstractItemModel(parent) {
mHeaders << tr("Title") << tr("Filename") << tr("MD5Sum") << tr("Size") << tr("Genre") << tr("Quality") << tr("Archived");
+ mInsertQuery = new QSqlQuery("SELECT insert_movie(:title, :filename, :md5, :filesize, :genre, :quality, :dvd");
+ mDeleteActorsForMovie = new QSqlQuery("DELETE FROM movieactormap WHERE imovid = :id");
+ mInsertActorsForMovie = new QSqlQuery("INSERT INTO movieactormap VALUES(:movid, :actorid");
+ mDeleteCovers = new QSqlQuery("DELETE FROM covers WHERE imovid = :id");
+ mInsertCovers = new QSqlQuery("INSERT INTO covers VALUES(:filename, :movid, :covertype, :md5sum");
populate();
}
@@ -62,10 +68,11 @@ bool MovieModel::insertRows(int row, int count, const QModelIndex &){
if(row > rowCount(QModelIndex())){
return false;
}
- Q_ASSERT(count == 1);
beginInsertRows(QModelIndex(), row, row);
- MovieItem *newItem = new MovieItem(-1, 0);
- mItems.insert(row, newItem);
+ for(int i = row; i < (row + count); ++i){
+ MovieItem *newItem = new MovieItem(-1, 0);
+ mItems.insert(row, newItem);
+ }
endInsertRows();
return true;
}
@@ -84,19 +91,62 @@ bool MovieModel::removeRows(int row, int count, const QModelIndex &){
return true;
}
-bool MovieModel::setDataAt(const QModelIndex &index, const QList<QVariant> &data, qint32 genre, const QList<QVariant> &actors, const QList<QVariant> &covers){
- if(!index.isValid() || (data.size() > MovieItem::NumRows)){
+bool MovieModel::setDataAt(const QModelIndex &idx, const QList<QVariant> &data, const QList<QVariant> &actors, const QList<CoverItem> &covers){
+ if(!idx.isValid() || (data.size() > MovieItem::NumRows)){
return false;
}
- MovieItem *item = static_cast<MovieItem*>(index.internalPointer());
+ MovieItem *item = static_cast<MovieItem*>(idx.internalPointer());
Q_ASSERT(item != 0);
if(item->id() != -1){
qWarning("ID not as expected: item->id() == %d", item->id());
return false;
}
+ for(int i = 0; i < MovieItem::NumRows; ++i){
+ mInsertQuery->bindValue(i, data[i]);
+ }
+ mInsertQuery->exec();
+ int id(0);
+ while(mInsertQuery->next()){
+ id = mInsertQuery->value(0).toInt();
+ }
+ if(id == -1){
+ return false;
+ }
+ if(!actors.isEmpty()){
+ setActors(id, actors);
+ }
+ if(!covers.isEmpty()){
+ setCovers(id, covers);
+ }
+ item->setId(id);
+ QModelIndex start = index(idx.row(), 0, QModelIndex());
+ QModelIndex end = index(idx.row(), MovieItem::NumRows - 1, QModelIndex());
+ emit dataChanged(start, end);
return true;
}
+void MovieModel::setActors(int id, const QList<QVariant> &actors){
+ mDeleteActorsForMovie->bindValue(":id", id);
+ mDeleteActorsForMovie->exec();
+ foreach(QVariant a, actors){
+ mInsertActorsForMovie->bindValue(":movid", id);
+ mInsertActorsForMovie->bindValue(":actorid", a);
+ mInsertActorsForMovie->exec();
+ }
+}
+
+void MovieModel::setCovers(int id, const QList<CoverItem> &covers){
+ mDeleteCovers->bindValue(":id", id);
+ mDeleteCovers->exec();
+ foreach(CoverItem c, covers){
+ mInsertCovers->bindValue(":filename", c.fileName());
+ mInsertCovers->bindValue(":movid", id);
+ mInsertCovers->bindValue(":covertype", c.type());
+ mInsertCovers->bindValue(":md5", c.md5());
+ mInsertCovers->exec();
+ }
+}
+
void MovieModel::populate(){
QSqlQuery movieQuery("SELECT imovid FROM movies");
movieQuery.exec();
diff --git a/moviemodel.h b/moviemodel.h
index 8ff8c87..acfa8fa 100644
--- a/moviemodel.h
+++ b/moviemodel.h
@@ -11,6 +11,9 @@
#include <QAbstractItemModel>
#include <QVariant>
+class QSqlQuery;
+class CoverItem;
+
#include "movieitem.h"
class MovieModel : public QAbstractItemModel {
@@ -29,12 +32,19 @@ class MovieModel : public QAbstractItemModel {
QVariant headerData(int section, Qt::Orientation o, int role) const;
bool insertRows(int row, int count, const QModelIndex &);
bool removeRows(int row, int count, const QModelIndex &);
- bool setDataAt(const QModelIndex &index, const QList<QVariant> &data, qint32 genre, const QList<QVariant> &actors, const QList<QVariant> &covers);
+ bool setDataAt(const QModelIndex &index, const QList<QVariant> &data, const QList<QVariant> &actors, const QList<CoverItem> &covers);
+ void setActors(int id, const QList<QVariant> &actors);
+ void setCovers(int id, const QList<CoverItem> &covers);
private:
void populate();
QList<MovieItem*> mItems;
QList<QVariant> mHeaders;
+ QSqlQuery *mInsertQuery;
+ QSqlQuery *mDeleteActorsForMovie;
+ QSqlQuery *mInsertActorsForMovie;
+ QSqlQuery *mDeleteCovers;
+ QSqlQuery *mInsertCovers;
};
#endif
diff --git a/shemov.pro b/shemov.pro
index bba50ae..55a6647 100644
--- a/shemov.pro
+++ b/shemov.pro
@@ -5,8 +5,10 @@ QT += sql
SOURCES = main.cpp \
listmodel.cpp \
movieitem.cpp \
-moviemodel.cpp
+moviemodel.cpp \
+coveritem.cpp
HEADERS = listitem.h \
listmodel.h \
movieitem.h \
-moviemodel.h
+moviemodel.h \
+coveritem.h