1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013-2014 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "GravatarPictureBox.h"
22 #include "MessageBox.h"
23 #include "UnicodeUtils.h"
25 #include "LoglistCommonResource.h"
29 static CString
CalcMD5(CString text
)
32 if (!CryptAcquireContext(&hProv
, nullptr, nullptr, PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
))
36 if (!CryptCreateHash(hProv
, CALG_MD5
, 0, 0, &hHash
))
38 CryptReleaseContext(hProv
, 0);
42 CStringA textA
= CUnicodeUtils::GetUTF8(text
);
43 if (!CryptHashData(hHash
, (LPBYTE
)textA
.GetBuffer(), textA
.GetLength(), 0))
45 CryptDestroyHash(hHash
);
46 CryptReleaseContext(hProv
, 0);
52 DWORD cbHash
= _countof(rgbHash
);
53 if (!CryptGetHashParam(hHash
, HP_HASHVAL
, rgbHash
, &cbHash
, 0))
55 CryptDestroyHash(hHash
);
56 CryptReleaseContext(hProv
, 0);
59 for (DWORD i
= 0; i
< cbHash
; i
++)
61 BYTE hi
= rgbHash
[i
] >> 4;
62 BYTE lo
= rgbHash
[i
] & 0xf;
63 hash
.AppendChar(hi
+ (hi
> 9 ? 87 : 48));
64 hash
.AppendChar(lo
+ (lo
> 9 ? 87 : 48));
67 CryptDestroyHash(hHash
);
68 CryptReleaseContext(hProv
, 0);
72 BEGIN_MESSAGE_MAP(CGravatar
, CStatic
)
76 CGravatar::CGravatar()
78 , m_gravatarEvent(INVALID_HANDLE_VALUE
)
79 , m_gravatarThread(nullptr)
80 , m_gravatarExit(nullptr)
81 , m_bEnableGravatar(false)
83 m_gravatarLock
.Init();
86 CGravatar::~CGravatar()
88 SafeTerminateGravatarThread();
89 m_gravatarLock
.Term();
91 CloseHandle(m_gravatarEvent
);
94 void CGravatar::Init()
96 if (m_bEnableGravatar
)
98 if (m_gravatarEvent
== INVALID_HANDLE_VALUE
)
99 m_gravatarEvent
= ::CreateEvent(nullptr, FALSE
, TRUE
, nullptr);
100 if (m_gravatarThread
== nullptr)
102 m_gravatarExit
= new bool(false);
103 m_gravatarThread
= AfxBeginThread([] (LPVOID lpVoid
) -> UINT
{ ((CGravatar
*)lpVoid
)->GravatarThread(); return 0; }, this, THREAD_PRIORITY_BELOW_NORMAL
);
104 if (m_gravatarThread
== nullptr)
106 CMessageBox::Show(nullptr, IDS_ERR_THREADSTARTFAILED
, IDS_APPNAME
, MB_OK
| MB_ICONERROR
);
107 delete m_gravatarExit
;
108 m_gravatarExit
= nullptr;
115 void CGravatar::LoadGravatar(CString email
)
117 if (m_gravatarThread
== nullptr)
122 m_gravatarLock
.Lock();
124 if (!m_filename
.IsEmpty())
127 m_gravatarLock
.Unlock();
131 m_gravatarLock
.Unlock();
137 m_gravatarLock
.Lock();
138 bool diff
= m_email
!= email
;
140 m_gravatarLock
.Unlock();
142 ::SetEvent(m_gravatarEvent
);
145 void CGravatar::GravatarThread()
147 bool *gravatarExit
= m_gravatarExit
;
148 CString gravatarBaseUrl
= CRegString(_T("Software\\TortoiseGit\\GravatarUrl"), _T("http://www.gravatar.com/avatar/%HASH%?d=identicon"));
152 URL_COMPONENTS urlComponents
= {0};
153 urlComponents
.dwStructSize
= sizeof(urlComponents
);
154 urlComponents
.lpszHostName
= hostname
.GetBufferSetLength(INTERNET_MAX_HOST_NAME_LENGTH
);
155 urlComponents
.dwHostNameLength
= INTERNET_MAX_HOST_NAME_LENGTH
;
156 urlComponents
.lpszUrlPath
= baseUrlPath
.GetBufferSetLength(INTERNET_MAX_PATH_LENGTH
);
157 urlComponents
.dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
158 if (!InternetCrackUrl(gravatarBaseUrl
, gravatarBaseUrl
.GetLength(), 0, &urlComponents
))
164 hostname
.ReleaseBuffer();
165 baseUrlPath
.ReleaseBuffer();
167 HINTERNET hOpenHandle
= InternetOpen(L
"TortoiseGit", INTERNET_OPEN_TYPE_PRECONFIG
, nullptr, nullptr, 0);
168 bool isHttps
= urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
;
169 HINTERNET hConnectHandle
= InternetConnect(hOpenHandle
, hostname
, urlComponents
.nPort
, nullptr, nullptr, isHttps
? INTERNET_SCHEME_HTTP
: urlComponents
.nScheme
, 0, 0);
172 InternetCloseHandle(hOpenHandle
);
178 while (!*gravatarExit
)
180 ::WaitForSingleObject(m_gravatarEvent
, INFINITE
);
181 while (!*gravatarExit
)
183 m_gravatarLock
.Lock();
184 CString email
= m_email
;
185 m_gravatarLock
.Unlock();
192 m_gravatarLock
.Lock();
193 bool diff
= email
!= m_email
;
194 m_gravatarLock
.Unlock();
198 CString md5
= CalcMD5(email
);
202 CString gravatarUrl
= baseUrlPath
;
203 gravatarUrl
.Replace(_T("%HASH%"), md5
);
205 GetTempPath(tempFile
);
207 if (PathFileExists(tempFile
))
209 m_gravatarLock
.Lock();
210 if (m_email
== email
)
212 m_filename
= tempFile
;
217 m_gravatarLock
.Unlock();
221 BOOL ret
= DownloadToFile(gravatarExit
, hConnectHandle
, isHttps
, gravatarUrl
, tempFile
);
224 DeleteFile(tempFile
);
227 m_gravatarLock
.Lock();
229 m_gravatarLock
.Unlock();
233 m_gravatarLock
.Lock();
234 if (m_email
== email
&& !ret
)
236 HANDLE hFile
= CreateFile(tempFile
, FILE_READ_ATTRIBUTES
| FILE_WRITE_ATTRIBUTES
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
237 FILETIME creationTime
= {};
238 GetFileTime(hFile
, &creationTime
, nullptr, nullptr);
239 uint64_t delta
= 7 * 24 * 60 * 60 * 10000000LL;
240 DWORD low
= creationTime
.dwLowDateTime
;
241 creationTime
.dwLowDateTime
+= delta
& 0xffffffff;
242 creationTime
.dwHighDateTime
+= delta
>> 32;
243 if (creationTime
.dwLowDateTime
< low
)
244 creationTime
.dwHighDateTime
++;
245 SetFileTime(hFile
, &creationTime
, nullptr, nullptr);
247 m_filename
= tempFile
;
252 m_gravatarLock
.Unlock();
259 InternetCloseHandle(hConnectHandle
);
260 InternetCloseHandle(hOpenHandle
);
264 BOOL
CGravatar::DownloadToFile(bool *gravatarExit
, const HINTERNET hConnectHandle
, bool isHttps
, const CString
& urlpath
, const CString
& dest
)
266 HINTERNET hResourceHandle
= HttpOpenRequest(hConnectHandle
, nullptr, urlpath
, nullptr, nullptr, nullptr, INTERNET_FLAG_KEEP_CONNECTION
| (isHttps
? INTERNET_FLAG_SECURE
: 0), 0);
267 if (!hResourceHandle
)
273 InternetCloseHandle(hResourceHandle
);
274 return INET_E_DOWNLOAD_FAILURE
;
277 BOOL httpsendrequest
= HttpSendRequest(hResourceHandle
, nullptr, 0, nullptr, 0);
279 DWORD dwError
= InternetErrorDlg(GetSafeHwnd(), hResourceHandle
, ERROR_SUCCESS
, FLAGS_ERROR_UI_FILTER_FOR_ERRORS
| FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS
| FLAGS_ERROR_UI_FLAGS_GENERATE_DATA
, nullptr);
281 if (dwError
== ERROR_INTERNET_FORCE_RETRY
)
283 else if (!httpsendrequest
|| *gravatarExit
)
285 InternetCloseHandle(hResourceHandle
);
286 return INET_E_DOWNLOAD_FAILURE
;
289 DWORD statusCode
= 0;
290 DWORD length
= sizeof(statusCode
);
291 if (!HttpQueryInfo(hResourceHandle
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, (LPVOID
)&statusCode
, &length
, NULL
) || statusCode
!= 200)
293 InternetCloseHandle(hResourceHandle
);
294 if (statusCode
== 404)
295 return ERROR_FILE_NOT_FOUND
;
296 else if (statusCode
== 403)
297 return ERROR_ACCESS_DENIED
;
298 return INET_E_DOWNLOAD_FAILURE
;
301 CFile destinationFile
;
302 if (!destinationFile
.Open(dest
, CFile::modeCreate
| CFile::modeWrite
))
304 InternetCloseHandle(hResourceHandle
);
305 return ERROR_ACCESS_DENIED
;
308 DWORD downloadedSum
= 0; // sum of bytes downloaded so far
309 while (!*gravatarExit
)
311 DWORD size
; // size of the data available
312 if (!InternetQueryDataAvailable(hResourceHandle
, &size
, 0, 0))
314 InternetCloseHandle(hResourceHandle
);
315 return INET_E_DOWNLOAD_FAILURE
;
318 DWORD downloaded
; // size of the downloaded data
319 LPTSTR lpszData
= new TCHAR
[size
+ 1];
320 if (!InternetReadFile(hResourceHandle
, (LPVOID
)lpszData
, size
, &downloaded
))
323 InternetCloseHandle(hResourceHandle
);
324 return INET_E_DOWNLOAD_FAILURE
;
333 lpszData
[downloaded
] = '\0';
334 destinationFile
.Write(lpszData
, downloaded
);
337 downloadedSum
+= downloaded
;
339 destinationFile
.Close();
340 InternetCloseHandle(hResourceHandle
);
341 if (downloadedSum
== 0)
342 return INET_E_DOWNLOAD_FAILURE
;
347 void CGravatar::SafeTerminateGravatarThread()
349 if (m_gravatarExit
!= nullptr)
350 *m_gravatarExit
= true;
351 if (m_gravatarThread
)
353 ::SetEvent(m_gravatarEvent
);
354 ::WaitForSingleObject(m_gravatarThread
, 1000);
355 m_gravatarThread
= nullptr;
359 void CGravatar::OnPaint()
363 GetClientRect(&rect
);
364 dc
.FillSolidRect(&rect
, GetSysColor(COLOR_BTNFACE
));
365 m_gravatarLock
.Lock();
366 CString filename
= m_filename
;
367 m_gravatarLock
.Unlock();
368 if (filename
.IsEmpty()) return;
370 picture
.Load(filename
.GetString());
371 picture
.Show(dc
, rect
);