OSDN Git Service

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