Make thread creation thread-safe
[TortoiseGit.git] / src / TortoiseProc / AddDlg.cpp
blob1c01b087f194ede77f0f3b5dbf11e0ca884dc812
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-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 "AddDlg.h"
24 #include "PathUtils.h"
25 #include "Git.h"
26 #include "AppUtils.h"
28 #define REFRESHTIMER 100
30 IMPLEMENT_DYNAMIC(CAddDlg, CResizableStandAloneDialog)
31 CAddDlg::CAddDlg(CWnd* pParent /*=nullptr*/)
32 : CResizableStandAloneDialog(CAddDlg::IDD, pParent)
33 , m_bThreadRunning(FALSE)
34 , m_bCancelled(false)
35 , m_bIncludeIgnored(FALSE)
39 CAddDlg::~CAddDlg()
43 void CAddDlg::DoDataExchange(CDataExchange* pDX)
45 CResizableStandAloneDialog::DoDataExchange(pDX);
46 DDX_Control(pDX, IDC_ADDLIST, m_addListCtrl);
47 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
48 DDX_Check(pDX, IDC_INCLUDE_IGNORED, m_bIncludeIgnored);
51 BEGIN_MESSAGE_MAP(CAddDlg, CResizableStandAloneDialog)
52 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
53 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
54 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
55 ON_WM_TIMER()
56 ON_BN_CLICKED(IDC_INCLUDE_IGNORED, &CAddDlg::OnBnClickedIncludeIgnored)
57 END_MESSAGE_MAP()
60 BOOL CAddDlg::OnInitDialog()
62 CResizableStandAloneDialog::OnInitDialog();
63 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
65 // initialize the svn status list control
66 m_addListCtrl.Init(GITSLC_COLEXT, L"AddDlg", GITSLC_POPALL ^ (GITSLC_POPADD | GITSLC_POPCOMMIT | GITSLC_POPCHANGELISTS | GITSLC_POPPREPAREDIFF), true, true, GITSLC_COLEXT | GITSLC_COLMODIFICATIONDATE | GITSLC_COLSIZE); // adding and committing is useless in the add dialog
67 m_addListCtrl.SetIgnoreRemoveOnly(); // when ignoring, don't add the parent folder since we're in the add dialog
68 m_addListCtrl.SetSelectButton(&m_SelectAll);
69 m_addListCtrl.SetConfirmButton((CButton*)GetDlgItem(IDOK));
70 m_addListCtrl.SetEmptyString(IDS_ERR_NOTHINGTOADD);
71 m_addListCtrl.SetCancelBool(&m_bCancelled);
72 m_addListCtrl.SetBackgroundImage(IDI_ADD_BKG);
73 m_addListCtrl.EnableFileDrop();
75 CString sWindowTitle;
76 GetWindowText(sWindowTitle);
77 CAppUtils::SetWindowTitle(m_hWnd, g_Git.CombinePath(m_pathList.GetCommonRoot().GetUIPathString()), sWindowTitle);
79 AdjustControlSize(IDC_SELECTALL);
80 AdjustControlSize(IDC_INCLUDE_IGNORED);
82 AddAnchor(IDC_ADDLIST, TOP_LEFT, BOTTOM_RIGHT);
83 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
84 AddAnchor(IDC_INCLUDE_IGNORED, BOTTOM_LEFT);
85 AddAnchor(IDOK, BOTTOM_RIGHT);
86 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
87 AddAnchor(IDHELP, BOTTOM_RIGHT);
89 if (GetExplorerHWND())
90 CenterWindow(CWnd::FromHandle(GetExplorerHWND()));
91 EnableSaveRestore(L"AddDlg");
93 //first start a thread to obtain the file list with the status without
94 //blocking the dialog
95 InterlockedExchange(&m_bThreadRunning, TRUE);
96 if (!AfxBeginThread(AddThreadEntry, this))
98 InterlockedExchange(&m_bThreadRunning, FALSE);
99 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
101 return TRUE;
104 void CAddDlg::OnOK()
106 if (m_bThreadRunning)
107 return;
109 // save only the files the user has selected into the path list
110 m_addListCtrl.WriteCheckedNamesToPathList(m_pathList);
112 CResizableStandAloneDialog::OnOK();
115 void CAddDlg::OnCancel()
117 m_bCancelled = true;
118 if (m_bThreadRunning)
119 return;
121 CResizableStandAloneDialog::OnCancel();
124 void CAddDlg::OnBnClickedSelectall()
126 UINT state = (m_SelectAll.GetState() & 0x0003);
127 if (state == BST_INDETERMINATE)
129 // It is not at all useful to manually place the checkbox into the indeterminate state...
130 // We will force this on to the unchecked state
131 state = BST_UNCHECKED;
132 m_SelectAll.SetCheck(state);
134 theApp.DoWaitCursor(1);
135 m_addListCtrl.SelectAll(state == BST_CHECKED);
136 theApp.DoWaitCursor(-1);
139 UINT CAddDlg::AddThreadEntry(LPVOID pVoid)
141 return reinterpret_cast<CAddDlg*>(pVoid)->AddThread();
144 UINT CAddDlg::AddThread()
146 // get the status of all selected file/folders recursively
147 // and show the ones which the user can add (i.e. the unversioned ones)
148 DialogEnableWindow(IDOK, false);
149 m_bCancelled = false;
150 m_addListCtrl.StoreScrollPos();
151 m_addListCtrl.Clear();
152 if (!m_addListCtrl.GetStatus(&m_pathList, false, m_bIncludeIgnored != FALSE, true))
153 m_addListCtrl.SetEmptyString(m_addListCtrl.GetLastErrorMessage());
154 unsigned int dwShow = GITSLC_SHOWUNVERSIONED | GITSLC_SHOWDIRECTFILES | GITSLC_SHOWREMOVEDANDPRESENT;
155 if (m_bIncludeIgnored)
156 dwShow |= GITSLC_SHOWIGNORED;
157 m_addListCtrl.Show(dwShow, GITSLC_SHOWUNVERSIONED);
159 InterlockedExchange(&m_bThreadRunning, FALSE);
160 return 0;
163 BOOL CAddDlg::PreTranslateMessage(MSG* pMsg)
165 if (pMsg->message == WM_KEYDOWN)
167 switch (pMsg->wParam)
169 case VK_RETURN:
171 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
173 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
174 PostMessage(WM_COMMAND, IDOK);
175 return TRUE;
178 break;
179 case VK_F5:
181 Refresh();
183 break;
187 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
190 LRESULT CAddDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
192 if (InterlockedExchange(&m_bThreadRunning, TRUE))
193 return 0;
194 if (!AfxBeginThread(AddThreadEntry, this))
196 InterlockedExchange(&m_bThreadRunning, FALSE);
197 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
199 return 0;
202 LRESULT CAddDlg::OnFileDropped(WPARAM, LPARAM lParam)
204 BringWindowToTop();
205 SetForegroundWindow();
206 SetActiveWindow();
207 // if multiple files/folders are dropped
208 // this handler is called for every single item
209 // separately.
210 // To avoid creating multiple refresh threads and
211 // causing crashes, we only add the items to the
212 // list control and start a timer.
213 // When the timer expires, we start the refresh thread,
214 // but only if it isn't already running - otherwise we
215 // restart the timer.
216 CTGitPath path;
217 path.SetFromWin((LPCTSTR)lParam);
219 // check whether the dropped file belongs to the very same repository
220 CString projectDir;
221 if (!path.HasAdminDir(&projectDir) || !CPathUtils::ArePathStringsEqual(g_Git.m_CurrentDir, projectDir))
222 return 0;
224 if (!m_addListCtrl.HasPath(path))
226 if (m_pathList.AreAllPathsFiles())
228 m_pathList.AddPath(path);
229 m_pathList.RemoveDuplicates();
231 else
233 // if the path list contains folders, we have to check whether
234 // our just (maybe) added path is a child of one of those. If it is
235 // a child of a folder already in the list, we must not add it. Otherwise
236 // that path could show up twice in the list.
237 bool bHasParentInList = false;
238 for (int i=0; i<m_pathList.GetCount(); ++i)
240 if (m_pathList[i].IsAncestorOf(path))
242 bHasParentInList = true;
243 break;
246 if (!bHasParentInList)
248 m_pathList.AddPath(path);
249 m_pathList.RemoveDuplicates();
253 m_addListCtrl.ResetChecked(path);
255 // Always start the timer, since the status of an existing item might have changed
256 SetTimer(REFRESHTIMER, 200, nullptr);
257 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Item %s dropped, timer started\n", path.GetWinPath());
258 return 0;
261 void CAddDlg::Refresh()
263 if (InterlockedExchange(&m_bThreadRunning, TRUE))
264 return;
265 if (!AfxBeginThread(AddThreadEntry, this))
267 InterlockedExchange(&m_bThreadRunning, FALSE);
268 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
272 void CAddDlg::OnTimer(UINT_PTR nIDEvent)
274 switch (nIDEvent)
276 case REFRESHTIMER:
277 if (m_bThreadRunning)
279 SetTimer(REFRESHTIMER, 200, nullptr);
280 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
282 else
284 KillTimer(REFRESHTIMER);
285 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
286 OnSVNStatusListCtrlNeedsRefresh(0, 0);
288 break;
290 __super::OnTimer(nIDEvent);
293 void CAddDlg::OnBnClickedIncludeIgnored()
295 UpdateData();
296 Refresh();