Drop ColumnProvider support
[TortoiseGit.git] / src / TortoiseShell / ShellExt.cpp
blob0eb52d4638689547cdac1b9ebbabbe10cd521838
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011-2013 - TortoiseGit
4 // Copyright (C) 2003-2013 - 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 #if ENABLE_CRASHHANLDER
36 : m_crasher(L"TortoiseGit", TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD, TGIT_VERDATE, false)
37 #else
38 : m_State(state)
39 #endif
40 ,regDiffLater(L"Software\\TortoiseGit\\DiffLater", L"")
42 m_State = state;
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 < GetTickCount())))
71 g_langid = g_ShellCache.GetLangID();
72 DWORD langId = g_langid;
73 TCHAR langDll[MAX_PATH*4];
74 HINSTANCE hInst = NULL;
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");
94 // bindtextdomain ("subversion", langdirA);
96 BOOL bIsWow = FALSE;
97 IsWow64Process(GetCurrentProcess(), &bIsWow);
101 if (bIsWow)
102 _stprintf_s(langDll, _T("%s\\Languages\\TortoiseProc32%lu.dll"), langdir, langId);
103 else
104 _stprintf_s(langDll, _T("%s\\Languages\\TortoiseProc%lu.dll"), langdir, langId);
105 BOOL versionmatch = TRUE;
107 struct TRANSARRAY
109 WORD wLanguageID;
110 WORD wCharacterSet;
113 DWORD dwReserved,dwBufferSize;
114 dwBufferSize = GetFileVersionInfoSize((LPTSTR)langDll,&dwReserved);
116 if (dwBufferSize > 0)
118 LPVOID pBuffer = (void*) malloc(dwBufferSize);
120 if (pBuffer != (void*) NULL)
122 UINT nInfoSize = 0;
123 UINT nFixedLength = 0;
124 LPSTR lpVersion = NULL;
125 VOID* lpFixedPointer;
126 TRANSARRAY* lpTransArray;
127 TCHAR strLangProductVersion[MAX_PATH];
129 if (GetFileVersionInfo((LPTSTR)langDll,
130 dwReserved,
131 dwBufferSize,
132 pBuffer))
134 // Query the current language
135 if (VerQueryValue( pBuffer,
136 _T("\\VarFileInfo\\Translation"),
137 &lpFixedPointer,
138 &nFixedLength))
140 lpTransArray = (TRANSARRAY*) lpFixedPointer;
142 _stprintf_s(strLangProductVersion, _T("\\StringFileInfo\\%04x%04x\\ProductVersion"),
143 lpTransArray[0].wLanguageID, lpTransArray[0].wCharacterSet);
145 if (VerQueryValue(pBuffer,
146 (LPTSTR)strLangProductVersion,
147 (LPVOID *)&lpVersion,
148 &nInfoSize))
150 versionmatch = (_tcscmp((LPCTSTR)lpVersion, _T(STRPRODUCTVER)) == 0);
155 free(pBuffer);
156 } // if (pBuffer != (void*) NULL)
157 } // if (dwBufferSize > 0)
158 else
159 versionmatch = FALSE;
161 if (versionmatch)
162 hInst = LoadLibrary(langDll);
163 if (hInst != NULL)
165 if (g_hResInst != g_hmodThisDll)
166 FreeLibrary(g_hResInst);
167 g_hResInst = hInst;
169 else
171 DWORD lid = SUBLANGID(langId);
172 lid--;
173 if (lid > 0)
175 langId = MAKELANGID(PRIMARYLANGID(langId), lid);
177 else
178 langId = 0;
180 } while ((hInst == NULL) && (langId != 0));
181 if (hInst == NULL)
183 // either the dll for the selected language is not present, or
184 // it is the wrong version.
185 // fall back to English and set a timeout so we don't retry
186 // to load the language dll too often
187 if (g_hResInst != g_hmodThisDll)
188 FreeLibrary(g_hResInst);
189 g_hResInst = g_hmodThisDll;
190 g_langid = 1033;
191 // set a timeout of 10 seconds
192 if (g_ShellCache.GetLangID() != 1033)
193 g_langTimeout = GetTickCount() + 10000;
195 else
196 g_langTimeout = 0;
197 } // if (g_langid != g_ShellCache.GetLangID())
200 STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
202 if(ppv == 0)
203 return E_POINTER;
205 *ppv = NULL;
207 if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
209 *ppv = static_cast<LPSHELLEXTINIT>(this);
211 else if (IsEqualIID(riid, IID_IContextMenu))
213 *ppv = static_cast<LPCONTEXTMENU>(this);
215 else if (IsEqualIID(riid, IID_IContextMenu2))
217 *ppv = static_cast<LPCONTEXTMENU2>(this);
219 else if (IsEqualIID(riid, IID_IContextMenu3))
221 *ppv = static_cast<LPCONTEXTMENU3>(this);
223 else if (IsEqualIID(riid, IID_IShellIconOverlayIdentifier))
225 *ppv = static_cast<IShellIconOverlayIdentifier*>(this);
227 else if (IsEqualIID(riid, IID_IShellPropSheetExt))
229 *ppv = static_cast<LPSHELLPROPSHEETEXT>(this);
231 else if (IsEqualIID(riid, IID_IShellCopyHook))
233 *ppv = static_cast<ICopyHook*>(this);
235 else
237 return E_NOINTERFACE;
240 AddRef();
241 return S_OK;
244 STDMETHODIMP_(ULONG) CShellExt::AddRef()
246 return ++m_cRef;
249 STDMETHODIMP_(ULONG) CShellExt::Release()
251 if (--m_cRef)
252 return m_cRef;
254 delete this;
256 return 0L;
259 // IPersistFile members
260 STDMETHODIMP CShellExt::GetClassID(CLSID *pclsid)
262 if(pclsid == 0)
263 return E_POINTER;
264 *pclsid = CLSID_Tortoisegit_UNCONTROLLED;
265 return S_OK;
268 STDMETHODIMP CShellExt::Load(LPCOLESTR /*pszFileName*/, DWORD /*dwMode*/)
270 return S_OK;
273 // ICopyHook member
274 UINT __stdcall CShellExt::CopyCallback(HWND hWnd, UINT wFunc, UINT wFlags, LPCTSTR pszSrcFile, DWORD dwSrcAttribs, LPCTSTR pszDestFile, DWORD dwDestAttribs)
276 __try
278 return CopyCallback_Wrap(hWnd, wFunc, wFlags, pszSrcFile, dwSrcAttribs, pszDestFile, dwDestAttribs);
280 __except(CCrashReport::Instance().SendReport(GetExceptionInformation()))
283 return IDYES;
286 UINT __stdcall CShellExt::CopyCallback_Wrap(HWND /*hWnd*/, UINT wFunc, UINT /*wFlags*/, LPCTSTR pszSrcFile, DWORD /*dwSrcAttribs*/, LPCTSTR /*pszDestFile*/, DWORD /*dwDestAttribs*/)
288 if (wFunc == FO_COPY)
289 return IDYES; // copying is not a problem for us
291 m_remoteCacheLink.ReleaseLockForPath(CTGitPath(pszSrcFile));
292 // we could now wait a little bit to give the cache time to release the handles.
293 // but the explorer/shell already retries any action for about two seconds
294 // if it first fails. So if the cache hasn't released the handle yet, the explorer
295 // will retry anyway, so we just leave here immediately.
296 return IDYES;