| [90] | 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| [186] | 2 | Copyright (C) 2006-2017 Ricardo Villalba <[email protected]>
|
|---|
| [90] | 3 |
|
|---|
| 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.
|
|---|
| 8 |
|
|---|
| 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.
|
|---|
| 13 |
|
|---|
| 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
|
|---|
| 17 |
|
|---|
| 18 | Winfileassoc.cpp
|
|---|
| 19 |
|
|---|
| [133] | 20 | Handles file associations in Windows 7/Vista/XP/2000.
|
|---|
| [90] | 21 | We assume that the code is run without administrator privileges, so the associations are done for current user only.
|
|---|
| 22 | System-wide associations require writing to HKEY_CLASSES_ROOT and we don't want to get our hands dirty with that.
|
|---|
| 23 | Each user on the computer can configure his own set of file associations for SMPlayer, which is extremely cool.
|
|---|
| 24 |
|
|---|
| 25 | Optionally, during uninstall, it would be a good idea to call RestoreFileAssociations for all media types so
|
|---|
| [133] | 26 | that we can clean up the registry and restore the old associations for current user.
|
|---|
| [90] | 27 |
|
|---|
| 28 | Vista:
|
|---|
| [133] | 29 | The code can only register the app as default program for selected extensions and check if it is the default.
|
|---|
| [90] | 30 | It cannot restore 'old' default application, since this doesn't seem to be possible with the current Vista API.
|
|---|
| 31 |
|
|---|
| [133] | 32 | Add libole32.a library if compiling with MinGW. In smplayer.pro, under 'win32 {': LIBS += libole32
|
|---|
| [90] | 33 |
|
|---|
| [133] | 34 | Tested on: WinXP, Vista, Win7.
|
|---|
| 35 |
|
|---|
| [90] | 36 | Author: Florin Braghis ([email protected])
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include "winfileassoc.h"
|
|---|
| 40 | #include <QSettings>
|
|---|
| 41 | #include <QApplication>
|
|---|
| 42 | #include <QFileInfo>
|
|---|
| [133] | 43 | #include <windows.h>
|
|---|
| [90] | 44 |
|
|---|
| [133] | 45 | WinFileAssoc::WinFileAssoc(const QString ClassId, const QString AppName)
|
|---|
| [90] | 46 | {
|
|---|
| [133] | 47 | m_ClassId = ClassId;
|
|---|
| 48 | m_AppName = AppName;
|
|---|
| 49 | m_ClassId2 = QFileInfo(QApplication::applicationFilePath()).fileName();
|
|---|
| [90] | 50 | }
|
|---|
| 51 |
|
|---|
| [133] | 52 | // Associates all extensions in the fileExtensions list with current app.
|
|---|
| 53 | // Returns number of extensions processed successfully.
|
|---|
| 54 | int WinFileAssoc::CreateFileAssociations(const QStringList &fileExtensions)
|
|---|
| [90] | 55 | {
|
|---|
| [133] | 56 | if (QSysInfo::WindowsVersion >= QSysInfo::WV_VISTA) {
|
|---|
| 57 | return VistaSetAppsAsDefault(fileExtensions);
|
|---|
| 58 | }
|
|---|
| [90] | 59 |
|
|---|
| [133] | 60 | QSettings RegCR("HKEY_CLASSES_ROOT", QSettings::NativeFormat); //Read only on NT+
|
|---|
| 61 | QSettings RegCU("HKEY_CURRENT_USER", QSettings::NativeFormat);
|
|---|
| [90] | 62 |
|
|---|
| [133] | 63 | if (!RegCU.isWritable() || RegCU.status() != QSettings::NoError)
|
|---|
| 64 | return 0;
|
|---|
| [90] | 65 |
|
|---|
| [133] | 66 | if (RegCR.status() != QSettings::NoError)
|
|---|
| 67 | return 0;
|
|---|
| [90] | 68 |
|
|---|
| [133] | 69 | // Check if classId exists in the registry
|
|---|
| 70 | if (!RegCR.contains(m_ClassId) && !RegCU.contains("Software/Classes/" + m_ClassId)) {
|
|---|
| 71 | // If doesn't exist (user didn't run the setup program), try to create the ClassId for current user.
|
|---|
| 72 | if (!CreateClassId(QApplication::applicationFilePath(), "SMPlayer Media Player"))
|
|---|
| 73 | return 0;
|
|---|
| 74 | }
|
|---|
| [90] | 75 |
|
|---|
| [133] | 76 | int count = 0;
|
|---|
| 77 | foreach(const QString & fileExtension, fileExtensions) {
|
|---|
| 78 | QString ExtKeyName = QString("Software/Microsoft/Windows/CurrentVersion/Explorer/FileExts/.%1").arg(fileExtension);
|
|---|
| 79 | QString ClassesKeyName = m_ClassId;
|
|---|
| [90] | 80 |
|
|---|
| [133] | 81 | QString BackupKeyName = ClassesKeyName + "/" + fileExtension;
|
|---|
| 82 | QString CUKeyName = "Software/Classes/." + fileExtension;
|
|---|
| [90] | 83 |
|
|---|
| [133] | 84 | // Save current ClassId for current user
|
|---|
| 85 | QString KeyVal = RegCU.value(CUKeyName + "/.").toString();
|
|---|
| [90] | 86 |
|
|---|
| [133] | 87 | if (KeyVal.length() == 0 || KeyVal == m_ClassId) {
|
|---|
| 88 | // No registered app for this extension for current user.
|
|---|
| 89 | // Check the system-wide (HKEY_CLASSES_ROOT) ClassId for this extension
|
|---|
| 90 | KeyVal = RegCR.value("." + fileExtension + "/.").toString();
|
|---|
| 91 | }
|
|---|
| [90] | 92 |
|
|---|
| [133] | 93 | if (KeyVal != m_ClassId)
|
|---|
| 94 | RegCU.setValue(CUKeyName + "/MPlayer_Backup", KeyVal);
|
|---|
| [90] | 95 |
|
|---|
| [133] | 96 | // Save last ProgId and Application values from the Exts key
|
|---|
| 97 | KeyVal = RegCU.value(ExtKeyName + "/Progid").toString();
|
|---|
| [90] | 98 |
|
|---|
| [133] | 99 | if (KeyVal != m_ClassId && KeyVal != m_ClassId2)
|
|---|
| 100 | RegCU.setValue(ExtKeyName + "/MPlayer_Backup_ProgId", KeyVal);
|
|---|
| [90] | 101 |
|
|---|
| [133] | 102 | KeyVal = RegCU.value(ExtKeyName + "/Application").toString();
|
|---|
| [90] | 103 |
|
|---|
| [133] | 104 | if (KeyVal != m_ClassId || KeyVal != m_ClassId2)
|
|---|
| 105 | RegCU.setValue(ExtKeyName + "/MPlayer_Backup_Application", KeyVal);
|
|---|
| [90] | 106 |
|
|---|
| [133] | 107 | // Create the associations
|
|---|
| 108 | RegCU.setValue(CUKeyName + "/.", m_ClassId); // Extension class
|
|---|
| 109 | RegCU.setValue(ExtKeyName + "/Progid", m_ClassId); // Explorer FileExt association
|
|---|
| [90] | 110 |
|
|---|
| [133] | 111 | if (RegCU.status() == QSettings::NoError && RegCR.status() == QSettings::NoError)
|
|---|
| 112 | count++;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | return count;
|
|---|
| [90] | 116 | }
|
|---|
| 117 |
|
|---|
| [133] | 118 | // Checks if extensions in extensionsToCheck are registered with this application. Returns a list of registered extensions.
|
|---|
| 119 | // Returns false if there was an error accessing the registry.
|
|---|
| 120 | // Returns true and 0 elements in registeredExtensions if no extension is associated with current app.
|
|---|
| 121 | bool WinFileAssoc::GetRegisteredExtensions(const QStringList &extensionsToCheck, QStringList ®isteredExtensions)
|
|---|
| [90] | 122 | {
|
|---|
| [133] | 123 | registeredExtensions.clear();
|
|---|
| [90] | 124 |
|
|---|
| [133] | 125 | if (QSysInfo::WindowsVersion >= QSysInfo::WV_VISTA) {
|
|---|
| 126 | return VistaGetDefaultApps(extensionsToCheck, registeredExtensions);
|
|---|
| 127 | }
|
|---|
| [90] | 128 |
|
|---|
| [133] | 129 | QSettings RegCR("HKEY_CLASSES_ROOT", QSettings::NativeFormat);
|
|---|
| 130 | QSettings RegCU("HKEY_CURRENT_USER", QSettings::NativeFormat);
|
|---|
| [90] | 131 |
|
|---|
| [133] | 132 | if (RegCR.status() != QSettings::NoError)
|
|---|
| 133 | return false;
|
|---|
| [90] | 134 |
|
|---|
| [133] | 135 | if (RegCU.status() != QSettings::NoError)
|
|---|
| 136 | return false;
|
|---|
| [90] | 137 |
|
|---|
| [133] | 138 | foreach(const QString & fileExtension, extensionsToCheck) {
|
|---|
| 139 | bool bRegistered = false;
|
|---|
| 140 | // Check the Explorer extension (Always use this program to open this kind of file...)
|
|---|
| [90] | 141 |
|
|---|
| [133] | 142 | QString FileExtsKey = QString("Software/Microsoft/Windows/CurrentVersion/Explorer/FileExts/.%1").arg(fileExtension);
|
|---|
| 143 | QString CurClassId = RegCU.value(FileExtsKey + "/Progid").toString();
|
|---|
| 144 | QString CurAppId = RegCU.value(FileExtsKey + "/Application").toString();
|
|---|
| [90] | 145 |
|
|---|
| [133] | 146 | if (CurClassId.size()) { // Registered with Open With... / ProgId ?
|
|---|
| 147 | bRegistered = (CurClassId == m_ClassId) || (0 == CurClassId.compare(m_ClassId2, Qt::CaseInsensitive));
|
|---|
| 148 | } else if (CurAppId.size()) {
|
|---|
| 149 | // If user uses Open With..., explorer creates it's own ClassId under Application, usually "smplayer.exe"
|
|---|
| 150 | bRegistered = (CurAppId == m_ClassId) || (0 == CurAppId.compare(m_ClassId2, Qt::CaseInsensitive));
|
|---|
| 151 | } else {
|
|---|
| 152 | // No classId means that no associations exists in Default Programs or Explorer
|
|---|
| 153 | // Check the default per-user association
|
|---|
| 154 | bRegistered = RegCU.value("Software/Classes/." + fileExtension + "/.").toString() == m_ClassId;
|
|---|
| 155 | }
|
|---|
| [90] | 156 |
|
|---|
| [133] | 157 | // Finally, check the system-wide association
|
|---|
| 158 | if (!bRegistered)
|
|---|
| 159 | bRegistered = RegCR.value("." + fileExtension + "/.").toString() == m_ClassId;
|
|---|
| [90] | 160 |
|
|---|
| [133] | 161 | if (bRegistered)
|
|---|
| 162 | registeredExtensions.append(fileExtension);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | return true;
|
|---|
| [90] | 166 | }
|
|---|
| 167 |
|
|---|
| [133] | 168 | // Restores file associations to old defaults (if any) for all extensions in the fileExtensions list.
|
|---|
| 169 | // Cleans up our backup keys from the registry.
|
|---|
| 170 | // Returns number of extensions successfully processed (error if fileExtensions.count() != return value && count > 0).
|
|---|
| 171 | int WinFileAssoc::RestoreFileAssociations(const QStringList &fileExtensions)
|
|---|
| [90] | 172 | {
|
|---|
| [133] | 173 | if (QSysInfo::WindowsVersion >= QSysInfo::WV_VISTA)
|
|---|
| 174 | return 0; // Not supported by the API
|
|---|
| [90] | 175 |
|
|---|
| [133] | 176 | QSettings RegCR("HKEY_CLASSES_ROOT", QSettings::NativeFormat);
|
|---|
| 177 | QSettings RegCU("HKEY_CURRENT_USER", QSettings::NativeFormat);
|
|---|
| [90] | 178 |
|
|---|
| [133] | 179 | if (!RegCU.isWritable() || RegCU.status() != QSettings::NoError)
|
|---|
| 180 | return 0;
|
|---|
| [90] | 181 |
|
|---|
| [133] | 182 | if (RegCR.status() != QSettings::NoError)
|
|---|
| 183 | return 0;
|
|---|
| [90] | 184 |
|
|---|
| [133] | 185 | int count = 0;
|
|---|
| 186 | foreach(const QString & fileExtension, fileExtensions) {
|
|---|
| 187 | QString ExtKeyName = QString("Software/Microsoft/Windows/CurrentVersion/Explorer/FileExts/.%1").arg(fileExtension);
|
|---|
| 188 | QString OldProgId = RegCU.value(ExtKeyName + "/MPlayer_Backup_ProgId").toString();
|
|---|
| 189 | QString OldApp = RegCU.value(ExtKeyName + "/MPlayer_Backup_Application").toString();
|
|---|
| 190 | QString OldClassId = RegCU.value("Software/Classes/." + fileExtension + "/MPlayer_Backup").toString();
|
|---|
| [90] | 191 |
|
|---|
| [133] | 192 | // Restore old explorer ProgId
|
|---|
| 193 | if (!OldProgId.isEmpty() && OldProgId != m_ClassId)
|
|---|
| 194 | RegCU.setValue(ExtKeyName + "/Progid", OldProgId);
|
|---|
| 195 | else {
|
|---|
| 196 | QString CurProgId = RegCU.value(ExtKeyName + "/Progid").toString();
|
|---|
| [90] | 197 |
|
|---|
| [133] | 198 | if ((CurProgId == m_ClassId) || (0 == CurProgId.compare(m_ClassId2, Qt::CaseInsensitive))) //Only remove if we own it
|
|---|
| 199 | RegCU.remove(ExtKeyName + "/Progid");
|
|---|
| 200 | }
|
|---|
| [90] | 201 |
|
|---|
| [133] | 202 | // Restore old explorer Application
|
|---|
| 203 | if (!OldApp.isEmpty() && OldApp != m_ClassId)
|
|---|
| 204 | RegCU.setValue(ExtKeyName + "/Application", OldApp);
|
|---|
| 205 | else {
|
|---|
| 206 | QString CurApp = RegCU.value(ExtKeyName + "/Application").toString();
|
|---|
| [90] | 207 |
|
|---|
| [133] | 208 | if ((CurApp == m_ClassId) || (0 == CurApp.compare(m_ClassId2, Qt::CaseInsensitive))) //Only remove if we own it
|
|---|
| 209 | RegCU.remove(ExtKeyName + "/Application");
|
|---|
| 210 | }
|
|---|
| [90] | 211 |
|
|---|
| [133] | 212 | // Restore old association for current user
|
|---|
| 213 | if (!OldClassId.isEmpty() && OldClassId != m_ClassId)
|
|---|
| 214 | RegCU.setValue("Software/Classes/." + fileExtension + "/.", OldClassId);
|
|---|
| 215 | else {
|
|---|
| 216 | if (RegCU.value("Software/Classes/." + fileExtension + "/.").toString() == m_ClassId) //Only remove if we own it
|
|---|
| 217 | RegCU.remove("Software/Classes/." + fileExtension);
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | // Remove our keys:
|
|---|
| 221 | // CurrentUserClasses/.ext/MPlayerBackup
|
|---|
| 222 | // Explorer: Backup_Application and Backup_ProgId
|
|---|
| 223 | RegCU.remove("Software/Classes/." + fileExtension + "/MPlayer_Backup");
|
|---|
| 224 | RegCU.remove(ExtKeyName + "/MPlayer_Backup_Application");
|
|---|
| 225 | RegCU.remove(ExtKeyName + "/MPlayer_Backup_ProgId");
|
|---|
| 226 | }
|
|---|
| 227 | return count;
|
|---|
| [90] | 228 | }
|
|---|
| 229 |
|
|---|
| [133] | 230 | // Creates a ClassId for current application.
|
|---|
| 231 | // Note: It's better to create the classId from the installation program.
|
|---|
| 232 | bool WinFileAssoc::CreateClassId(const QString &executablePath, const QString &friendlyName)
|
|---|
| [90] | 233 | {
|
|---|
| [133] | 234 | QString RootKeyName;
|
|---|
| 235 | QString classId;
|
|---|
| [90] | 236 |
|
|---|
| [133] | 237 | classId = "Software/Classes/" + m_ClassId;
|
|---|
| 238 | RootKeyName = "HKEY_CURRENT_USER";
|
|---|
| [90] | 239 |
|
|---|
| [133] | 240 | QSettings Reg(RootKeyName, QSettings::NativeFormat);
|
|---|
| [90] | 241 |
|
|---|
| [133] | 242 | if (!Reg.isWritable() || Reg.status() != QSettings::NoError)
|
|---|
| 243 | return false;
|
|---|
| [90] | 244 |
|
|---|
| [133] | 245 | QString appPath = executablePath;
|
|---|
| 246 | appPath.replace('/', '\\'); // Explorer gives 'Access Denied' if we write the path with forward slashes to the registry
|
|---|
| 247 |
|
|---|
| 248 | // Add our ProgId to the HKCR classes
|
|---|
| 249 | Reg.setValue(classId + "/shell/open/FriendlyAppName", friendlyName);
|
|---|
| 250 | Reg.setValue(classId + "/shell/open/command/.", QString("\"%1\" \"%2\"").arg(appPath, "%1"));
|
|---|
| 251 | Reg.setValue(classId + "/DefaultIcon/.", QString("\"%1\",1").arg(appPath));
|
|---|
| 252 |
|
|---|
| 253 | // Add "Enqueue" command
|
|---|
| 254 | Reg.setValue(classId + "/shell/enqueue/.", QObject::tr("Enqueue in SMPlayer"));
|
|---|
| 255 | Reg.setValue(classId + "/shell/enqueue/command/.", QString("\"%1\" -add-to-playlist \"%2\"").arg(appPath, "%1"));
|
|---|
| 256 | return true;
|
|---|
| [90] | 257 | }
|
|---|
| [133] | 258 |
|
|---|
| 259 | // Remove ClassId from the registry.
|
|---|
| 260 | // Called when no associations exist. Note: It's better to do this in the Setup program.
|
|---|
| [90] | 261 | bool WinFileAssoc::RemoveClassId()
|
|---|
| 262 | {
|
|---|
| [133] | 263 | QString RootKeyName;
|
|---|
| 264 | QString classId;
|
|---|
| [90] | 265 |
|
|---|
| [133] | 266 | classId = "Software/Classes/" + m_ClassId;
|
|---|
| 267 | RootKeyName = "HKEY_CURRENT_USER";
|
|---|
| [90] | 268 |
|
|---|
| [133] | 269 | QSettings RegCU(RootKeyName, QSettings::NativeFormat);
|
|---|
| [90] | 270 |
|
|---|
| [133] | 271 | if (!RegCU.isWritable() || RegCU.status() != QSettings::NoError)
|
|---|
| 272 | return false;
|
|---|
| [90] | 273 |
|
|---|
| [133] | 274 | RegCU.remove(classId);
|
|---|
| 275 | return true;
|
|---|
| [90] | 276 | }
|
|---|
| 277 |
|
|---|
| [133] | 278 | // Windows Vista specific implementation
|
|---|
| [90] | 279 |
|
|---|
| 280 | #if !defined(IApplicationAssociationRegistration)
|
|---|
| 281 |
|
|---|
| [133] | 282 | typedef enum tagASSOCIATIONLEVEL {
|
|---|
| |
|---|