1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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 QMutex
ShellIntegration::m_mutex
;
53 static const int STATE_ENABLED
= 1;
54 static const int STATE_UNKNOWN
= 0;
55 static const int STATE_DISABLD
= -1;
58 volatile int ShellIntegration::m_state
= STATE_UNKNOWN
;
61 #define REG_WRITE_STRING(KEY, STR) RegSetValueEx(key, NULL, NULL, REG_SZ, reinterpret_cast<const BYTE*>(STR.utf16()), (STR.size() + 1) * sizeof(wchar_t))
63 ////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////
67 ShellIntegration::ShellIntegration(void)
69 MUTILS_THROW("Cannot create instance of this class, sorry!");
72 ////////////////////////////////////////////////////////////
74 ////////////////////////////////////////////////////////////
76 void ShellIntegration::install(bool async
)
78 //Install asynchronously
81 QFuture
<void>(QtConcurrent::run(install
, false));
86 QMutexLocker
lock(&m_mutex
);
89 if(m_state
== STATE_ENABLED
)
91 return; /*already enabled, don't enable again!*/
95 const QString
lamexpFileType(g_lamexpFileType
);
96 const QString
lamexpFileInfo(tr("Audio File supported by LameXP"));
97 const QString
lamexpShellText(tr("Convert this file with LameXP v%1").arg(QString().sprintf("%d.%02d", lamexp_version_major(), lamexp_version_minor())));
98 const QString lamexpShellCommand
= QString("\"%1\" \"--add=%2\"").arg(QDir::toNativeSeparators(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()), "%1");
99 const QString
lamexpShellAction(g_lamexpShellAction
);
101 //Register the LameXP file type
102 const bool ok1
= MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1") .arg(lamexpFileType
), QString(), lamexpFileInfo
);
103 const bool ok2
= MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\shell") .arg(lamexpFileType
), QString(), lamexpShellAction
);
104 const bool ok3
= MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\shell\\%2") .arg(lamexpFileType
, lamexpShellAction
), QString(), lamexpShellText
);
105 const bool ok4
= MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\shell\\%2\\command").arg(lamexpFileType
, lamexpShellAction
), QString(), lamexpShellCommand
);
106 if(!(ok1
&& ok2
&& ok3
&& ok4
))
108 qWarning("Failed to create LameXP file type!");
112 //Detect supported file types
114 detectTypes(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
);
124 MUtils::OS::shell_change_notification();
125 m_state
= STATE_ENABLED
;
128 void ShellIntegration::remove(bool async
)
130 //Remove asynchronously
133 QFuture
<void>(QtConcurrent::run(remove
, false));
138 QMutexLocker
lock(&m_mutex
);
141 if(m_state
== STATE_DISABLD
)
143 return; /*already enabled, don't enable again!*/
147 const QString
lamexpFileType(g_lamexpFileType
);
148 const QString
lamexpShellAction(g_lamexpShellAction
);
151 QStringList fileTypes
;
153 //Find all registered file types
154 if(!MUtils::Registry::reg_enum_subkeys(MUtils::Registry::root_user
, "Software\\Classes", fileTypes
))
156 qWarning("Failed to enumerate file types!");
160 //Remove shell action from all file types
161 for(QStringList::ConstIterator iter
= fileTypes
.constBegin(); iter
!= fileTypes
.constEnd(); iter
++)
163 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\shell\\%2").arg((*iter
), lamexpShellAction
));
165 //Remove from sub-tree too
166 QStringList subTypes
;
167 if(MUtils::Registry::reg_enum_subkeys(MUtils::Registry::root_user
, QString("Software\\Classes\\%1").arg(*iter
), subTypes
))
169 for(QStringList::ConstIterator iter2
= subTypes
.constBegin(); iter2
!= subTypes
.constEnd(); iter2
++)
171 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user
, QString("Software\\Classes\\%1\\%2\\shell\\%3").arg((*iter
), (*iter2
), lamexpShellAction
));
177 //Unregister LameXP file type
178 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user
, QString("Software\\Classes\\%1").arg(lamexpFileType
));
181 MUtils::OS::shell_change_notification();
182 m_state
= STATE_DISABLD
;
185 ////////////////////////////////////////////////////////////
187 ////////////////////////////////////////////////////////////
189 void ShellIntegration::detectTypes(const QString
&lamexpFileType
, const QString
&lamexpShellAction
, QStringList
&nativeTypes
)
192 const QStringList supportedTypes
= DecoderRegistry::getSupportedTypes();
193 const QString progId
= "Progid";
195 QRegExp
regExp1("\\((.+)\\)");
196 QRegExp
regExp2("(\\.\\w+)");
198 //Find all supported file extensions
199 QStringList extensions
;
200 for(QStringList::ConstIterator iter
= supportedTypes
.constBegin(); iter
!= supportedTypes
.constEnd(); iter
++)
202 if(regExp1
.lastIndexIn(*iter
) > 0)
205 while((lastIndex
= regExp2
.indexIn(regExp1
.cap(1), lastIndex
) + 1) >= 1)
207 if(!extensions
.contains(regExp2
.cap(1), Qt::CaseInsensitive
))
209 extensions
.append(regExp2
.cap(1));
215 //Map supported extensions to native types
216 for(QStringList::ConstIterator iter
= extensions
.constBegin(); iter
!= extensions
.constEnd(); iter
++)
219 if(MUtils::Registry::reg_value_read(MUtils::Registry::root_classes
, (*iter
), QString(), currentType
))
221 currentType
= QDir::toNativeSeparators(currentType
);
222 if((currentType
.compare(lamexpFileType
, Qt::CaseInsensitive
) != 0) && (!nativeTypes
.contains(currentType
, Qt::CaseInsensitive
)))
224 nativeTypes
.append(currentType
);
229 MUtils::Registry::reg_value_write(MUtils::Registry::root_user
, (*iter
), QString(), lamexpFileType
);
232 if(MUtils::Registry::reg_value_read(MUtils::Registry::root_user
, QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\UserChoice").arg(*iter
), progId
, currentType
))
234 currentType
= QDir::toNativeSeparators(currentType
);
235 if((currentType
.compare(lamexpFileType
, Qt::CaseInsensitive
) != 0) && (!nativeTypes
.contains(currentType
, Qt::CaseInsensitive
)))
237 nativeTypes
.append(currentType
);
242 if(MUtils::Registry::reg_enum_values(MUtils::Registry::root_user
, QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\OpenWithProgids").arg(*iter
), progids
))
244 for(QStringList::ConstIterator iter2
= progids
.constBegin(); iter2
!= progids
.constEnd(); iter2
++)
246 currentType
= QDir::toNativeSeparators(currentType
);
247 if((iter2
->compare(lamexpFileType
, Qt::CaseInsensitive
) != 0) && (!nativeTypes
.contains((*iter2
), Qt::CaseInsensitive
)))
249 nativeTypes
.append(*iter2
);