Use STL algorithms more often
[TortoiseGit.git] / src / TortoiseShell / TortoiseGIT.cpp
blobc3d3d3bf61720ae7eefffdf85637c738f0bb4d7b
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2010, 2012 - TortoiseSVN
4 // Copyright (C) 2008-2012, 2014, 2016-2017 - TortoiseGit
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"
21 #include "ShellExt.h"
22 #include "Guids.h"
23 #include "ShellExtClassFactory.h"
24 #include "ShellObjects.h"
26 volatile LONG g_cRefThisDll = 0; ///< reference count of this DLL.
27 HINSTANCE g_hmodThisDll = nullptr; ///< handle to this DLL itself.
28 ShellCache g_ShellCache; ///< caching of registry entries, ...
29 DWORD g_langid;
30 ULONGLONG g_langTimeout = 0;
31 HINSTANCE g_hResInst = nullptr;
32 std::wstring g_filepath;
33 git_wc_status_kind g_filestatus = git_wc_status_none; ///< holds the corresponding status to the file/dir above
34 bool g_readonlyoverlay = false;
35 bool g_lockedoverlay = false;
37 bool g_normalovlloaded = false;
38 bool g_modifiedovlloaded = false;
39 bool g_conflictedovlloaded = false;
40 bool g_readonlyovlloaded = false;
41 bool g_deletedovlloaded = false;
42 bool g_lockedovlloaded = false;
43 bool g_addedovlloaded = false;
44 bool g_ignoredovlloaded = false;
45 bool g_unversionedovlloaded = false;
46 CComCriticalSection g_csGlobalCOMGuard;
48 LPCTSTR g_MenuIDString = L"TortoiseGit";
50 ShellObjects g_shellObjects;
52 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
54 extern "C" int APIENTRY
55 DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /* lpReserved */)
57 #ifdef _DEBUG
58 // if no debugger is present, then don't load the dll.
59 // this prevents other apps from loading the dll and locking
60 // it.
62 bool bInShellTest = false;
63 TCHAR buf[MAX_PATH + 1] = {0}; // MAX_PATH ok, the test really is for debugging anyway.
64 DWORD pathLength = GetModuleFileName(nullptr, buf, _countof(buf) - 1);
65 if(pathLength >= 14)
67 if (pathLength >= 24 && _wcsicmp(&buf[pathLength - 24], L"\\TortoiseGitExplorer.exe") == 0)
68 bInShellTest = true;
69 if ((_wcsicmp(&buf[pathLength-14], L"\\ShellTest.exe")) == 0)
70 bInShellTest = true;
71 if ((_wcsicmp(&buf[pathLength-13], L"\\verclsid.exe")) == 0)
72 bInShellTest = true;
75 if (!::IsDebuggerPresent() && !bInShellTest)
77 ATLTRACE("In debug load preventer\n");
78 return FALSE;
80 #endif
82 // NOTE: Do *NOT* call apr_initialize() or apr_terminate() here in DllMain(),
83 // because those functions call LoadLibrary() indirectly through malloc().
84 // And LoadLibrary() inside DllMain() is not allowed and can lead to unexpected
85 // behavior and even may create dependency loops in the dll load order.
86 if (dwReason == DLL_PROCESS_ATTACH)
88 if (!g_hmodThisDll)
89 g_csGlobalCOMGuard.Init();
91 // Extension DLL one-time initialization
92 g_hmodThisDll = hInstance;
94 else if (dwReason == DLL_PROCESS_DETACH)
96 // do not clean up memory here:
97 // if an application doesn't release all COM objects
98 // but still unloads the dll, cleaning up ourselves
99 // will lead to crashes.
100 // better to leak some memory than to crash other apps.
101 // sometimes an application doesn't release all COM objects
102 // but still unloads the dll.
103 // in that case, we do it ourselves
104 g_csGlobalCOMGuard.Term();
106 return 1; // ok
109 STDAPI DllCanUnloadNow(void)
111 return (g_cRefThisDll == 0 ? S_OK : S_FALSE);
114 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppvOut)
116 if (!ppvOut)
117 return E_POINTER;
118 *ppvOut = nullptr;
120 FileState state = FileStateInvalid;
121 if (IsEqualIID(rclsid, CLSID_Tortoisegit_UPTODATE))
122 state = FileStateVersioned;
123 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_MODIFIED))
124 state = FileStateModified;
125 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_CONFLICTING))
126 state = FileStateConflict;
127 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_UNCONTROLLED))
128 state = FileStateUncontrolled;
129 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_DROPHANDLER))
130 state = FileStateDropHandler;
131 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_DELETED))
132 state = FileStateDeleted;
133 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_READONLY))
134 state = FileStateReadOnly;
135 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_LOCKED))
136 state = FileStateLockedOverlay;
137 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_ADDED))
138 state = FileStateAddedOverlay;
139 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_IGNORED))
140 state = FileStateIgnoredOverlay;
141 else if (IsEqualIID(rclsid, CLSID_Tortoisegit_UNVERSIONED))
142 state = FileStateUnversionedOverlay;
144 if (state != FileStateInvalid)
146 CShellExtClassFactory *pcf = new (std::nothrow) CShellExtClassFactory(state);
147 if (!pcf)
148 return E_OUTOFMEMORY;
149 // refcount currently set to 0
150 const HRESULT hr = pcf->QueryInterface(riid, ppvOut);
151 if(FAILED(hr))
152 delete pcf;
153 return hr;
156 return CLASS_E_CLASSNOTAVAILABLE;