Merge branch 'bare-repo'
[TortoiseGit.git] / src / TortoiseShell / ShellExt.cpp
blob8d22b58e3a6bf78d4abec872ef956f1c85488082
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2008 - 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"
21 #pragma warning (disable : 4786)
23 // Initialize GUIDs (should be done only and at-least once per DLL/EXE)
24 #include <initguid.h>
25 #include "Guids.h"
26 #include "git.h"
27 #include "ShellExt.h"
28 //#include "..\version.h"
29 //#include "libintl.h"
30 #undef swprintf
31 #include "GitPropertyPage.h"
33 std::set<CShellExt *> g_exts;
35 // *********************** CShellExt *************************
36 CShellExt::CShellExt(FileState state)
38 m_State = state;
40 m_cRef = 0L;
41 g_cRefThisDll++;
43 g_exts.insert(this);
45 INITCOMMONCONTROLSEX used = {
46 sizeof(INITCOMMONCONTROLSEX),
47 ICC_LISTVIEW_CLASSES | ICC_WIN95_CLASSES | ICC_BAR_CLASSES | ICC_USEREX_CLASSES
49 InitCommonControlsEx(&used);
50 LoadLangDll();
52 if (SysInfo::Instance().IsVistaOrLater())
54 HMODULE hUxTheme = ::GetModuleHandle (_T("UXTHEME.DLL"));
56 pfnGetBufferedPaintBits = (FN_GetBufferedPaintBits)::GetProcAddress(hUxTheme, "GetBufferedPaintBits");
57 pfnBeginBufferedPaint = (FN_BeginBufferedPaint)::GetProcAddress(hUxTheme, "BeginBufferedPaint");
58 pfnEndBufferedPaint = (FN_EndBufferedPaint)::GetProcAddress(hUxTheme, "EndBufferedPaint");
62 CShellExt::~CShellExt()
64 std::map<UINT, HBITMAP>::iterator it;
65 for (it = bitmaps.begin(); it != bitmaps.end(); ++it)
67 ::DeleteObject(it->second);
69 bitmaps.clear();
70 g_cRefThisDll--;
71 g_exts.erase(this);
74 void LoadLangDll()
76 if ((g_langid != g_ShellCache.GetLangID())&&((g_langTimeout == 0)||(g_langTimeout < GetTickCount())))
78 g_langid = g_ShellCache.GetLangID();
79 DWORD langId = g_langid;
80 TCHAR langDll[MAX_PATH*4];
81 HINSTANCE hInst = NULL;
82 TCHAR langdir[MAX_PATH] = {0};
83 char langdirA[MAX_PATH] = {0};
84 if (GetModuleFileName(g_hmodThisDll, langdir, MAX_PATH)==0)
85 return;
86 if (GetModuleFileNameA(g_hmodThisDll, langdirA, MAX_PATH)==0)
87 return;
88 TCHAR * dirpoint = _tcsrchr(langdir, '\\');
89 char * dirpointA = strrchr(langdirA, '\\');
90 if (dirpoint)
91 *dirpoint = 0;
92 if (dirpointA)
93 *dirpointA = 0;
94 dirpoint = _tcsrchr(langdir, '\\');
95 dirpointA = strrchr(langdirA, '\\');
96 if (dirpoint)
97 *dirpoint = 0;
98 if (dirpointA)
99 *dirpointA = 0;
100 strcat_s(langdirA, MAX_PATH, "\\Languages");
101 // bindtextdomain ("subversion", langdirA);
105 _stprintf_s(langDll, MAX_PATH*4, _T("%s\\Languages\\TortoiseProc%d.dll"), langdir, langId);
106 BOOL versionmatch = TRUE;
108 struct TRANSARRAY
110 WORD wLanguageID;
111 WORD wCharacterSet;
114 DWORD dwReserved,dwBufferSize;
115 dwBufferSize = GetFileVersionInfoSize((LPTSTR)langDll,&dwReserved);
117 if (dwBufferSize > 0)
119 LPVOID pBuffer = (void*) malloc(dwBufferSize);
121 if (pBuffer != (void*) NULL)
123 UINT nInfoSize = 0,
124 nFixedLength = 0;
125 LPSTR lpVersion = NULL;
126 VOID* lpFixedPointer;
127 TRANSARRAY* lpTransArray;
128 TCHAR strLangProduktVersion[MAX_PATH];
130 if (GetFileVersionInfo((LPTSTR)langDll,
131 dwReserved,
132 dwBufferSize,
133 pBuffer))
135 // Query the current language
136 if (VerQueryValue( pBuffer,
137 _T("\\VarFileInfo\\Translation"),
138 &lpFixedPointer,
139 &nFixedLength))
141 lpTransArray = (TRANSARRAY*) lpFixedPointer;
143 _stprintf_s(strLangProduktVersion, MAX_PATH, _T("\\StringFileInfo\\%04x%04x\\ProductVersion"),
144 lpTransArray[0].wLanguageID, lpTransArray[0].wCharacterSet);
146 if (VerQueryValue(pBuffer,
147 (LPTSTR)strLangProduktVersion,
148 (LPVOID *)&lpVersion,
149 &nInfoSize))
151 // versionmatch = (_tcscmp((LPCTSTR)lpVersion, _T(STRPRODUCTVER)) == 0);
156 free(pBuffer);
157 } // if (pBuffer != (void*) NULL)
158 } // if (dwBufferSize > 0)
159 else
160 versionmatch = FALSE;
162 if (versionmatch)
163 hInst = LoadLibrary(langDll);
164 if (hInst != NULL)
166 if (g_hResInst != g_hmodThisDll)
167 FreeLibrary(g_hResInst);
168 g_hResInst = hInst;
170 else
172 DWORD lid = SUBLANGID(langId);
173 lid--;
174 if (lid > 0)
176 langId = MAKELANGID(PRIMARYLANGID(langId), lid);
178 else
179 langId = 0;
181 } while ((hInst == NULL) && (langId != 0));
182 if (hInst == NULL)
184 // either the dll for the selected language is not present, or
185 // it is the wrong version.
186 // fall back to English and set a timeout so we don't retry
187 // to load the language dll too often
188 if (g_hResInst != g_hmodThisDll)
189 FreeLibrary(g_hResInst);
190 g_hResInst = g_hmodThisDll;
191 g_langid = 1033;
192 // set a timeout of 10 seconds
193 if (g_ShellCache.GetLangID() != 1033)
194 g_langTimeout = GetTickCount() + 10000;
196 else
197 g_langTimeout = 0;
198 } // if (g_langid != g_ShellCache.GetLangID())
201 STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
203 *ppv = NULL;
205 if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
207 *ppv = (LPSHELLEXTINIT)this;
209 else if (IsEqualIID(riid, IID_IContextMenu))
211 *ppv = (LPCONTEXTMENU)this;
213 else if (IsEqualIID(riid, IID_IContextMenu2))
215 *ppv = (LPCONTEXTMENU2)this;
217 else if (IsEqualIID(riid, IID_IContextMenu3))
219 *ppv = (LPCONTEXTMENU3)this;
221 else if (IsEqualIID(riid, IID_IShellIconOverlayIdentifier))
223 *ppv = (IShellIconOverlayIdentifier*)this;
225 else if (IsEqualIID(riid, IID_IShellPropSheetExt))
227 *ppv = (LPSHELLPROPSHEETEXT)this;
229 else if (IsEqualIID(riid, IID_IColumnProvider))
231 *ppv = (IColumnProvider *)this;
233 else if (IsEqualIID(riid, IID_IShellCopyHook))
235 *ppv = (ICopyHook *)this;
237 if (*ppv)
239 AddRef();
241 return NOERROR;
244 return E_NOINTERFACE;
247 STDMETHODIMP_(ULONG) CShellExt::AddRef()
249 return ++m_cRef;
252 STDMETHODIMP_(ULONG) CShellExt::Release()
254 if (--m_cRef)
255 return m_cRef;
257 delete this;
259 return 0L;
262 // IPersistFile members
263 STDMETHODIMP CShellExt::GetClassID(CLSID *pclsid)
265 *pclsid = CLSID_Tortoisegit_UNCONTROLLED;
266 return S_OK;
269 STDMETHODIMP CShellExt::Load(LPCOLESTR /*pszFileName*/, DWORD /*dwMode*/)
271 return S_OK;
274 // ICopyHook member
275 UINT __stdcall CShellExt::CopyCallback(HWND /*hWnd*/, UINT wFunc, UINT /*wFlags*/, LPCTSTR pszSrcFile, DWORD /*dwSrcAttribs*/, LPCTSTR /*pszDestFile*/, DWORD /*dwDestAttribs*/)
277 if (wFunc == FO_COPY)
278 return IDYES; // copying is not a problem for us
280 m_remoteCacheLink.ReleaseLockForPath(CTGitPath(pszSrcFile));
281 // we could now wait a little bit to give the cache time to release the handles.
282 // but the explorer/shell already retries any action for about two seconds
283 // if it first fails. So if the cache hasn't released the handle yet, the explorer
284 // will retry anyway, so we just leave here immediately.
285 return IDYES;