1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2010-2012 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "Libraries.h"
21 #include "PathUtils.h"
24 #include <propkeydef.h>
25 #include "SmartHandle.h"
28 // {DC9E616B-7611-461c-9D37-730FDD4CE278}
29 DEFINE_GUID(FOLDERTYPEID_GITWC
, 0xdc9e616b, 0x7611, 0x461c, 0x9d, 0x37, 0x73, 0xf, 0xdd, 0x4c, 0xe2, 0x78);
30 // {B118C031-A977-4a67-9344-47F057388105}
31 DEFINE_GUID(FOLDERTYPEID_GITWC32
, 0xb118c031, 0xa977, 0x4a67, 0x93, 0x44, 0x47, 0xf0, 0x57, 0x38, 0x81, 0x5);
33 DEFINE_GUID(FOLDERTYPEID_GITWC
, 0xb118c031, 0xa977, 0x4a67, 0x93, 0x44, 0x47, 0xf0, 0x57, 0x38, 0x81, 0x5);
37 * Makes sure a library named "Git" exists and has our template
39 * If the library already exists, the template is set.
40 * If the library doesn't exist, it is created.
42 void EnsureGitLibrary(bool bCreate
/* = true*/)
44 // when running the 32-bit version of TortoiseProc on x64 OS,
45 // we must not create the library! This would break
46 // the library in the x64 explorer.
47 BOOL bIsWow64
= FALSE
;
48 IsWow64Process(GetCurrentProcess(), &bIsWow64
);
52 CComPtr
<IShellLibrary
> pLibrary
= NULL
;
53 if (FAILED(OpenShellLibrary(L
"Git", &pLibrary
)))
57 if (FAILED(SHCreateLibrary(IID_PPV_ARGS(&pLibrary
))))
60 // Save the new library under the user's Libraries folder.
61 CComPtr
<IShellItem
> pSavedTo
= NULL
;
62 if (FAILED(pLibrary
->SaveInKnownFolder(FOLDERID_UsersLibraries
, L
"Git", LSF_OVERRIDEEXISTING
, &pSavedTo
)))
66 if (SUCCEEDED(pLibrary
->SetFolderType(FOLDERTYPEID_GITWC
)))
68 // create the path for the icon
70 CString appDir
= CPathUtils::GetAppDirectory();
71 if (appDir
.GetLength() < MAX_PATH
)
73 TCHAR buf
[MAX_PATH
] = {0};
74 PathCanonicalize(buf
, (LPCTSTR
)appDir
);
77 path
.Format(_T("%s%s,-%d"), (LPCTSTR
)appDir
, _T("TortoiseGitProc.exe"), IDI_LIBRARY
);
78 pLibrary
->SetIcon((LPCTSTR
)path
);
84 * Open the shell library under the user's Libraries folder according to the
85 * specified library name with both read and write permissions.
87 * \param pwszLibraryName
88 * The name of the shell library to be opened.
91 * If the open operation succeeds, ppShellLib outputs the IShellLibrary
92 * interface of the shell library object. The caller is responsible for calling
93 * Release on the shell library. If the function fails, NULL is returned from
96 HRESULT
OpenShellLibrary(LPWSTR pwszLibraryName
, IShellLibrary
** ppShellLib
)
101 IShellItem2
* pShellItem
= NULL
;
102 hr
= GetShellLibraryItem(pwszLibraryName
, &pShellItem
);
106 // Get the shell library object from the shell item with a read and write permissions
107 hr
= SHLoadLibraryFromItem(pShellItem
, STGM_READWRITE
, IID_PPV_ARGS(ppShellLib
));
109 pShellItem
->Release();
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, NULL is returned from
126 HRESULT
GetShellLibraryItem(LPWSTR pwszLibraryName
, IShellItem2
** ppShellItem
)
128 HRESULT hr
= E_NOINTERFACE
;
131 // Create the real library file name
132 WCHAR wszRealLibraryName
[MAX_PATH
] = {0};
133 swprintf_s(wszRealLibraryName
, L
"%s%s", pwszLibraryName
, L
".library-ms");
135 typedef HRESULT STDAPICALLTYPE
SHCreateItemInKnownFolderFN(REFKNOWNFOLDERID kfid
, DWORD dwKFFlags
, __in_opt PCWSTR pszItem
, REFIID riid
, __deref_out
void **ppv
);
136 CAutoLibrary hShell
= AtlLoadSystemLibraryUsingFullPath(_T("shell32.dll"));
139 SHCreateItemInKnownFolderFN
*pfnSHCreateItemInKnownFolder
= (SHCreateItemInKnownFolderFN
*)GetProcAddress(hShell
, "SHCreateItemInKnownFolder");
140 if (pfnSHCreateItemInKnownFolder
)
142 hr
= pfnSHCreateItemInKnownFolder(FOLDERID_UsersLibraries
, KF_FLAG_DEFAULT_PATH
| KF_FLAG_NO_ALIAS
, wszRealLibraryName
, IID_PPV_ARGS(ppShellItem
));