OSDN Git Service

Bump version.
[lamexp/LameXP.git] / src / ShellIntegration.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "ShellIntegration.h"
24
25 //Internal
26 #include "Global.h"
27 #include "Registry_Decoder.h"
28
29 //MUtils
30 #include <MUtils/Global.h>
31 #include <MUtils/Exception.h>
32 #include <MUtils/OSSupport.h>
33 #include <MUtils/Registry.h>
34
35 //Qt
36 #include <QString>
37 #include <QStringList>
38 #include <QRegExp>
39 #include <QApplication>
40 #include <QFileInfo>
41 #include <QDir>
42 #include <QMutexLocker>
43 #include <QtConcurrentRun>
44
45 //Const
46 static const char *g_lamexpShellAction = "ConvertWithLameXP";
47 static const char *g_lamexpFileType    = "LameXP.SupportedAudioFile";
48
49 //Mutex
50 QMutex ShellIntegration::m_mutex;
51
52 //State values
53 static const int STATE_ENABLED =  1;
54 static const int STATE_UNKNOWN =  0;
55 static const int STATE_DISABLD = -1;
56
57 //State
58 volatile int ShellIntegration::m_state = STATE_UNKNOWN;
59
60 //Macros
61 #define REG_WRITE_STRING(KEY, STR) RegSetValueEx(key, NULL, NULL, REG_SZ, reinterpret_cast<const BYTE*>(STR.utf16()), (STR.size() + 1) * sizeof(wchar_t))
62
63 ////////////////////////////////////////////////////////////
64 // Constructor
65 ////////////////////////////////////////////////////////////
66
67 ShellIntegration::ShellIntegration(void)
68 {
69         MUTILS_THROW("Cannot create instance of this class, sorry!");
70 }
71
72 ////////////////////////////////////////////////////////////
73 // Public Functions
74 ////////////////////////////////////////////////////////////
75
76 void ShellIntegration::install(bool async)
77 {
78         //Install asynchronously
79         if(async)
80         {
81                 QFuture<void>(QtConcurrent::run(install, false));
82                 return;
83         }
84         
85         //Serialize
86         QMutexLocker lock(&m_mutex);
87         
88         //Checking
89         if(m_state == STATE_ENABLED)
90         {
91                 return; /*already enabled, don't enable again!*/
92         }
93
94         //Init some consts
95         const QString lamexpFileType(g_lamexpFileType);
96         const QString lamexpFileInfo(tr("Audio File supported by LameXP"));
97         const QString lamexpShellText(tr("Convert this file with LameXP v%1").arg(QString().sprintf("%d.%02d", lamexp_version_major(), lamexp_version_minor())));
98         const QString lamexpShellCommand = QString("\"%1\" \"--add=%2\"").arg(QDir::toNativeSeparators(QFileInfo(QApplication::applicationFilePath()).canonicalFilePath()), "%1");
99         const QString lamexpShellAction(g_lamexpShellAction);
100
101         //Register the LameXP file type
102         bool ok[4] = {false, false, false, false};
103         ok[0] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1")                    .arg(lamexpFileType),                    QString(), lamexpFileInfo);
104         ok[1] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell")             .arg(lamexpFileType),                    QString(), lamexpShellAction);
105         ok[2] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2")         .arg(lamexpFileType, lamexpShellAction), QString(), lamexpShellText);
106         ok[3] = MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2\\command").arg(lamexpFileType, lamexpShellAction), QString(), lamexpShellCommand);
107         if(!(ok[0] && ok[1] && ok[2] && ok[3]))
108         {
109                 qWarning("Failed to register the LameXP file type!");
110                 return;
111         }
112
113         //Detect supported file types
114         QStringList types;
115         initializeTypes(lamexpFileType, lamexpShellAction, types);
116
117         //Add LameXP shell action to all supported file types
118         for(QStringList::ConstIterator iter = types.constBegin(); iter != types.constEnd(); iter++)
119         {
120                 MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2")         .arg((*iter), lamexpShellAction), QString(), lamexpShellText);
121                 MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2\\command").arg((*iter), lamexpShellAction), QString(), lamexpShellCommand);
122         }
123         
124         //Shell notification
125         MUtils::OS::shell_change_notification();
126         m_state = STATE_ENABLED;
127 }
128
129 void ShellIntegration::remove(bool async)
130 {
131         //Remove asynchronously
132         if(async)
133         {
134                 QFuture<void>(QtConcurrent::run(remove, false));
135                 return;
136         }
137
138         //Serialize
139         QMutexLocker lock(&m_mutex);
140         
141         //Checking
142         if(m_state == STATE_DISABLD)
143         {
144                 return; /*already enabled, don't enable again!*/
145         }
146
147         //Init some consts
148         const QString lamexpFileType(g_lamexpFileType);
149         const QString lamexpShellAction(g_lamexpShellAction);
150
151         //Initialization
152         QStringList fileTypes;
153
154         //Find all registered file types
155         if(!MUtils::Registry::reg_enum_subkeys(MUtils::Registry::root_user, "Software\\Classes", fileTypes))
156         {
157                 qWarning("Failed to enumerate file types!");
158                 return;
159         }
160
161         //Remove shell action from all file types
162         for(QStringList::ConstIterator iter = fileTypes.constBegin(); iter != fileTypes.constEnd(); iter++)
163         {
164                 //Remove LameXP-specific types altogether
165                 if(iter->startsWith('.'))
166                 {
167                         QString currentFileType;
168                         if(MUtils::Registry::reg_value_read(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(*iter), QString(), currentFileType))
169                         {
170                                 if(currentFileType.compare(lamexpFileType, Qt::CaseInsensitive) == 0)
171                                 {
172                                         MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(*iter));
173                                         continue;
174                                 }
175                         }
176                 }
177
178                 //Remove shell action for non-LameXP types
179                 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\shell\\%2").arg((*iter), lamexpShellAction));
180
181                 //Remove from sub-tree too
182                 QStringList subTypes;
183                 if(MUtils::Registry::reg_enum_subkeys(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(*iter), subTypes))
184                 {
185                         for(QStringList::ConstIterator iter2 = subTypes.constBegin(); iter2 != subTypes.constEnd(); iter2++)
186                         {
187                                 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1\\%2\\shell\\%3").arg((*iter), (*iter2), lamexpShellAction));
188                         }
189                 }
190         }
191
192
193         //Unregister LameXP file type
194         MUtils::Registry::reg_key_delete(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(lamexpFileType));
195
196         //Shell notification
197         MUtils::OS::shell_change_notification();
198         m_state = STATE_DISABLD;
199 }
200
201 ////////////////////////////////////////////////////////////
202 // Private Functions
203 ////////////////////////////////////////////////////////////
204
205 void ShellIntegration::initializeTypes(const QString &lamexpFileType, const QString &lamexpShellAction, QStringList &nativeTypes)
206 {
207         nativeTypes.clear();
208         const QString progId = "Progid";
209
210         //Map supported extensions to native types
211         const QStringList &supportedExts = DecoderRegistry::getSupportedExts();
212         for(QStringList::ConstIterator iter = supportedExts.constBegin(); iter != supportedExts.constEnd(); iter++)
213         {
214                 const QString currentExt = (*iter).mid(1).trimmed(); /*remove leading asterisk*/
215                 if(currentExt.isEmpty() || (!currentExt.startsWith('.')))
216                 {
217                         qWarning("Invalid file extension '%s' encountered -> skipping!", currentExt);
218                         continue;
219                 }
220
221                 bool hasExistingType = false;
222                 QString currentType;
223                 if(MUtils::Registry::reg_key_exists(MUtils::Registry::root_classes, currentExt))
224                 {
225                         if(MUtils::Registry::reg_value_read(MUtils::Registry::root_classes, currentExt, QString(), currentType))
226                         {
227                                 currentType = QDir::toNativeSeparators(currentType);
228                                 if((currentType.compare(lamexpFileType, Qt::CaseInsensitive) != 0) && (!nativeTypes.contains(currentType, Qt::CaseInsensitive)))
229                                 {
230                                         nativeTypes.append(currentType);
231                                         hasExistingType = true;
232                                 }
233                         }
234                 }
235                 if(!hasExistingType)
236                 {
237                         currentType = lamexpFileType;
238                         MUtils::Registry::reg_value_write(MUtils::Registry::root_user, QString("Software\\Classes\\%1").arg(currentExt), QString(), lamexpFileType);
239                 }
240
241                 const QString userChoiceKey = QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\UserChoice").arg(currentExt);
242                 if(MUtils::Registry::reg_key_exists(MUtils::Registry::root_user, userChoiceKey))
243                 {
244                         if(MUtils::Registry::reg_value_read(MUtils::Registry::root_user, userChoiceKey, progId, currentType))
245                         {
246                                 currentType = QDir::toNativeSeparators(currentType);
247                                 if((currentType.compare(lamexpFileType, Qt::CaseInsensitive) != 0) && (!nativeTypes.contains(currentType, Qt::CaseInsensitive)))
248                                 {
249                                         nativeTypes.append(currentType);
250                                 }
251                         }
252                 }
253
254                 QStringList progids;
255                 if(MUtils::Registry::reg_enum_values(MUtils::Registry::root_user, QString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\%1\\OpenWithProgids").arg(currentExt), progids))
256                 {
257                         for(QStringList::ConstIterator iter2 = progids.constBegin(); iter2 != progids.constEnd(); iter2++)
258                         {
259                                 currentType = QDir::toNativeSeparators(currentType);
260                                 if((iter2->compare(lamexpFileType, Qt::CaseInsensitive) != 0) && (!nativeTypes.contains((*iter2), Qt::CaseInsensitive)))
261                                 {
262                                         nativeTypes.append(*iter2);
263                                 }
264                         }
265                 }
266         }
267 }