Deduplicate code
[TortoiseGit.git] / src / TortoiseProc / AddDlg.cpp
blob12aaf278604a22213725419b4eef390db0f948a8
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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 "DirFileEnum.h"
24 #include "AddDlg.h"
25 //#include "SVNConfig.h"
26 #include "registry.h"
27 #include "AppUtils.h"
29 #define REFRESHTIMER 100
31 IMPLEMENT_DYNAMIC(CAddDlg, CResizableStandAloneDialog)
32 CAddDlg::CAddDlg(CWnd* pParent /*=NULL*/)
33 : CResizableStandAloneDialog(CAddDlg::IDD, pParent)
34 , m_bThreadRunning(FALSE)
35 , m_bCancelled(false)
36 , m_bIncludeIgnored(FALSE)
40 CAddDlg::~CAddDlg()
44 void CAddDlg::DoDataExchange(CDataExchange* pDX)
46 CResizableStandAloneDialog::DoDataExchange(pDX);
47 DDX_Control(pDX, IDC_ADDLIST, m_addListCtrl);
48 DDX_Control(pDX, IDC_SELECTALL, m_SelectAll);
49 DDX_Check(pDX, IDC_INCLUDE_IGNORED, m_bIncludeIgnored);
52 BEGIN_MESSAGE_MAP(CAddDlg, CResizableStandAloneDialog)
53 ON_BN_CLICKED(IDC_SELECTALL, OnBnClickedSelectall)
54 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_NEEDSREFRESH, OnSVNStatusListCtrlNeedsRefresh)
55 ON_REGISTERED_MESSAGE(CGitStatusListCtrl::GITSLNM_ADDFILE, OnFileDropped)
56 ON_WM_TIMER()
57 ON_BN_CLICKED(IDC_INCLUDE_IGNORED, &CAddDlg::OnBnClickedIncludeIgnored)
58 END_MESSAGE_MAP()
61 BOOL CAddDlg::OnInitDialog()
63 CResizableStandAloneDialog::OnInitDialog();
64 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
66 // initialize the svn status list control
67 m_addListCtrl.Init(GITSLC_COLEXT, _T("AddDlg"), GITSLC_POPALL ^ (GITSLC_POPADD|GITSLC_POPCOMMIT|GITSLC_POPCHANGELISTS), true, true, GITSLC_COLEXT | GITSLC_COLMODIFICATIONDATE | GITSLC_COLSIZE); // adding and committing is useless in the add dialog
68 m_addListCtrl.SetIgnoreRemoveOnly(); // when ignoring, don't add the parent folder since we're in the add dialog
69 m_addListCtrl.SetSelectButton(&m_SelectAll);
70 m_addListCtrl.SetConfirmButton((CButton*)GetDlgItem(IDOK));
71 m_addListCtrl.SetEmptyString(IDS_ERR_NOTHINGTOADD);
72 m_addListCtrl.SetCancelBool(&m_bCancelled);
73 m_addListCtrl.SetBackgroundImage(IDI_ADD_BKG);
74 m_addListCtrl.EnableFileDrop();
76 CString sWindowTitle;
77 GetWindowText(sWindowTitle);
78 CAppUtils::SetWindowTitle(m_hWnd, g_Git.CombinePath(m_pathList.GetCommonRoot().GetUIPathString()), sWindowTitle);
80 AdjustControlSize(IDC_SELECTALL);
81 AdjustControlSize(IDC_INCLUDE_IGNORED);
83 AddAnchor(IDC_ADDLIST, TOP_LEFT, BOTTOM_RIGHT);
84 AddAnchor(IDC_SELECTALL, BOTTOM_LEFT);
85 AddAnchor(IDC_INCLUDE_IGNORED, BOTTOM_LEFT);
86 AddAnchor(IDOK, BOTTOM_RIGHT);
87 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
88 AddAnchor(IDHELP, BOTTOM_RIGHT);
90 if (hWndExplorer)
91 CenterWindow(CWnd::FromHandle(hWndExplorer));
92 EnableSaveRestore(_T("AddDlg"));
94 //first start a thread to obtain the file list with the status without
95 //blocking the dialog
96 if(AfxBeginThread(AddThreadEntry, this) == NULL)
98 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
100 InterlockedExchange(&m_bThreadRunning, TRUE);
102 return TRUE;
105 void CAddDlg::OnOK()
107 if (m_bThreadRunning)
108 return;
110 // save only the files the user has selected into the path list
111 m_addListCtrl.WriteCheckedNamesToPathList(m_pathList);
113 CResizableStandAloneDialog::OnOK();
116 void CAddDlg::OnCancel()
118 m_bCancelled = true;
119 if (m_bThreadRunning)
120 return;
122 CResizableStandAloneDialog::OnCancel();
125 void CAddDlg::OnBnClickedSelectall()
127 UINT state = (m_SelectAll.GetState() & 0x0003);
128 if (state == BST_INDETERMINATE)
130 // It is not at all useful to manually place the checkbox into the indeterminate state...
131 // We will force this on to the unchecked state
132 state = BST_UNCHECKED;
133 m_SelectAll.SetCheck(state);
135 theApp.DoWaitCursor(1);
136 m_addListCtrl.SelectAll(state == BST_CHECKED);
137 theApp.DoWaitCursor(-1);
140 UINT CAddDlg::AddThreadEntry(LPVOID pVoid)
142 return ((CAddDlg*)pVoid)->AddThread();
145 UINT CAddDlg::AddThread()
147 // get the status of all selected file/folders recursively
148 // and show the ones which the user can add (i.e. the unversioned ones)
149 DialogEnableWindow(IDOK, false);
150 m_bCancelled = false;
151 m_addListCtrl.Clear();
152 if (!m_addListCtrl.GetStatus(&m_pathList, false, m_bIncludeIgnored != FALSE, true))
154 m_addListCtrl.SetEmptyString(m_addListCtrl.GetLastErrorMessage());
156 unsigned int dwShow = GITSLC_SHOWUNVERSIONED | GITSLC_SHOWDIRECTFILES | GITSLC_SHOWREMOVEDANDPRESENT;
157 if (m_bIncludeIgnored)
158 dwShow |= GITSLC_SHOWIGNORED;
159 m_addListCtrl.Show(dwShow);
161 InterlockedExchange(&m_bThreadRunning, FALSE);
162 return 0;
165 BOOL CAddDlg::PreTranslateMessage(MSG* pMsg)
167 if (pMsg->message == WM_KEYDOWN)
169 switch (pMsg->wParam)
171 case VK_RETURN:
173 if (GetAsyncKeyState(VK_CONTROL)&0x8000)
175 if ( GetDlgItem(IDOK)->IsWindowEnabled() )
177 PostMessage(WM_COMMAND, IDOK);
179 return TRUE;
182 break;
183 case VK_F5:
185 Refresh();
187 break;
191 return CResizableStandAloneDialog::PreTranslateMessage(pMsg);
194 LRESULT CAddDlg::OnSVNStatusListCtrlNeedsRefresh(WPARAM, LPARAM)
196 if(AfxBeginThread(AddThreadEntry, this) == NULL)
198 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
200 return 0;
203 LRESULT CAddDlg::OnFileDropped(WPARAM, LPARAM lParam)
205 BringWindowToTop();
206 SetForegroundWindow();
207 SetActiveWindow();
208 // if multiple files/folders are dropped
209 // this handler is called for every single item
210 // separately.
211 // To avoid creating multiple refresh threads and
212 // causing crashes, we only add the items to the
213 // list control and start a timer.
214 // When the timer expires, we start the refresh thread,
215 // but only if it isn't already running - otherwise we
216 // restart the timer.
217 CTGitPath path;
218 path.SetFromWin((LPCTSTR)lParam);
220 if (!m_addListCtrl.HasPath(path))
222 if (m_pathList.AreAllPathsFiles())
224 m_pathList.AddPath(path);
225 m_pathList.RemoveDuplicates();
227 else
229 // if the path list contains folders, we have to check whether
230 // our just (maybe) added path is a child of one of those. If it is
231 // a child of a folder already in the list, we must not add it. Otherwise
232 // that path could show up twice in the list.
233 bool bHasParentInList = false;
234 for (int i=0; i<m_pathList.GetCount(); ++i)
236 if (m_pathList[i].IsAncestorOf(path))
238 bHasParentInList = true;
239 break;
242 if (!bHasParentInList)
244 m_pathList.AddPath(path);
245 m_pathList.RemoveDuplicates();
250 // Always start the timer, since the status of an existing item might have changed
251 SetTimer(REFRESHTIMER, 200, NULL);
252 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Item %s dropped, timer started\n"), path.GetWinPath());
253 return 0;
256 void CAddDlg::Refresh()
258 if (!m_bThreadRunning)
260 if(AfxBeginThread(AddThreadEntry, this) == NULL)
262 CMessageBox::Show(this->m_hWnd, IDS_ERR_THREADSTARTFAILED, IDS_APPNAME, MB_OK | MB_ICONERROR);
264 else
265 InterlockedExchange(&m_bThreadRunning, TRUE);
269 void CAddDlg::OnTimer(UINT_PTR nIDEvent)
271 switch (nIDEvent)
273 case REFRESHTIMER:
274 if (m_bThreadRunning)
276 SetTimer(REFRESHTIMER, 200, NULL);
277 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Wait some more before refreshing\n");
279 else
281 KillTimer(REFRESHTIMER);
282 CTraceToOutputDebugString::Instance()(__FUNCTION__ ": Refreshing after items dropped\n");
283 OnSVNStatusListCtrlNeedsRefresh(0, 0);
285 break;
287 __super::OnTimer(nIDEvent);
290 void CAddDlg::OnBnClickedIncludeIgnored()
292 UpdateData();
293 Refresh();