summaryrefslogtreecommitdiffstats
path: root/filestreewidget.cpp
blob: 2b28c93efc911186bc6fcff921b355f96140b690 (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
/*
  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 <QHBoxLayout>
#include <QVBoxLayout>
#include <QSettings>
#include <QMessageBox>
#include <QFile>
#include <QDir>
#include <QContextMenuEvent>
#include <QMenu>
#include <QLabel>
#include <QSpinBox>
#include <QPushButton>
#include <QProcess>
#include <QFileDialog>
#include <QSettings>
#include <QEvent>
#include <QSettings>

#include "filestreewidget.h"
#include "smglobals.h"
#include "filestreemodel.h"
#include "seriestreemodel.h"
#include "helper.h"
#include "pictureviewer.h"
#include "filepropertiesdialog.h"

FilesTreeWidget::FilesTreeWidget(QWidget *parent) : QWidget(parent), mSelectedSize(0){
	//the view
	mView = new FilesTreeView;
	mView->setSelectionMode(QAbstractItemView::ExtendedSelection);
	mView->setEditTriggers(QAbstractItemView::NoEditTriggers);
	mModel = static_cast<FilesTreeModel*>(SmGlobals::instance()->model("FilesModel"));
	mProxy = new FilesTreeSortModel(this);
	mProxy->setSourceModel(mModel);
	mView->setModel(mProxy);
	mView->setSortingEnabled(true);
	QItemSelectionModel *selModel = mView->selectionModel();
	connect(selModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(fileSelectionChanged(QModelIndex,QModelIndex)));
	connect(selModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(fileSelectionChanged(QItemSelection,QItemSelection)));
	connect(mView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(itemDoubleClicked(QModelIndex)));

	//layout
	QHBoxLayout *mainLayout = new QHBoxLayout;
	mainLayout->addWidget(mView);
	setLayout(mainLayout);

	//globals
	mSeriesModel = static_cast<SeriesTreeModel*>(SmGlobals::instance()->model("SeriesModel"));
	mPictureViewer = SmGlobals::instance()->pictureViewer();
}

void FilesTreeWidget::resetSize(){
	mSelectedSize = 0;
	emit sizeChanged(mSelectedSize);
}

void FilesTreeWidget::moveToBurn(){
	QModelIndexList selected = mView->selectionModel()->selectedRows();
	if(selected.isEmpty()){
		return;
	}
	QSettings s;
	QString burnDir = s.value("paths/burn").toString();
	if(burnDir.isEmpty()){
		QMessageBox::critical(this, tr("Error"), tr("No target directory configured."));
		return;
	}
	foreach(QModelIndex i, selected){
		int type = i.data(FilesTreeModel::FileTypeRole).toInt();
		if(type == FilesTreeModel::BackCover || type == FilesTreeModel::FrontCover || type == FilesTreeModel::GeneralCover){
			continue;
		}
		int seriesPartId = i.data(FilesTreeModel::SeriesPartIdRole).toInt();
		int seriesId = mSeriesModel->seriesIdByPartId(seriesPartId);
		if(seriesId != -1){
			QModelIndex seriesIdx = mSeriesModel->findValue(seriesId, QModelIndex(), SeriesTreeModel::SeriesId);
			if(seriesIdx.isValid()){
				QString seriesName = seriesIdx.data(SeriesTreeModel::NameRole).toString();
				QString dirName = seriesName.replace(' ', ".");
				QDir target(burnDir);
				target.mkdir(dirName);
				const QHash<QString, QString> files = mModel->filesBySeriesPartId(seriesPartId);
				foreach(QString name, files.keys()){
					QString sourceFile = Helper::createArchivePath(name, files.value(name));
					QString targetFile = QString("%1/%2/%3").arg(burnDir).arg(dirName).arg(name);
					if(name == i.data(FilesTreeModel::FileNameRole).toString()){
						QFile::rename(sourceFile, targetFile);
					}else{
						QFile::copy(sourceFile, targetFile);
					}
				}
			}
		}
	}
}

void FilesTreeWidget::setDvdNo(){
	DvdNoDialog dlg(this);
	int retval = dlg.exec();
	if(retval == QDialog::Accepted){
		QModelIndexList selected = mView->selectionModel()->selectedRows();
		if(selected.isEmpty()){
			return;
		}
		int dvdNo = dlg.dvdNo();
		foreach(QModelIndex i, selected){
			int type = i.data(FilesTreeModel::FileTypeRole).toInt();
			if(type != FilesTreeModel::Movie){
				continue;
			}
			QModelIndex real = mProxy->mapToSource(i);
			if(real.isValid()){
				QModelIndex dvdColumn = mModel->index(real.row(), FilesTreeModel::DvdNo, real.parent());
				mModel->setData(dvdColumn, dvdNo, Qt::EditRole);
			}
		}
	}
}

void FilesTreeWidget::removeFiles(){
	QModelIndexList selected = mView->selectionModel()->selectedRows();
	if(selected.isEmpty()){
		return;
	}
	QString message = QString(tr("<p>Really delete these file(s):</p><ul>"));
	foreach(QModelIndex i, selected){
		message.append(QString(tr("<li>%1</li>")).arg(i.data(FilesTreeModel::FileNameRole).toString()));
	}
	message.append("</ul>");
	int retval = QMessageBox::critical(this, tr("Question"), message, QMessageBox::Yes | QMessageBox::No);
	if(retval == QMessageBox::Yes){
		QModelIndexList realSelected;
		foreach(QModelIndex i, selected){
			realSelected << mProxy->mapToSource(i);
		}
		mModel->deleteFiles(realSelected);
	}
}

void FilesTreeWidget::moveToDirectory(){
	QModelIndexList selected = mView->selectionModel()->selectedRows();
	if(selected.isEmpty()){
		return;
	}
	QSettings s;
	QString startDir = s.value("paths/selecteddir", QDir::homePath()).toString();
	QString dir = QFileDialog::getExistingDirectory(this, tr("Select directory"), startDir);
	if(!dir.isEmpty()){
		foreach(QModelIndex i, selected){
			QString source = i.data(FilesTreeModel::FullPathRole).toString();
			QString destination = QString("%1/%2").arg(dir).arg(i.data(FilesTreeModel::FileNameRole).toString());
			QFile::rename(source, destination);
		}
	}
}

void FilesTreeWidget::fileProperties(){
	QModelIndexList selected = mView->selectionModel()->selectedRows();
	if(selected.isEmpty()){
		return;
	}
	QModelIndex real = mProxy->mapToSource(selected.at(0));
	if(real.isValid()){
		int dvd = real.data(FilesTreeModel::DvdNoRole).toInt();
		if(dvd != -1){
			QMessageBox::critical(this, tr("Error"), tr("File is not available!"));
			return;
		}
		QString fullPath = real.data(FilesTreeModel::FullPathRole).toString();
		QString mimeType = Helper::mimeType(fullPath);
		FilePropertiesDialog dlg(this);
		dlg.setFileName(real.data(FilesTreeModel::FileNameRole).toString());
		if(mimeType.startsWith("video")){
			QList<QMap<QString, QString> > fileData = mModel->streamInfo(real);
			dlg.setStreamData(fileData);
		}else if(mimeType.startsWith("image")){
			QMap<QString, QString> imageData = mModel->pictureInfo(real);
			dlg.addData("Image data", imageData);
			QMap<QString, QString> textData = mModel->pictureMetaInfo(real);
			if(!textData.isEmpty()){
				dlg.addData("Meta data", textData);
			}
		}
		dlg.exec();
	}

}

void FilesTreeWidget::editQuality(){
	QModelIndex current = mView->selectionModel()->currentIndex();
	if(current.isValid()){
		QModelIndex qualityIndex = mView->model()->index(current.row(), FilesTreeModel::Quality, current.parent());
		mView->edit(qualityIndex);
	}
}

void FilesTreeWidget::fileSelectionChanged(const QModelIndex &current, const QModelIndex &previous){
	Q_UNUSED(previous);
	int seriesPartId = current.data(FilesTreeModel::SeriesPartIdRole).toInt();
	int seriesId = mSeriesModel->seriesIdByPartId(seriesPartId);
	int filePart = current.data(FilesTreeModel::PartNoRole).toInt();
	QModelIndex seriesIdx = mSeriesModel->findValue(seriesId, QModelIndex(), SeriesTreeModel::SeriesId);
	if(seriesIdx.isValid()){
		QModelIndex seriesPartIdx = mSeriesModel->findValue(seriesPartId, seriesIdx, SeriesTreeModel::SeriesPartId);
		QString seriesNumber = QString::number(seriesPartIdx.data(SeriesTreeModel::SeriesPartRole).toInt());
		QString msg;
		if(filePart > 0){
			msg = QString(tr("%1 %2 (%3)")).arg(seriesIdx.data(SeriesTreeModel::NameRole).toString()).arg(seriesNumber).arg(filePart);
		}else{
			msg = QString(tr("%1 %2")).arg(seriesIdx.data(SeriesTreeModel::NameRole).toString()).arg(seriesNumber);
		}
		emit statusMessage(msg);
	}
}

void FilesTreeWidget::itemDoubleClicked(const QModelIndex &index){
	int dvdNo = index.data(FilesTreeModel::DvdNoRole).toInt();
	if(dvdNo != -1){
		QString msg = QString(tr("%1 is archived on DVD %2.")).arg(index.data().toString()).arg(QString::number(index.data(FilesTreeModel::DvdNoRole).toInt()));
		QMessageBox::critical(this, tr("Error"), msg);
		return;
	}
	QString file = index.data(FilesTreeModel::FullPathRole).toString();
	QString mimeType = Helper::mimeType(file);
	if(mimeType.startsWith("image")){
		mPictureViewer->showPic(file);
		return;
	}else{
		QPair<QString, QStringList> data = Helper::programData("movieviewer", QString());
		if(data.first.isEmpty()){
			QMessageBox::critical(this, tr("Error"), tr("No viedeo viewer configured."));
			return;
		}
		QString program = data.first;
		QStringList args = data.second;
		args << file;
		QProcess::startDetached(program, args);
	}
}

void FilesTreeWidget::fileSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected){
	QModelIndexList sel = selected.indexes();
	QModelIndexList desel = deselected.indexes();
	foreach(QModelIndex i, sel){
		if(i.column() == 0){
			mSelectedSize += i.data(FilesTreeModel::SizeRole).toLongLong();
		}
	}
	foreach(QModelIndex i, desel){
		if(i.column() == 0){
			mSelectedSize -= i.data(FilesTreeModel::SizeRole).toLongLong();
		}
	}
	emit sizeChanged(mSelectedSize);
}

FilesTreeView::FilesTreeView(QWidget *parent) : QTreeView(parent){}

void FilesTreeView::contextMenuEvent(QContextMenuEvent *event){
	QMenu ctxMenu;
	foreach(QAction *a, actions()){
		ctxMenu.addAction(a);
	}
	ctxMenu.exec(event->globalPos());
}

FilesTreeSortModel::FilesTreeSortModel(QObject *parent) : QSortFilterProxyModel(parent) {}

bool FilesTreeSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const{
	if(left.parent() == QModelIndex()){
		return false;
	}
	if(left.column() == FilesTreeModel::SizeDisplay){
		return left.data(FilesTreeModel::SizeRole).toLongLong() < right.data(FilesTreeModel::SizeRole).toLongLong();
	}
	if(left.column() == FilesTreeModel::DvdNoRole){
		return left.data(FilesTreeModel::DvdNoRole).toInt() < right.data(FilesTreeModel::DvdNoRole).toInt();
	}
	if(left.column() == FilesTreeModel::DisplayName){
		if(left.data(FilesTreeModel::SeriesNameRole) == right.data(FilesTreeModel::SeriesNameRole)){
			return left.data(FilesTreeModel::SeriesPartRole).toInt() < right.data(FilesTreeModel::SeriesPartRole).toInt();
		}
	}
	return QSortFilterProxyModel::lessThan(left, right);
}

DvdNoDialog::DvdNoDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
	//spin box
	QHBoxLayout *spinBoxLayout = new QHBoxLayout;
	QLabel *l1 = new QLabel(tr("Select &Dvd no."));
	mDvdNo = new QSpinBox;
	l1->setBuddy(mDvdNo);
	spinBoxLayout->addWidget(l1);
	spinBoxLayout->addWidget(mDvdNo);
	mDvdNo->setMinimum(-1);
	SeriesTreeModel *seriesModel = static_cast<SeriesTreeModel*>(SmGlobals::instance()->model("SeriesModel"));
	int nextDvdNo = seriesModel->findNextDvdNo();
	mDvdNo->setValue(nextDvdNo);

	//buttons
	QHBoxLayout *buttonLayout = new QHBoxLayout;
	mOk = new QPushButton(tr("Ok"));
	connect(mOk, SIGNAL(clicked()), this, SLOT(accept()));
	buttonLayout->addStretch();
	buttonLayout->addWidget(mOk);
	mCancel = new QPushButton(tr("Cancel"));
	connect(mCancel, SIGNAL(clicked()), this, SLOT(reject()));
	buttonLayout->addWidget(mCancel);

	//main layout
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addLayout(spinBoxLayout);
	mainLayout->addLayout(buttonLayout);
	setLayout(mainLayout);

	setWindowTitle(tr("Select Dvd no."));
}

int DvdNoDialog::dvdNo() const {
	return mDvdNo->value();
}