Applied backgroundcolors.patch
[TortoiseGit.git] / src / Utils / Libraries.cpp
blob7c0e186c37386cf37c59aa6093ab69664e1f90f2
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2012, 2015-2016 - TortoiseGit
4 // Copyright (C) 2010-2012, 2016 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
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.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "Libraries.h"
22 #include "PathUtils.h"
23 #include "resource.h"
24 #include <initguid.h>
25 #include <propkeydef.h>
26 #include "SmartHandle.h"
27 #include "SysInfo.h"
29 #ifdef _WIN64
30 // {DC9E616B-7611-461c-9D37-730FDD4CE278}
31 DEFINE_GUID(FOLDERTYPEID_GITWC, 0xdc9e616b, 0x7611, 0x461c, 0x9d, 0x37, 0x73, 0xf, 0xdd, 0x4c, 0xe2, 0x78);
32 // {B118C031-A977-4a67-9344-47F057388105}
33 DEFINE_GUID(FOLDERTYPEID_GITWC32, 0xb118c031, 0xa977, 0x4a67, 0x93, 0x44, 0x47, 0xf0, 0x57, 0x38, 0x81, 0x5);
34 #else
35 DEFINE_GUID(FOLDERTYPEID_GITWC, 0xb118c031, 0xa977, 0x4a67, 0x93, 0x44, 0x47, 0xf0, 0x57, 0x38, 0x81, 0x5);
36 #endif
38 /**
39 * Makes sure a library named "Git" exists and has our template
40 * set to it.
41 * If the library already exists, the template is set.
42 * If the library doesn't exist, it is created.
44 void EnsureGitLibrary(bool bCreate /* = true*/)
46 // when running the 32-bit version of TortoiseProc on x64 OS,
47 // we must not create the library! This would break
48 // the library in the x64 explorer.
49 BOOL bIsWow64 = FALSE;
50 IsWow64Process(GetCurrentProcess(), &bIsWow64);
51 if (bIsWow64)
52 return;
54 CComPtr<IShellLibrary> pLibrary;
55 if (FAILED(OpenShellLibrary(L"Git", &pLibrary)))
57 if (!bCreate)
58 return;
59 if (FAILED(SHCreateLibrary(IID_PPV_ARGS(&pLibrary))))
60 return;
62 // Save the new library under the user's Libraries folder.
63 CComPtr<IShellItem> pSavedTo;
64 if (FAILED(pLibrary->SaveInKnownFolder(FOLDERID_UsersLibraries, L"Git", LSF_OVERRIDEEXISTING, &pSavedTo)))
65 return;
68 if (SUCCEEDED(pLibrary->SetFolderType(SysInfo::Instance().IsWin8OrLater() ? FOLDERTYPEID_Documents : FOLDERTYPEID_GITWC)))
70 // create the path for the icon
71 CString path;
72 CString appDir = CPathUtils::GetAppDirectory();
73 if (appDir.GetLength() < MAX_PATH)
75 TCHAR buf[MAX_PATH] = {0};
76 PathCanonicalize(buf, (LPCTSTR)appDir);
77 appDir = buf;
79 path.Format(L"%s%s,-%d", (LPCTSTR)appDir, L"TortoiseGitProc.exe", SysInfo::Instance().IsWin10() ? IDI_LIBRARY_WIN10 : IDI_LIBRARY);
80 pLibrary->SetIcon((LPCTSTR)path);
81 pLibrary->Commit();
85 /**
86 * Open the shell library under the user's Libraries folder according to the
87 * specified library name with both read and write permissions.
89 * \param pwszLibraryName
90 * The name of the shell library to be opened.
92 * \param ppShellLib
93 * If the open operation succeeds, ppShellLib outputs the IShellLibrary
94 * interface of the shell library object. The caller is responsible for calling
95 * Release on the shell library. If the function fails, nullptr is returned from
96 * *ppShellLib.
98 HRESULT OpenShellLibrary(LPWSTR pwszLibraryName, IShellLibrary** ppShellLib)
100 HRESULT hr;
101 *ppShellLib = nullptr;
103 CComPtr<IShellItem2> pShellItem;
104 hr = GetShellLibraryItem(pwszLibraryName, &pShellItem);
105 if (FAILED(hr))
106 return hr;
108 // Get the shell library object from the shell item with a read and write permissions
109 hr = SHLoadLibraryFromItem(pShellItem, STGM_READWRITE, IID_PPV_ARGS(ppShellLib));
111 return hr;
115 * Get the shell item that represents the library.
117 * \param pwszLibraryName
118 * The name of the shell library
120 * \param ppShellItem
121 * If the operation succeeds, ppShellItem outputs the IShellItem2 interface
122 * that represents the library. The caller is responsible for calling
123 * Release on the shell item. If the function fails, nullptr is returned from
124 * *ppShellItem.
126 HRESULT GetShellLibraryItem(LPWSTR pwszLibraryName, IShellItem2** ppShellItem)
128 *ppShellItem = nullptr;
130 // Create the real library file name
131 WCHAR wszRealLibraryName[MAX_PATH] = {0};
132 swprintf_s(wszRealLibraryName, L"%s%s", pwszLibraryName, L".library-ms");
134 return SHCreateItemInKnownFolder(FOLDERID_UsersLibraries, KF_FLAG_DEFAULT_PATH | KF_FLAG_NO_ALIAS, wszRealLibraryName, IID_PPV_ARGS(ppShellItem));