OSDN Git Service

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