1
/* smplayer, GUI front-end for mplayer.
2
Copyright (C) 2007 Ricardo Villalba <rvm@escomposlinux.org>
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
Handles file associations in Windows
21
Author: Florin Braghis (florin@libertv.ro)
24
#include "winfileassoc.h"
27
WinFileAssoc::WinFileAssoc( const QString& ClassId )
32
bool WinFileAssoc::CreateFileAssociation(const QString& fileExtension)
34
//Registry keys modified:
35
//HKEY_CLASSES_ROOT\.extension
36
//HKEY_CLASSES_ROOT\smplayer.exe
37
//Shell 'Open With...' entry:
38
//HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.avi
40
QSettings RegCR ("HKEY_CLASSES_ROOT", QSettings::NativeFormat);
41
QSettings RegCU ("HKEY_CURRENT_USER", QSettings::NativeFormat);
43
if (!RegCR.isWritable() || RegCR.status() != QSettings::NoError)
46
if (!RegCU.isWritable() || RegCU.status() != QSettings::NoError)
49
QString ExtKeyName = QString("SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/FileExts/.%1").arg(fileExtension);
50
QString ClassesKeyName = m_ClassId;
52
QString BackupKeyName = ClassesKeyName + "/" + fileExtension;
54
//Save last ClassId from the extension class
55
QString KeyVal = RegCR.value("." + fileExtension + "/.").toString();
56
if (KeyVal != m_ClassId)
57
RegCR.setValue("." + fileExtension + "/MPlayer_Backup", KeyVal);
59
//Save last ProgId and Application values from the Exts key
60
KeyVal = RegCU.value(ExtKeyName + "/Progid").toString();
61
if (KeyVal != m_ClassId)
62
RegCU.setValue(ExtKeyName + "/MPlayer_Backup_ProgId", KeyVal);
64
KeyVal = RegCU.value(ExtKeyName + "/Application").toString();
65
if (KeyVal != m_ClassId)
66
RegCU.setValue(ExtKeyName + "/MPlayer_Backup_Application", KeyVal);
68
//Create the associations
69
RegCR.setValue("." + fileExtension + "/.", m_ClassId); //Extension class
70
RegCU.setValue(ExtKeyName + "/Progid", m_ClassId); //Explorer FileExt association
74
bool WinFileAssoc::RestoreFileAssociation(const QString& fileExtension)
76
QSettings RegCR ("HKEY_CLASSES_ROOT", QSettings::NativeFormat);
77
QSettings RegCU ("HKEY_CURRENT_USER", QSettings::NativeFormat);
79
if (!RegCR.isWritable() || RegCR.status() != QSettings::NoError)
82
if (!RegCU.isWritable() || RegCU.status() != QSettings::NoError)
86
QString ClassesKeyName = m_ClassId;
87
QString ExtKeyName = QString("SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/FileExts/.%1").arg(fileExtension);
89
QString BackupKeyName = ClassesKeyName + "/" + fileExtension;
90
QString OldProgId = RegCR.value("." + fileExtension + "/MPlayer_Backup").toString();
91
QString OldApp = RegCU.value(ExtKeyName + "/MPlayer_Backup_Application").toString();
92
QString OldClassId = RegCU.value(ExtKeyName + "/MPlayer_Backup_ProgId").toString();
94
//Restore old association
95
if (!OldProgId.isEmpty() && OldProgId != m_ClassId)
97
RegCU.setValue(ExtKeyName + "/Progid", OldProgId);
101
RegCU.remove(ExtKeyName + "/Progid");
104
if (!OldApp.isEmpty() && OldApp != m_ClassId)
106
RegCU.setValue(ExtKeyName + "/Application", OldApp);
110
RegCU.remove(ExtKeyName + "/Application");
113
if (!OldClassId.isEmpty() && OldClassId != m_ClassId)
115
RegCR.setValue("." + fileExtension + "/.", OldClassId);
119
//No old association with this extension, it's better to remove it entirely
120
RegCR.remove("." + fileExtension + "/.");
124
RegCR.remove(BackupKeyName + "/OldProgId");
125
RegCR.remove(BackupKeyName + "/OldApplication");
126
RegCR.remove(BackupKeyName + "/OldClassId");
127
RegCR.remove(BackupKeyName);
131
bool WinFileAssoc::CreateClassId(const QString& executablePath, const QString& friendlyName)
133
QSettings RegCR ("HKEY_CLASSES_ROOT", QSettings::NativeFormat);
134
if (!RegCR.isWritable() || RegCR.status() != QSettings::NoError)
137
QString appPath = executablePath;
138
appPath.replace('/', '\\'); //Explorer gives 'Access Denied' if we write the path with forward slashes to the registry
140
//Add our ProgId to the HKCR classes
141
RegCR.setValue(m_ClassId + "/shell/open/FriendlyAppName", friendlyName);
142
RegCR.setValue(m_ClassId + "/shell/open/command/.", QString("\"%1\" \"%2\"").arg(appPath, "%1"));
143
RegCR.setValue(m_ClassId + "/DefaultIcon/.", QString("\"%1\",0").arg(appPath));
144
//Add "Enqueue" command
145
RegCR.setValue(m_ClassId + "/shell/enqueue/.", QObject::tr("Enqueue in SMPlayer"));
146
RegCR.setValue(m_ClassId + "/shell/enqueue/command/.", QString("\"%1\" -add-to-playlist \"%2\"").arg(appPath, "%1"));
150
//Called when no associations exist
151
bool WinFileAssoc::RemoveClassId()
153
QSettings RegCR ("HKEY_CLASSES_ROOT", QSettings::NativeFormat);
155
if (!RegCR.isWritable() || RegCR.status() != QSettings::NoError)
158
RegCR.remove(m_ClassId + "/shell/open/FriendlyAppName");
159
RegCR.remove(m_ClassId + "/shell/open/command/.");
160
RegCR.remove(m_ClassId + "/shell/enqueue/command/.");
161
RegCR.remove(m_ClassId + "/shell/enqueue/.");
162
RegCR.remove(m_ClassId + "/DefaultIcon/.");
163
RegCR.remove(m_ClassId);
1
/* smplayer, GUI front-end for mplayer.
2
Copyright (C) 2006-2008 Ricardo Villalba <rvm@escomposlinux.org>
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
Handles file associations in Windows Vista/XP/2000/NT/ME/98/95.
22
We assume that the code is run without administrator privileges, so the associations are done for current user only.
23
System-wide associations require writing to HKEY_CLASSES_ROOT and we don't want to get our hands dirty with that.
24
Each user on the computer can configure his own set of file associations for SMPlayer, which is extremely cool.
26
Optionally, during uninstall, it would be a good idea to call RestoreFileAssociations for all media types so
27
that we can clean up the registry and restore the old associations for current user.
30
The code can only register the app as default program for selected extensions and check if it is the default.
31
It cannot restore 'old' default application, since this doesn't seem to be possible with the current Vista API.
33
Tested on: Win98, Win2000, WinXP, Vista.
34
NOT tested on: Win95, ME and NT 4.0 (it should work on 95, ME; Not sure about NT 4.0).
36
Author: Florin Braghis (florin@libertv.ro)
39
#include "winfileassoc.h"
41
#include <QApplication>
45
Note by RVM: Added some #ifdef Q_OS_WIN to allow the file to compile under linux.
46
It should compile the code for Windows XP.
47
The registry entries are saved on a file named HKEY_CURRENT_USER.
50
WinFileAssoc::WinFileAssoc( const QString ClassId, const QString AppName )
54
m_ClassId2 = QFileInfo(QApplication::applicationFilePath()).fileName();
57
//Associates all extensions in the fileExtensions list with current app.
58
//Returns number of extensions processed successfully.
59
int WinFileAssoc::CreateFileAssociations(const QStringList& fileExtensions)
62
if (QSysInfo::WindowsVersion == QSysInfo::WV_VISTA)
64
return VistaSetAppsAsDefault(fileExtensions);
68
QSettings RegCR ("HKEY_CLASSES_ROOT", QSettings::NativeFormat); //Read only on NT+
69
QSettings RegCU ("HKEY_CURRENT_USER", QSettings::NativeFormat);
71
if (!RegCU.isWritable() || RegCU.status() != QSettings::NoError)
74
if (RegCR.status() != QSettings::NoError)
78
if (QSysInfo::WindowsVersion < QSysInfo::WV_NT && !RegCR.isWritable()) //Win98
82
//Check if classId exists in the registry
83
if (!RegCR.contains(m_ClassId) && !RegCU.contains("Software/Classes/" + m_ClassId))
85
//If doesn't exist (user didn't run the setup program), try to create the ClassId for current user.
86
if (!CreateClassId(QApplication::applicationFilePath(), "SMPlayer Media Player"))
91
foreach(const QString& fileExtension, fileExtensions)
93
QString ExtKeyName = QString("Software/Microsoft/Windows/CurrentVersion/Explorer/FileExts/.%1").arg(fileExtension);
94
QString ClassesKeyName = m_ClassId;
96
QString BackupKeyName = ClassesKeyName + "/" + fileExtension;
97
QString CUKeyName = "Software/Classes/." + fileExtension;
99
//Save current ClassId for current user
100
QString KeyVal = RegCU.value(CUKeyName + "/.").toString();
102
if (KeyVal.length() == 0 || KeyVal == m_ClassId)
104
//No registered app for this extension for current user.
105
//Check the system-wide (HKEY_CLASSES_ROOT) ClassId for this extension
106
KeyVal = RegCR.value("." + fileExtension + "/.").toString();
109
if (KeyVal != m_ClassId)
110
RegCU.setValue(CUKeyName + "/MPlayer_Backup", KeyVal);
112
//Save last ProgId and Application values from the Exts key
113
KeyVal = RegCU.value(ExtKeyName + "/Progid").toString();
115
if (KeyVal != m_ClassId && KeyVal != m_ClassId2)
116
RegCU.setValue(ExtKeyName + "/MPlayer_Backup_ProgId", KeyVal);
118
KeyVal = RegCU.value(ExtKeyName + "/Application").toString();
119
if (KeyVal != m_ClassId || KeyVal != m_ClassId2)
120
RegCU.setValue(ExtKeyName + "/MPlayer_Backup_Application", KeyVal);
122
//Create the associations
124
if (QSysInfo::WindowsVersion >= QSysInfo::WV_NT)
127
RegCU.setValue(CUKeyName + "/.", m_ClassId); //Extension class
128
RegCU.setValue(ExtKeyName + "/Progid", m_ClassId); //Explorer FileExt association
134
//Windows ME/98/95 support
135
RegCR.setValue("." + fileExtension + "/.", m_ClassId);
139
if (RegCU.status() == QSettings::NoError && RegCR.status() == QSettings::NoError)
146
//Checks if extensions in extensionsToCheck are registered with this application. Returns a list of registered extensions.
147
//Returns false if there was an error accessing the registry.
148
//Returns true and 0 elements in registeredExtensions if no extension is associated with current app.
149
bool WinFileAssoc::GetRegisteredExtensions( const QStringList& extensionsToCheck, QStringList& registeredExtensions)
151
registeredExtensions.clear();
154
if (QSysInfo::WindowsVersion == QSysInfo::WV_VISTA)
156
return VistaGetDefaultApps(extensionsToCheck, registeredExtensions);
160
QSettings RegCR ("HKEY_CLASSES_ROOT", QSettings::NativeFormat);
161
QSettings RegCU ("HKEY_CURRENT_USER", QSettings::NativeFormat);
163
if (RegCR.status() != QSettings::NoError)
166
if (RegCU.status() != QSettings::NoError)
169
foreach(const QString& fileExtension, extensionsToCheck)
171
bool bRegistered = false;
172
//Check the explorer extension (Always use this program to open this kind of file...)
175
if (QSysInfo::WindowsVersion >= QSysInfo::WV_NT)
178
QString FileExtsKey = QString("Software/Microsoft/Windows/CurrentVersion/Explorer/FileExts/.%1").arg(fileExtension);
179
QString CurClassId = RegCU.value(FileExtsKey + "/Progid").toString();
180
QString CurAppId = RegCU.value(FileExtsKey + "/Application").toString();
182
if (CurClassId.size()) //Registered with Open With... / ProgId ?
184
bRegistered = (CurClassId == m_ClassId) || (0 == CurClassId.compare(m_ClassId2, Qt::CaseInsensitive));
189
//If user uses Open With..., explorer creates it's own ClassId under Application, usually "smplayer.exe"
190
bRegistered = (CurAppId == m_ClassId) || (0 == CurAppId.compare(m_ClassId2, Qt::CaseInsensitive));
194
//No classId means that no associations exists in Default Programs or Explorer
195
//Check the default per-user association
196
bRegistered = RegCU.value("Software/Classes/." + fileExtension + "/.").toString() == m_ClassId;
200
//Finally, check the system-wide association
202
bRegistered = RegCR.value("." + fileExtension + "/.").toString() == m_ClassId;
206
registeredExtensions.append(fileExtension);
212
//Restores file associations to old defaults (if any) for all extensions in the fileExtensions list.
213
//Cleans up our backup keys from the registry.
214
//Returns number of extensions successfully processed (error if fileExtensions.count() != return value && count > 0).
215
int WinFileAssoc::RestoreFileAssociations(const QStringList& fileExtensions)
218
if (QSysInfo::WindowsVersion == QSysInfo::WV_VISTA)
219
return 0; //Not supported by the API
222
QSettings RegCR ("HKEY_CLASSES_ROOT", QSettings::NativeFormat);
223
QSettings RegCU ("HKEY_CURRENT_USER", QSettings::NativeFormat);
225
if (!RegCU.isWritable() || RegCU.status() != QSettings::NoError)
228
if (RegCR.status() != QSettings::NoError)
232
if (QSysInfo::WindowsVersion < QSysInfo::WV_NT && !RegCR.isWritable()) //Win98
237
foreach(const QString& fileExtension, fileExtensions)
239
QString ExtKeyName = QString("Software/Microsoft/Windows/CurrentVersion/Explorer/FileExts/.%1").arg(fileExtension);
240
QString OldProgId = RegCU.value(ExtKeyName + "/MPlayer_Backup_ProgId").toString();
241
QString OldApp = RegCU.value(ExtKeyName + "/MPlayer_Backup_Application").toString();
242
QString OldClassId = RegCU.value("Software/Classes/." + fileExtension + "/MPlayer_Backup").toString();
244
//Restore old explorer ProgId
245
if (!OldProgId.isEmpty() && OldProgId != m_ClassId)
246
RegCU.setValue(ExtKeyName + "/Progid", OldProgId);
249
QString CurProgId = RegCU.value(ExtKeyName + "/Progid").toString();
250
if ((CurProgId == m_ClassId) || (0 == CurProgId.compare(m_ClassId2, Qt::CaseInsensitive))) //Only remove if we own it
251
RegCU.remove(ExtKeyName + "/Progid");
254
//Restore old explorer Application
255
if (!OldApp.isEmpty() && OldApp != m_ClassId)
256
RegCU.setValue(ExtKeyName + "/Application", OldApp);
259
QString CurApp = RegCU.value(ExtKeyName + "/Application").toString();
260
if ((CurApp == m_ClassId) || (0 == CurApp.compare(m_ClassId2, Qt::CaseInsensitive))) //Only remove if we own it
261
RegCU.remove(ExtKeyName + "/Application");
265
if (QSysInfo::WindowsVersion >= QSysInfo::WV_NT)
268
//Restore old association for current user
269
if (!OldClassId.isEmpty() && OldClassId != m_ClassId)
270
RegCU.setValue("Software/Classes/." + fileExtension + "/.", OldClassId);
273
if (RegCU.value("Software/Classes/." + fileExtension + "/.").toString() == m_ClassId) //Only remove if we own it
274
RegCU.remove("Software/Classes/." + fileExtension);
280
//Windows 98 ==> Write to HKCR
281
if (!OldClassId.isEmpty() && OldClassId != m_ClassId)
282
RegCR.setValue("." + fileExtension + "/.", OldClassId);
285
if (RegCR.value("." + fileExtension + "/.").toString() == m_ClassId)
286
RegCR.remove("." + fileExtension);
291
//CurrentUserClasses/.ext/MPlayerBackup
292
//Explorer: Backup_Application and Backup_ProgId
293
RegCU.remove("Software/Classes/." + fileExtension + "/MPlayer_Backup");
294
RegCU.remove(ExtKeyName + "/MPlayer_Backup_Application");
295
RegCU.remove(ExtKeyName + "/MPlayer_Backup_ProgId");
300
//Creates a ClassId for current application.
301
//Note: It's better to create the classId from the installation program.
302
bool WinFileAssoc::CreateClassId(const QString& executablePath, const QString& friendlyName)
308
if (QSysInfo::WindowsVersion >= QSysInfo::WV_NT)
311
classId = "Software/Classes/" + m_ClassId;
312
RootKeyName = "HKEY_CURRENT_USER";
318
RootKeyName = "HKEY_CLASSES_ROOT"; //Windows 95/98/ME
322
QSettings Reg (RootKeyName, QSettings::NativeFormat);
323
if (!Reg.isWritable() || Reg.status() != QSettings::NoError)
326
QString appPath = executablePath;
327
appPath.replace('/', '\\'); //Explorer gives 'Access Denied' if we write the path with forward slashes to the registry
329
//Add our ProgId to the HKCR classes
330
Reg.setValue(classId + "/shell/open/FriendlyAppName", friendlyName);
331
Reg.setValue(classId + "/shell/open/command/.", QString("\"%1\" \"%2\"").arg(appPath, "%1"));
332
Reg.setValue(classId + "/DefaultIcon/.", QString("\"%1\",1").arg(appPath));
333
//Add "Enqueue" command
334
Reg.setValue(classId + "/shell/enqueue/.", QObject::tr("Enqueue in SMPlayer"));
335
Reg.setValue(classId + "/shell/enqueue/command/.", QString("\"%1\" -add-to-playlist \"%2\"").arg(appPath, "%1"));
338
//Remove ClassId from the registry.
339
//Called when no associations exist. Note: It's better to do this in the Setup program.
340
bool WinFileAssoc::RemoveClassId()
346
if (QSysInfo::WindowsVersion >= QSysInfo::WV_NT)
349
classId = "Software/Classes/" + m_ClassId;
350
RootKeyName = "HKEY_CURRENT_USER";
356
RootKeyName = "HKEY_CLASSES_ROOT"; //Windows 95/98/ME
360
QSettings RegCU (RootKeyName, QSettings::NativeFormat);
362
if (!RegCU.isWritable() || RegCU.status() != QSettings::NoError)
365
RegCU.remove(classId);
369
//Windows Vista specific implementation
370
//Add libole32.a library if compiling with mingw.
371
//In smplayer.pro, under win32{ :
376
#if !defined(IApplicationAssociationRegistration)
378
typedef enum tagASSOCIATIONLEVEL
385
typedef enum tagASSOCIATIONTYPE
393
MIDL_INTERFACE("4e530b0a-e611-4c77-a3ac-9031d022281b")
394
IApplicationAssociationRegistration : public IUnknown
397
virtual HRESULT STDMETHODCALLTYPE QueryCurrentDefault(LPCWSTR pszQuery,
398
ASSOCIATIONTYPE atQueryType,
399
ASSOCIATIONLEVEL alQueryLevel,
400
LPWSTR *ppszAssociation) = 0;
401
virtual HRESULT STDMETHODCALLTYPE QueryAppIsDefault(LPCWSTR pszQuery,
402
ASSOCIATIONTYPE atQueryType,
403
ASSOCIATIONLEVEL alQueryLevel,
404
LPCWSTR pszAppRegistryName,
405
BOOL *pfDefault) = 0;
406
virtual HRESULT STDMETHODCALLTYPE QueryAppIsDefaultAll(ASSOCIATIONLEVEL alQueryLevel,
407
LPCWSTR pszAppRegistryName,
408
BOOL *pfDefault) = 0;
409
virtual HRESULT STDMETHODCALLTYPE SetAppAsDefault(LPCWSTR pszAppRegistryName,
411
ASSOCIATIONTYPE atSetType) = 0;
412
virtual HRESULT STDMETHODCALLTYPE SetAppAsDefaultAll(LPCWSTR pszAppRegistryName) = 0;
413
virtual HRESULT STDMETHODCALLTYPE ClearUserAssociations( void) = 0;
417
static const CLSID CLSID_ApplicationAssociationReg = {0x591209C7,0x767B,0x42B2,{0x9F,0xBA,0x44,0xEE,0x46,0x15,0xF2,0xC7}};
418
static const IID IID_IApplicationAssociationReg = {0x4e530b0a,0xe611,0x4c77,{0xa3,0xac,0x90,0x31,0xd0,0x22,0x28,0x1b}};
420
int WinFileAssoc::VistaSetAppsAsDefault(const QStringList& fileExtensions)
422
IApplicationAssociationRegistration* pAAR;
423
HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationReg,
424
NULL, CLSCTX_INPROC, IID_IApplicationAssociationReg, (void**)&pAAR);
427
if (SUCCEEDED(hr) && (pAAR != NULL))
429
foreach(const QString& fileExtension, fileExtensions)
431
hr = pAAR->SetAppAsDefault((const WCHAR*)m_AppName.utf16(),
432
(const WCHAR*)QString("." + fileExtension).utf16(),
443
bool WinFileAssoc::VistaGetDefaultApps(const QStringList &extensions, QStringList& registeredExt)
445
IApplicationAssociationRegistration* pAAR;
447
HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationReg,
448
NULL, CLSCTX_INPROC, IID_IApplicationAssociationReg, (void**)&pAAR);
450
if (SUCCEEDED(hr) && (pAAR != NULL))
452
foreach(const QString& fileExtension, extensions)
454
BOOL bIsDefault = FALSE;
455
hr = pAAR->QueryAppIsDefault((const WCHAR*)QString("." + fileExtension).utf16(),
458
(const WCHAR*)m_AppName.utf16(),
460
if (SUCCEEDED(hr) && bIsDefault)
462
registeredExt.append(fileExtension);
472
bool WinFileAssoc::VistaGetDefaultApps(const QStringList &extensions, QStringList& registeredExt)
477
int WinFileAssoc::VistaSetAppsAsDefault(const QStringList& extensions)