Updated Ukrainian translation.
[LameXP.git] / src / ShellIntegration.cpp
blob0ac49c9edb0d30e88ed5b91dd367d248e7cda7b6
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
9 //
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"
24 #include <QString>
25 #include <QStringList>
26 #include <QRegExp>
27 #include <QApplication>
28 #include <QFileInfo>
29 #include <QDir>
30 #include <QMutexLocker>
31 #include <QtConcurrentRun>
33 #include <Shlobj.h>
34 #include <Shlwapi.h>
36 #include "Global.h"
37 #include "Registry_Decoder.h"
39 //Const
40 static const char *g_lamexpShellAction = "ConvertWithLameXP";
41 static const char *g_lamexpFileType = "LameXP.SupportedAudioFile";
43 //Mutex
44 QMutex ShellIntegration::m_mutex;
46 //Macros
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 ////////////////////////////////////////////////////////////
50 // Constructor
51 ////////////////////////////////////////////////////////////
53 ShellIntegration::ShellIntegration(void)
55 throw "Cannot create instance of this class, sorry!";
58 ////////////////////////////////////////////////////////////
59 // Public Functions
60 ////////////////////////////////////////////////////////////
62 void ShellIntegration::install(bool async)
64 //Install asynchronously
65 if(async)
67 QFuture<void>(QtConcurrent::run(install, false));
68 return;
71 //Serialize
72 QMutexLocker lock(&m_mutex);
74 //Registry key
75 HKEY key = NULL;
77 //Init some consts
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);
88 RegCloseKey(key);
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);
93 RegCloseKey(key);
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);
98 RegCloseKey(key);
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);
103 RegCloseKey(key);
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);
117 RegCloseKey(key);
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);
123 RegCloseKey(key);
127 //Shell notification
128 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
130 //Free
131 delete types;
134 void ShellIntegration::remove(bool async)
136 //Remove asynchronously
137 if(async)
139 QFuture<void>(QtConcurrent::run(remove, false));
140 return;
143 //Serialize
144 QMutexLocker lock(&m_mutex);
146 //Init some consts
147 const QString lamexpFileType(g_lamexpFileType);
148 const QString lamexpShellAction(g_lamexpShellAction);
150 //Initialization
151 HKEY key = NULL;
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)
157 wchar_t name[256];
159 for(DWORD i = 0; true; i++)
161 DWORD size = 256;
162 if(RegEnumKeyEx(key, i, name, &size, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
164 fileTypes << QString::fromUtf16(reinterpret_cast<const unsigned short*>(name));
165 continue;
167 break;
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)));
180 //Shell notification
181 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
184 ////////////////////////////////////////////////////////////
185 // Private Functions
186 ////////////////////////////////////////////////////////////
188 QStringList *ShellIntegration::detectTypes(const QString &lamexpFileType, const QString &lamexpShellAction)
190 HKEY key = NULL;
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)
204 int lastIndex = 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)
219 wchar_t data[256];
220 DWORD dataLen = 256 * sizeof(wchar_t);
221 DWORD type = NULL;
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);
234 RegCloseKey(key);
236 else
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);
241 RegCloseKey(key);
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)
247 wchar_t data[256];
248 DWORD dataLen = 256 * sizeof(wchar_t);
249 DWORD type = NULL;
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);
262 RegCloseKey(key);
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)
267 wchar_t name[256];
268 for(DWORD i = 0; true; i++)
270 DWORD size = 256;
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);
278 continue;
280 break;
282 RegCloseKey(key);
286 return nativeTypes;