OSDN Git Service

Improved processing dialog + setup a proper "big" window icon for application windows...
[lamexp/LameXP.git] / src / Dialog_MainWindow.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Dialog_MainWindow.h"
24
25 //UIC includes
26 #include "../tmp/UIC_MainWindow.h"
27
28 //LameXP includes
29 #include "Global.h"
30 #include "Resource.h"
31 #include "Dialog_WorkingBanner.h"
32 #include "Dialog_MetaInfo.h"
33 #include "Dialog_About.h"
34 #include "Dialog_Update.h"
35 #include "Dialog_DropBox.h"
36 #include "Dialog_CueImport.h"
37 #include "Dialog_LogView.h"
38 #include "Thread_FileAnalyzer.h"
39 #include "Thread_MessageHandler.h"
40 #include "Model_MetaInfo.h"
41 #include "Model_Settings.h"
42 #include "Model_FileList.h"
43 #include "Model_FileSystem.h"
44 #include "WinSevenTaskbar.h"
45 #include "Registry_Encoder.h"
46 #include "Registry_Decoder.h"
47 #include "Encoder_Abstract.h"
48 #include "ShellIntegration.h"
49 #include "CustomEventFilter.h"
50
51 //Qt includes
52 #include <QMessageBox>
53 #include <QTimer>
54 #include <QDesktopWidget>
55 #include <QDate>
56 #include <QFileDialog>
57 #include <QInputDialog>
58 #include <QFileSystemModel>
59 #include <QDesktopServices>
60 #include <QUrl>
61 #include <QPlastiqueStyle>
62 #include <QCleanlooksStyle>
63 #include <QWindowsVistaStyle>
64 #include <QWindowsStyle>
65 #include <QSysInfo>
66 #include <QDragEnterEvent>
67 #include <QMimeData>
68 #include <QProcess>
69 #include <QUuid>
70 #include <QProcessEnvironment>
71 #include <QCryptographicHash>
72 #include <QTranslator>
73 #include <QResource>
74 #include <QScrollBar>
75
76 ////////////////////////////////////////////////////////////
77 // Helper macros
78 ////////////////////////////////////////////////////////////
79
80 #define ABORT_IF_BUSY do \
81 { \
82         if(m_banner->isVisible() || m_delayedFileTimer->isActive()) \
83         { \
84                 lamexp_beep(lamexp_beep_warning); \
85                 return; \
86         } \
87 } \
88 while(0)
89
90 #define SET_TEXT_COLOR(WIDGET, COLOR) do \
91 { \
92         QPalette _palette = WIDGET->palette(); \
93         _palette.setColor(QPalette::WindowText, (COLOR)); \
94         _palette.setColor(QPalette::Text, (COLOR)); \
95         WIDGET->setPalette(_palette); \
96 } \
97 while(0)
98
99 #define SET_FONT_BOLD(WIDGET,BOLD) do \
100 { \
101         QFont _font = WIDGET->font(); \
102         _font.setBold(BOLD); \
103         WIDGET->setFont(_font); \
104 } \
105 while(0)
106
107 #define TEMP_HIDE_DROPBOX(CMD) do \
108 { \
109         bool _dropBoxVisible = m_dropBox->isVisible(); \
110         if(_dropBoxVisible) m_dropBox->hide(); \
111         do { CMD } while(0); \
112         if(_dropBoxVisible) m_dropBox->show(); \
113 } \
114 while(0)
115
116 #define SET_MODEL(VIEW, MODEL) do \
117 { \
118         QItemSelectionModel *_tmp = (VIEW)->selectionModel(); \
119         (VIEW)->setModel(MODEL); \
120         LAMEXP_DELETE(_tmp); \
121 } \
122 while(0)
123
124 #define SET_CHECKBOX_STATE(CHCKBX, STATE) do \
125 { \
126         if((CHCKBX)->isChecked() != (STATE)) \
127         { \
128                 (CHCKBX)->click(); \
129         } \
130         if((CHCKBX)->isChecked() != (STATE)) \
131         { \
132                 qWarning("Warning: Failed to set checkbox " #CHCKBX " state!"); \
133         } \
134 } \
135 while(0)
136
137 #define TRIM_STRING_RIGHT(STR) do \
138 { \
139         while((STR.length() > 0) && STR[STR.length()-1].isSpace()) STR.chop(1); \
140 } \
141 while(0)
142
143 #define MAKE_TRANSPARENT(WIDGET, FLAG) do \
144 { \
145         QPalette _p = (WIDGET)->palette(); \
146         _p.setColor(QPalette::Background, Qt::transparent); \
147         (WIDGET)->setPalette(FLAG ? _p : QPalette()); \
148 } \
149 while(0)
150
151 #define WITH_BLOCKED_SIGNALS(WIDGET, CMD, ...) do \
152 { \
153         const bool _flag = (WIDGET)->blockSignals(true); \
154         (WIDGET)->CMD(__VA_ARGS__); \
155         if(!(_flag)) { (WIDGET)->blockSignals(false); } \
156 } \
157 while(0)
158
159 #define LINK(URL) QString("<a href=\"%1\">%2</a>").arg(URL).arg(QString(URL).replace("-", "&minus;"))
160 #define FSLINK(PATH) QString("<a href=\"file:///%1\">%2</a>").arg(PATH).arg(QString(PATH).replace("-", "&minus;"))
161 //#define USE_NATIVE_FILE_DIALOG (lamexp_themes_enabled() || ((QSysInfo::windowsVersion() & QSysInfo::WV_NT_based) < QSysInfo::WV_XP))
162 #define CENTER_CURRENT_OUTPUT_FOLDER_DELAYED QTimer::singleShot(125, this, SLOT(centerOutputFolderModel()))
163
164 static const unsigned int IDM_ABOUTBOX = 0xEFF0;
165
166 ////////////////////////////////////////////////////////////
167 // Constructor
168 ////////////////////////////////////////////////////////////
169
170 MainWindow::MainWindow(FileListModel *fileListModel, AudioFileModel_MetaInfo *metaInfo, SettingsModel *settingsModel, QWidget *parent)
171 :
172         QMainWindow(parent),
173         ui(new Ui::MainWindow),
174         m_fileListModel(fileListModel),
175         m_metaData(metaInfo),
176         m_settings(settingsModel),
177         m_fileSystemModel(NULL),
178         m_accepted(false),
179         m_firstTimeShown(true),
180         m_outputFolderViewCentering(false),
181         m_outputFolderViewInitCounter(0)
182 {
183         //Init the dialog, from the .ui file
184         ui->setupUi(this);
185         setWindowFlags(windowFlags() ^ Qt::WindowMaximizeButtonHint);
186         
187         //Register meta types
188         qRegisterMetaType<AudioFileModel>("AudioFileModel");
189
190         //Enabled main buttons
191         connect(ui->buttonAbout, SIGNAL(clicked()), this, SLOT(aboutButtonClicked()));
192         connect(ui->buttonStart, SIGNAL(clicked()), this, SLOT(encodeButtonClicked()));
193         connect(ui->buttonQuit, SIGNAL(clicked()), this, SLOT(closeButtonClicked()));
194
195         //Setup tab widget
196         ui->tabWidget->setCurrentIndex(0);
197         connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(tabPageChanged(int)));
198
199         //Add system menu
200         lamexp_append_sysmenu(this, IDM_ABOUTBOX, "About...");
201
202         //--------------------------------
203         // Setup "Source" tab
204         //--------------------------------
205
206         ui->sourceFileView->setModel(m_fileListModel);
207         ui->sourceFileView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
208         ui->sourceFileView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
209         ui->sourceFileView->setContextMenuPolicy(Qt::CustomContextMenu);
210         ui->sourceFileView->viewport()->installEventFilter(this);
211         m_dropNoteLabel = new QLabel(ui->sourceFileView);
212         m_dropNoteLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
213         SET_FONT_BOLD(m_dropNoteLabel, true);
214         SET_TEXT_COLOR(m_dropNoteLabel, Qt::darkGray);
215         m_sourceFilesContextMenu = new QMenu();
216         m_showDetailsContextAction = m_sourceFilesContextMenu->addAction(QIcon(":/icons/zoom.png"), "N/A");
217         m_previewContextAction = m_sourceFilesContextMenu->addAction(QIcon(":/icons/sound.png"), "N/A");
218         m_findFileContextAction = m_sourceFilesContextMenu->addAction(QIcon(":/icons/folder_go.png"), "N/A");
219         m_sourceFilesContextMenu->addSeparator();
220         m_exportCsvContextAction = m_sourceFilesContextMenu->addAction(QIcon(":/icons/table_save.png"), "N/A");
221         m_importCsvContextAction = m_sourceFilesContextMenu->addAction(QIcon(":/icons/folder_table.png"), "N/A");
222         SET_FONT_BOLD(m_showDetailsContextAction, true);
223         connect(ui->buttonAddFiles, SIGNAL(clicked()), this, SLOT(addFilesButtonClicked()));
224         connect(ui->buttonRemoveFile, SIGNAL(clicked()), this, SLOT(removeFileButtonClicked()));
225         connect(ui->buttonClearFiles, SIGNAL(clicked()), this, SLOT(clearFilesButtonClicked()));
226         connect(ui->buttonFileUp, SIGNAL(clicked()), this, SLOT(fileUpButtonClicked()));
227         connect(ui->buttonFileDown, SIGNAL(clicked()), this, SLOT(fileDownButtonClicked()));
228         connect(ui->buttonShowDetails, SIGNAL(clicked()), this, SLOT(showDetailsButtonClicked()));
229         connect(m_fileListModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(sourceModelChanged()));
230         connect(m_fileListModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(sourceModelChanged()));
231         connect(m_fileListModel, SIGNAL(modelReset()), this, SLOT(sourceModelChanged()));
232         connect(ui->sourceFileView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(sourceFilesContextMenu(QPoint)));
233         connect(ui->sourceFileView->verticalScrollBar(), SIGNAL(sliderMoved(int)), this, SLOT(sourceFilesScrollbarMoved(int)));
234         connect(ui->sourceFileView->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(sourceFilesScrollbarMoved(int)));
235         connect(m_showDetailsContextAction, SIGNAL(triggered(bool)), this, SLOT(showDetailsButtonClicked()));
236         connect(m_previewContextAction, SIGNAL(triggered(bool)), this, SLOT(previewContextActionTriggered()));
237         connect(m_findFileContextAction, SIGNAL(triggered(bool)), this, SLOT(findFileContextActionTriggered()));
238         connect(m_exportCsvContextAction, SIGNAL(triggered(bool)), this, SLOT(exportCsvContextActionTriggered()));
239         connect(m_importCsvContextAction, SIGNAL(triggered(bool)), this, SLOT(importCsvContextActionTriggered()));
240
241         //--------------------------------
242         // Setup "Output" tab
243         //--------------------------------
244
245         ui->outputFolderView->setHeaderHidden(true);
246         ui->outputFolderView->setAnimated(false);
247         ui->outputFolderView->setMouseTracking(false);
248         ui->outputFolderView->setContextMenuPolicy(Qt::CustomContextMenu);
249         ui->outputFolderView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
250
251         m_evenFilterOutputFolderMouse = new CustomEventFilter;
252         ui->outputFoldersGoUpLabel->installEventFilter(m_evenFilterOutputFolderMouse);
253         ui->outputFoldersEditorLabel->installEventFilter(m_evenFilterOutputFolderMouse);
254         ui->outputFoldersFovoritesLabel->installEventFilter(m_evenFilterOutputFolderMouse);
255         ui->outputFolderLabel->installEventFilter(m_evenFilterOutputFolderMouse);
256
257         m_evenFilterOutputFolderView = new CustomEventFilter;
258         ui->outputFolderView->installEventFilter(m_evenFilterOutputFolderView);
259
260         SET_CHECKBOX_STATE(ui->saveToSourceFolderCheckBox, m_settings->outputToSourceDir());
261         ui->prependRelativePathCheckBox->setChecked(m_settings->prependRelativeSourcePath());
262         
263         connect(ui->outputFolderView, SIGNAL(clicked(QModelIndex)), this, SLOT(outputFolderViewClicked(QModelIndex)));
264         connect(ui->outputFolderView, SIGNAL(activated(QModelIndex)), this, SLOT(outputFolderViewClicked(QModelIndex)));
265         connect(ui->outputFolderView, SIGNAL(pressed(QModelIndex)), this, SLOT(outputFolderViewClicked(QModelIndex)));
266         connect(ui->outputFolderView, SIGNAL(entered(QModelIndex)), this, SLOT(outputFolderViewMoved(QModelIndex)));
267         connect(ui->outputFolderView, SIGNAL(expanded(QModelIndex)), this, SLOT(outputFolderItemExpanded(QModelIndex)));
268         connect(ui->buttonMakeFolder, SIGNAL(clicked()), this, SLOT(makeFolderButtonClicked()));
269         connect(ui->buttonGotoHome, SIGNAL(clicked()), SLOT(gotoHomeFolderButtonClicked()));
270         connect(ui->buttonGotoDesktop, SIGNAL(clicked()), this, SLOT(gotoDesktopButtonClicked()));
271         connect(ui->buttonGotoMusic, SIGNAL(clicked()), this, SLOT(gotoMusicFolderButtonClicked()));
272         connect(ui->saveToSourceFolderCheckBox, SIGNAL(clicked()), this, SLOT(saveToSourceFolderChanged()));
273         connect(ui->prependRelativePathCheckBox, SIGNAL(clicked()), this, SLOT(prependRelativePathChanged()));
274         connect(ui->outputFolderEdit, SIGNAL(editingFinished()), this, SLOT(outputFolderEditFinished()));
275         connect(m_evenFilterOutputFolderMouse, SIGNAL(eventOccurred(QWidget*, QEvent*)), this, SLOT(outputFolderMouseEventOccurred(QWidget*, QEvent*)));
276         connect(m_evenFilterOutputFolderView, SIGNAL(eventOccurred(QWidget*, QEvent*)), this, SLOT(outputFolderViewEventOccurred(QWidget*, QEvent*)));
277
278         if(m_outputFolderContextMenu = new QMenu())
279         {
280                 m_showFolderContextAction = m_outputFolderContextMenu->addAction(QIcon(":/icons/zoom.png"), "N/A");
281                 m_goUpFolderContextAction = m_outputFolderContextMenu->addAction(QIcon(":/icons/folder_up.png"), "N/A");
282                 m_outputFolderContextMenu->addSeparator();
283                 m_refreshFolderContextAction = m_outputFolderContextMenu->addAction(QIcon(":/icons/arrow_refresh.png"), "N/A");
284                 m_outputFolderContextMenu->setDefaultAction(m_showFolderContextAction);
285                 connect(ui->outputFolderView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(outputFolderContextMenu(QPoint)));
286                 connect(m_showFolderContextAction, SIGNAL(triggered(bool)), this, SLOT(showFolderContextActionTriggered()));
287                 connect(m_refreshFolderContextAction, SIGNAL(triggered(bool)), this, SLOT(refreshFolderContextActionTriggered()));
288                 connect(m_goUpFolderContextAction, SIGNAL(triggered(bool)), this, SLOT(goUpFolderContextActionTriggered()));
289         }
290
291         if(m_outputFolderFavoritesMenu = new QMenu())
292         {
293                 m_addFavoriteFolderAction = m_outputFolderFavoritesMenu->addAction(QIcon(":/icons/add.png"), "N/A");
294                 m_outputFolderFavoritesMenu->insertSeparator(m_addFavoriteFolderAction);
295                 connect(m_addFavoriteFolderAction, SIGNAL(triggered(bool)), this, SLOT(addFavoriteFolderActionTriggered()));
296         }
297
298         ui->outputFolderEdit->setVisible(false);
299         if(m_outputFolderNoteBox = new QLabel(ui->outputFolderView))
300         {
301                 m_outputFolderNoteBox->setAutoFillBackground(true);
302                 m_outputFolderNoteBox->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
303                 m_outputFolderNoteBox->setFrameShape(QFrame::StyledPanel);
304                 SET_FONT_BOLD(m_outputFolderNoteBox, true);
305                 m_outputFolderNoteBox->hide();
306
307         }
308
309         outputFolderViewClicked(QModelIndex());
310         refreshFavorites();
311         
312         //--------------------------------
313         // Setup "Meta Data" tab
314         //--------------------------------
315
316         m_metaInfoModel = new MetaInfoModel(m_metaData);
317         m_metaInfoModel->clearData();
318         m_metaInfoModel->setData(m_metaInfoModel->index(4, 1), m_settings->metaInfoPosition());
319         ui->metaDataView->setModel(m_metaInfoModel);
320         ui->metaDataView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
321         ui->metaDataView->verticalHeader()->hide();
322         ui->metaDataView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
323         SET_CHECKBOX_STATE(ui->writeMetaDataCheckBox, m_settings->writeMetaTags());
324         ui->generatePlaylistCheckBox->setChecked(m_settings->createPlaylist());
325         connect(ui->buttonEditMeta, SIGNAL(clicked()), this, SLOT(editMetaButtonClicked()));
326         connect(ui->buttonClearMeta, SIGNAL(clicked()), this, SLOT(clearMetaButtonClicked()));
327         connect(ui->writeMetaDataCheckBox, SIGNAL(clicked()), this, SLOT(metaTagsEnabledChanged()));
328         connect(ui->generatePlaylistCheckBox, SIGNAL(clicked()), this, SLOT(playlistEnabledChanged()));
329
330         //--------------------------------
331         //Setup "Compression" tab
332         //--------------------------------
333
334         m_encoderButtonGroup = new QButtonGroup(this);
335         m_encoderButtonGroup->addButton(ui->radioButtonEncoderMP3, SettingsModel::MP3Encoder);
336         m_encoderButtonGroup->addButton(ui->radioButtonEncoderVorbis, SettingsModel::VorbisEncoder);
337         m_encoderButtonGroup->addButton(ui->radioButtonEncoderAAC, SettingsModel::AACEncoder);
338         m_encoderButtonGroup->addButton(ui->radioButtonEncoderAC3, SettingsModel::AC3Encoder);
339         m_encoderButtonGroup->addButton(ui->radioButtonEncoderFLAC, SettingsModel::FLACEncoder);
340         m_encoderButtonGroup->addButton(ui->radioButtonEncoderOpus, SettingsModel::OpusEncoder);
341         m_encoderButtonGroup->addButton(ui->radioButtonEncoderDCA, SettingsModel::DCAEncoder);
342         m_encoderButtonGroup->addButton(ui->radioButtonEncoderPCM, SettingsModel::PCMEncoder);
343
344         const int aacEncoder = EncoderRegistry::getAacEncoder();
345         ui->radioButtonEncoderAAC->setEnabled(aacEncoder > SettingsModel::AAC_ENCODER_NONE);
346
347         m_modeButtonGroup = new QButtonGroup(this);
348         m_modeButtonGroup->addButton(ui->radioButtonModeQuality, SettingsModel::VBRMode);
349         m_modeButtonGroup->addButton(ui->radioButtonModeAverageBitrate, SettingsModel::ABRMode);
350         m_modeButtonGroup->addButton(ui->radioButtonConstBitrate, SettingsModel::CBRMode);
351
352         ui->radioButtonEncoderMP3->setChecked(true);
353         foreach(QAbstractButton *currentButton, m_encoderButtonGroup->buttons())
354         {
355                 if(currentButton->isEnabled() && (m_encoderButtonGroup->id(currentButton) == m_settings->compressionEncoder()))
356                 {
357                         currentButton->setChecked(true);
358                         break;
359                 }
360         }
361
362         m_evenFilterCompressionTab = new CustomEventFilter();
363         ui->labelCompressionHelp->installEventFilter(m_evenFilterCompressionTab);
364         ui->labelResetEncoders ->installEventFilter(m_evenFilterCompressionTab);
365
366         connect(m_encoderButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(updateEncoder(int)));
367         connect(m_modeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(updateRCMode(int)));
368         connect(m_evenFilterCompressionTab, SIGNAL(eventOccurred(QWidget*, QEvent*)), this, SLOT(compressionTabEventOccurred(QWidget*, QEvent*)));
369         connect(ui->sliderBitrate, SIGNAL(valueChanged(int)), this, SLOT(updateBitrate(int)));
370
371         updateEncoder(m_encoderButtonGroup->checkedId());
372
373         //--------------------------------
374         //Setup "Advanced Options" tab
375         //--------------------------------
376
377         ui->sliderLameAlgoQuality->setValue(m_settings->lameAlgoQuality());
378         if(m_settings->maximumInstances() > 0) ui->sliderMaxInstances->setValue(m_settings->maximumInstances());
379
380         ui->spinBoxBitrateManagementMin->setValue(m_settings->bitrateManagementMinRate());
381         ui->spinBoxBitrateManagementMax->setValue(m_settings->bitrateManagementMaxRate());
382         ui->spinBoxNormalizationFilter->setValue(static_cast<double>(m_settings->normalizationFilterMaxVolume()) / 100.0);
383         ui->spinBoxToneAdjustBass->setValue(static_cast<double>(m_settings->toneAdjustBass()) / 100.0);
384         ui->spinBoxToneAdjustTreble->setValue(static_cast<double>(m_settings->toneAdjustTreble()) / 100.0);
385         ui->spinBoxAftenSearchSize->setValue(m_settings->aftenExponentSearchSize());
386         ui->spinBoxOpusComplexity->setValue(m_settings->opusComplexity());
387         
388         ui->comboBoxMP3ChannelMode->setCurrentIndex(m_settings->lameChannelMode());
389         ui->comboBoxSamplingRate->setCurrentIndex(m_settings->samplingRate());
390         ui->comboBoxAACProfile->setCurrentIndex(m_settings->aacEncProfile());
391         ui->comboBoxAftenCodingMode->setCurrentIndex(m_settings->aftenAudioCodingMode());
392         ui->comboBoxAftenDRCMode->setCurrentIndex(m_settings->aftenDynamicRangeCompression());
393         ui->comboBoxNormalizationMode->setCurrentIndex(m_settings->normalizationFilterEQMode());
394         //comboBoxOpusOptimize->setCurrentIndex(m_settings->opusOptimizeFor());
395         ui->comboBoxOpusFramesize->setCurrentIndex(m_settings->opusFramesize());
396         
397         SET_CHECKBOX_STATE(ui->checkBoxBitrateManagement, m_settings->bitrateManagementEnabled());
398         SET_CHECKBOX_STATE(ui->checkBoxNeroAAC2PassMode, m_settings->neroAACEnable2Pass());
399         SET_CHECKBOX_STATE(ui->checkBoxAftenFastAllocation, m_settings->aftenFastBitAllocation());
400         SET_CHECKBOX_STATE(ui->checkBoxNormalizationFilter, m_settings->normalizationFilterEnabled());
401         SET_CHECKBOX_STATE(ui->checkBoxAutoDetectInstances, (m_settings->maximumInstances() < 1));
402         SET_CHECKBOX_STATE(ui->checkBoxUseSystemTempFolder, !m_settings->customTempPathEnabled());
403         SET_CHECKBOX_STATE(ui->checkBoxRenameOutput, m_settings->renameOutputFilesEnabled());
404         SET_CHECKBOX_STATE(ui->checkBoxForceStereoDownmix, m_settings->forceStereoDownmix());
405         SET_CHECKBOX_STATE(ui->checkBoxOpusDisableResample, m_settings->opusDisableResample());
406         ui->checkBoxNeroAAC2PassMode->setEnabled(aacEncoder == SettingsModel::AAC_ENCODER_NERO);
407         
408         ui->lineEditCustomParamLAME   ->setText(EncoderRegistry::loadEncoderCustomParams(m_settings, SettingsModel::MP3Encoder));
409         ui->lineEditCustomParamOggEnc ->setText(EncoderRegistry::loadEncoderCustomParams(m_settings, SettingsModel::VorbisEncoder));
410         ui->lineEditCustomParamNeroAAC->setText(EncoderRegistry::loadEncoderCustomParams(m_settings, SettingsModel::AACEncoder));
411         ui->lineEditCustomParamFLAC   ->setText(EncoderRegistry::loadEncoderCustomParams(m_settings, SettingsModel::FLACEncoder));
412         ui->lineEditCustomParamAften  ->setText(EncoderRegistry::loadEncoderCustomParams(m_settings, SettingsModel::AC3Encoder));
413         ui->lineEditCustomParamOpus   ->setText(EncoderRegistry::loadEncoderCustomParams(m_settings, SettingsModel::OpusEncoder));
414         ui->lineEditCustomTempFolder  ->setText(QDir::toNativeSeparators(m_settings->customTempPath()));
415         ui->lineEditRenamePattern     ->setText(m_settings->renameOutputFilesPattern());
416         
417         m_evenFilterCustumParamsHelp = new CustomEventFilter();
418         ui->helpCustomParamLAME->installEventFilter(m_evenFilterCustumParamsHelp);
419         ui->helpCustomParamOggEnc->installEventFilter(m_evenFilterCustumParamsHelp);
420         ui->helpCustomParamNeroAAC->installEventFilter(m_evenFilterCustumParamsHelp);
421         ui->helpCustomParamFLAC->installEventFilter(m_evenFilterCustumParamsHelp);
422         ui->helpCustomParamAften->installEventFilter(m_evenFilterCustumParamsHelp);
423         ui->helpCustomParamOpus->installEventFilter(m_evenFilterCustumParamsHelp);
424         
425         m_overwriteButtonGroup = new QButtonGroup(this);
426         m_overwriteButtonGroup->addButton(ui->radioButtonOverwriteModeKeepBoth, SettingsModel::Overwrite_KeepBoth);
427         m_overwriteButtonGroup->addButton(ui->radioButtonOverwriteModeSkipFile, SettingsModel::Overwrite_SkipFile);
428         m_overwriteButtonGroup->addButton(ui->radioButtonOverwriteModeReplaces, SettingsModel::Overwrite_Replaces);
429
430         ui->radioButtonOverwriteModeKeepBoth->setChecked(m_settings->overwriteMode() == SettingsModel::Overwrite_KeepBoth);
431         ui->radioButtonOverwriteModeSkipFile->setChecked(m_settings->overwriteMode() == SettingsModel::Overwrite_SkipFile);
432         ui->radioButtonOverwriteModeReplaces->setChecked(m_settings->overwriteMode() == SettingsModel::Overwrite_Replaces);
433
434         connect(ui->sliderLameAlgoQuality, SIGNAL(valueChanged(int)), this, SLOT(updateLameAlgoQuality(int)));
435         connect(ui->checkBoxBitrateManagement, SIGNAL(clicked(bool)), this, SLOT(bitrateManagementEnabledChanged(bool)));
436         connect(ui->spinBoxBitrateManagementMin, SIGNAL(valueChanged(int)), this, SLOT(bitrateManagementMinChanged(int)));
437         connect(ui->spinBoxBitrateManagementMax, SIGNAL(valueChanged(int)), this, SLOT(bitrateManagementMaxChanged(int)));
438         connect(ui->comboBoxMP3ChannelMode, SIGNAL(currentIndexChanged(int)), this, SLOT(channelModeChanged(int)));
439         connect(ui->comboBoxSamplingRate, SIGNAL(currentIndexChanged(int)), this, SLOT(samplingRateChanged(int)));
440         connect(ui->checkBoxNeroAAC2PassMode, SIGNAL(clicked(bool)), this, SLOT(neroAAC2PassChanged(bool)));
441         connect(ui->comboBoxAACProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(neroAACProfileChanged(int)));
442         connect(ui->checkBoxNormalizationFilter, SIGNAL(clicked(bool)), this, SLOT(normalizationEnabledChanged(bool)));
443         connect(ui->comboBoxAftenCodingMode, SIGNAL(currentIndexChanged(int)), this, SLOT(aftenCodingModeChanged(int)));
444         connect(ui->comboBoxAftenDRCMode, SIGNAL(currentIndexChanged(int)), this, SLOT(aftenDRCModeChanged(int)));
445         connect(ui->spinBoxAftenSearchSize, SIGNAL(valueChanged(int)), this, SLOT(aftenSearchSizeChanged(int)));
446         connect(ui->checkBoxAftenFastAllocation, SIGNAL(clicked(bool)), this, SLOT(aftenFastAllocationChanged(bool)));
447         connect(ui->spinBoxNormalizationFilter, SIGNAL(valueChanged(double)), this, SLOT(normalizationMaxVolumeChanged(double)));
448         connect(ui->comboBoxNormalizationMode, SIGNAL(currentIndexChanged(int)), this, SLOT(normalizationModeChanged(int)));
449         connect(ui->spinBoxToneAdjustBass, SIGNAL(valueChanged(double)), this, SLOT(toneAdjustBassChanged(double)));
450         connect(ui->spinBoxToneAdjustTreble, SIGNAL(valueChanged(double)), this, SLOT(toneAdjustTrebleChanged(double)));
451         connect(ui->buttonToneAdjustReset, SIGNAL(clicked()), this, SLOT(toneAdjustTrebleReset()));
452         connect(ui->lineEditCustomParamLAME, SIGNAL(editingFinished()), this, SLOT(customParamsChanged()));
453         connect(ui->lineEditCustomParamOggEnc, SIGNAL(editingFinished()), this, SLOT(customParamsChanged()));
454         connect(ui->lineEditCustomParamNeroAAC, SIGNAL(editingFinished()), this, SLOT(customParamsChanged()));
455         connect(ui->lineEditCustomParamFLAC, SIGNAL(editingFinished()), this, SLOT(customParamsChanged()));
456         connect(ui->lineEditCustomParamAften, SIGNAL(editingFinished()), this, SLOT(customParamsChanged()));
457         connect(ui->lineEditCustomParamOpus, SIGNAL(editingFinished()), this, SLOT(customParamsChanged()));
458         connect(ui->sliderMaxInstances, SIGNAL(valueChanged(int)), this, SLOT(updateMaximumInstances(int)));
459         connect(ui->checkBoxAutoDetectInstances, SIGNAL(clicked(bool)), this, SLOT(autoDetectInstancesChanged(bool)));
460         connect(ui->buttonBrowseCustomTempFolder, SIGNAL(clicked()), this, SLOT(browseCustomTempFolderButtonClicked()));
461         connect(ui->lineEditCustomTempFolder, SIGNAL(textChanged(QString)), this, SLOT(customTempFolderChanged(QString)));
462         connect(ui->checkBoxUseSystemTempFolder, SIGNAL(clicked(bool)), this, SLOT(useCustomTempFolderChanged(bool)));
463         connect(ui->buttonResetAdvancedOptions, SIGNAL(clicked()), this, SLOT(resetAdvancedOptionsButtonClicked()));
464         connect(ui->checkBoxRenameOutput, SIGNAL(clicked(bool)), this, SLOT(renameOutputEnabledChanged(bool)));
465         connect(ui->lineEditRenamePattern, SIGNAL(editingFinished()), this, SLOT(renameOutputPatternChanged()));
466         connect(ui->lineEditRenamePattern, SIGNAL(textChanged(QString)), this, SLOT(renameOutputPatternChanged(QString)));
467         connect(ui->labelShowRenameMacros, SIGNAL(linkActivated(QString)), this, SLOT(showRenameMacros(QString)));
468         connect(ui->checkBoxForceStereoDownmix, SIGNAL(clicked(bool)), this, SLOT(forceStereoDownmixEnabledChanged(bool)));
469         connect(ui->comboBoxOpusFramesize, SIGNAL(currentIndexChanged(int)), this, SLOT(opusSettingsChanged()));
470         connect(ui->spinBoxOpusComplexity, SIGNAL(valueChanged(int)), this, SLOT(opusSettingsChanged()));
471         connect(ui->checkBoxOpusDisableResample, SIGNAL(clicked(bool)), SLOT(opusSettingsChanged()));
472         connect(m_overwriteButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(overwriteModeChanged(int)));
473         connect(m_evenFilterCustumParamsHelp, SIGNAL(eventOccurred(QWidget*, QEvent*)), this, SLOT(customParamsHelpRequested(QWidget*, QEvent*)));
474
475         //--------------------------------
476         // Force initial GUI update
477         //--------------------------------
478
479         updateLameAlgoQuality(ui->sliderLameAlgoQuality->value());
480         updateMaximumInstances(ui->sliderMaxInstances->value());
481         toneAdjustTrebleChanged(ui->spinBoxToneAdjustTreble->value());
482         toneAdjustBassChanged(ui->spinBoxToneAdjustBass->value());
483         customParamsChanged();
484         
485         //--------------------------------
486         // Initialize actions
487         //--------------------------------
488
489         //Activate file menu actions
490         ui->actionOpenFolder->setData(QVariant::fromValue<bool>(false));
491         ui->actionOpenFolderRecursively->setData(QVariant::fromValue<bool>(true));
492         connect(ui->actionOpenFolder, SIGNAL(triggered()), this, SLOT(openFolderActionActivated()));
493         connect(ui->actionOpenFolderRecursively, SIGNAL(triggered()), this, SLOT(openFolderActionActivated()));
494
495         //Activate view menu actions
496         m_tabActionGroup = new QActionGroup(this);
497         m_tabActionGroup->addAction(ui->actionSourceFiles);
498         m_tabActionGroup->addAction(ui->actionOutputDirectory);
499         m_tabActionGroup->addAction(ui->actionCompression);
500         m_tabActionGroup->addAction(ui->actionMetaData);
501         m_tabActionGroup->addAction(ui->actionAdvancedOptions);
502         ui->actionSourceFiles->setData(0);
503         ui->actionOutputDirectory->setData(1);
504         ui->actionMetaData->setData(2);
505         ui->actionCompression->setData(3);
506         ui->actionAdvancedOptions->setData(4);
507         ui->actionSourceFiles->setChecked(true);
508         connect(m_tabActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(tabActionActivated(QAction*)));
509
510         //Activate style menu actions
511         m_styleActionGroup = new QActionGroup(this);
512         m_styleActionGroup->addAction(ui->actionStylePlastique);
513         m_styleActionGroup->addAction(ui->actionStyleCleanlooks);
514         m_styleActionGroup->addAction(ui->actionStyleWindowsVista);
515         m_styleActionGroup->addAction(ui->actionStyleWindowsXP);
516         m_styleActionGroup->addAction(ui->actionStyleWindowsClassic);
517         ui->actionStylePlastique->setData(0);
518         ui->actionStyleCleanlooks->setData(1);
519         ui->actionStyleWindowsVista->setData(2);
520         ui->actionStyleWindowsXP->setData(3);
521         ui->actionStyleWindowsClassic->setData(4);
522         ui->actionStylePlastique->setChecked(true);
523         ui->actionStyleWindowsXP->setEnabled((QSysInfo::windowsVersion() & QSysInfo::WV_NT_based) >= QSysInfo::WV_XP && lamexp_themes_enabled());
524         ui->actionStyleWindowsVista->setEnabled((QSysInfo::windowsVersion() & QSysInfo::WV_NT_based) >= QSysInfo::WV_VISTA && lamexp_themes_enabled());
525         connect(m_styleActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(styleActionActivated(QAction*)));
526         styleActionActivated(NULL);
527
528         //Populate the language menu
529         m_languageActionGroup = new QActionGroup(this);
530         QStringList translations = lamexp_query_translations();
531         while(!translations.isEmpty())
532         {
533                 QString langId = translations.takeFirst();
534                 QAction *currentLanguage = new QAction(this);
535                 currentLanguage->setData(langId);
536                 currentLanguage->setText(lamexp_translation_name(langId));
537                 currentLanguage->setIcon(QIcon(QString(":/flags/%1.png").arg(langId)));
538                 currentLanguage->setCheckable(true);
539                 currentLanguage->setChecked(false);
540                 m_languageActionGroup->addAction(currentLanguage);
541                 ui->menuLanguage->insertAction(ui->actionLoadTranslationFromFile, currentLanguage);
542         }
543         ui->menuLanguage->insertSeparator(ui->actionLoadTranslationFromFile);
544         connect(ui->actionLoadTranslationFromFile, SIGNAL(triggered(bool)), this, SLOT(languageFromFileActionActivated(bool)));
545         connect(m_languageActionGroup, SIGNAL(triggered(QAction*)), this, SLOT(languageActionActivated(QAction*)));
546         ui->actionLoadTranslationFromFile->setChecked(false);
547
548         //Activate tools menu actions
549         ui->actionDisableUpdateReminder->setChecked(!m_settings->autoUpdateEnabled());
550         ui->actionDisableSounds->setChecked(!m_settings->soundsEnabled());
551         ui->actionDisableNeroAacNotifications->setChecked(!m_settings->neroAacNotificationsEnabled());
552         ui->actionDisableSlowStartupNotifications->setChecked(!m_settings->antivirNotificationsEnabled());
553         ui->actionDisableShellIntegration->setChecked(!m_settings->shellIntegrationEnabled());
554         ui->actionDisableShellIntegration->setDisabled(lamexp_portable_mode() && ui->actionDisableShellIntegration->isChecked());
555         ui->actionCheckForBetaUpdates->setChecked(m_settings->autoUpdateCheckBeta() || lamexp_version_demo());
556         ui->actionCheckForBetaUpdates->setEnabled(!lamexp_version_demo());
557         ui->actionHibernateComputer->setChecked(m_settings->hibernateComputer());
558         ui->actionHibernateComputer->setEnabled(lamexp_is_hibernation_supported());
559         connect(ui->actionDisableUpdateReminder, SIGNAL(triggered(bool)), this, SLOT(disableUpdateReminderActionTriggered(bool)));
560         connect(ui->actionDisableSounds, SIGNAL(triggered(bool)), this, SLOT(disableSoundsActionTriggered(bool)));
561         connect(ui->actionDisableNeroAacNotifications, SIGNAL(triggered(bool)), this, SLOT(disableNeroAacNotificationsActionTriggered(bool)));
562         connect(ui->actionDisableSlowStartupNotifications, SIGNAL(triggered(bool)), this, SLOT(disableSlowStartupNotificationsActionTriggered(bool)));
563         connect(ui->actionDisableShellIntegration, SIGNAL(triggered(bool)), this, SLOT(disableShellIntegrationActionTriggered(bool)));
564         connect(ui->actionShowDropBoxWidget, SIGNAL(triggered(bool)), this, SLOT(showDropBoxWidgetActionTriggered(bool)));
565         connect(ui->actionHibernateComputer, SIGNAL(triggered(bool)), this, SLOT(hibernateComputerActionTriggered(bool)));
566         connect(ui->actionCheckForBetaUpdates, SIGNAL(triggered(bool)), this, SLOT(checkForBetaUpdatesActionTriggered(bool)));
567         connect(ui->actionImportCueSheet, SIGNAL(triggered(bool)), this, SLOT(importCueSheetActionTriggered(bool)));
568                 
569         //Activate help menu actions
570         ui->actionVisitHomepage->setData(QString::fromLatin1(lamexp_website_url()));
571         ui->actionVisitSupport->setData(QString::fromLatin1(lamexp_support_url()));
572         ui->actionVisitMuldersSite->setData(QString::fromLatin1(lamexp_mulders_url()));
573         ui->actionDocumentFAQ->setData(QString("%1/FAQ.html").arg(QApplication::applicationDirPath()));
574         ui->actionDocumentChangelog->setData(QString("%1/Changelog.html").arg(QApplication::applicationDirPath()));
575         ui->actionDocumentTranslate->setData(QString("%1/Translate.html").arg(QApplication::applicationDirPath()));
576         connect(ui->actionCheckUpdates, SIGNAL(triggered()), this, SLOT(checkUpdatesActionActivated()));
577         connect(ui->actionVisitHomepage, SIGNAL(triggered()), this, SLOT(visitHomepageActionActivated()));
578         connect(ui->actionVisitMuldersSite, SIGNAL(triggered()), this, SLOT(visitHomepageActionActivated()));
579         connect(ui->actionVisitSupport, SIGNAL(triggered()), this, SLOT(visitHomepageActionActivated()));
580         connect(ui->actionDocumentFAQ, SIGNAL(triggered()), this, SLOT(documentActionActivated()));
581         connect(ui->actionDocumentChangelog, SIGNAL(triggered()), this, SLOT(documentActionActivated()));
582         connect(ui->actionDocumentTranslate, SIGNAL(triggered()), this, SLOT(documentActionActivated()));
583         
584         //--------------------------------
585         // Prepare to show window
586         //--------------------------------
587
588         //Center window in screen
589         QRect desktopRect = QApplication::desktop()->screenGeometry();
590         QRect thisRect = this->geometry();
591         move((desktopRect.width() - thisRect.width()) / 2, (desktopRect.height() - thisRect.height()) / 2);
592         setMinimumSize(thisRect.width(), thisRect.height());
593
594         //Create banner
595         m_banner = new WorkingBanner(this);
596
597         //Create DropBox widget
598         m_dropBox = new DropBox(this, m_fileListModel, m_settings);
599         connect(m_fileListModel, SIGNAL(modelReset()), m_dropBox, SLOT(modelChanged()));
600         connect(m_fileListModel, SIGNAL(rowsInserted(QModelIndex,int,int)), m_dropBox, SLOT(modelChanged()));
601         connect(m_fileListModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), m_dropBox, SLOT(modelChanged()));
602         connect(m_fileListModel, SIGNAL(rowAppended()), m_dropBox, SLOT(modelChanged()));
603
604         //Create message handler thread
605         m_messageHandler = new MessageHandlerThread();
606         m_delayedFileList = new QStringList();
607         m_delayedFileTimer = new QTimer();
608         m_delayedFileTimer->setSingleShot(true);
609         m_delayedFileTimer->setInterval(5000);
610         connect(m_messageHandler, SIGNAL(otherInstanceDetected()), this, SLOT(notifyOtherInstance()), Qt::QueuedConnection);
611         connect(m_messageHandler, SIGNAL(fileReceived(QString)), this, SLOT(addFileDelayed(QString)), Qt::QueuedConnection);
612         connect(m_messageHandler, SIGNAL(folderReceived(QString, bool)), this, SLOT(addFolderDelayed(QString, bool)), Qt::QueuedConnection);
613         connect(m_messageHandler, SIGNAL(killSignalReceived()), this, SLOT(close()), Qt::QueuedConnection);
614         connect(m_delayedFileTimer, SIGNAL(timeout()), this, SLOT(handleDelayedFiles()));
615         m_messageHandler->start();
616
617         //Load translation
618         initializeTranslation();
619
620         //Re-translate (make sure we translate once)
621         QEvent languageChangeEvent(QEvent::LanguageChange);
622         changeEvent(&languageChangeEvent);
623
624         //Enable Drag & Drop
625         this->setAcceptDrops(true);
626 }
627
628 ////////////////////////////////////////////////////////////
629 // Destructor
630 ////////////////////////////////////////////////////////////
631
632 MainWindow::~MainWindow(void)
633 {
634         //Stop message handler thread
635         if(m_messageHandler && m_messageHandler->isRunning())
636         {
637                 m_messageHandler->stop();
638                 if(!m_messageHandler->wait(2500))
639                 {
640                         m_messageHandler->terminate();
641                         m_messageHandler->wait();
642                 }
643         }
644
645         //Unset models
646         SET_MODEL(ui->sourceFileView, NULL);
647         SET_MODEL(ui->outputFolderView, NULL);
648         SET_MODEL(ui->metaDataView, NULL);
649
650         //Free memory
651         LAMEXP_DELETE(m_tabActionGroup);
652         LAMEXP_DELETE(m_styleActionGroup);
653         LAMEXP_DELETE(m_languageActionGroup);
654         LAMEXP_DELETE(m_banner);
655         LAMEXP_DELETE(m_fileSystemModel);
656         LAMEXP_DELETE(m_messageHandler);
657         LAMEXP_DELETE(m_delayedFileList);
658         LAMEXP_DELETE(m_delayedFileTimer);
659         LAMEXP_DELETE(m_metaInfoModel);
660         LAMEXP_DELETE(m_encoderButtonGroup);
661         LAMEXP_DELETE(m_modeButtonGroup);
662         LAMEXP_DELETE(m_overwriteButtonGroup);
663         LAMEXP_DELETE(m_sourceFilesContextMenu);
664         LAMEXP_DELETE(m_outputFolderFavoritesMenu);
665         LAMEXP_DELETE(m_outputFolderContextMenu);
666         LAMEXP_DELETE(m_dropBox);
667         LAMEXP_DELETE(m_evenFilterCustumParamsHelp);
668         LAMEXP_DELETE(m_evenFilterOutputFolderMouse);
669         LAMEXP_DELETE(m_evenFilterOutputFolderView);
670         LAMEXP_DELETE(m_evenFilterCompressionTab);
671         
672         //Un-initialize the dialog
673         LAMEXP_DELETE(ui);
674 }
675
676 ////////////////////////////////////////////////////////////
677 // PRIVATE FUNCTIONS
678 ////////////////////////////////////////////////////////////
679
680 /*
681  * Add file to source list
682  */
683 void MainWindow::addFiles(const QStringList &files)
684 {
685         if(files.isEmpty())
686         {
687                 return;
688         }
689
690         ui->tabWidget->setCurrentIndex(0);
691
692         FileAnalyzer *analyzer = new FileAnalyzer(files);
693         connect(analyzer, SIGNAL(fileSelected(QString)), m_banner, SLOT(setText(QString)), Qt::QueuedConnection);
694         connect(analyzer, SIGNAL(progressValChanged(unsigned int)), m_banner, SLOT(setProgressVal(unsigned int)), Qt::QueuedConnection);
695         connect(analyzer, SIGNAL(progressMaxChanged(unsigned int)), m_banner, SLOT(setProgressMax(unsigned int)), Qt::QueuedConnection);
696         connect(analyzer, SIGNAL(fileAnalyzed(AudioFileModel)), m_fileListModel, SLOT(addFile(AudioFileModel)), Qt::QueuedConnection);
697         connect(m_banner, SIGNAL(userAbort()), analyzer, SLOT(abortProcess()), Qt::DirectConnection);
698
699         try
700         {
701                 m_fileListModel->setBlockUpdates(true);
702                 QTime startTime = QTime::currentTime();
703                 m_banner->show(tr("Adding file(s), please wait..."), analyzer);
704         }
705         catch(...)
706         {
707                 /* ignore any exceptions that may occur */
708         }
709
710         m_fileListModel->setBlockUpdates(false);
711         qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
712         ui->sourceFileView->update();
713         qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
714         ui->sourceFileView->scrollToBottom();
715         qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
716
717         if(analyzer->filesDenied())
718         {
719                 QMessageBox::warning(this, tr("Access Denied"), QString("%1<br>%2").arg(NOBR(tr("%n file(s) have been rejected, because read access was not granted!", "", analyzer->filesDenied())), NOBR(tr("This usually means the file is locked by another process."))));
720         }
721         if(analyzer->filesDummyCDDA())
722         {
723                 QMessageBox::warning(this, tr("CDDA Files"), QString("%1<br><br>%2<br>%3").arg(NOBR(tr("%n file(s) have been rejected, because they are dummy CDDA files!", "", analyzer->filesDummyCDDA())), NOBR(tr("Sorry, LameXP cannot extract audio tracks from an Audio-CD at present.")), NOBR(tr("We recommend using %1 for that purpose.").arg("<a href=\"http://www.exactaudiocopy.de/\">Exact Audio Copy</a>"))));
724         }
725         if(analyzer->filesCueSheet())
726         {
727                 QMessageBox::warning(this, tr("Cue Sheet"), QString("%1<br>%2").arg(NOBR(tr("%n file(s) have been rejected, because they appear to be Cue Sheet images!", "",analyzer->filesCueSheet())), NOBR(tr("Please use LameXP's Cue Sheet wizard for importing Cue Sheet files."))));
728         }
729         if(analyzer->filesRejected())
730         {
731                 QMessageBox::warning(this, tr("Files Rejected"), QString("%1<br>%2").arg(NOBR(tr("%n file(s) have been rejected, because the file format could not be recognized!", "", analyzer->filesRejected())), NOBR(tr("This usually means the file is damaged or the file format is not supported."))));
732         }
733
734         LAMEXP_DELETE(analyzer);
735         m_banner->close();
736 }
737
738 /*
739  * Add folder to source list
740  */
741 void MainWindow::addFolder(const QString &path, bool recursive, bool delayed)
742 {
743         QFileInfoList folderInfoList;
744         folderInfoList << QFileInfo(path);
745         QStringList fileList;
746         
747         m_banner->show(tr("Scanning folder(s) for files, please wait..."));
748         
749         QApplication::processEvents();
750         lamexp_check_escape_state();
751
752         while(!folderInfoList.isEmpty())
753         {
754                 if(lamexp_check_escape_state())
755                 {
756                         lamexp_beep(lamexp_beep_error);
757                         qWarning("Operation cancelled by user!");
758                         fileList.clear();
759                         break;
760                 }
761                 
762                 QDir currentDir(folderInfoList.takeFirst().canonicalFilePath());
763                 QFileInfoList fileInfoList = currentDir.entryInfoList(QDir::Files | QDir::NoSymLinks);
764
765                 while(!fileInfoList.isEmpty())
766                 {
767                         fileList << fileInfoList.takeFirst().canonicalFilePath();
768                 }
769
770                 QApplication::processEvents();
771
772                 if(recursive)
773                 {
774                         folderInfoList.append(currentDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks));
775                         QApplication::processEvents();
776                 }
777         }
778         
779         m_banner->close();
780         QApplication::processEvents();
781
782         if(!fileList.isEmpty())
783         {
784                 if(delayed)
785                 {
786                         addFilesDelayed(fileList);
787                 }
788                 else
789                 {
790                         addFiles(fileList);
791                 }
792         }
793 }
794
795 /*
796  * Check for updates
797  */
798 bool MainWindow::checkForUpdates(void)
799 {
800         bool bReadyToInstall = false;
801         
802         UpdateDialog *updateDialog = new UpdateDialog(m_settings, this);
803         updateDialog->exec();
804
805         if(updateDialog->getSuccess())
806         {
807                 m_settings->autoUpdateLastCheck(QDate::currentDate().toString(Qt::ISODate));
808                 bReadyToInstall = updateDialog->updateReadyToInstall();
809         }
810
811         LAMEXP_DELETE(updateDialog);
812         return bReadyToInstall;
813 }
814
815 /*
816  * Refresh list of favorites
817  */
818 void MainWindow::refreshFavorites(void)
819 {
820         QList<QAction*> folderList = m_outputFolderFavoritesMenu->actions();
821         QStringList favorites = m_settings->favoriteOutputFolders().split("|", QString::SkipEmptyParts);
822         while(favorites.count() > 6) favorites.removeFirst();
823
824         while(!folderList.isEmpty())
825         {
826                 QAction *currentItem = folderList.takeFirst();
827                 if(currentItem->isSeparator()) break;
828                 m_outputFolderFavoritesMenu->removeAction(currentItem);
829                 LAMEXP_DELETE(currentItem);
830         }
831
832         QAction *lastItem = m_outputFolderFavoritesMenu->actions().first();
833
834         while(!favorites.isEmpty())
835         {
836                 QString path = favorites.takeLast();
837                 if(QDir(path).exists())
838                 {
839                         QAction *action = new QAction(QIcon(":/icons/folder_go.png"), QDir::toNativeSeparators(path), this);
840                         action->setData(path);
841                         m_outputFolderFavoritesMenu->insertAction(lastItem, action);
842                         connect(action, SIGNAL(triggered(bool)), this, SLOT(gotoFavoriteFolder()));
843                         lastItem = action;
844                 }
845         }
846 }
847
848 /*
849  * Initilaize translation
850  */
851 void MainWindow::initializeTranslation(void)
852 {
853         bool translationLoaded = false;
854
855         //Try to load "external" translation file
856         if(!m_settings->currentLanguageFile().isEmpty())
857         {
858                 const QString qmFilePath = QFileInfo(m_settings->currentLanguageFile()).canonicalFilePath();
859                 if((!qmFilePath.isEmpty()) && QFileInfo(qmFilePath).exists() && QFileInfo(qmFilePath).isFile() && (QFileInfo(qmFilePath).suffix().compare("qm", Qt::CaseInsensitive) == 0))
860                 {
861                         if(lamexp_install_translator_from_file(qmFilePath))
862                         {
863                                 QList<QAction*> actions = m_languageActionGroup->actions();
864                                 while(!actions.isEmpty()) actions.takeFirst()->setChecked(false);
865                                 ui->actionLoadTranslationFromFile->setChecked(true);
866                                 translationLoaded = true;
867                         }
868                 }
869         }
870
871         //Try to load "built-in" translation file
872         if(!translationLoaded)
873         {
874                 QList<QAction*> languageActions = m_languageActionGroup->actions();
875                 while(!languageActions.isEmpty())
876                 {
877                         QAction *currentLanguage = languageActions.takeFirst();
878                         if(currentLanguage->data().toString().compare(m_settings->currentLanguage(), Qt::CaseInsensitive) == 0)
879                         {
880                                 currentLanguage->setChecked(true);
881                                 languageActionActivated(currentLanguage);
882                                 translationLoaded = true;
883                         }
884                 }
885         }
886
887         //Fallback to default translation
888         if(!translationLoaded)
889         {
890                 QList<QAction*> languageActions = m_languageActionGroup->actions();
891                 while(!languageActions.isEmpty())
892                 {
893                         QAction *currentLanguage = languageActions.takeFirst();
894                         if(currentLanguage->data().toString().compare(LAMEXP_DEFAULT_LANGID, Qt::CaseInsensitive) == 0)
895                         {
896                                 currentLanguage->setChecked(true);
897                                 languageActionActivated(currentLanguage);
898                                 translationLoaded = true;
899                         }
900                 }
901         }
902
903         //Make sure we loaded some translation
904         if(!translationLoaded)
905         {
906                 qFatal("Failed to load any translation, this is NOT supposed to happen!");
907         }
908 }
909
910 ////////////////////////////////////////////////////////////
911 // EVENTS
912 ////////////////////////////////////////////////////////////
913
914 /*
915  * Window is about to be shown
916  */
917 void MainWindow::showEvent(QShowEvent *event)
918 {
919         m_accepted = false;
920         resizeEvent(NULL);
921         sourceModelChanged();
922
923         if(!event->spontaneous())
924         {
925                 ui->tabWidget->setCurrentIndex(0);
926         }
927
928         if(m_firstTimeShown)
929         {
930                 m_firstTimeShown = false;
931                 lamexp_set_window_icon(this, lamexp_app_icon(), true);
932                 QTimer::singleShot(0, this, SLOT(windowShown()));
933         }
934         else
935         {
936                 if(m_settings->dropBoxWidgetEnabled())
937                 {
938                         m_dropBox->setVisible(true);
939                 }
940         }
941 }
942
943 /*
944  * Re-translate the UI
945  */
946 void MainWindow::changeEvent(QEvent *e)
947 {
948         if(e->type() == QEvent::LanguageChange)
949         {
950                 /*qWarning("\nMainWindow::changeEvent()\n");*/
951
952                 int comboBoxIndex[8];
953                 
954                 //Backup combobox indices, as retranslateUi() resets
955                 comboBoxIndex[0] = ui->comboBoxMP3ChannelMode->currentIndex();
956                 comboBoxIndex[1] = ui->comboBoxSamplingRate->currentIndex();
957                 comboBoxIndex[2] = ui->comboBoxAACProfile->currentIndex();
958                 comboBoxIndex[3] = ui->comboBoxAftenCodingMode->currentIndex();
959                 comboBoxIndex[4] = ui->comboBoxAftenDRCMode->currentIndex();
960                 comboBoxIndex[5] = ui->comboBoxNormalizationMode->currentIndex();
961                 comboBoxIndex[6] = 0; //comboBoxOpusOptimize->currentIndex();
962                 comboBoxIndex[7] = ui->comboBoxOpusFramesize->currentIndex();
963                 
964                 //Re-translate from UIC
965                 ui->retranslateUi(this);
966
967                 //Restore combobox indices
968                 ui->comboBoxMP3ChannelMode->setCurrentIndex(comboBoxIndex[0]);
969                 ui->comboBoxSamplingRate->setCurrentIndex(comboBoxIndex[1]);
970                 ui->comboBoxAACProfile->setCurrentIndex(comboBoxIndex[2]);
971                 ui->comboBoxAftenCodingMode->setCurrentIndex(comboBoxIndex[3]);
972                 ui->comboBoxAftenDRCMode->setCurrentIndex(comboBoxIndex[4]);
973                 ui->comboBoxNormalizationMode->setCurrentIndex(comboBoxIndex[5]);
974                 //comboBoxOpusOptimize->setCurrentIndex(comboBoxIndex[6]);
975                 ui->comboBoxOpusFramesize->setCurrentIndex(comboBoxIndex[7]);
976
977                 //Update the window title
978                 if(LAMEXP_DEBUG)
979                 {
980                         setWindowTitle(QString("%1 [!!! DEBUG BUILD !!!]").arg(windowTitle()));
981                 }
982                 else if(lamexp_version_demo())
983                 {
984                         setWindowTitle(QString("%1 [%2]").arg(windowTitle(), tr("DEMO VERSION")));
985                 }
986
987                 //Manually re-translate widgets that UIC doesn't handle
988                 m_outputFolderNoteBox->setText(tr("Initializing directory outline, please be patient..."));
989                 m_dropNoteLabel->setText(QString("<br><img src=\":/images/DropZone.png\"><br><br>%1").arg(tr("You can drop in audio files here!")));
990                 m_showDetailsContextAction->setText(tr("Show Details"));
991                 m_previewContextAction->setText(tr("Open File in External Application"));
992                 m_findFileContextAction->setText(tr("Browse File Location"));
993                 m_showFolderContextAction->setText(tr("Browse Selected Folder"));
994                 m_refreshFolderContextAction->setText(tr("Refresh Directory Outline"));
995                 m_goUpFolderContextAction->setText(tr("Go To Parent Directory"));
996                 m_addFavoriteFolderAction->setText(tr("Bookmark Current Output Folder"));
997                 m_exportCsvContextAction->setText(tr("Export Meta Tags to CSV File"));
998                 m_importCsvContextAction->setText(tr("Import Meta Tags from CSV File"));
999
1000                 //Force GUI update
1001                 m_metaInfoModel->clearData();
1002                 m_metaInfoModel->setData(m_metaInfoModel->index(4, 1), m_settings->metaInfoPosition());
1003                 updateEncoder(m_settings->compressionEncoder());
1004                 updateLameAlgoQuality(ui->sliderLameAlgoQuality->value());
1005                 updateMaximumInstances(ui->sliderMaxInstances->value());
1006                 renameOutputPatternChanged(ui->lineEditRenamePattern->text(), true);
1007
1008                 //Re-install shell integration
1009                 if(m_settings->shellIntegrationEnabled())
1010                 {
1011                         ShellIntegration::install();
1012                 }
1013
1014                 //Translate system menu
1015                 lamexp_update_sysmenu(this, IDM_ABOUTBOX, ui->buttonAbout->text());
1016                         
1017                 //Force resize, if needed
1018                 tabPageChanged(ui->tabWidget->currentIndex(), true);
1019         }
1020 }
1021
1022 /*
1023  * File dragged over window
1024  */
1025 void MainWindow::dragEnterEvent(QDragEnterEvent *event)
1026 {
1027         QStringList formats = event->mimeData()->formats();
1028         
1029         if(formats.contains("application/x-qt-windows-mime;value=\"FileNameW\"", Qt::CaseInsensitive) && formats.contains("text/uri-list", Qt::CaseInsensitive))
1030         {
1031                 event->acceptProposedAction();
1032         }
1033 }
1034
1035 /*
1036  * File dropped onto window
1037  */
1038 void MainWindow::dropEvent(QDropEvent *event)
1039 {
1040         ABORT_IF_BUSY;
1041
1042         if(m_settings->soundsEnabled()) lamexp_play_sound(IDR_WAVE_DROP, true);
1043
1044         QStringList droppedFiles;
1045         QList<QUrl> urls = event->mimeData()->urls();
1046
1047         while(!urls.isEmpty())
1048         {
1049                 QUrl currentUrl = urls.takeFirst();
1050                 QFileInfo file(currentUrl.toLocalFile());
1051                 if(!file.exists())
1052                 {
1053                         continue;
1054                 }
1055                 if(file.isFile())
1056                 {
1057                         qDebug("Dropped File: %s", QUTF8(file.canonicalFilePath()));
1058                         droppedFiles << file.canonicalFilePath();
1059                         continue;
1060                 }
1061                 if(file.isDir())
1062                 {
1063                         qDebug("Dropped Folder: %s", QUTF8(file.canonicalFilePath()));
1064                         QList<QFileInfo> list = QDir(file.canonicalFilePath()).entryInfoList(QDir::Files | QDir::NoSymLinks);
1065                         if(list.count() > 0)
1066                         {
1067                                 for(int j = 0; j < list.count(); j++)
1068                                 {
1069                                         droppedFiles << list.at(j).canonicalFilePath();
1070                                 }
1071                         }
1072                         else
1073                         {
1074                                 list = QDir(file.canonicalFilePath()).entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
1075                                 for(int j = 0; j < list.count(); j++)
1076                                 {
1077                                         qDebug("Descending to Folder: %s", QUTF8(list.at(j).canonicalFilePath()));
1078                                         urls.prepend(QUrl::fromLocalFile(list.at(j).canonicalFilePath()));
1079                                 }
1080                         }
1081                 }
1082         }
1083         
1084         if(!droppedFiles.isEmpty())
1085         {
1086                 addFilesDelayed(droppedFiles, true);
1087         }
1088 }
1089
1090 /*
1091  * Window tries to close
1092  */
1093 void MainWindow::closeEvent(QCloseEvent *event)
1094 {
1095         if(m_banner->isVisible() || m_delayedFileTimer->isActive())
1096         {
1097                 lamexp_beep(lamexp_beep_warning);
1098                 event->ignore();
1099         }
1100         
1101         if(m_dropBox)
1102         {
1103                 m_dropBox->hide();
1104         }
1105 }
1106
1107 /*
1108  * Window was resized
1109  */
1110 void MainWindow::resizeEvent(QResizeEvent *event)
1111 {
1112         if(event) QMainWindow::resizeEvent(event);
1113
1114         if(QWidget *port = ui->sourceFileView->viewport())
1115         {
1116                 m_dropNoteLabel->setGeometry(port->geometry());
1117         }
1118
1119         if (QWidget *port = ui->outputFolderView->viewport())
1120         {
1121                 m_outputFolderNoteBox->setGeometry(16, (port->height() - 64) / 2, port->width() - 32, 64);
1122         }
1123 }
1124
1125 /*
1126  * Key press event filter
1127  */
1128 void MainWindow::keyPressEvent(QKeyEvent *e)
1129 {
1130         if(e->key() == Qt::Key_Delete)
1131         {
1132                 if(ui->sourceFileView->isVisible())
1133                 {
1134                         QTimer::singleShot(0, this, SLOT(removeFileButtonClicked()));
1135                         return;
1136                 }
1137         }
1138
1139         if(e->modifiers().testFlag(Qt::ControlModifier) && (e->key() == Qt::Key_F5))
1140         {
1141                 initializeTranslation();
1142                 lamexp_beep(lamexp_beep_info);
1143                 return;
1144         }
1145
1146         if(e->key() == Qt::Key_F5)
1147         {
1148                 if(ui->outputFolderView->isVisible())
1149                 {
1150                         QTimer::singleShot(0, this, SLOT(refreshFolderContextActionTriggered()));
1151                         return;
1152                 }
1153         }
1154
1155         QMainWindow::keyPressEvent(e);
1156 }
1157
1158 /*
1159  * Event filter
1160  */
1161 bool MainWindow::eventFilter(QObject *obj, QEvent *event)
1162 {
1163         if(obj == m_fileSystemModel)
1164         {
1165                 if(QApplication::overrideCursor() == NULL)
1166                 {
1167                         QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
1168                         QTimer::singleShot(250, this, SLOT(restoreCursor()));
1169                 }
1170         }
1171
1172         return QMainWindow::eventFilter(obj, event);
1173 }
1174
1175 bool MainWindow::event(QEvent *e)
1176 {
1177         switch(e->type())
1178         {
1179         case lamexp_event_queryendsession:
1180                 qWarning("System is shutting down, main window prepares to close...");
1181                 if(m_banner->isVisible()) m_banner->close();
1182                 if(m_delayedFileTimer->isActive()) m_delayedFileTimer->stop();
1183                 return true;
1184         case lamexp_event_endsession:
1185                 qWarning("System is shutting down, main window will close now...");
1186                 if(isVisible())
1187                 {
1188                         while(!close())
1189                         {
1190                                 QApplication::processEvents(QEventLoop::WaitForMoreEvents & QEventLoop::ExcludeUserInputEvents);
1191                         }
1192                 }
1193                 m_fileListModel->clearFiles();
1194                 return true;
1195         case QEvent::MouseButtonPress:
1196                 if(ui->outputFolderEdit->isVisible())
1197                 {
1198                         QTimer::singleShot(0, this, SLOT(outputFolderEditFinished()));
1199                 }
1200         default:
1201                 return QMainWindow::event(e);
1202         }
1203 }
1204
1205 bool MainWindow::winEvent(MSG *message, long *result)
1206 {
1207         if(lamexp_check_sysmenu_msg(message, IDM_ABOUTBOX))
1208         {
1209                 QTimer::singleShot(0, ui->buttonAbout, SLOT(click()));
1210                 *result = 0;
1211                 return true;
1212         }
1213         return WinSevenTaskbar::handleWinEvent(message, result);
1214 }
1215
1216 ////////////////////////////////////////////////////////////
1217 // Slots
1218 ////////////////////////////////////////////////////////////
1219
1220 // =========================================================
1221 // Show window slots
1222 // =========================================================
1223
1224 /*
1225  * Window shown
1226  */
1227 void MainWindow::windowShown(void)
1228 {
1229         const QStringList &arguments = lamexp_arguments(); //QApplication::arguments();
1230
1231         //Force resize event
1232         resizeEvent(NULL);
1233
1234         //First run?
1235         bool firstRun = false;
1236         for(int i = 0; i < arguments.count(); i++)
1237         {
1238                 /*QMessageBox::information(this, QString::number(i), arguments[i]);*/
1239                 if(!arguments[i].compare("--first-run", Qt::CaseInsensitive)) firstRun = true;
1240         }
1241
1242         //Check license
1243         if((m_settings->licenseAccepted() <= 0) || firstRun)
1244         {
1245                 int iAccepted = m_settings->licenseAccepted();
1246
1247                 if((iAccepted == 0) || firstRun)
1248                 {
1249                         AboutDialog *about = new AboutDialog(m_settings, this, true);
1250                         iAccepted = about->exec();
1251                         if(iAccepted <= 0) iAccepted = -2;
1252                         LAMEXP_DELETE(about);
1253                 }
1254
1255                 if(iAccepted <= 0)
1256                 {
1257                         m_settings->licenseAccepted(++iAccepted);
1258                         m_settings->syncNow();
1259                         QApplication::processEvents();
1260                         lamexp_play_sound(IDR_WAVE_WHAMMY, false);
1261                         QMessageBox::critical(this, tr("License Declined"), tr("You have declined the license. Consequently the application will exit now!"), tr("Goodbye!"));
1262                         QFileInfo uninstallerInfo = QFileInfo(QString("%1/Uninstall.exe").arg(QApplication::applicationDirPath()));
1263                         if(uninstallerInfo.exists())
1264                         {
1265                                 QString uninstallerDir = uninstallerInfo.canonicalPath();
1266                                 QString uninstallerPath = uninstallerInfo.canonicalFilePath();
1267                                 for(int i = 0; i < 3; i++)
1268                                 {
1269                                         if(lamexp_exec_shell(this, QDir::toNativeSeparators(uninstallerPath), "/Force", QDir::toNativeSeparators(uninstallerDir))) break;
1270                                 }
1271                         }
1272                         QApplication::quit();
1273                         return;
1274                 }
1275                 
1276                 lamexp_play_sound(IDR_WAVE_WOOHOO, false);
1277                 m_settings->licenseAccepted(1);
1278                 m_settings->syncNow();
1279                 if(lamexp_version_demo()) showAnnounceBox();
1280         }
1281         
1282         //Check for expiration
1283         if(lamexp_version_demo())
1284         {
1285                 if(lamexp_current_date_safe() >= lamexp_version_expires())
1286                 {
1287                         qWarning("Binary has expired !!!");
1288                         lamexp_play_sound(IDR_WAVE_WHAMMY, false);
1289                         if(QMessageBox::warning(this, tr("LameXP - Expired"), QString("%1<br>%2").arg(NOBR(tr("This demo (pre-release) version of LameXP has expired at %1.").arg(lamexp_version_expires().toString(Qt::ISODate))), NOBR(tr("LameXP is free software and release versions won't expire."))), tr("Check for Updates"), tr("Exit Program")) == 0)
1290                         {
1291                                 checkForUpdates();
1292                         }
1293                         QApplication::quit();
1294                         return;
1295                 }
1296         }
1297
1298         //Slow startup indicator
1299         if(m_settings->slowStartup() && m_settings->antivirNotificationsEnabled())
1300         {
1301                 QString message;
1302                 message += NOBR(tr("It seems that a bogus anti-virus software is slowing down the startup of LameXP.")).append("<br>");
1303                 message += NOBR(tr("Please refer to the %1 document for details and solutions!")).arg("<a href=\"http://lamexp.sourceforge.net/doc/FAQ.html#df406578\">F.A.Q.</a>").append("<br>");
1304                 if(QMessageBox::warning(this, tr("Slow Startup"), message, tr("Discard"), tr("Don't Show Again")) == 1)
1305                 {
1306                         m_settings->antivirNotificationsEnabled(false);
1307                         ui->actionDisableSlowStartupNotifications->setChecked(!m_settings->antivirNotificationsEnabled());
1308                 }
1309         }
1310
1311         //Update reminder
1312         if(lamexp_current_date_safe() >= lamexp_version_date().addYears(1))
1313         {
1314                 qWarning("Binary is more than a year old, time to update!");
1315                 int ret = QMessageBox::warning(this, tr("Urgent Update"), NOBR(tr("Your version of LameXP is more than a year old. Time for an update!")), tr("Check for Updates"), tr("Exit Program"), tr("Ignore"));
1316                 switch(ret)
1317                 {
1318                 case 0:
1319                         if(checkForUpdates())
1320                         {
1321                                 QApplication::quit();
1322                                 return;
1323                         }
1324                         break;
1325                 case 1:
1326                         QApplication::quit();
1327                         return;
1328                 default:
1329                         QEventLoop loop; QTimer::singleShot(7000, &loop, SLOT(quit()));
1330                         lamexp_play_sound(IDR_WAVE_WAITING, true);
1331                         m_banner->show(tr("Skipping update check this time, please be patient..."), &loop);
1332                         break;
1333                 }
1334         }
1335         else if(m_settings->autoUpdateEnabled())
1336         {
1337                 QDate lastUpdateCheck = QDate::fromString(m_settings->autoUpdateLastCheck(), Qt::ISODate);
1338                 if(!firstRun && (!lastUpdateCheck.isValid() || lamexp_current_date_safe() >= lastUpdateCheck.addDays(14)))
1339                 {
1340                         if(QMessageBox::information(this, tr("Update Reminder"), NOBR(lastUpdateCheck.isValid() ? tr("Your last update check was more than 14 days ago. Check for updates now?") : tr("Your did not check for LameXP updates yet. Check for updates now?")), tr("Check for Updates"), tr("Postpone")) == 0)
1341                         {
1342                                 if(checkForUpdates())
1343                                 {
1344                                         QApplication::quit();
1345                                         return;
1346                                 }
1347                         }
1348                 }
1349         }
1350
1351         //Check for AAC support
1352         const int aacEncoder = EncoderRegistry::getAacEncoder();
1353         if(aacEncoder == SettingsModel::AAC_ENCODER_NERO)
1354         {
1355                 if(m_settings->neroAacNotificationsEnabled())
1356                 {
1357                         if(lamexp_tool_version("neroAacEnc.exe") < lamexp_toolver_neroaac())
1358                         {
1359                                 QString messageText;
1360                                 messageText += NOBR(tr("LameXP detected that your version of the Nero AAC encoder is outdated!")).append("<br>");
1361                                 messageText += NOBR(tr("The current version available is %1 (or later), but you still have version %2 installed.").arg(lamexp_version2string("?.?.?.?", lamexp_toolver_neroaac(), tr("n/a")), lamexp_version2string("?.?.?.?", lamexp_tool_version("neroAacEnc.exe"), tr("n/a")))).append("<br><br>");
1362                                 messageText += NOBR(tr("You can download the latest version of the Nero AAC encoder from the Nero website at:")).append("<br>");
1363                                 messageText += "<nobr><tt>" + LINK(AboutDialog::neroAacUrl) + "</tt></nobr><br><br>";
1364                                 messageText += NOBR(tr("(Hint: Please ignore the name of the downloaded ZIP file and check the included 'changelog.txt' instead!)")).append("<br>");
1365                                 QMessageBox::information(this, tr("AAC Encoder Outdated"), messageText);
1366                         }
1367                 }
1368         }
1369         else
1370         {
1371                 if(m_settings->neroAacNotificationsEnabled() && (aacEncoder <= SettingsModel::AAC_ENCODER_NONE))
1372                 {
1373                         QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
1374                         if(appPath.isEmpty()) appPath = QCoreApplication::applicationDirPath();
1375                         QString messageText;
1376                         messageText += NOBR(tr("The Nero AAC encoder could not be found. AAC encoding support will be disabled.")).append("<br>");
1377                         messageText += NOBR(tr("Please put 'neroAacEnc.exe', 'neroAacDec.exe' and 'neroAacTag.exe' into the LameXP directory!")).append("<br><br>");
1378                         messageText += NOBR(tr("Your LameXP directory is located here:")).append("<br>");
1379                         messageText += QString("<nobr><tt>%1</tt></nobr><br><br>").arg(FSLINK(QDir::toNativeSeparators(appPath)));
1380                         messageText += NOBR(tr("You can download the Nero AAC encoder for free from the official Nero website at:")).append("<br>");
1381                         messageText += "<nobr><tt>" + LINK(AboutDialog::neroAacUrl) + "</tt></nobr><br>";
1382                         if(QMessageBox::information(this, tr("AAC Support Disabled"), messageText, tr("Discard"), tr("Don't Show Again")) == 1)
1383                         {
1384                                 m_settings->neroAacNotificationsEnabled(false);
1385                                 ui->actionDisableNeroAacNotifications->setChecked(!m_settings->neroAacNotificationsEnabled());
1386                         }
1387                 }
1388         }
1389
1390         //Add files from the command-line
1391         for(int i = 0; i < arguments.count() - 1; i++)
1392         {
1393                 QStringList addedFiles;
1394                 if(!arguments[i].compare("--add", Qt::CaseInsensitive))
1395                 {
1396                         QFileInfo currentFile(arguments[++i].trimmed());
1397                         qDebug("Adding file from CLI: %s", QUTF8(currentFile.absoluteFilePath()));
1398                         addedFiles.append(currentFile.absoluteFilePath());
1399                 }
1400                 if(!addedFiles.isEmpty())
1401                 {
1402                         addFilesDelayed(addedFiles);
1403                 }
1404         }
1405
1406         //Add folders from the command-line
1407         for(int i = 0; i < arguments.count() - 1; i++)
1408         {
1409                 if(!arguments[i].compare("--add-folder", Qt::CaseInsensitive))
1410                 {
1411                         QFileInfo currentFile(arguments[++i].trimmed());
1412                         qDebug("Adding folder from CLI: %s", QUTF8(currentFile.absoluteFilePath()));
1413                         addFolder(currentFile.absoluteFilePath(), false, true);
1414                 }
1415                 if(!arguments[i].compare("--add-recursive", Qt::CaseInsensitive))
1416                 {
1417                         QFileInfo currentFile(arguments[++i].trimmed());
1418                         qDebug("Adding folder recursively from CLI: %s", QUTF8(currentFile.absoluteFilePath()));
1419                         addFolder(currentFile.absoluteFilePath(), true, true);
1420                 }
1421         }
1422
1423         //Enable shell integration
1424         if(m_settings->shellIntegrationEnabled())
1425         {
1426                 ShellIntegration::install();
1427         }
1428
1429         //Make DropBox visible
1430         if(m_settings->dropBoxWidgetEnabled())
1431         {
1432                 m_dropBox->setVisible(true);
1433         }
1434 }
1435
1436 /*
1437  * Show announce box
1438  */
1439 void MainWindow::showAnnounceBox(void)
1440 {
1441         const unsigned int timeout = 8U;
1442
1443         const QString announceText = QString("%1<br><br>%2<br><nobr><tt>%3</tt></nobr><br>").arg
1444         (
1445                 NOBR("We are still looking for LameXP translators!"),
1446                 NOBR("If you are willing to translate LameXP to your language or to complete an existing translation, please refer to:"),
1447                 LINK("http://lamexp.sourceforge.net/doc/Translate.html")
1448         );
1449
1450         QMessageBox *announceBox = new QMessageBox(QMessageBox::Warning, "We want you!", announceText, QMessageBox::NoButton, this);
1451         announceBox->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
1452         announceBox->setIconPixmap(QIcon(":/images/Announcement.png").pixmap(64,79));
1453         
1454         QTimer *timers[timeout+1];
1455         QPushButton *buttons[timeout+1];
1456
1457         for(unsigned int i = 0; i <= timeout; i++)
1458         {
1459                 QString text = (i > 0) ? QString("%1 (%2)").arg(tr("Discard"), QString::number(i)) : tr("Discard");
1460                 buttons[i] = announceBox->addButton(text, (i > 0) ? QMessageBox::NoRole : QMessageBox::AcceptRole);
1461         }
1462
1463         for(unsigned int i = 0; i <= timeout; i++)
1464         {
1465                 buttons[i]->setEnabled(i == 0);
1466                 buttons[i]->setVisible(i == timeout);
1467         }
1468
1469         for(unsigned int i = 0; i < timeout; i++)
1470         {
1471                 timers[i] = new QTimer(this);
1472                 timers[i]->setSingleShot(true);
1473                 timers[i]->setInterval(1000);
1474                 connect(timers[i], SIGNAL(timeout()), buttons[i+1], SLOT(hide()));
1475                 connect(timers[i], SIGNAL(timeout()), buttons[i], SLOT(show()));
1476                 if(i > 0)
1477                 {
1478                         connect(timers[i], SIGNAL(timeout()), timers[i-1], SLOT(start()));
1479                 }
1480         }
1481
1482         timers[timeout-1]->start();
1483         announceBox->exec();
1484
1485         for(unsigned int i = 0; i < timeout; i++)
1486         {
1487                 timers[i]->stop();
1488                 LAMEXP_DELETE(timers[i]);
1489         }
1490
1491         LAMEXP_DELETE(announceBox);
1492 }
1493
1494 // =========================================================
1495 // Main button solots
1496 // =========================================================
1497
1498 /*
1499  * Encode button
1500  */
1501 void MainWindow::encodeButtonClicked(void)
1502 {
1503         static const unsigned __int64 oneGigabyte = 1073741824ui64; 
1504         static const unsigned __int64 minimumFreeDiskspaceMultiplier = 2ui64;
1505         static const char *writeTestBuffer = "LAMEXP_WRITE_TEST";
1506         
1507         ABORT_IF_BUSY;
1508
1509         if(m_fileListModel->rowCount() < 1)
1510         {
1511                 QMessageBox::warning(this, tr("LameXP"), NOBR(tr("You must add at least one file to the list before proceeding!")));
1512                 ui->tabWidget->setCurrentIndex(0);
1513                 return;
1514         }
1515         
1516         QString tempFolder = m_settings->customTempPathEnabled() ? m_settings->customTempPath() : lamexp_temp_folder2();
1517         if(!QFileInfo(tempFolder).exists() || !QFileInfo(tempFolder).isDir())
1518         {
1519                 if(QMessageBox::warning(this, tr("Not Found"), QString("%1<br><tt>%2</tt>").arg(NOBR(tr("Your currently selected TEMP folder does not exist anymore:")), NOBR(QDir::toNativeSeparators(tempFolder))), tr("Restore Default"), tr("Cancel")) == 0)
1520                 {
1521                         SET_CHECKBOX_STATE(ui->checkBoxUseSystemTempFolder, m_settings->customTempPathEnabledDefault());
1522                 }
1523                 return;
1524         }
1525
1526         bool ok = false;
1527         unsigned __int64 currentFreeDiskspace = lamexp_free_diskspace(tempFolder, &ok);
1528
1529         if(ok && (currentFreeDiskspace < (oneGigabyte * minimumFreeDiskspaceMultiplier)))
1530         {
1531                 QStringList tempFolderParts = tempFolder.split("/", QString::SkipEmptyParts, Qt::CaseInsensitive);
1532                 tempFolderParts.takeLast();
1533                 if(m_settings->soundsEnabled()) lamexp_play_sound(IDR_WAVE_WHAMMY, false);
1534                 QString lowDiskspaceMsg = QString("%1<br>%2<br><br>%3<br>%4<br>").arg
1535                 (
1536                         NOBR(tr("There are less than %1 GB of free diskspace available on your system's TEMP folder.").arg(QString::number(minimumFreeDiskspaceMultiplier))),
1537                         NOBR(tr("It is highly recommend to free up more diskspace before proceeding with the encode!")),
1538                         NOBR(tr("Your TEMP folder is located at:")),
1539                         QString("<nobr><tt>%1</tt></nobr>").arg(FSLINK(tempFolderParts.join("\\")))
1540                 );
1541                 switch(QMessageBox::warning(this, tr("Low Diskspace Warning"), lowDiskspaceMsg, tr("Abort Encoding Process"), tr("Clean Disk Now"), tr("Ignore")))
1542                 {
1543                 case 1:
1544                         QProcess::startDetached(QString("%1/cleanmgr.exe").arg(lamexp_known_folder(lamexp_folder_systemfolder)), QStringList() << "/D" << tempFolderParts.first());
1545                 case 0:
1546                         return;
1547                         break;
1548                 default:
1549                         QMessageBox::warning(this, tr("Low Diskspace"), NOBR(tr("You are proceeding with low diskspace. Problems might occur!")));
1550                         break;
1551                 }
1552         }
1553
1554         switch(m_settings->compressionEncoder())
1555         {
1556         case SettingsModel::MP3Encoder:
1557         case SettingsModel::VorbisEncoder:
1558         case SettingsModel::AACEncoder:
1559         case SettingsModel::AC3Encoder:
1560         case SettingsModel::FLACEncoder:
1561         case SettingsModel::OpusEncoder:
1562         case SettingsModel::DCAEncoder:
1563         case SettingsModel::PCMEncoder:
1564                 break;
1565         default:
1566                 QMessageBox::warning(this, tr("LameXP"), tr("Sorry, an unsupported encoder has been chosen!"));
1567                 ui->tabWidget->setCurrentIndex(3);
1568                 return;
1569         }
1570
1571         if(!m_settings->outputToSourceDir())
1572         {
1573                 QFile writeTest(QString("%1/~%2.txt").arg(m_settings->outputDir(), lamexp_rand_str()));
1574                 if(!(writeTest.open(QIODevice::ReadWrite) && (writeTest.write(writeTestBuffer) == strlen(writeTestBuffer))))
1575                 {
1576                         QMessageBox::warning(this, tr("LameXP"), QString("%1<br><nobr>%2</nobr><br><br>%3").arg(tr("Cannot write to the selected output directory."), m_settings->outputDir(), tr("Please choose a different directory!")));
1577                         ui->tabWidget->setCurrentIndex(1);
1578                         return;
1579                 }
1580                 else
1581                 {
1582                         writeTest.close();
1583                         writeTest.remove();
1584                 }
1585         }
1586
1587         m_accepted = true;
1588         close();
1589 }
1590
1591 /*
1592  * About button
1593  */
1594 void MainWindow::aboutButtonClicked(void)
1595 {
1596         ABORT_IF_BUSY;
1597
1598         TEMP_HIDE_DROPBOX
1599         (
1600                 AboutDialog *aboutBox = new AboutDialog(m_settings, this);
1601                 aboutBox->exec();
1602                 LAMEXP_DELETE(aboutBox);
1603         );
1604 }
1605
1606 /*
1607  * Close button
1608  */
1609 void MainWindow::closeButtonClicked(void)
1610 {
1611         ABORT_IF_BUSY;
1612         close();
1613 }
1614
1615 // =========================================================
1616 // Tab widget slots
1617 // =========================================================
1618
1619 /*
1620  * Tab page changed
1621  */
1622 void MainWindow::tabPageChanged(int idx, const bool silent)
1623 {
1624         resizeEvent(NULL);
1625         
1626         //Update "view" menu
1627         QList<QAction*> actions = m_tabActionGroup->actions();
1628         for(int i = 0; i < actions.count(); i++)
1629         {
1630                 bool ok = false;
1631                 int actionIndex = actions.at(i)->data().toInt(&ok);
1632                 if(ok && actionIndex == idx)
1633                 {
1634                         actions.at(i)->setChecked(true);
1635                 }
1636         }
1637
1638         //Play tick sound
1639         if(m_settings->soundsEnabled() && (!silent))
1640         {
1641                 lamexp_play_sound(IDR_WAVE_TICK, true);
1642         }
1643
1644         int initialWidth = this->width();
1645         int maximumWidth = QApplication::desktop()->availableGeometry().width();
1646
1647         //Make sure all tab headers are fully visible
1648         if(this->isVisible())
1649         {
1650                 int delta = ui->tabWidget->sizeHint().width() - ui->tabWidget->width();
1651                 if(delta > 0)
1652                 {
1653                         this->resize(qMin(this->width() + delta, maximumWidth), this->height());
1654                 }
1655         }
1656
1657         //Tab specific operations
1658         if(idx == ui->tabWidget->indexOf(ui->tabOptions) && ui->scrollArea->widget() && this->isVisible())
1659         {
1660                 ui->scrollArea->widget()->updateGeometry();
1661                 ui->scrollArea->viewport()->updateGeometry();
1662                 qApp->processEvents();
1663                 int delta = ui->scrollArea->widget()->width() - ui->scrollArea->viewport()->width();
1664                 if(delta > 0)
1665                 {
1666                         this->resize(qMin(this->width() + delta, maximumWidth), this->height());
1667                 }
1668         }
1669         else if(idx == ui->tabWidget->indexOf(ui->tabSourceFiles))
1670         {
1671                 m_dropNoteLabel->setGeometry(0, 0, ui->sourceFileView->width(), ui->sourceFileView->height());
1672         }
1673         else if(idx == ui->tabWidget->indexOf(ui->tabOutputDir))
1674         {
1675                 if(!m_fileSystemModel)
1676                 {
1677                         QTimer::singleShot(125, this, SLOT(initOutputFolderModel()));
1678                 }
1679                 else
1680                 {
1681                         CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
1682                 }
1683         }
1684
1685         //Center window around previous position
1686         if(initialWidth < this->width())
1687         {
1688                 QPoint prevPos = this->pos();
1689                 int delta = (this->width() - initialWidth) >> 2;
1690                 move(prevPos.x() - delta, prevPos.y());
1691         }
1692 }
1693
1694 /*
1695  * Tab action triggered
1696  */
1697 void MainWindow::tabActionActivated(QAction *action)
1698 {
1699         if(action && action->data().isValid())
1700         {
1701                 bool ok = false;
1702                 int index = action->data().toInt(&ok);
1703                 if(ok)
1704                 {
1705                         ui->tabWidget->setCurrentIndex(index);
1706                 }
1707         }
1708 }
1709
1710 // =========================================================
1711 // View menu slots
1712 // =========================================================
1713
1714 /*
1715  * Style action triggered
1716  */
1717 void MainWindow::styleActionActivated(QAction *action)
1718 {
1719         //Change style setting
1720         if(action && action->data().isValid())
1721         {
1722                 bool ok = false;
1723                 int actionIndex = action->data().toInt(&ok);
1724                 if(ok)
1725                 {
1726                         m_settings->interfaceStyle(actionIndex);
1727                 }
1728         }
1729
1730         //Set up the new style
1731         switch(m_settings->interfaceStyle())
1732         {
1733         case 1:
1734                 if(ui->actionStyleCleanlooks->isEnabled())
1735                 {
1736                         ui->actionStyleCleanlooks->setChecked(true);
1737                         QApplication::setStyle(new QCleanlooksStyle());
1738                         break;
1739                 }
1740         case 2:
1741                 if(ui->actionStyleWindowsVista->isEnabled())
1742                 {
1743                         ui->actionStyleWindowsVista->setChecked(true);
1744                         QApplication::setStyle(new QWindowsVistaStyle());
1745                         break;
1746                 }
1747         case 3:
1748                 if(ui->actionStyleWindowsXP->isEnabled())
1749                 {
1750                         ui->actionStyleWindowsXP->setChecked(true);
1751                         QApplication::setStyle(new QWindowsXPStyle());
1752                         break;
1753                 }
1754         case 4:
1755                 if(ui->actionStyleWindowsClassic->isEnabled())
1756                 {
1757                         ui->actionStyleWindowsClassic->setChecked(true);
1758                         QApplication::setStyle(new QWindowsStyle());
1759                         break;
1760                 }
1761         default:
1762                 ui->actionStylePlastique->setChecked(true);
1763                 QApplication::setStyle(new QPlastiqueStyle());
1764                 break;
1765         }
1766
1767         //Force re-translate after style change
1768         if(QEvent *e = new QEvent(QEvent::LanguageChange))
1769         {
1770                 changeEvent(e);
1771                 LAMEXP_DELETE(e);
1772         }
1773
1774         //Make transparent
1775         const type_info &styleType = typeid(*qApp->style());
1776         const bool bTransparent = ((typeid(QWindowsVistaStyle) == styleType) || (typeid(QWindowsXPStyle) == styleType));
1777         MAKE_TRANSPARENT(ui->scrollArea, bTransparent);
1778
1779         //Also force a re-size event
1780         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
1781         resizeEvent(NULL);
1782 }
1783
1784 /*
1785  * Language action triggered
1786  */
1787 void MainWindow::languageActionActivated(QAction *action)
1788 {
1789         if(action->data().type() == QVariant::String)
1790         {
1791                 QString langId = action->data().toString();
1792
1793                 if(lamexp_install_translator(langId))
1794                 {
1795                         action->setChecked(true);
1796                         ui->actionLoadTranslationFromFile->setChecked(false);
1797                         m_settings->currentLanguage(langId);
1798                         m_settings->currentLanguageFile(QString());
1799                 }
1800         }
1801 }
1802
1803 /*
1804  * Load language from file action triggered
1805  */
1806 void MainWindow::languageFromFileActionActivated(bool checked)
1807 {
1808         QFileDialog dialog(this, tr("Load Translation"));
1809         dialog.setFileMode(QFileDialog::ExistingFile);
1810         dialog.setNameFilter(QString("%1 (*.qm)").arg(tr("Translation Files")));
1811
1812         if(dialog.exec())
1813         {
1814                 QStringList selectedFiles = dialog.selectedFiles();
1815                 const QString qmFile = QFileInfo(selectedFiles.first()).canonicalFilePath();
1816                 if(lamexp_install_translator_from_file(qmFile))
1817                 {
1818                         QList<QAction*> actions = m_languageActionGroup->actions();
1819                         while(!actions.isEmpty())
1820                         {
1821                                 actions.takeFirst()->setChecked(false);
1822                         }
1823                         ui->actionLoadTranslationFromFile->setChecked(true);
1824                         m_settings->currentLanguageFile(qmFile);
1825                 }
1826                 else
1827                 {
1828                         languageActionActivated(m_languageActionGroup->actions().first());
1829                 }
1830         }
1831 }
1832
1833 // =========================================================
1834 // Tools menu slots
1835 // =========================================================
1836
1837 /*
1838  * Disable update reminder action
1839  */
1840 void MainWindow::disableUpdateReminderActionTriggered(bool checked)
1841 {
1842         if(checked)
1843         {
1844                 if(0 == QMessageBox::question(this, tr("Disable Update Reminder"), NOBR(tr("Do you really want to disable the update reminder?")), tr("Yes"), tr("No"), QString(), 1))
1845                 {
1846                         QMessageBox::information(this, tr("Update Reminder"), QString("%1<br>%2").arg(NOBR(tr("The update reminder has been disabled.")), NOBR(tr("Please remember to check for updates at regular intervals!"))));
1847                         m_settings->autoUpdateEnabled(false);
1848                 }
1849                 else
1850                 {
1851                         m_settings->autoUpdateEnabled(true);
1852                 }
1853         }
1854         else
1855         {
1856                         QMessageBox::information(this, tr("Update Reminder"), NOBR(tr("The update reminder has been re-enabled.")));
1857                         m_settings->autoUpdateEnabled(true);
1858         }
1859
1860         ui->actionDisableUpdateReminder->setChecked(!m_settings->autoUpdateEnabled());
1861 }
1862
1863 /*
1864  * Disable sound effects action
1865  */
1866 void MainWindow::disableSoundsActionTriggered(bool checked)
1867 {
1868         if(checked)
1869         {
1870                 if(0 == QMessageBox::question(this, tr("Disable Sound Effects"), NOBR(tr("Do you really want to disable all sound effects?")), tr("Yes"), tr("No"), QString(), 1))
1871                 {
1872                         QMessageBox::information(this, tr("Sound Effects"), NOBR(tr("All sound effects have been disabled.")));
1873                         m_settings->soundsEnabled(false);
1874                 }
1875                 else
1876                 {
1877                         m_settings->soundsEnabled(true);
1878                 }
1879         }
1880         else
1881         {
1882                         QMessageBox::information(this, tr("Sound Effects"), NOBR(tr("The sound effects have been re-enabled.")));
1883                         m_settings->soundsEnabled(true);
1884         }
1885
1886         ui->actionDisableSounds->setChecked(!m_settings->soundsEnabled());
1887 }
1888
1889 /*
1890  * Disable Nero AAC encoder action
1891  */
1892 void MainWindow::disableNeroAacNotificationsActionTriggered(bool checked)
1893 {
1894         if(checked)
1895         {
1896                 if(0 == QMessageBox::question(this, tr("Nero AAC Notifications"), NOBR(tr("Do you really want to disable all Nero AAC Encoder notifications?")), tr("Yes"), tr("No"), QString(), 1))
1897                 {
1898                         QMessageBox::information(this, tr("Nero AAC Notifications"), NOBR(tr("All Nero AAC Encoder notifications have been disabled.")));
1899                         m_settings->neroAacNotificationsEnabled(false);
1900                 }
1901                 else
1902                 {
1903                         m_settings->neroAacNotificationsEnabled(true);
1904                 }
1905         }
1906         else
1907         {
1908                         QMessageBox::information(this, tr("Nero AAC Notifications"), NOBR(tr("The Nero AAC Encoder notifications have been re-enabled.")));
1909                         m_settings->neroAacNotificationsEnabled(true);
1910         }
1911
1912         ui->actionDisableNeroAacNotifications->setChecked(!m_settings->neroAacNotificationsEnabled());
1913 }
1914
1915 /*
1916  * Disable slow startup action
1917  */
1918 void MainWindow::disableSlowStartupNotificationsActionTriggered(bool checked)
1919 {
1920         if(checked)
1921         {
1922                 if(0 == QMessageBox::question(this, tr("Slow Startup Notifications"), NOBR(tr("Do you really want to disable the slow startup notifications?")), tr("Yes"), tr("No"), QString(), 1))
1923                 {
1924                         QMessageBox::information(this, tr("Slow Startup Notifications"), NOBR(tr("The slow startup notifications have been disabled.")));
1925                         m_settings->antivirNotificationsEnabled(false);
1926                 }
1927                 else
1928                 {
1929                         m_settings->antivirNotificationsEnabled(true);
1930                 }
1931         }
1932         else
1933         {
1934                         QMessageBox::information(this, tr("Slow Startup Notifications"), NOBR(tr("The slow startup notifications have been re-enabled.")));
1935                         m_settings->antivirNotificationsEnabled(true);
1936         }
1937
1938         ui->actionDisableSlowStartupNotifications->setChecked(!m_settings->antivirNotificationsEnabled());
1939 }
1940
1941 /*
1942  * Import a Cue Sheet file
1943  */
1944 void MainWindow::importCueSheetActionTriggered(bool checked)
1945 {
1946         ABORT_IF_BUSY;
1947         
1948         TEMP_HIDE_DROPBOX
1949         (
1950                 while(true)
1951                 {
1952                         int result = 0;
1953                         QString selectedCueFile;
1954
1955                         if(lamexp_themes_enabled())
1956                         {
1957                                 selectedCueFile = QFileDialog::getOpenFileName(this, tr("Open Cue Sheet"), m_settings->mostRecentInputPath(), QString("%1 (*.cue)").arg(tr("Cue Sheet File")));
1958                         }
1959                         else
1960                         {
1961                                 QFileDialog dialog(this, tr("Open Cue Sheet"));
1962                                 dialog.setFileMode(QFileDialog::ExistingFile);
1963                                 dialog.setNameFilter(QString("%1 (*.cue)").arg(tr("Cue Sheet File")));
1964                                 dialog.setDirectory(m_settings->mostRecentInputPath());
1965                                 if(dialog.exec())
1966                                 {
1967                                         selectedCueFile = dialog.selectedFiles().first();
1968                                 }
1969                         }
1970
1971                         if(!selectedCueFile.isEmpty())
1972                         {
1973                                 m_settings->mostRecentInputPath(QFileInfo(selectedCueFile).canonicalPath());
1974                                 CueImportDialog *cueImporter  = new CueImportDialog(this, m_fileListModel, selectedCueFile, m_settings);
1975                                 result = cueImporter->exec();
1976                                 LAMEXP_DELETE(cueImporter);
1977                         }
1978
1979                         if(result == QDialog::Accepted)
1980                         {
1981                                 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
1982                                 ui->sourceFileView->update();
1983                                 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
1984                                 ui->sourceFileView->scrollToBottom();
1985                                 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
1986                         }
1987
1988                         if(result != (-1)) break;
1989                 }
1990         );
1991 }
1992
1993 /*
1994  * Show the "drop box" widget
1995  */
1996 void MainWindow::showDropBoxWidgetActionTriggered(bool checked)
1997 {
1998         m_settings->dropBoxWidgetEnabled(true);
1999         
2000         if(!m_dropBox->isVisible())
2001         {
2002                 m_dropBox->show();
2003                 QTimer::singleShot(2500, m_dropBox, SLOT(showToolTip()));
2004         }
2005         
2006         lamexp_blink_window(m_dropBox);
2007 }
2008
2009 /*
2010  * Check for beta (pre-release) updates
2011  */
2012 void MainWindow::checkForBetaUpdatesActionTriggered(bool checked)
2013 {       
2014         bool checkUpdatesNow = false;
2015         
2016         if(checked)
2017         {
2018                 if(0 == QMessageBox::question(this, tr("Beta Updates"), NOBR(tr("Do you really want LameXP to check for Beta (pre-release) updates?")), tr("Yes"), tr("No"), QString(), 1))
2019                 {
2020                         if(0 == QMessageBox::information(this, tr("Beta Updates"), NOBR(tr("LameXP will check for Beta (pre-release) updates from now on.")), tr("Check Now"), tr("Discard")))
2021                         {
2022                                 checkUpdatesNow = true;
2023                         }
2024                         m_settings->autoUpdateCheckBeta(true);
2025                 }
2026                 else
2027                 {
2028                         m_settings->autoUpdateCheckBeta(false);
2029                 }
2030         }
2031         else
2032         {
2033                         QMessageBox::information(this, tr("Beta Updates"), NOBR(tr("LameXP will <i>not</i> check for Beta (pre-release) updates from now on.")));
2034                         m_settings->autoUpdateCheckBeta(false);
2035         }
2036
2037         ui->actionCheckForBetaUpdates->setChecked(m_settings->autoUpdateCheckBeta());
2038
2039         if(checkUpdatesNow)
2040         {
2041                 if(checkForUpdates())
2042                 {
2043                         QApplication::quit();
2044                 }
2045         }
2046 }
2047
2048 /*
2049  * Hibernate computer action
2050  */
2051 void MainWindow::hibernateComputerActionTriggered(bool checked)
2052 {
2053         if(checked)
2054         {
2055                 if(0 == QMessageBox::question(this, tr("Hibernate Computer"), NOBR(tr("Do you really want the computer to be hibernated on shutdown?")), tr("Yes"), tr("No"), QString(), 1))
2056                 {
2057                         QMessageBox::information(this, tr("Hibernate Computer"), NOBR(tr("LameXP will hibernate the computer on shutdown from now on.")));
2058                         m_settings->hibernateComputer(true);
2059                 }
2060                 else
2061                 {
2062                         m_settings->hibernateComputer(false);
2063                 }
2064         }
2065         else
2066         {
2067                         QMessageBox::information(this, tr("Hibernate Computer"), NOBR(tr("LameXP will <i>not</i> hibernate the computer on shutdown from now on.")));
2068                         m_settings->hibernateComputer(false);
2069         }
2070
2071         ui->actionHibernateComputer->setChecked(m_settings->hibernateComputer());
2072 }
2073
2074 /*
2075  * Disable shell integration action
2076  */
2077 void MainWindow::disableShellIntegrationActionTriggered(bool checked)
2078 {
2079         if(checked)
2080         {
2081                 if(0 == QMessageBox::question(this, tr("Shell Integration"), NOBR(tr("Do you really want to disable the LameXP shell integration?")), tr("Yes"), tr("No"), QString(), 1))
2082                 {
2083                         ShellIntegration::remove();
2084                         QMessageBox::information(this, tr("Shell Integration"), NOBR(tr("The LameXP shell integration has been disabled.")));
2085                         m_settings->shellIntegrationEnabled(false);
2086                 }
2087                 else
2088                 {
2089                         m_settings->shellIntegrationEnabled(true);
2090                 }
2091         }
2092         else
2093         {
2094                         ShellIntegration::install();
2095                         QMessageBox::information(this, tr("Shell Integration"), NOBR(tr("The LameXP shell integration has been re-enabled.")));
2096                         m_settings->shellIntegrationEnabled(true);
2097         }
2098
2099         ui->actionDisableShellIntegration->setChecked(!m_settings->shellIntegrationEnabled());
2100         
2101         if(lamexp_portable_mode() && ui->actionDisableShellIntegration->isChecked())
2102         {
2103                 ui->actionDisableShellIntegration->setEnabled(false);
2104         }
2105 }
2106
2107 // =========================================================
2108 // Help menu slots
2109 // =========================================================
2110
2111 /*
2112  * Visit homepage action
2113  */
2114 void MainWindow::visitHomepageActionActivated(void)
2115 {
2116         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
2117         {
2118                 if(action->data().isValid() && (action->data().type() == QVariant::String))
2119                 {
2120                         QDesktopServices::openUrl(QUrl(action->data().toString()));
2121                 }
2122         }
2123 }
2124
2125 /*
2126  * Show document
2127  */
2128 void MainWindow::documentActionActivated(void)
2129 {
2130         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
2131         {
2132                 if(action->data().isValid() && (action->data().type() == QVariant::String))
2133                 {
2134                         QFileInfo document(action->data().toString());
2135                         QFileInfo resource(QString(":/doc/%1.html").arg(document.baseName()));
2136                         if(document.exists() && document.isFile() && (document.size() == resource.size()))
2137                         {
2138                                 QDesktopServices::openUrl(QUrl::fromLocalFile(document.canonicalFilePath()));
2139                         }
2140                         else
2141                         {
2142                                 QFile source(resource.filePath());
2143                                 QFile output(QString("%1/%2.%3.html").arg(lamexp_temp_folder2(), document.baseName(), lamexp_rand_str().left(8)));
2144                                 if(source.open(QIODevice::ReadOnly) && output.open(QIODevice::ReadWrite))
2145                                 {
2146                                         output.write(source.readAll());
2147                                         action->setData(output.fileName());
2148                                         source.close();
2149                                         output.close();
2150                                         QDesktopServices::openUrl(QUrl::fromLocalFile(output.fileName()));
2151                                 }
2152                         }
2153                 }
2154         }
2155 }
2156
2157 /*
2158  * Check for updates action
2159  */
2160 void MainWindow::checkUpdatesActionActivated(void)
2161 {
2162         ABORT_IF_BUSY;
2163         bool bFlag = false;
2164
2165         TEMP_HIDE_DROPBOX
2166         (
2167                 bFlag = checkForUpdates();
2168         );
2169         
2170         if(bFlag)
2171         {
2172                 QApplication::quit();
2173         }
2174 }
2175
2176 // =========================================================
2177 // Source file slots
2178 // =========================================================
2179
2180 /*
2181  * Add file(s) button
2182  */
2183 void MainWindow::addFilesButtonClicked(void)
2184 {
2185         ABORT_IF_BUSY;
2186
2187         TEMP_HIDE_DROPBOX
2188         (
2189                 if(lamexp_themes_enabled())
2190                 {
2191                         QStringList fileTypeFilters = DecoderRegistry::getSupportedTypes();
2192                         QStringList selectedFiles = QFileDialog::getOpenFileNames(this, tr("Add file(s)"), m_settings->mostRecentInputPath(), fileTypeFilters.join(";;"));
2193                         if(!selectedFiles.isEmpty())
2194                         {
2195                                 m_settings->mostRecentInputPath(QFileInfo(selectedFiles.first()).canonicalPath());
2196                                 addFiles(selectedFiles);
2197                         }
2198                 }
2199                 else
2200                 {
2201                         QFileDialog dialog(this, tr("Add file(s)"));
2202                         QStringList fileTypeFilters = DecoderRegistry::getSupportedTypes();
2203                         dialog.setFileMode(QFileDialog::ExistingFiles);
2204                         dialog.setNameFilter(fileTypeFilters.join(";;"));
2205                         dialog.setDirectory(m_settings->mostRecentInputPath());
2206                         if(dialog.exec())
2207                         {
2208                                 QStringList selectedFiles = dialog.selectedFiles();
2209                                 if(!selectedFiles.isEmpty())
2210                                 {
2211                                         m_settings->mostRecentInputPath(QFileInfo(selectedFiles.first()).canonicalPath());
2212                                         addFiles(selectedFiles);
2213                                 }
2214                         }
2215                 }
2216         );
2217 }
2218
2219 /*
2220  * Open folder action
2221  */
2222 void MainWindow::openFolderActionActivated(void)
2223 {
2224         ABORT_IF_BUSY;
2225         QString selectedFolder;
2226         
2227         if(QAction *action = dynamic_cast<QAction*>(QObject::sender()))
2228         {
2229                 TEMP_HIDE_DROPBOX
2230                 (
2231                         if(lamexp_themes_enabled())
2232                         {
2233                                 selectedFolder = QFileDialog::getExistingDirectory(this, tr("Add Folder"), m_settings->mostRecentInputPath());
2234                         }
2235                         else
2236                         {
2237                                 QFileDialog dialog(this, tr("Add Folder"));
2238                                 dialog.setFileMode(QFileDialog::DirectoryOnly);
2239                                 dialog.setDirectory(m_settings->mostRecentInputPath());
2240                                 if(dialog.exec())
2241                                 {
2242                                         selectedFolder = dialog.selectedFiles().first();
2243                                 }
2244                         }
2245                         
2246                         if(!selectedFolder.isEmpty())
2247                         {
2248                                 m_settings->mostRecentInputPath(QDir(selectedFolder).canonicalPath());
2249                                 addFolder(selectedFolder, action->data().toBool());
2250                         }
2251                 );
2252         }
2253 }
2254
2255 /*
2256  * Remove file button
2257  */
2258 void MainWindow::removeFileButtonClicked(void)
2259 {
2260         if(ui->sourceFileView->currentIndex().isValid())
2261         {
2262                 int iRow = ui->sourceFileView->currentIndex().row();
2263                 m_fileListModel->removeFile(ui->sourceFileView->currentIndex());
2264                 ui->sourceFileView->selectRow(iRow < m_fileListModel->rowCount() ? iRow : m_fileListModel->rowCount()-1);
2265         }
2266 }
2267
2268 /*
2269  * Clear files button
2270  */
2271 void MainWindow::clearFilesButtonClicked(void)
2272 {
2273         m_fileListModel->clearFiles();
2274 }
2275
2276 /*
2277  * Move file up button
2278  */
2279 void MainWindow::fileUpButtonClicked(void)
2280 {
2281         if(ui->sourceFileView->currentIndex().isValid())
2282         {
2283                 int iRow = ui->sourceFileView->currentIndex().row() - 1;
2284                 m_fileListModel->moveFile(ui->sourceFileView->currentIndex(), -1);
2285                 ui->sourceFileView->selectRow(iRow >= 0 ? iRow : 0);
2286         }
2287 }
2288
2289 /*
2290  * Move file down button
2291  */
2292 void MainWindow::fileDownButtonClicked(void)
2293 {
2294         if(ui->sourceFileView->currentIndex().isValid())
2295         {
2296                 int iRow = ui->sourceFileView->currentIndex().row() + 1;
2297                 m_fileListModel->moveFile(ui->sourceFileView->currentIndex(), 1);
2298                 ui->sourceFileView->selectRow(iRow < m_fileListModel->rowCount() ? iRow : m_fileListModel->rowCount()-1);
2299         }
2300 }
2301
2302 /*
2303  * Show details button
2304  */
2305 void MainWindow::showDetailsButtonClicked(void)
2306 {
2307         ABORT_IF_BUSY;
2308
2309         int iResult = 0;
2310         MetaInfoDialog *metaInfoDialog = new MetaInfoDialog(this);
2311         QModelIndex index = ui->sourceFileView->currentIndex();
2312
2313         while(index.isValid())
2314         {
2315                 if(iResult > 0)
2316                 {
2317                         index = m_fileListModel->index(index.row() + 1, index.column()); 
2318                         ui->sourceFileView->selectRow(index.row());
2319                 }
2320                 if(iResult < 0)
2321                 {
2322                         index = m_fileListModel->index(index.row() - 1, index.column()); 
2323                         ui->sourceFileView->selectRow(index.row());
2324                 }
2325
2326                 AudioFileModel &file = (*m_fileListModel)[index];
2327                 TEMP_HIDE_DROPBOX
2328                 (
2329                         iResult = metaInfoDialog->exec(file, index.row() > 0, index.row() < m_fileListModel->rowCount() - 1);
2330                 );
2331                 
2332                 //Copy all info to Meta Info tab
2333                 if(iResult == INT_MAX)
2334                 {
2335                         m_metaInfoModel->assignInfoFrom(file);
2336                         ui->tabWidget->setCurrentIndex(ui->tabWidget->indexOf(ui->tabMetaData));
2337                         break;
2338                 }
2339
2340                 if(!iResult) break;
2341         }
2342
2343         LAMEXP_DELETE(metaInfoDialog);
2344         QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
2345         sourceFilesScrollbarMoved(0);
2346 }
2347
2348 /*
2349  * Show context menu for source files
2350  */
2351 void MainWindow::sourceFilesContextMenu(const QPoint &pos)
2352 {
2353         QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(QObject::sender());
2354         QWidget *sender = scrollArea ? scrollArea->viewport() : dynamic_cast<QWidget*>(QObject::sender());
2355
2356         if(sender)
2357         {
2358                 if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0)
2359                 {
2360                         m_sourceFilesContextMenu->popup(sender->mapToGlobal(pos));
2361                 }
2362         }
2363 }
2364
2365 /*
2366  * Scrollbar of source files moved
2367  */
2368 void MainWindow::sourceFilesScrollbarMoved(int)
2369 {
2370         ui->sourceFileView->resizeColumnToContents(0);
2371 }
2372
2373 /*
2374  * Open selected file in external player
2375  */
2376 void MainWindow::previewContextActionTriggered(void)
2377 {
2378         QModelIndex index = ui->sourceFileView->currentIndex();
2379         if(!index.isValid())
2380         {
2381                 return;
2382         }
2383
2384         if(!lamexp_open_media_file(m_fileListModel->getFile(index).filePath()))
2385         {
2386                 qDebug("Player not found, falling back to default application...");
2387                 QDesktopServices::openUrl(QString("file:///").append(m_fileListModel->getFile(index).filePath()));
2388         }
2389 }
2390
2391 /*
2392  * Find selected file in explorer
2393  */
2394 void MainWindow::findFileContextActionTriggered(void)
2395 {
2396         QModelIndex index = ui->sourceFileView->currentIndex();
2397         if(index.isValid())
2398         {
2399                 QString systemRootPath;
2400
2401                 QDir systemRoot(lamexp_known_folder(lamexp_folder_systemfolder));
2402                 if(systemRoot.exists() && systemRoot.cdUp())
2403                 {
2404                         systemRootPath = systemRoot.canonicalPath();
2405                 }
2406
2407                 if(!systemRootPath.isEmpty())
2408                 {
2409                         QFileInfo explorer(QString("%1/explorer.exe").arg(systemRootPath));
2410                         if(explorer.exists() && explorer.isFile())
2411                         {
2412                                 QProcess::execute(explorer.canonicalFilePath(), QStringList() << "/select," << QDir::toNativeSeparators(m_fileListModel->getFile(index).filePath()));
2413                                 return;
2414                         }
2415                 }
2416                 else
2417                 {
2418                         qWarning("SystemRoot directory could not be detected!");
2419                 }
2420         }
2421 }
2422
2423 /*
2424  * Add all pending files
2425  */
2426 void MainWindow::handleDelayedFiles(void)
2427 {
2428         m_delayedFileTimer->stop();
2429
2430         if(m_delayedFileList->isEmpty())
2431         {
2432                 return;
2433         }
2434
2435         if(m_banner->isVisible())
2436         {
2437                 m_delayedFileTimer->start(5000);
2438                 return;
2439         }
2440
2441         QStringList selectedFiles;
2442         ui->tabWidget->setCurrentIndex(0);
2443
2444         while(!m_delayedFileList->isEmpty())
2445         {
2446                 QFileInfo currentFile = QFileInfo(m_delayedFileList->takeFirst());
2447                 if(!currentFile.exists() || !currentFile.isFile())
2448                 {
2449                         continue;
2450                 }
2451                 selectedFiles << currentFile.canonicalFilePath();
2452         }
2453         
2454         addFiles(selectedFiles);
2455 }
2456
2457 /*
2458  * Export Meta tags to CSV file
2459  */
2460 void MainWindow::exportCsvContextActionTriggered(void)
2461 {
2462         TEMP_HIDE_DROPBOX
2463         (
2464                 QString selectedCsvFile;
2465         
2466                 if(lamexp_themes_enabled())
2467                 {
2468                         selectedCsvFile = QFileDialog::getSaveFileName(this, tr("Save CSV file"), m_settings->mostRecentInputPath(), QString("%1 (*.csv)").arg(tr("CSV File")));
2469                 }
2470                 else
2471                 {
2472                         QFileDialog dialog(this, tr("Save CSV file"));
2473                         dialog.setFileMode(QFileDialog::AnyFile);
2474                         dialog.setAcceptMode(QFileDialog::AcceptSave);
2475                         dialog.setNameFilter(QString("%1 (*.csv)").arg(tr("CSV File")));
2476                         dialog.setDirectory(m_settings->mostRecentInputPath());
2477                         if(dialog.exec())
2478                         {
2479                                 selectedCsvFile = dialog.selectedFiles().first();
2480                         }
2481                 }
2482
2483                 if(!selectedCsvFile.isEmpty())
2484                 {
2485                         m_settings->mostRecentInputPath(QFileInfo(selectedCsvFile).canonicalPath());
2486                         switch(m_fileListModel->exportToCsv(selectedCsvFile))
2487                         {
2488                         case FileListModel::CsvError_NoTags:
2489                                 QMessageBox::critical(this, tr("CSV Export"), NOBR(tr("Sorry, there are no meta tags that can be exported!")));
2490                                 break;
2491                         case FileListModel::CsvError_FileOpen:
2492                                 QMessageBox::critical(this, tr("CSV Export"), NOBR(tr("Sorry, failed to open CSV file for writing!")));
2493                                 break;
2494                         case FileListModel::CsvError_FileWrite:
2495                                 QMessageBox::critical(this, tr("CSV Export"), NOBR(tr("Sorry, failed to write to the CSV file!")));
2496                                 break;
2497                         case FileListModel::CsvError_OK:
2498                                 QMessageBox::information(this, tr("CSV Export"), NOBR(tr("The CSV files was created successfully!")));
2499                                 break;
2500                         default:
2501                                 qWarning("exportToCsv: Unknown return code!");
2502                         }
2503                 }
2504         );
2505 }
2506
2507
2508 /*
2509  * Import Meta tags from CSV file
2510  */
2511 void MainWindow::importCsvContextActionTriggered(void)
2512 {
2513         TEMP_HIDE_DROPBOX
2514         (
2515                 QString selectedCsvFile;
2516         
2517                 if(lamexp_themes_enabled())
2518                 {
2519                         selectedCsvFile = QFileDialog::getOpenFileName(this, tr("Open CSV file"), m_settings->mostRecentInputPath(), QString("%1 (*.csv)").arg(tr("CSV File")));
2520                 }
2521                 else
2522                 {
2523                         QFileDialog dialog(this, tr("Open CSV file"));
2524                         dialog.setFileMode(QFileDialog::ExistingFile);
2525                         dialog.setNameFilter(QString("%1 (*.csv)").arg(tr("CSV File")));
2526                         dialog.setDirectory(m_settings->mostRecentInputPath());
2527                         if(dialog.exec())
2528                         {
2529                                 selectedCsvFile = dialog.selectedFiles().first();
2530                         }
2531                 }
2532
2533                 if(!selectedCsvFile.isEmpty())
2534                 {
2535                         m_settings->mostRecentInputPath(QFileInfo(selectedCsvFile).canonicalPath());
2536                         switch(m_fileListModel->importFromCsv(this, selectedCsvFile))
2537                         {
2538                         case FileListModel::CsvError_FileOpen:
2539                                 QMessageBox::critical(this, tr("CSV Import"), NOBR(tr("Sorry, failed to open CSV file for reading!")));
2540                                 break;
2541                         case FileListModel::CsvError_FileRead:
2542                                 QMessageBox::critical(this, tr("CSV Import"), NOBR(tr("Sorry, failed to read from the CSV file!")));
2543                                 break;
2544                         case FileListModel::CsvError_NoTags:
2545                                 QMessageBox::critical(this, tr("CSV Import"), NOBR(tr("Sorry, the CSV file does not contain any known fields!")));
2546                                 break;
2547                         case FileListModel::CsvError_Incomplete:
2548                                 QMessageBox::warning(this, tr("CSV Import"), NOBR(tr("CSV file is incomplete. Not all files were updated!")));
2549                                 break;
2550                         case FileListModel::CsvError_OK:
2551                                 QMessageBox::information(this, tr("CSV Import"), NOBR(tr("The CSV files was imported successfully!")));
2552                                 break;
2553                         case FileListModel::CsvError_Aborted:
2554                                 /* User aborted, ignore! */
2555                                 break;
2556                         default:
2557                                 qWarning("exportToCsv: Unknown return code!");
2558                         }
2559                 }
2560         );
2561 }
2562
2563 /*
2564  * Show or hide Drag'n'Drop notice after model reset
2565  */
2566 void MainWindow::sourceModelChanged(void)
2567 {
2568         m_dropNoteLabel->setVisible(m_fileListModel->rowCount() <= 0);
2569 }
2570
2571 // =========================================================
2572 // Output folder slots
2573 // =========================================================
2574
2575 /*
2576  * Output folder changed (mouse clicked)
2577  */
2578 void MainWindow::outputFolderViewClicked(const QModelIndex &index)
2579 {
2580         if(index.isValid() && (ui->outputFolderView->currentIndex() != index))
2581         {
2582                 ui->outputFolderView->setCurrentIndex(index);
2583         }
2584         
2585         if(m_fileSystemModel && index.isValid())
2586         {
2587                 QString selectedDir = m_fileSystemModel->filePath(index);
2588                 if(selectedDir.length() < 3) selectedDir.append(QDir::separator());
2589                 ui->outputFolderLabel->setText(QDir::toNativeSeparators(selectedDir));
2590                 ui->outputFolderLabel->setToolTip(ui->outputFolderLabel->text());
2591                 m_settings->outputDir(selectedDir);
2592         }
2593         else
2594         {
2595                 ui->outputFolderLabel->setText(QDir::toNativeSeparators(m_settings->outputDir()));
2596                 ui->outputFolderLabel->setToolTip(ui->outputFolderLabel->text());
2597         }
2598 }
2599
2600 /*
2601  * Output folder changed (mouse moved)
2602  */
2603 void MainWindow::outputFolderViewMoved(const QModelIndex &index)
2604 {
2605         if(QApplication::mouseButtons() & Qt::LeftButton)
2606         {
2607                 outputFolderViewClicked(index);
2608         }
2609 }
2610
2611 /*
2612  * Goto desktop button
2613  */
2614 void MainWindow::gotoDesktopButtonClicked(void)
2615 {
2616         if(!m_fileSystemModel)
2617         {
2618                 qWarning("File system model not initialized yet!");
2619                 return;
2620         }
2621         
2622         QString desktopPath = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
2623         
2624         if(!desktopPath.isEmpty() && QDir(desktopPath).exists())
2625         {
2626                 ui->outputFolderView->setCurrentIndex(m_fileSystemModel->index(desktopPath));
2627                 outputFolderViewClicked(ui->outputFolderView->currentIndex());
2628                 CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
2629         }
2630         else
2631         {
2632                 ui->buttonGotoDesktop->setEnabled(false);
2633         }
2634 }
2635
2636 /*
2637  * Goto home folder button
2638  */
2639 void MainWindow::gotoHomeFolderButtonClicked(void)
2640 {
2641         if(!m_fileSystemModel)
2642         {
2643                 qWarning("File system model not initialized yet!");
2644                 return;
2645         }
2646
2647         QString homePath = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
2648         
2649         if(!homePath.isEmpty() && QDir(homePath).exists())
2650         {
2651                 ui->outputFolderView->setCurrentIndex(m_fileSystemModel->index(homePath));
2652                 outputFolderViewClicked(ui->outputFolderView->currentIndex());
2653                 CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
2654         }
2655         else
2656         {
2657                 ui->buttonGotoHome->setEnabled(false);
2658         }
2659 }
2660
2661 /*
2662  * Goto music folder button
2663  */
2664 void MainWindow::gotoMusicFolderButtonClicked(void)
2665 {
2666         if(!m_fileSystemModel)
2667         {
2668                 qWarning("File system model not initialized yet!");
2669                 return;
2670         }
2671
2672         QString musicPath = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
2673         
2674         if(!musicPath.isEmpty() && QDir(musicPath).exists())
2675         {
2676                 ui->outputFolderView->setCurrentIndex(m_fileSystemModel->index(musicPath));
2677                 outputFolderViewClicked(ui->outputFolderView->currentIndex());
2678                 CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
2679         }
2680         else
2681         {
2682                 ui->buttonGotoMusic->setEnabled(false);
2683         }
2684 }
2685
2686 /*
2687  * Goto music favorite output folder
2688  */
2689 void MainWindow::gotoFavoriteFolder(void)
2690 {
2691         if(!m_fileSystemModel)
2692         {
2693                 qWarning("File system model not initialized yet!");
2694                 return;
2695         }
2696
2697         QAction *item = dynamic_cast<QAction*>(QObject::sender());
2698         
2699         if(item)
2700         {
2701                 QDir path(item->data().toString());
2702                 if(path.exists())
2703                 {
2704                         ui->outputFolderView->setCurrentIndex(m_fileSystemModel->index(path.canonicalPath()));
2705                         outputFolderViewClicked(ui->outputFolderView->currentIndex());
2706                         CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
2707                 }
2708                 else
2709                 {
2710                         lamexp_beep(lamexp_beep_error);
2711                         m_outputFolderFavoritesMenu->removeAction(item);
2712                         item->deleteLater();
2713                 }
2714         }
2715 }
2716
2717 /*
2718  * Make folder button
2719  */
2720 void MainWindow::makeFolderButtonClicked(void)
2721 {
2722         ABORT_IF_BUSY;
2723
2724         if(!m_fileSystemModel)
2725         {
2726                 qWarning("File system model not initialized yet!");
2727                 return;
2728         }
2729
2730         QDir basePath(m_fileSystemModel->fileInfo(ui->outputFolderView->currentIndex()).absoluteFilePath());
2731         QString suggestedName = tr("New Folder");
2732
2733         if(!m_metaData->artist().isEmpty() && !m_metaData->album().isEmpty())
2734         {
2735                 suggestedName = QString("%1 - %2").arg(m_metaData->artist(),m_metaData->album());
2736         }
2737         else if(!m_metaData->artist().isEmpty())
2738         {
2739                 suggestedName = m_metaData->artist();
2740         }
2741         else if(!m_metaData->album().isEmpty())
2742         {
2743                 suggestedName =m_metaData->album();
2744         }
2745         else
2746         {
2747                 for(int i = 0; i < m_fileListModel->rowCount(); i++)
2748                 {
2749                         const AudioFileModel &audioFile = m_fileListModel->getFile(m_fileListModel->index(i, 0));
2750                         const AudioFileModel_MetaInfo &fileMetaInfo = audioFile.metaInfo();
2751
2752                         if(!fileMetaInfo.album().isEmpty() || !fileMetaInfo.artist().isEmpty())
2753                         {
2754                                 if(!fileMetaInfo.artist().isEmpty() && !fileMetaInfo.album().isEmpty())
2755                                 {
2756                                         suggestedName = QString("%1 - %2").arg(fileMetaInfo.artist(), fileMetaInfo.album());
2757                                 }
2758                                 else if(!fileMetaInfo.artist().isEmpty())
2759                                 {
2760                                         suggestedName = fileMetaInfo.artist();
2761                                 }
2762                                 else if(!fileMetaInfo.album().isEmpty())
2763                                 {
2764                                         suggestedName = fileMetaInfo.album();
2765                                 }
2766                                 break;
2767                         }
2768                 }
2769         }
2770         
2771         suggestedName = lamexp_clean_filename(suggestedName);
2772
2773         while(true)
2774         {
2775                 bool bApplied = false;
2776                 QString folderName = QInputDialog::getText(this, tr("New Folder"), tr("Enter the name of the new folder:").leftJustified(96, ' '), QLineEdit::Normal, suggestedName, &bApplied, Qt::WindowStaysOnTopHint).simplified();
2777
2778                 if(bApplied)
2779                 {
2780                         folderName = lamexp_clean_filepath(folderName.simplified());
2781
2782                         if(folderName.isEmpty())
2783                         {
2784                                 lamexp_beep(lamexp_beep_error);
2785                                 continue;
2786                         }
2787
2788                         int i = 1;
2789                         QString newFolder = folderName;
2790
2791                         while(basePath.exists(newFolder))
2792                         {
2793                                 newFolder = QString(folderName).append(QString().sprintf(" (%d)", ++i));
2794                         }
2795                         
2796                         if(basePath.mkpath(newFolder))
2797                         {
2798                                 QDir createdDir = basePath;
2799                                 if(createdDir.cd(newFolder))
2800                                 {
2801                                         QModelIndex newIndex = m_fileSystemModel->index(createdDir.canonicalPath());
2802                                         ui->outputFolderView->setCurrentIndex(newIndex);
2803                                         outputFolderViewClicked(newIndex);
2804                                         CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
2805                                 }
2806                         }
2807                         else
2808                         {
2809                                 QMessageBox::warning(this, tr("Failed to create folder"), QString("%1<br><nobr>%2</nobr><br><br>%3").arg(tr("The new folder could not be created:"), basePath.absoluteFilePath(newFolder), tr("Drive is read-only or insufficient access rights!")));
2810                         }
2811                 }
2812                 break;
2813         }
2814 }
2815
2816 /*
2817  * Output to source dir changed
2818  */
2819 void MainWindow::saveToSourceFolderChanged(void)
2820 {
2821         m_settings->outputToSourceDir(ui->saveToSourceFolderCheckBox->isChecked());
2822 }
2823
2824 /*
2825  * Prepend relative source file path to output file name changed
2826  */
2827 void MainWindow::prependRelativePathChanged(void)
2828 {
2829         m_settings->prependRelativeSourcePath(ui->prependRelativePathCheckBox->isChecked());
2830 }
2831
2832 /*
2833  * Show context menu for output folder
2834  */
2835 void MainWindow::outputFolderContextMenu(const QPoint &pos)
2836 {
2837         QAbstractScrollArea *scrollArea = dynamic_cast<QAbstractScrollArea*>(QObject::sender());
2838         QWidget *sender = scrollArea ? scrollArea->viewport() : dynamic_cast<QWidget*>(QObject::sender());      
2839
2840         if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0)
2841         {
2842                 m_outputFolderContextMenu->popup(sender->mapToGlobal(pos));
2843         }
2844 }
2845
2846 /*
2847  * Show selected folder in explorer
2848  */
2849 void MainWindow::showFolderContextActionTriggered(void)
2850 {
2851         if(!m_fileSystemModel)
2852         {
2853                 qWarning("File system model not initialized yet!");
2854                 return;
2855         }
2856
2857         QString path = QDir::toNativeSeparators(m_fileSystemModel->filePath(ui->outputFolderView->currentIndex()));
2858         if(!path.endsWith(QDir::separator())) path.append(QDir::separator());
2859         lamexp_exec_shell(this, path, true);
2860 }
2861
2862 /*
2863  * Refresh the directory outline
2864  */
2865 void MainWindow::refreshFolderContextActionTriggered(void)
2866 {
2867         //force re-initialization
2868         QTimer::singleShot(0, this, SLOT(initOutputFolderModel()));
2869 }
2870
2871 /*
2872  * Go one directory up
2873  */
2874 void MainWindow::goUpFolderContextActionTriggered(void)
2875 {
2876         QModelIndex current = ui->outputFolderView->currentIndex();
2877         if(current.isValid())
2878         {
2879                 QModelIndex parent = current.parent();
2880                 if(parent.isValid())
2881                 {
2882                         
2883                         ui->outputFolderView->setCurrentIndex(parent);
2884                         outputFolderViewClicked(parent);
2885                 }
2886                 else
2887                 {
2888                         lamexp_beep(lamexp_beep_warning);
2889                 }
2890                 CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
2891         }
2892 }
2893
2894 /*
2895  * Add current folder to favorites
2896  */
2897 void MainWindow::addFavoriteFolderActionTriggered(void)
2898 {
2899         QString path = m_fileSystemModel->filePath(ui->outputFolderView->currentIndex());
2900         QStringList favorites = m_settings->favoriteOutputFolders().split("|", QString::SkipEmptyParts);
2901
2902         if(!favorites.contains(path, Qt::CaseInsensitive))
2903         {
2904                 favorites.append(path);
2905                 while(favorites.count() > 6) favorites.removeFirst();
2906         }
2907         else
2908         {
2909                 lamexp_beep(lamexp_beep_warning);
2910         }
2911
2912         m_settings->favoriteOutputFolders(favorites.join("|"));
2913         refreshFavorites();
2914 }
2915
2916 /*
2917  * Output folder edit finished
2918  */
2919 void MainWindow::outputFolderEditFinished(void)
2920 {
2921         if(ui->outputFolderEdit->isHidden())
2922         {
2923                 return; //Not currently in edit mode!
2924         }
2925         
2926         bool ok = false;
2927         
2928         QString text = QDir::fromNativeSeparators(ui->outputFolderEdit->text().trimmed());
2929         while(text.startsWith('"') || text.startsWith('/')) text = text.right(text.length() - 1).trimmed();
2930         while(text.endsWith('"') || text.endsWith('/')) text = text.left(text.length() - 1).trimmed();
2931
2932         static const char *str = "?*<>|\"";
2933         for(size_t i = 0; str[i]; i++) text.replace(str[i], "_");
2934
2935         if(!((text.length() >= 2) && text.at(0).isLetter() && text.at(1) == QChar(':')))
2936         {
2937                 text = QString("%1/%2").arg(QDir::fromNativeSeparators(ui->outputFolderLabel->text()), text);
2938         }
2939
2940         if(text.length() == 2) text += "/"; /* "X:" => "X:/" */
2941
2942         while(text.length() > 2)
2943         {
2944                 QFileInfo info(text);
2945                 if(info.exists() && info.isDir())
2946                 {
2947                         QModelIndex index = m_fileSystemModel->index(QFileInfo(info.canonicalFilePath()).absoluteFilePath());
2948                         if(index.isValid())
2949                         {
2950                                 ok = true;
2951                                 ui->outputFolderView->setCurrentIndex(index);
2952                                 outputFolderViewClicked(index);
2953                                 break;
2954                         }
2955                 }
2956                 else if(info.exists() && info.isFile())
2957                 {
2958                         QModelIndex index = m_fileSystemModel->index(QFileInfo(info.canonicalPath()).absoluteFilePath());
2959                         if(index.isValid())
2960                         {
2961                                 ok = true;
2962                                 ui->outputFolderView->setCurrentIndex(index);
2963                                 outputFolderViewClicked(index);
2964                                 break;
2965                         }
2966                 }
2967
2968                 text = text.left(text.length() - 1).trimmed();
2969         }
2970
2971         ui->outputFolderEdit->setVisible(false);
2972         ui->outputFolderLabel->setVisible(true);
2973         ui->outputFolderView->setEnabled(true);
2974
2975         if(!ok) lamexp_beep(lamexp_beep_error);
2976         CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
2977 }
2978
2979 /*
2980  * Initialize file system model
2981  */
2982 void MainWindow::initOutputFolderModel(void)
2983 {
2984         if(m_outputFolderNoteBox->isHidden())
2985         {
2986                 m_outputFolderNoteBox->show();
2987                 m_outputFolderNoteBox->repaint();
2988                 m_outputFolderViewInitCounter = 4;
2989
2990                 if(m_fileSystemModel)
2991                 {
2992                         SET_MODEL(ui->outputFolderView, NULL);
2993                         LAMEXP_DELETE(m_fileSystemModel);
2994                         ui->outputFolderView->repaint();
2995                 }
2996
2997                 if(m_fileSystemModel = new QFileSystemModelEx())
2998                 {
2999                         m_fileSystemModel->installEventFilter(this);
3000                         connect(m_fileSystemModel, SIGNAL(directoryLoaded(QString)), this, SLOT(outputFolderDirectoryLoaded(QString)));
3001                         connect(m_fileSystemModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(outputFolderRowsInserted(QModelIndex,int,int)));
3002
3003                         SET_MODEL(ui->outputFolderView, m_fileSystemModel);
3004                         ui->outputFolderView->header()->setStretchLastSection(true);
3005                         ui->outputFolderView->header()->hideSection(1);
3006                         ui->outputFolderView->header()->hideSection(2);
3007                         ui->outputFolderView->header()->hideSection(3);
3008                 
3009                         m_fileSystemModel->setRootPath("");
3010                         QModelIndex index = m_fileSystemModel->index(m_settings->outputDir());
3011                         if(index.isValid()) ui->outputFolderView->setCurrentIndex(index);
3012                         outputFolderViewClicked(ui->outputFolderView->currentIndex());
3013                 }
3014
3015                 CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
3016                 QTimer::singleShot(125, this, SLOT(initOutputFolderModel_doAsync()));
3017         }
3018 }
3019
3020 /*
3021  * Initialize file system model (do NOT call this one directly!)
3022  */
3023 void MainWindow::initOutputFolderModel_doAsync(void)
3024 {
3025         if(m_outputFolderViewInitCounter > 0)
3026         {
3027                 m_outputFolderViewInitCounter--;
3028                 QTimer::singleShot(125, this, SLOT(initOutputFolderModel_doAsync()));
3029         }
3030         else
3031         {
3032                 QTimer::singleShot(125, m_outputFolderNoteBox, SLOT(hide()));
3033                 ui->outputFolderView->setFocus();
3034         }
3035 }
3036
3037 /*
3038  * Center current folder in view
3039  */
3040 void MainWindow::centerOutputFolderModel(void)
3041 {
3042         if(ui->outputFolderView->isVisible())
3043         {
3044                 centerOutputFolderModel_doAsync();
3045                 QTimer::singleShot(125, this, SLOT(centerOutputFolderModel_doAsync()));
3046         }
3047 }
3048
3049 /*
3050  * Center current folder in view (do NOT call this one directly!)
3051  */
3052 void MainWindow::centerOutputFolderModel_doAsync(void)
3053 {
3054         if(ui->outputFolderView->isVisible())
3055         {
3056                 m_outputFolderViewCentering = true;
3057                 const QModelIndex index = ui->outputFolderView->currentIndex();
3058                 ui->outputFolderView->scrollTo(index, QAbstractItemView::PositionAtCenter);
3059                 ui->outputFolderView->setFocus();
3060         }
3061 }
3062
3063 /*
3064  * File system model asynchronously loaded a dir
3065  */
3066 void MainWindow::outputFolderDirectoryLoaded(const QString &path)
3067 {
3068         if(m_outputFolderViewCentering)
3069         {
3070                 CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
3071         }
3072 }
3073
3074 /*
3075  * File system model inserted new items
3076  */
3077 void MainWindow::outputFolderRowsInserted(const QModelIndex &parent, int start, int end)
3078 {
3079         if(m_outputFolderViewCentering)
3080         {
3081                 CENTER_CURRENT_OUTPUT_FOLDER_DELAYED;
3082         }
3083 }
3084
3085 /*
3086  * Directory view item was expanded by user
3087  */
3088 void MainWindow::outputFolderItemExpanded(const QModelIndex &item)
3089 {
3090         //We need to stop centering as soon as the user has expanded an item manually!
3091         m_outputFolderViewCentering = false;
3092 }
3093
3094 /*
3095  * View event for output folder control occurred
3096  */
3097 void MainWindow::outputFolderViewEventOccurred(QWidget *sender, QEvent *event)
3098 {
3099         switch(event->type())
3100         {
3101         case QEvent::Enter:
3102         case QEvent::Leave:
3103         case QEvent::KeyPress:
3104         case QEvent::KeyRelease:
3105         case QEvent::FocusIn:
3106         case QEvent::FocusOut:
3107         case QEvent::TouchEnd:
3108                 outputFolderViewClicked(ui->outputFolderView->currentIndex());
3109                 break;
3110         }
3111 }
3112
3113 /*
3114  * Mouse event for output folder control occurred
3115  */
3116 void MainWindow::outputFolderMouseEventOccurred(QWidget *sender, QEvent *event)
3117 {
3118         QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
3119         QPoint pos = (mouseEvent) ? mouseEvent->pos() : QPoint();
3120
3121         if(sender == ui->outputFolderLabel)
3122         {
3123                 switch(event->type())
3124                 {
3125                 case QEvent::MouseButtonPress:
3126                         if(mouseEvent && (mouseEvent->button() == Qt::LeftButton))
3127                         {
3128                                 QString path = ui->outputFolderLabel->text();
3129                                 if(!path.endsWith(QDir::separator())) path.append(QDir::separator());
3130                                 lamexp_exec_shell(this, path, true);
3131                         }
3132                         break;
3133                 case QEvent::Enter:
3134                         ui->outputFolderLabel->setForegroundRole(QPalette::Link);
3135                         break;
3136                 case QEvent::Leave:
3137                         ui->outputFolderLabel->setForegroundRole(QPalette::WindowText);
3138                         break;
3139                 }
3140         }
3141
3142         if((sender == ui->outputFoldersFovoritesLabel) || (sender == ui->outputFoldersEditorLabel) || (sender == ui->outputFoldersGoUpLabel))
3143         {
3144                 const type_info &styleType = typeid(*qApp->style());
3145                 if((typeid(QPlastiqueStyle) == styleType) || (typeid(QWindowsStyle) == styleType))
3146                 {
3147                         switch(event->type())
3148                         {
3149                         case QEvent::Enter:
3150                                 dynamic_cast<QLabel*>(sender)->setFrameShadow(ui->outputFolderView->isEnabled() ? QFrame::Raised : QFrame::Plain);
3151                                 break;
3152                         case QEvent::MouseButtonPress:
3153                                 dynamic_cast<QLabel*>(sender)->setFrameShadow(ui->outputFolderView->isEnabled() ? QFrame::Sunken : QFrame::Plain);
3154                                 break;
3155                         case QEvent::MouseButtonRelease:
3156                                 dynamic_cast<QLabel*>(sender)->setFrameShadow(ui->outputFolderView->isEnabled() ? QFrame::Raised : QFrame::Plain);
3157                                 break;
3158                         case QEvent::Leave:
3159                                 dynamic_cast<QLabel*>(sender)->setFrameShadow(ui->outputFolderView->isEnabled() ? QFrame::Plain : QFrame::Plain);
3160                                 break;
3161                         }
3162                 }
3163                 else
3164                 {
3165                         dynamic_cast<QLabel*>(sender)->setFrameShadow(QFrame::Plain);
3166                 }
3167
3168                 if((event->type() == QEvent::MouseButtonRelease) && ui->outputFolderView->isEnabled() && (mouseEvent))
3169                 {
3170                         if(pos.x() <= sender->width() && pos.y() <= sender->height() && pos.x() >= 0 && pos.y() >= 0 && mouseEvent->button() != Qt::MidButton)
3171                         {
3172                                 if(sender == ui->outputFoldersFovoritesLabel)
3173                                 {
3174                                         m_outputFolderFavoritesMenu->popup(sender->mapToGlobal(pos));
3175                                 }
3176                                 else if(sender == ui->outputFoldersEditorLabel)
3177                                 {
3178                                         ui->outputFolderView->setEnabled(false);
3179                                         ui->outputFolderLabel->setVisible(false);
3180                                         ui->outputFolderEdit->setVisible(true);
3181                                         ui->outputFolderEdit->setText(ui->outputFolderLabel->text());
3182                                         ui->outputFolderEdit->selectAll();
3183                                         ui->outputFolderEdit->setFocus();
3184                                 }
3185                                 else if(sender == ui->outputFoldersGoUpLabel)
3186                                 {
3187                                         QTimer::singleShot(0, this, SLOT(goUpFolderContextActionTriggered()));
3188                                 }
3189                                 else
3190                                 {
3191                                         THROW("Oups, this is not supposed to happen!");
3192                                 }
3193                         }
3194                 }
3195         }
3196 }
3197
3198 // =========================================================
3199 // Metadata tab slots
3200 // =========================================================
3201
3202 /*
3203  * Edit meta button clicked
3204  */
3205 void MainWindow::editMetaButtonClicked(void)
3206 {
3207         ABORT_IF_BUSY;
3208
3209         const QModelIndex index = ui->metaDataView->currentIndex();
3210
3211         if(index.isValid())
3212         {
3213                 m_metaInfoModel->editItem(index, this);
3214         
3215                 if(index.row() == 4)
3216                 {
3217                         m_settings->metaInfoPosition(m_metaData->position());
3218                 }
3219         }
3220 }
3221
3222 /*
3223  * Reset meta button clicked
3224  */
3225 void MainWindow::clearMetaButtonClicked(void)
3226 {
3227         ABORT_IF_BUSY;
3228         m_metaInfoModel->clearData();
3229 }
3230
3231 /*
3232  * Meta tags enabled changed
3233  */
3234 void MainWindow::metaTagsEnabledChanged(void)
3235 {
3236         m_settings->writeMetaTags(ui->writeMetaDataCheckBox->isChecked());
3237 }
3238
3239 /*
3240  * Playlist enabled changed
3241  */
3242 void MainWindow::playlistEnabledChanged(void)
3243 {
3244         m_settings->createPlaylist(ui->generatePlaylistCheckBox->isChecked());
3245 }
3246
3247 // =========================================================
3248 // Compression tab slots
3249 // =========================================================
3250
3251 /*
3252  * Update encoder
3253  */
3254 void MainWindow::updateEncoder(int id)
3255 {
3256         /*qWarning("\nupdateEncoder(%d)", id);*/
3257
3258         m_settings->compressionEncoder(id);
3259         const AbstractEncoderInfo *info = EncoderRegistry::getEncoderInfo(id);
3260
3261         //Update UI controls
3262         ui->radioButtonModeQuality       ->setEnabled(info->isModeSupported(SettingsModel::VBRMode));
3263         ui->radioButtonModeAverageBitrate->setEnabled(info->isModeSupported(SettingsModel::ABRMode));
3264         ui->radioButtonConstBitrate      ->setEnabled(info->isModeSupported(SettingsModel::CBRMode));
3265         
3266         //Initialize checkbox state
3267         if(ui->radioButtonModeQuality->isEnabled())             ui->radioButtonModeQuality->setChecked(true);
3268         else if(ui->radioButtonModeAverageBitrate->isEnabled()) ui->radioButtonModeAverageBitrate->setChecked(true);
3269         else if(ui->radioButtonConstBitrate->isEnabled())       ui->radioButtonConstBitrate->setChecked(true);
3270         else THROW("It appears that the encoder does not support *any* RC mode!");
3271
3272         //Apply current RC mode
3273         const int currentRCMode = EncoderRegistry::loadEncoderMode(m_settings, id);
3274         switch(currentRCMode)
3275         {
3276                 case SettingsModel::VBRMode: if(ui->radioButtonModeQuality->isEnabled())        ui->radioButtonModeQuality->setChecked(true);        break;
3277                 case SettingsModel::ABRMode: if(ui->radioButtonModeAverageBitrate->isEnabled()) ui->radioButtonModeAverageBitrate->setChecked(true); break;
3278                 case SettingsModel::CBRMode: if(ui->radioButtonConstBitrate->isEnabled())       ui->radioButtonConstBitrate->setChecked(true);       break;
3279                 default: THROW("updateEncoder(): Unknown rc-mode encountered!");
3280         }
3281
3282         //Display encoder description
3283         if(const char* description = info->description())
3284         {
3285                 ui->labelEncoderInfo->setVisible(true);
3286                 ui->labelEncoderInfo->setText(tr("Current Encoder: %1").arg(QString::fromUtf8(description)));
3287         }
3288         else
3289         {
3290                 ui->labelEncoderInfo->setVisible(false);
3291         }
3292
3293         //Update RC mode!
3294         updateRCMode(m_modeButtonGroup->checkedId());
3295 }
3296
3297 /*
3298  * Update rate-control mode
3299  */
3300 void MainWindow::updateRCMode(int id)
3301 {
3302         /*qWarning("updateRCMode(%d)", id);*/
3303
3304         //Store new RC mode
3305         const int currentEncoder = m_encoderButtonGroup->checkedId();
3306         EncoderRegistry::saveEncoderMode(m_settings, currentEncoder, id);
3307
3308         //Fetch encoder info
3309         const AbstractEncoderInfo *info = EncoderRegistry::getEncoderInfo(currentEncoder);
3310         const int valueCount = info->valueCount(id);
3311
3312         //Sanity check
3313         if(!info->isModeSupported(id))
3314         {
3315                 qWarning("Attempting to use an unsupported RC mode (%d) with current encoder (%d)!", id, currentEncoder);
3316                 ui->labelBitrate->setText("(ERROR)");
3317                 return;
3318         }
3319
3320         //Update slider min/max values
3321         if(valueCount > 0)
3322         {
3323                 WITH_BLOCKED_SIGNALS(ui->sliderBitrate, setEnabled, true);
3324                 WITH_BLOCKED_SIGNALS(ui->sliderBitrate, setMinimum, 0);
3325                 WITH_BLOCKED_SIGNALS(ui->sliderBitrate, setMaximum, valueCount-1);
3326         }
3327         else
3328         {
3329                 WITH_BLOCKED_SIGNALS(ui->sliderBitrate, setEnabled, false);
3330                 WITH_BLOCKED_SIGNALS(ui->sliderBitrate, setMinimum, 0);
3331                 WITH_BLOCKED_SIGNALS(ui->sliderBitrate, setMaximum, 2);
3332         }
3333
3334         //Now update bitrate/quality value!
3335         if(valueCount > 0)
3336         {
3337                 const int currentValue = EncoderRegistry::loadEncoderValue(m_settings, currentEncoder, id);
3338                 ui->sliderBitrate->setValue(qBound(0, currentValue, valueCount-1));
3339                 updateBitrate(qBound(0, currentValue, valueCount-1));
3340         }
3341         else
3342         {
3343                 ui->sliderBitrate->setValue(1);
3344                 updateBitrate(0);
3345         }
3346 }
3347
3348 /*
3349  * Update bitrate
3350  */
3351 void MainWindow::updateBitrate(int value)
3352 {
3353         /*qWarning("updateBitrate(%d)", value);*/
3354
3355         //Load current encoder and RC mode
3356         const int currentEncoder = m_encoderButtonGroup->checkedId();
3357         const int currentRCMode = m_modeButtonGroup->checkedId();
3358
3359         //Fetch encoder info
3360         const AbstractEncoderInfo *info = EncoderRegistry::getEncoderInfo(currentEncoder);
3361         const int valueCount = info->valueCount(currentRCMode);
3362
3363         //Sanity check
3364         if(!info->isModeSupported(currentRCMode))
3365         {
3366                 qWarning("Attempting to use an unsupported RC mode (%d) with current encoder (%d)!", currentRCMode, currentEncoder);
3367                 ui->labelBitrate->setText("(ERROR)");
3368                 return;
3369         }
3370
3371         //Store new bitrate value
3372         if(valueCount > 0)
3373         {
3374                 EncoderRegistry::saveEncoderValue(m_settings, currentEncoder, currentRCMode, qBound(0, value, valueCount-1));
3375         }
3376
3377         //Update bitrate value
3378         const int displayValue = (valueCount > 0) ? info->valueAt(currentRCMode, qBound(0, value, valueCount-1)) : INT_MAX;
3379         switch(info->valueType(currentRCMode))
3380         {
3381         case AbstractEncoderInfo::TYPE_BITRATE:
3382                 ui->labelBitrate->setText(QString("%1 kbps").arg(QString::number(displayValue)));
3383                 break;
3384         case AbstractEncoderInfo::TYPE_APPROX_BITRATE:
3385                 ui->labelBitrate->setText(QString("&asymp; %1 kbps").arg(QString::number(displayValue)));
3386                 break;
3387         case AbstractEncoderInfo::TYPE_QUALITY_LEVEL_INT:
3388                 ui->labelBitrate->setText(tr("Quality Level %1").arg(QString::number(displayValue)));
3389                 break;
3390         case AbstractEncoderInfo::TYPE_QUALITY_LEVEL_FLT:
3391                 ui->labelBitrate->setText(tr("Quality Level %1").arg(QString().sprintf("%.2f", double(displayValue)/100.0)));
3392                 break;
3393         case AbstractEncoderInfo::TYPE_COMPRESSION_LEVEL:
3394                 ui->labelBitrate->setText(tr("Compression %1").arg(QString::number(displayValue)));
3395                 break;
3396         case AbstractEncoderInfo::TYPE_UNCOMPRESSED:
3397                 ui->labelBitrate->setText(tr("Uncompressed"));
3398                 break;
3399         default:
3400                 THROW("Unknown display value type encountered!");
3401                 break;
3402         }
3403 }
3404
3405 /*
3406  * Event for compression tab occurred
3407  */
3408 void MainWindow::compressionTabEventOccurred(QWidget *sender, QEvent *event)
3409 {
3410         static const QUrl helpUrl("http://lamexp.sourceforge.net/doc/FAQ.html#054010d9");
3411         
3412         if((sender == ui->labelCompressionHelp) && (event->type() == QEvent::MouseButtonPress))
3413         {
3414                 QDesktopServices::openUrl(helpUrl);
3415         }
3416         else if((sender == ui->labelResetEncoders) && (event->type() == QEvent::MouseButtonPress))
3417         {
3418                 if(m_settings->soundsEnabled())
3419                 {
3420                         lamexp_play_sound(IDR_WAVE_BLAST, true);
3421                 }
3422
3423                 EncoderRegistry::resetAllEncoders(m_settings);
3424                 m_settings->compressionEncoder(SettingsModel::MP3Encoder);
3425                 ui->radioButtonEncoderMP3->setChecked(true);
3426                 QTimer::singleShot(0, this, SLOT(updateEncoder()));
3427         }
3428 }
3429
3430 // =========================================================
3431 // Advanced option slots
3432 // =========================================================
3433
3434 /*
3435  * Lame algorithm quality changed
3436  */
3437 void MainWindow::updateLameAlgoQuality(int value)
3438 {
3439         QString text;
3440
3441         switch(value)
3442         {
3443         case 3:
3444                 text = tr("Best Quality (Slow)");
3445                 break;
3446         case 2:
3447                 text = tr("High Quality (Recommended)");
3448                 break;
3449         case 1:
3450                 text = tr("Acceptable Quality (Fast)");
3451                 break;
3452         case 0:
3453                 text = tr("Poor Quality (Very Fast)");
3454                 break;
3455         }
3456
3457         if(!text.isEmpty())
3458         {
3459                 m_settings->lameAlgoQuality(value);
3460                 ui->labelLameAlgoQuality->setText(text);
3461         }
3462
3463         bool warning = (value == 0), notice = (value == 3);
3464         ui->labelLameAlgoQualityWarning->setVisible(warning);
3465         ui->labelLameAlgoQualityWarningIcon->setVisible(warning);
3466         ui->labelLameAlgoQualityNotice->setVisible(notice);
3467         ui->labelLameAlgoQualityNoticeIcon->setVisible(notice);
3468         ui->labelLameAlgoQualitySpacer->setVisible(warning || notice);
3469 }
3470
3471 /*
3472  * Bitrate management endabled/disabled
3473  */
3474 void MainWindow::bitrateManagementEnabledChanged(bool checked)
3475 {
3476         m_settings->bitrateManagementEnabled(checked);
3477 }
3478
3479 /*
3480  * Minimum bitrate has changed
3481  */
3482 void MainWindow::bitrateManagementMinChanged(int value)
3483 {
3484         if(value > ui->spinBoxBitrateManagementMax->value())
3485         {
3486                 ui->spinBoxBitrateManagementMin->setValue(ui->spinBoxBitrateManagementMax->value());
3487                 m_settings->bitrateManagementMinRate(ui->spinBoxBitrateManagementMax->value());
3488         }
3489         else
3490         {
3491                 m_settings->bitrateManagementMinRate(value);
3492         }
3493 }
3494
3495 /*
3496  * Maximum bitrate has changed
3497  */
3498 void MainWindow::bitrateManagementMaxChanged(int value)
3499 {
3500         if(value < ui->spinBoxBitrateManagementMin->value())
3501         {
3502                 ui->spinBoxBitrateManagementMax->setValue(ui->spinBoxBitrateManagementMin->value());
3503                 m_settings->bitrateManagementMaxRate(ui->spinBoxBitrateManagementMin->value());
3504         }
3505         else
3506         {
3507                 m_settings->bitrateManagementMaxRate(value);
3508         }
3509 }
3510
3511 /*
3512  * Channel mode has changed
3513  */
3514 void MainWindow::channelModeChanged(int value)
3515 {
3516         if(value >= 0) m_settings->lameChannelMode(value);
3517 }
3518
3519 /*
3520  * Sampling rate has changed
3521  */
3522 void MainWindow::samplingRateChanged(int value)
3523 {
3524         if(value >= 0) m_settings->samplingRate(value);
3525 }
3526
3527 /*
3528  * Nero AAC 2-Pass mode changed
3529  */
3530 void MainWindow::neroAAC2PassChanged(bool checked)
3531 {
3532         m_settings->neroAACEnable2Pass(checked);
3533 }
3534
3535 /*
3536  * Nero AAC profile mode changed
3537  */
3538 void MainWindow::neroAACProfileChanged(int value)
3539 {
3540         if(value >= 0) m_settings->aacEncProfile(value);
3541 }
3542
3543 /*
3544  * Aften audio coding mode changed
3545  */
3546 void MainWindow::aftenCodingModeChanged(int value)
3547 {
3548         if(value >= 0) m_settings->aftenAudioCodingMode(value);
3549 }
3550
3551 /*
3552  * Aften DRC mode changed
3553  */
3554 void MainWindow::aftenDRCModeChanged(int value)
3555 {
3556         if(value >= 0) m_settings->aftenDynamicRangeCompression(value);
3557 }
3558
3559 /*
3560  * Aften exponent search size changed
3561  */
3562 void MainWindow::aftenSearchSizeChanged(int value)
3563 {
3564         if(value >= 0) m_settings->aftenExponentSearchSize(value);
3565 }
3566
3567 /*
3568  * Aften fast bit allocation changed
3569  */
3570 void MainWindow::aftenFastAllocationChanged(bool checked)
3571 {
3572         m_settings->aftenFastBitAllocation(checked);
3573 }
3574
3575
3576 /*
3577  * Opus encoder settings changed
3578  */
3579 void MainWindow::opusSettingsChanged(void)
3580 {
3581         m_settings->opusFramesize(ui->comboBoxOpusFramesize->currentIndex());
3582         m_settings->opusComplexity(ui->spinBoxOpusComplexity->value());
3583         m_settings->opusDisableResample(ui->checkBoxOpusDisableResample->isChecked());
3584 }
3585
3586 /*
3587  * Normalization filter enabled changed
3588  */
3589 void MainWindow::normalizationEnabledChanged(bool checked)
3590 {
3591         m_settings->normalizationFilterEnabled(checked);
3592 }
3593
3594 /*
3595  * Normalization max. volume changed
3596  */
3597 void MainWindow::normalizationMaxVolumeChanged(double value)
3598 {
3599         m_settings->normalizationFilterMaxVolume(static_cast<int>(value * 100.0));
3600 }
3601
3602 /*
3603  * Normalization equalization mode changed
3604  */
3605 void MainWindow::normalizationModeChanged(int mode)
3606 {
3607         m_settings->normalizationFilterEQMode(mode);
3608 }
3609
3610 /*
3611  * Tone adjustment has changed (Bass)
3612  */
3613 void MainWindow::toneAdjustBassChanged(double value)
3614 {
3615         m_settings->toneAdjustBass(static_cast<int>(value * 100.0));
3616         ui->spinBoxToneAdjustBass->setPrefix((value > 0) ? "+" : QString());
3617 }
3618
3619 /*
3620  * Tone adjustment has changed (Treble)
3621  */
3622 void MainWindow::toneAdjustTrebleChanged(double value)
3623 {
3624         m_settings->toneAdjustTreble(static_cast<int>(value * 100.0));
3625         ui->spinBoxToneAdjustTreble->setPrefix((value > 0) ? "+" : QString());
3626 }
3627
3628 /*
3629  * Tone adjustment has been reset
3630  */
3631 void MainWindow::toneAdjustTrebleReset(void)
3632 {
3633         ui->spinBoxToneAdjustBass->setValue(m_settings->toneAdjustBassDefault());
3634         ui->spinBoxToneAdjustTreble->setValue(m_settings->toneAdjustTrebleDefault());
3635         toneAdjustBassChanged(ui->spinBoxToneAdjustBass->value());
3636         toneAdjustTrebleChanged(ui->spinBoxToneAdjustTreble->value());
3637 }
3638
3639 /*
3640  * Custom encoder parameters changed
3641  */
3642 void MainWindow::customParamsChanged(void)
3643 {
3644         ui->lineEditCustomParamLAME->setText(ui->lineEditCustomParamLAME->text().simplified());
3645         ui->lineEditCustomParamOggEnc->setText(ui->lineEditCustomParamOggEnc->text().simplified());
3646         ui->lineEditCustomParamNeroAAC->setText(ui->lineEditCustomParamNeroAAC->text().simplified());
3647         ui->lineEditCustomParamFLAC->setText(ui->lineEditCustomParamFLAC->text().simplified());
3648         ui->lineEditCustomParamAften->setText(ui->lineEditCustomParamAften->text().simplified());
3649         ui->lineEditCustomParamOpus->setText(ui->lineEditCustomParamOpus->text().simplified());
3650
3651         bool customParamsUsed = false;
3652         if(!ui->lineEditCustomParamLAME->text().isEmpty()) customParamsUsed = true;
3653         if(!ui->lineEditCustomParamOggEnc->text().isEmpty()) customParamsUsed = true;
3654         if(!ui->lineEditCustomParamNeroAAC->text().isEmpty()) customParamsUsed = true;
3655         if(!ui->lineEditCustomParamFLAC->text().isEmpty()) customParamsUsed = true;
3656         if(!ui->lineEditCustomParamAften->text().isEmpty()) customParamsUsed = true;
3657         if(!ui->lineEditCustomParamOpus->text().isEmpty()) customParamsUsed = true;
3658
3659         ui->labelCustomParamsIcon->setVisible(customParamsUsed);
3660         ui->labelCustomParamsText->setVisible(customParamsUsed);
3661         ui->labelCustomParamsSpacer->setVisible(customParamsUsed);
3662
3663         EncoderRegistry::saveEncoderCustomParams(m_settings, SettingsModel::MP3Encoder,    ui->lineEditCustomParamLAME->text());
3664         EncoderRegistry::saveEncoderCustomParams(m_settings, SettingsModel::VorbisEncoder, ui->lineEditCustomParamOggEnc->text());
3665         EncoderRegistry::saveEncoderCustomParams(m_settings, SettingsModel::AACEncoder,    ui->lineEditCustomParamNeroAAC->text());
3666         EncoderRegistry::saveEncoderCustomParams(m_settings, SettingsModel::FLACEncoder,   ui->lineEditCustomParamFLAC->text());
3667         EncoderRegistry::saveEncoderCustomParams(m_settings, SettingsModel::AC3Encoder,    ui->lineEditCustomParamAften->text());
3668         EncoderRegistry::saveEncoderCustomParams(m_settings, SettingsModel::OpusEncoder,   ui->lineEditCustomParamOpus->text());
3669 }
3670
3671 /*
3672  * Rename output files enabled changed
3673  */
3674 void MainWindow::renameOutputEnabledChanged(bool checked)
3675 {
3676         m_settings->renameOutputFilesEnabled(checked);
3677 }
3678
3679 /*
3680  * Rename output files patterm changed
3681  */
3682 void MainWindow::renameOutputPatternChanged(void)
3683 {
3684         QString temp = ui->lineEditRenamePattern->text().simplified();
3685         ui->lineEditRenamePattern->setText(temp.isEmpty() ? m_settings->renameOutputFilesPatternDefault() : temp);
3686         m_settings->renameOutputFilesPattern(ui->lineEditRenamePattern->text());
3687 }
3688
3689 /*
3690  * Rename output files patterm changed
3691  */
3692 void MainWindow::renameOutputPatternChanged(const QString &text, bool silent)
3693 {
3694         QString pattern(text.simplified());
3695         
3696         pattern.replace("<BaseName>", "The_White_Stripes_-_Fell_In_Love_With_A_Girl", Qt::CaseInsensitive);
3697         pattern.replace("<TrackNo>", "04", Qt::CaseInsensitive);
3698         pattern.replace("<Title>", "Fell In Love With A Girl", Qt::CaseInsensitive);
3699         pattern.replace("<Artist>", "The White Stripes", Qt::CaseInsensitive);
3700         pattern.replace("<Album>", "White Blood Cells", Qt::CaseInsensitive);
3701         pattern.replace("<Year>", "2001", Qt::CaseInsensitive);
3702         pattern.replace("<Comment>", "Encoded by LameXP", Qt::CaseInsensitive);
3703
3704         const QString patternClean = lamexp_clean_filename(pattern);
3705
3706         if(pattern.compare(patternClean))
3707         {
3708                 if(ui->lineEditRenamePattern->palette().color(QPalette::Text) != Qt::red)
3709                 {
3710                         if(!silent) lamexp_beep(lamexp_beep_error);
3711                         SET_TEXT_COLOR(ui->lineEditRenamePattern, Qt::red);
3712                 }
3713         }
3714         else
3715         {
3716                 if(ui->lineEditRenamePattern->palette() != QPalette())
3717                 {
3718                         if(!silent) lamexp_beep(lamexp_beep_info);
3719                         ui->lineEditRenamePattern->setPalette(QPalette());
3720                 }
3721         }
3722
3723         ui->labelRanameExample->setText(patternClean);
3724 }
3725
3726 /*
3727  * Show list of rename macros
3728  */
3729 void MainWindow::showRenameMacros(const QString &text)
3730 {
3731         if(text.compare("reset", Qt::CaseInsensitive) == 0)
3732         {
3733                 ui->lineEditRenamePattern->setText(m_settings->renameOutputFilesPatternDefault());
3734                 return;
3735         }
3736
3737         const QString format = QString("<tr><td><tt>&lt;%1&gt;</tt></td><td>&nbsp;&nbsp;</td><td>%2</td></tr>");
3738
3739         QString message = QString("<table>");
3740         message += QString(format).arg("BaseName", tr("File name without extension"));
3741         message += QString(format).arg("TrackNo", tr("Track number with leading zero"));
3742         message += QString(format).arg("Title", tr("Track title"));
3743         message += QString(format).arg("Artist", tr("Artist name"));
3744         message += QString(format).arg("Album", tr("Album name"));
3745         message += QString(format).arg("Year", tr("Year with (at least) four digits"));
3746         message += QString(format).arg("Comment", tr("Comment"));
3747         message += "</table><br><br>";
3748         message += QString("%1<br>").arg(tr("Characters forbidden in file names:"));
3749         message += "<b><tt>\\ / : * ? &lt; &gt; |<br>";
3750         
3751         QMessageBox::information(this, tr("Rename Macros"), message, tr("Discard"));
3752 }
3753
3754 void MainWindow::forceStereoDownmixEnabledChanged(bool checked)
3755 {
3756         m_settings->forceStereoDownmix(checked);
3757 }
3758
3759 /*
3760  * Maximum number of instances changed
3761  */
3762 void MainWindow::updateMaximumInstances(int value)
3763 {
3764         ui->labelMaxInstances->setText(tr("%n Instance(s)", "", value));
3765         m_settings->maximumInstances(ui->checkBoxAutoDetectInstances->isChecked() ? NULL : value);
3766 }
3767
3768 /*
3769  * Auto-detect number of instances
3770  */
3771 void MainWindow::autoDetectInstancesChanged(bool checked)
3772 {
3773         m_settings->maximumInstances(checked ? NULL : ui->sliderMaxInstances->value());
3774 }
3775
3776 /*
3777  * Browse for custom TEMP folder button clicked
3778  */
3779 void MainWindow::browseCustomTempFolderButtonClicked(void)
3780 {
3781         QString newTempFolder;
3782
3783         if(lamexp_themes_enabled())
3784         {
3785                 newTempFolder = QFileDialog::getExistingDirectory(this, QString(), m_settings->customTempPath());
3786         }
3787         else
3788         {
3789                 QFileDialog dialog(this);
3790                 dialog.setFileMode(QFileDialog::DirectoryOnly);
3791                 dialog.setDirectory(m_settings->customTempPath());
3792                 if(dialog.exec())
3793                 {
3794                         newTempFolder = dialog.selectedFiles().first();
3795                 }
3796         }
3797
3798         if(!newTempFolder.isEmpty())
3799         {
3800                 QFile writeTest(QString("%1/~%2.tmp").arg(newTempFolder, lamexp_rand_str()));
3801                 if(writeTest.open(QIODevice::ReadWrite))
3802                 {
3803                         writeTest.remove();
3804                         ui->lineEditCustomTempFolder->setText(QDir::toNativeSeparators(newTempFolder));
3805                 }
3806                 else
3807                 {
3808                         QMessageBox::warning(this, tr("Access Denied"), tr("Cannot write to the selected directory. Please choose another directory!"));
3809                 }
3810         }
3811 }
3812
3813 /*
3814  * Custom TEMP folder changed
3815  */
3816 void MainWindow::customTempFolderChanged(const QString &text)
3817 {
3818         m_settings->customTempPath(QDir::fromNativeSeparators(text));
3819 }
3820
3821 /*
3822  * Use custom TEMP folder option changed
3823  */
3824 void MainWindow::useCustomTempFolderChanged(bool checked)
3825 {
3826         m_settings->customTempPathEnabled(!checked);
3827 }
3828
3829 /*
3830  * Help for custom parameters was requested
3831  */
3832 void MainWindow::customParamsHelpRequested(QWidget *obj, QEvent *event)
3833 {
3834         if(event->type() != QEvent::MouseButtonRelease)
3835         {
3836                 return;
3837         }
3838
3839         if(QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event))
3840         {
3841                 QPoint pos = mouseEvent->pos();
3842                 if(!(pos.x() <= obj->width() && pos.y() <= obj->height() && pos.x() >= 0 && pos.y() >= 0 && mouseEvent->button() != Qt::MidButton))
3843                 {
3844                         return;
3845                 }
3846         }
3847
3848         if(obj == ui->helpCustomParamLAME)         showCustomParamsHelpScreen("lame.exe", "--longhelp");
3849         else if(obj == ui->helpCustomParamOggEnc)  showCustomParamsHelpScreen("oggenc2.exe", "--help");
3850         else if(obj == ui->helpCustomParamNeroAAC)
3851         {
3852                 switch(EncoderRegistry::getAacEncoder())
3853                 {
3854                         case SettingsModel::AAC_ENCODER_QAAC: showCustomParamsHelpScreen("qaac.exe", "--help"); break;
3855                         case SettingsModel::AAC_ENCODER_FHG : showCustomParamsHelpScreen("fhgaacenc.exe", ""); break;
3856                         case SettingsModel::AAC_ENCODER_NERO: showCustomParamsHelpScreen("neroAacEnc.exe", "-help"); break;
3857                         default: lamexp_beep(lamexp_beep_error); break;
3858                 }
3859         }
3860         else if(obj == ui->helpCustomParamFLAC)    showCustomParamsHelpScreen("flac.exe", "--help");
3861         else if(obj == ui->helpCustomParamAften)   showCustomParamsHelpScreen("aften.exe", "-h");
3862         else if(obj == ui->helpCustomParamOpus)    showCustomParamsHelpScreen("opusenc.exe", "--help");
3863         else lamexp_beep(lamexp_beep_error);
3864 }
3865
3866 /*
3867  * Show help for custom parameters
3868  */
3869 void MainWindow::showCustomParamsHelpScreen(const QString &toolName, const QString &command)
3870 {
3871         const QString binary = lamexp_lookup_tool(toolName);
3872         if(binary.isEmpty())
3873         {
3874                 lamexp_beep(lamexp_beep_error);
3875                 qWarning("customParamsHelpRequested: Binary could not be found!");
3876                 return;
3877         }
3878
3879         QProcess process;
3880         lamexp_init_process(process, QFileInfo(binary).absolutePath());
3881
3882         process.start(binary, command.isEmpty() ? QStringList() : QStringList() << command);
3883
3884         qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
3885
3886         if(process.waitForStarted(15000))
3887         {
3888                 qApp->processEvents();
3889                 process.waitForFinished(15000);
3890         }
3891         
3892         if(process.state() != QProcess::NotRunning)
3893         {
3894                 process.kill();
3895                 process.waitForFinished(-1);
3896         }
3897
3898         qApp->restoreOverrideCursor();
3899         QStringList output; bool spaceFlag = true;
3900
3901         while(process.canReadLine())
3902         {
3903                 QString temp = QString::fromUtf8(process.readLine());
3904                 TRIM_STRING_RIGHT(temp);
3905                 if(temp.isEmpty())
3906                 {
3907                         if(!spaceFlag) { output << temp; spaceFlag = true; }
3908                 }
3909                 else
3910                 {
3911                         output << temp; spaceFlag = false;
3912                 }
3913         }
3914
3915         if(output.count() < 1)
3916         {
3917                 qWarning("Empty output, cannot show help screen!");
3918                 lamexp_beep(lamexp_beep_error);
3919         }
3920
3921         LogViewDialog *dialog = new LogViewDialog(this);
3922         TEMP_HIDE_DROPBOX( dialog->exec(output); );
3923         LAMEXP_DELETE(dialog);
3924 }
3925
3926 void MainWindow::overwriteModeChanged(int id)
3927 {
3928         if((id == SettingsModel::Overwrite_Replaces) && (m_settings->overwriteMode() != SettingsModel::Overwrite_Replaces))
3929         {
3930                 if(QMessageBox::warning(this, tr("Overwrite Mode"), tr("Warning: This mode may overwrite existing files with no way to revert!"), tr("Continue"), tr("Revert"), QString(), 1) != 0)
3931                 {
3932                         ui->radioButtonOverwriteModeKeepBoth->setChecked(m_settings->overwriteMode() == SettingsModel::Overwrite_KeepBoth);
3933                         ui->radioButtonOverwriteModeSkipFile->setChecked(m_settings->overwriteMode() == SettingsModel::Overwrite_SkipFile);
3934                         return;
3935                 }
3936         }
3937
3938         m_settings->overwriteMode(id);
3939 }
3940
3941 /*
3942  * Reset all advanced options to their defaults
3943  */
3944 void MainWindow::resetAdvancedOptionsButtonClicked(void)
3945 {
3946         if(m_settings->soundsEnabled())
3947         {
3948                 lamexp_play_sound(IDR_WAVE_BLAST, true);
3949         }
3950
3951         ui->sliderLameAlgoQuality->setValue(m_settings->lameAlgoQualityDefault());
3952         ui->spinBoxBitrateManagementMin->setValue(m_settings->bitrateManagementMinRateDefault());
3953         ui->spinBoxBitrateManagementMax->setValue(m_settings->bitrateManagementMaxRateDefault());
3954         ui->spinBoxNormalizationFilter->setValue(static_cast<double>(m_settings->normalizationFilterMaxVolumeDefault()) / 100.0);
3955         ui->spinBoxToneAdjustBass->setValue(static_cast<double>(m_settings->toneAdjustBassDefault()) / 100.0);
3956         ui->spinBoxToneAdjustTreble->setValue(static_cast<double>(m_settings->toneAdjustTrebleDefault()) / 100.0);
3957         ui->spinBoxAftenSearchSize->setValue(m_settings->aftenExponentSearchSizeDefault());
3958         ui->spinBoxOpusComplexity->setValue(m_settings->opusComplexityDefault());
3959         ui->comboBoxMP3ChannelMode->setCurrentIndex(m_settings->lameChannelModeDefault());
3960         ui->comboBoxSamplingRate->setCurrentIndex(m_settings->samplingRateDefault());
3961         ui->comboBoxAACProfile->setCurrentIndex(m_settings->aacEncProfileDefault());
3962         ui->comboBoxAftenCodingMode->setCurrentIndex(m_settings->aftenAudioCodingModeDefault());
3963         ui->comboBoxAftenDRCMode->setCurrentIndex(m_settings->aftenDynamicRangeCompressionDefault());
3964         ui->comboBoxNormalizationMode->setCurrentIndex(m_settings->normalizationFilterEQModeDefault());
3965         ui->comboBoxOpusFramesize->setCurrentIndex(m_settings->opusFramesizeDefault());
3966
3967         SET_CHECKBOX_STATE(ui->checkBoxBitrateManagement, m_settings->bitrateManagementEnabledDefault());
3968         SET_CHECKBOX_STATE(ui->checkBoxNeroAAC2PassMode, m_settings->neroAACEnable2PassDefault());
3969         SET_CHECKBOX_STATE(ui->checkBoxNormalizationFilter, m_settings->normalizationFilterEnabledDefault());
3970         SET_CHECKBOX_STATE(ui->checkBoxAutoDetectInstances, (m_settings->maximumInstancesDefault() < 1));
3971         SET_CHECKBOX_STATE(ui->checkBoxUseSystemTempFolder, !m_settings->customTempPathEnabledDefault());
3972         SET_CHECKBOX_STATE(ui->checkBoxAftenFastAllocation, m_settings->aftenFastBitAllocationDefault());
3973         SET_CHECKBOX_STATE(ui->checkBoxRenameOutput, m_settings->renameOutputFilesEnabledDefault());
3974         SET_CHECKBOX_STATE(ui->checkBoxForceStereoDownmix, m_settings->forceStereoDownmixDefault());
3975         SET_CHECKBOX_STATE(ui->checkBoxOpusDisableResample, m_settings->opusDisableResampleDefault());
3976         
3977         ui->lineEditCustomParamLAME   ->setText(m_settings->customParametersLAMEDefault());
3978         ui->lineEditCustomParamOggEnc ->setText(m_settings->customParametersOggEncDefault());
3979         ui->lineEditCustomParamNeroAAC->setText(m_settings->customParametersAacEncDefault());
3980         ui->lineEditCustomParamFLAC   ->setText(m_settings->customParametersFLACDefault());
3981         ui->lineEditCustomParamOpus   ->setText(m_settings->customParametersOpusEncDefault());
3982         ui->lineEditCustomTempFolder  ->setText(QDir::toNativeSeparators(m_settings->customTempPathDefault()));
3983         ui->lineEditRenamePattern     ->setText(m_settings->renameOutputFilesPatternDefault());
3984
3985         if(m_settings->overwriteModeDefault() == SettingsModel::Overwrite_KeepBoth) ui->radioButtonOverwriteModeKeepBoth->click();
3986         if(m_settings->overwriteModeDefault() == SettingsModel::Overwrite_SkipFile) ui->radioButtonOverwriteModeSkipFile->click();
3987         if(m_settings->overwriteModeDefault() == SettingsModel::Overwrite_Replaces) ui->radioButtonOverwriteModeReplaces->click();
3988
3989         customParamsChanged();
3990         ui->scrollArea->verticalScrollBar()->setValue(0);
3991 }
3992
3993 // =========================================================
3994 // Multi-instance handling slots
3995 // =========================================================
3996
3997 /*
3998  * Other instance detected
3999  */
4000 void MainWindow::notifyOtherInstance(void)
4001 {
4002         if(!m_banner->isVisible())
4003         {
4004                 QMessageBox msgBox(QMessageBox::Warning, tr("Already Running"), tr("LameXP is already running, please use the running instance!"), QMessageBox::NoButton, this, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
4005                 msgBox.exec();
4006         }
4007 }
4008
4009 /*
4010  * Add file from another instance
4011  */
4012 void MainWindow::addFileDelayed(const QString &filePath, bool tryASAP)
4013 {
4014         if(tryASAP && !m_delayedFileTimer->isActive())
4015         {
4016                 qDebug("Received file: %s", QUTF8(filePath));
4017                 m_delayedFileList->append(filePath);
4018                 QTimer::singleShot(0, this, SLOT(handleDelayedFiles()));
4019         }
4020         
4021         m_delayedFileTimer->stop();
4022         qDebug("Received file: %s", QUTF8(filePath));
4023         m_delayedFileList->append(filePath);
4024         m_delayedFileTimer->start(5000);
4025 }
4026
4027 /*
4028  * Add files from another instance
4029  */
4030 void MainWindow::addFilesDelayed(const QStringList &filePaths, bool tryASAP)
4031 {
4032         if(tryASAP && !m_delayedFileTimer->isActive())
4033         {
4034                 qDebug("Received %d file(s).", filePaths.count());
4035                 m_delayedFileList->append(filePaths);
4036                 QTimer::singleShot(0, this, SLOT(handleDelayedFiles()));
4037         }
4038         else
4039         {
4040                 m_delayedFileTimer->stop();
4041                 qDebug("Received %d file(s).", filePaths.count());
4042                 m_delayedFileList->append(filePaths);
4043                 m_delayedFileTimer->start(5000);
4044         }
4045 }
4046
4047 /*
4048  * Add folder from another instance
4049  */
4050 void MainWindow::addFolderDelayed(const QString &folderPath, bool recursive)
4051 {
4052         if(!m_banner->isVisible())
4053         {
4054                 addFolder(folderPath, recursive, true);
4055         }
4056 }
4057
4058 // =========================================================
4059 // Misc slots
4060 // =========================================================
4061
4062 /*
4063  * Restore the override cursor
4064  */
4065 void MainWindow::restoreCursor(void)
4066 {
4067         QApplication::restoreOverrideCursor();
4068 }