summaryrefslogtreecommitdiffstats
path: root/configurationdialog.cpp
blob: c8e71974fffc41e95477ec1dcf08a73d1a2847e7 (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
/*
  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 <QPushButton>
#include <QTabWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QLineEdit>
#include <QLabel>
#include <QComboBox>
#include <QDirModel>
#include <QCompleter>
#include <QApplication>
#include <QSettings>
#include <QRegExp>
#include <QFileInfo>
#include <QGroupBox>
#include <QCheckBox>
#include <QSpinBox>
#include <QFileInfo>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QRegExpValidator>

#include "configurationdialog.h"
#include "programconfigurator.h"

ConfigurationDialog::ConfigurationDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
	//setup
	mTab = new QTabWidget;
	QDirModel *model = new QDirModel(this);
	QCompleter *fsCompleter = new QCompleter(this);
	fsCompleter->setModel(model);
	fsCompleter->setCompletionMode(QCompleter::PopupCompletion);

	//directories tab
	//directories
	QWidget *pathWidget = new QWidget;
	QGroupBox *pathBox = new QGroupBox(tr("Paths"));
	QGridLayout *pathGrid = new QGridLayout;
	QLabel *pathl1 = new QLabel(tr("Archive directory"));
	mArchiveDir = new QLineEdit;
	mArchiveDir->setCompleter(fsCompleter);
	pathGrid->addWidget(pathl1, 0, 0);
	pathGrid->addWidget(mArchiveDir, 0, 1);
	QLabel *pathl2 = new QLabel(tr("Burn Directory"));
	mBurnDir = new QLineEdit;
	mBurnDir->setCompleter(fsCompleter);
	pathGrid->addWidget(pathl2, 1, 0);
	pathGrid->addWidget(mBurnDir, 1, 1);
	QLabel *pathl3 = new QLabel(tr("Path to ffprobe"));
	mFfProbePath = new QLineEdit;
	mFfProbePath->setCompleter(fsCompleter);
	pathGrid->addWidget(pathl3, 2, 0);
	pathGrid->addWidget(mFfProbePath, 2, 1);
	QLabel *pathl4 = new QLabel(tr("DVD mount directory"));
	mDvdMountPath = new QLineEdit;
	pathGrid->addWidget(pathl4, 3, 0);
	pathGrid->addWidget(mDvdMountPath, 3, 1);
	pathGrid->setAlignment(Qt::AlignTop);
	pathBox->setLayout(pathGrid);
	QVBoxLayout *pathLayout = new QVBoxLayout;
	pathLayout->addWidget(pathBox);
	pathWidget->setLayout(pathLayout);

	//copy path options
	QGroupBox *copyBox = new QGroupBox(tr("Copy path options"));
	QGridLayout *copyGrid = new QGridLayout;
	QLabel *pathl5 = new QLabel(tr("Windows drive"));
	mWindowsDrive = new QLineEdit;
	QRegExp pathValid("[a-z]{1}:");
	pathValid.setCaseSensitivity(Qt::CaseInsensitive);
	QRegExpValidator *pathValidator = new QRegExpValidator(pathValid, this);
	mWindowsDrive->setValidator(pathValidator);
	copyGrid->addWidget(pathl5, 0, 0);
	copyGrid->addWidget(mWindowsDrive, 0, 1);
	QLabel *pathl6 = new QLabel(tr("Strip from path"));
	mStripPath = new QLineEdit;
	copyGrid->addWidget(pathl6, 1, 0);
	copyGrid->addWidget(mStripPath, 1, 1);
	copyGrid->setAlignment(Qt::AlignTop);
	copyBox->setLayout(copyGrid);
	pathLayout->addWidget(copyBox);
	mTab->addTab(pathWidget, tr("Directories"));

	//misc tab
	QWidget *miscWidget = new QWidget;
	QVBoxLayout *miscLayout = new QVBoxLayout;
	miscLayout->setAlignment(Qt::AlignTop);

	//misc - icons
	QGroupBox *iconBox = new QGroupBox(tr("Icon for folders"));
	QHBoxLayout *iconLayout = new QHBoxLayout;
	mIconForFolder = new QComboBox;
	QStringList icons = QStringList() << tr("Dildo") << tr("Normal");
	mIconForFolder->addItems(icons);
	iconLayout->addWidget(mIconForFolder);
	iconBox->setLayout(iconLayout);

	//misc - hover
	QGroupBox *hoverBox = new QGroupBox(tr("Hover options"));
	QVBoxLayout *hoverLayout = new QVBoxLayout;
	mHoverPics = new QCheckBox(tr("Show picture when hovering"));
	hoverLayout->addWidget(mHoverPics);
	mHoverArchive = new QCheckBox(tr("Show further information when hovering over archive"));
	hoverLayout->addWidget(mHoverArchive);
	mHoverOpacity = new QSpinBox;
	mHoverOpacity->setMinimum(1);
	mHoverOpacity->setMaximum(10);
	QLabel *miscl6 = new QLabel(tr("Opacity of hover window"));
	QHBoxLayout *opacityLayout = new QHBoxLayout;
	opacityLayout->addWidget(miscl6);
	opacityLayout->addWidget(mHoverOpacity);
	hoverLayout->addLayout(opacityLayout);
	hoverBox->setLayout(hoverLayout);

	//misc - assemble
	miscLayout->addWidget(iconBox);
	miscLayout->addWidget(hoverBox);
	miscWidget->setLayout(miscLayout);
	mTab->addTab(miscWidget, tr("Misc. settings"));

	// movie viewer
	mMovieConfig = new ProgramConfigurator("movieviewer", "Movie viewer");
	mTab->addTab(mMovieConfig, tr("Movies"));

	// picture viewer
	mPicConfig = new ProgramConfigurator("pictureviewer", "Picture viewer");
	mTab->addTab(mPicConfig, tr("Pictures"));

	//database tab
	QWidget *databaseWidget = new QWidget;
	QGridLayout *dbGrid = new QGridLayout;
	QLabel *dbl1 = new QLabel(tr("Database &host"));
	mDatabaseHost = new QLineEdit;
	dbl1->setBuddy(mDatabaseHost);
	dbGrid->addWidget(dbl1, 0, 0);
	dbGrid->addWidget(mDatabaseHost, 0, 1);
	QLabel *dbl2 = new QLabel(tr("Database &name"));
	mDatabaseName = new QLineEdit;
	dbl2->setBuddy(mDatabaseName);
	dbGrid->addWidget(dbl2, 1, 0);
	dbGrid->addWidget(mDatabaseName, 1, 1);
	QLabel *dbl3 = new QLabel(tr("Database &user"));
	mDatabaseUsername = new QLineEdit;
	dbl3->setBuddy(mDatabaseUsername);
	dbGrid->addWidget(dbl3, 2, 0);
	dbGrid->addWidget(mDatabaseUsername, 2, 1);
	QLabel *dbl4 = new QLabel(tr("Database &password"));
	mDatabasePassword = new QLineEdit;
	dbl4->setBuddy(mDatabasePassword);
	mDatabasePassword->setEchoMode(QLineEdit::Password);
	dbGrid->addWidget(dbl4, 3, 0);
	dbGrid->addWidget(mDatabasePassword, 3, 1);
	dbGrid->addLayout(new QHBoxLayout, 4, 0, 2, 4);
	databaseWidget->setLayout(dbGrid);
	mTab->addTab(databaseWidget, tr("Database"));

	//main layout
	mOk = new QPushButton(tr("Ok"));
	connect(mOk, SIGNAL(clicked()), this, SLOT(accept()));
	mCancel = new QPushButton(tr("Cancel"));
	connect(mCancel, SIGNAL(clicked()), this, SLOT(reject()));
	QHBoxLayout *mainButtonLayout = new QHBoxLayout;
	mainButtonLayout->addStretch();
	mainButtonLayout->addWidget(mOk);
	mainButtonLayout->addWidget(mCancel);
	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(mTab);
	mainLayout->addLayout(mainButtonLayout);
	setLayout(mainLayout);
	QString winTitle = QString(tr("%1 - Configure")).arg(qApp->applicationName());
	setWindowTitle(winTitle);
	readSettings();
}

void ConfigurationDialog::accept(){
	writeSettings();
	QDialog::accept();
}

void ConfigurationDialog::readSettings(){
	QSettings s;

	//read misc
	QString icon = s.value("ui/foldericon", "Normal").toString();
	int pos = mIconForFolder->findText(icon);
	if(pos != -1){
		mIconForFolder->setCurrentIndex(pos);
	}
	mHoverPics->setChecked(s.value("ui/hoverpics", false).toBool());
	mHoverArchive->setChecked(s.value("ui/hoverarchive", false).toBool());
	mHoverOpacity->setValue(s.value("ui/hoveropacity", 10).toInt());

	//read paths
	mArchiveDir->setText(s.value("paths/archivedir").toString());
	mBurnDir->setText(s.value("paths/burn").toString());
	mFfProbePath->setText(s.value("paths/ffprobe").toString());
	mDvdMountPath->setText(s.value("paths/dvdmount").toString());
	mWindowsDrive->setText(s.value("paths/windowsdrive").toString());
	mStripPath->setText(s.value("paths/strippath").toString());

	//read database
	mDatabaseHost->setText(s.value("database/hostname").toString());
	mDatabaseName->setText(s.value("database/dbname").toString());
	mDatabaseUsername->setText(s.value("database/dbuser").toString());
	mDatabasePassword->setText(s.value("database/dbpass").toString());

}

void ConfigurationDialog::writeSettings(){
	QSettings s;

	//write paths
	s.setValue("paths/archivedir", mArchiveDir->text());
	s.setValue("paths/burn", mBurnDir->text());
	QString ffprobe = mFfProbePath->text();
	QFileInfo ffProbeInfo(ffprobe);
	if(ffProbeInfo.exists() && ffProbeInfo.isExecutable()){
		s.setValue("paths/ffprobe", ffprobe);
	}
	if(checkDvdPath()){
		s.setValue("paths/dvdmount", mDvdMountPath->text());
	}
	s.setValue("paths/windowsdrive", mWindowsDrive->text());
	s.setValue("paths/strippath", mStripPath->text());

	s.setValue("ui/folderIcon", mIconForFolder->currentText());
	s.setValue("ui/hoverpics", (mHoverPics->checkState() == Qt::Checked));
	s.setValue("ui/hoverarchive", (mHoverArchive->checkState() == Qt::Checked));
	s.setValue("ui/hoveropacity", mHoverOpacity->value());

	//write database
	s.setValue("database/hostname", mDatabaseHost->text());
	s.setValue("database/dbname", mDatabaseName->text());
	s.setValue("database/dbuser", mDatabaseUsername->text());
	s.setValue("database/dbpass", mDatabasePassword->text());

	//write movies
	mMovieConfig->writeSettings();

	//write pics
	mPicConfig->writeSettings();
}

bool ConfigurationDialog::checkDvdPath(){
	QString path = mDvdMountPath->text();
	if(path.isEmpty()){
		return true;
	}
	QFileInfo mountInfo(path);
	if(!mountInfo.exists() || !mountInfo.isDir()){
		QString message = QString(tr("%1 does not exist or is not a directory. Ignoring.")).arg(path);
		mountError(message);
		return false;
	}
	QFile fstab("/etc/fstab");
	if(!fstab.exists() || !fstab.open(QFile::ReadOnly)){
		mountError(QString());
		return true;
	}
	QTextStream fStream(&fstab);
	QString line;
	bool ok = false;
	do {
		line = fStream.readLine();
		if(line.contains(path)){
			QStringList fsParts = line.split(QRegExp("\\s+"));
			if(fsParts.isEmpty() || (fsParts.size() < 4)){
				continue;
			}
			QStringList options = fsParts.at(3).split(',');
			foreach(QString opt, options){
				if(opt.toLower().trimmed() == "user"){
					ok = true;
					break;
				}
			}
		}
	} while(!line.isNull());
	if(!ok){
		QString message = QString(tr("Didn't find %1 in fstab or it isn't user mountable. Ignoring.")).arg(path);
		mountError(message);
		return false;
	}
	return true;
}

void ConfigurationDialog::mountError(const QString &error){
	if(!error.isEmpty()){
		QMessageBox::critical(this, tr("Mount Path Error"), error);
	}
	QSettings s;
	s.setValue("paths/dvdmount", QString());
}