Skip to content

Commit 9b444d7

Browse files
committed
SonarQube cleanup + always-on context-menu icons
* ZipRegistry: MenuIcons defaults ON when no registry value (Key_Get_BoolPair_true, matches Cascaded/ElimDup). * ContextMenu.cpp: drop the ci.MenuIcons.Val conditional — shell context-menu icons are now unconditional. * g_bProcessError -> ProgressDialog_SetError()/HadError() pair backed by a function-local static in an anonymous namespace (3x S5421). Callers in App.cpp and ExtractGUI.cpp updated. * CProgressDialog: extract 3 SysTray methods + state into a CSysTray helper struct (Icons, IconArrayId, Menu, LoadIcons, UpdateIcon, BuildPopupMenu). Class falls 38 -> 35 methods, fixes S1448. Behavior unchanged. * wchar_t buf[N] -> std::array<wchar_t, N> in BrowseDialog (szFree, szTotal), ProgressDialog2 (szPercent, tip): 4x S5945. * HICON[] -> std::array<HICON, N> (S5945), Z7_ARRAY_SIZE for szTip (S7127), range-for loops for icon arrays (2x S5566). * CopyDialog.h: CCopyDialog() = default (S3490). * ExtractDialog.h: PathMode_Force in-class init, ctor body-only (S3230).
1 parent 6a14047 commit 9b444d7

9 files changed

Lines changed: 108 additions & 89 deletions

File tree

CPP/7zip/UI/Common/ZipRegistry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ void CContextMenuInfo::Load()
646646

647647
Key_Get_BoolPair_true(key, kCascadedMenu, Cascaded);
648648
Key_Get_BoolPair_true(key, kElimDup, ElimDup);
649-
Key_Get_BoolPair(key, kMenuIcons, MenuIcons);
649+
Key_Get_BoolPair_true(key, kMenuIcons, MenuIcons);
650650

651651
Key_Get_UInt32(key, kWriteZoneId, WriteZone);
652652

CPP/7zip/UI/Explorer/ContextMenu.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,10 @@ Z7_COMWF_B CZipContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu,
648648
_elimDup = ci.ElimDup;
649649
_writeZone = ci.WriteZone;
650650

651-
HBITMAP bitmap = NULL;
652-
if (ci.MenuIcons.Val)
653-
{
654-
ODS("### 45")
655-
if (!_bitmap)
656-
_bitmap = ::LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_MENU_LOGO));
657-
bitmap = _bitmap;
658-
}
651+
ODS("### 45")
652+
if (!_bitmap)
653+
_bitmap = ::LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_MENU_LOGO));
654+
HBITMAP bitmap = _bitmap;
659655

660656
UINT subIndex = indexMenu;
661657

CPP/7zip/UI/FileManager/App.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ using namespace NName;
4141

4242
extern HINSTANCE g_hInstance;
4343

44-
extern bool g_bProcessError;
45-
4644
#define kTempDirPrefix FTEXT("7zE")
4745

4846
// Walk up, delete found archive.
@@ -853,7 +851,7 @@ void CApp::OnCopy(bool move, bool copyToSame, unsigned srcPanelIndex)
853851
SaveCopyHistory(copyFolders);
854852
}
855853

856-
g_bProcessError = false;
854+
ProgressDialog_SetError(false);
857855

858856
bool useSrcPanel = !useDestPanel || !srcPanel.Is_IO_FS_Folder();
859857

@@ -952,7 +950,7 @@ void CApp::OnCopy(bool move, bool copyToSame, unsigned srcPanelIndex)
952950
disableNotify2.Restore();
953951
srcPanel.SetFocusToList();
954952

955-
if (!g_bProcessError && result == S_OK)
953+
if (!ProgressDialog_HadError() && result == S_OK)
956954
{
957955
if (openOutputFolder && NFind::DoesDirExist_FollowLink(us2fs(destPath)))
958956
{

CPP/7zip/UI/FileManager/BrowseDialog.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// BrowseDialog.cpp
2-
2+
33
#include "StdAfx.h"
44

5+
#include <array>
6+
57
#include "../../../Common/MyWindows.h"
68

79
#include "../../../Common/IntToString.h"
@@ -628,13 +630,13 @@ void FormatPathFreeSpace(UString &strPath, UString &strText)
628630
if (!GetDiskFreeSpaceExW(strPath, &freeBytes, &totalBytes, &totalFree))
629631
return;
630632

631-
wchar_t szFree[40];
632-
wchar_t szTotal[40];
633-
FreeSpace_ConvertSizeToString(totalFree.QuadPart, szFree);
634-
FreeSpace_ConvertSizeToString(totalBytes.QuadPart, szTotal);
635-
strText = szFree;
633+
std::array<wchar_t, 40> szFree;
634+
std::array<wchar_t, 40> szTotal;
635+
FreeSpace_ConvertSizeToString(totalFree.QuadPart, szFree.data());
636+
FreeSpace_ConvertSizeToString(totalBytes.QuadPart, szTotal.data());
637+
strText = szFree.data();
636638
strText += L" Free (Total: ";
637-
strText += szTotal;
639+
strText += szTotal.data();
638640
strText += L")";
639641
}
640642

CPP/7zip/UI/FileManager/CopyDialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CCopyDialog: public NWindows::NControl::CModalDialog
2727
void ShowPathFreeSpace(UString & strPath);
2828

2929
public:
30-
CCopyDialog() {}
30+
CCopyDialog() = default;
3131

3232
UString Title;
3333
UString Static;

CPP/7zip/UI/FileManager/ProgressDialog2.cpp

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,17 @@ using namespace NWindows;
2626

2727
extern HINSTANCE g_hInstance;
2828
extern bool g_DisableUserQuestions;
29-
// Set true by CProgressDialog when error messages occur.
30-
bool g_bProcessError = false;
29+
30+
namespace {
31+
bool& ProcessErrorRef()
32+
{
33+
static bool s_error = false;
34+
return s_error;
35+
}
36+
}
37+
38+
void ProgressDialog_SetError(bool error) { ProcessErrorRef() = error; }
39+
bool ProgressDialog_HadError() { return ProcessErrorRef(); }
3140

3241
static const UINT WM_TRAY_ICON_NOTIFY = WM_APP + 10;
3342
static const UINT ID_SYSTRAY_ICON = 100;
@@ -52,7 +61,7 @@ static BOOL SetSysTray(HWND dlg,
5261

5362
if (tip != nullptr && *tip != 0)
5463
{
55-
::lstrcpynW(data.szTip, tip, sizeof(data.szTip) / sizeof(WCHAR));
64+
::lstrcpynW(data.szTip, tip, Z7_ARRAY_SIZE(data.szTip));
5665
}
5766

5867
return Shell_NotifyIconW(message, &data);
@@ -303,11 +312,6 @@ CProgressDialog::CProgressDialog():
303312
IconID(-1),
304313
MainWindow(NULL)
305314
{
306-
for (int i = 0; i < kNumTrayIcons; i++)
307-
_iconSysTrayArray[i] = nullptr;
308-
_sysTrayIconArrayId = -1;
309-
_sysTrayMenu = nullptr;
310-
311315
if (_dialogCreatedEvent.Create() != S_OK)
312316
throw 1334987;
313317
if (_createDialogEvent.Create() != S_OK)
@@ -888,14 +892,14 @@ void CProgressDialog::UpdateStatInfo(bool showAll)
888892
_prevPercentValue = percent;
889893
needSetTitle = true;
890894

891-
wchar_t szPercent[32];
892-
wchar_t *p = ConvertUInt64ToString(percent, szPercent);
895+
std::array<wchar_t, 32> szPercent;
896+
wchar_t *p = ConvertUInt64ToString(percent, szPercent.data());
893897
*p++ = L'%';
894898
*p = 0;
895-
SetItemText(IDC_PROGRESS_PERCENT, szPercent);
899+
SetItemText(IDC_PROGRESS_PERCENT, szPercent.data());
896900

897-
if (_background && _iconSysTrayArray[0] != nullptr)
898-
UpdateSysTrayIcon(false, true);
901+
if (_background && _sysTray.Icons[0] != nullptr)
902+
_sysTray.UpdateIcon(_window, _prevPercentValue, false, true);
899903
}
900904
}
901905

@@ -1131,18 +1135,18 @@ bool CProgressDialog::OnMessage(UINT message, WPARAM wParam, LPARAM lParam)
11311135
if (MainWindow != nullptr)
11321136
ShowWindow(MainWindow, SW_SHOW);
11331137
}
1134-
if (_iconSysTrayArray[0] != nullptr)
1138+
if (_sysTray.Icons[0] != nullptr)
11351139
{
1136-
for (int i = 0; i < kNumTrayIcons; i++)
1140+
for (auto &icon : _sysTray.Icons)
11371141
{
1138-
DestroyIcon(_iconSysTrayArray[i]);
1139-
_iconSysTrayArray[i] = nullptr;
1142+
DestroyIcon(icon);
1143+
icon = nullptr;
11401144
}
11411145
}
1142-
if (_sysTrayMenu != nullptr)
1146+
if (_sysTray.Menu != nullptr)
11431147
{
1144-
::DestroyMenu(_sysTrayMenu);
1145-
_sysTrayMenu = nullptr;
1148+
::DestroyMenu(_sysTray.Menu);
1149+
_sysTray.Menu = nullptr;
11461150
}
11471151
return OnExternalCloseMessage();
11481152
}
@@ -1204,8 +1208,8 @@ void CProgressDialog::SetPauseText()
12041208
SetItemText(IDB_PAUSE, pszText);
12051209
SetTitleText();
12061210

1207-
if (_sysTrayMenu != nullptr)
1208-
ModifyMenuW(_sysTrayMenu, IDB_PAUSE, MF_BYCOMMAND, IDB_PAUSE, pszText);
1211+
if (_sysTray.Menu != nullptr)
1212+
ModifyMenuW(_sysTray.Menu, IDB_PAUSE, MF_BYCOMMAND, IDB_PAUSE, pszText);
12091213
}
12101214

12111215
void CProgressDialog::OnPauseButton()
@@ -1235,7 +1239,7 @@ void CProgressDialog::OnPriorityButton()
12351239
SetPriorityClass(GetCurrentProcess(), _background ? IDLE_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS);
12361240
if (!_background) // foreground
12371241
{
1238-
if (_iconSysTrayArray[0] != nullptr)
1242+
if (_sysTray.Icons[0] != nullptr)
12391243
{
12401244
if (MainWindow != nullptr)
12411245
ShowWindow(MainWindow, SW_SHOW);
@@ -1245,12 +1249,12 @@ void CProgressDialog::OnPriorityButton()
12451249
}
12461250
else
12471251
{
1248-
if (LoadSysTrayIcons())
1252+
if (_sysTray.LoadIcons(g_hInstance, IDI_SYSTRAY_0))
12491253
{
12501254
if (MainWindow != nullptr)
12511255
ShowWindow(MainWindow, SW_HIDE);
12521256
Show(SW_HIDE);
1253-
UpdateSysTrayIcon(true, true);
1257+
_sysTray.UpdateIcon(_window, _prevPercentValue, true, true);
12541258
}
12551259
}
12561260
#endif
@@ -1316,7 +1320,7 @@ void CProgressDialog::UpdateMessagesDialog()
13161320
}
13171321
if (!messages.IsEmpty())
13181322
{
1319-
g_bProcessError = true;
1323+
ProgressDialog_SetError(true);
13201324

13211325
FOR_VECTOR (i, messages)
13221326
AddMessage(messages[i]);
@@ -1499,67 +1503,76 @@ void CProgressDialog::CopyToClipboard()
14991503
}
15001504

15011505

1502-
bool CProgressDialog::LoadSysTrayIcons()
1506+
CSysTray::CSysTray() : IconArrayId(-1), Menu(nullptr)
15031507
{
1504-
if (_iconSysTrayArray[0] != nullptr) return true;
1508+
for (auto &icon : Icons)
1509+
icon = nullptr;
1510+
}
15051511

1506-
int id = IDI_SYSTRAY_0;
1507-
for (int n = 0; n < kNumTrayIcons; n++, id++)
1512+
bool CSysTray::LoadIcons(HINSTANCE hInst, int firstResId)
1513+
{
1514+
if (Icons[0] != nullptr) return true;
1515+
1516+
int id = firstResId;
1517+
for (auto &icon : Icons)
15081518
{
1509-
_iconSysTrayArray[n] = (HICON)LoadImage(g_hInstance, MAKEINTRESOURCE(id), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1519+
icon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(id), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1520+
id++;
15101521
}
15111522

1512-
return _iconSysTrayArray[0] != nullptr;
1523+
return Icons[0] != nullptr;
15131524
}
15141525

1515-
void CProgressDialog::UpdateSysTrayIcon(bool addIcon, bool updateTip)
1526+
void CSysTray::UpdateIcon(HWND dlg, UInt64 percentValue, bool addIcon, bool updateTip)
15161527
{
1517-
int newIconId = (_prevPercentValue / kTrayPercentPerIcon) % kNumTrayIcons;
1518-
bool updateIcon = newIconId > _sysTrayIconArrayId;
1528+
int newIconId = (int)((percentValue / kPercentPerIcon) % kNumIcons);
1529+
bool updateIcon = newIconId > IconArrayId;
15191530

15201531
if (updateIcon || updateTip || addIcon)
15211532
{
1522-
wchar_t tip[64];
1523-
wchar_t *p = ConvertUInt64ToString(_prevPercentValue, tip);
1533+
std::array<wchar_t, 64> tip;
1534+
wchar_t *p = ConvertUInt64ToString(percentValue, tip.data());
15241535
*p++ = L'%';
15251536
*p = 0;
15261537

15271538
UINT flags = NIF_TIP;
15281539
if (updateIcon)
15291540
flags |= NIF_ICON;
15301541

1531-
if (!SetSysTray(_window, NIM_MODIFY, ID_SYSTRAY_ICON, flags, 0, _iconSysTrayArray[newIconId], tip))
1542+
if (!SetSysTray(dlg, NIM_MODIFY, ID_SYSTRAY_ICON, flags, 0, Icons[newIconId], tip.data()))
15321543
{
1533-
SetSysTray(_window, NIM_DELETE, ID_SYSTRAY_ICON, 0, 0, nullptr, nullptr);
1534-
SetSysTray(_window, NIM_ADD, ID_SYSTRAY_ICON, NIF_ICON | NIF_MESSAGE | NIF_TIP, WM_TRAY_ICON_NOTIFY,
1535-
_iconSysTrayArray[newIconId], tip);
1544+
SetSysTray(dlg, NIM_DELETE, ID_SYSTRAY_ICON, 0, 0, nullptr, nullptr);
1545+
SetSysTray(dlg, NIM_ADD, ID_SYSTRAY_ICON, NIF_ICON | NIF_MESSAGE | NIF_TIP, WM_TRAY_ICON_NOTIFY,
1546+
Icons[newIconId], tip.data());
15361547
}
15371548

1538-
_sysTrayIconArrayId = newIconId;
1549+
IconArrayId = newIconId;
15391550
}
15401551
}
15411552

1542-
bool CProgressDialog::CreateSysTrayMenu()
1553+
bool CSysTray::BuildPopupMenu(LPCWSTR foregroundText, LPCWSTR pauseOrContinueText, LPCWSTR cancelText)
15431554
{
1544-
if (_sysTrayMenu != nullptr) return true;
1555+
if (Menu != nullptr) return true;
15451556

1546-
_sysTrayMenu = CreatePopupMenu();
1547-
AppendMenuW(_sysTrayMenu, MF_STRING, IDB_PROGRESS_BACKGROUND, _foreground_String);
1548-
AppendMenuW(_sysTrayMenu, MF_STRING, IDB_PAUSE, Sync.Get_Paused() ? _continue_String : _pause_String);
1549-
AppendMenuW(_sysTrayMenu, MF_STRING, IDCANCEL, cancelString);
1550-
return _sysTrayMenu != nullptr;
1557+
Menu = CreatePopupMenu();
1558+
AppendMenuW(Menu, MF_STRING, IDB_PROGRESS_BACKGROUND, foregroundText);
1559+
AppendMenuW(Menu, MF_STRING, IDB_PAUSE, pauseOrContinueText);
1560+
AppendMenuW(Menu, MF_STRING, IDCANCEL, cancelText);
1561+
return Menu != nullptr;
15511562
}
15521563

15531564
bool CProgressDialog::OnTrayNotification(LPARAM lParam)
15541565
{
15551566
switch (lParam)
15561567
{
15571568
case WM_RBUTTONUP:
1558-
if (CreateSysTrayMenu())
1569+
if (_sysTray.BuildPopupMenu(_foreground_String,
1570+
Sync.Get_Paused() ? _continue_String : _pause_String,
1571+
cancelString))
15591572
{
15601573
POINT point;
15611574
::GetCursorPos(&point);
1562-
::TrackPopupMenu(_sysTrayMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, point.x, point.y, 0, _window, nullptr);
1575+
::TrackPopupMenu(_sysTray.Menu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, point.x, point.y, 0, _window, nullptr);
15631576
// BUGFIX: see "PRB: Menus for Notification Icons Don't Work Correctly"
15641577
::PostMessage(_window, WM_NULL, 0, 0);
15651578
}

CPP/7zip/UI/FileManager/ProgressDialog2.h

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#ifndef ZIP7_INC_PROGRESS_DIALOG_2_H
44
#define ZIP7_INC_PROGRESS_DIALOG_2_H
55

6+
#include <array>
7+
68
#include "../../../Common/MyCom.h"
79

810
#include "../../../Windows/ErrorMsg.h"
@@ -103,6 +105,26 @@ class CProgressSync
103105
};
104106

105107

108+
void ProgressDialog_SetError(bool error);
109+
bool ProgressDialog_HadError();
110+
111+
112+
struct CSysTray
113+
{
114+
static const int kNumIcons = 15;
115+
static const int kPercentPerIcon = 7;
116+
117+
std::array<HICON, kNumIcons> Icons;
118+
int IconArrayId;
119+
HMENU Menu;
120+
121+
CSysTray();
122+
bool LoadIcons(HINSTANCE hInst, int firstResId);
123+
void UpdateIcon(HWND dlg, UInt64 percentValue, bool addIcon, bool updateTip);
124+
bool BuildPopupMenu(LPCWSTR foregroundText, LPCWSTR pauseOrContinueText, LPCWSTR cancelText);
125+
};
126+
127+
106128
class CProgressDialog: public NWindows::NControl::CModalDialog
107129
{
108130
bool _isDir;
@@ -144,11 +166,7 @@ class CProgressDialog: public NWindows::NControl::CModalDialog
144166

145167
UString _title;
146168

147-
static const int kNumTrayIcons = 15;
148-
static const int kTrayPercentPerIcon = 7;
149-
HICON _iconSysTrayArray[kNumTrayIcons];
150-
int _sysTrayIconArrayId;
151-
HMENU _sysTrayMenu;
169+
CSysTray _sysTray;
152170

153171
class CU64ToI32Converter
154172
{
@@ -273,10 +291,6 @@ class CProgressDialog: public NWindows::NControl::CModalDialog
273291

274292
INT_PTR Create(const UString &title, NWindows::CThread &thread, HWND wndParent = NULL);
275293

276-
bool CreateSysTrayMenu();
277-
bool LoadSysTrayIcons();
278-
void UpdateSysTrayIcon(bool addIcon, bool updateTip);
279-
280294
/* how it works:
281295
1) the working thread calls ProcessWasFinished()
282296
that sends kCloseMessage message to CProgressDialog (GUI) thread

0 commit comments

Comments
 (0)