summaryrefslogtreecommitdiffstats
path: root/moviemodel.cpp
blob: 53849cc90b851db51d42f56e10f82823d1bc1502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
  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 <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();
}

MovieModel::~MovieModel(){
	qDeleteAll(mItems);
}

QModelIndex MovieModel::index(int row, int column, const QModelIndex &parent) const{
	if((parent != QModelIndex()) || (!hasIndex(row, column, parent)) || (row > mItems.size())){
		return QModelIndex();
	}
	MovieItem *internal = mItems.at(row);
	return createIndex(row, column, internal);
}

QVariant MovieModel::data(const QModelIndex &index, int role) const{
	if(!index.isValid()){
		return QVariant();
	}
	if(role == Qt::DisplayRole){
		MovieItem *item = static_cast<MovieItem*>(index.internalPointer());
		Q_ASSERT(item != 0);
		switch (index.column()){
			case MovieItem::Dvd:
				return QVariant(QString(tr("DVD %1")).arg(QString::number(item->dataAt(MovieItem::Dvd).toInt())));
		}
		return item->dataAt(index.column());
	}
	return QVariant();
}

Qt::ItemFlags MovieModel::flags(const QModelIndex &index) const{
	if(!index.isValid()){
		return 0;
	}
	return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant MovieModel::headerData(int section, Qt::Orientation o, int role) const{
	if((o == Qt::Horizontal) && (role == Qt::DisplayRole)){
		if(section < mHeaders.size()){
			return mHeaders.at(section);
		}
	}
	return QVariant();
}

bool MovieModel::insertRows(int row, int count, const QModelIndex &){
	if(row > rowCount(QModelIndex())){
		return false;
	}
	beginInsertRows(QModelIndex(), row, row);
	for(int i = row; i < (row + count); ++i){
		MovieItem *newItem = new MovieItem(-1, 0);
		mItems.insert(row, newItem);
	}
	endInsertRows();
	return true;
}

bool MovieModel::removeRows(int row, int count, const QModelIndex &){
	if(row >= rowCount(QModelIndex())){
		return false;
	}
	beginRemoveRows(QModelIndex(), row, row + count - 1);
	for(int i = row; i < (row + count); ++i){
		MovieItem *item = mItems.at(i);
		delete item;
		mItems.removeAt(i);
	}
	endRemoveRows();
	return true;
}

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*>(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();
	while(movieQuery.next()){
		int id = movieQuery.value(0).toInt();
		MovieItem *item = new MovieItem(id);
		mItems << item;
	}
}