1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "ShellIntegration.h"
25 #include <QStringList>
27 #include <QApplication>
30 #include <QMutexLocker>
31 #include <QtConcurrentRun>
37 #include "Registry_Decoder.h"
40 static const char *g_lamexpShellAction
= "ConvertWithLameXP";
41 static const char *g_lamexpFileType
= "LameXP.SupportedAudioFile";
44 QMutex
ShellIntegration::m_mutex
;
47 #define REG_WRITE_STRING(KEY, STR) RegSetValueEx(key, NULL, NULL, REG_SZ, reinterpret_cast<const BYTE*>(STR.utf16()), (STR.size() + 1) * sizeof(wchar_t))
49 ////////////////////////////////////////////////////////////
51 ////////////////////////////////////////////////////////////
53 ShellIntegration::ShellIntegration(void)
55 throw "Cannot create instance of this class, sorry!";
58 ////////////////////////////////////////////////////////////
60 ////////////////////////////////////////////////////////////
62 void ShellIntegration::install(bool async
)
64 //Install asynchronously
67 QFuture
<void>(QtConcurrent::run(install
, false));
72 QMutexLocker
lock(&m_mutex
);
78 const QString
lamexpFileType(g_lamexpFileType
);
79 const QString
lamexpFileInfo(tr("Audio File supported by LameXP"));
80 const QString
lamexpShellText(tr("Convert this file with LameXP v%1").arg(QString().sprintf("%d.%02d", lamexp_version_major(), lamexp_version_minor())));
81 const QString lamexpShellCommand
= QString("\"%1\" --add \"%2\"").arg(QDir::toNativeSeparators(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()), "%1");
82 const QString
lamexpShellAction(g_lamexpShellAction
);
84 //Register the LameXP file type
85 if(RegCreateKeyEx(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Classes\\%1").arg(lamexpFileType
)), NULL
, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
)
87 REG_WRITE_STRING(key
, lamexpFileInfo
);
90 if(RegCreateKeyEx(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Classes\\%1\\shell").arg(lamexpFileType
)), NULL
, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
)
92 REG_WRITE_STRING(key
, lamexpShellAction
);
95 if(RegCreateKeyEx(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Classes\\%1\\shell\\%2").arg(lamexpFileType
, lamexpShellAction
)), NULL
, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
)
97 REG_WRITE_STRING(key
, lamexpShellText
);
100 if(RegCreateKeyEx(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Classes\\%1\\shell\\%2\\command").arg(lamexpFileType
, lamexpShellAction
)), NULL
, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
)
102 REG_WRITE_STRING(key
, lamexpShellCommand
);
106 //Detect supported file types
107 QStringList
*types
= detectTypes(lamexpFileType
, lamexpShellAction
);
109 //Add LameXP shell action to all supported file types
110 while(types
&& (!types
->isEmpty()))
112 QString currentType
= types
->takeFirst();
114 if(RegCreateKeyEx(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Classes\\%1\\shell\\%2").arg(currentType
, lamexpShellAction
)), NULL
, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
)
116 REG_WRITE_STRING(key
, lamexpShellText
);
120 if(RegCreateKeyEx(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Classes\\%1\\shell\\%2\\command").arg(currentType
, lamexpShellAction
)), NULL
, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
)
122 REG_WRITE_STRING(key
, lamexpShellCommand
);
128 SHChangeNotify(SHCNE_ASSOCCHANGED
, SHCNF_IDLIST
, NULL
, NULL
);
134 void ShellIntegration::remove(bool async
)
136 //Remove asynchronously
139 QFuture
<void>(QtConcurrent::run(remove
, false));
144 QMutexLocker
lock(&m_mutex
);
147 const QString
lamexpFileType(g_lamexpFileType
);
148 const QString
lamexpShellAction(g_lamexpShellAction
);
152 QStringList fileTypes
;
154 //Find all registered file types
155 if(RegOpenKeyEx(HKEY_CURRENT_USER
, L
"Software\\Classes", NULL
, KEY_ENUMERATE_SUB_KEYS
,&key
) == ERROR_SUCCESS
)
159 for(DWORD i
= 0; true; i
++)
162 if(RegEnumKeyEx(key
, i
, name
, &size
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
)
164 fileTypes
<< QString::fromUtf16(reinterpret_cast<const unsigned short*>(name
));
171 //Remove shell action from all file types
172 while(!fileTypes
.isEmpty())
174 SHDeleteKey(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Classes\\%1\\shell\\%2").arg(fileTypes
.takeFirst(), lamexpShellAction
)));
177 //Unregister LameXP file type
178 SHDeleteKey(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Classes\\%1").arg(lamexpFileType
)));
181 SHChangeNotify(SHCNE_ASSOCCHANGED
, SHCNF_IDLIST
, NULL
, NULL
);
184 ////////////////////////////////////////////////////////////
186 ////////////////////////////////////////////////////////////
188 QStringList
*ShellIntegration::detectTypes(const QString
&lamexpFileType
, const QString
&lamexpShellAction
)
192 QStringList
*nativeTypes
= new QStringList();
193 QStringList supportedTypes
= DecoderRegistry::getSupportedTypes();
194 QStringList extensions
;
196 QRegExp
regExp1("\\((.+)\\)");
197 QRegExp
regExp2("(\\.\\w+)");
199 //Find all supported file extensions
200 while(!supportedTypes
.isEmpty())
202 if(regExp1
.lastIndexIn(supportedTypes
.takeFirst()) > 0)
205 while((lastIndex
= regExp2
.indexIn(regExp1
.cap(1), lastIndex
) + 1) >= 1)
207 extensions
.append(regExp2
.cap(1));
212 //Map supported extensions to native types
213 while(!extensions
.isEmpty())
215 QString currentExt
= extensions
.takeFirst();
217 if(RegOpenKeyEx(HKEY_CLASSES_ROOT
, QWCHAR(currentExt
), NULL
, KEY_QUERY_VALUE
, &key
) == ERROR_SUCCESS
)
220 DWORD dataLen
= 256 * sizeof(wchar_t);
223 if(RegQueryValueEx(key
, NULL
, NULL
, &type
, reinterpret_cast<BYTE
*>(data
), &dataLen
) == ERROR_SUCCESS
)
225 if((type
== REG_SZ
) || (type
== REG_EXPAND_SZ
))
227 QString currentType
= QString::fromUtf16(reinterpret_cast<unsigned short*>(data
));
228 if((currentType
.compare(lamexpFileType
, Qt::CaseInsensitive
) != 0) && !nativeTypes
->contains(currentType
, Qt::CaseInsensitive
))
230 nativeTypes
->append(currentType
);
238 if(RegCreateKeyEx(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Classes\\%1").arg(currentExt
)), NULL
, NULL
, REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &key
, NULL
) == ERROR_SUCCESS
)
240 REG_WRITE_STRING(key
, lamexpFileType
);
245 if(RegOpenKeyEx(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\UserChoice").arg(currentExt
)), NULL
, KEY_QUERY_VALUE
, &key
) == ERROR_SUCCESS
)
248 DWORD dataLen
= 256 * sizeof(wchar_t);
251 if(RegQueryValueEx(key
, L
"Progid", NULL
, &type
, reinterpret_cast<BYTE
*>(data
), &dataLen
) == ERROR_SUCCESS
)
253 if((type
== REG_SZ
) || (type
== REG_EXPAND_SZ
))
255 QString currentType
= QString::fromUtf16(reinterpret_cast<unsigned short*>(data
));
256 if((currentType
.compare(lamexpFileType
, Qt::CaseInsensitive
) != 0) && !nativeTypes
->contains(currentType
, Qt::CaseInsensitive
))
258 nativeTypes
->append(currentType
);
265 if(RegOpenKeyEx(HKEY_CURRENT_USER
, QWCHAR(QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\OpenWithProgids").arg(currentExt
)), NULL
, KEY_QUERY_VALUE
, &key
) == ERROR_SUCCESS
)
268 for(DWORD i
= 0; true; i
++)
271 if(RegEnumValue(key
, i
, name
, &size
, NULL
, NULL
, NULL
, NULL
) == ERROR_SUCCESS
)
273 QString currentType
= QString::fromUtf16(reinterpret_cast<unsigned short*>(name
));
274 if((currentType
.compare(lamexpFileType
, Qt::CaseInsensitive
) != 0) && !nativeTypes
->contains(currentType
, Qt::CaseInsensitive
))
276 nativeTypes
->append(currentType
);