From 1baa7a1a29c0af4b3c4744b05cb91b4fb41831a5 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 24 Sep 2023 13:01:26 +0300 Subject: code cleaning --- plugins/SmileyAdd/src/download.cpp | 60 +++++++++++++++++++++++--------------- plugins/SmileyAdd/src/main.cpp | 6 ++-- plugins/SmileyAdd/src/options.cpp | 2 +- plugins/SmileyAdd/src/services.cpp | 8 ++--- plugins/SmileyAdd/src/stdafx.h | 2 +- 5 files changed, 45 insertions(+), 33 deletions(-) (limited to 'plugins/SmileyAdd/src') diff --git a/plugins/SmileyAdd/src/download.cpp b/plugins/SmileyAdd/src/download.cpp index f6a02f1643..4235e312b2 100644 --- a/plugins/SmileyAdd/src/download.cpp +++ b/plugins/SmileyAdd/src/download.cpp @@ -27,14 +27,14 @@ struct QueueElem CMStringW fname; bool needext; - QueueElem(CMStringW &purl, CMStringW &pfname, bool ne) - : url(purl), fname(pfname), needext(ne) + QueueElem(const CMStringW &purl, const CMStringW &pfname, bool ne) : + url(purl), fname(pfname), needext(ne) { } }; -static HANDLE g_hDlMutex; -static OBJLIST dlQueue(10); +static mir_cs csQueue; +static LIST dlQueue(10); static wchar_t g_wszCachePath[MAX_PATH]; static bool threadRunning; @@ -123,34 +123,48 @@ void __cdecl SmileyDownloadThread(void*) bool needext = false; HNETLIBCONN hHttpDwnl = nullptr; - WaitForSingleObject(g_hDlMutex, 3000); - while (!Miranda_IsTerminated() && dlQueue.getCount()) { - ReleaseMutex(g_hDlMutex); - if (_waccess(dlQueue[0].fname.c_str(), 0) != 0) { - InternetDownloadFile(_T2A(dlQueue[0].url.c_str()), _T2A(dlQueue[0].fname.c_str()), hHttpDwnl); - WaitForSingleObject(g_hDlMutex, 3000); - - CMStringW fname(dlQueue[0].fname); - if (dlQueue[0].needext) { + + while (!Miranda_IsTerminated()) { + QueueElem *pItem = nullptr; + { + mir_cslock lck(csQueue); + if (dlQueue.getCount()) { + pItem = dlQueue[0]; + dlQueue.remove(int(0)); + } + } + + if (pItem == nullptr) { + SleepEx(3000, TRUE); + continue; + } + + if (_waccess(pItem->fname.c_str(), 0) != 0) { + InternetDownloadFile(_T2A(pItem->url.c_str()), _T2A(pItem->fname.c_str()), hHttpDwnl); + + CMStringW fname(pItem->fname); + if (pItem->needext) { fname += ProtoGetAvatarExtension(ProtoGetAvatarFileFormat(fname)); needext = true; } - _wrename(dlQueue[0].fname.c_str(), fname.c_str()); + _wrename(pItem->fname.c_str(), fname.c_str()); } - else WaitForSingleObject(g_hDlMutex, 3000); - dlQueue.remove(0); + delete pItem; } + + for (auto &it : dlQueue) + delete it; dlQueue.destroy(); + Netlib_CloseHandle(hHttpDwnl); threadRunning = false; - ReleaseMutex(g_hDlMutex); if (!Miranda_IsTerminated()) { if (needext) CallServiceSync(MS_SMILEYADD_RELOAD, 0, 0); else - NotifyEventHooks(hEvent1, 0, 0); + NotifyEventHooks(g_hevOptionsChanged, 0, 0); } } @@ -199,9 +213,10 @@ bool GetSmileyFile(CMStringW &url, const CMStringW &packstr) filename += L"."; } - WaitForSingleObject(g_hDlMutex, 3000); - dlQueue.insert(new QueueElem(url, filename, iExtIdx == -1)); - ReleaseMutex(g_hDlMutex); + { + mir_cslock lck(csQueue); + dlQueue.insert(new QueueElem(url, filename, iExtIdx == -1)); + } if (!threadRunning) { threadRunning = true; @@ -241,12 +256,9 @@ void DownloadInit(void) wcsncpy_s(g_wszCachePath, VARSW(L"%miranda_userdata%\\SmileyCache"), _TRUNCATE); wcsncpy_s(g_plugin.wszDefaultPath, VARSW(L"%miranda_path%\\"), _TRUNCATE); } - - g_hDlMutex = CreateMutex(nullptr, FALSE, nullptr); } void DownloadClose(void) { - CloseHandle(g_hDlMutex); Netlib_CloseHandle(hNetlibUser); } diff --git a/plugins/SmileyAdd/src/main.cpp b/plugins/SmileyAdd/src/main.cpp index c8ecf447dd..08c0f6924a 100644 --- a/plugins/SmileyAdd/src/main.cpp +++ b/plugins/SmileyAdd/src/main.cpp @@ -20,7 +20,7 @@ along with this program. If not, see . #include "stdafx.h" // globals -HANDLE hEvent1; +HANDLE g_hevOptionsChanged; HGENMENU hContactMenuItem; CMPlugin g_plugin; @@ -94,7 +94,7 @@ int CMPlugin::Load() opt.Load(); // create smiley events - hEvent1 = CreateHookableEvent(ME_SMILEYADD_OPTIONSCHANGED); + g_hevOptionsChanged = CreateHookableEvent(ME_SMILEYADD_OPTIONSCHANGED); HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoaded); HookEvent(ME_SYSTEM_PRESHUTDOWN, MirandaShutdown); @@ -126,7 +126,7 @@ int CMPlugin::Unload() { RemoveDialogBoxHook(); - DestroyHookableEvent(hEvent1); + DestroyHookableEvent(g_hevOptionsChanged); RichEditData_Destroy(); diff --git a/plugins/SmileyAdd/src/options.cpp b/plugins/SmileyAdd/src/options.cpp index f4f5299ed6..bcd1b16574 100644 --- a/plugins/SmileyAdd/src/options.cpp +++ b/plugins/SmileyAdd/src/options.cpp @@ -127,7 +127,7 @@ class COptionsBaseDialog : public CDlgBase opt.Save(); - NotifyEventHooks(hEvent1, 0, 0); + NotifyEventHooks(g_hevOptionsChanged, 0, 0); ProcessAllInputAreas(false); } diff --git a/plugins/SmileyAdd/src/services.cpp b/plugins/SmileyAdd/src/services.cpp index b94bdd48ad..f14cef58ae 100644 --- a/plugins/SmileyAdd/src/services.cpp +++ b/plugins/SmileyAdd/src/services.cpp @@ -217,7 +217,7 @@ INT_PTR CustomCatMenu(WPARAM hContact, LPARAM lParam) if (lParam == 1) empty = L""; opt.WriteContactCategory(hContact, empty); } - NotifyEventHooks(hEvent1, hContact, 0); + NotifyEventHooks(g_hevOptionsChanged, hContact, 0); } for (auto &it : menuHandleArray) @@ -315,7 +315,7 @@ INT_PTR ReloadPack(WPARAM, LPARAM lParam) g_SmileyCategories.ClearAndLoadAll(); } - NotifyEventHooks(hEvent1, 0, 0); + NotifyEventHooks(g_hevOptionsChanged, 0, 0); return 0; } @@ -326,12 +326,12 @@ INT_PTR LoadContactSmileys(WPARAM, LPARAM lParam) switch (cont->type) { case 0: g_SmileyPackCStore.AddSmileyPack(cont->pszModule, cont->path); - NotifyEventHooks(hEvent1, (WPARAM)cont->pszModule, 0); + NotifyEventHooks(g_hevOptionsChanged, (WPARAM)cont->pszModule, 0); break; case 1: g_SmileyPackCStore.AddSmiley(cont->pszModule, cont->path); - NotifyEventHooks(hEvent1, (WPARAM)cont->pszModule, 0); + NotifyEventHooks(g_hevOptionsChanged, (WPARAM)cont->pszModule, 0); break; case 2: diff --git a/plugins/SmileyAdd/src/stdafx.h b/plugins/SmileyAdd/src/stdafx.h index 9b0df601a5..c3f6cae5dc 100644 --- a/plugins/SmileyAdd/src/stdafx.h +++ b/plugins/SmileyAdd/src/stdafx.h @@ -86,7 +86,7 @@ along with this program. If not, see . #include "smltool.h" extern HNETLIBUSER hNetlibUser; -extern HANDLE hEvent1; +extern HANDLE g_hevOptionsChanged; extern HGENMENU hContactMenuItem; extern SmileyCategoryListType g_SmileyCategories; extern SmileyPackListType g_SmileyPacks; -- cgit v1.2.3