aboutsummaryrefslogtreecommitdiff
path: root/src/qmmpui/winfileassoc.cpp
blob: 1d99ea9330a149c4b444fbf0062c14105d08e0ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*  smplayer, GUI front-end for mplayer.
    Copyright (C) 2006-2014 Ricardo Villalba <rvm@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

	Winfileassoc.cpp

	Handles file associations in Windows 7/Vista/XP/2000.
	We assume that the code is run without administrator privileges, so the associations are done for current user only.
	System-wide associations require writing to HKEY_CLASSES_ROOT and we don't want to get our hands dirty with that.
	Each user on the computer can configure his own set of file associations for SMPlayer, which is extremely cool.

	Optionally, during uninstall, it would be a good idea to call RestoreFileAssociations for all media types so
	that we can clean up the registry and restore the old associations for current user.

	Vista:
	The code can only register the app as default program for selected extensions and check if it is the default.
	It cannot restore 'old' default application, since this doesn't seem to be possible with the current Vista API.

	Add libole32.a library if compiling with MinGW. In smplayer.pro, under 'win32 {': LIBS += libole32

	Tested on: WinXP, Vista, Win7.

	Author: Florin Braghis (florin@libertv.ro)
*/

/*
   main changes compared to SMPlayer:
     replaced SMPlayer by Qmmp
     added icon cache update
     removed xp support
*/


#include "winfileassoc.h"
#include <QSettings>
#include <QApplication>
#include <QFileInfo>
#include <windows.h>
#include <shlobj.h>

#ifndef SHCNE_ASSOCHANGED
#define SHCNE_ASSOCHANGED __MSABI_LONG(0x08000000)
#endif

WinFileAssoc::WinFileAssoc(const QString AppName) : m_AppName(AppName)
{}

// Associates all extensions in the fileExtensions list with current app.
// Returns number of extensions processed successfully.
int WinFileAssoc::CreateFileAssociations(const QStringList &fileExtensions)
{
    return VistaSetAppsAsDefault(fileExtensions);
}

// Checks if extensions in extensionsToCheck are registered with this application. Returns a list of registered extensions.
// Returns false if there was an error accessing the registry.
// Returns true and 0 elements in registeredExtensions if no extension is associated with current app.
bool WinFileAssoc::GetRegisteredExtensions(const QStringList &extensionsToCheck, QStringList &registeredExtensions)
{
    registeredExtensions.clear();
    return VistaGetDefaultApps(extensionsToCheck, registeredExtensions);
}

// Windows Vista specific implementation

#if !defined(IApplicationAssociationRegistration)

typedef enum tagASSOCIATIONLEVEL {
    AL_MACHINE,
    AL_EFFECTIVE,
    AL_USER
} ASSOCIATIONLEVEL;

typedef enum tagASSOCIATIONTYPE {
    AT_FILEEXTENSION,
    AT_URLPROTOCOL,
    AT_STARTMENUCLIENT,
    AT_MIMETYPE
} ASSOCIATIONTYPE;

MIDL_INTERFACE("4e530b0a-e611-4c77-a3ac-9031d022281b")
IApplicationAssociationRegistration :
public IUnknown {
public:
    virtual HRESULT STDMETHODCALLTYPE QueryCurrentDefault(LPCWSTR pszQuery,
    ASSOCIATIONTYPE atQueryType,
    ASSOCIATIONLEVEL alQueryLevel,
    LPWSTR * ppszAssociation) = 0;
    virtual HRESULT STDMETHODCALLTYPE QueryAppIsDefault(LPCWSTR pszQuery,
    ASSOCIATIONTYPE atQueryType,
    ASSOCIATIONLEVEL alQueryLevel,
    LPCWSTR pszAppRegistryName,
    BOOL * pfDefault) = 0;
    virtual HRESULT STDMETHODCALLTYPE QueryAppIsDefaultAll(ASSOCIATIONLEVEL alQueryLevel,
    LPCWSTR pszAppRegistryName,
    BOOL * pfDefault) = 0;
    virtual HRESULT STDMETHODCALLTYPE SetAppAsDefault(LPCWSTR pszAppRegistryName,
    LPCWSTR pszSet,
    ASSOCIATIONTYPE atSetType) = 0;
    virtual HRESULT STDMETHODCALLTYPE SetAppAsDefaultAll(LPCWSTR pszAppRegistryName) = 0;
    virtual HRESULT STDMETHODCALLTYPE ClearUserAssociations(void) = 0;
};
#endif

static const CLSID CLSID_ApplicationAssociationReg = {0x591209C7, 0x767B, 0x42B2, {0x9F, 0xBA, 0x44, 0xEE, 0x46, 0x15, 0xF2, 0xC7}};
static const IID   IID_IApplicationAssociationReg  = {0x4e530b0a, 0xe611, 0x4c77, {0xa3, 0xac, 0x90, 0x31, 0xd0, 0x22, 0x28, 0x1b}};

int WinFileAssoc::VistaSetAppsAsDefault(const QStringList &fileExtensions)
{
    IApplicationAssociationRegistration *pAAR;
    HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationReg,
                                  NULL, CLSCTX_INPROC, IID_IApplicationAssociationReg,	(void **)&pAAR);

    int count = 0;

    if (SUCCEEDED(hr) && (pAAR != NULL)) {
        for(const QString &fileExtension : qAsConst(fileExtensions)) {
            hr = pAAR->SetAppAsDefault((const WCHAR *)m_AppName.utf16(),
                                       (const WCHAR *)QString("." + fileExtension).utf16(),
                                       AT_FILEEXTENSION);

            if (SUCCEEDED(hr))
                count++;
        }
        pAAR->Release();
    }

    return count;
}

bool WinFileAssoc::VistaGetDefaultApps(const QStringList &extensions, QStringList &registeredExt)
{
    IApplicationAssociationRegistration *pAAR;

    HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationReg,
                                  NULL, CLSCTX_INPROC, IID_IApplicationAssociationReg,	(void **)&pAAR);

    if (SUCCEEDED(hr) && (pAAR != NULL)) {
        for(const QString &fileExtension : qAsConst(extensions)) {
            BOOL bIsDefault = false;
            hr = pAAR->QueryAppIsDefault((const WCHAR *)QString("." + fileExtension).utf16(),
                                         AT_FILEEXTENSION,
                                         AL_EFFECTIVE,
                                         (const WCHAR *)m_AppName.utf16(),
                                         &bIsDefault);

            if (SUCCEEDED(hr) && bIsDefault) {
                registeredExt.append(fileExtension);
            }
        }

        pAAR->Release();
        return true;
    }

    return false;
}