Fix compilation warnings
[TortoiseGit.git] / src / TortoiseProc / Settings / Settings.cpp
blob2f97934e782309c9697177f5fd7bfa9723666def
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-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 "Settings.h"
23 #include "MessageBox.h"
24 #include "..\..\TGitCache\CacheInterface.h"
25 #include "GitAdminDir.h"
26 #include "AppUtils.h"
28 IMPLEMENT_DYNAMIC(CSettings, CTreePropSheet)
29 CSettings::CSettings(UINT nIDCaption,CTGitPath *cmdPath, CWnd* pParentWnd, UINT iSelectPage)
30 :CTreePropSheet(nIDCaption, pParentWnd, iSelectPage)
32 if(cmdPath)
33 this->m_CmdPath=*cmdPath;
35 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
36 AddPropPages();
39 CSettings::~CSettings()
41 RemovePropPages();
44 void CSettings::AddPropPages()
46 m_pMainPage = new CSetMainPage();
47 m_pOverlayPage = new CSetOverlayPage();
48 m_pOverlaysPage = new CSetOverlayIcons();
49 m_pOverlayHandlersPage = new CSetOverlayHandlers();
50 m_pProxyPage = new CSetProxyPage();
51 m_pProgsDiffPage = new CSettingsProgsDiff();
52 m_pProgsMergePage = new CSettingsProgsMerge();
53 m_pProgsAlternativeEditor = new CSettingsProgsAlternativeEditor();
54 m_pLookAndFeelPage = new CSetLookAndFeelPage();
56 m_pExtMenu = new CSetExtMenu();
58 m_pDialogsPage = new CSetDialogs();
59 m_pColorsPage = new CSettingsColors();
60 m_pColorsPage2 = new CSettingsColors2();
61 m_pColorsPage3 = new CSettingsColors3();
62 m_pSavedPage = new CSetSavedDataPage();
63 m_pHooksPage = new CSetHooks();
64 m_pBugTraqPage = new CSetBugTraq();
65 m_pTBlamePage = new CSettingsTBlame();
66 m_pGitConfig = new CSettingGitConfig();
67 m_pGitRemote = new CSettingGitRemote(m_CmdPath.GetWinPath());
68 m_pGitCredential = new CSettingGitCredential(m_CmdPath.GetWinPath());
69 m_pBugtraqConfig = new CSettingsBugtraqConfig(m_CmdPath.GetWinPath());
70 m_pAdvanced = new CSettingsAdvanced();
72 SetPageIcon(m_pExtMenu,m_pExtMenu->GetIconID());
74 SetPageIcon(m_pMainPage, m_pMainPage->GetIconID());
75 SetPageIcon(m_pOverlayPage, m_pOverlayPage->GetIconID());
76 SetPageIcon(m_pOverlaysPage, m_pOverlaysPage->GetIconID());
77 SetPageIcon(m_pOverlayHandlersPage, m_pOverlayHandlersPage->GetIconID());
78 SetPageIcon(m_pProxyPage, m_pProxyPage->GetIconID());
79 SetPageIcon(m_pProgsDiffPage, m_pProgsDiffPage->GetIconID());
80 SetPageIcon(m_pProgsMergePage, m_pProgsMergePage->GetIconID());
81 SetPageIcon(m_pProgsAlternativeEditor, m_pProgsAlternativeEditor->GetIconID());
82 SetPageIcon(m_pLookAndFeelPage, m_pLookAndFeelPage->GetIconID());
83 SetPageIcon(m_pDialogsPage, m_pDialogsPage->GetIconID());
84 SetPageIcon(m_pColorsPage, m_pColorsPage->GetIconID());
85 SetPageIcon(m_pColorsPage2, m_pColorsPage2->GetIconID());
86 SetPageIcon(m_pColorsPage3, m_pColorsPage3->GetIconID());
88 SetPageIcon(m_pSavedPage, m_pSavedPage->GetIconID());
89 SetPageIcon(m_pHooksPage, m_pHooksPage->GetIconID());
91 SetPageIcon(m_pGitConfig, m_pGitConfig->GetIconID());
92 SetPageIcon(m_pGitRemote, m_pGitRemote->GetIconID());
93 SetPageIcon(m_pGitCredential, m_pGitCredential->GetIconID());
94 SetPageIcon(m_pBugTraqPage, m_pBugTraqPage->GetIconID());
95 SetPageIcon(m_pBugtraqConfig, m_pBugtraqConfig->GetIconID());
96 SetPageIcon(m_pTBlamePage, m_pTBlamePage->GetIconID());
97 SetPageIcon(m_pAdvanced, m_pAdvanced->GetIconID());
99 AddPage(m_pMainPage);
100 AddPage(m_pOverlayPage);
101 AddPage(m_pOverlaysPage);
102 AddPage(m_pOverlayHandlersPage);
103 AddPage(m_pProxyPage);
104 AddPage(m_pProgsDiffPage);
105 AddPage(m_pProgsMergePage);
106 AddPage(m_pLookAndFeelPage);
107 AddPage(m_pExtMenu);
108 AddPage(m_pDialogsPage);
109 AddPage(m_pColorsPage);
110 AddPage(m_pColorsPage2);
111 AddPage(m_pColorsPage3);
112 AddPage(m_pProgsAlternativeEditor);
113 AddPage(m_pSavedPage);
115 AddPage(m_pGitConfig);
117 if (g_GitAdminDir.HasAdminDir(this->m_CmdPath.GetWinPath()) || g_GitAdminDir.IsBareRepo(this->m_CmdPath.GetWinPath()))
119 AddPage(m_pGitRemote);
121 AddPage(m_pGitCredential);
122 AddPage(m_pHooksPage);
123 AddPage(m_pBugTraqPage);
124 if (g_GitAdminDir.HasAdminDir(this->m_CmdPath.GetWinPath()))
126 AddPage(m_pBugtraqConfig);
128 AddPage(m_pTBlamePage);
129 AddPage(m_pAdvanced);
132 void CSettings::RemovePropPages()
134 delete m_pMainPage;
135 delete m_pOverlayPage;
136 delete m_pOverlaysPage;
137 delete m_pOverlayHandlersPage;
138 delete m_pProxyPage;
139 delete m_pProgsDiffPage;
140 delete m_pProgsMergePage;
141 delete m_pProgsAlternativeEditor;
142 delete m_pLookAndFeelPage;
143 delete m_pDialogsPage;
144 delete m_pColorsPage;
145 delete m_pColorsPage2;
146 delete m_pColorsPage3;
147 delete m_pSavedPage;
148 delete m_pHooksPage;
149 delete m_pBugTraqPage;
150 delete m_pTBlamePage;
152 delete m_pGitConfig;
153 delete m_pGitRemote;
154 delete m_pGitCredential;
155 delete m_pBugtraqConfig;
156 delete m_pExtMenu;
157 delete m_pAdvanced;
161 void CSettings::HandleRestart()
163 int restart = ISettingsPropPage::Restart_None;
164 restart |= m_pMainPage->GetRestart();
165 restart |= m_pOverlayPage->GetRestart();
166 restart |= m_pOverlaysPage->GetRestart();
167 restart |= m_pOverlayHandlersPage->GetRestart();
168 restart |= m_pProxyPage->GetRestart();
169 restart |= m_pProgsDiffPage->GetRestart();
170 restart |= m_pProgsMergePage->GetRestart();
171 restart |= m_pProgsAlternativeEditor->GetRestart();
172 restart |= m_pLookAndFeelPage->GetRestart();
173 restart |= m_pDialogsPage->GetRestart();
174 restart |= m_pColorsPage->GetRestart();
175 restart |= m_pColorsPage2->GetRestart();
176 restart |= m_pColorsPage3->GetRestart();
177 restart |= m_pSavedPage->GetRestart();
178 restart |= m_pHooksPage->GetRestart();
179 restart |= m_pBugTraqPage->GetRestart();
180 restart |= m_pTBlamePage->GetRestart();
182 restart |= m_pGitConfig->GetRestart();
183 restart |= m_pGitRemote->GetRestart();
184 restart |= m_pGitCredential->GetRestart();
185 restart |= m_pBugTraqPage->GetRestart();
186 restart |= m_pExtMenu->GetRestart();
187 restart |= m_pAdvanced->GetRestart();
189 if (restart & ISettingsPropPage::Restart_System)
191 DWORD_PTR res = 0;
192 ::SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0, SMTO_ABORTIFHUNG, 20, &res);
193 CMessageBox::Show(NULL, IDS_SETTINGS_RESTARTSYSTEM, IDS_APPNAME, MB_ICONINFORMATION);
195 if (restart & ISettingsPropPage::Restart_Cache)
197 DWORD_PTR res = 0;
198 ::SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0, SMTO_ABORTIFHUNG, 20, &res);
199 // tell the cache to refresh everything
200 HANDLE hPipe = CreateFile(
201 GetCacheCommandPipeName(), // pipe name
202 GENERIC_READ | // read and write access
203 GENERIC_WRITE,
204 0, // no sharing
205 NULL, // default security attributes
206 OPEN_EXISTING, // opens existing pipe
207 FILE_FLAG_OVERLAPPED, // default attributes
208 NULL); // no template file
211 if (hPipe != INVALID_HANDLE_VALUE)
213 // The pipe connected; change to message-read mode.
214 DWORD dwMode;
216 dwMode = PIPE_READMODE_MESSAGE;
217 if (SetNamedPipeHandleState(
218 hPipe, // pipe handle
219 &dwMode, // new pipe mode
220 NULL, // don't set maximum bytes
221 NULL)) // don't set maximum time
223 DWORD cbWritten;
224 TGITCacheCommand cmd;
225 SecureZeroMemory(&cmd, sizeof(TGITCacheCommand));
226 cmd.command = TGITCACHECOMMAND_REFRESHALL;
227 BOOL fSuccess = WriteFile(
228 hPipe, // handle to pipe
229 &cmd, // buffer to write from
230 sizeof(cmd), // number of bytes to write
231 &cbWritten, // number of bytes written
232 NULL); // not overlapped I/O
234 if (! fSuccess || sizeof(cmd) != cbWritten)
236 DisconnectNamedPipe(hPipe);
237 CloseHandle(hPipe);
238 hPipe = INVALID_HANDLE_VALUE;
240 if (hPipe != INVALID_HANDLE_VALUE)
242 // now tell the cache we don't need it's command thread anymore
243 DWORD cbWritten;
244 TGITCacheCommand cmd;
245 SecureZeroMemory(&cmd, sizeof(TGITCacheCommand));
246 cmd.command = TGITCACHECOMMAND_END;
247 WriteFile(
248 hPipe, // handle to pipe
249 &cmd, // buffer to write from
250 sizeof(cmd), // number of bytes to write
251 &cbWritten, // number of bytes written
252 NULL); // not overlapped I/O
253 DisconnectNamedPipe(hPipe);
254 CloseHandle(hPipe);
255 hPipe = INVALID_HANDLE_VALUE;
258 else
260 ATLTRACE("SetNamedPipeHandleState failed");
261 CloseHandle(hPipe);
267 BEGIN_MESSAGE_MAP(CSettings, CTreePropSheet)
268 ON_WM_QUERYDRAGICON()
269 ON_WM_PAINT()
270 END_MESSAGE_MAP()
272 BOOL CSettings::OnInitDialog()
274 BOOL bResult = CTreePropSheet::OnInitDialog();
275 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
277 SetIcon(m_hIcon, TRUE); // Set big icon
278 SetIcon(m_hIcon, FALSE); // Set small icon
279 if (g_GitAdminDir.HasAdminDir(this->m_CmdPath.GetWinPath()) || g_GitAdminDir.IsBareRepo(this->m_CmdPath.GetWinPath()))
281 CString title;
282 GetWindowText(title);
283 SetWindowText(g_Git.m_CurrentDir + _T(" - ") + title);
286 CenterWindow(CWnd::FromHandle(hWndExplorer));
288 if (this->m_DefaultPage == _T("gitremote"))
290 this->SetActivePage(this->m_pGitRemote);
291 this->m_pGitRemote->m_bNoFetch = true;
293 else if (this->m_DefaultPage == _T("gitconfig"))
295 this->SetActivePage(this->m_pGitConfig);
297 else if (this->m_DefaultPage == _T("gitcredential"))
299 this->SetActivePage(this->m_pGitCredential);
301 else if (this->m_DefaultPage == _T("main"))
303 this->SetActivePage(this->m_pMainPage);
305 else if (this->m_DefaultPage == _T("overlay"))
307 this->SetActivePage(this->m_pOverlayPage);
309 else if (this->m_DefaultPage == _T("overlays"))
311 this->SetActivePage(this->m_pOverlaysPage);
313 else if (this->m_DefaultPage == _T("overlayshandlers"))
315 this->SetActivePage(this->m_pOverlayHandlersPage);
317 else if (this->m_DefaultPage == _T("proxy"))
319 this->SetActivePage(this->m_pProxyPage);
321 else if (this->m_DefaultPage == _T("diff"))
323 this->SetActivePage(this->m_pProgsDiffPage);
325 else if (this->m_DefaultPage == _T("merge"))
327 this->SetActivePage(this->m_pProgsMergePage);
329 else if (this->m_DefaultPage == _T("alternativeeditor"))
331 this->SetActivePage(this->m_pProgsAlternativeEditor);
333 else if (this->m_DefaultPage == _T("look"))
335 this->SetActivePage(this->m_pLookAndFeelPage);
337 else if (this->m_DefaultPage == _T("dialog"))
339 this->SetActivePage(this->m_pDialogsPage);
341 else if (this->m_DefaultPage == _T("color1"))
343 this->SetActivePage(this->m_pColorsPage);
345 else if (this->m_DefaultPage == _T("color2"))
347 this->SetActivePage(this->m_pColorsPage2);
349 else if (this->m_DefaultPage == _T("color3"))
351 this->SetActivePage(this->m_pColorsPage3);
353 else if (this->m_DefaultPage == _T("save"))
355 this->SetActivePage(this->m_pSavedPage);
357 else if (this->m_DefaultPage == _T("advanced"))
359 this->SetActivePage(this->m_pAdvanced);
361 else if (this->m_DefaultPage == _T("blame"))
363 this->SetActivePage(this->m_pTBlamePage);
365 else if (g_GitAdminDir.HasAdminDir(this->m_CmdPath.GetWinPath()) || g_GitAdminDir.IsBareRepo(this->m_CmdPath.GetWinPath()))
367 this->SetActivePage(this->m_pGitConfig);
369 return bResult;
372 void CSettings::OnPaint()
374 if (IsIconic())
376 CPaintDC dc(this); // device context for painting
378 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
380 // Center icon in client rectangle
381 int cxIcon = GetSystemMetrics(SM_CXICON);
382 int cyIcon = GetSystemMetrics(SM_CYICON);
383 CRect rect;
384 GetClientRect(&rect);
385 int x = (rect.Width() - cxIcon + 1) / 2;
386 int y = (rect.Height() - cyIcon + 1) / 2;
388 // Draw the icon
389 dc.DrawIcon(x, y, m_hIcon);
391 else
393 CTreePropSheet::OnPaint();
397 HCURSOR CSettings::OnQueryDragIcon()
399 return static_cast<HCURSOR>(m_hIcon);