summaryrefslogtreecommitdiffstats
path: root/archivebrowser.cpp
blob: 1b1107d02ddc460fdb0eeaa32eb85b95a55301e9 (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
/*
  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 <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QCheckBox>
#include <QSettings>
#include <QMessageBox>
#include <QDir>
#include <QFileInfo>
#include <QSortFilterProxyModel>
#include <QProcess>
#include <QToolBar>
#include <QSplitter>
#include <QProgressDialog>

#include "archivebrowser.h"
#include "archivebrowsermodel.h"
#include "copyworker.h"
#include "smtreeview.h"
#include "smglobals.h"
#include "pictureviewer2.h"
#include "delegates.h"
#include "helper.h"

ArchiveBrowser::ArchiveBrowser(QWidget *parent) : QWidget(parent), mSelectedSize(0), mSelectedItems(0){
    //prep
    mModel = static_cast<ArchiveBrowserModel*>(SmGlobals::instance()->model("BrowserModel"));
    mProxy = new ArchiveBrowserModelProxy;
    mProxy->setSourceModel(mModel);
    mTree = new SmTreeView("ui/archivebrowserheaders");
    mTree->setModel(mProxy);
    mTree->setColumnHidden(ArchiveBrowserModel::GenericId, true);
    mTree->setColumnHidden(ArchiveBrowserModel::NodeType, true);

    mTree->setItemDelegateForColumn(ArchiveBrowserModel::TotalSize, new SizeDelegate(this));
    mTree->setItemDelegateForColumn(ArchiveBrowserModel::FileType, new FileTypeDelegate(this));
    mTree->setSelectionMode(QAbstractItemView::ExtendedSelection);

    mToolBar = new QToolBar;

    //filters + toolbar
    QHBoxLayout *filterLayout = new QHBoxLayout;
    filterLayout->setAlignment(Qt::AlignLeft);
    mQualityFilter = new QComboBox;
    QLabel *filterL = new QLabel(tr("Filters:"));
    QLabel *qualityL = new QLabel(tr("Quality"));
    filterLayout->addWidget(filterL);
    filterLayout->addWidget(qualityL);
    setupQualityFilter();
    connect(mQualityFilter, SIGNAL(currentIndexChanged(QString)), mProxy, SLOT(setQualityFilter(QString)));
    filterLayout->addWidget(mQualityFilter);
    mSizeFilter = new QCheckBox(tr("Size Filter"));
    connect(mSizeFilter, SIGNAL(stateChanged(int)), mProxy, SLOT(setSizeFilter(int)));
    filterLayout->addWidget(mSizeFilter);
    QWidget *filterWidget = new QWidget;
    filterWidget->setLayout(filterLayout);
    mTbSplitter = new QSplitter;
    mTbSplitter->addWidget(filterWidget);
    mTbSplitter->addWidget(mToolBar);

    //connect
    connect(mTree->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(browserSelectionChanged(QItemSelection,QItemSelection)));
    connect(mTree, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(itemDoubleClicked(QModelIndex)));
    connect(mModel, SIGNAL(populated()), this, SLOT(resetAll()));

    //make widget
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(mTbSplitter);
    mainLayout->addWidget(mTree);
    setLayout(mainLayout);
    mTree->setSortingEnabled(true);

    //copyworker
    mUSBProgress = 0;
    mCopyWorker = new CopyWorker(this);
    connect(mCopyWorker, SIGNAL(error(QString)), this, SLOT(copyError(QString)));
    connect(mCopyWorker, SIGNAL(success(QString)), this, SLOT(copySuccess(QString)));
    connect(mCopyWorker, SIGNAL(file(QString)), this, SLOT(setCopyFile(QString)));
}

void ArchiveBrowser::browserSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
    QModelIndexList selectedIdx = selectedRows(selected);
    QModelIndexList deselectedIdx = selectedRows(deselected);
    foreach(QModelIndex sel, selectedIdx){
        mSelectedSize += sel.data(ArchiveBrowserModel::TotalSizeRole).toDouble();
        ++mSelectedItems;
        mModel->setData(sel, true, ArchiveBrowserModel::SelectedRole);
    }
    foreach(QModelIndex desel, deselectedIdx){
        mSelectedSize -= desel.data(ArchiveBrowserModel::TotalSizeRole).toDouble();
        --mSelectedItems;
        mModel->setData(desel, false, ArchiveBrowserModel::SelectedRole);
    }
    emit sizeChanged(mSelectedSize);
    emit itemCountChanged(mSelectedItems);
    qint64 remaining = DVDSIZE - mSelectedSize;
    mProxy->setBytesRemaining(remaining);
}

void ArchiveBrowser::readConfig(){
    mTree->readHeaderConfig();
    QSettings s;
    QString qualFilter = s.value("ui/browserquality", tr("(none)")).toString();
    mQualityFilter->setCurrentText(qualFilter);
    mTbSplitter->restoreState(s.value("ui/abtoolbarsplitter").toByteArray());
}

void ArchiveBrowser::writeSettings(){
    mTree->writeHeaderConfig();
    QSettings s;
    s.setValue("ui/browserquality", mQualityFilter->currentText());
    s.setValue("ui/abtoolbarsplitter", mTbSplitter->saveState());
}

void ArchiveBrowser::moveToBurn() {
    QModelIndexList sel = mTree->selectionModel()->selectedRows();
    if(sel.isEmpty()){
        return;
    }
    QSettings s;
    QString destDirS = s.value("paths/burn").toString();
    QDir burnDir(destDirS);
    if(!burnDir.exists()){
        QString msg = QString(tr("Destination directory %1 does not exist!\nBailing out!")).arg(destDirS);
        QMessageBox::critical(this, tr("Error"), msg);
        return;
    }
    QString msg = QString(tr("<p>This will do the following:</p><p><ul><li>Move %1 file(s) to %2</li><li>Update the DVD no. for %1 files</li></ul></p><p>Continue?</p>")).arg(sel.size()).arg(destDirS);
    int retval = QMessageBox::question(this, tr("Question"), msg, QMessageBox::Yes | QMessageBox::No);
    if(retval == QMessageBox::Yes){
        QList<int> filesToUpdate;
        foreach(QModelIndex idx, sel){
            QString dirName = idx.data(ArchiveBrowserModel::NameRole).toString();
            dirName.replace(' ', '.');
            burnDir.mkdir(dirName);
            QString burnDirS = QString("%1/%2").arg(destDirS).arg(dirName);
            QModelIndex real = mProxy->mapToSource(idx);
            QModelIndexList children = mModel->children(real);
            foreach(QModelIndex child, children){
                QFileInfo current(child.data(ArchiveBrowserModel::FullPathRole).toString());
                int type = child.data(ArchiveBrowserModel::FileTypeRole).toInt();
                QString destination = QString("%1/%2").arg(burnDirS).arg(current.fileName());
                if(type == FT_MOVIE){
                    QFile::rename(current.absoluteFilePath(), destination);
                    filesToUpdate << child.data(ArchiveBrowserModel::GenericIdRole).toInt();
                }else{
                    QFile::copy(current.absoluteFilePath(), destination);
                }
            }

        }
        mModel->updateDVDNo(filesToUpdate);
        mModel->refresh();
        mProxy->setBytesRemaining(0);
    }
}

void ArchiveBrowser::moveToUSB(){
    QModelIndexList sel = mTree->selectionModel()->selectedRows();
    if(sel.isEmpty()){
        return;
    }
    QSettings s;
    QString destDirS = s.value("paths/usb").toString();
    QFileInfo destFi(destDirS);
    if(!destFi.exists() || !destFi.isDir()){
        QString msg = QString(tr("%1 does not exist or isn't a directory!")).arg(destFi.absoluteFilePath());
        QMessageBox::critical(this, tr("Error"), msg);
        return;
    }
    int nextDVDNo = mModel->nextDVDNo();
    QString dvdDirS = QString("DVD_%1").arg(QString::number(nextDVDNo));
    QDir dest(destDirS);
    if(dest.exists(dvdDirS)){
        QString msg = QString(tr("Something fishy is going on: %1 already exists in %2!")).arg(dvdDirS).arg(destDirS);
        QMessageBox::critical(this, tr("Error"), msg);
        return;
    }
    bool mkdir = dest.mkdir(dvdDirS);
    if(!mkdir){
        QString msg = QString(tr("Failed to create %1 in %2!")).arg(dvdDirS).arg(destDirS);
        QMessageBox::critical(this, tr("Error"), msg);
        return;
    }
    // this one is .../DVD_123
    QString finalDir = QString("%1/%2").arg(destDirS).arg(dvdDirS);
    QString msg = QString(tr("<p>This will do the following:</p><p><ul><li>Move %1 file(s) to %2</li><li>Update the DVD no. for %1 files</li></ul></p><p>Continue?</p>")).arg(sel.size()).arg(finalDir);
    int retval = QMessageBox::question(this, tr("Question"), msg, QMessageBox::Yes | QMessageBox::No);
    if(retval == QMessageBox::Yes){
        mCopyWorker->clear();
        QDir finalD(finalDir);
        foreach(QModelIndex idx, sel){
            QString dirName = idx.data(ArchiveBrowserModel::NameRole).toString();
            dirName.replace(' ', '.');
            // this one .../DVD_123/series_name
            // don't check for success, it may already exist!
            finalD.mkdir(dirName);
            QModelIndex real = mProxy->mapToSource(idx);
            QModelIndexList children = mModel->children(real);
            foreach(QModelIndex child, children){
                QString source = child.data(ArchiveBrowserModel::FullPathRole).toString();
                QFileInfo sFi(source);
                QString destination = QString("%1/%2/%3").arg(finalDir).arg(dirName).arg(sFi.fileName());
                mCopyWorker->enqueue(source, destination);
                mCopyWorker->appendData(source, child.data(ArchiveBrowserModel::FileTypeRole));
                mCopyWorker->appendData(source, child.data(ArchiveBrowserModel::GenericIdRole));
            }
        }
        mUSBProgress = new QProgressDialog(this);
        connect(mCopyWorker, SIGNAL(bytesRead(int)), mUSBProgress, SLOT(setValue(int)));
        mUSBProgress->setLabelText(tr("Copying files..."));
        mUSBProgress->setMinimum(0);
        mUSBProgress->setMaximum(mCopyWorker->max());
        mUSBProgress->show();
        mCopyWorker->start();
    }
}

void ArchiveBrowser::refresh() {
    mModel->refresh();
}

void ArchiveBrowser::playSelected(){
    QPair<QString, QStringList> pgdata = Helper::programData("movieviewer");
    QModelIndexList sel = mTree->selectionModel()->selectedRows();
    QStringList movieFiles;
    foreach(QModelIndex idx, sel){
        QModelIndex real = mProxy->mapToSource(idx);
        QModelIndexList children = mModel->children(real);
        foreach(QModelIndex ci, children){
            if(ci.data(ArchiveBrowserModel::FileTypeRole).toInt() == 1){
                movieFiles << ci.data(ArchiveBrowserModel::FullPathRole).toString();
            }
        }
    }
    if(movieFiles.isEmpty()){
        return;
    }
    QString prg = pgdata.first;
    QStringList args = pgdata.second;
    args.append(movieFiles);
    QProcess::startDetached(prg, args);
}

void ArchiveBrowser::itemDoubleClicked(QModelIndex cur){
    if(cur.data(ArchiveBrowserModel::NodeTypeRole).toInt() == ArchiveBrowserModel::SeriesPartNode){
        return;
    }
    if(cur.data(ArchiveBrowserModel::NodeTypeRole).toInt() == ArchiveBrowserModel::FileNode){
        int fileType = cur.data(ArchiveBrowserModel::FileTypeRole).toInt();
        PictureViewer2 *picView = SmGlobals::instance()->pictureViewer();
        if(fileType == FT_MOVIE){
            QPixmap pm = Helper::preview(cur.data(ArchiveBrowserModel::FullPathRole).toString());
            picView->setPixmap(pm);
        }else{
            picView->setFile(cur.data(ArchiveBrowserModel::FullPathRole).toString());
        }
        picView->show();
    }
}

void ArchiveBrowser::setupQualityFilter(){
    mQualityFilter->clear();
    QList<int> qualities = mModel->availableQualities();
    qSort(qualities);
    QStringList qualityList = QStringList() << tr("(none)");
    foreach(int q, qualities){
        qualityList << QString::number(q);
    }
    mQualityFilter->addItems(qualityList);
}

void ArchiveBrowser::resetAll() {
    mTree->selectionModel()->clear();
    mSelectedItems = 0;
    mSelectedSize = 0;
    emit sizeChanged(0);
    emit itemCountChanged(0);
}

void ArchiveBrowser::copyError(QString error){
    mUSBProgress->hide();
    QMessageBox::critical(this, tr("Copy Error"), error);
    mCopyWorker->disconnect(mUSBProgress);
    QProgressDialog *old = mUSBProgress;
    old->deleteLater();
    mUSBProgress = 0;
}

void ArchiveBrowser::copySuccess(QString success){
    mUSBProgress->hide();
    QMessageBox::information(this, tr("Copy Success"), success);
    mCopyWorker->disconnect(mUSBProgress);
    QProgressDialog *old = mUSBProgress;
    old->deleteLater();
    mUSBProgress = 0;
    QString msg = QString(tr("Delete source files and update database?"));
    int q = QMessageBox::question(this, tr("Question"), msg);
    if(q == QMessageBox::Yes){
        QHash<QString, QList<QVariant> > data = mCopyWorker->data();
        QList<int> filesToUpdate;
        foreach(QString source, data.keys()){
            int ft = data.value(source).at(0).toInt();
            if(ft == FT_MOVIE){
                QFile::remove(source);
                filesToUpdate << data.value(source).at(1).toInt();
            }
        }
        mModel->updateDVDNo(filesToUpdate);
        mModel->refresh();
        mProxy->setBytesRemaining(0);
        emit needFSFreeUpdate();
        mSizeFilter->setChecked(false);
    }
}

void ArchiveBrowser::setCopyFile(QString file){
    QString msg = QString(tr("Copying %1")).arg(file);
    mUSBProgress->setLabelText(msg);
}

QModelIndexList ArchiveBrowser::selectedRows(const QItemSelection &sel){
    QModelIndexList retval;
    QModelIndexList selIdx = sel.indexes();
    foreach(QModelIndex idx, selIdx){
        QModelIndex real = mProxy->mapToSource(idx);
        if(real.column() == 0){
            retval << real;
        }
    }
    return retval;
}