Fixed blame crash at XP system
[TortoiseGit.git] / src / TortoiseProc / RevertDlg.cpp
blobdab62432bbf0c5e51fdd14ddb24765868cefbf44
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"
20 #include "TortoiseProc.h"
21 #include "messagebox.h"
22 #include "Revertdlg.h"
23 #include "Git.h"
24 #include "Registry.h"
25 #include ".\revertdlg.h"
26 #include "CommonResource.h"
28 #define REFRESHTIMER 100
30 IMPLEMENT_DYNAMIC(CRevertDlg, CResizableStandAloneDialog)
31 CRevertDlg::CRevertDlg(CWnd* pParent /*=NULL*/)
32 : CResizableStandAloneDialog(CRevertDlg::IDD, pParent)
33 , m_bSelectAll(TRUE)
34 , m_bThreadRunning(FALSE)
35 , m_bCancelled(false)
39 CRevertDlg::~CRevertDlg()
43 void CRevertDlg::DoDataExchange(CDataExchange* pDX)
45 CResizableStandAloneDialog::DoDataExchange(pDX);
46 DDX_Control(pDX, IDC_REVERTLIST, m_RevertList);
47 DDX_Check(pDX, IDC_SELECTALL, m_bSelectAll);
48 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
52 BEGIN_MESSAGE_MAP(CRevertDlg, CResizableStandAloneDialog)
53 ON_BN_CLICKED(IDHELP, OnBnClickedHelp)
54 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
55 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
56 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::SVNSLNM_ADDFILE, OnFileDropped)
57 ON_WM_TIMER()
58 END_MESSAGE_MAP()
62 BOOL CRevertDlg::OnInitDialog()
64 CResizableStandAloneDialog::OnInitDialog();
66 m_RevertList.Init(0, _T("RevertDlg"));
67 m_RevertList.SetConfirmButton((CButton*)GetDlgItem(IDOK));
68 m_RevertList.SetSelectButton(&m_SelectAll);
69 m_RevertList.SetCancelBool(&m_bCancelled);
70 m_RevertList.SetBackgroundImage(IDI_REVERT_BKG);
71 m_RevertList.EnableFileDrop();
73 GetWindowText(m_sWindowTitle);
75 AdjustControlSize(IDC_SELECTALL);
77 AddAnchor(IDC_REVERTLIST, TOP_LEFT, BOTTOM_RIGHT);
78 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
79 AddAnchor(IDC_UNVERSIONEDITEMS, BOTTOM_RIGHT);
80 AddAnchor(IDOK, BOTTOM_RIGHT);
81 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
82 AddAnchor(IDHELP, BOTTOM_RIGHT);
83 if (hWndExplorer)
84 CenterWindow(CWnd::FromHandle(hWndExplorer));
85 EnableSaveRestore(_T("RevertDlg"));
87 // first start a thread to obtain the file list with the status without
88 // blocking the dialog
89 if (AfxBeginThread(RevertThreadEntry, this)==0)
91 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
93 InterlockedExchange(&m_bThreadRunning, TRUE);
95 return TRUE;
98 UINT CRevertDlg::RevertThreadEntry(LPVOID pVoid)
100 return ((CRevertDlg*)pVoid)->RevertThread();
103 UINT CRevertDlg::RevertThread()
105 // get the status of all selected file/folders recursively
106 // and show the ones which can be reverted to the user
107 // in a list control.
108 DialogEnableWindow(IDOK, false);
109 m_bCancelled = false;
111 m_RevertList.Clear();
113 if (!m_RevertList.GetStatus(&m_pathList))
115 m_RevertList.SetEmptyString(m_RevertList.GetLastErrorMessage());
117 m_RevertList.Show(SVNSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | SVNSLC_SHOWDIRECTFILES | SVNSLC_SHOWEXTERNALFROMDIFFERENTREPO,
118 // do not select all files, only the ones the user has selected directly
119 SVNSLC_SHOWDIRECTFILES|SVNSLC_SHOWADDED);
121 CTGitPath commonDir = m_RevertList.GetCommonDirectory(false);
122 SetWindowText(m_sWindowTitle + _T(" - ") + commonDir.GetWinPathString());
124 if (m_RevertList.HasUnversionedItems())
126 if (DWORD(CRegStdWORD(_T("Software\\TortoiseGit\\UnversionedAsModified"), FALSE)))
128 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_SHOW);
130 else
131 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
133 else
134 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
136 InterlockedExchange(&m_bThreadRunning, FALSE);
137 RefreshCursor();
139 return 0;
142 void CRevertDlg::OnOK()
144 if (m_bThreadRunning)
145 return;
146 // save only the files the user has selected into the temporary file
147 m_bRecursive = TRUE;
148 for (int i=0; i<m_RevertList.GetItemCount(); ++i)
150 if (!m_RevertList.GetCheck(i))
152 m_bRecursive = FALSE;
154 else
156 m_selectedPathList.AddPath(*(CTGitPath*)m_RevertList.GetItemData(i));
157 #if 0
158 CGitStatusListCtrl::FileEntry * entry = m_RevertList.GetListEntry(i);
159 // add all selected entries to the list, except the ones with 'added'
160 // status: we later *delete* all the entries in the list before
161 // the actual revert is done (so the user has the reverted files
162 // still in the trash bin to recover from), but it's not good to
163 // delete added files because they're not restored by the revert.
164 if (entry->status != svn_wc_status_added)
165 m_selectedPathList.AddPath(entry->GetPath());
166 // if an entry inside an external is selected, we can't revert
167 // recursively anymore because the recursive revert stops at the
168 // external boundaries.
169 if (entry->IsInExternal())
170 m_bRecursive = FALSE;
171 #endif
174 if (!m_bRecursive)
176 m_RevertList.WriteCheckedNamesToPathList(m_pathList);
178 m_selectedPathList.SortByPathname();
180 CResizableStandAloneDialog::OnOK();
183 void CRevertDlg::OnCancel()
185 m_bCancelled = true;
186 if (m_bThreadRunning)
187 return;
189 CResizableStandAloneDialog::OnCancel();
192 void CRevertDlg::OnBnClickedHelp()
194 OnHelp();
197 void CRevertDlg::OnBnClickedSelectall()
199 UINT state = (m_SelectAll.GetState() & 0x0003);
200 if (state == BST_INDETERMINATE)
202 // It is not at all useful to manually place the checkbox into the indeterminate state...
203 // We will force this on to the unchecked state
204 state = BST_UNCHECKED;
205 m_SelectAll.SetCheck(state);
207 theApp.DoWaitCursor(1);
208 m_RevertList.SelectAll(state == BST_CHECKED);
209 theApp.DoWaitCursor(-1);
212 BOOL CRevertDlg::PreTranslateMessage(MSG* pMsg)
214 if (pMsg->message == WM_KEYDOWN)
216 switch (pMsg->wParam)
218 case VK_RETURN:
220 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
222 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
224 PostMessage(WM_COMMAND, IDOK);
226 return TRUE;
229 break;
230 case VK_F5:
232 if (!m_bThreadRunning)
234 if (AfxBeginThread(RevertThreadEntry, this)==0)
236 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
238 else
239 InterlockedExchange(&m_bThreadRunning, TRUE);
242 break;
246 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
249 LRESULT CRevertDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
251 if (AfxBeginThread(RevertThreadEntry, this)==0)
253 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
255 return 0;
258 LRESULT CRevertDlg::OnFileDropped(WPARAM, LPARAM lParam)
260 BringWindowToTop();
261 SetForegroundWindow();
262 SetActiveWindow();
263 // if multiple files/folders are dropped
264 // this handler is called for every single item
265 // separately.
266 // To avoid creating multiple refresh threads and
267 // causing crashes, we only add the items to the
268 // list control and start a timer.
269 // When the timer expires, we start the refresh thread,
270 // but only if it isn't already running - otherwise we
271 // restart the timer.
272 CTGitPath path;
273 path.SetFromWin((LPCTSTR)lParam);
275 if (!m_RevertList.HasPath(path))
277 if (m_pathList.AreAllPathsFiles())
279 m_pathList.AddPath(path);
280 m_pathList.RemoveDuplicates();
282 else
284 // if the path list contains folders, we have to check whether
285 // our just (maybe) added path is a child of one of those. If it is
286 // a child of a folder already in the list, we must not add it. Otherwise
287 // that path could show up twice in the list.
288 bool bHasParentInList = false;
289 for (int i=0; i<m_pathList.GetCount(); ++i)
291 if (m_pathList[i].IsAncestorOf(path))
293 bHasParentInList = true;
294 break;
297 if (!bHasParentInList)
299 m_pathList.AddPath(path);
300 m_pathList.RemoveDuplicates();
305 // Always start the timer, since the status of an existing item might have changed
306 SetTimer(REFRESHTIMER, 200, NULL);
307 ATLTRACE(_T("Item %s dropped, timer started\n"), path.GetWinPath());
308 return 0;
311 void CRevertDlg::OnTimer(UINT_PTR nIDEvent)
313 switch (nIDEvent)
315 case REFRESHTIMER:
316 if (m_bThreadRunning)
318 SetTimer(REFRESHTIMER, 200, NULL);
319 ATLTRACE("Wait some more before refreshing\n");
321 else
323 KillTimer(REFRESHTIMER);
324 ATLTRACE("Refreshing after items dropped\n");
325 OnSVNStatusListCtrlNeedsRefresh(0, 0);
327 break;
329 __super::OnTimer(nIDEvent);