Use CAutoGeneralHandle
[TortoiseGit.git] / src / TortoiseProc / RevertDlg.cpp
blob9fb71797fa7f005b8f46627b33675d4356971446
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2009-2012 - 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 "Registry.h"
26 #include ".\revertdlg.h"
27 #include "AppUtils.h"
29 #define REFRESHTIMER 100
31 IMPLEMENT_DYNAMIC(CRevertDlg, CResizableStandAloneDialog)
32 CRevertDlg::CRevertDlg(CWnd* pParent /*=NULL*/)
33 : CResizableStandAloneDialog(CRevertDlg::IDD, pParent)
34 , m_bSelectAll(TRUE)
35 , m_bThreadRunning(FALSE)
36 , m_bCancelled(false)
40 CRevertDlg::~CRevertDlg()
44 void CRevertDlg::DoDataExchange(CDataExchange* pDX)
46 CResizableStandAloneDialog::DoDataExchange(pDX);
47 DDX_Control(pDX, IDC_REVERTLIST, m_RevertList);
48 DDX_Check(pDX, IDC_SELECTALL, m_bSelectAll);
49 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
53 BEGIN_MESSAGE_MAP(CRevertDlg, CResizableStandAloneDialog)
54 ON_BN_CLICKED(IDHELP, OnBnClickedHelp)
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, _T("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 if (m_pathList.GetCount() == 1)
78 CAppUtils::SetWindowTitle(m_hWnd, (g_Git.m_CurrentDir + _T("\\") + m_pathList[0].GetUIPathString()).TrimRight('\\'), sWindowTitle);
79 else
80 CAppUtils::SetWindowTitle(m_hWnd, m_pathList.GetCommonRoot().GetUIPathString(), sWindowTitle);
82 AdjustControlSize(IDC_SELECTALL);
84 AddAnchor(IDC_REVERTLIST, TOP_LEFT, BOTTOM_RIGHT);
85 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
86 AddAnchor(IDC_UNVERSIONEDITEMS, BOTTOM_RIGHT);
87 AddAnchor(IDOK, BOTTOM_RIGHT);
88 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
89 AddAnchor(IDHELP, BOTTOM_RIGHT);
90 if (hWndExplorer)
91 CenterWindow(CWnd::FromHandle(hWndExplorer));
92 EnableSaveRestore(_T("RevertDlg"));
94 // first start a thread to obtain the file list with the status without
95 // blocking the dialog
96 if (AfxBeginThread(RevertThreadEntry, this)==0)
98 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
100 InterlockedExchange(&m_bThreadRunning, TRUE);
102 return TRUE;
105 UINT CRevertDlg::RevertThreadEntry(LPVOID pVoid)
107 return ((CRevertDlg*)pVoid)->RevertThread();
110 UINT CRevertDlg::RevertThread()
112 // get the status of all selected file/folders recursively
113 // and show the ones which can be reverted to the user
114 // in a list control.
115 DialogEnableWindow(IDOK, false);
116 m_bCancelled = false;
118 m_RevertList.Clear();
120 if (!m_RevertList.GetStatus(&m_pathList))
122 m_RevertList.SetEmptyString(m_RevertList.GetLastErrorMessage());
124 m_RevertList.Show(GITSLC_SHOWVERSIONEDBUTNORMALANDEXTERNALSFROMDIFFERENTREPOS | GITSLC_SHOWDIRECTFILES | GITSLC_SHOWEXTERNALFROMDIFFERENTREPO,
125 // do not select all files, only the ones the user has selected directly
126 GITSLC_SHOWDIRECTFILES|GITSLC_SHOWADDED);
128 if (m_RevertList.HasUnversionedItems())
130 if (DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\UnversionedAsModified"), FALSE)))
132 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_SHOW);
134 else
135 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
137 else
138 GetDlgItem(IDC_UNVERSIONEDITEMS)->ShowWindow(SW_HIDE);
140 InterlockedExchange(&m_bThreadRunning, FALSE);
141 RefreshCursor();
143 return 0;
146 void CRevertDlg::OnOK()
148 if (m_bThreadRunning)
149 return;
150 // save only the files the user has selected into the temporary file
151 m_bRecursive = TRUE;
152 for (int i=0; i<m_RevertList.GetItemCount(); ++i)
154 if (!m_RevertList.GetCheck(i))
156 m_bRecursive = FALSE;
158 else
160 m_selectedPathList.AddPath(*(CTGitPath*)m_RevertList.GetItemData(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)
180 m_RevertList.WriteCheckedNamesToPathList(m_pathList);
182 m_selectedPathList.SortByPathname();
184 CResizableStandAloneDialog::OnOK();
187 void CRevertDlg::OnCancel()
189 m_bCancelled = true;
190 if (m_bThreadRunning)
191 return;
193 CResizableStandAloneDialog::OnCancel();
196 void CRevertDlg::OnBnClickedHelp()
198 OnHelp();
201 void CRevertDlg::OnBnClickedSelectall()
203 UINT state = (m_SelectAll.GetState() & 0x0003);
204 if (state == BST_INDETERMINATE)
206 // It is not at all useful to manually place the checkbox into the indeterminate state...
207 // We will force this on to the unchecked state
208 state = BST_UNCHECKED;
209 m_SelectAll.SetCheck(state);
211 theApp.DoWaitCursor(1);
212 m_RevertList.SelectAll(state == BST_CHECKED);
213 theApp.DoWaitCursor(-1);
216 BOOL CRevertDlg::PreTranslateMessage(MSG* pMsg)
218 if (pMsg->message == WM_KEYDOWN)
220 switch (pMsg->wParam)
222 case VK_RETURN:
224 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
226 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
228 PostMessage(WM_COMMAND, IDOK);
230 return TRUE;
233 break;
234 case VK_F5:
236 if (!m_bThreadRunning)
238 if (AfxBeginThread(RevertThreadEntry, this)==0)
240 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
242 else
243 InterlockedExchange(&m_bThreadRunning, TRUE);
246 break;
250 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
253 LRESULT CRevertDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
255 if (AfxBeginThread(RevertThreadEntry, this)==0)
257 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
259 return 0;
262 LRESULT CRevertDlg::OnFileDropped(WPARAM, LPARAM lParam)
264 BringWindowToTop();
265 SetForegroundWindow();
266 SetActiveWindow();
267 // if multiple files/folders are dropped
268 // this handler is called for every single item
269 // separately.
270 // To avoid creating multiple refresh threads and
271 // causing crashes, we only add the items to the
272 // list control and start a timer.
273 // When the timer expires, we start the refresh thread,
274 // but only if it isn't already running - otherwise we
275 // restart the timer.
276 CTGitPath path;
277 path.SetFromWin((LPCTSTR)lParam);
279 if (!m_RevertList.HasPath(path))
281 if (m_pathList.AreAllPathsFiles())
283 m_pathList.AddPath(path);
284 m_pathList.RemoveDuplicates();
286 else
288 // if the path list contains folders, we have to check whether
289 // our just (maybe) added path is a child of one of those. If it is
290 // a child of a folder already in the list, we must not add it. Otherwise
291 // that path could show up twice in the list.
292 bool bHasParentInList = false;
293 for (int i=0; i<m_pathList.GetCount(); ++i)
295 if (m_pathList[i].IsAncestorOf(path))
297 bHasParentInList = true;
298 break;
301 if (!bHasParentInList)
303 m_pathList.AddPath(path);
304 m_pathList.RemoveDuplicates();
309 // Always start the timer, since the status of an existing item might have changed
310 SetTimer(REFRESHTIMER, 200, NULL);
311 ATLTRACE(_T("Item %s dropped, timer started\n"), path.GetWinPath());
312 return 0;
315 void CRevertDlg::OnTimer(UINT_PTR nIDEvent)
317 switch (nIDEvent)
319 case REFRESHTIMER:
320 if (m_bThreadRunning)
322 SetTimer(REFRESHTIMER, 200, NULL);
323 ATLTRACE("Wait some more before refreshing\n");
325 else
327 KillTimer(REFRESHTIMER);
328 ATLTRACE("Refreshing after items dropped\n");
329 OnSVNStatusListCtrlNeedsRefresh(0, 0);
331 break;
333 __super::OnTimer(nIDEvent);