OSDN Git Service

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