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.
21 #include "TortoiseProc.h"
22 #include "messagebox.h"
23 #include "Revertdlg.h"
26 #include ".\revertdlg.h"
29 #define REFRESHTIMER 100
31 IMPLEMENT_DYNAMIC(CRevertDlg
, CResizableStandAloneDialog
)
32 CRevertDlg::CRevertDlg(CWnd
* pParent
/*=NULL*/)
33 : CResizableStandAloneDialog(CRevertDlg::IDD
, pParent
)
35 , m_bThreadRunning(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
)
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();
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
);
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
);
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
);
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 for (int i
= 0; i
< m_RevertList
.GetItemCount(); ++i
)
131 CTGitPath
*path
= (CTGitPath
*)m_RevertList
.GetItemData(i
);
132 for (int j
= 0; j
< m_pathList
.GetCount(); ++j
)
133 if (m_pathList
[j
].GetGitPathString() == path
->GetGitPathString())
134 m_RevertList
.SetCheck(i
, TRUE
);
137 if (m_RevertList
.HasUnversionedItems())
139 if (DWORD(CRegStdDWORD(_T("Software\\TortoiseGit\\UnversionedAsModified"), FALSE
)))
141 GetDlgItem(IDC_UNVERSIONEDITEMS
)->ShowWindow(SW_SHOW
);
144 GetDlgItem(IDC_UNVERSIONEDITEMS
)->ShowWindow(SW_HIDE
);
147 GetDlgItem(IDC_UNVERSIONEDITEMS
)->ShowWindow(SW_HIDE
);
149 InterlockedExchange(&m_bThreadRunning
, FALSE
);
155 void CRevertDlg::OnOK()
157 if (m_bThreadRunning
)
159 // save only the files the user has selected into the temporary file
161 for (int i
=0; i
<m_RevertList
.GetItemCount(); ++i
)
163 if (!m_RevertList
.GetCheck(i
))
165 m_bRecursive
= FALSE
;
169 m_selectedPathList
.AddPath(*(CTGitPath
*)m_RevertList
.GetItemData(i
));
171 CGitStatusListCtrl::FileEntry
* entry
= m_RevertList
.GetListEntry(i
);
172 // add all selected entries to the list, except the ones with 'added'
173 // status: we later *delete* all the entries in the list before
174 // the actual revert is done (so the user has the reverted files
175 // still in the trash bin to recover from), but it's not good to
176 // delete added files because they're not restored by the revert.
177 if (entry
->status
!= svn_wc_status_added
)
178 m_selectedPathList
.AddPath(entry
->GetPath());
179 // if an entry inside an external is selected, we can't revert
180 // recursively anymore because the recursive revert stops at the
181 // external boundaries.
182 if (entry
->IsInExternal())
183 m_bRecursive
= FALSE
;
189 m_RevertList
.WriteCheckedNamesToPathList(m_pathList
);
191 m_selectedPathList
.SortByPathname();
193 CResizableStandAloneDialog::OnOK();
196 void CRevertDlg::OnCancel()
199 if (m_bThreadRunning
)
202 CResizableStandAloneDialog::OnCancel();
205 void CRevertDlg::OnBnClickedHelp()
210 void CRevertDlg::OnBnClickedSelectall()
212 UINT state
= (m_SelectAll
.GetState() & 0x0003);
213 if (state
== BST_INDETERMINATE
)
215 // It is not at all useful to manually place the checkbox into the indeterminate state...
216 // We will force this on to the unchecked state
217 state
= BST_UNCHECKED
;
218 m_SelectAll
.SetCheck(state
);
220 theApp
.DoWaitCursor(1);
221 m_RevertList
.SelectAll(state
== BST_CHECKED
);
222 theApp
.DoWaitCursor(-1);
225 BOOL
CRevertDlg::PreTranslateMessage(MSG
* pMsg
)
227 if (pMsg
->message
== WM_KEYDOWN
)
229 switch (pMsg
->wParam
)
233 if (GetAsyncKeyState(VK_CONTROL
)&0x8000)
235 if ( GetDlgItem(IDOK
)->IsWindowEnabled() )
237 PostMessage(WM_COMMAND
, IDOK
);
245 if (!m_bThreadRunning
)
247 if (AfxBeginThread(RevertThreadEntry
, this)==0)
249 CMessageBox::Show(this->m_hWnd
, IDS_ERR_THREADSTARTFAILED
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
252 InterlockedExchange(&m_bThreadRunning
, TRUE
);
259 return CResizableStandAloneDialog::PreTranslateMessage(pMsg
);
262 LRESULT
CRevertDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM
, LPARAM
)
264 if (AfxBeginThread(RevertThreadEntry
, this)==0)
266 CMessageBox::Show(this->m_hWnd
, IDS_ERR_THREADSTARTFAILED
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
271 LRESULT
CRevertDlg::OnFileDropped(WPARAM
, LPARAM lParam
)
274 SetForegroundWindow();
276 // if multiple files/folders are dropped
277 // this handler is called for every single item
279 // To avoid creating multiple refresh threads and
280 // causing crashes, we only add the items to the
281 // list control and start a timer.
282 // When the timer expires, we start the refresh thread,
283 // but only if it isn't already running - otherwise we
284 // restart the timer.
286 path
.SetFromWin((LPCTSTR
)lParam
);
288 if (!m_RevertList
.HasPath(path
))
290 if (m_pathList
.AreAllPathsFiles())
292 m_pathList
.AddPath(path
);
293 m_pathList
.RemoveDuplicates();
297 // if the path list contains folders, we have to check whether
298 // our just (maybe) added path is a child of one of those. If it is
299 // a child of a folder already in the list, we must not add it. Otherwise
300 // that path could show up twice in the list.
301 bool bHasParentInList
= false;
302 for (int i
=0; i
<m_pathList
.GetCount(); ++i
)
304 if (m_pathList
[i
].IsAncestorOf(path
))
306 bHasParentInList
= true;
310 if (!bHasParentInList
)
312 m_pathList
.AddPath(path
);
313 m_pathList
.RemoveDuplicates();
318 // Always start the timer, since the status of an existing item might have changed
319 SetTimer(REFRESHTIMER
, 200, NULL
);
320 ATLTRACE(_T("Item %s dropped, timer started\n"), path
.GetWinPath());
324 void CRevertDlg::OnTimer(UINT_PTR nIDEvent
)
329 if (m_bThreadRunning
)
331 SetTimer(REFRESHTIMER
, 200, NULL
);
332 ATLTRACE("Wait some more before refreshing\n");
336 KillTimer(REFRESHTIMER
);
337 ATLTRACE("Refreshing after items dropped\n");
338 OnSVNStatusListCtrlNeedsRefresh(0, 0);
342 __super::OnTimer(nIDEvent
);