summaryrefslogtreecommitdiffstats
path: root/newmoviewizard.cpp
blob: 8cd99368529f89c22deb64745688c890232572a8 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
  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 <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QFileInfo>

#include "newmoviewizard.h"
#include "movieinfopage.h"
#include "moviemappingpage.h"
#include "moviemetadatapage.h"
#include "wizardtreemodel.h"
#include "smglobals.h"
#include "helper.h"

NewMovieWizard::NewMovieWizard(QWidget *parent) : QWizard(parent){
	mInfoPage = new MovieInfoPage;
    mActorPage = new MovieMappingPage("Actors");
    mGenrePage = new MovieMappingPage("Genres");
	mMetadataPage = new MovieMetadataPage;
	addPage(mInfoPage);
	addPage(mActorPage);
	addPage(mGenrePage);
	addPage(mMetadataPage);
	setOption(QWizard::IndependentPages, true);
}

void NewMovieWizard::reject(){
    mInfoPage->saveData();
    QWizard::reject();
}

void NewMovieWizard::accept(){
    QSqlDatabase db = QSqlDatabase::database("treedb");
    db.open();
    db.transaction();
    QString seriesName = field("title").toString().toLower().trimmed();
    QSqlQuery seriesIdQ(db);
    seriesIdQ.prepare("SELECT iseries_id FROM series WHERE tseries_name = :value");
    seriesIdQ.bindValue(":value", seriesName);
    //see if we already have a series with this name
    int seriesId = 0;
    seriesIdQ.exec();
    while(seriesIdQ.next()){
        seriesId = seriesIdQ.value(0).toInt();
    }
    //we don't have an id
    if(!seriesId){
        QSqlQuery insertSeriesQ(db);
        insertSeriesQ.prepare("INSERT INTO series (tseries_name) VALUES(:name)");
        insertSeriesQ.bindValue(":name", seriesName);
        if(insertSeriesQ.exec()){
            QSqlQuery curval("SELECT currval('series_iseries_id__seq')", db);
            while(curval.next()){
                seriesId = curval.value(0).toInt();
            }
            db.commit();
            db.transaction();
        }else{
            QMessageBox::critical(this, tr("Error"), tr("Failed to create series!"));
            db.rollback();
            return;
        }
    }
    //now we have a series id, handle seriespart
    int partNo = field("seriesNo").toInt();
    QString subtitle = field("subtitle").toString().toLower().trimmed();
    QStringList subtitles = Helper::fuzzyCheck(subtitle);
    if(!subtitles.isEmpty()){
        QString msg = QString(tr("%1:<ul>")).arg(subtitle);
        for(int i = 0; i < subtitles.count() && i < 5; ++i){
            msg = msg.append("<li>%1</li>").arg(subtitles.at(i));
        }
        msg.append("</ul>");
        int res = QMessageBox::question(this, tr("Fuzzy search"), msg, QMessageBox::Cancel | QMessageBox::Ignore, QMessageBox::Cancel);
        if(res == QMessageBox::Cancel){
            db.rollback();
            return;
        }
    }
    QSqlQuery insertPartQ(db);
    insertPartQ.prepare("INSERT INTO seriesparts (iseries_id, iseriespart, tsubtitle) VALUES(:sid, :pid, :tsub)");
    insertPartQ.bindValue(":sid", seriesId);
    insertPartQ.bindValue(":pid", partNo);
    insertPartQ.bindValue(":tsub", subtitle);
    int seriesPartId = 0;
    if(!insertPartQ.exec()){
        QMessageBox::critical(this, tr("Error"), tr("Hmm, seems we already have that part of the series... Bailing out!"));
        db.rollback();
        return;
    }
    QSqlQuery partNoQ(db);
    partNoQ.prepare("SELECT iseriesparts_id FROM seriesparts WHERE iseries_id = :sid AND iseriespart = :pid AND tsubtitle = :tsub");
    partNoQ.bindValue(":sid", seriesId);
    partNoQ.bindValue(":pid", partNo);
    partNoQ.bindValue(":tsub", subtitle);
    if(!partNoQ.exec()){
        db.rollback();
        return;
    }
    while(partNoQ.next()){
        seriesPartId = partNoQ.value(0).toInt();
    }
    //since we're in a transaction, we have to have a valid seriespart
    //handle files
    MovieInfoPage *movieInfoPage = qobject_cast<MovieInfoPage*>(page(0));
    WizardTreeModel *wizardModel = movieInfoPage->model();
    QSqlQuery insertFilesQ(db);
    insertFilesQ.prepare("INSERT INTO files (iseriespart_id, tfilename, cmd5sum, bisize, sifiletype, sifileno, siquality, cpicsize, iduration) VALUES(:ipid, :tfn, :md5, :size, :ft, :fno, :qual, :psize, :dur)");
    QSqlQuery insertOriginQ(db);
    insertOriginQ.prepare("INSERT INTO files_origin (ifiles_id, tname, cmd5sum, bisize, ibitrate) VALUES(:oid, :oname, :omd5, :osize, :obitrate)");
    QHash<QString, QString> md5Sums;
    for(int i = 0; i < wizardModel->rowCount(QModelIndex()); ++i){
        QModelIndex curIdx = wizardModel->index(i, 0, QModelIndex());
        QList<QVariant> fData = wizardModel->fileData(curIdx);
        QString fullPath = fData.value(WizardTreeModel::FullPath).toString();
        QFileInfo fi(fullPath);
        qint64 size = fi.size();
        QString md5 = Helper::md5Sum(fullPath);
        md5Sums.insert(fullPath, md5);
        qint64 secs = 0;
        QString picSize;
        int type = fData.value(WizardTreeModel::FileType).toInt();
        QVariant quality;
        QString oName, oMD5;
        qint64 oSize = 0, oldBitrate = 0;
        bool hasOrigin = false;
        if(type == FT_MOVIE){
            QVariantMap m = Helper::ffmpegData(fullPath);
            secs = static_cast<qint64>(m.value("duration").toDouble());
            quality = field("quality").toInt();
            //check for origin
            QModelIndex oIdx = wizardModel->index(0, 0, curIdx);
            if(oIdx.isValid()){
                QList<QVariant> oData = wizardModel->fileData(oIdx);
                QString oFullPath = oData.value(WizardTreeModel::FullPath).toString();
                QFileInfo oFi(oFullPath);
                oName = oFi.fileName();
                oSize = oFi.size();
                oMD5 = Helper::md5Sum(oFullPath);
                QVariantMap oldFfmpeg = Helper::ffmpegData(oFullPath);
                oldBitrate = oldFfmpeg.value("bit_rate").toLongLong();
                oldBitrate /= 1000;
                hasOrigin = true;
            }

        }else{
            QPixmap pix(fullPath);
            picSize = QString("%1x%2").arg(QString::number(pix.width())).arg(QString::number(pix.height()));
        }
        insertFilesQ.bindValue(":ipid", seriesPartId);
        insertFilesQ.bindValue(":tfn", fi.fileName());
        insertFilesQ.bindValue(":md5", md5);
        insertFilesQ.bindValue(":size", size);
        insertFilesQ.bindValue(":ft", type);
        insertFilesQ.bindValue(":fno", fData.value(WizardTreeModel::FilePart));
        insertFilesQ.bindValue(":qual", quality);
        insertFilesQ.bindValue(":psize", picSize);
        insertFilesQ.bindValue(":dur", secs);
        if(insertFilesQ.exec()){
            if(hasOrigin){
                int curFileId = -1;
                QSqlQuery curval("SELECT currval('files_ifiles_id__seq')", db);
                while(curval.next()){
                    curFileId= curval.value(0).toInt();
                }
                insertOriginQ.bindValue(":oid", curFileId);
                insertOriginQ.bindValue(":oname", oName);
                insertOriginQ.bindValue(":omd5", oMD5);
                insertOriginQ.bindValue(":osize", oSize);
                insertOriginQ.bindValue(":obitrate", oldBitrate);
                if(!insertOriginQ.exec()){
                    QMessageBox::critical(this, tr("Error"), tr("Failed to insert old!"));
                    db.rollback();
                    return;
                }
            }
        }else{
            QMessageBox::critical(this, tr("Error"), tr("Craptastic... Something went wrong archiving files."));
            db.rollback();
            return;
        }

    }
    //files have landed
    //handle actors
    MovieMappingPage *actorPage = qobject_cast<MovieMappingPage*>(page(1));
    QStringList actors = actorPage->widget()->items();
    QSqlQuery actorIdQ(db);
    actorIdQ.prepare("SELECT iactors_id FROM actors WHERE tactorname = :name");
    QSqlQuery insertActorQ(db);
    insertActorQ.prepare("INSERT INTO actors (tactorname) VALUES(:name)");
    QSqlQuery actorMapQ(db);
    actorMapQ.prepare("INSERT INTO seriesparts_actormap (iseriesparts_id, iactors_id) VALUES(:pid, :aid)");
    for(QString a : actors){
        a = a.toLower().trimmed();
        int actorId = 0;
        actorIdQ.bindValue(":name", a);
        actorIdQ.exec();
        while(actorIdQ.next()){
            actorId = actorIdQ.value(0).toInt();
        }
        if(!actorId){
            insertActorQ.bindValue(":name", a);
            insertActorQ.exec();
            actorIdQ.bindValue(":name", a);
            actorIdQ.exec();
            while(actorIdQ.next()){
                actorId = actorIdQ.value(0).toInt();
            }
        }
        actorMapQ.bindValue(":pid", seriesPartId);
        actorMapQ.bindValue(":aid", actorId);
        if(!actorMapQ.exec()){
            QMessageBox::critical(this, tr("Error"), tr("Failed to handle actors."));
            db.rollback();
            return;
        }
    }
    //actors are in place!
    //handle genres
    MovieMappingPage *genrePage = qobject_cast<MovieMappingPage*>(page(2));
    QStringList genres = genrePage->widget()->items();
    QSqlQuery genreIdQ(db);
    genreIdQ.prepare("SELECT igenres_id FROM genres WHERE tgenrename = :name");
    QSqlQuery insertGenreQ(db);
    insertGenreQ.prepare("INSERT INTO genres (tgenrename) VALUES(:name)");
    QSqlQuery genreMapQ(db);
    genreMapQ.prepare("INSERT INTO seriesparts_genremap (iseriesparts_id, igenres_id) VALUES(:pid, :gid)");
    for(QString g : genres){
        g = g.toLower().trimmed();
        int genreId = 0;
        genreIdQ.bindValue(":name", g);
        genreIdQ.exec();
        while(genreIdQ.next()){
            genreId = genreIdQ.value(0).toInt();
        }
        if(!genreId){
            insertGenreQ.bindValue(":name", g);
            insertGenreQ.exec();
            genreIdQ.bindValue(":name", g);
            genreIdQ.exec();
            while(genreIdQ.next()){
                genreId = genreIdQ.value(0).toInt();
            }
        }
        genreMapQ.bindValue(":pid", seriesPartId);
        genreMapQ.bindValue(":gid", genreId);
        if(!genreMapQ.exec()){
            QMessageBox::critical(this, tr("Error"), tr("Failed to handle genres."));
            db.rollback();
            return;
        }
    }
    //genres done!
    //now handle metadata
    QSqlQuery insertMetadataQ(db);
    insertMetadataQ.prepare("INSERT INTO metadata (iseriespart_id, sireleaseyear, tsourcemedium, tsubject, treleasegroup, tencoderopts, tcomment, sipasses) VALUES(:pid, :rely, :source, :sub, :group, :encopts, :comment, :passes)");
    MovieMetadataPage *metadataPage = qobject_cast<MovieMetadataPage*>(page(3));
    QList<QVariant> metadata = metadataPage->widget()->metadata();
    insertMetadataQ.bindValue(":pid", seriesPartId);
    insertMetadataQ.bindValue(":rely", metadata.at(ArchiveModel::ReleaseYear));
    insertMetadataQ.bindValue(":source", metadata.at(ArchiveModel::Source));
    insertMetadataQ.bindValue(":sub", metadata.at(ArchiveModel::Subject));
    insertMetadataQ.bindValue(":group", metadata.at(ArchiveModel::ReleaseGroup));
    insertMetadataQ.bindValue(":encopts", metadata.at(ArchiveModel::EncoderOpts));
    insertMetadataQ.bindValue(":comment", metadata.at(ArchiveModel::Comment));
    insertMetadataQ.bindValue(":passes", metadata.at(ArchiveModel::Passes));
    if(!insertMetadataQ.exec()){
        QMessageBox::critical(this, tr("Error"), tr("Failed to insert metadata."));
        db.rollback();
        return;
    }
    //we're still here, good
    //now actually move the files
    for(int i = 0; i < wizardModel->rowCount(QModelIndex()); ++i){
        QModelIndex curIdx = wizardModel->index(i, 0, QModelIndex());
        QList<QVariant> fData = wizardModel->fileData(curIdx);
        QString fullPath = fData.value(WizardTreeModel::FullPath).toString();
        QString md5 = md5Sums.value(fullPath);
        Helper::moveToArchive(fullPath, md5);
    }
    db.commit();
    mInfoPage->saveData();
    const QStringList &origins = mInfoPage->origins();
    if(!origins.isEmpty()){
        QString q = QString("<p>Also delete origins?</p><ul>");
        for(const QString &o : origins){
            q.append(QString("<li>%1</li>").arg(o));
        }
        q.append("</ul>");
        int qRes = QMessageBox::question(this, tr("Delete other files"), q);
        if(qRes == QMessageBox::Yes){
            for(const QString &o : origins){
                QFile::remove(o);
            }
        }
    }
    QWizard::accept();
}