OSDN Git Service

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