fixed the ContextMenuStash.png screenshot
[TortoiseGit.git] / src / Utils / Libraries.cpp
blob5d75ed6a999eae2e8a338152572fe4e86ab39432
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2010 - 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 #if (NTDDI_VERSION < 0x06010000)
27 #define INITGUID
29 #ifdef _WIN64
30 DEFINE_GUID(FOLDERTYPEID_GITWC, 0xcacb1c79, 0xeafd, 0x4a4c, 0x94, 0x38, 0x38, 0xc0, 0x23, 0x95, 0x2d, 0x97));
31 #else
32 DEFINE_GUID(FOLDERTYPEID_GITWC, 0xd1d4f493, 0x832d, 0x4847, 0x8e, 0xfd, 0xf0, 0x6c, 0x4c, 0x75, 0xd0, 0xd2);
33 #endif
35 #endif /* (NTDDI_VERSION < NTDDI_WIN7) */
37 #include "StdAfx.h"
38 #include "Libraries.h"
39 #include "win7.h"
41 /**
42 * Makes sure a library named "Subversion" exists and has our template
43 * set to it.
44 * If the library already exists, the template is set.
45 * If the library doesn't exist, it is created.
47 void EnsureSVNLibrary()
49 CComPtr<IShellLibrary> pLibrary = NULL;
50 if (FAILED(OpenShellLibrary(L"Subversion", &pLibrary)))
52 if (FAILED(SHCreateLibrary(IID_PPV_ARGS(&pLibrary))))
53 return;
55 // Save the new library under the user's Libraries folder.
56 CComPtr<IShellItem> pSavedTo = NULL;
57 if (FAILED(pLibrary->SaveInKnownFolder(FOLDERID_UsersLibraries, L"Subversion", LSF_OVERRIDEEXISTING, &pSavedTo)))
58 return;
61 if (SUCCEEDED(pLibrary->SetFolderType(FOLDERTYPEID_GITWC)))
63 // create the path for the icon
64 CString path;
65 CString appDir = CPathUtils::GetAppDirectory();
66 if (appDir.GetLength() < MAX_PATH)
68 TCHAR buf[MAX_PATH] = {0};
69 PathCanonicalize(buf, (LPCTSTR)appDir);
70 appDir = buf;
72 path.Format(_T("%s%s,-%d"), (LPCTSTR)appDir, _T("TortoiseProc.exe"), IDI_LIBRARY);
73 pLibrary->SetIcon((LPCTSTR)path);
74 pLibrary->Commit();
78 /**
79 * Open the shell library under the user's Libraries folder according to the
80 * specified library name with both read and write permissions.
82 * \param pwszLibraryName
83 * The name of the shell library to be opened.
85 * \param ppShellLib
86 * If the open operation succeeds, ppShellLib outputs the IShellLibrary
87 * interface of the shell library object. The caller is responsible for calling
88 * Release on the shell library. If the function fails, NULL is returned from
89 * *ppShellLib.
91 HRESULT OpenShellLibrary(LPWSTR pwszLibraryName, IShellLibrary** ppShellLib)
93 HRESULT hr;
94 *ppShellLib = NULL;
96 IShellItem2* pShellItem = NULL;
97 hr = GetShellLibraryItem(pwszLibraryName, &pShellItem);
98 if (FAILED(hr))
99 return hr;
101 // Get the shell library object from the shell item with a read and write permissions
102 hr = SHLoadLibraryFromItem(pShellItem, STGM_READWRITE, IID_PPV_ARGS(ppShellLib));
104 pShellItem->Release();
106 return hr;
110 * Get the shell item that represents the library.
112 * \param pwszLibraryName
113 * The name of the shell library
115 * \param ppShellItem
116 * If the operation succeeds, ppShellItem outputs the IShellItem2 interface
117 * that represents the library. The caller is responsible for calling
118 * Release on the shell item. If the function fails, NULL is returned from
119 * *ppShellItem.
121 HRESULT GetShellLibraryItem(LPWSTR pwszLibraryName, IShellItem2** ppShellItem)
123 HRESULT hr = E_NOINTERFACE;
124 *ppShellItem = NULL;
126 // Create the real library file name
127 WCHAR wszRealLibraryName[MAX_PATH];
128 swprintf_s(wszRealLibraryName, MAX_PATH, L"%s%s", pwszLibraryName, L".library-ms");
130 typedef HRESULT STDAPICALLTYPE SHCreateItemInKnownFolderFN(REFKNOWNFOLDERID kfid, DWORD dwKFFlags, __in_opt PCWSTR pszItem, REFIID riid, __deref_out void **ppv);
131 HMODULE hShell = ::LoadLibrary(_T("shell32.dll"));
132 if (hShell)
134 SHCreateItemInKnownFolderFN *pfnSHCreateItemInKnownFolder = (SHCreateItemInKnownFolderFN*)GetProcAddress(hShell, "SHCreateItemInKnownFolder");
135 if (pfnSHCreateItemInKnownFolder)
137 hr = pfnSHCreateItemInKnownFolder(FOLDERID_UsersLibraries, KF_FLAG_DEFAULT_PATH | KF_FLAG_NO_ALIAS, wszRealLibraryName, IID_PPV_ARGS(ppShellItem));
139 FreeLibrary(hShell);
142 return hr;