OSDN Git Service

Add proptery page for git
[tortoisegit/TortoiseGitJp.git] / src / TortoiseShell / SVNPropertyPage.cpp
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2008 - TortoiseGit\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "stdafx.h"\r
20 \r
21 #include "ShellExt.h"\r
22 #include "svnpropertypage.h"\r
23 #include "UnicodeUtils.h"\r
24 #include "PathUtils.h"\r
25 #include "GitStatus.h"\r
26 \r
27 #define MAX_STRING_LENGTH               4096                    //should be big enough\r
28 \r
29 // Nonmember function prototypes\r
30 BOOL CALLBACK PageProc (HWND, UINT, WPARAM, LPARAM);\r
31 UINT CALLBACK PropPageCallbackProc ( HWND hwnd, UINT uMsg, LPPROPSHEETPAGE ppsp );\r
32 \r
33 /////////////////////////////////////////////////////////////////////////////\r
34 // Dialog procedures and other callback functions\r
35 \r
36 BOOL CALLBACK PageProc (HWND hwnd, UINT uMessage, WPARAM wParam, LPARAM lParam)\r
37 {\r
38     CGitPropertyPage * sheetpage;\r
39 \r
40     if (uMessage == WM_INITDIALOG)\r
41     {\r
42         sheetpage = (CGitPropertyPage*) ((LPPROPSHEETPAGE) lParam)->lParam;\r
43         SetWindowLongPtr (hwnd, GWLP_USERDATA, (LONG_PTR) sheetpage);\r
44         sheetpage->SetHwnd(hwnd);\r
45     }\r
46     else\r
47     {\r
48         sheetpage = (CGitPropertyPage*) GetWindowLongPtr (hwnd, GWLP_USERDATA);\r
49     }\r
50 \r
51     if (sheetpage != 0L)\r
52         return sheetpage->PageProc(hwnd, uMessage, wParam, lParam);\r
53     else\r
54         return FALSE;\r
55 }\r
56 \r
57 UINT CALLBACK PropPageCallbackProc ( HWND /*hwnd*/, UINT uMsg, LPPROPSHEETPAGE ppsp )\r
58 {\r
59     // Delete the page before closing.\r
60     if (PSPCB_RELEASE == uMsg)\r
61     {\r
62         CGitPropertyPage* sheetpage = (CGitPropertyPage*) ppsp->lParam;\r
63         if (sheetpage != NULL)\r
64             delete sheetpage;\r
65     }\r
66     return 1;\r
67 }\r
68 \r
69 // *********************** CGitPropertyPage *************************\r
70 \r
71 CGitPropertyPage::CGitPropertyPage(const std::vector<stdstring> &newFilenames)\r
72         :filenames(newFilenames)\r
73 {\r
74 }\r
75 \r
76 CGitPropertyPage::~CGitPropertyPage(void)\r
77 {\r
78 }\r
79 \r
80 void CGitPropertyPage::SetHwnd(HWND newHwnd)\r
81 {\r
82     m_hwnd = newHwnd;\r
83 }\r
84 \r
85 BOOL CGitPropertyPage::PageProc (HWND /*hwnd*/, UINT uMessage, WPARAM wParam, LPARAM lParam)\r
86 {\r
87         switch (uMessage)\r
88         {\r
89         case WM_INITDIALOG:\r
90                 {\r
91                         InitWorkfileView();\r
92                         return TRUE;\r
93                 }\r
94         case WM_NOTIFY:\r
95                 {\r
96                         LPNMHDR point = (LPNMHDR)lParam;\r
97                         int code = point->code;\r
98                         //\r
99                         // Respond to notifications.\r
100                         //    \r
101                         if (code == PSN_APPLY)\r
102                         {\r
103                         }\r
104                         SetWindowLongPtr (m_hwnd, DWLP_MSGRESULT, FALSE);\r
105                         return TRUE;        \r
106 \r
107                         }\r
108                 case WM_DESTROY:\r
109                         return TRUE;\r
110 \r
111                 case WM_COMMAND:\r
112                         switch (HIWORD(wParam))\r
113                         {\r
114                                 case BN_CLICKED:\r
115                                         if (LOWORD(wParam) == IDC_SHOWLOG)\r
116                                         {\r
117                                                 STARTUPINFO startup;\r
118                                                 PROCESS_INFORMATION process;\r
119                                                 memset(&startup, 0, sizeof(startup));\r
120                                                 startup.cb = sizeof(startup);\r
121                                                 memset(&process, 0, sizeof(process));\r
122                                                 CRegStdString tortoiseProcPath(_T("Software\\TortoiseGit\\ProcPath"), _T("TortoiseProc.exe"), false, HKEY_LOCAL_MACHINE);\r
123                                                 stdstring svnCmd = _T(" /command:");\r
124                                                 svnCmd += _T("log /path:\"");\r
125                                                 svnCmd += filenames.front().c_str();\r
126                                                 svnCmd += _T("\"");\r
127                                                 if (CreateProcess(((stdstring)tortoiseProcPath).c_str(), const_cast<TCHAR*>(svnCmd.c_str()), NULL, NULL, FALSE, 0, 0, 0, &startup, &process))\r
128                                                 {\r
129                                                         CloseHandle(process.hThread);\r
130                                                         CloseHandle(process.hProcess);\r
131                                                 }\r
132                                         }\r
133                                         if (LOWORD(wParam) == IDC_EDITPROPERTIES)\r
134                                         {\r
135                                                 DWORD pathlength = GetTempPath(0, NULL);\r
136                                                 TCHAR * path = new TCHAR[pathlength+1];\r
137                                                 TCHAR * tempFile = new TCHAR[pathlength + 100];\r
138                                                 GetTempPath (pathlength+1, path);\r
139                                                 GetTempFileName (path, _T("svn"), 0, tempFile);\r
140                                                 stdstring retFilePath = stdstring(tempFile);\r
141 \r
142                                                 HANDLE file = ::CreateFile (tempFile,\r
143                                                         GENERIC_WRITE, \r
144                                                         FILE_SHARE_READ, \r
145                                                         0, \r
146                                                         CREATE_ALWAYS, \r
147                                                         FILE_ATTRIBUTE_TEMPORARY,\r
148                                                         0);\r
149 \r
150                                                 delete [] path;\r
151                                                 delete [] tempFile;\r
152                                                 if (file != INVALID_HANDLE_VALUE)\r
153                                                 {\r
154                                                         DWORD written = 0;\r
155                                                         for (std::vector<stdstring>::iterator I = filenames.begin(); I != filenames.end(); ++I)\r
156                                                         {\r
157                                                                 ::WriteFile (file, I->c_str(), I->size()*sizeof(TCHAR), &written, 0);\r
158                                                                 ::WriteFile (file, _T("\n"), 2, &written, 0);\r
159                                                         }\r
160                                                         ::CloseHandle(file);\r
161 \r
162                                                         STARTUPINFO startup;\r
163                                                         PROCESS_INFORMATION process;\r
164                                                         memset(&startup, 0, sizeof(startup));\r
165                                                         startup.cb = sizeof(startup);\r
166                                                         memset(&process, 0, sizeof(process));\r
167                                                         CRegStdString tortoiseProcPath(_T("Software\\TortoiseGit\\ProcPath"), _T("TortoiseProc.exe"), false, HKEY_LOCAL_MACHINE);\r
168                                                         stdstring svnCmd = _T(" /command:");\r
169                                                         svnCmd += _T("properties /pathfile:\"");\r
170                                                         svnCmd += retFilePath.c_str();\r
171                                                         svnCmd += _T("\"");\r
172                                                         svnCmd += _T(" /deletepathfile");\r
173                                                         if (CreateProcess(((stdstring)tortoiseProcPath).c_str(), const_cast<TCHAR*>(svnCmd.c_str()), NULL, NULL, FALSE, 0, 0, 0, &startup, &process))\r
174                                                         {\r
175                                                                 CloseHandle(process.hThread);\r
176                                                                 CloseHandle(process.hProcess);\r
177                                                         }\r
178                                                 }\r
179                                         }\r
180                                         break;\r
181                         } // switch (HIWORD(wParam)) \r
182         } // switch (uMessage) \r
183         return FALSE;\r
184 }\r
185 void CGitPropertyPage::Time64ToTimeString(__time64_t time, TCHAR * buf, size_t buflen)\r
186 {\r
187         struct tm newtime;\r
188         SYSTEMTIME systime;\r
189         TCHAR timebuf[MAX_STRING_LENGTH];\r
190         TCHAR datebuf[MAX_STRING_LENGTH];\r
191 \r
192         LCID locale = (WORD)CRegStdWORD(_T("Software\\TortoiseGit\\LanguageID"), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT));\r
193         locale = MAKELCID(locale, SORT_DEFAULT);\r
194 \r
195         *buf = '\0';\r
196         if (time)\r
197         {\r
198                 _localtime64_s(&newtime, &time);\r
199 \r
200                 systime.wDay = (WORD)newtime.tm_mday;\r
201                 systime.wDayOfWeek = (WORD)newtime.tm_wday;\r
202                 systime.wHour = (WORD)newtime.tm_hour;\r
203                 systime.wMilliseconds = 0;\r
204                 systime.wMinute = (WORD)newtime.tm_min;\r
205                 systime.wMonth = (WORD)newtime.tm_mon+1;\r
206                 systime.wSecond = (WORD)newtime.tm_sec;\r
207                 systime.wYear = (WORD)newtime.tm_year+1900;\r
208                 if (CRegStdWORD(_T("Software\\TortoiseGit\\LogDateFormat")) == 1)\r
209                         GetDateFormat(locale, DATE_SHORTDATE, &systime, NULL, datebuf, MAX_STRING_LENGTH);\r
210                 else\r
211                         GetDateFormat(locale, DATE_LONGDATE, &systime, NULL, datebuf, MAX_STRING_LENGTH);\r
212                 GetTimeFormat(locale, 0, &systime, NULL, timebuf, MAX_STRING_LENGTH);\r
213                 *buf = '\0';\r
214                 _tcsncat_s(buf, buflen, timebuf, MAX_STRING_LENGTH-1);\r
215                 _tcsncat_s(buf, buflen, _T(", "), MAX_STRING_LENGTH-1);\r
216                 _tcsncat_s(buf, buflen, datebuf, MAX_STRING_LENGTH-1);\r
217         }\r
218 }\r
219 \r
220 void CGitPropertyPage::InitWorkfileView()\r
221 {\r
222 #if 0\r
223         GitStatus svn = GitStatus();\r
224         TCHAR tbuf[MAX_STRING_LENGTH];\r
225         if (filenames.size() == 1)\r
226         {\r
227                 if (svn.GetStatus(CTGitPath(filenames.front().c_str()))>(-2))\r
228                 {\r
229                         TCHAR buf[MAX_STRING_LENGTH];\r
230                         __time64_t      time;\r
231                         if (svn.status->entry != NULL)\r
232                         {\r
233                                 LoadLangDll();\r
234                                 if (svn.status->text_status == svn_wc_status_added)\r
235                                 {\r
236                                         // disable the "show log" button for added files\r
237                                         HWND showloghwnd = GetDlgItem(m_hwnd, IDC_SHOWLOG);\r
238                                         if (GetFocus() == showloghwnd)\r
239                                                 ::SendMessage(m_hwnd, WM_NEXTDLGCTL, 0, FALSE);\r
240                                         ::EnableWindow(showloghwnd, FALSE);\r
241                                 }\r
242                                 else\r
243                                 {\r
244                                         _stprintf_s(buf, MAX_STRING_LENGTH, _T("%d"), svn.status->entry->revision);\r
245                                         SetDlgItemText(m_hwnd, IDC_REVISION, buf);\r
246                                 }\r
247                                 if (svn.status->entry->url)\r
248                                 {\r
249                                         size_t len = strlen(svn.status->entry->url);\r
250                                         char * unescapedurl = new char[len+1];\r
251                                         strcpy_s(unescapedurl, len+1, svn.status->entry->url);\r
252                                         CPathUtils::Unescape(unescapedurl);\r
253                                         SetDlgItemText(m_hwnd, IDC_REPOURL, UTF8ToWide(unescapedurl).c_str());\r
254                                         if (strcmp(unescapedurl, svn.status->entry->url))\r
255                                         {\r
256                                                 ShowWindow(GetDlgItem(m_hwnd, IDC_ESCAPEDURLLABEL), SW_SHOW);\r
257                                                 ShowWindow(GetDlgItem(m_hwnd, IDC_REPOURLUNESCAPED), SW_SHOW);\r
258                                                 SetDlgItemText(m_hwnd, IDC_REPOURLUNESCAPED, UTF8ToWide(svn.status->entry->url).c_str());\r
259                                         }\r
260                                         else\r
261                                         {\r
262                                                 ShowWindow(GetDlgItem(m_hwnd, IDC_ESCAPEDURLLABEL), SW_HIDE);\r
263                                                 ShowWindow(GetDlgItem(m_hwnd, IDC_REPOURLUNESCAPED), SW_HIDE);\r
264                                         }\r
265                                         delete [] unescapedurl;\r
266                                 }\r
267                                 else\r
268                                 {\r
269                                         ShowWindow(GetDlgItem(m_hwnd, IDC_ESCAPEDURLLABEL), SW_HIDE);\r
270                                         ShowWindow(GetDlgItem(m_hwnd, IDC_REPOURLUNESCAPED), SW_HIDE);\r
271                                 }\r
272                                 if (svn.status->text_status != svn_wc_status_added)\r
273                                 {\r
274                                         _stprintf_s(buf, MAX_STRING_LENGTH, _T("%d"), svn.status->entry->cmt_rev);\r
275                                         SetDlgItemText(m_hwnd, IDC_CREVISION, buf);\r
276                                         time = (__time64_t)svn.status->entry->cmt_date/1000000L;\r
277                                         Time64ToTimeString(time, buf, MAX_STRING_LENGTH);\r
278                                         SetDlgItemText(m_hwnd, IDC_CDATE, buf);\r
279                                 }\r
280                                 if (svn.status->entry->cmt_author)\r
281                                         SetDlgItemText(m_hwnd, IDC_AUTHOR, UTF8ToWide(svn.status->entry->cmt_author).c_str());\r
282                                 GitStatus::GetStatusString(g_hResInst, svn.status->text_status, buf, sizeof(buf)/sizeof(TCHAR), (WORD)CRegStdWORD(_T("Software\\TortoiseGit\\LanguageID"), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)));\r
283                                 SetDlgItemText(m_hwnd, IDC_TEXTSTATUS, buf);\r
284                                 GitStatus::GetStatusString(g_hResInst, svn.status->prop_status, buf, sizeof(buf)/sizeof(TCHAR), (WORD)CRegStdWORD(_T("Software\\TortoiseGit\\LanguageID"), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)));\r
285                                 SetDlgItemText(m_hwnd, IDC_PROPSTATUS, buf);\r
286                                 time = (__time64_t)svn.status->entry->text_time/1000000L;\r
287                                 Time64ToTimeString(time, buf, MAX_STRING_LENGTH);\r
288                                 SetDlgItemText(m_hwnd, IDC_TEXTDATE, buf);\r
289                                 time = (__time64_t)svn.status->entry->prop_time/1000000L;\r
290                                 Time64ToTimeString(time, buf, MAX_STRING_LENGTH);\r
291                                 SetDlgItemText(m_hwnd, IDC_PROPDATE, buf);\r
292 \r
293                                 if (svn.status->entry->lock_owner)\r
294                                         SetDlgItemText(m_hwnd, IDC_LOCKOWNER, UTF8ToWide(svn.status->entry->lock_owner).c_str());\r
295                                 time = (__time64_t)svn.status->entry->lock_creation_date/1000000L;\r
296                                 Time64ToTimeString(time, buf, MAX_STRING_LENGTH);\r
297                                 SetDlgItemText(m_hwnd, IDC_LOCKDATE, buf);\r
298         \r
299                                 if (svn.status->entry->uuid)\r
300                                         SetDlgItemText(m_hwnd, IDC_REPOUUID, UTF8ToWide(svn.status->entry->uuid).c_str());\r
301                                 if (svn.status->entry->changelist)\r
302                                         SetDlgItemText(m_hwnd, IDC_CHANGELIST, UTF8ToWide(svn.status->entry->changelist).c_str());\r
303                                 GitStatus::GetDepthString(g_hResInst, svn.status->entry->depth, buf, sizeof(buf)/sizeof(TCHAR), (WORD)CRegStdWORD(_T("Software\\TortoiseGit\\LanguageID"), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)));\r
304                                 SetDlgItemText(m_hwnd, IDC_DEPTHEDIT, buf);\r
305 \r
306                                 if (svn.status->entry->checksum)\r
307                                         SetDlgItemText(m_hwnd, IDC_CHECKSUM, UTF8ToWide(svn.status->entry->checksum).c_str());\r
308 \r
309                                 if (svn.status->locked)\r
310                                         MAKESTRING(IDS_YES);\r
311                                 else\r
312                                         MAKESTRING(IDS_NO);\r
313                                 SetDlgItemText(m_hwnd, IDC_LOCKED, stringtablebuffer);\r
314 \r
315                                 if (svn.status->copied)\r
316                                         MAKESTRING(IDS_YES);\r
317                                 else\r
318                                         MAKESTRING(IDS_NO);\r
319                                 SetDlgItemText(m_hwnd, IDC_COPIED, stringtablebuffer);\r
320 \r
321                                 if (svn.status->switched)\r
322                                         MAKESTRING(IDS_YES);\r
323                                 else\r
324                                         MAKESTRING(IDS_NO);\r
325                                 SetDlgItemText(m_hwnd, IDC_SWITCHED, stringtablebuffer);\r
326                         } // if (svn.status->entry != NULL)\r
327                 } // if (svn.GetStatus(CTGitPath(filenames.front().c_str()))>(-2))\r
328         } // if (filenames.size() == 1) \r
329         else if (filenames.size() != 0)\r
330         {\r
331                 //deactivate the show log button\r
332                 HWND logwnd = GetDlgItem(m_hwnd, IDC_SHOWLOG);\r
333                 if (GetFocus() == logwnd)\r
334                         ::SendMessage(m_hwnd, WM_NEXTDLGCTL, 0, FALSE);\r
335                 ::EnableWindow(logwnd, FALSE);\r
336                 //get the handle of the list view\r
337                 if (svn.GetStatus(CTGitPath(filenames.front().c_str()))>(-2))\r
338                 {\r
339                         if (svn.status->entry != NULL)\r
340                         {\r
341                                 LoadLangDll();\r
342                                 if (svn.status->entry->url)\r
343                                 {\r
344                                         CPathUtils::Unescape((char*)svn.status->entry->url);\r
345                                         _tcsncpy_s(tbuf, MAX_STRING_LENGTH, UTF8ToWide(svn.status->entry->url).c_str(), 4095);\r
346                                         TCHAR * ptr = _tcsrchr(tbuf, '/');\r
347                                         if (ptr != 0)\r
348                                         {\r
349                                                 *ptr = 0;\r
350                                         }\r
351                                         SetDlgItemText(m_hwnd, IDC_REPOURL, tbuf);\r
352                                 }\r
353                                 SetDlgItemText(m_hwnd, IDC_LOCKED, _T(""));\r
354                                 SetDlgItemText(m_hwnd, IDC_COPIED, _T(""));\r
355                                 SetDlgItemText(m_hwnd, IDC_SWITCHED, _T(""));\r
356 \r
357                                 SetDlgItemText(m_hwnd, IDC_DEPTHEDIT, _T(""));\r
358                                 SetDlgItemText(m_hwnd, IDC_CHECKSUM, _T(""));\r
359                                 SetDlgItemText(m_hwnd, IDC_REPOUUID, _T(""));\r
360 \r
361                                 ShowWindow(GetDlgItem(m_hwnd, IDC_ESCAPEDURLLABEL), SW_HIDE);\r
362                                 ShowWindow(GetDlgItem(m_hwnd, IDC_REPOURLUNESCAPED), SW_HIDE);\r
363                         }\r
364                 }\r
365         }\r
366 #endif\r
367 }\r
368 \r
369 \r
370 // CShellExt member functions (needed for IShellPropSheetExt)\r
371 STDMETHODIMP CShellExt::AddPages (LPFNADDPROPSHEETPAGE lpfnAddPage,\r
372                                   LPARAM lParam)\r
373 {\r
374 \r
375         CString ProjectTopDir;\r
376 \r
377         for (std::vector<stdstring>::iterator I = files_.begin(); I != files_.end(); ++I)\r
378         {\r
379                 /*\r
380                 GitStatus svn = GitStatus();\r
381                 if (svn.GetStatus(CTGitPath(I->c_str())) == (-2))\r
382                         return NOERROR;                 // file/directory not under version control\r
383 \r
384                 if (svn.status->entry == NULL)\r
385                         return NOERROR;\r
386                 */\r
387                 if( CTGitPath(I->c_str()).HasAdminDir(&ProjectTopDir))\r
388                         break;\r
389                 else\r
390                         return NOERROR;\r
391         }\r
392 \r
393         if (files_.size() == 0)\r
394                 return NOERROR;\r
395 \r
396         LoadLangDll();\r
397     PROPSHEETPAGE psp;\r
398         SecureZeroMemory(&psp, sizeof(PROPSHEETPAGE));\r
399         HPROPSHEETPAGE hPage;\r
400         CGitPropertyPage *sheetpage = new CGitPropertyPage(files_);\r
401 \r
402     psp.dwSize = sizeof (psp);\r
403     psp.dwFlags = PSP_USEREFPARENT | PSP_USETITLE | PSP_USEICONID | PSP_USECALLBACK;    \r
404         psp.hInstance = g_hResInst;\r
405         psp.pszTemplate = MAKEINTRESOURCE(IDD_PROPPAGE);\r
406     psp.pszIcon = MAKEINTRESOURCE(IDI_APPSMALL);\r
407     psp.pszTitle = _T("Git");\r
408     psp.pfnDlgProc = (DLGPROC) PageProc;\r
409     psp.lParam = (LPARAM) sheetpage;\r
410     psp.pfnCallback = PropPageCallbackProc;\r
411     psp.pcRefParent = &g_cRefThisDll;\r
412 \r
413     hPage = CreatePropertySheetPage (&psp);\r
414 \r
415         if (hPage != NULL)\r
416         {\r
417         if (!lpfnAddPage (hPage, lParam))\r
418         {\r
419             delete sheetpage;\r
420             DestroyPropertySheetPage (hPage);\r
421         }\r
422         }\r
423 \r
424     return NOERROR;\r
425 }\r
426 \r
427 \r
428 \r
429 STDMETHODIMP CShellExt::ReplacePage (UINT /*uPageID*/, LPFNADDPROPSHEETPAGE /*lpfnReplaceWith*/, LPARAM /*lParam*/)\r
430 {\r
431     return E_FAIL;\r
432 }\r
433 \r