CommitDlg: Update empty file list message
[TortoiseGit.git] / src / Utils / Libraries.cpp
blobf5d7747bc48dd680be3f476f5ec858b60aa74165
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.
19 #include "stdafx.h"
20 #include "Libraries.h"
21 #include "PathUtils.h"
22 #include "resource.h"
23 #include <initguid.h>
24 #include <propkeydef.h>
25 #include "SmartHandle.h"
27 #ifdef _WIN64
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);
32 #else
33 DEFINE_GUID(FOLDERTYPEID_GITWC, 0xb118c031, 0xa977, 0x4a67, 0x93, 0x44, 0x47, 0xf0, 0x57, 0x38, 0x81, 0x5);
34 #endif
36 /**
37 * Makes sure a library named "Git" exists and has our template
38 * set to it.
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);
49 if (bIsWow64)
50 return;
52 CComPtr<IShellLibrary> pLibrary = NULL;
53 if (FAILED(OpenShellLibrary(L"Git", &pLibrary)))
55 if (!bCreate)
56 return;
57 if (FAILED(SHCreateLibrary(IID_PPV_ARGS(&pLibrary))))
58 return;
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)))
63 return;
66 if (SUCCEEDED(pLibrary->SetFolderType(FOLDERTYPEID_GITWC)))
68 // create the path for the icon
69 CString path;
70 CString appDir = CPathUtils::GetAppDirectory();
71 if (appDir.GetLength() < MAX_PATH)
73 TCHAR buf[MAX_PATH] = {0};
74 PathCanonicalize(buf, (LPCTSTR)appDir);
75 appDir = buf;
77 path.Format(_T("%s%s,-%d"), (LPCTSTR)appDir, _T("TortoiseGitProc.exe"), IDI_LIBRARY);
78 pLibrary->SetIcon((LPCTSTR)path);
79 pLibrary->Commit();
83 /**
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.
90 * \param ppShellLib
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
94 * *ppShellLib.
96 HRESULT OpenShellLibrary(LPWSTR pwszLibraryName, IShellLibrary** ppShellLib)
98 HRESULT hr;
99 *ppShellLib = NULL;
101 IShellItem2* pShellItem = NULL;
102 hr = GetShellLibraryItem(pwszLibraryName, &pShellItem);
103 if (FAILED(hr))
104 return hr;
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();
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, NULL is returned from
124 * *ppShellItem.
126 HRESULT GetShellLibraryItem(LPWSTR pwszLibraryName, IShellItem2** ppShellItem)
128 HRESULT hr = E_NOINTERFACE;
129 *ppShellItem = NULL;
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"));
137 if (hShell)
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));
146 return hr;