Reduce C style casts
[TortoiseGit.git] / src / TortoiseShell / ShellExt.cpp
blob7d6f88497ffd27c8f7522af2dc3d5ab946d358b6
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011-2016 - TortoiseGit
4 // Copyright (C) 2003-2014 - 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"
22 // Initialize GUIDs (should be done only and at-least once per DLL/EXE)
23 #include <initguid.h>
24 #include "Guids.h"
26 #include "ShellExt.h"
27 #include "ShellObjects.h"
28 #include "..\version.h"
29 #undef swprintf
31 extern ShellObjects g_shellObjects;
33 // *********************** CShellExt *************************
34 CShellExt::CShellExt(FileState state)
35 : m_State(state)
36 , itemStates(0)
37 , itemStatesFolder(0)
38 , space(0)
39 #if ENABLE_CRASHHANLDER
40 , m_crasher(L"TortoiseGit", TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD, TGIT_VERDATE, false)
41 #endif
42 ,regDiffLater(L"Software\\TortoiseGit\\DiffLater", L"")
44 m_cRef = 0L;
45 InterlockedIncrement(&g_cRefThisDll);
48 AutoLocker lock(g_csGlobalCOMGuard);
49 g_shellObjects.Insert(this);
52 INITCOMMONCONTROLSEX used = {
53 sizeof(INITCOMMONCONTROLSEX),
54 ICC_LISTVIEW_CLASSES | ICC_WIN95_CLASSES | ICC_BAR_CLASSES | ICC_USEREX_CLASSES
56 InitCommonControlsEx(&used);
57 LoadLangDll();
60 CShellExt::~CShellExt()
62 AutoLocker lock(g_csGlobalCOMGuard);
63 InterlockedDecrement(&g_cRefThisDll);
64 g_shellObjects.Erase(this);
67 void LoadLangDll()
69 if (g_langid != g_ShellCache.GetLangID() && (g_langTimeout == 0 || g_langTimeout < GetTickCount64()))
71 g_langid = g_ShellCache.GetLangID();
72 DWORD langId = g_langid;
73 TCHAR langDll[MAX_PATH*4] = {0};
74 HINSTANCE hInst = nullptr;
75 TCHAR langdir[MAX_PATH] = {0};
76 char langdirA[MAX_PATH] = {0};
77 if (GetModuleFileName(g_hmodThisDll, langdir, _countof(langdir))==0)
78 return;
79 if (GetModuleFileNameA(g_hmodThisDll, langdirA, _countof(langdirA))==0)
80 return;
81 TCHAR * dirpoint = _tcsrchr(langdir, '\\');
82 char * dirpointA = strrchr(langdirA, '\\');
83 if (dirpoint)
84 *dirpoint = 0;
85 if (dirpointA)
86 *dirpointA = 0;
87 dirpoint = _tcsrchr(langdir, '\\');
88 dirpointA = strrchr(langdirA, '\\');
89 if (dirpoint)
90 *dirpoint = 0;
91 if (dirpointA)
92 *dirpointA = 0;
93 strcat_s(langdirA, "\\Languages");
95 BOOL bIsWow = FALSE;
96 IsWow64Process(GetCurrentProcess(), &bIsWow);
100 if (bIsWow)
101 _stprintf_s(langDll, _T("%s\\Languages\\TortoiseProc32%lu.dll"), langdir, langId);
102 else
103 _stprintf_s(langDll, _T("%s\\Languages\\TortoiseProc%lu.dll"), langdir, langId);
104 BOOL versionmatch = TRUE;
106 struct TRANSARRAY
108 WORD wLanguageID;
109 WORD wCharacterSet;
112 DWORD dwReserved,dwBufferSize;
113 dwBufferSize = GetFileVersionInfoSize((LPTSTR)langDll,&dwReserved);
115 if (dwBufferSize > 0)
117 LPVOID pBuffer = (void*) malloc(dwBufferSize);
119 if (pBuffer)
121 UINT nInfoSize = 0;
122 UINT nFixedLength = 0;
123 LPSTR lpVersion = nullptr;
124 VOID* lpFixedPointer;
126 if (GetFileVersionInfo((LPTSTR)langDll,
127 dwReserved,
128 dwBufferSize,
129 pBuffer))
131 // Query the current language
132 if (VerQueryValue( pBuffer,
133 _T("\\VarFileInfo\\Translation"),
134 &lpFixedPointer,
135 &nFixedLength))
137 auto lpTransArray = reinterpret_cast<TRANSARRAY*>(lpFixedPointer);
138 TCHAR strLangProductVersion[MAX_PATH] = { 0 };
140 _stprintf_s(strLangProductVersion, _T("\\StringFileInfo\\%04x%04x\\ProductVersion"),
141 lpTransArray[0].wLanguageID, lpTransArray[0].wCharacterSet);
143 if (VerQueryValue(pBuffer, (LPTSTR)strLangProductVersion, (LPVOID*)&lpVersion, &nInfoSize))
144 versionmatch = (_tcscmp((LPCTSTR)lpVersion, _T(STRPRODUCTVER)) == 0);
148 free(pBuffer);
149 } // if (pBuffer)
150 } // if (dwBufferSize > 0)
151 else
152 versionmatch = FALSE;
154 if (versionmatch)
155 hInst = LoadLibrary(langDll);
156 if (hInst)
158 if (g_hResInst != g_hmodThisDll)
159 FreeLibrary(g_hResInst);
160 g_hResInst = hInst;
162 else
164 DWORD lid = SUBLANGID(langId);
165 lid--;
166 if (lid > 0)
167 langId = MAKELANGID(PRIMARYLANGID(langId), lid);
168 else
169 langId = 0;
171 } while (!hInst && langId != 0);
172 if (!hInst)
174 // either the dll for the selected language is not present, or
175 // it is the wrong version.
176 // fall back to English and set a timeout so we don't retry
177 // to load the language dll too often
178 if (g_hResInst != g_hmodThisDll)
179 FreeLibrary(g_hResInst);
180 g_hResInst = g_hmodThisDll;
181 g_langid = 1033;
182 // set a timeout of 10 seconds
183 if (g_ShellCache.GetLangID() != 1033)
184 g_langTimeout = GetTickCount64() + 10000;
186 else
187 g_langTimeout = 0;
188 } // if (g_langid != g_ShellCache.GetLangID())
191 STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
193 if (!ppv)
194 return E_POINTER;
196 *ppv = nullptr;
198 if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
199 *ppv = static_cast<LPSHELLEXTINIT>(this);
200 else if (IsEqualIID(riid, IID_IContextMenu))
201 *ppv = static_cast<LPCONTEXTMENU>(this);
202 else if (IsEqualIID(riid, IID_IContextMenu2))
203 *ppv = static_cast<LPCONTEXTMENU2>(this);
204 else if (IsEqualIID(riid, IID_IContextMenu3))
205 *ppv = static_cast<LPCONTEXTMENU3>(this);
206 else if (IsEqualIID(riid, IID_IShellIconOverlayIdentifier))
207 *ppv = static_cast<IShellIconOverlayIdentifier*>(this);
208 else if (IsEqualIID(riid, IID_IShellPropSheetExt))
209 *ppv = static_cast<LPSHELLPROPSHEETEXT>(this);
210 else if (IsEqualIID(riid, IID_IShellCopyHook))
211 *ppv = static_cast<ICopyHook*>(this);
212 else
213 return E_NOINTERFACE;
215 AddRef();
216 return S_OK;
219 STDMETHODIMP_(ULONG) CShellExt::AddRef()
221 return InterlockedIncrement(&m_cRef);
224 STDMETHODIMP_(ULONG) CShellExt::Release()
226 if (InterlockedDecrement(&m_cRef))
227 return m_cRef;
229 delete this;
231 return 0L;
234 // IPersistFile members
235 STDMETHODIMP CShellExt::GetClassID(CLSID *pclsid)
237 if (!pclsid)
238 return E_POINTER;
239 *pclsid = CLSID_Tortoisegit_UNCONTROLLED;
240 return S_OK;
243 STDMETHODIMP CShellExt::Load(LPCOLESTR /*pszFileName*/, DWORD /*dwMode*/)
245 return S_OK;
248 // ICopyHook member
249 UINT __stdcall CShellExt::CopyCallback(HWND hWnd, UINT wFunc, UINT wFlags, LPCTSTR pszSrcFile, DWORD dwSrcAttribs, LPCTSTR pszDestFile, DWORD dwDestAttribs)
251 __try
253 return CopyCallback_Wrap(hWnd, wFunc, wFlags, pszSrcFile, dwSrcAttribs, pszDestFile, dwDestAttribs);
255 __except(CCrashReport::Instance().SendReport(GetExceptionInformation()))
258 return IDYES;
261 UINT __stdcall CShellExt::CopyCallback_Wrap(HWND /*hWnd*/, UINT wFunc, UINT /*wFlags*/, LPCTSTR pszSrcFile, DWORD /*dwSrcAttribs*/, LPCTSTR /*pszDestFile*/, DWORD /*dwDestAttribs*/)
263 switch (wFunc)
265 case FO_MOVE:
266 case FO_DELETE:
267 case FO_RENAME:
268 if (pszSrcFile && pszSrcFile[0])
270 CString topDir;
271 if (GitAdminDir::HasAdminDir(pszSrcFile, &topDir))
272 m_CachedStatus.m_GitStatus.ReleasePath(topDir);
273 m_remoteCacheLink.ReleaseLockForPath(CTGitPath(pszSrcFile));
275 break;
276 default:
277 break;
280 // we could now wait a little bit to give the cache time to release the handles.
281 // but the explorer/shell already retries any action for about two seconds
282 // if it first fails. So if the cache hasn't released the handle yet, the explorer
283 // will retry anyway, so we just leave here immediately.
284 return IDYES;