1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
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"
27 #include "Registry_Decoder.h"
30 #include <MUtils/Global.h>
31 #include <MUtils/Exception.h>
32 #include <MUtils/OSSupport.h>
33 #include <MUtils/Registry.h>
37 #include <QStringList>
39 #include <QApplication>
42 #include <QMutexLocker>
43 #include <QtConcurrentRun>
46 static const char *g_lamexpShellAction
= "ConvertWithLameXP";
47 static const char *g_lamexpFileType
= "LameXP.SupportedAudioFile";
50 static const int STATE_ENABLED
= 1;
51 static const int STATE_UNKNOWN
= 0;
52 static const int STATE_DISABLD
= -1;
55 QAtomicInt
ShellIntegration::m_state(STATE_UNKNOWN
);
58 #define REG_WRITE_STRING(KEY, STR) RegSetValueEx(key, NULL, NULL, REG_SZ, reinterpret_cast<const BYTE*>(STR.utf16()), (STR.size() + 1) * sizeof(wchar_t))
60 ////////////////////////////////////////////////////////////
62 ////////////////////////////////////////////////////////////
64 ShellIntegration::ShellIntegration(void)
66 MUTILS_THROW("Cannot create instance of this class, sorry!");
69 ////////////////////////////////////////////////////////////
71 ////////////////////////////////////////////////////////////
73 void ShellIntegration::install(bool async
)
75 //Install asynchronously
78 QFuture
<void>(QtConcurrent::run(install
, false));
83 QMutexLocker
lock(&m_mutex
);
86 if(m_state
.fetchAndStoreOrdered(STATE_ENABLED
) == STATE_ENABLED
)
88 return; /*already enabled, don't enable again!*/
92 const QString
lamexpFileType(g_lamexpFileType
);
93 const QString
lamexpFileInfo(tr("Audio File supported by LameXP"));
94 const QString
lamexpShellText(tr("Convert this file with LameXP v%1").arg(QString().sprintf("%d.%02d", lamexp_version_major(), lamexp_version_minor())));
95 const QString lamexpShellCommand
= QString("\"%1\" \"--add=%2\"").arg(QDir::toNativeSeparators(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()), "%1");
96 const QString
lamexpShellAction(g_lamexpShellAction
);
98 //Register the LameXP file type
99 bool ok
[4] = {false, false, false, false};
100 ok
[0] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1") .arg(lamexpFileType
), QString(), lamexpFileInfo
);
101 ok
[1] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\shell") .arg(lamexpFileType
), QString(), lamexpShellAction
);
102 ok
[2] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\shell\\%2") .arg(lamexpFileType
, lamexpShellAction
), QString(), lamexpShellText
);
103 ok
[3] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\shell\\%2\\command").arg(lamexpFileType
, lamexpShellAction
), QString(), lamexpShellCommand
);
104 if(!(ok
[0] && ok
[1] && ok
[2] && ok
[3]))
106 m_state
.fetchAndStoreOrdered(STATE_UNKNOWN
);
107 qWarning("Failed to register the LameXP file type!");
111 //Detect supported file types
113 initializeTypes(lamexpFileType
, lamexpShellAction
, types
);
115 //Add LameXP shell action to all supported file types
116 for(QStringList::ConstIterator iter
= types
.constBegin(); iter
!= types
.constEnd(); iter
++)
118 MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\shell\\%2") .arg((*iter
), lamexpShellAction
), QString(), lamexpShellText
);
119 MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\shell\\%2\\command").arg((*iter
), lamexpShellAction
), QString(), lamexpShellCommand
);
123 MUtils::OS::shell_change_notification();
126 void ShellIntegration::remove(bool async
)
128 //Remove asynchronously
131 QFuture
<void>(QtConcurrent::run(remove
, false));
136 QMutexLocker
lock(&m_mutex
);
139 if(m_state
.fetchAndStoreOrdered(STATE_DISABLD
) == STATE_DISABLD
)
141 return; /*already disabled, don't disable again!*/
145 const QString
lamexpFileType(g_lamexpFileType
);
146 const QString
lamexpShellAction(g_lamexpShellAction
);
149 QStringList fileTypes
;
151 //Find all registered file types
152 if(!MUtils::Registry::reg_enum_subkeys(MUtils::Registry::root_user
, "Software\\Classes", fileTypes
))
154 m_state
.fetchAndStoreOrdered(STATE_UNKNOWN
);
155 qWarning("Failed to enumerate file types!");
159 //Remove shell action from all file types
160 for(QStringList::ConstIterator iter
= fileTypes
.constBegin(); iter
!= fileTypes
.constEnd(); iter
++)
162 //Remove LameXP-specific types altogether
163 if(iter
->startsWith('.'))
165 QString currentFileType
;
166 if(MUtils::Registry::reg_value_read(MUtils::Registry::root_user
, QString("Software\\Classes\\%1").arg(*iter
), QString(), currentFileType
))
168 if(currentFileType
.compare(lamexpFileType
, Qt::CaseInsensitive
) == 0)
170 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user
, QString("Software\\Classes\\%1").arg(*iter
));
176 //Remove shell action for non-LameXP types
177 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\shell\\%2").arg((*iter
), lamexpShellAction
));
179 //Remove from sub-tree too
180 QStringList subTypes
;
181 if(MUtils::Registry::reg_enum_subkeys(MUtils::Registry::root_user
, QString("Software\\Classes\\%1").arg(*iter
), subTypes
))
183 for(QStringList::ConstIterator iter2
= subTypes
.constBegin(); iter2
!= subTypes
.constEnd(); iter2
++)
185 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\%2\\shell\\%3").arg((*iter
), (*iter2
), lamexpShellAction
));
191 //Unregister LameXP file type
192 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user
, QString("Software\\Classes\\%1").arg(lamexpFileType
));
195 MUtils::OS::shell_change_notification();
198 ////////////////////////////////////////////////////////////
200 ////////////////////////////////////////////////////////////
202 void ShellIntegration::initializeTypes(const QString
&lamexpFileType
, const QString
&lamexpShellAction
, QStringList
&nativeTypes
)
205 const QString progId
= "Progid";
207 //Map supported extensions to native types
208 const QStringList
&supportedExts
= DecoderRegistry::getSupportedExts();
209 for(QStringList::ConstIterator iter
= supportedExts
.constBegin(); iter
!= supportedExts
.constEnd(); iter
++)
211 const QString currentExt
= (*iter
).mid(1).trimmed(); /*remove leading asterisk*/
212 if(currentExt
.isEmpty() || (!currentExt
.startsWith('.')))
214 qWarning("Invalid file extension '%s' encountered -> skipping!", currentExt
);
218 bool hasExistingType
= false;
220 if(MUtils::Registry::reg_key_exists(MUtils::Registry::root_classes
, currentExt
))
222 if(MUtils::Registry::reg_value_read(MUtils::Registry::root_classes
, currentExt
, QString(), currentType
))
224 currentType
= QDir::toNativeSeparators(currentType
);
225 if((currentType
.compare(lamexpFileType
, Qt::CaseInsensitive
) != 0) && (!nativeTypes
.contains(currentType
, Qt::CaseInsensitive
)))
227 nativeTypes
.append(currentType
);
228 hasExistingType
= true;
234 currentType
= lamexpFileType
;
235 MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1").arg(currentExt
), QString(), lamexpFileType
);
238 const QString userChoiceKey
= QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\UserChoice").arg(currentExt
);
239 if(MUtils::Registry::reg_key_exists(MUtils::Registry::root_user
, userChoiceKey
))
241 if(MUtils::Registry::reg_value_read(MUtils::Registry::root_user
, userChoiceKey
, progId
, currentType
))
243 currentType
= QDir::toNativeSeparators(currentType
);
244 if((currentType
.compare(lamexpFileType
, Qt::CaseInsensitive
) != 0) && (!nativeTypes
.contains(currentType
, Qt::CaseInsensitive
)))
246 nativeTypes
.append(currentType
);
252 if(MUtils::Registry::reg_enum_values(MUtils::Registry::root_user
, QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\OpenWithProgids").arg(currentExt
), progids
))
254 for(QStringList::ConstIterator iter2
= progids
.constBegin(); iter2
!= progids
.constEnd(); iter2
++)
256 currentType
= QDir::toNativeSeparators(currentType
);
257 if((iter2
->compare(lamexpFileType
, Qt::CaseInsensitive
) != 0) && (!nativeTypes
.contains((*iter2
), Qt::CaseInsensitive
)))
259 nativeTypes
.append(*iter2
);