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.
21 #include "Libraries.h"
22 #include "PathUtils.h"
25 #include <propkeydef.h>
26 #include "SmartHandle.h"
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);
35 DEFINE_GUID(FOLDERTYPEID_GITWC
, 0xb118c031, 0xa977, 0x4a67, 0x93, 0x44, 0x47, 0xf0, 0x57, 0x38, 0x81, 0x5);
39 * Makes sure a library named "Git" exists and has our template
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
);
54 CComPtr
<IShellLibrary
> pLibrary
;
55 if (FAILED(OpenShellLibrary(L
"Git", &pLibrary
)))
59 if (FAILED(SHCreateLibrary(IID_PPV_ARGS(&pLibrary
))))
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
)))
68 if (SUCCEEDED(pLibrary
->SetFolderType(SysInfo::Instance().IsWin8OrLater() ? FOLDERTYPEID_Documents
: FOLDERTYPEID_GITWC
)))
70 // create the path for the icon
72 CString appDir
= CPathUtils::GetAppDirectory();
73 if (appDir
.GetLength() < MAX_PATH
)
75 TCHAR buf
[MAX_PATH
] = {0};
76 PathCanonicalize(buf
, (LPCTSTR
)appDir
);
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
);
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.
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
98 HRESULT
OpenShellLibrary(LPWSTR pwszLibraryName
, IShellLibrary
** ppShellLib
)
101 *ppShellLib
= nullptr;
103 CComPtr
<IShellItem2
> pShellItem
;
104 hr
= GetShellLibraryItem(pwszLibraryName
, &pShellItem
);
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
));
115 * Get the shell item that represents the library.
117 * \param pwszLibraryName
118 * The name of the shell library
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
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
));