2011-01-27 22:10:51 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2017-03-12 12:12:49 +01:00
|
|
|
// Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
|
2011-01-27 22:10:51 +01:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
2013-10-23 20:56:57 +02:00
|
|
|
// (at your option) any later version, but always including the *additional*
|
|
|
|
// restrictions defined in the "License.txt" file.
|
2011-01-27 22:10:51 +01:00
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "ShellIntegration.h"
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//Internal
|
|
|
|
#include "Global.h"
|
|
|
|
#include "Registry_Decoder.h"
|
|
|
|
|
|
|
|
//MUtils
|
|
|
|
#include <MUtils/Global.h>
|
2014-11-30 21:32:23 +01:00
|
|
|
#include <MUtils/Exception.h>
|
2015-05-03 18:16:34 +02:00
|
|
|
#include <MUtils/OSSupport.h>
|
|
|
|
#include <MUtils/Registry.h>
|
2014-11-25 02:14:42 +01:00
|
|
|
|
|
|
|
//Qt
|
2011-01-27 22:10:51 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QRegExp>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
2011-01-29 00:40:29 +01:00
|
|
|
#include <QMutexLocker>
|
2011-07-15 13:44:32 +02:00
|
|
|
#include <QtConcurrentRun>
|
2011-01-27 22:10:51 +01:00
|
|
|
|
|
|
|
//Const
|
|
|
|
static const char *g_lamexpShellAction = "ConvertWithLameXP";
|
2015-05-03 18:16:34 +02:00
|
|
|
static const char *g_lamexpFileType = "LameXP.SupportedAudioFile";
|
2011-01-27 22:10:51 +01:00
|
|
|
|
2011-01-29 00:40:29 +01:00
|
|
|
//Mutex
|
|
|
|
QMutex ShellIntegration::m_mutex;
|
|
|
|
|
2015-05-03 21:35:22 +02:00
|
|
|
//State values
|
|
|
|
static const int STATE_ENABLED = 1;
|
|
|
|
static const int STATE_UNKNOWN = 0;
|
|
|
|
static const int STATE_DISABLD = -1;
|
|
|
|
|
|
|
|
//State
|
|
|
|
volatile int ShellIntegration::m_state = STATE_UNKNOWN;
|
|
|
|
|
2011-01-29 00:40:29 +01:00
|
|
|
//Macros
|
2011-01-29 21:57:53 +01:00
|
|
|
#define REG_WRITE_STRING(KEY, STR) RegSetValueEx(key, NULL, NULL, REG_SZ, reinterpret_cast<const BYTE*>(STR.utf16()), (STR.size() + 1) * sizeof(wchar_t))
|
2011-01-29 00:40:29 +01:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Constructor
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2011-07-15 13:44:32 +02:00
|
|
|
ShellIntegration::ShellIntegration(void)
|
2011-01-29 00:40:29 +01:00
|
|
|
{
|
2014-11-30 21:32:23 +01:00
|
|
|
MUTILS_THROW("Cannot create instance of this class, sorry!");
|
2011-01-29 00:40:29 +01:00
|
|
|
}
|
|
|
|
|
2011-01-27 22:10:51 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Public Functions
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2011-01-29 00:40:29 +01:00
|
|
|
void ShellIntegration::install(bool async)
|
2011-01-27 22:10:51 +01:00
|
|
|
{
|
2011-01-29 00:40:29 +01:00
|
|
|
//Install asynchronously
|
|
|
|
if(async)
|
|
|
|
{
|
2011-07-15 13:44:32 +02:00
|
|
|
QFuture<void>(QtConcurrent::run(install, false));
|
2011-01-29 00:40:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Serialize
|
|
|
|
QMutexLocker lock(&m_mutex);
|
|
|
|
|
2015-05-03 21:35:22 +02:00
|
|
|
//Checking
|
|
|
|
if(m_state == STATE_ENABLED)
|
|
|
|
{
|
|
|
|
return; /*already enabled, don't enable again!*/
|
|
|
|
}
|
|
|
|
|
2011-01-29 00:40:29 +01:00
|
|
|
//Init some consts
|
2011-01-27 22:10:51 +01:00
|
|
|
const QString lamexpFileType(g_lamexpFileType);
|
|
|
|
const QString lamexpFileInfo(tr("Audio File supported by LameXP"));
|
2011-01-29 00:40:29 +01:00
|
|
|
const QString lamexpShellText(tr("Convert this file with LameXP v%1").arg(QString().sprintf("%d.%02d", lamexp_version_major(), lamexp_version_minor())));
|
2015-01-04 17:04:26 +01:00
|
|
|
const QString lamexpShellCommand = QString("\"%1\" \"--add=%2\"").arg(QDir::toNativeSeparators(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()), "%1");
|
2011-01-27 22:10:51 +01:00
|
|
|
const QString lamexpShellAction(g_lamexpShellAction);
|
|
|
|
|
|
|
|
//Register the LameXP file type
|
2015-07-26 14:19:40 +02:00
|
|
|
bool ok[4] = {false, false, false, false};
|
|
|
|
ok[0] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1") .arg(lamexpFileType), QString(), lamexpFileInfo);
|
|
|
|
ok[1] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell") .arg(lamexpFileType), QString(), lamexpShellAction);
|
|
|
|
ok[2] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2") .arg(lamexpFileType, lamexpShellAction), QString(), lamexpShellText);
|
|
|
|
ok[3] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2\\command").arg(lamexpFileType, lamexpShellAction), QString(), lamexpShellCommand);
|
|
|
|
if(!(ok[0] && ok[1] && ok[2] && ok[3]))
|
2015-05-03 21:35:22 +02:00
|
|
|
{
|
2015-07-26 14:19:40 +02:00
|
|
|
qWarning("Failed to register the LameXP file type!");
|
2015-05-03 21:35:22 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-01-27 22:10:51 +01:00
|
|
|
|
|
|
|
//Detect supported file types
|
2015-05-03 18:16:34 +02:00
|
|
|
QStringList types;
|
2015-07-26 14:19:40 +02:00
|
|
|
initializeTypes(lamexpFileType, lamexpShellAction, types);
|
2011-01-27 22:10:51 +01:00
|
|
|
|
|
|
|
//Add LameXP shell action to all supported file types
|
2015-05-03 21:35:22 +02:00
|
|
|
for(QStringList::ConstIterator iter = types.constBegin(); iter != types.constEnd(); iter++)
|
2011-01-27 22:10:51 +01:00
|
|
|
{
|
2015-05-03 21:35:22 +02:00
|
|
|
MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2") .arg((*iter), lamexpShellAction), QString(), lamexpShellText);
|
|
|
|
MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2\\command").arg((*iter), lamexpShellAction), QString(), lamexpShellCommand);
|
2011-01-27 22:10:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Shell notification
|
2015-05-03 18:16:34 +02:00
|
|
|
MUtils::OS::shell_change_notification();
|
2015-05-03 21:35:22 +02:00
|
|
|
m_state = STATE_ENABLED;
|
2011-01-27 22:10:51 +01:00
|
|
|
}
|
|
|
|
|
2011-01-29 00:40:29 +01:00
|
|
|
void ShellIntegration::remove(bool async)
|
2011-01-27 22:10:51 +01:00
|
|
|
{
|
2011-01-29 00:40:29 +01:00
|
|
|
//Remove asynchronously
|
|
|
|
if(async)
|
|
|
|
{
|
2011-07-15 13:44:32 +02:00
|
|
|
QFuture<void>(QtConcurrent::run(remove, false));
|
2011-01-29 00:40:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Serialize
|
|
|
|
QMutexLocker lock(&m_mutex);
|
|
|
|
|
2015-05-03 21:35:22 +02:00
|
|
|
//Checking
|
|
|
|
if(m_state == STATE_DISABLD)
|
|
|
|
{
|
|
|
|
return; /*already enabled, don't enable again!*/
|
|
|
|
}
|
|
|
|
|
2011-01-29 00:40:29 +01:00
|
|
|
//Init some consts
|
|
|
|
const QString lamexpFileType(g_lamexpFileType);
|
|
|
|
const QString lamexpShellAction(g_lamexpShellAction);
|
|
|
|
|
|
|
|
//Initialization
|
|
|
|
QStringList fileTypes;
|
|
|
|
|
|
|
|
//Find all registered file types
|
2015-05-03 18:16:34 +02:00
|
|
|
if(!MUtils::Registry::reg_enum_subkeys(MUtils::Registry::root_user, "Software\\Classes", fileTypes))
|
2011-01-29 00:40:29 +01:00
|
|
|
{
|
2015-05-03 18:16:34 +02:00
|
|
|
qWarning("Failed to enumerate file types!");
|
|
|
|
return;
|
2011-01-29 00:40:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Remove shell action from all file types
|
2015-05-03 21:35:22 +02:00
|
|
|
for(QStringList::ConstIterator iter = fileTypes.constBegin(); iter != fileTypes.constEnd(); iter++)
|
2011-01-29 00:40:29 +01:00
|
|
|
{
|
2015-08-06 21:08:48 +02:00
|
|
|
//Remove LameXP-specific types altogether
|
|
|
|
if(iter->startsWith('.'))
|
|
|
|
{
|
|
|
|
QString currentFileType;
|
|
|
|
if(MUtils::Registry::reg_value_read(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(*iter), QString(), currentFileType))
|
|
|
|
{
|
|
|
|
if(currentFileType.compare(lamexpFileType, Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(*iter));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove shell action for non-LameXP types
|
2015-05-03 21:35:22 +02:00
|
|
|
MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2").arg((*iter), lamexpShellAction));
|
|
|
|
|
|
|
|
//Remove from sub-tree too
|
|
|
|
QStringList subTypes;
|
|
|
|
if(MUtils::Registry::reg_enum_subkeys(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(*iter), subTypes))
|
|
|
|
{
|
|
|
|
for(QStringList::ConstIterator iter2 = subTypes.constBegin(); iter2 != subTypes.constEnd(); iter2++)
|
|
|
|
{
|
|
|
|
MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\%2\\shell\\%3").arg((*iter), (*iter2), lamexpShellAction));
|
|
|
|
}
|
|
|
|
}
|
2011-01-29 00:40:29 +01:00
|
|
|
}
|
|
|
|
|
2015-05-03 21:35:22 +02:00
|
|
|
|
2011-01-29 00:40:29 +01:00
|
|
|
//Unregister LameXP file type
|
2015-05-03 18:16:34 +02:00
|
|
|
MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(lamexpFileType));
|
2011-01-29 00:40:29 +01:00
|
|
|
|
|
|
|
//Shell notification
|
2015-05-03 18:16:34 +02:00
|
|
|
MUtils::OS::shell_change_notification();
|
2015-05-03 21:35:22 +02:00
|
|
|
m_state = STATE_DISABLD;
|
2011-01-27 22:10:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Private Functions
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2015-07-26 14:19:40 +02:00
|
|
|
void ShellIntegration::initializeTypes(const QString &lamexpFileType, const QString &lamexpShellAction, QStringList &nativeTypes)
|
2011-01-27 22:10:51 +01:00
|
|
|
{
|
2015-05-03 18:16:34 +02:00
|
|
|
nativeTypes.clear();
|
2015-05-03 21:35:22 +02:00
|
|
|
const QString progId = "Progid";
|
2011-01-27 22:10:51 +01:00
|
|
|
|
2015-07-26 14:19:40 +02:00
|
|
|
//Map supported extensions to native types
|
|
|
|
const QStringList &supportedExts = DecoderRegistry::getSupportedExts();
|
|
|
|
for(QStringList::ConstIterator iter = supportedExts.constBegin(); iter != supportedExts.constEnd(); iter++)
|
2011-01-27 22:10:51 +01:00
|
|
|
{
|
2015-07-26 14:19:40 +02:00
|
|
|
const QString currentExt = (*iter).mid(1).trimmed(); /*remove leading asterisk*/
|
|
|
|
if(currentExt.isEmpty() || (!currentExt.startsWith('.')))
|
2011-01-27 22:10:51 +01:00
|
|
|
{
|
2015-07-26 14:19:40 +02:00
|
|
|
qWarning("Invalid file extension '%s' encountered -> skipping!", currentExt);
|
|
|
|
continue;
|
2011-01-27 22:10:51 +01:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:08:27 +02:00
|
|
|
bool hasExistingType = false;
|
2015-05-03 18:16:34 +02:00
|
|
|
QString currentType;
|
2015-07-26 15:08:27 +02:00
|
|
|
if(MUtils::Registry::reg_key_exists(MUtils::Registry::root_classes, currentExt))
|
2011-01-27 22:10:51 +01:00
|
|
|
{
|
2015-07-26 15:08:27 +02:00
|
|
|
if(MUtils::Registry::reg_value_read(MUtils::Registry::root_classes, currentExt, QString(), currentType))
|
2011-01-27 22:10:51 +01:00
|
|
|
{
|
2015-07-26 15:08:27 +02:00
|
|
|
currentType = QDir::toNativeSeparators(currentType);
|
|
|
|
if((currentType.compare(lamexpFileType, Qt::CaseInsensitive) != 0) && (!nativeTypes.contains(currentType, Qt::CaseInsensitive)))
|
|
|
|
{
|
|
|
|
nativeTypes.append(currentType);
|
|
|
|
hasExistingType = true;
|
|
|
|
}
|
2011-01-27 22:10:51 +01:00
|
|
|
}
|
|
|
|
}
|
2015-07-26 15:08:27 +02:00
|
|
|
if(!hasExistingType)
|
2011-01-27 22:10:51 +01:00
|
|
|
{
|
2015-07-26 15:08:27 +02:00
|
|
|
currentType = lamexpFileType;
|
2015-07-26 14:19:40 +02:00
|
|
|
MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(currentExt), QString(), lamexpFileType);
|
2011-01-27 22:10:51 +01:00
|
|
|
}
|
2011-01-29 00:40:29 +01:00
|
|
|
|
2015-07-26 15:08:27 +02:00
|
|
|
const QString userChoiceKey = QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\UserChoice").arg(currentExt);
|
|
|
|
if(MUtils::Registry::reg_key_exists(MUtils::Registry::root_user, userChoiceKey))
|
2011-01-29 00:40:29 +01:00
|
|
|
{
|
2015-07-26 15:08:27 +02:00
|
|
|
if(MUtils::Registry::reg_value_read(MUtils::Registry::root_user, userChoiceKey, progId, currentType))
|
2011-01-29 00:40:29 +01:00
|
|
|
{
|
2015-07-26 15:08:27 +02:00
|
|
|
currentType = QDir::toNativeSeparators(currentType);
|
|
|
|
if((currentType.compare(lamexpFileType, Qt::CaseInsensitive) != 0) && (!nativeTypes.contains(currentType, Qt::CaseInsensitive)))
|
|
|
|
{
|
|
|
|
nativeTypes.append(currentType);
|
|
|
|
}
|
2011-01-29 00:40:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 21:35:22 +02:00
|
|
|
QStringList progids;
|
2015-07-26 14:19:40 +02:00
|
|
|
if(MUtils::Registry::reg_enum_values(MUtils::Registry::root_user, QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\OpenWithProgids").arg(currentExt), progids))
|
2011-01-29 00:40:29 +01:00
|
|
|
{
|
2015-05-03 21:35:22 +02:00
|
|
|
for(QStringList::ConstIterator iter2 = progids.constBegin(); iter2 != progids.constEnd(); iter2++)
|
2011-01-29 00:40:29 +01:00
|
|
|
{
|
2015-05-03 21:35:22 +02:00
|
|
|
currentType = QDir::toNativeSeparators(currentType);
|
|
|
|
if((iter2->compare(lamexpFileType, Qt::CaseInsensitive) != 0) && (!nativeTypes.contains((*iter2), Qt::CaseInsensitive)))
|
|
|
|
{
|
|
|
|
nativeTypes.append(*iter2);
|
|
|
|
}
|
2011-01-29 00:40:29 +01:00
|
|
|
}
|
|
|
|
}
|
2011-01-27 22:10:51 +01:00
|
|
|
}
|
|
|
|
}
|