diff options
author | Underground78 <underground78@users.sourceforge.net> | 2012-06-19 01:55:48 +0400 |
---|---|---|
committer | Underground78 <underground78@users.sourceforge.net> | 2012-06-19 01:55:48 +0400 |
commit | e491adb964b84812d4af25e02be160ea0a8cd213 (patch) | |
tree | 7e5fa80804b1e0aea4e92b558aaf4479c5e7a560 | |
parent | 62fd0292215cb68ab8e114150a04447f67797d58 (diff) |
File associations improvements (part 1): move the functional code into its own class.
git-svn-id: https://mpc-hc.svn.sourceforge.net/svnroot/mpc-hc/trunk@5190 10f7b99b-c216-0410-bff0-8a66a9350fd8
-rw-r--r-- | src/mpc-hc/FileAssoc.cpp | 694 | ||||
-rw-r--r-- | src/mpc-hc/FileAssoc.h | 69 | ||||
-rw-r--r-- | src/mpc-hc/PPageFormats.cpp | 648 | ||||
-rw-r--r-- | src/mpc-hc/PPageFormats.h | 14 | ||||
-rw-r--r-- | src/mpc-hc/mpc-hc.vcxproj | 2 | ||||
-rw-r--r-- | src/mpc-hc/mpc-hc.vcxproj.filters | 6 | ||||
-rw-r--r-- | src/mpc-hc/mplayerc.cpp | 25 |
7 files changed, 813 insertions, 645 deletions
diff --git a/src/mpc-hc/FileAssoc.cpp b/src/mpc-hc/FileAssoc.cpp new file mode 100644 index 000000000..181b4cedf --- /dev/null +++ b/src/mpc-hc/FileAssoc.cpp @@ -0,0 +1,694 @@ +/*
+ * $Id$
+ *
+ * (C) 2003-2006 Gabest
+ * (C) 2006-2012 see Authors.txt
+ *
+ * This file is part of MPC-HC.
+ *
+ * MPC-HC 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * MPC-HC 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "stdafx.h"
+#include "FileAssoc.h"
+
+
+// TODO: change this along with the root key for settings and the mutex name to
+// avoid possible risks of conflict with the old MPC (non HC version).
+#ifdef _WIN64
+#define PROGID _T("mplayerc64")
+#else
+#define PROGID _T("mplayerc")
+#endif // _WIN64
+
+LPCTSTR CFileAssoc::strRegisteredAppName = _T("Media Player Classic");
+LPCTSTR CFileAssoc::strOldAssocKey = _T("PreviousRegistration");
+LPCTSTR CFileAssoc::strRegisteredAppKey = _T("Software\\Clients\\Media\\Media Player Classic\\Capabilities");
+
+const CString CFileAssoc::strOpenCommand = CFileAssoc::GetOpenCommand();
+const CString CFileAssoc::strEnqueueCommand = CFileAssoc::GetEnqueueCommand();
+
+CComPtr<IApplicationAssociationRegistration> CFileAssoc::m_pAAR;
+
+static CString GetProgramDir()
+{
+ CString RtnVal;
+ TCHAR FileName[_MAX_PATH];
+ ::GetModuleFileName(AfxGetInstanceHandle(), FileName, _MAX_PATH);
+ RtnVal = FileName;
+ RtnVal = RtnVal.Left(RtnVal.ReverseFind('\\'));
+ return RtnVal;
+}
+
+static int FileExists(const TCHAR* fileName)
+{
+ DWORD fileAttr;
+ fileAttr = ::GetFileAttributes(fileName);
+ if (0xFFFFFFFF == fileAttr) {
+ return false;
+ }
+ return true;
+}
+
+typedef int (*GetIconIndexFunc)(CString);
+
+static int GetIconIndex(CString ext)
+{
+ int iconindex = -1;
+ GetIconIndexFunc _getIconIndexFunc;
+ HINSTANCE mpciconlib = LoadLibrary(_T("mpciconlib.dll"));
+ if (mpciconlib) {
+ _getIconIndexFunc = (GetIconIndexFunc) GetProcAddress(mpciconlib, "get_icon_index");
+ if (_getIconIndexFunc) {
+ iconindex = _getIconIndexFunc(ext);
+ }
+ FreeLibrary(mpciconlib);
+ }
+
+ return iconindex;
+}
+
+CString CFileAssoc::GetOpenCommand()
+{
+ CString path;
+ TCHAR buff[_MAX_PATH];
+
+ if (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH) == 0) {
+ return _T("");
+ }
+
+ path = buff;
+ return _T("\"") + path + _T("\" \"%1\"");
+}
+
+CString CFileAssoc::GetEnqueueCommand()
+{
+ CString path;
+ TCHAR buff[_MAX_PATH];
+
+ if (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH) == 0) {
+ return _T("");
+ }
+
+ path = buff;
+ return _T("\"") + path + _T("\" /add \"%1\"");
+}
+
+IApplicationAssociationRegistration* CFileAssoc::CreateRegistrationManager()
+{
+ IApplicationAssociationRegistration *pAAR = NULL;
+
+ // Default manager (requires at least Vista)
+ HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
+ NULL,
+ CLSCTX_INPROC,
+ __uuidof(IApplicationAssociationRegistration),
+ (LPVOID*)&pAAR);
+ UNREFERENCED_PARAMETER(hr);
+
+ return pAAR;
+}
+
+bool CFileAssoc::RegisterApp()
+{
+ if (!m_pAAR) {
+ m_pAAR = CFileAssoc::CreateRegistrationManager();
+ }
+
+ if (m_pAAR) {
+ CString AppIcon = _T("");
+ TCHAR buff[_MAX_PATH];
+
+ if (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH)) {
+ AppIcon = buff;
+ AppIcon = "\"" + AppIcon + "\"";
+ AppIcon += _T(",0");
+ }
+
+ // Register MPC for the windows "Default application" manager
+ CRegKey key;
+
+ if (ERROR_SUCCESS == key.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\RegisteredApplications"))) {
+ key.SetStringValue(_T("Media Player Classic"), strRegisteredAppKey);
+
+ if (ERROR_SUCCESS != key.Create(HKEY_LOCAL_MACHINE, strRegisteredAppKey)) {
+ return false;
+ }
+
+ // ==>> TODO icon !!!
+ key.SetStringValue(_T("ApplicationDescription"), ResStr(IDS_APP_DESCRIPTION), REG_EXPAND_SZ);
+ key.SetStringValue(_T("ApplicationIcon"), AppIcon, REG_EXPAND_SZ);
+ key.SetStringValue(_T("ApplicationName"), ResStr(IDR_MAINFRAME), REG_EXPAND_SZ);
+ }
+ }
+ return true;
+}
+
+bool CFileAssoc::Register(CString ext, CString strLabel, bool bRegister, bool bRegisterContextMenuEntries, bool bAssociatedWithIcon)
+{
+ CRegKey key;
+ bool bSetValue;
+ CString strProgID = PROGID + ext;
+
+ if (!bRegister) {
+ if (bRegister != IsRegistered(ext)) {
+ SetFileAssociation(ext, strProgID, bRegister);
+ }
+ key.Attach(HKEY_CLASSES_ROOT);
+ key.RecurseDeleteKey(strProgID);
+ return true;
+ }
+
+ bSetValue = bRegister || (ERROR_SUCCESS != key.Open(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\open\\command"), KEY_READ));
+
+ // Create ProgID for this file type
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID)) {
+ return false;
+ }
+ if (ERROR_SUCCESS != key.SetStringValue(NULL, strLabel)) {
+ return false;
+ }
+
+ // Add to playlist option
+ if (bRegisterContextMenuEntries) {
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\enqueue"))) {
+ return false;
+ }
+ if (ERROR_SUCCESS != key.SetStringValue(NULL, ResStr(IDS_ADD_TO_PLAYLIST))) {
+ return false;
+ }
+
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\enqueue\\command"))) {
+ return false;
+ }
+ if (bSetValue && (ERROR_SUCCESS != key.SetStringValue(NULL, strEnqueueCommand))) {
+ return false;
+ }
+ } else {
+ key.Close();
+ key.Attach(HKEY_CLASSES_ROOT);
+ key.RecurseDeleteKey(strProgID + _T("\\shell\\enqueue"));
+ }
+
+ // Play option
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\open"))) {
+ return false;
+ }
+ if (bRegisterContextMenuEntries) {
+ if (ERROR_SUCCESS != key.SetStringValue(NULL, ResStr(IDS_OPEN_WITH_MPC))) {
+ return false;
+ }
+ } else {
+ if (ERROR_SUCCESS != key.SetStringValue(NULL, _T(""))) {
+ return false;
+ }
+ }
+
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\open\\command"))) {
+ return false;
+ }
+ if (bSetValue && (ERROR_SUCCESS != key.SetStringValue(NULL, strOpenCommand))) {
+ return false;
+ }
+
+ if (ERROR_SUCCESS != key.Create(HKEY_LOCAL_MACHINE, CString(strRegisteredAppKey) + _T("\\FileAssociations"))) {
+ return false;
+ }
+ if (ERROR_SUCCESS != key.SetStringValue(ext, strProgID)) {
+ return false;
+ }
+
+ if (bAssociatedWithIcon) {
+ CString AppIcon = _T("");
+ TCHAR buff[_MAX_PATH];
+
+ CString mpciconlib = GetProgramDir() + _T("\\mpciconlib.dll");
+
+ if (FileExists(mpciconlib)) {
+ int icon_index = GetIconIndex(ext);
+ CString m_typeicon = mpciconlib;
+
+ /* icon_index value -1 means no icon was found in the iconlib for the file extension */
+ if ((icon_index >= 0) && ExtractIcon(AfxGetApp()->m_hInstance, (LPCWSTR)m_typeicon, icon_index)) {
+ m_typeicon = "\"" + mpciconlib + "\"";
+ AppIcon.Format(_T("%s,%d"), m_typeicon, icon_index);
+ }
+ }
+
+ /* no icon was found for the file extension, so use MPC's icon */
+ if ((AppIcon.IsEmpty()) && (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH))) {
+ AppIcon = buff;
+ AppIcon = "\"" + AppIcon + "\"";
+ AppIcon += _T(",0");
+ }
+
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\DefaultIcon"))) {
+ return false;
+ }
+ if (bSetValue && (ERROR_SUCCESS != key.SetStringValue(NULL, AppIcon))) {
+ return false;
+ }
+ } else {
+ key.Attach(HKEY_CLASSES_ROOT);
+ key.RecurseDeleteKey(strProgID + _T("\\DefaultIcon"));
+ }
+
+ if (bRegister != IsRegistered(ext)) {
+ SetFileAssociation(ext, strProgID, bRegister);
+ }
+
+ return true;
+}
+
+bool CFileAssoc::SetFileAssociation(CString strExt, CString strProgID, bool bRegister)
+{
+ CString extoldreg, extOldIcon;
+ CRegKey key;
+ HRESULT hr = S_OK;
+ TCHAR buff[256];
+ ULONG len = _countof(buff);
+ memset(buff, 0, sizeof(buff));
+
+ if (!m_pAAR) {
+ m_pAAR = CFileAssoc::CreateRegistrationManager();
+ }
+
+ if (m_pAAR) {
+ // The Vista way
+ CString strNewApp;
+ if (bRegister) {
+ // Create non existing file type
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strExt)) {
+ return false;
+ }
+
+ WCHAR* pszCurrentAssociation;
+ // Save current application associated
+ if (SUCCEEDED(m_pAAR->QueryCurrentDefault(strExt, AT_FILEEXTENSION, AL_EFFECTIVE, &pszCurrentAssociation))) {
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID)) {
+ return false;
+ }
+
+ key.SetStringValue(strOldAssocKey, pszCurrentAssociation);
+
+ // Get current icon for file type
+ /*
+ if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, CString(pszCurrentAssociation) + _T("\\DefaultIcon")))
+ {
+ len = sizeof(buff);
+ memset(buff, 0, len);
+ if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len) && !CString(buff).Trim().IsEmpty())
+ {
+ if (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\DefaultIcon")))
+ key.SetStringValue (NULL, buff);
+ }
+ }
+ */
+ CoTaskMemFree(pszCurrentAssociation);
+ }
+ strNewApp = strRegisteredAppName;
+ } else {
+ if (ERROR_SUCCESS != key.Open(HKEY_CLASSES_ROOT, strProgID)) {
+ return false;
+ }
+
+ if (ERROR_SUCCESS == key.QueryStringValue(strOldAssocKey, buff, &len)) {
+ strNewApp = buff;
+ }
+
+ // TODO : retrieve registered app name from previous association (or find Bill function for that...)
+ }
+
+ hr = m_pAAR->SetAppAsDefault(strNewApp, strExt, AT_FILEEXTENSION);
+ } else {
+ // The 2000/XP way
+ if (bRegister) {
+ // Set new association
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strExt)) {
+ return false;
+ }
+
+ len = _countof(buff);
+ memset(buff, 0, sizeof(buff));
+ if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len) && !CString(buff).Trim().IsEmpty()) {
+ extoldreg = buff;
+ }
+ if (ERROR_SUCCESS != key.SetStringValue(NULL, strProgID)) {
+ return false;
+ }
+
+ // Get current icon for file type
+ /*
+ if (!extoldreg.IsEmpty())
+ {
+ if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, extoldreg + _T("\\DefaultIcon")))
+ {
+ len = sizeof(buff);
+ memset(buff, 0, len);
+ if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len) && !CString(buff).Trim().IsEmpty())
+ extOldIcon = buff;
+ }
+ }
+ */
+
+ // Save old association
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID)) {
+ return false;
+ }
+ key.SetStringValue(strOldAssocKey, extoldreg);
+
+ /*
+ if (!extOldIcon.IsEmpty() && (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\DefaultIcon"))))
+ key.SetStringValue (NULL, extOldIcon);
+ */
+ } else {
+ // Get previous association
+ len = _countof(buff);
+ memset(buff, 0, sizeof(buff));
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID)) {
+ return false;
+ }
+ if (ERROR_SUCCESS == key.QueryStringValue(strOldAssocKey, buff, &len) && !CString(buff).Trim().IsEmpty()) {
+ extoldreg = buff;
+ }
+
+ // Set previous association
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strExt)) {
+ return false;
+ }
+ key.SetStringValue(NULL, extoldreg);
+ }
+ }
+
+ return SUCCEEDED(hr);
+}
+
+bool CFileAssoc::IsRegistered(CString ext)
+{
+ HRESULT hr;
+ BOOL bIsDefault = FALSE;
+ CString strProgID = PROGID + ext;
+
+ if (!m_pAAR) {
+ m_pAAR = CFileAssoc::CreateRegistrationManager();
+ }
+
+ if (m_pAAR) {
+ // The Vista way
+ hr = m_pAAR->QueryAppIsDefault(ext, AT_FILEEXTENSION, AL_EFFECTIVE, strRegisteredAppName, &bIsDefault);
+ } else {
+ // The 2000/XP way
+ CRegKey key;
+ TCHAR buff[256];
+ ULONG len = _countof(buff);
+ memset(buff, 0, sizeof(buff));
+
+ if (ERROR_SUCCESS != key.Open(HKEY_CLASSES_ROOT, ext)) {
+ return false;
+ }
+
+ if (ERROR_SUCCESS != key.QueryStringValue(NULL, buff, &len) && !CString(buff).Trim().IsEmpty()) {
+ return false;
+ }
+
+ bIsDefault = (buff == strProgID);
+ }
+
+ // Check if association is for this instance of MPC
+ if (bIsDefault) {
+ CRegKey key;
+ TCHAR buff[_MAX_PATH];
+ ULONG len = _countof(buff);
+
+ bIsDefault = FALSE;
+ if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\open\\command"), KEY_READ)) {
+ if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len)) {
+ bIsDefault = (strOpenCommand.CompareNoCase(CString(buff)) == 0);
+ }
+ }
+ }
+
+ return !!bIsDefault;
+}
+
+bool CFileAssoc::AreRegisteredFileContextMenuEntries(CString strExt)
+{
+ CRegKey key;
+ TCHAR buff[_MAX_PATH];
+ ULONG len = _countof(buff);
+ CString strProgID = PROGID + strExt;
+ bool registered = false;
+
+ if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\open"), KEY_READ)) {
+ CString strCommand = ResStr(IDS_OPEN_WITH_MPC);
+ if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len)) {
+ registered = (strCommand.CompareNoCase(CString(buff)) == 0);
+ }
+ }
+
+ return registered;
+}
+
+bool CFileAssoc::Register(CMediaFormatCategory &mfc, bool bRegister, bool bRegisterContextMenuEntries, bool bAssociatedWithIcon)
+{
+ CAtlList<CString> exts;
+ ExplodeMin(mfc.GetExtsWithPeriod(), exts, ' ');
+
+ CString strLabel = mfc.GetDescription();
+ bool res = true;
+
+ POSITION pos = exts.GetHeadPosition();
+ while (pos) {
+ res &= Register(exts.GetNext(pos), strLabel, bRegister, bRegisterContextMenuEntries, bAssociatedWithIcon);
+ }
+
+ return res;
+}
+
+CFileAssoc::reg_state_t CFileAssoc::IsRegistered(CMediaFormatCategory &mfc)
+{
+ CAtlList<CString> exts;
+ ExplodeMin(mfc.GetExtsWithPeriod(), exts, ' ');
+
+ size_t cnt = 0;
+
+ POSITION pos = exts.GetHeadPosition();
+ while (pos) {
+ if (CFileAssoc::IsRegistered(exts.GetNext(pos))) {
+ cnt++;
+ }
+ }
+
+ reg_state_t res;
+ if (cnt == 0) {
+ res = NOT_REGISTERED;
+ } else if (cnt == exts.GetCount()) {
+ res = ALL_REGISTERED;
+ } else {
+ res = SOME_REGISTERED;
+ }
+
+ return res;
+}
+
+CFileAssoc::reg_state_t CFileAssoc::AreRegisteredFileContextMenuEntries(CMediaFormatCategory &mfc)
+{
+ CAtlList<CString> exts;
+ ExplodeMin(mfc.GetExtsWithPeriod(), exts, ' ');
+
+ size_t cnt = 0;
+
+ POSITION pos = exts.GetHeadPosition();
+ while (pos) {
+ if (CFileAssoc::AreRegisteredFileContextMenuEntries(exts.GetNext(pos))) {
+ cnt++;
+ }
+ }
+
+ reg_state_t res;
+ if (cnt == 0) {
+ res = NOT_REGISTERED;
+ } else if (cnt == exts.GetCount()) {
+ res = ALL_REGISTERED;
+ } else {
+ res = SOME_REGISTERED;
+ }
+
+ return res;
+}
+
+bool CFileAssoc::RegisterFolderContextMenuEntries(bool bRegister)
+{
+ CRegKey key;
+ bool success;
+
+ if (bRegister) {
+ success = false;
+
+ if (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, _T("Directory\\shell\\") PROGID _T(".enqueue"))) {
+ key.SetStringValue(NULL, ResStr(IDS_ADD_TO_PLAYLIST));
+
+ if (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, _T("Directory\\shell\\") PROGID _T(".enqueue\\command"))) {
+ key.SetStringValue(NULL, strEnqueueCommand);
+ success = true;
+ }
+ }
+
+ if (success && ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, _T("Directory\\shell\\") PROGID _T(".play"))) {
+ success = false;
+
+ key.SetStringValue(NULL, ResStr(IDS_OPEN_WITH_MPC));
+
+ if (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, _T("Directory\\shell\\") PROGID _T(".play\\command"))) {
+ key.SetStringValue(NULL, strOpenCommand);
+ success = true;
+ }
+ }
+
+ } else {
+ key.Attach(HKEY_CLASSES_ROOT);
+ success = (ERROR_SUCCESS == key.RecurseDeleteKey(_T("Directory\\shell\\") PROGID _T(".enqueue")));
+ success &= (ERROR_SUCCESS == key.RecurseDeleteKey(_T("Directory\\shell\\") PROGID _T(".play")));
+ }
+
+ return success;
+}
+
+bool CFileAssoc::AreRegisteredFolderContextMenuEntries()
+{
+ CRegKey key;
+ TCHAR buff[_MAX_PATH];
+ ULONG len = _countof(buff);
+ bool registered = false;
+
+ if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, _T("Directory\\shell\\") PROGID _T(".play\\command"), KEY_READ)) {
+ if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len)) {
+ registered = (strOpenCommand.CompareNoCase(CString(buff)) == 0);
+ }
+ }
+
+ return registered;
+}
+
+static struct {
+ LPCSTR verb, cmd;
+ UINT action;
+} handlers[] = {
+ {"VideoFiles", " %1", IDS_AUTOPLAY_PLAYVIDEO},
+ {"MusicFiles", " %1", IDS_AUTOPLAY_PLAYMUSIC},
+ {"CDAudio", " %1 /cd", IDS_AUTOPLAY_PLAYAUDIOCD},
+ {"DVDMovie", " %1 /dvd", IDS_AUTOPLAY_PLAYDVDMOVIE},
+};
+
+void CFileAssoc::RegisterAutoPlay(autoplay_t ap, bool bRegister)
+{
+ TCHAR buff[_MAX_PATH];
+ if (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH) == 0) {
+ return;
+ }
+ CString exe = buff;
+
+ int i = (int)ap;
+ if (i < 0 || i >= _countof(handlers)) {
+ return;
+ }
+
+ CRegKey key;
+
+ if (bRegister) {
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, _T("MediaPlayerClassic.Autorun"))) {
+ return;
+ }
+ key.Close();
+
+ if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT,
+ CString(CStringA("MediaPlayerClassic.Autorun\\Shell\\Play") + handlers[i].verb + "\\Command"))) {
+ return;
+ }
+ key.SetStringValue(NULL, _T("\"") + exe + _T("\"") + handlers[i].cmd);
+ key.Close();
+
+ if (ERROR_SUCCESS != key.Create(HKEY_LOCAL_MACHINE,
+ CString(CStringA("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers\\Handlers\\MPCPlay") + handlers[i].verb + "OnArrival"))) {
+ return;
+ }
+ key.SetStringValue(_T("Action"), ResStr(handlers[i].action));
+ key.SetStringValue(_T("Provider"), _T("Media Player Classic"));
+ key.SetStringValue(_T("InvokeProgID"), _T("MediaPlayerClassic.Autorun"));
+ key.SetStringValue(_T("InvokeVerb"), CString(CStringA("Play") + handlers[i].verb));
+ key.SetStringValue(_T("DefaultIcon"), exe + _T(",0"));
+ key.Close();
+
+ if (ERROR_SUCCESS != key.Create(HKEY_LOCAL_MACHINE,
+ CString(CStringA("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers\\EventHandlers\\Play") + handlers[i].verb + "OnArrival"))) {
+ return;
+ }
+ key.SetStringValue(CString(CStringA("MPCPlay") + handlers[i].verb + "OnArrival"), _T(""));
+ key.Close();
+ } else {
+ if (ERROR_SUCCESS != key.Create(HKEY_LOCAL_MACHINE,
+ CString(CStringA("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers\\EventHandlers\\Play") + handlers[i].verb + "OnArrival"))) {
+ return;
+ }
+ key.DeleteValue(CString(CStringA("MPCPlay") + handlers[i].verb + "OnArrival"));
+ key.Close();
+ }
+}
+
+bool CFileAssoc::IsAutoPlayRegistered(autoplay_t ap)
+{
+ ULONG len;
+ TCHAR buff[_MAX_PATH];
+ if (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH) == 0) {
+ return false;
+ }
+ CString exe = buff;
+
+ int i = (int)ap;
+ if (i < 0 || i >= _countof(handlers)) {
+ return false;
+ }
+
+ CRegKey key;
+
+ if (ERROR_SUCCESS != key.Open(HKEY_LOCAL_MACHINE,
+ CString(CStringA("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers\\EventHandlers\\Play") + handlers[i].verb + "OnArrival"),
+ KEY_READ)) {
+ return false;
+ }
+ len = _countof(buff);
+ if (ERROR_SUCCESS != key.QueryStringValue(
+ CString(_T("MPCPlay")) + handlers[i].verb + _T("OnArrival"),
+ buff, &len)) {
+ return false;
+ }
+ key.Close();
+
+ if (ERROR_SUCCESS != key.Open(HKEY_CLASSES_ROOT,
+ CString(CStringA("MediaPlayerClassic.Autorun\\Shell\\Play") + handlers[i].verb + "\\Command"),
+ KEY_READ)) {
+ return false;
+ }
+ len = _countof(buff);
+ if (ERROR_SUCCESS != key.QueryStringValue(NULL, buff, &len)) {
+ return false;
+ }
+ if (_tcsnicmp(_T("\"") + exe, buff, exe.GetLength() + 1)) {
+ return false;
+ }
+ key.Close();
+
+ return true;
+}
diff --git a/src/mpc-hc/FileAssoc.h b/src/mpc-hc/FileAssoc.h new file mode 100644 index 000000000..61daa592d --- /dev/null +++ b/src/mpc-hc/FileAssoc.h @@ -0,0 +1,69 @@ +/*
+ * $Id$
+ *
+ * (C) 2003-2006 Gabest
+ * (C) 2006-2012 see Authors.txt
+ *
+ * This file is part of MPC-HC.
+ *
+ * MPC-HC 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * MPC-HC 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, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+#include <afxwin.h>
+
+
+class CFileAssoc
+{
+private:
+ CFileAssoc();
+
+ static CString GetOpenCommand();
+ static CString GetEnqueueCommand();
+
+ static IApplicationAssociationRegistration* CreateRegistrationManager();
+
+ static bool SetFileAssociation(CString strExt, CString strProgID, bool bRegister);
+
+ static LPCTSTR strRegisteredAppName;
+ static LPCTSTR strOldAssocKey;
+ static LPCTSTR strRegisteredAppKey;
+
+ static const CString strOpenCommand;
+ static const CString strEnqueueCommand;
+
+ static CComPtr<IApplicationAssociationRegistration> m_pAAR;
+
+public:
+ enum reg_state_t { NOT_REGISTERED, SOME_REGISTERED, ALL_REGISTERED };
+ enum autoplay_t { AP_VIDEO, AP_MUSIC, AP_AUDIOCD, AP_DVDMOVIE };
+
+ static bool RegisterApp();
+
+ static bool Register(CString ext, CString strLabel, bool bRegister, bool bRegisterContextMenuEntries, bool bAssociatedWithIcon);
+ static bool IsRegistered(CString ext);
+ static bool AreRegisteredFileContextMenuEntries(CString strExt);
+
+ static bool Register(CMediaFormatCategory &mfc, bool bRegister, bool bRegisterContextMenuEntries, bool bAssociatedWithIcon);
+ static reg_state_t IsRegistered(CMediaFormatCategory &mfc);
+ static reg_state_t AreRegisteredFileContextMenuEntries(CMediaFormatCategory &mfc);
+
+ static bool RegisterFolderContextMenuEntries(bool bRegister);
+ static bool AreRegisteredFolderContextMenuEntries();
+
+ static void RegisterAutoPlay(autoplay_t ap, bool bRegister);
+ static bool IsAutoPlayRegistered(autoplay_t ap);
+};
diff --git a/src/mpc-hc/PPageFormats.cpp b/src/mpc-hc/PPageFormats.cpp index 6be37ffda..578fc8010 100644 --- a/src/mpc-hc/PPageFormats.cpp +++ b/src/mpc-hc/PPageFormats.cpp @@ -24,6 +24,7 @@ #include "stdafx.h"
#include "mplayerc.h"
#include "PPageFormats.h"
+#include "FileAssoc.h"
#include "SysVersion.h"
#include <psapi.h>
#include <string>
@@ -32,25 +33,6 @@ // CPPageFormats dialog
-CComPtr<IApplicationAssociationRegistration> CPPageFormats::m_pAAR;
-
-// TODO: change this along with the root key for settings and the mutex name to
-// avoid possible risks of conflict with the old MPC (non HC version).
-#ifdef _WIN64
-#define PROGID _T("mplayerc64")
-#else
-#define PROGID _T("mplayerc")
-#endif // _WIN64
-
-LPCTSTR g_strRegisteredAppName = _T("Media Player Classic");
-LPCTSTR g_strOldAssoc = _T("PreviousRegistration");
-CString g_strRegisteredKey = _T("Software\\Clients\\Media\\Media Player Classic\\Capabilities");
-
-int f_setContextFiles = 0;
-int f_getContextFiles = 0;
-
-int f_setAssociatedWithIcon = 0;
-
IMPLEMENT_DYNAMIC(CPPageFormats, CPPageBase)
CPPageFormats::CPPageFormats()
: CPPageBase(CPPageFormats::IDD, CPPageFormats::IDD)
@@ -60,15 +42,6 @@ CPPageFormats::CPPageFormats() , m_fRtspFileExtFirst(FALSE)
, m_bInsufficientPrivileges(false)
{
- if (m_pAAR == NULL) {
- // Default manager (requires at least Vista)
- HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
- NULL,
- CLSCTX_INPROC,
- __uuidof(IApplicationAssociationRegistration),
- (void**)&m_pAAR);
- UNREFERENCED_PARAMETER(hr);
- }
}
CPPageFormats::~CPPageFormats()
@@ -112,99 +85,6 @@ void CPPageFormats::SetChecked(int iItem, int iChecked) m_list.SetItem(&lvi);
}
-CString CPPageFormats::GetEnqueueCommand()
-{
- CString path;
-
- TCHAR buff[_MAX_PATH];
- if (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH) == 0) {
- return _T("");
- }
-
- path = buff;
- return _T("\"") + path + _T("\" /add \"%1\"");
-}
-
-CString CPPageFormats::GetOpenCommand()
-{
- CString path;
- TCHAR buff[_MAX_PATH];
-
- if (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH) == 0) {
- return _T("");
- }
-
- path = buff;
- return _T("\"") + path + _T("\" \"%1\"");
-}
-
-bool CPPageFormats::IsRegistered(CString ext)
-{
- HRESULT hr;
- BOOL bIsDefault = FALSE;
- CString strProgID = PROGID + ext;
-
- if (m_pAAR == NULL) {
- // Default manager (requires at least Vista)
- hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
- NULL,
- CLSCTX_INPROC,
- __uuidof(IApplicationAssociationRegistration),
- (void**)&m_pAAR);
- }
-
- if (m_pAAR) {
- // The Vista way
- hr = m_pAAR->QueryAppIsDefault(ext, AT_FILEEXTENSION, AL_EFFECTIVE, g_strRegisteredAppName, &bIsDefault);
- } else {
- // The 2000/XP way
- CRegKey key;
- TCHAR buff[256];
- ULONG len = _countof(buff);
- memset(buff, 0, sizeof(buff));
-
- if (ERROR_SUCCESS != key.Open(HKEY_CLASSES_ROOT, ext)) {
- return false;
- }
-
- if (ERROR_SUCCESS != key.QueryStringValue(NULL, buff, &len) && !CString(buff).Trim().IsEmpty()) {
- return false;
- }
-
- bIsDefault = (buff == strProgID);
- }
- if (!f_setContextFiles) {
- CRegKey key;
- TCHAR buff[_MAX_PATH];
- ULONG len = _countof(buff);
-
- if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\open"), KEY_READ)) {
- CString strCommand = ResStr(IDS_OPEN_WITH_MPC);
- if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len)) {
- f_setContextFiles = (strCommand.CompareNoCase(CString(buff)) == 0);
- }
- }
- }
-
- // Check if association is for this instance of MPC
- if (bIsDefault) {
- CRegKey key;
- TCHAR buff[_MAX_PATH];
- ULONG len = _countof(buff);
-
- bIsDefault = FALSE;
- if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\open\\command"), KEY_READ)) {
- CString strCommand = GetOpenCommand();
- if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len)) {
- bIsDefault = (strCommand.CompareNoCase(CString(buff)) == 0);
- }
- }
-
- }
-
- return !!bIsDefault;
-}
-
CString GetProgramDir()
{
CString RtnVal;
@@ -225,314 +105,15 @@ int FileExists(const TCHAR* fileName) return true;
}
-typedef int (*GetIconIndexFunc)(CString);
-
-int GetIconIndex(CString ext)
-{
- int iconindex = -1;
- GetIconIndexFunc _getIconIndexFunc;
- HINSTANCE mpciconlib = LoadLibrary(_T("mpciconlib.dll"));
- if (mpciconlib) {
- _getIconIndexFunc = (GetIconIndexFunc) GetProcAddress(mpciconlib, "get_icon_index");
- if (_getIconIndexFunc) {
- iconindex = _getIconIndexFunc(ext);
- }
- FreeLibrary(mpciconlib);
- }
-
- return iconindex;
-}
-
-bool CPPageFormats::RegisterApp()
-{
- if (m_pAAR == NULL) {
- // Default manager (requires at least Vista)
- HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
- NULL,
- CLSCTX_INPROC,
- __uuidof(IApplicationAssociationRegistration),
- (void**)&m_pAAR);
- UNREFERENCED_PARAMETER(hr);
- }
-
- if (m_pAAR) {
- CString AppIcon = _T("");
- TCHAR buff[_MAX_PATH];
-
- if (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH)) {
- AppIcon = buff;
- AppIcon = "\"" + AppIcon + "\"";
- AppIcon += _T(",0");
- }
-
- // Register MPC for the windows "Default application" manager
- CRegKey key;
-
- if (ERROR_SUCCESS == key.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\RegisteredApplications"))) {
- key.SetStringValue(_T("Media Player Classic"), g_strRegisteredKey);
-
- if (ERROR_SUCCESS != key.Create(HKEY_LOCAL_MACHINE, g_strRegisteredKey)) {
- return false;
- }
-
- // ==>> TODO icon !!!
- key.SetStringValue(_T("ApplicationDescription"), ResStr(IDS_APP_DESCRIPTION), REG_EXPAND_SZ);
- key.SetStringValue(_T("ApplicationIcon"), AppIcon, REG_EXPAND_SZ);
- key.SetStringValue(_T("ApplicationName"), ResStr(IDR_MAINFRAME), REG_EXPAND_SZ);
- }
- }
- return true;
-}
-
-bool CPPageFormats::RegisterExt(CString ext, CString strLabel, bool fRegister)
-{
- CRegKey key;
- bool bSetValue;
- CString strProgID = PROGID + ext;
-
- if (!fRegister) {
- if (fRegister != IsRegistered(ext)) {
- SetFileAssociation(ext, strProgID, fRegister);
- }
- key.Attach(HKEY_CLASSES_ROOT);
- key.RecurseDeleteKey(strProgID);
- return true;
- }
-
- bSetValue = fRegister || (ERROR_SUCCESS != key.Open(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\open\\command"), KEY_READ));
-
- // Create ProgID for this file type
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID)) {
- return false;
- }
- if (ERROR_SUCCESS != key.SetStringValue(NULL, strLabel)) {
- return false;
- }
-
- // Add to playlist option
- if (f_setContextFiles) {
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\enqueue"))) {
- return false;
- }
- if (ERROR_SUCCESS != key.SetStringValue(NULL, ResStr(IDS_ADD_TO_PLAYLIST))) {
- return false;
- }
-
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\enqueue\\command"))) {
- return false;
- }
- if (bSetValue && (ERROR_SUCCESS != key.SetStringValue(NULL, GetEnqueueCommand()))) {
- return false;
- }
- } else {
- key.Close();
- key.Attach(HKEY_CLASSES_ROOT);
- key.RecurseDeleteKey(strProgID + _T("\\shell\\enqueue"));
- }
-
- // Play option
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\open"))) {
- return false;
- }
- if (f_setContextFiles) {
- if (ERROR_SUCCESS != key.SetStringValue(NULL, ResStr(IDS_OPEN_WITH_MPC))) {
- return false;
- }
- } else {
- if (ERROR_SUCCESS != key.SetStringValue(NULL, _T(""))) {
- return false;
- }
- }
-
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\shell\\open\\command"))) {
- return false;
- }
- if (bSetValue && (ERROR_SUCCESS != key.SetStringValue(NULL, GetOpenCommand()))) {
- return false;
- }
-
- if (ERROR_SUCCESS != key.Create(HKEY_LOCAL_MACHINE, g_strRegisteredKey + _T("\\FileAssociations"))) {
- return false;
- }
- if (ERROR_SUCCESS != key.SetStringValue(ext, strProgID)) {
- return false;
- }
-
- if (f_setAssociatedWithIcon) {
- CString AppIcon = _T("");
- TCHAR buff[_MAX_PATH];
-
- CString mpciconlib = GetProgramDir() + _T("\\mpciconlib.dll");
-
- if (FileExists(mpciconlib)) {
- int icon_index = GetIconIndex(ext);
- CString m_typeicon = mpciconlib;
-
- /* icon_index value -1 means no icon was found in the iconlib for the file extension */
- if ((icon_index >= 0) && ExtractIcon(AfxGetApp()->m_hInstance, (LPCWSTR)m_typeicon, icon_index)) {
- m_typeicon = "\"" + mpciconlib + "\"";
- AppIcon.Format(_T("%s,%d"), m_typeicon, icon_index);
- }
- }
-
- /* no icon was found for the file extension, so use MPC's icon */
- if ((AppIcon.IsEmpty()) && (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH))) {
- AppIcon = buff;
- AppIcon = "\"" + AppIcon + "\"";
- AppIcon += _T(",0");
- }
-
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\DefaultIcon"))) {
- return false;
- }
- if (bSetValue && (ERROR_SUCCESS != key.SetStringValue(NULL, AppIcon))) {
- return false;
- }
- } else {
- key.Attach(HKEY_CLASSES_ROOT);
- key.RecurseDeleteKey(strProgID + _T("\\DefaultIcon"));
- }
-
- if (fRegister != IsRegistered(ext)) {
- SetFileAssociation(ext, strProgID, fRegister);
- }
-
- return true;
-}
-
-static struct {
- LPCSTR verb, cmd;
- UINT action;
-} handlers[] = {
- {"VideoFiles", " %1", IDS_AUTOPLAY_PLAYVIDEO},
- {"MusicFiles", " %1", IDS_AUTOPLAY_PLAYMUSIC},
- {"CDAudio", " %1 /cd", IDS_AUTOPLAY_PLAYAUDIOCD},
- {"DVDMovie", " %1 /dvd", IDS_AUTOPLAY_PLAYDVDMOVIE},
-};
-
-void CPPageFormats::AddAutoPlayToRegistry(autoplay_t ap, bool fRegister)
-{
- TCHAR buff[_MAX_PATH];
- if (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH) == 0) {
- return;
- }
- CString exe = buff;
-
- int i = (int)ap;
- if (i < 0 || i >= _countof(handlers)) {
- return;
- }
-
- CRegKey key;
-
- if (fRegister) {
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, _T("MediaPlayerClassic.Autorun"))) {
- return;
- }
- key.Close();
-
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT,
- CString(CStringA("MediaPlayerClassic.Autorun\\Shell\\Play") + handlers[i].verb + "\\Command"))) {
- return;
- }
- key.SetStringValue(NULL, _T("\"") + exe + _T("\"") + handlers[i].cmd);
- key.Close();
-
- if (ERROR_SUCCESS != key.Create(HKEY_LOCAL_MACHINE,
- CString(CStringA("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers\\Handlers\\MPCPlay") + handlers[i].verb + "OnArrival"))) {
- return;
- }
- key.SetStringValue(_T("Action"), ResStr(handlers[i].action));
- key.SetStringValue(_T("Provider"), _T("Media Player Classic"));
- key.SetStringValue(_T("InvokeProgID"), _T("MediaPlayerClassic.Autorun"));
- key.SetStringValue(_T("InvokeVerb"), CString(CStringA("Play") + handlers[i].verb));
- key.SetStringValue(_T("DefaultIcon"), exe + _T(",0"));
- key.Close();
-
- if (ERROR_SUCCESS != key.Create(HKEY_LOCAL_MACHINE,
- CString(CStringA("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers\\EventHandlers\\Play") + handlers[i].verb + "OnArrival"))) {
- return;
- }
- key.SetStringValue(CString(CStringA("MPCPlay") + handlers[i].verb + "OnArrival"), _T(""));
- key.Close();
- } else {
- if (ERROR_SUCCESS != key.Create(HKEY_LOCAL_MACHINE,
- CString(CStringA("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers\\EventHandlers\\Play") + handlers[i].verb + "OnArrival"))) {
- return;
- }
- key.DeleteValue(CString(CStringA("MPCPlay") + handlers[i].verb + "OnArrival"));
- key.Close();
- }
-}
-
-bool CPPageFormats::IsAutoPlayRegistered(autoplay_t ap)
-{
- ULONG len;
- TCHAR buff[_MAX_PATH];
- if (::GetModuleFileName(AfxGetInstanceHandle(), buff, _MAX_PATH) == 0) {
- return false;
- }
- CString exe = buff;
-
- int i = (int)ap;
- if (i < 0 || i >= _countof(handlers)) {
- return false;
- }
-
- CRegKey key;
-
- if (ERROR_SUCCESS != key.Open(HKEY_LOCAL_MACHINE,
- CString(CStringA("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers\\EventHandlers\\Play") + handlers[i].verb + "OnArrival"),
- KEY_READ)) {
- return false;
- }
- len = _countof(buff);
- if (ERROR_SUCCESS != key.QueryStringValue(
- CString(_T("MPCPlay")) + handlers[i].verb + _T("OnArrival"),
- buff, &len)) {
- return false;
- }
- key.Close();
-
- if (ERROR_SUCCESS != key.Open(HKEY_CLASSES_ROOT,
- CString(CStringA("MediaPlayerClassic.Autorun\\Shell\\Play") + handlers[i].verb + "\\Command"),
- KEY_READ)) {
- return false;
- }
- len = _countof(buff);
- if (ERROR_SUCCESS != key.QueryStringValue(NULL, buff, &len)) {
- return false;
- }
- if (_tcsnicmp(_T("\"") + exe, buff, exe.GetLength() + 1)) {
- return false;
- }
- key.Close();
-
- return true;
-}
-
void CPPageFormats::SetListItemState(int nItem)
{
if (nItem < 0) {
return;
}
- CString str = AfxGetAppSettings().m_Formats[(int)m_list.GetItemData(nItem)].GetExtsWithPeriod();
-
- CAtlList<CString> exts;
- ExplodeMin(str, exts, ' ');
-
- int cnt = 0;
-
- POSITION pos = exts.GetHeadPosition();
- while (pos) if (IsRegistered(exts.GetNext(pos))) {
- cnt++;
- }
+ CFileAssoc::reg_state_t state = CFileAssoc::IsRegistered(AfxGetAppSettings().m_Formats[(int)m_list.GetItemData(nItem)]);
- if (cnt != 0) {
- cnt = (cnt == (int)exts.GetCount() ? 1 : 2);
- }
- SetChecked(nItem, cnt);
+ SetChecked(nItem, (state == CFileAssoc::ALL_REGISTERED) ? 1 : (state == CFileAssoc::NOT_REGISTERED) ? 0 : 2);
}
bool CPPageFormats::IsNeededIconsLib()
@@ -584,6 +165,8 @@ BOOL CPPageFormats::OnInitDialog() m_onoff.Create(IDB_ONOFF, 12, 3, 0xffffff);
m_list.SetImageList(&m_onoff, LVSIL_SMALL);
+ int fSetContextFiles = FALSE;
+
CMediaFormats& mf = AfxGetAppSettings().m_Formats;
mf.UpdateData(FALSE);
for (int i = 0; i < (int)mf.GetCount(); i++) {
@@ -598,6 +181,13 @@ BOOL CPPageFormats::OnInitDialog() e == RealMedia ? _T("RealMedia") :
e == QuickTime ? _T("QuickTime") :
e == ShockWave ? _T("ShockWave") : _T("-"));
+
+ CFileAssoc::reg_state_t state = CFileAssoc::IsRegistered(mf[i]);
+ SetChecked(iItem, (state == CFileAssoc::SOME_REGISTERED) ? 2 : (state == CFileAssoc::ALL_REGISTERED));
+
+ if (!fSetContextFiles && CFileAssoc::AreRegisteredFileContextMenuEntries(mf[i]) != CFileAssoc::NOT_REGISTERED) {
+ fSetContextFiles = TRUE;
+ }
}
// m_list.SetColumnWidth(COL_CATEGORY, LVSCW_AUTOSIZE);
@@ -613,23 +203,15 @@ BOOL CPPageFormats::OnInitDialog() m_iRtspHandler = (e == RealMedia ? 0 : e == QuickTime ? 1 : 2);
m_fRtspFileExtFirst = fRtspFileExtFirst;
- UpdateData(FALSE);
+ m_fContextFiles.SetCheck(fSetContextFiles);
- f_setContextFiles = 0;
-
- for (int i = 0; i < m_list.GetItemCount(); i++) {
- SetListItemState(i);
- }
- m_fContextFiles.SetCheck(f_setContextFiles);
-
- m_apvideo.SetCheck(IsAutoPlayRegistered(AP_VIDEO));
- m_apmusic.SetCheck(IsAutoPlayRegistered(AP_MUSIC));
- m_apaudiocd.SetCheck(IsAutoPlayRegistered(AP_AUDIOCD));
- m_apdvd.SetCheck(IsAutoPlayRegistered(AP_DVDMOVIE));
+ m_apvideo.SetCheck(CFileAssoc::IsAutoPlayRegistered(CFileAssoc::AP_VIDEO));
+ m_apmusic.SetCheck(CFileAssoc::IsAutoPlayRegistered(CFileAssoc::AP_MUSIC));
+ m_apaudiocd.SetCheck(CFileAssoc::IsAutoPlayRegistered(CFileAssoc::AP_AUDIOCD));
+ m_apdvd.SetCheck(CFileAssoc::IsAutoPlayRegistered(CFileAssoc::AP_DVDMOVIE));
CreateToolTip();
-
if (SysVersion::IsVistaOrLater() && !IsUserAnAdmin()) {
GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_BUTTON3)->ShowWindow(SW_HIDE);
@@ -652,155 +234,15 @@ BOOL CPPageFormats::OnInitDialog() GetDlgItem(IDC_BUTTON5)->ShowWindow(SW_HIDE);
}
-
- CRegKey key;
- TCHAR buff[_MAX_PATH];
- ULONG len = _countof(buff);
-
- int fContextDir = 0;
- if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, _T("Directory\\shell\\") PROGID _T(".play\\command"), KEY_READ)) {
- CString strCommand = GetOpenCommand();
- if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len)) {
- fContextDir = (strCommand.CompareNoCase(CString(buff)) == 0);
- }
- }
- m_fContextDir.SetCheck(fContextDir);
+ m_fContextDir.SetCheck(CFileAssoc::AreRegisteredFolderContextMenuEntries());
m_fAssociatedWithIcons.SetCheck(s.fAssociatedWithIcons);
+ UpdateData(FALSE);
+
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
-BOOL CPPageFormats::SetFileAssociation(CString strExt, CString strProgID, bool fRegister)
-{
- CString extoldreg, extOldIcon;
- CRegKey key;
- HRESULT hr = S_OK;
- TCHAR buff[256];
- ULONG len = _countof(buff);
- memset(buff, 0, sizeof(buff));
-
- if (m_pAAR == NULL) {
- // Default manager (requires at least Vista)
- HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
- NULL,
- CLSCTX_INPROC,
- __uuidof(IApplicationAssociationRegistration),
- (void**)&m_pAAR);
- UNREFERENCED_PARAMETER(hr);
- }
-
- if (m_pAAR) {
- // The Vista way
- CString strNewApp;
- if (fRegister) {
- // Create non existing file type
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strExt)) {
- return false;
- }
-
- WCHAR* pszCurrentAssociation;
- // Save current application associated
- if (SUCCEEDED(m_pAAR->QueryCurrentDefault(strExt, AT_FILEEXTENSION, AL_EFFECTIVE, &pszCurrentAssociation))) {
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID)) {
- return false;
- }
-
- key.SetStringValue(g_strOldAssoc, pszCurrentAssociation);
-
- // Get current icon for file type
- /*
- if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, CString(pszCurrentAssociation) + _T("\\DefaultIcon")))
- {
- len = sizeof(buff);
- memset(buff, 0, len);
- if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len) && !CString(buff).Trim().IsEmpty())
- {
- if (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\DefaultIcon")))
- key.SetStringValue (NULL, buff);
- }
- }
- */
- CoTaskMemFree(pszCurrentAssociation);
- }
- strNewApp = g_strRegisteredAppName;
- } else {
- if (ERROR_SUCCESS != key.Open(HKEY_CLASSES_ROOT, strProgID)) {
- return false;
- }
-
- if (ERROR_SUCCESS == key.QueryStringValue(g_strOldAssoc, buff, &len)) {
- strNewApp = buff;
- }
-
- // TODO : retrieve registered app name from previous association (or find Bill function for that...)
- }
-
- hr = m_pAAR->SetAppAsDefault(strNewApp, strExt, AT_FILEEXTENSION);
- } else {
- // The 2000/XP way
- if (fRegister) {
- // Set new association
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strExt)) {
- return false;
- }
-
- len = _countof(buff);
- memset(buff, 0, sizeof(buff));
- if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len) && !CString(buff).Trim().IsEmpty()) {
- extoldreg = buff;
- }
- if (ERROR_SUCCESS != key.SetStringValue(NULL, strProgID)) {
- return false;
- }
-
- // Get current icon for file type
- /*
- if (!extoldreg.IsEmpty())
- {
- if (ERROR_SUCCESS == key.Open(HKEY_CLASSES_ROOT, extoldreg + _T("\\DefaultIcon")))
- {
- len = sizeof(buff);
- memset(buff, 0, len);
- if (ERROR_SUCCESS == key.QueryStringValue(NULL, buff, &len) && !CString(buff).Trim().IsEmpty())
- extOldIcon = buff;
- }
- }
- */
-
- // Save old association
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID)) {
- return false;
- }
- key.SetStringValue(g_strOldAssoc, extoldreg);
-
- /*
- if (!extOldIcon.IsEmpty() && (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, strProgID + _T("\\DefaultIcon"))))
- key.SetStringValue (NULL, extOldIcon);
- */
- } else {
- // Get previous association
- len = _countof(buff);
- memset(buff, 0, sizeof(buff));
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strProgID)) {
- return false;
- }
- if (ERROR_SUCCESS == key.QueryStringValue(g_strOldAssoc, buff, &len) && !CString(buff).Trim().IsEmpty()) {
- extoldreg = buff;
- }
-
- // Set previous association
- if (ERROR_SUCCESS != key.Create(HKEY_CLASSES_ROOT, strExt)) {
- return false;
- }
- key.SetStringValue(NULL, extoldreg);
- }
-
- }
-
- return SUCCEEDED(hr);
-}
-
BOOL CPPageFormats::OnApply()
{
UpdateData();
@@ -820,13 +262,13 @@ BOOL CPPageFormats::OnApply() CMediaFormats& mf = AfxGetAppSettings().m_Formats;
- RegisterApp();
+ CFileAssoc::RegisterApp();
- f_setContextFiles = m_fContextFiles.GetCheck();
- f_setAssociatedWithIcon = m_fAssociatedWithIcons.GetCheck();
+ int fSetContextFiles = m_fContextFiles.GetCheck();
+ int fSetAssociatedWithIcon = m_fAssociatedWithIcons.GetCheck();
if (m_bFileExtChanged) {
- if (f_setAssociatedWithIcon && IsNeededIconsLib() && !FileExists(GetProgramDir() + _T("\\mpciconlib.dll"))) {
+ if (fSetAssociatedWithIcon && IsNeededIconsLib() && !FileExists(GetProgramDir() + _T("\\mpciconlib.dll"))) {
AfxMessageBox(IDS_MISSING_ICONS_LIB, MB_ICONEXCLAMATION | MB_OK, 0);
}
@@ -836,48 +278,20 @@ BOOL CPPageFormats::OnApply() continue;
}
- CAtlList<CString> exts;
- Explode(mf[(int)m_list.GetItemData(i)].GetExtsWithPeriod(), exts, ' ');
-
- POSITION pos = exts.GetHeadPosition();
- while (pos) {
- RegisterExt(exts.GetNext(pos), mf[(int)m_list.GetItemData(i)].GetDescription(), !!iChecked);
- }
+ CFileAssoc::Register(mf[(int)m_list.GetItemData(i)], !!iChecked, !!fSetContextFiles, !!fSetAssociatedWithIcon);
}
m_bFileExtChanged = false;
}
- CRegKey key;
- if (m_fContextDir.GetCheck()) {
- if (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, _T("Directory\\shell\\") PROGID _T(".enqueue"))) {
- key.SetStringValue(NULL, ResStr(IDS_ADD_TO_PLAYLIST));
- }
- if (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, _T("Directory\\shell\\") PROGID _T(".enqueue\\command"))) {
- key.SetStringValue(NULL, GetEnqueueCommand());
- }
+ CFileAssoc::RegisterFolderContextMenuEntries(!!m_fContextDir.GetCheck());
- if (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, _T("Directory\\shell\\") PROGID _T(".play"))) {
- key.SetStringValue(NULL, ResStr(IDS_OPEN_WITH_MPC));
- }
-
- if (ERROR_SUCCESS == key.Create(HKEY_CLASSES_ROOT, _T("Directory\\shell\\") PROGID _T(".play\\command"))) {
- key.SetStringValue(NULL, GetOpenCommand());
- }
- } else {
- key.Attach(HKEY_CLASSES_ROOT);
- key.RecurseDeleteKey(_T("Directory\\shell\\") PROGID _T(".enqueue"));
- key.RecurseDeleteKey(_T("Directory\\shell\\") PROGID _T(".play"));
- }
-
- {
- SetListItemState(m_list.GetSelectionMark());
- }
+ SetListItemState(m_list.GetSelectionMark());
- AddAutoPlayToRegistry(AP_VIDEO, !!m_apvideo.GetCheck());
- AddAutoPlayToRegistry(AP_MUSIC, !!m_apmusic.GetCheck());
- AddAutoPlayToRegistry(AP_AUDIOCD, !!m_apaudiocd.GetCheck());
- AddAutoPlayToRegistry(AP_DVDMOVIE, !!m_apdvd.GetCheck());
+ CFileAssoc::RegisterAutoPlay(CFileAssoc::AP_VIDEO, !!m_apvideo.GetCheck());
+ CFileAssoc::RegisterAutoPlay(CFileAssoc::AP_MUSIC, !!m_apmusic.GetCheck());
+ CFileAssoc::RegisterAutoPlay(CFileAssoc::AP_AUDIOCD, !!m_apaudiocd.GetCheck());
+ CFileAssoc::RegisterAutoPlay(CFileAssoc::AP_DVDMOVIE, !!m_apdvd.GetCheck());
AppSettings& s = AfxGetAppSettings();
s.m_Formats.SetRtspHandler(m_iRtspHandler == 0 ? RealMedia : m_iRtspHandler == 1 ? QuickTime : DirectShow, !!m_fRtspFileExtFirst);
diff --git a/src/mpc-hc/PPageFormats.h b/src/mpc-hc/PPageFormats.h index 22a32abb6..459f2de48 100644 --- a/src/mpc-hc/PPageFormats.h +++ b/src/mpc-hc/PPageFormats.h @@ -42,15 +42,7 @@ private: int GetChecked(int iItem);
void SetChecked(int iItem, int fChecked);
- typedef enum {AP_VIDEO = 0, AP_MUSIC, AP_AUDIOCD, AP_DVDMOVIE} autoplay_t;
- void AddAutoPlayToRegistry(autoplay_t ap, bool fRegister);
- bool IsAutoPlayRegistered(autoplay_t ap);
-
void SetListItemState(int nItem);
- static CComPtr<IApplicationAssociationRegistration> m_pAAR;
- static BOOL SetFileAssociation(CString strExt, CString extfile, bool fRegister);
- static CString GetOpenCommand();
- static CString GetEnqueueCommand();
bool IsNeededIconsLib();
@@ -58,11 +50,7 @@ public: CPPageFormats();
virtual ~CPPageFormats();
- static bool RegisterApp();
- static bool IsRegistered(CString ext);
- static bool RegisterExt(CString ext, CString strLabel, bool fRegister);
-
- enum {COL_CATEGORY, COL_ENGINE};
+ enum { COL_CATEGORY, COL_ENGINE };
CPlayerListCtrl m_list;
CString m_exts;
CStatic m_autoplay;
diff --git a/src/mpc-hc/mpc-hc.vcxproj b/src/mpc-hc/mpc-hc.vcxproj index 672cf39a6..89b463960 100644 --- a/src/mpc-hc/mpc-hc.vcxproj +++ b/src/mpc-hc/mpc-hc.vcxproj @@ -171,6 +171,7 @@ <ClCompile Include="FGFilter.cpp" />
<ClCompile Include="FGManager.cpp" />
<ClCompile Include="FGManagerBDA.cpp" />
+ <ClCompile Include="FileAssoc.cpp" />
<ClCompile Include="FileDropTarget.cpp" />
<ClCompile Include="FloatEdit.cpp" />
<ClCompile Include="FullscreenWnd.cpp" />
@@ -285,6 +286,7 @@ <ClInclude Include="FGFilter.h" />
<ClInclude Include="FGManager.h" />
<ClInclude Include="FGManagerBDA.h" />
+ <ClInclude Include="FileAssoc.h" />
<ClInclude Include="FileDropTarget.h" />
<ClInclude Include="FilterEnum.h" />
<ClInclude Include="FloatEdit.h" />
diff --git a/src/mpc-hc/mpc-hc.vcxproj.filters b/src/mpc-hc/mpc-hc.vcxproj.filters index 0ca9492d0..acf029cdb 100644 --- a/src/mpc-hc/mpc-hc.vcxproj.filters +++ b/src/mpc-hc/mpc-hc.vcxproj.filters @@ -354,6 +354,9 @@ <ClCompile Include="MPCPngImage.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="FileAssoc.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="AppSettings.h">
@@ -704,6 +707,9 @@ <ClInclude Include="MPCPngImage.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="FileAssoc.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="res\ani.avi">
diff --git a/src/mpc-hc/mplayerc.cpp b/src/mpc-hc/mplayerc.cpp index 23da1a2de..4d835488a 100644 --- a/src/mpc-hc/mplayerc.cpp +++ b/src/mpc-hc/mplayerc.cpp @@ -33,6 +33,7 @@ #include "Ifo.h"
#include "Monitors.h"
#include "WinAPIUtils.h"
+#include "FileAssoc.h"
#include "UpdateChecker.h"
#include "winddk/ntddcdvd.h"
#include "detours/detours.h"
@@ -923,7 +924,7 @@ BOOL CMPlayerCApp::InitInstance() }
if (m_s.nCLSwitches & (CLSW_REGEXTVID | CLSW_REGEXTAUD | CLSW_REGEXTPL)) { // register file types
- CPPageFormats::RegisterApp();
+ CFileAssoc::RegisterApp();
CMediaFormats& mf = m_s.m_Formats;
mf.UpdateData(false);
@@ -939,32 +940,26 @@ BOOL CMPlayerCApp::InitInstance() bAudioOnly = mf[i].IsAudioOnly();
- int j = 0;
- CString str = mf[i].GetExtsWithPeriod();
- for (CString ext = str.Tokenize(_T(" "), j); !ext.IsEmpty(); ext = str.Tokenize(_T(" "), j)) {
- if (((m_s.nCLSwitches & CLSW_REGEXTVID) && !bAudioOnly) ||
- ((m_s.nCLSwitches & CLSW_REGEXTAUD) && bAudioOnly) ||
- ((m_s.nCLSwitches & CLSW_REGEXTPL) && bPlaylist)) {
- CPPageFormats::RegisterExt(ext, mf[i].GetDescription(), true);
- }
+ if (((m_s.nCLSwitches & CLSW_REGEXTVID) && !bAudioOnly) ||
+ ((m_s.nCLSwitches & CLSW_REGEXTAUD) && bAudioOnly) ||
+ ((m_s.nCLSwitches & CLSW_REGEXTPL) && bPlaylist)) {
+ CFileAssoc::Register(mf[i], true, false, true);
}
}
+
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
return FALSE;
}
- if ((m_s.nCLSwitches & CLSW_UNREGEXT)) { // unregistered file types
+ if (m_s.nCLSwitches & CLSW_UNREGEXT) { // unregistered file types
CMediaFormats& mf = m_s.m_Formats;
mf.UpdateData(false);
for (unsigned int i = 0; i < mf.GetCount(); i++) {
- int j = 0;
- CString str = mf[i].GetExtsWithPeriod();
- for (CString ext = str.Tokenize(_T(" "), j); !ext.IsEmpty(); ext = str.Tokenize(_T(" "), j)) {
- CPPageFormats::RegisterExt(ext, mf[i].GetDescription(), false);
- }
+ CFileAssoc::Register(mf[i], false, false, false);
}
+
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
return FALSE;
|