Make thread creation thread-safe
[TortoiseGit.git] / src / TortoiseProc / RevertDlg.cpp
blob799da30937d87409052f037d31e4c53fa9959961
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2016-2019 - TortoiseGit
4 // Copyright (C) 2003-2008 - 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"
21 #include "TortoiseProc.h"
22 #include "MessageBox.h"
23 #include "RevertDlg.h"
24 #include "Git.h"
25 #include "PathUtils.h"
26 #include "registry.h"
27 #include "AppUtils.h"
29 #define REFRESHTIMER 100
31 IMPLEMENT_DYNAMIC(CRevertDlg, CResizableStandAloneDialog)
32 CRevertDlg::CRevertDlg(CWnd* pParent /*=nullptr*/)
33 : CResizableStandAloneDialog(CRevertDlg::IDD, pParent)
34 , m_bSelectAll(TRUE)
35 , m_bThreadRunning(FALSE)
36 , m_bCancelled(false)
37 , m_bRecursive(FALSE)
41 CRevertDlg::~CRevertDlg()
45 void CRevertDlg::DoDataExchange(CDataExchange* pDX)
47 CResizableStandAloneDialog::DoDataExchange(pDX);
48 DDX_Control(pDX, IDC_REVERTLIST, m_RevertList);
49 DDX_Check(pDX, IDC_SELECTALL, m_bSelectAll);
50 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
54 BEGIN_MESSAGE_MAP(CRevertDlg, CResizableStandAloneDialog)
55 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
56 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
57 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
58 ON_WM_TIMER()
59 END_MESSAGE_MAP()
63 BOOL CRevertDlg::OnInitDialog()
65 CResizableStandAloneDialog::OnInitDialog();
66 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
68 m_RevertList.Init(GITSLC_COLEXT | GITSLC_COLSTATUS | GITSLC_COLADD| GITSLC_COLDEL, L"RevertDlg");
69 m_RevertList.SetConfirmButton((CButton*)GetDlgItem(IDOK));
70 m_RevertList.SetSelectButton(&m_SelectAll);
71 m_RevertList.SetCancelBool(&m_bCancelled);
72 m_RevertList.SetBackgroundImage(IDI_REVERT_BKG);
73 m_RevertList.EnableFileDrop();
75 CString sWindowTitle;
76 GetWindowText(sWindowTitle);
77 CAppUtils::SetWindowTitle(m_hWnd, g_Git.CombinePath(m_pathList.GetCommonRoot()), sWindowTitle);
79 AdjustControlSize(IDC_SELECTALL);
81 AddAnchor(IDC_REVERTLIST, TOP_LEFT, BOTTOM_RIGHT);
82 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
83 AddAnchor(IDC_UNVERSIONEDITEMS, BOTTOM_RIGHT);
84 AddAnchor(IDOK, BOTTOM_RIGHT);
85 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
86 AddAnchor(IDHELP, BOTTOM_RIGHT);
87 if (GetExplorerHWND())
88 CenterWindow(CWnd::FromHandle(GetExplorerHWND()));
89 EnableSaveRestore(L"RevertDlg");
91 // first start a thread to obtain the file list with the status without
92 // blocking the dialog
93 InterlockedExchange(&m_bThreadRunning, TRUE);
94 if (AfxBeginThread(RevertThreadEntry, this) == nullptr)
96 InterlockedExchange(&m_bThreadRunning, FALSE);
97 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
100 return TRUE;
103 UINT CRevertDlg::RevertThreadEntry(LPVOID pVoid)
105 return reinterpret_cast<CRevertDlg*>(pVoid)->RevertThread();
108 UINT CRevertDlg::RevertThread()
110 // get the status of all selected file/folders recursively
111 // and show the ones which can be reverted to the user
112 // in a list control.
113 DialogEnableWindow(IDOK, false);
114 m_bCancelled = false;
116 m_RevertList.StoreScrollPos();
117 m_RevertList.Clear();
119 g_Git.RefreshGitIndex();
121 if (!m_RevertList.GetStatus(&m_pathList))
123 m_RevertList.SetEmptyString(m_RevertList.GetLastErrorMessage());
125 m_RevertList.Show(GITSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | GITSLC_SHOWDIRECTFILES | GITSLC_SHOWEXTERNALFROMDIFFERENTREPO,
126 // do not select all files, only the ones the user has selected directly
127 GITSLC_SHOWDIRECTFILES|GITSLC_SHOWADDED);
129 if (m_RevertList.HasUnversionedItems())
131 if (DWORD(CRegStdDWORD(L"Software\\TortoiseGit\\UnversionedAsModified", FALSE)))
133 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_SHOW);
135 else
136 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
138 else
139 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
141 InterlockedExchange(&m_bThreadRunning, FALSE);
142 RefreshCursor();
144 return 0;
147 void CRevertDlg::OnOK()
149 if (m_bThreadRunning)
150 return;
151 auto locker(m_RevertList.AcquireReadLock());
152 // save only the files the user has selected into the temporary file
153 m_bRecursive = TRUE;
154 for (int i=0; i<m_RevertList.GetItemCount(); ++i)
156 if (!m_RevertList.GetCheck(i))
157 m_bRecursive = FALSE;
158 else
160 m_selectedPathList.AddPath(*m_RevertList.GetListEntry(i));
161 #if 0
162 CGitStatusListCtrl::FileEntry * entry = m_RevertList.GetListEntry(i);
163 // add all selected entries to the list, except the ones with 'added'
164 // status: we later *delete* all the entries in the list before
165 // the actual revert is done (so the user has the reverted files
166 // still in the trash bin to recover from), but it's not good to
167 // delete added files because they're not restored by the revert.
168 if (entry->status != svn_wc_status_added)
169 m_selectedPathList.AddPath(entry->GetPath());
170 // if an entry inside an external is selected, we can't revert
171 // recursively anymore because the recursive revert stops at the
172 // external boundaries.
173 if (entry->IsInExternal())
174 m_bRecursive = FALSE;
175 #endif
178 if (!m_bRecursive)
179 m_RevertList.WriteCheckedNamesToPathList(m_pathList);
180 m_selectedPathList.SortByPathname();
182 CResizableStandAloneDialog::OnOK();
185 void CRevertDlg::OnCancel()
187 m_bCancelled = true;
188 if (m_bThreadRunning)
189 return;
191 CResizableStandAloneDialog::OnCancel();
194 void CRevertDlg::OnBnClickedSelectall()
196 UINT state = (m_SelectAll.GetState() & 0x0003);
197 if (state == BST_INDETERMINATE)
199 // It is not at all useful to manually place the checkbox into the indeterminate state...
200 // We will force this on to the unchecked state
201 state = BST_UNCHECKED;
202 m_SelectAll.SetCheck(state);
204 theApp.DoWaitCursor(1);
205 m_RevertList.SelectAll(state == BST_CHECKED);
206 theApp.DoWaitCursor(-1);
209 BOOL CRevertDlg::PreTranslateMessage(MSG* pMsg)
211 if (pMsg->message == WM_KEYDOWN)
213 switch (pMsg->wParam)
215 case VK_RETURN:
217 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
219 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
220 PostMessage(WM_COMMAND, IDOK);
221 return TRUE;
224 break;
225 case VK_F5:
227 if (!InterlockedExchange(&m_bThreadRunning, TRUE))
229 if (AfxBeginThread(RevertThreadEntry, this) == nullptr)
231 InterlockedExchange(&m_bThreadRunning, FALSE);
232 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
236 break;
240 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
243 LRESULT CRevertDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
245 if (InterlockedExchange(&m_bThreadRunning, TRUE))
246 return 0;
247 if (AfxBeginThread(RevertThreadEntry, this) == nullptr)
249 InterlockedExchange(&m_bThreadRunning, FALSE);
250 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
252 return 0;
255 LRESULT CRevertDlg::OnFileDropped(WPARAM, LPARAM lParam)
257 BringWindowToTop();
258 SetForegroundWindow();
259 SetActiveWindow();
260 // if multiple files/folders are dropped
261 // this handler is called for every single item
262 // separately.
263 // To avoid creating multiple refresh threads and
264 // causing crashes, we only add the items to the
265 // list control and start a timer.
266 // When the timer expires, we start the refresh thread,
267 // but only if it isn't already running - otherwise we
268 // restart the timer.
269 CTGitPath path;
270 path.SetFromWin((LPCTSTR)lParam);
272 // check whether the dropped file belongs to the very same repository
273 CString projectDir;
274 if (!path.HasAdminDir(&projectDir) || !CPathUtils::ArePathStringsEqual(g_Git.m_CurrentDir, projectDir))
275 return 0;
277 if (!m_RevertList.HasPath(path))
279 if (m_pathList.AreAllPathsFiles())
281 m_pathList.AddPath(path);
282 m_pathList.RemoveDuplicates();
284 else
286 // if the path list contains folders, we have to check whether
287 // our just (maybe) added path is a child of one of those. If it is
288 // a child of a folder already in the list, we must not add it. Otherwise
289 // that path could show up twice in the list.
290 bool bHasParentInList = false;
291 for (int i=0; i<m_pathList.GetCount(); ++i)
293 if (m_pathList[i].IsAncestorOf(path))
295 bHasParentInList = true;
296 break;
299 if (!bHasParentInList)
301 m_pathList.AddPath(path);
302 m_pathList.RemoveDuplicates();
306 m_RevertList.ResetChecked(path);
308 // Always start the timer, since the status of an existing item might have changed
309 SetTimer(REFRESHTIMER, 200, nullptr);
310 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Item %s dropped, timer started\n", path.GetWinPath());
311 return 0;
314 void CRevertDlg::OnTimer(UINT_PTR nIDEvent)
316 switch (nIDEvent)
318 case REFRESHTIMER:
319 if (m_bThreadRunning)
321 SetTimer(REFRESHTIMER, 200, nullptr);
322 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
324 else
326 KillTimer(REFRESHTIMER);
327 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
328 OnSVNStatusListCtrlNeedsRefresh(0, 0);
330 break;
332 __super::OnTimer(nIDEvent);