summaryrefslogtreecommitdiffstats
path: root/configurationdialog.cpp
blob: 95e0f8986b0988247e03eb2c0b2b6b7b4546a22b (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
  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 <QFormLayout>
#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 <QSignalMapper>
#include <QColor>
#include <QColorDialog>
#include <QPalette>
#include <QImage>
#include <QPainter>
#include <QStandardItemModel>
#include <QStandardItem>

#include "configurationdialog.h"
#include "programconfigurator.h"
#include "smglobals.h"
#include "helper.h"

ConfigurationDialog::ConfigurationDialog(QWidget *parent, Qt::WindowFlags f) : SmDialog(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"));
    QFormLayout *pathL = new QFormLayout;
	mArchiveDir = new QLineEdit;
	mArchiveDir->setCompleter(fsCompleter);
    pathL->addRow(tr("Archive directory"), mArchiveDir);
	mBurnDir = new QLineEdit;
	mBurnDir->setCompleter(fsCompleter);
    pathL->addRow(tr("Burn directory"), mBurnDir);
	mFfProbePath = new QLineEdit;
	mFfProbePath->setCompleter(fsCompleter);
    pathL->addRow(tr("Path to ffprobe"), mFfProbePath);
	mFfMpegPath = new QLineEdit;
	mFfMpegPath->setCompleter(fsCompleter);
    pathL->addRow(tr("Path to ffmpeg"), mFfMpegPath);
	mDvdMountPath = new QLineEdit;
    pathL->addRow(tr("DVD mount directory"), mDvdMountPath);
	mCoverPath = new QLineEdit;
	mCoverPath->setCompleter(fsCompleter);
    pathL->addRow(tr("Cover directory"), mCoverPath);
    pathBox->setLayout(pathL);
	QVBoxLayout *pathLayout = new QVBoxLayout;
	pathLayout->addWidget(pathBox);
	pathWidget->setLayout(pathLayout);

	//copy path options
	QGroupBox *copyBox = new QGroupBox(tr("Copy path options"));
    QFormLayout *copyL = new QFormLayout;
	mWindowsDrive = new QLineEdit;
	QRegExp pathValid("[a-z]{1}:");
	pathValid.setCaseSensitivity(Qt::CaseInsensitive);
	QRegExpValidator *pathValidator = new QRegExpValidator(pathValid, this);
	mWindowsDrive->setValidator(pathValidator);
    copyL->addRow(tr("Windows drive"), mWindowsDrive);
	mStripPath = new QLineEdit;
    copyL->addRow(tr("Strip from path"), mStripPath);
    copyBox->setLayout(copyL);
	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;
	mIconModel = new QStandardItemModel;
	const QHash<QString, QString> icons = SmGlobals::instance()->icons();
	for(QHash<QString, QString>::const_iterator it = icons.constBegin(); it != icons.constEnd(); ++it){
		QStandardItem *item = new QStandardItem(QIcon(it.value()), it.key());
		mIconModel->appendRow(item);
	}
	mIconForFolder->setModel(mIconModel);
	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);
	mHoverMovies = new QCheckBox(tr("Show frame when hovering over movie"));
	connect(mHoverMovies, SIGNAL(clicked(bool)), this, SLOT(setGrabFrameEnabled(bool)));
	hoverLayout->addWidget(mHoverMovies);
	QGridLayout *miscInputGrid = new QGridLayout;
	QLabel *miscl6 = new QLabel(tr("Opacity of hover window"));
	miscInputGrid->addWidget(miscl6, 0, 0);
	miscInputGrid->addWidget(mHoverOpacity, 0, 1);
	QLabel *miscl7 = new QLabel(tr("Grab frame at ([hh:mm:ss])"));
	mGrabFrameFrom = new QLineEdit;
	QRegExp frameValid("\\d{2}:\\d{2}:\\d{2}");
	QRegExpValidator *frameValidator = new QRegExpValidator(frameValid, this);
	mGrabFrameFrom->setValidator(frameValidator);
	miscInputGrid->addWidget(miscl7, 1, 0);
	miscInputGrid->addWidget(mGrabFrameFrom, 1, 1);
	QLabel *miscl8 = new QLabel(tr("Y cursor offset (hover)"));
	mCursorOffset = new QSpinBox;
	mCursorOffset->setMinimum(-100);
	mCursorOffset->setMaximum(100);
	miscInputGrid->addWidget(miscl8, 2, 0);
	miscInputGrid->addWidget(mCursorOffset, 2, 1);
	hoverLayout->addLayout(miscInputGrid);
	hoverBox->setLayout(hoverLayout);

	//misc - archiving movies
	QGroupBox *archiveBox = new QGroupBox(tr("Archiving options"));
	QVBoxLayout *archiveLayout = new QVBoxLayout;
	mAutoAddCovers = new QCheckBox(tr("Automatically add covers"));
	archiveLayout->addWidget(mAutoAddCovers);
	mIconizeCovers = new QCheckBox(tr("Iconize covers"));
	archiveLayout->addWidget(mIconizeCovers);
	archiveBox->setLayout(archiveLayout);

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

	// movie viewer
    QGroupBox *movieBox = new QGroupBox(tr("Movie viewer"));
    QHBoxLayout *movieBoxLayout = new QHBoxLayout;
	mMovieConfig = new ProgramConfigurator("movieviewer", "Movie viewer");
    movieBoxLayout->addWidget(mMovieConfig);
    movieBox->setLayout(movieBoxLayout);
    QWidget *movieWidget = new QWidget;
    QVBoxLayout *movieWidgetLayout = new QVBoxLayout;
    movieWidgetLayout->addWidget(movieBox);
    movieWidget->setLayout(movieWidgetLayout);
    mTab->addTab(movieWidget, tr("Movies"));

    //picture viewer - info item
    QGroupBox *picInfoBox = new QGroupBox(tr("Picture info item"));
    mInfoPositionBox = new QComboBox;
    createPositionMap();
    fillPositions(mInfoPositionBox);
    mInfoPositionBox->removeItem(mInfoPositionBox->findData(SideBySide));
    QFormLayout *pvInfoL = new QFormLayout;
    pvInfoL->addRow(tr("Picture info position"), mInfoPositionBox);
    mShowInfoItem = new QCheckBox(tr("Show info item"));
    connect(mShowInfoItem, SIGNAL(toggled(bool)), mInfoPositionBox, SLOT(setEnabled(bool)));
    pvInfoL->addRow(QString(), mShowInfoItem);
    picInfoBox->setLayout(pvInfoL);

    //picture viewer - mapping item
    QGroupBox *picMapBox = new QGroupBox(tr("Picture mappings"));
    mMapPositionBox = new QComboBox;
    fillPositions(mMapPositionBox);
    QFormLayout *pvMapL = new QFormLayout;
    pvMapL->addRow(tr("Picture mapping position"), mMapPositionBox);
    mShowMapItem = new QCheckBox(tr("Show mapping item"));
    connect(mShowMapItem, SIGNAL(toggled(bool)), mMapPositionBox, SLOT(setEnabled(bool)));
    pvMapL->addRow(QString(), mShowMapItem);
    picMapBox->setLayout(pvMapL);

    //picture viewer - layout
    QWidget *picWidget = new QWidget;
    QVBoxLayout *picLayout = new QVBoxLayout;
    picLayout->addWidget(picInfoBox);
    picLayout->addWidget(picMapBox);
    picWidget->setLayout(picLayout);
    picLayout->addStretch();
    mTab->addTab(picWidget, tr("Pictures"));

	//database tab
    QFormLayout *dbFormL = new QFormLayout;
	mDatabaseHost = new QLineEdit;
    dbFormL->addRow(tr("Database &host"), mDatabaseHost);
	mDatabaseName = new QLineEdit;
    dbFormL->addRow(tr("Database &name"), mDatabaseName);
	mDatabaseUsername = new QLineEdit;
    dbFormL->addRow(tr("Database &user"), mDatabaseUsername);
    mDatabasePassword = new QLineEdit;
	mDatabasePassword->setEchoMode(QLineEdit::Password);
    dbFormL->addRow(tr("Database &password"), mDatabasePassword);
    QGroupBox *dbBox = new QGroupBox(tr("Database connection"));
    dbBox->setLayout(dbFormL);
    QWidget *dbWidget = new QWidget;
    QVBoxLayout *dbWidgetLayout = new QVBoxLayout;
    dbWidgetLayout->addWidget(dbBox);
    dbWidget->setLayout(dbWidgetLayout);
    mTab->addTab(dbWidget, tr("Database"));

	//colors tab
	QWidget *colorsWidget = new QWidget;
	QGridLayout *colorGrid = new QGridLayout;
	QSignalMapper *colorMapper = new QSignalMapper(this);
	QLabel *colorl1 = new QLabel(tr("Files marked as seen"));
	mSeenColor = new QLabel;
	mSeenColor->setFrameStyle(QFrame::Sunken | QFrame::Panel);
	mSeenColor->setScaledContents(true);
	mSeenButton = new QPushButton(tr("Choose..."));
	colorGrid->addWidget(colorl1, 0, 0);
	colorGrid->addWidget(mSeenColor, 0, 1);
	colorGrid->addWidget(mSeenButton, 0, 2);
	colorMapper->setMapping(mSeenButton, mSeenColor);
	connect(mSeenButton, SIGNAL(clicked()), colorMapper, SLOT(map()));
	QLabel *colorl2 = new QLabel(tr("Files in clipboard"));
	mClipboardColor = new QLabel;
	mClipboardColor->setFrameStyle(QFrame::Sunken | QFrame::Panel);
	mClipboardColor->setScaledContents(true);
	mClipboardButton = new QPushButton(tr("Choose..."));
	colorGrid->addWidget(colorl2, 1, 0);
	colorGrid->addWidget(mClipboardColor, 1, 1);
	colorGrid->addWidget(mClipboardButton, 1, 2);
	colorMapper->setMapping(mClipboardButton, mClipboardColor);
	connect(mClipboardButton, SIGNAL(clicked()), colorMapper, SLOT(map()));
	QLabel *colorl3 = new QLabel(tr("Base color for rows"));
	mBaseColor = new QLabel;
	mBaseColor->setFrameStyle(QFrame::Sunken | QFrame::Panel);
	mBaseColor->setScaledContents(true);
	mBaseButton = new QPushButton(tr("Choose..."));
	colorGrid->addWidget(colorl3, 2, 0);
	colorGrid->addWidget(mBaseColor, 2, 1);
	colorGrid->addWidget(mBaseButton, 2, 2);
	colorMapper->setMapping(mBaseButton, mBaseColor);
	connect(mBaseButton, SIGNAL(clicked()), colorMapper, SLOT(map()));
	QLabel *colorl4 = new QLabel(tr("Alternate base for rows"));
	mAlternateBaseColor = new QLabel;
	mAlternateBaseColor->setFrameStyle(QFrame::Sunken | QFrame::Panel);
	mAlternateBaseColor->setScaledContents(true);
	mAlternateBaseButton = new QPushButton(tr("Choose..."));
	colorGrid->addWidget(colorl4, 3, 0);
	colorGrid->addWidget(mAlternateBaseColor, 3, 1);
	colorGrid->addWidget(mAlternateBaseButton, 3, 2);
	colorMapper->setMapping(mAlternateBaseButton, mAlternateBaseColor);
	connect(mAlternateBaseButton, SIGNAL(clicked()), colorMapper, SLOT(map()));
	QLabel *colorl5 = new QLabel(tr("Font color"));
	mFontColor = new QLabel;
	mFontColor->setFrameStyle(QFrame::Sunken | QFrame::Panel);
	mFontColor->setScaledContents(true);
	mFontColorButton = new QPushButton(tr("Choose..."));
	colorGrid->addWidget(colorl5, 4, 0);
	colorGrid->addWidget(mFontColor, 4, 1);
	colorGrid->addWidget(mFontColorButton, 4, 2);
	colorMapper->setMapping(mFontColorButton, mFontColor);
	connect(mFontColorButton, SIGNAL(clicked()), colorMapper, SLOT(map()));
	QLabel *colorl6 = new QLabel(tr("Local files"));
	mLocalColor = new QLabel;
	mLocalColor->setFrameStyle(QFrame::Sunken | QFrame::Panel);
	mLocalColor->setScaledContents(true);
	mLocalColorButton = new QPushButton(tr("Choose..."));
	colorGrid->addWidget(colorl6, 5, 0);
	colorGrid->addWidget(mLocalColor, 5, 1);
	colorGrid->addWidget(mLocalColorButton, 5, 2);
	colorMapper->setMapping(mLocalColorButton, mLocalColor);
	connect(mLocalColorButton, SIGNAL(clicked()), colorMapper, SLOT(map()));
	QLabel *colorl7 = new QLabel(tr("Archived Files"));
	mArchivedColor = new QLabel;
	mArchivedColor->setFrameStyle(QFrame::Sunken | QFrame::Panel);
	mArchivedColor->setScaledContents(true);
	mArchivedColorButton = new QPushButton(tr("Choose..."));
	colorGrid->addWidget(colorl7, 6, 0);
	colorGrid->addWidget(mArchivedColor, 6, 1);
	colorGrid->addWidget(mArchivedColorButton, 6, 2);
	colorMapper->setMapping(mArchivedColorButton, mArchivedColor);
	connect(mArchivedColorButton, SIGNAL(clicked()), colorMapper, SLOT(map()));
	QLabel *colorl8 = new QLabel(tr("Favorites"));
	mFavoriteColor = new QLabel;
	mFavoriteColor->setFrameStyle(QFrame::Sunken | QFrame::Panel);
	mFavoriteColor->setScaledContents(true);
	mFavoriteColorButton = new QPushButton(tr("Choose..."));
	colorGrid->addWidget(colorl8, 7, 0);
	colorGrid->addWidget(mFavoriteColor, 7, 1);
	colorGrid->addWidget(mFavoriteColorButton, 7, 2);
	colorMapper->setMapping(mFavoriteColorButton, mFavoriteColor);
	connect(mFavoriteColorButton, SIGNAL(clicked()), colorMapper, SLOT(map()));
    mAlternateColors = new QCheckBox(tr("Use alternating row colors"));
	mAlternateColors->setTristate(false);
	connect(mAlternateColors, SIGNAL(stateChanged(int)), this, SLOT(alternateColorsChanged(int)));
	colorGrid->addWidget(mAlternateColors, 8, 1, 1, 2, Qt::AlignLeft);
    colorsWidget->setLayout(colorGrid);

    QLabel *color9 = new QLabel(tr("PicViewer Background"));
    mPVBgColor = new QLabel;
    mPVBgColor->setFrameStyle(QFrame::Sunken | QFrame::Panel);
    mPVBgColor->setScaledContents(true);
    mPVBgButton = new QPushButton(tr("Choose..."));
    QGridLayout *pvGrid = new QGridLayout;
    pvGrid->addWidget(color9, 0, 0);
    pvGrid->addWidget(mPVBgColor, 0, 1);
    pvGrid->addWidget(mPVBgButton, 0, 2);
    colorMapper->setMapping(mPVBgButton, mPVBgColor);
    connect(mPVBgButton, SIGNAL(clicked()), colorMapper, SLOT(map()));
    mPVGradient = new QCheckBox(tr("Use random gradient as background"));
    mPVGradient->setTristate(false);
    connect(mPVGradient, SIGNAL(stateChanged(int)), this, SLOT(randomGradientChanged(int)));
    pvGrid->addWidget(mPVGradient, 1, 1, 1, 2, Qt::AlignLeft);

    connect(colorMapper, SIGNAL(mapped(QWidget*)), this, SLOT(chooseColor(QWidget*)));
    QGroupBox *treeGB = new QGroupBox(tr("Treeview colors"));
    treeGB->setLayout(colorGrid);
    QGroupBox *pvGB = new QGroupBox(tr("PicViewer colors"));
    pvGB->setLayout(pvGrid);
    QVBoxLayout *colorLayout = new QVBoxLayout;
    colorLayout->addWidget(treeGB);
    colorLayout->addWidget(pvGB);
    colorsWidget->setLayout(colorLayout);
	mTab->addTab(colorsWidget, tr("Colors"));

	//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::setGrabFrameEnabled(bool enabled){
	mGrabFrameFrom->setEnabled(enabled);
}

void ConfigurationDialog::readSettings(){
	QSettings s;

	//read misc
	QString icon = s.value("ui/foldericon", "Dildo").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());
	mHoverMovies->setChecked(s.value("ui/hovermovies", false).toBool());
	mHoverOpacity->setValue(s.value("ui/hoveropacity", 10).toInt());
	mGrabFrameFrom->setText(s.value("ui/grabframe", "00:00:00").toString());
	mGrabFrameFrom->setEnabled(s.value("ui/hovermovies", false).toBool());
	mAutoAddCovers->setChecked(s.value("ui/autoaddcovers", false).toBool());
	mIconizeCovers->setChecked(s.value("ui/iconizecovers", false).toBool());
	mCursorOffset->setValue(s.value("ui/cursoroffset", SmGlobals::instance()->cursorSize().height()).toInt());

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

    //read pictures
    mShowInfoItem->setChecked(s.value("ui/showinfoitem", true).toBool());
    mInfoPositionBox->setCurrentIndex(mInfoPositionBox->findData(s.value("ui/infoposition", TopLeft).toInt()));
    mInfoPositionBox->setEnabled(mShowInfoItem->checkState());
    mShowMapItem->setChecked(s.value("ui/showmapitem", true).toBool());
    mMapPositionBox->setCurrentIndex(mMapPositionBox->findData(s.value("ui/mappingposition", TopRight).toInt()));
    mMapPositionBox->setEnabled(mShowMapItem->checkState());

	//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());

	//read colors
	QVariant seen = s.value("ui/seencolor", Qt::red);
	mColors.insert(mSeenColor, seen.value<QColor>());
	QVariant clip = s.value("ui/clipcolor", Qt::darkBlue);
	mColors.insert(mClipboardColor, clip.value<QColor>());
	QVariant base = s.value("ui/basecolor", qApp->palette().base().color());
	mColors.insert(mBaseColor, base.value<QColor>());
	QVariant alternate = s.value("ui/alternatecolor", qApp->palette().alternateBase().color());
	mColors.insert(mAlternateBaseColor, alternate.value<QColor>());
	QVariant font = s.value("ui/fontcolor", qApp->palette().foreground().color());
	mColors.insert(mFontColor, font.value<QColor>());
	QVariant local = s.value("ui/localcolor", Qt::darkBlue);
	mColors.insert(mLocalColor, local.value<QColor>());
	QVariant archived = s.value("ui/archivedcolor", Qt::black);
	mColors.insert(mArchivedColor, archived.value<QColor>());
	QVariant favorite = s.value("ui/favoritecolor", Qt::red);
	mColors.insert(mFavoriteColor, favorite.value<QColor>());
    QVariant pvbackground = s.value("ui/pvbgcolor", Qt::red);
    mColors.insert(mPVBgColor, pvbackground.value<QColor>());
	QHash<QWidget*, QColor>::const_iterator it;
	for(it = mColors.constBegin(); it != mColors.constEnd(); ++it){
		setColor(it.key());
	}
	bool allowAlternate = s.value("ui/allowalternate", true).toBool();
	mAlternateColors->setChecked(allowAlternate);
	alternateColorsChanged(mAlternateColors->checkState());
    bool useGradient = s.value("ui/pvgradient", false).toBool();
    mPVGradient->setChecked(useGradient);
    randomGradientChanged(mPVGradient->checkState());
}

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);
	}
	QString ffmpeg = mFfMpegPath->text();
	QFileInfo ffMpegInfo(ffmpeg);
	if(ffMpegInfo.exists() && ffMpegInfo.isExecutable()){
		s.setValue("paths/ffmpeg", ffmpeg);
	}
	if(checkDvdPath()){
		s.setValue("paths/dvdmount", mDvdMountPath->text());
	}
	s.setValue("paths/windowsdrive", mWindowsDrive->text());
	s.setValue("paths/strippath", mStripPath->text());
	s.setValue("paths/coverpath", mCoverPath->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());
	s.setValue("ui/hovermovies", (mHoverMovies->checkState() == Qt::Checked));
	s.setValue("ui/grabframe", mGrabFrameFrom->text());
	s.setValue("ui/autoaddcovers", (mAutoAddCovers->checkState() == Qt::Checked));
	s.setValue("ui/iconizecovers", (mIconizeCovers->checkState() == Qt::Checked));
	s.setValue("ui/cursoroffset", mCursorOffset->value());

    //write pictures
    s.setValue("ui/showinfoitem", (mShowInfoItem->checkState() == Qt::Checked));
    QString curPos = mInfoPositionBox->currentText();
    s.setValue("ui/infoposition", mPositionMap.value(curPos));
    s.setValue("ui/showmapitem", (mShowMapItem->checkState() == Qt::Checked));
    curPos = mMapPositionBox->currentText();
    s.setValue("ui/mappingposition", mPositionMap.value(curPos));

	//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 colors
	s.setValue("ui/seencolor", mColors.value(mSeenColor));
	s.setValue("ui/clipcolor", mColors.value(mClipboardColor));
	s.setValue("ui/basecolor", mColors.value(mBaseColor));
	s.setValue("ui/alternatecolor", mColors.value(mAlternateBaseColor));
	s.setValue("ui/fontcolor", mColors.value(mFontColor));
	s.setValue("ui/localcolor", mColors.value(mLocalColor));
	s.setValue("ui/archivedcolor", mColors.value(mArchivedColor));
	s.setValue("ui/favoritecolor", mColors.value(mFavoriteColor));
    s.setValue("ui/pvbgcolor", mColors.value(mPVBgColor));
	s.setValue("ui/allowalternate", mAlternateColors->isChecked());
    s.setValue("ui/pvgradient", mPVGradient->isChecked());
}

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());
}

void ConfigurationDialog::chooseColor(QWidget *label){
	QColor newColor = QColorDialog::getColor(mColors.value(label), this);
	if(newColor.isValid()){
		mColors[label] = newColor;
		setColor(label);
	}
}

void ConfigurationDialog::alternateColorsChanged(int state){
	mBaseButton->setEnabled(state == Qt::Checked);
	mBaseColor->setEnabled(state == Qt::Checked);
	mAlternateBaseButton->setEnabled(state == Qt::Checked);
	mAlternateBaseColor->setEnabled(state == Qt::Checked);
}

void ConfigurationDialog::randomGradientChanged(int state){
    mPVBgButton->setEnabled(state == Qt::Unchecked);
    mPVBgColor->setEnabled(state == Qt::Unchecked);
}

void ConfigurationDialog::setColor(QWidget *label){
	QLabel *curLabel = qobject_cast<QLabel*>(label);
	if(!curLabel){
		return;
	}
	QColor curColor = mColors.value(label);
	QPixmap pm(curLabel->sizeHint());
	pm.fill(curColor);
	curLabel->setPixmap(pm);
}

void ConfigurationDialog::createPositionMap(){
    mPositionMap.insert(tr("Top left"), TopLeft);
    mPositionMap.insert(tr("Top right"), TopRight);
    mPositionMap.insert(tr("Bottom left"), BottomLeft);
    mPositionMap.insert(tr("Bottom right"), BottomRigth);
    mPositionMap.insert(tr("Side by side"), SideBySide);
    mPositionMap.insert(tr("Freely moveable"), Moveable);;
}

void ConfigurationDialog::fillPositions(QComboBox *cb){
    for(QMap<QString, int>::const_iterator it = mPositionMap.constBegin(); it != mPositionMap.constEnd(); ++it){
        cb->addItem(it.key(), it.value());
    }
}