OSDN Git Service

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