Fixed issue #1542: Can send pull request email
[TortoiseGit.git] / src / TortoiseProc / RevertDlg.cpp
blobeacf4e874b6ffbcd2bd1dba11bbe732a7ee1f4f2
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)
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(IDHELP, OnBnClickedHelp)
56 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
57 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
58 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
59 ON_WM_TIMER()
60 END_MESSAGE_MAP()
64 BOOL CRevertDlg::OnInitDialog()
66 CResizableStandAloneDialog::OnInitDialog();
67 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
69 m_RevertList.Init(GITSLC_COLEXT | GITSLC_COLSTATUS | GITSLC_COLADD| GITSLC_COLDEL, _T("RevertDlg"));
70 m_RevertList.SetConfirmButton((CButton*)GetDlgItem(IDOK));
71 m_RevertList.SetSelectButton(&m_SelectAll);
72 m_RevertList.SetCancelBool(&m_bCancelled);
73 m_RevertList.SetBackgroundImage(IDI_REVERT_BKG);
74 m_RevertList.EnableFileDrop();
76 CString sWindowTitle;
77 GetWindowText(sWindowTitle);
78 if (m_pathList.GetCount() == 1)
79 CAppUtils::SetWindowTitle(m_hWnd, (g_Git.m_CurrentDir + _T("\\") + m_pathList[0].GetUIPathString()).TrimRight('\\'), sWindowTitle);
80 else
81 CAppUtils::SetWindowTitle(m_hWnd, m_pathList.GetCommonRoot().GetUIPathString(), sWindowTitle);
83 AdjustControlSize(IDC_SELECTALL);
85 AddAnchor(IDC_REVERTLIST, TOP_LEFT, BOTTOM_RIGHT);
86 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
87 AddAnchor(IDC_UNVERSIONEDITEMS, BOTTOM_RIGHT);
88 AddAnchor(IDOK, BOTTOM_RIGHT);
89 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
90 AddAnchor(IDHELP, BOTTOM_RIGHT);
91 if (hWndExplorer)
92 CenterWindow(CWnd::FromHandle(hWndExplorer));
93 EnableSaveRestore(_T("RevertDlg"));
95 // first start a thread to obtain the file list with the status without
96 // blocking the dialog
97 if (AfxBeginThread(RevertThreadEntry, this)==0)
99 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
101 InterlockedExchange(&m_bThreadRunning, TRUE);
103 return TRUE;
106 UINT CRevertDlg::RevertThreadEntry(LPVOID pVoid)
108 return ((CRevertDlg*)pVoid)->RevertThread();
111 UINT CRevertDlg::RevertThread()
113 // get the status of all selected file/folders recursively
114 // and show the ones which can be reverted to the user
115 // in a list control.
116 DialogEnableWindow(IDOK, false);
117 m_bCancelled = false;
119 m_RevertList.Clear();
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(_T("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 // save only the files the user has selected into the temporary file
152 m_bRecursive = TRUE;
153 for (int i=0; i<m_RevertList.GetItemCount(); ++i)
155 if (!m_RevertList.GetCheck(i))
157 m_bRecursive = FALSE;
159 else
161 m_selectedPathList.AddPath(*(CTGitPath*)m_RevertList.GetItemData(i));
162 #if 0
163 CGitStatusListCtrl::FileEntry * entry = m_RevertList.GetListEntry(i);
164 // add all selected entries to the list, except the ones with 'added'
165 // status: we later *delete* all the entries in the list before
166 // the actual revert is done (so the user has the reverted files
167 // still in the trash bin to recover from), but it's not good to
168 // delete added files because they're not restored by the revert.
169 if (entry->status != svn_wc_status_added)
170 m_selectedPathList.AddPath(entry->GetPath());
171 // if an entry inside an external is selected, we can't revert
172 // recursively anymore because the recursive revert stops at the
173 // external boundaries.
174 if (entry->IsInExternal())
175 m_bRecursive = FALSE;
176 #endif
179 if (!m_bRecursive)
181 m_RevertList.WriteCheckedNamesToPathList(m_pathList);
183 m_selectedPathList.SortByPathname();
185 CResizableStandAloneDialog::OnOK();
188 void CRevertDlg::OnCancel()
190 m_bCancelled = true;
191 if (m_bThreadRunning)
192 return;
194 CResizableStandAloneDialog::OnCancel();
197 void CRevertDlg::OnBnClickedHelp()
199 OnHelp();
202 void CRevertDlg::OnBnClickedSelectall()
204 UINT state = (m_SelectAll.GetState() & 0x0003);
205 if (state == BST_INDETERMINATE)
207 // It is not at all useful to manually place the checkbox into the indeterminate state...
208 // We will force this on to the unchecked state
209 state = BST_UNCHECKED;
210 m_SelectAll.SetCheck(state);
212 theApp.DoWaitCursor(1);
213 m_RevertList.SelectAll(state == BST_CHECKED);
214 theApp.DoWaitCursor(-1);
217 BOOL CRevertDlg::PreTranslateMessage(MSG* pMsg)
219 if (pMsg->message == WM_KEYDOWN)
221 switch (pMsg->wParam)
223 case VK_RETURN:
225 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
227 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
229 PostMessage(WM_COMMAND, IDOK);
231 return TRUE;
234 break;
235 case VK_F5:
237 if (!m_bThreadRunning)
239 if (AfxBeginThread(RevertThreadEntry, this)==0)
241 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
243 else
244 InterlockedExchange(&m_bThreadRunning, TRUE);
247 break;
251 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
254 LRESULT CRevertDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
256 if (AfxBeginThread(RevertThreadEntry, this)==0)
258 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
260 return 0;
263 LRESULT CRevertDlg::OnFileDropped(WPARAM, LPARAM lParam)
265 BringWindowToTop();
266 SetForegroundWindow();
267 SetActiveWindow();
268 // if multiple files/folders are dropped
269 // this handler is called for every single item
270 // separately.
271 // To avoid creating multiple refresh threads and
272 // causing crashes, we only add the items to the
273 // list control and start a timer.
274 // When the timer expires, we start the refresh thread,
275 // but only if it isn't already running - otherwise we
276 // restart the timer.
277 CTGitPath path;
278 path.SetFromWin((LPCTSTR)lParam);
280 if (!m_RevertList.HasPath(path))
282 if (m_pathList.AreAllPathsFiles())
284 m_pathList.AddPath(path);
285 m_pathList.RemoveDuplicates();
287 else
289 // if the path list contains folders, we have to check whether
290 // our just (maybe) added path is a child of one of those. If it is
291 // a child of a folder already in the list, we must not add it. Otherwise
292 // that path could show up twice in the list.
293 bool bHasParentInList = false;
294 for (int i=0; i<m_pathList.GetCount(); ++i)
296 if (m_pathList[i].IsAncestorOf(path))
298 bHasParentInList = true;
299 break;
302 if (!bHasParentInList)
304 m_pathList.AddPath(path);
305 m_pathList.RemoveDuplicates();
310 // Always start the timer, since the status of an existing item might have changed
311 SetTimer(REFRESHTIMER, 200, NULL);
312 ATLTRACE(_T("Item %s dropped, timer started\n"), path.GetWinPath());
313 return 0;
316 void CRevertDlg::OnTimer(UINT_PTR nIDEvent)
318 switch (nIDEvent)
320 case REFRESHTIMER:
321 if (m_bThreadRunning)
323 SetTimer(REFRESHTIMER, 200, NULL);
324 ATLTRACE("Wait some more before refreshing\n");
326 else
328 KillTimer(REFRESHTIMER);
329 ATLTRACE("Refreshing after items dropped\n");
330 OnSVNStatusListCtrlNeedsRefresh(0, 0);
332 break;
334 __super::OnTimer(nIDEvent);