Happy new year 2019!
[LameXP.git] / src / ShellIntegration.cpp
blob0bf9d71125159402db282d1a42b962587373fdbf
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
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.
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.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "ShellIntegration.h"
25 //Internal
26 #include "Global.h"
27 #include "Registry_Decoder.h"
29 //MUtils
30 #include <MUtils/Global.h>
31 #include <MUtils/Exception.h>
32 #include <MUtils/OSSupport.h>
33 #include <MUtils/Registry.h>
35 //Qt
36 #include <QString>
37 #include <QStringList>
38 #include <QRegExp>
39 #include <QApplication>
40 #include <QFileInfo>
41 #include <QDir>
42 #include <QMutexLocker>
43 #include <QtConcurrentRun>
45 //Const
46 static const char *g_lamexpShellAction = "ConvertWithLameXP";
47 static const char *g_lamexpFileType = "LameXP.SupportedAudioFile";
49 //State values
50 static const int STATE_ENABLED = 1;
51 static const int STATE_UNKNOWN = 0;
52 static const int STATE_DISABLD = -1;
54 //State
55 QAtomicInt ShellIntegration::m_state(STATE_UNKNOWN);
56 QMutex ShellIntegration::m_mutex;
58 //Macros
59 #define REG_WRITE_STRING(KEY, STR) RegSetValueEx(key, NULL, NULL, REG_SZ, reinterpret_cast<const BYTE*>(STR.utf16()), (STR.size() + 1) * sizeof(wchar_t))
61 ////////////////////////////////////////////////////////////
62 // Constructor
63 ////////////////////////////////////////////////////////////
65 ShellIntegration::ShellIntegration(void)
67 MUTILS_THROW("Cannot create instance of this class, sorry!");
70 ////////////////////////////////////////////////////////////
71 // Public Functions
72 ////////////////////////////////////////////////////////////
74 void ShellIntegration::install(bool async)
76 //Install asynchronously
77 if(async)
79 QFuture<void>(QtConcurrent::run(install, false));
80 return;
83 //Serialize
84 QMutexLocker lock(&m_mutex);
86 //Checking
87 if(m_state.fetchAndStoreOrdered(STATE_ENABLED) == STATE_ENABLED)
89 return; /*already enabled, don't enable again!*/
92 //Init some consts
93 const QString lamexpFileType(g_lamexpFileType);
94 const QString lamexpFileInfo(tr("Audio File supported by LameXP"));
95 const QString lamexpShellText(tr("Convert this file with LameXP v%1").arg(QString().sprintf("%d.%02d", lamexp_version_major(), lamexp_version_minor())));
96 const QString lamexpShellCommand = QString("\"%1\" \"--add=%2\"").arg(QDir::toNativeSeparators(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()), "%1");
97 const QString lamexpShellAction(g_lamexpShellAction);
99 //Register the LameXP file type
100 bool ok[4] = {false, false, false, false};
101 ok[0] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1") .arg(lamexpFileType), QString(), lamexpFileInfo);
102 ok[1] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell") .arg(lamexpFileType), QString(), lamexpShellAction);
103 ok[2] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2") .arg(lamexpFileType, lamexpShellAction), QString(), lamexpShellText);
104 ok[3] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2\\command").arg(lamexpFileType, lamexpShellAction), QString(), lamexpShellCommand);
105 if(!(ok[0] && ok[1] && ok[2] && ok[3]))
107 m_state.fetchAndStoreOrdered(STATE_UNKNOWN);
108 qWarning("Failed to register the LameXP file type!");
109 return;
112 //Detect supported file types
113 QStringList types;
114 initializeTypes(lamexpFileType, lamexpShellAction, types);
116 //Add LameXP shell action to all supported file types
117 for(QStringList::ConstIterator iter = types.constBegin(); iter != types.constEnd(); iter++)
119 MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2") .arg((*iter), lamexpShellAction), QString(), lamexpShellText);
120 MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2\\command").arg((*iter), lamexpShellAction), QString(), lamexpShellCommand);
123 //Shell notification
124 MUtils::OS::shell_change_notification();
127 void ShellIntegration::remove(bool async)
129 //Remove asynchronously
130 if(async)
132 QFuture<void>(QtConcurrent::run(remove, false));
133 return;
136 //Serialize
137 QMutexLocker lock(&m_mutex);
139 //Checking
140 if(m_state.fetchAndStoreOrdered(STATE_DISABLD) == STATE_DISABLD)
142 return; /*already disabled, don't disable again!*/
145 //Init some consts
146 const QString lamexpFileType(g_lamexpFileType);
147 const QString lamexpShellAction(g_lamexpShellAction);
149 //Initialization
150 QStringList fileTypes;
152 //Find all registered file types
153 if(!MUtils::Registry::reg_enum_subkeys(MUtils::Registry::root_user, "Software\\Classes", fileTypes))
155 m_state.fetchAndStoreOrdered(STATE_UNKNOWN);
156 qWarning("Failed to enumerate file types!");
157 return;
160 //Remove shell action from all file types
161 for(QStringList::ConstIterator iter = fileTypes.constBegin(); iter != fileTypes.constEnd(); iter++)
163 //Remove LameXP-specific types altogether
164 if(iter->startsWith('.'))
166 QString currentFileType;
167 if(MUtils::Registry::reg_value_read(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(*iter), QString(), currentFileType))
169 if(currentFileType.compare(lamexpFileType, Qt::CaseInsensitive) == 0)
171 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(*iter));
172 continue;
177 //Remove shell action for non-LameXP types
178 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2").arg((*iter), lamexpShellAction));
180 //Remove from sub-tree too
181 QStringList subTypes;
182 if(MUtils::Registry::reg_enum_subkeys(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(*iter), subTypes))
184 for(QStringList::ConstIterator iter2 = subTypes.constBegin(); iter2 != subTypes.constEnd(); iter2++)
186 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\%2\\shell\\%3").arg((*iter), (*iter2), lamexpShellAction));
192 //Unregister LameXP file type
193 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(lamexpFileType));
195 //Shell notification
196 MUtils::OS::shell_change_notification();
199 ////////////////////////////////////////////////////////////
200 // Private Functions
201 ////////////////////////////////////////////////////////////
203 void ShellIntegration::initializeTypes(const QString &lamexpFileType, const QString &lamexpShellAction, QStringList &nativeTypes)
205 nativeTypes.clear();
206 const QString progId = "Progid";
208 //Map supported extensions to native types
209 const QStringList &supportedExts = DecoderRegistry::getSupportedExts();
210 for(QStringList::ConstIterator iter = supportedExts.constBegin(); iter != supportedExts.constEnd(); iter++)
212 const QString currentExt = (*iter).mid(1).trimmed(); /*remove leading asterisk*/
213 if(currentExt.isEmpty() || (!currentExt.startsWith('.')))
215 qWarning("Invalid file extension '%s' encountered -> skipping!", currentExt);
216 continue;
219 bool hasExistingType = false;
220 QString currentType;
221 if(MUtils::Registry::reg_key_exists(MUtils::Registry::root_classes, currentExt))
223 if(MUtils::Registry::reg_value_read(MUtils::Registry::root_classes, currentExt, QString(), currentType))
225 currentType = QDir::toNativeSeparators(currentType);
226 if((currentType.compare(lamexpFileType, Qt::CaseInsensitive) != 0) && (!nativeTypes.contains(currentType, Qt::CaseInsensitive)))
228 nativeTypes.append(currentType);
229 hasExistingType = true;
233 if(!hasExistingType)
235 currentType = lamexpFileType;
236 MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(currentExt), QString(), lamexpFileType);
239 const QString userChoiceKey = QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\UserChoice").arg(currentExt);
240 if(MUtils::Registry::reg_key_exists(MUtils::Registry::root_user, userChoiceKey))
242 if(MUtils::Registry::reg_value_read(MUtils::Registry::root_user, userChoiceKey, progId, currentType))
244 currentType = QDir::toNativeSeparators(currentType);
245 if((currentType.compare(lamexpFileType, Qt::CaseInsensitive) != 0) && (!nativeTypes.contains(currentType, Qt::CaseInsensitive)))
247 nativeTypes.append(currentType);
252 QStringList progids;
253 if(MUtils::Registry::reg_enum_values(MUtils::Registry::root_user, QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\OpenWithProgids").arg(currentExt), progids))
255 for(QStringList::ConstIterator iter2 = progids.constBegin(); iter2 != progids.constEnd(); iter2++)
257 currentType = QDir::toNativeSeparators(currentType);
258 if((iter2->compare(lamexpFileType, Qt::CaseInsensitive) != 0) && (!nativeTypes.contains((*iter2), Qt::CaseInsensitive)))
260 nativeTypes.append(*iter2);