summaryrefslogtreecommitdiffstats
path: root/archivecontroller.cpp
blob: af05c7aae3f26faebb72dbb8fdf8fc0e73cc0a68 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
  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 <QSettings>
#include <QProcess>
#include <QFileInfo>
#include <QMessageBox>
#include <QInputDialog>
#include <QTextEdit>
#include <QStandardItemModel>
#include <QAction>
#include <QApplication>
#include <QFileDialog>

#include "archivecontroller.h"
#include "archivemodel.h"
#include "archiveview.h"
#include "pictureviewer2.h"
#include "filepropertiesdialog.h"
#include "smglobals.h"
#include "helper.h"

ArchiveController::ArchiveController(QObject *parent) : QObject(parent) {
    if(parent){
        mParentWidget = qobject_cast<QWidget*>(this->parent());
    }
    readConfig();
}

void ArchiveController::setArchiveView(ArchiveView *view){
    mArchiveView = view;
}

void ArchiveController::setArchiveTree(ArchiveTree *atree, ArchiveProxy *aproxy){
    mArchiveTree = atree;
    mArchiveProxy = aproxy;
    mArchiveSelection = mArchiveTree->selectionModel();
}

void ArchiveController::setArchiveFiles(ArchiveFiles *afiles, ArchiveFilesProxy *afilesproxy){
    mArchiveFiles = afiles;
    mArchiveFilesProxy = afilesproxy;
    mFileSelection = mArchiveFiles->selectionModel();
}

void ArchiveController::setModels(ArchiveModel *amodel, ArchiveFilesModel *afilesmodel){
    mArchiveModel = amodel;
    mArchiveFilesModel = afilesmodel;
}

void ArchiveController::setMappingModels(QStandardItemModel *actorModel, QStandardItemModel *genreModel){
    mActorModel = actorModel;
    mGenreModel = genreModel;
}

void ArchiveController::init(){
    connect(mArchiveSelection, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(treeSelectionChanged(QItemSelection,QItemSelection)));
    connect(mArchiveFiles, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(fileDoubleClicked(QModelIndex)));
    connect(mFileSelection, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(fileSelectionChanged(QItemSelection,QItemSelection)));
}

void ArchiveController::setSeriesPart(int seriesPartId){
    QModelIndex res = mArchiveModel->findRecursive(seriesPartId, ArchiveModel::SeriesPartId, mArchiveModel->rootIndex());
    if(res.isValid()){
        mArchiveView->clearFilter();
        QModelIndex real = mArchiveView->archiveProxy()->mapFromSource(res);
        mArchiveTree->scrollTo(real, QAbstractItemView::PositionAtCenter);
        mArchiveTree->selectionModel()->select(real, QItemSelectionModel::Rows | QItemSelectionModel::SelectCurrent);
    }
}

void ArchiveController::playSelectedFiles(){
    QModelIndexList sel = mFileSelection->selectedRows();
    QStringList files;
    foreach(QModelIndex i, sel){
        if(i.data(ArchiveFilesModel::FileTypeRole).toInt() == FT_MOVIE){
            QString fullPath = i.data(ArchiveFilesModel::FullPathRole).toString();
            QFileInfo fi(fullPath);
            if(fi.exists()){
                files << fullPath;
            }
        }
    }
    if(!files.isEmpty()){
        QPair<QString, QStringList> playerData = Helper::programData("movieviewer");
        QStringList args = playerData.second;
        args << files;
        QProcess::startDetached(playerData.first, args);
    }
}

void ArchiveController::editQuality(){
    QModelIndexList sel = mFileSelection->selectedRows(ArchiveFilesModel::Quality);
    if(sel.isEmpty()){
        return;
    }
    bool ok;
    int quality = QInputDialog::getInt(mParentWidget, tr("Set Quality"), tr("Quality"), 7, 1, 10, 1, &ok);
    if(ok){
        foreach(QModelIndex i, sel){
            QModelIndex real = mArchiveFilesProxy->mapToSource(i);
            if(mArchiveFilesModel->isMovie(real)){
                mArchiveFilesModel->setData(real, quality, ArchiveFilesModel::QualityRole);
            }
        }
    }
}

void ArchiveController::editDvdNo(){
    QModelIndexList sel = mFileSelection->selectedRows(ArchiveFilesModel::DvdNo);
    if(sel.isEmpty()){
        return;
    }
    bool ok;
    int dvdNo = QInputDialog::getInt(mParentWidget, tr("Set DVD no."), tr("Number (-1 for local)"), mArchiveFilesModel->nextDvd(), -1, 1024 * 1024, 1, &ok);
    if(ok){
        foreach(QModelIndex i, sel){
            QModelIndex real = mArchiveFilesProxy->mapToSource(i);
            if(mArchiveFilesModel->isMovie(real)){
                mArchiveFilesModel->setData(real, dvdNo, ArchiveFilesModel::DvdNoRole);
            }
        }
    }
}

void ArchiveController::editFileType(){
    QModelIndexList sel = mFileSelection->selectedRows(ArchiveFilesModel::FileType);
    if(sel.isEmpty()){
        return;
    }
    bool ok;
    QStringList types = QStringList() << tr("Movie") << tr("Front Cover") << tr("Back Cover") << tr("General Cover");
    QString item = QInputDialog::getItem(mParentWidget, tr("Set file type"), tr("Type:"), types, 0, false, &ok);
    if(ok && !item.isEmpty()){
        int newType = 0;
        if(item == tr("Movie")){
            newType = FT_MOVIE;
        }else if(item == tr("Front Cover")){
            newType = FT_FRONTCOVER;
        }else if(item == tr("Back Cover")){
            newType = FT_BACKCOVER;
        }else if(item == tr("General Cover")){
            newType = FT_GENERALCOVER;
        }
        if(newType){
            foreach(QModelIndex i, sel){
                QModelIndex real = mArchiveFilesProxy->mapToSource(i);
                mArchiveFilesModel->setData(real, newType, ArchiveFilesModel::FileTypeRole);
            }
            mArchiveFilesModel->refresh();
            mArchiveFiles->expandAll();
        }
    }
}

void ArchiveController::editFileNo(){
    QModelIndexList sel = mFileSelection->selectedRows(ArchiveFilesModel::FileNumber);
    if(sel.isEmpty()){
        return;
    }
    bool ok;
    int fileNo = QInputDialog::getInt(mParentWidget, tr("Set DVD no."), tr("Number (-1 for none)"), -1, -1, 1024 * 1024, 1, &ok);
    if(ok){
        foreach(QModelIndex i, sel){
            QModelIndex real = mArchiveFilesProxy->mapToSource(i);
            if(mArchiveFilesModel->isMovie(real)){
                mArchiveFilesModel->setData(real, fileNo, ArchiveFilesModel::FileNumberRole);
            }
        }
    }
}

void ArchiveController::showProperties(){
    QModelIndexList sel = mFileSelection->selectedRows(ArchiveFilesModel::FullPath);
    if(sel.isEmpty()){
        return;
    }
    QModelIndex first = sel.first();
    FilePropertiesDialog dlg(first.data().toString());
    dlg.exec();
}

void ArchiveController::showPreview(){
    QModelIndexList sel = mFileSelection->selectedRows(ArchiveFilesModel::FullPath);
    if(sel.isEmpty()){
        return;
    }
    QString first = sel.first().data().toString();
    PictureViewer2 *pv = SmGlobals::instance()->pictureViewer();
    pv->setShowMappingItem(false);
    QFileInfo fi(first);
    if(!fi.exists()){
        pv->setFile();
    }else if(sel.first().data(ArchiveFilesModel::FileTypeRole).toInt() == FT_MOVIE){
        qApp->setOverrideCursor(Qt::BusyCursor);
        QPixmap preview = Helper::preview(first);
        if(!preview.isNull()){
            pv->setPixmap(preview);
            pv->show();
        }else{
            pv->setFile();
        }
        qApp->restoreOverrideCursor();
    }else{
        pv->setFile(first);
    }
    pv->show();
}

void ArchiveController::addActionForTree(QAction *a){
    mActionsForTree << a;
    mArchiveTree->addAction(a);
}

void ArchiveController::addFiles(){
    QSettings s;
    QString startDir = s.value("paths/coverpath").toString();
    QStringList covers = QFileDialog::getOpenFileNames(mArchiveView, tr("Select covers"), startDir);
    if(covers.isEmpty()){
        return;
    }
    QModelIndex part = mArchiveSelection->currentIndex();
    if(!part.isValid()){
        QMessageBox::critical(mArchiveView, tr("Error"), tr("No part selected!"));
        return;
    }
    mArchiveModel->addFiles(part.data(ArchiveModel::SeriesPartIdRole).toInt(), covers);
    mArchiveFilesModel->refresh();
    mArchiveFiles->expandAll();
}

void ArchiveController::readConfig(){
    mActorIcon = SmGlobals::instance()->iconFor("actor");
    mGenreIcon = SmGlobals::instance()->iconFor("genre");
    mMetaIcon = SmGlobals::instance()->iconFor("meta");
}

void ArchiveController::moveFilesToSeriespart(const QStringList &md5Sums, int newSeriesPart){
    foreach(QString md5, md5Sums){
        mArchiveFilesModel->updateSeriesPartForFile(md5, newSeriesPart);
    }
    mArchiveFilesModel->refresh();
}

void ArchiveController::treeSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected){
    Q_UNUSED(selected);
    Q_UNUSED(deselected);
    QModelIndexList sel = mapToSource(mArchiveProxy, mArchiveSelection->selectedRows());
    if(sel.isEmpty()){
        return;
    }
    QSet<int> ids;
    foreach(QModelIndex idx, sel){
        ids.unite(mArchiveModel->seriesPartIds(idx));
    }
    mArchiveFilesModel->populate(ids);
    mArchiveFiles->expandToDepth(0);
    mArchiveView->setCurrentArchivePath(mArchiveModel->indexToPath(sel.first()));
    QStringList actors = mArchiveModel->actors(ids);
    mActorModel->clear();
    foreach(QString actor, actors){
        QStandardItem *newItem = new QStandardItem(mActorIcon, actor);
        mActorModel->appendRow(newItem);
    }
    QStringList genres = mArchiveModel->genres(ids);
    mGenreModel->clear();
    foreach(QString genre, genres){
        QStandardItem *newItem = new QStandardItem(mGenreIcon, genre);
        mGenreModel->appendRow(newItem);
    }

    getMetadata(ids);

    int nodeType = sel.first().data(ArchiveModel::TypeRole).toInt();
    foreach(QAction *a, mActionsForTree){
        int aData = a->data().toInt();
        bool enabled = aData & nodeType;
        a->setEnabled(enabled);
    }
}

void ArchiveController::setMetadata(QTextEdit *metaEdit){
    mMetaEdit = metaEdit;
}

void ArchiveController::getMetadata(QSet<int> ids){
    mMetaEdit->clear();
    if(ids.isEmpty()){
        return;
    }

    QString meta;

    if(ids.size() == 1){
        QList<QVariant> metadata = mArchiveModel->metadataList(*ids.begin());
        if(metadata.at(0).isValid()){
            meta.append("<html><body style=\"font-family: courier new; font-weight: bold\"><table>");
            meta.append(QString("<tr><td>Release year</td><td style=\"padding-left: 30px\">%1</td></tr>").arg(metadata.at(ArchiveModel::ReleaseYear).toString()));
            meta.append(QString("<tr><td>Source</td><td style=\"padding-left: 30px\">%1</td></tr>").arg(metadata.at(ArchiveModel::Source).toString()));
            meta.append(QString("<tr><td>Subj./Name</td><td style=\"padding-left: 30px\">%1</td></tr>").arg(metadata.at(ArchiveModel::Subject).toString()));
            meta.append(QString("<tr><td>Added</td><td style=\"padding-left: 30px\">%1</td></tr>").arg(metadata.at(ArchiveModel::Added).toDateTime().toString()));

            meta.append("</table>");
            QString comment = metadata.at(ArchiveModel::Comment).toString();
            if(comment.isEmpty()){
                comment = tr("(none)");
            }
            meta.append(QString("<p style=\"font-style: italic; margin-bottom: 0px\">Comments:</p><p style=\"margin-left: 10px; margin-top: 0px\">%1</p>").arg(comment));
            meta.append("</body></html>");
        }else{
            meta = tr("No metadata available");
        }
    }else{
        QStringList tmpH;
        for(int i = 0; i < ArchiveFilesModel::NumFields; ++i){
            tmpH << "";
        }
        tmpH.reserve(ArchiveFilesModel::NumFields);
        ArchiveFilesModel tmpFModel(tmpH, this);
        tmpFModel.populate(ids);
        quint64 totalSize = tmpFModel.totalSize();
        QString sizeStr;
        QLocale l;
        if(totalSize / 1024 / 1024 / 1024 > 0){ //display GB
            double ts = (double)totalSize / 1024.0 / 1024.0 / 1024.0;
            sizeStr = QString("%1 GB").arg(l.toString(ts));
        }else if(totalSize / 1024 / 1024 > 0){ //display MB
            double ts = (double)totalSize / 1024.0 / 1024.0;
            sizeStr = QString("%1 MB").arg(l.toString(ts));
        }else if(totalSize == 0){
            sizeStr = tr("n/a");
        }
        meta.append("<html><body style=\"font-family: courier new; font-weight: bold\"><table>");
        meta.append(QString("<tr><td>Total Size</td><td style=\"padding-left: 30px\">%1</td></tr>").arg(sizeStr));
        quint64 totalDur = tmpFModel.totalDuration();
        Helper::Duration dur(totalDur);
        meta.append(QString("<tr><td>Total Duration</td><td style=\"padding-left: 30px\">%1</td></tr>").arg(dur.toString()));
        int moviefiles = tmpFModel.movieFilesCount();
        meta.append(QString("<tr><td>Movie files</td><td style=\"padding-left: 30px\">%1</td></tr>").arg(QString::number(moviefiles)));
        int otherfiles = tmpFModel.otherFilesCount();
        meta.append(QString("<tr><td>Other files</td><td style=\"padding-left: 30px\">%1</td></tr>").arg(QString::number(otherfiles)));
        meta.append("</table>");
        meta.append("</body></html>");
    }
    mMetaEdit->setHtml(meta);
}

void ArchiveController::fileSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected){
    Q_UNUSED(selected);
    Q_UNUSED(deselected);
    QModelIndexList sel = mapToSource(mArchiveFilesProxy, mFileSelection->selectedRows());
    if(sel.isEmpty()){
        return;
    }
    qint64 size = 0;
    qint64 duration = 0;
    bool maybeMore = false;
    QSet<int> seriesParts;
    foreach(QModelIndex i, sel){
        seriesParts << i.data(ArchiveFilesModel::SeriesPartIdRole).toInt();
        size += i.data(ArchiveFilesModel::SizeRole).toInt();
        int type = i.data(ArchiveFilesModel::FileTypeRole).toInt();
        if(type == FT_MOVIE){
            int dur = i.data(ArchiveFilesModel::SizeDurRole).toInt();
            duration += dur;
            if(dur == 0){
                maybeMore = true;
            }
        }
    }
    getMetadata(seriesParts);
    emit sizeChanged(size);
    emit durationChanged(duration, maybeMore);
}

void ArchiveController::fileDoubleClicked(const QModelIndex &idx){
    if(!idx.isValid()){
        return;
    }
    int type = idx.data(ArchiveFilesModel::FileTypeRole).toInt();
    if(type == FT_MOVIE){
        QString fullPath = idx.data(ArchiveFilesModel::FullPathRole).toString();
        QFileInfo fi(fullPath);
        if(!fi.exists()){
            QString msg = QString(tr("%1 not available!")).arg(idx.data(ArchiveFilesModel::FilenameRole).toString());
            QMessageBox::critical(mParentWidget, tr("Error"), msg);
            return;
        }
        QPair<QString, QStringList> playerData = Helper::programData("movieviewer");
        QStringList args = playerData.second;
        args << idx.data(ArchiveFilesModel::FullPathRole).toString();
        QProcess::startDetached(playerData.first, args);
        return;
    }
    PictureViewer2 *pv = SmGlobals::instance()->pictureViewer();
    QModelIndex parent = idx.parent();
    QStringList paths;
    if(parent.isValid()){
        int row = 0;
        QModelIndex child = parent.child(row, ArchiveFilesModel::FullPath);
        while(child.isValid()){
            paths << child.data().toString();
            ++row;
            child = parent.child(row, ArchiveFilesModel::FullPath);
        }
    }
    pv->setShowMarkItem(false);
    pv->addFiles(paths);
    pv->selectPic(idx.data(ArchiveFilesModel::FullPathRole).toString());
    pv->show();
}

QModelIndexList ArchiveController::mapToSource(const QSortFilterProxyModel *proxy, const QModelIndexList idxs) const{
    QModelIndexList retval;
    foreach(QModelIndex idx, idxs){
        retval << proxy->mapToSource(idx);
    }
    return retval;
}