some spaces-tabs code cleanup
[TortoiseGit.git] / src / TortoiseShell / ShellExt.cpp
blob3b1e3143d661d6cd3f4340df4f3bdcb9fc6430a7
1 // TortoiseSVN - 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 "SVNPropertyPage.h"
33 std::set<CShellExt *> g_exts;
35 // *********************** CShellExt *************************
36 CShellExt::CShellExt(FileState state)
38 OSVERSIONINFOEX inf;
39 SecureZeroMemory(&inf, sizeof(OSVERSIONINFOEX));
40 inf.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
41 GetVersionEx((OSVERSIONINFO *)&inf);
42 fullver = MAKEWORD(inf.dwMinorVersion, inf.dwMajorVersion);
44 m_State = state;
46 m_cRef = 0L;
47 g_cRefThisDll++;
49 g_exts.insert(this);
51 INITCOMMONCONTROLSEX used = {
52 sizeof(INITCOMMONCONTROLSEX),
53 ICC_LISTVIEW_CLASSES | ICC_WIN95_CLASSES | ICC_BAR_CLASSES | ICC_USEREX_CLASSES
55 InitCommonControlsEx(&used);
56 LoadLangDll();
58 if (fullver >= 0x0600)
60 HMODULE hUxTheme = ::GetModuleHandle (_T("UXTHEME.DLL"));
62 pfnGetBufferedPaintBits = (FN_GetBufferedPaintBits)::GetProcAddress(hUxTheme, "GetBufferedPaintBits");
63 pfnBeginBufferedPaint = (FN_BeginBufferedPaint)::GetProcAddress(hUxTheme, "BeginBufferedPaint");
64 pfnEndBufferedPaint = (FN_EndBufferedPaint)::GetProcAddress(hUxTheme, "EndBufferedPaint");
68 CShellExt::~CShellExt()
70 std::map<UINT, HBITMAP>::iterator it;
71 for (it = bitmaps.begin(); it != bitmaps.end(); ++it)
73 ::DeleteObject(it->second);
75 bitmaps.clear();
76 g_cRefThisDll--;
77 g_exts.erase(this);
80 void LoadLangDll()
82 if ((g_langid != g_ShellCache.GetLangID())&&((g_langTimeout == 0)||(g_langTimeout < GetTickCount())))
84 g_langid = g_ShellCache.GetLangID();
85 DWORD langId = g_langid;
86 TCHAR langDll[MAX_PATH*4];
87 HINSTANCE hInst = NULL;
88 TCHAR langdir[MAX_PATH] = {0};
89 char langdirA[MAX_PATH] = {0};
90 if (GetModuleFileName(g_hmodThisDll, langdir, MAX_PATH)==0)
91 return;
92 if (GetModuleFileNameA(g_hmodThisDll, langdirA, MAX_PATH)==0)
93 return;
94 TCHAR * dirpoint = _tcsrchr(langdir, '\\');
95 char * dirpointA = strrchr(langdirA, '\\');
96 if (dirpoint)
97 *dirpoint = 0;
98 if (dirpointA)
99 *dirpointA = 0;
100 dirpoint = _tcsrchr(langdir, '\\');
101 dirpointA = strrchr(langdirA, '\\');
102 if (dirpoint)
103 *dirpoint = 0;
104 if (dirpointA)
105 *dirpointA = 0;
106 strcat_s(langdirA, MAX_PATH, "\\Languages");
107 // bindtextdomain ("subversion", langdirA);
111 _stprintf_s(langDll, MAX_PATH*4, _T("%s\\Languages\\TortoiseProc%d.dll"), langdir, langId);
112 BOOL versionmatch = TRUE;
114 struct TRANSARRAY
116 WORD wLanguageID;
117 WORD wCharacterSet;
120 DWORD dwReserved,dwBufferSize;
121 dwBufferSize = GetFileVersionInfoSize((LPTSTR)langDll,&dwReserved);
123 if (dwBufferSize > 0)
125 LPVOID pBuffer = (void*) malloc(dwBufferSize);
127 if (pBuffer != (void*) NULL)
129 UINT nInfoSize = 0,
130 nFixedLength = 0;
131 LPSTR lpVersion = NULL;
132 VOID* lpFixedPointer;
133 TRANSARRAY* lpTransArray;
134 TCHAR strLangProduktVersion[MAX_PATH];
136 if (GetFileVersionInfo((LPTSTR)langDll,
137 dwReserved,
138 dwBufferSize,
139 pBuffer))
141 // Query the current language
142 if (VerQueryValue( pBuffer,
143 _T("\\VarFileInfo\\Translation"),
144 &lpFixedPointer,
145 &nFixedLength))
147 lpTransArray = (TRANSARRAY*) lpFixedPointer;
149 _stprintf_s(strLangProduktVersion, MAX_PATH, _T("\\StringFileInfo\\%04x%04x\\ProductVersion"),
150 lpTransArray[0].wLanguageID, lpTransArray[0].wCharacterSet);
152 if (VerQueryValue(pBuffer,
153 (LPTSTR)strLangProduktVersion,
154 (LPVOID *)&lpVersion,
155 &nInfoSize))
157 // versionmatch = (_tcscmp((LPCTSTR)lpVersion, _T(STRPRODUCTVER)) == 0);
162 free(pBuffer);
163 } // if (pBuffer != (void*) NULL)
164 } // if (dwBufferSize > 0)
165 else
166 versionmatch = FALSE;
168 if (versionmatch)
169 hInst = LoadLibrary(langDll);
170 if (hInst != NULL)
172 if (g_hResInst != g_hmodThisDll)
173 FreeLibrary(g_hResInst);
174 g_hResInst = hInst;
176 else
178 DWORD lid = SUBLANGID(langId);
179 lid--;
180 if (lid > 0)
182 langId = MAKELANGID(PRIMARYLANGID(langId), lid);
184 else
185 langId = 0;
187 } while ((hInst == NULL) && (langId != 0));
188 if (hInst == NULL)
190 // either the dll for the selected language is not present, or
191 // it is the wrong version.
192 // fall back to English and set a timeout so we don't retry
193 // to load the language dll too often
194 if (g_hResInst != g_hmodThisDll)
195 FreeLibrary(g_hResInst);
196 g_hResInst = g_hmodThisDll;
197 g_langid = 1033;
198 // set a timeout of 10 seconds
199 if (g_ShellCache.GetLangID() != 1033)
200 g_langTimeout = GetTickCount() + 10000;
202 else
203 g_langTimeout = 0;
204 } // if (g_langid != g_ShellCache.GetLangID())
207 STDMETHODIMP CShellExt::QueryInterface(REFIID riid, LPVOID FAR *ppv)
209 *ppv = NULL;
211 if (IsEqualIID(riid, IID_IShellExtInit) || IsEqualIID(riid, IID_IUnknown))
213 *ppv = (LPSHELLEXTINIT)this;
215 else if (IsEqualIID(riid, IID_IContextMenu))
217 *ppv = (LPCONTEXTMENU)this;
219 else if (IsEqualIID(riid, IID_IContextMenu2))
221 *ppv = (LPCONTEXTMENU2)this;
223 else if (IsEqualIID(riid, IID_IContextMenu3))
225 *ppv = (LPCONTEXTMENU3)this;
227 else if (IsEqualIID(riid, IID_IShellIconOverlayIdentifier))
229 *ppv = (IShellIconOverlayIdentifier*)this;
231 else if (IsEqualIID(riid, IID_IShellPropSheetExt))
233 *ppv = (LPSHELLPROPSHEETEXT)this;
235 else if (IsEqualIID(riid, IID_IColumnProvider))
237 *ppv = (IColumnProvider *)this;
239 else if (IsEqualIID(riid, IID_IShellCopyHook))
241 *ppv = (ICopyHook *)this;
243 if (*ppv)
245 AddRef();
247 return NOERROR;
250 return E_NOINTERFACE;
253 STDMETHODIMP_(ULONG) CShellExt::AddRef()
255 return ++m_cRef;
258 STDMETHODIMP_(ULONG) CShellExt::Release()
260 if (--m_cRef)
261 return m_cRef;
263 delete this;
265 return 0L;
268 // IPersistFile members
269 STDMETHODIMP CShellExt::GetClassID(CLSID *pclsid)
271 *pclsid = CLSID_Tortoisegit_UNCONTROLLED;
272 return S_OK;
275 STDMETHODIMP CShellExt::Load(LPCOLESTR /*pszFileName*/, DWORD /*dwMode*/)
277 return S_OK;
280 // ICopyHook member
281 UINT __stdcall CShellExt::CopyCallback(HWND /*hWnd*/, UINT wFunc, UINT /*wFlags*/, LPCTSTR pszSrcFile, DWORD /*dwSrcAttribs*/, LPCTSTR /*pszDestFile*/, DWORD /*dwDestAttribs*/)
283 if (wFunc == FO_COPY)
284 return IDYES; // copying is not a problem for us
286 m_remoteCacheLink.ReleaseLockForPath(CTGitPath(pszSrcFile));
287 // we could now wait a little bit to give the cache time to release the handles.
288 // but the explorer/shell already retries any action for about two seconds
289 // if it first fails. So if the cache hasn't released the handle yet, the explorer
290 // will retry anyway, so we just leave here immediately.
291 return IDYES;