Fix the potential uncloseable dialogs if starting a thread failed
[TortoiseGit.git] / src / TortoiseProc / ResolveDlg.cpp
blob7b9209425c8ca15fbadc352cee388d0f2e77b35d
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2013, 2015-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 "ResolveDlg.h"
24 #include "PathUtils.h"
25 #include "AppUtils.h"
26 #include "Git.h"
28 #define REFRESHTIMER 100
30 IMPLEMENT_DYNAMIC(CResolveDlg, CResizableStandAloneDialog)
31 CResolveDlg::CResolveDlg(CWnd* pParent /*=nullptr*/)
32 : CResizableStandAloneDialog(CResolveDlg::IDD, pParent)
33 , m_bThreadRunning(FALSE)
34 , m_bCancelled(false)
38 CResolveDlg::~CResolveDlg()
42 void CResolveDlg::DoDataExchange(CDataExchange* pDX)
44 CResizableStandAloneDialog::DoDataExchange(pDX);
45 DDX_Control(pDX, IDC_RESOLVELIST, m_resolveListCtrl);
46 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
50 BEGIN_MESSAGE_MAP(CResolveDlg, CResizableStandAloneDialog)
51 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
52 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
53 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
54 ON_WM_TIMER()
55 END_MESSAGE_MAP()
57 BOOL CResolveDlg::OnInitDialog()
59 CResizableStandAloneDialog::OnInitDialog();
60 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
62 m_resolveListCtrl.Init(GITSLC_COLEXT, L"ResolveDlg", GITSLC_POPALL ^ (GITSLC_POPIGNORE | GITSLC_POPADD | GITSLC_POPCOMMIT | GITSLC_POPEXPORT | GITSLC_POPRESTORE | GITSLC_POPSAVEAS | GITSLC_POPPREPAREDIFF));
63 m_resolveListCtrl.SetConfirmButton((CButton*)GetDlgItem(IDOK));
64 m_resolveListCtrl.SetSelectButton(&m_SelectAll);
65 m_resolveListCtrl.SetCancelBool(&m_bCancelled);
66 m_resolveListCtrl.SetBackgroundImage(IDI_RESOLVE_BKG);
67 m_resolveListCtrl.EnableFileDrop();
69 CString sWindowTitle;
70 GetWindowText(sWindowTitle);
71 CAppUtils::SetWindowTitle(m_hWnd, g_Git.m_CurrentDir, sWindowTitle);
73 AdjustControlSize(IDC_SELECTALL);
75 AddAnchor(IDC_RESOLVELIST, TOP_LEFT, BOTTOM_RIGHT);
76 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
77 AddAnchor(IDOK, BOTTOM_RIGHT);
78 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
79 AddAnchor(IDHELP, BOTTOM_RIGHT);
80 AddAnchor(IDC_STATIC_REMINDER, BOTTOM_RIGHT);
81 if (GetExplorerHWND())
82 CenterWindow(CWnd::FromHandle(GetExplorerHWND()));
83 EnableSaveRestore(L"ResolveDlg");
85 // first start a thread to obtain the file list with the status without
86 // blocking the dialog
87 InterlockedExchange(&m_bThreadRunning, TRUE);
88 if (!AfxBeginThread(ResolveThreadEntry, this))
90 InterlockedExchange(&m_bThreadRunning, FALSE);
91 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
94 return TRUE;
97 void CResolveDlg::OnOK()
99 if (m_bThreadRunning)
100 return;
102 //save only the files the user has selected into the path list
103 m_resolveListCtrl.WriteCheckedNamesToPathList(m_pathList);
105 CResizableStandAloneDialog::OnOK();
108 void CResolveDlg::OnCancel()
110 m_bCancelled = true;
111 if (m_bThreadRunning)
112 return;
114 CResizableStandAloneDialog::OnCancel();
117 void CResolveDlg::OnBnClickedSelectall()
119 UINT state = (m_SelectAll.GetState() & 0x0003);
120 if (state == BST_INDETERMINATE)
122 // It is not at all useful to manually place the checkbox into the indeterminate state...
123 // We will force this on to the unchecked state
124 state = BST_UNCHECKED;
125 m_SelectAll.SetCheck(state);
127 theApp.DoWaitCursor(1);
128 m_resolveListCtrl.SelectAll(state == BST_CHECKED);
129 theApp.DoWaitCursor(-1);
132 UINT CResolveDlg::ResolveThreadEntry(LPVOID pVoid)
134 return reinterpret_cast<CResolveDlg*>(pVoid)->ResolveThread();
136 UINT CResolveDlg::ResolveThread()
138 // get the status of all selected file/folders recursively
139 // and show the ones which are in conflict
140 DialogEnableWindow(IDOK, false);
142 m_bCancelled = false;
144 m_resolveListCtrl.StoreScrollPos();
145 m_resolveListCtrl.Clear();
146 if (!m_resolveListCtrl.GetStatus(&m_pathList))
147 m_resolveListCtrl.SetEmptyString(m_resolveListCtrl.GetLastErrorMessage());
148 m_resolveListCtrl.Show(GITSLC_SHOWCONFLICTED|GITSLC_SHOWINEXTERNALS, GITSLC_SHOWCONFLICTED);
150 InterlockedExchange(&m_bThreadRunning, FALSE);
151 return 0;
154 BOOL CResolveDlg::PreTranslateMessage(MSG* pMsg)
156 if (pMsg->message == WM_KEYDOWN)
158 switch (pMsg->wParam)
160 case VK_RETURN:
162 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
164 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
165 PostMessage(WM_COMMAND, IDOK);
166 return TRUE;
169 break;
170 case VK_F5:
172 if (!m_bThreadRunning)
174 InterlockedExchange(&m_bThreadRunning, TRUE);
176 if (!AfxBeginThread(ResolveThreadEntry, this))
178 InterlockedExchange(&m_bThreadRunning, FALSE);
179 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
183 break;
187 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
190 LRESULT CResolveDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
192 InterlockedExchange(&m_bThreadRunning, TRUE);
193 if (!AfxBeginThread(ResolveThreadEntry, this))
195 InterlockedExchange(&m_bThreadRunning, FALSE);
196 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
198 return 0;
201 LRESULT CResolveDlg::OnFileDropped(WPARAM, LPARAM lParam)
203 BringWindowToTop();
204 SetForegroundWindow();
205 SetActiveWindow();
206 // if multiple files/folders are dropped
207 // this handler is called for every single item
208 // separately.
209 // To avoid creating multiple refresh threads and
210 // causing crashes, we only add the items to the
211 // list control and start a timer.
212 // When the timer expires, we start the refresh thread,
213 // but only if it isn't already running - otherwise we
214 // restart the timer.
215 CTGitPath path;
216 path.SetFromWin((LPCTSTR)lParam);
218 // check whether the dropped file belongs to the very same repository
219 CString projectDir;
220 if (!path.HasAdminDir(&projectDir) || !CPathUtils::ArePathStringsEqual(g_Git.m_CurrentDir, projectDir))
221 return 0;
223 if (!m_resolveListCtrl.HasPath(path))
225 if (m_pathList.AreAllPathsFiles())
227 m_pathList.AddPath(path);
228 m_pathList.RemoveDuplicates();
230 else
232 // if the path list contains folders, we have to check whether
233 // our just (maybe) added path is a child of one of those. If it is
234 // a child of a folder already in the list, we must not add it. Otherwise
235 // that path could show up twice in the list.
236 bool bHasParentInList = false;
237 for (int i=0; i<m_pathList.GetCount(); ++i)
239 if (m_pathList[i].IsAncestorOf(path))
241 bHasParentInList = true;
242 break;
245 if (!bHasParentInList)
247 m_pathList.AddPath(path);
248 m_pathList.RemoveDuplicates();
252 m_resolveListCtrl.ResetChecked(path);
254 // Always start the timer, since the status of an existing item might have changed
255 SetTimer(REFRESHTIMER, 200, nullptr);
256 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) L": Item %s dropped, timer started\n", path.GetWinPath());
257 return 0;
260 void CResolveDlg::OnTimer(UINT_PTR nIDEvent)
262 switch (nIDEvent)
264 case REFRESHTIMER:
265 if (m_bThreadRunning)
267 SetTimer(REFRESHTIMER, 200, nullptr);
268 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
270 else
272 KillTimer(REFRESHTIMER);
273 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
274 OnSVNStatusListCtrlNeedsRefresh(0, 0);
276 break;
278 __super::OnTimer(nIDEvent);