From 2ef624682b21579ae76115a176f2ef5c658ad639 Mon Sep 17 00:00:00 2001 From: LI Daobing Date: Sat, 6 Jul 2024 21:28:57 -0700 Subject: [PATCH] remove flags --- src/api/iptux-core/Models.h | 7 ++- src/api/iptux-core/ProgramData.h | 33 +++++++--- src/iptux-core/Models.cpp | 41 ++++++------ src/iptux-core/ProgramData.cpp | 62 ++++++++----------- .../internal/UdpDataServiceTest.cpp | 8 ++- src/iptux-utils/UtilsTest.cpp | 10 --- src/iptux-utils/utils.cpp | 12 ---- src/iptux-utils/utils.h | 5 -- src/iptux/DataSettings.cpp | 26 +++++--- src/iptux/MainWindow.cpp | 2 +- 10 files changed, 97 insertions(+), 109 deletions(-) diff --git a/src/api/iptux-core/Models.h b/src/api/iptux-core/Models.h index b4fe1d10..94cb1a3e 100644 --- a/src/api/iptux-core/Models.h +++ b/src/api/iptux-core/Models.h @@ -134,7 +134,7 @@ class PalInfo { PalInfo& setCompatible(bool value); PalInfo& setOnline(bool value); PalInfo& setChanged(bool value); - PalInfo& setInBlacklistl(bool value); + PalInfo& setInBlacklist(bool value); private: in_addr ipv4_; ///< 好友IP @@ -146,7 +146,10 @@ class PalInfo { std::string version; ///< 版本串 * std::string encode; ///< 好友编码 * std::string group; ///< 所在群组 - uint8_t flags; ///< 3 黑名单:2 更改:1 在线:0 兼容 + uint8_t compatible : 1; + uint8_t online : 1; + uint8_t changed : 1; + uint8_t in_blacklist : 1; }; /// pointer to PalInfo diff --git a/src/api/iptux-core/ProgramData.h b/src/api/iptux-core/ProgramData.h index de90e84a..1a02433b 100644 --- a/src/api/iptux-core/ProgramData.h +++ b/src/api/iptux-core/ProgramData.h @@ -52,7 +52,12 @@ class ProgramData { void Lock(); void Unlock(); - bool IsAutoOpenCharDialog() const; + const std::string& GetPasswd() const { return passwd; } + void SetPasswd(const std::string& val) { passwd = val; } + int getSendMessageRetryInUs() const { return send_message_retry_in_us; } + + uint16_t port() const { return port_; } + bool IsAutoOpenChatDialog() const; bool IsAutoHidePanelAfterLogin() const; bool IsAutoOpenFileTrans() const; bool IsEnterSendMessage() const; @@ -60,14 +65,15 @@ class ProgramData { bool IsSaveChatHistory() const; bool IsUsingBlacklist() const; bool IsFilterFileShareRequest() const; - void SetFlag(int idx, bool flag); - - const std::string& GetPasswd() const { return passwd; } - void SetPasswd(const std::string& val) { passwd = val; } - int getSendMessageRetryInUs() const { return send_message_retry_in_us; } - - uint16_t port() const { return port_; } void set_port(uint16_t port, bool is_init = false); + void setOpenChat(bool value) { open_chat = value; } + void setHideStartup(bool value) { hide_startup = value; } + void setOpenTransmission(bool value) { open_transmission = value; } + void setUseEnterKey(bool value) { use_enter_key = value; } + void setClearupHistory(bool value) { clearup_history = value; } + void setRecordLog(bool value) { record_log = value; } + void setOpenBlacklist(bool value) { open_blacklist = value; } + void setProofShared(bool value) { proof_shared = value; } bool need_restart() const { return need_restart_; } @@ -99,10 +105,17 @@ class ProgramData { std::vector netseg; // 需要通知登录的IP段 std::shared_ptr config; std::mutex mutex; // 锁 - uint8_t flags; // 6 图标,5 传输:4 enter:3 历史:2 日志:1 黑名单:0 共享 std::string passwd; std::vector sharedFileInfos; - bool need_restart_ = false; + uint8_t open_chat : 1; + uint8_t hide_startup : 1; + uint8_t open_transmission : 1; + uint8_t use_enter_key : 1; + uint8_t clearup_history : 1; + uint8_t record_log : 1; + uint8_t open_blacklist : 1; + uint8_t proof_shared : 1; + uint8_t need_restart_ : 1; private: void InitSublayer(); diff --git a/src/iptux-core/Models.cpp b/src/iptux-core/Models.cpp index ae231d33..8de2dd3d 100644 --- a/src/iptux-core/Models.cpp +++ b/src/iptux-core/Models.cpp @@ -27,15 +27,23 @@ using namespace std; namespace iptux { PalInfo::PalInfo(in_addr ipv4, uint16_t port) - : segdes(NULL), photo(NULL), sign(NULL), packetn(0), rpacketn(0), flags(0) { + : segdes(NULL), photo(NULL), sign(NULL), packetn(0), rpacketn(0) { this->ipv4_ = ipv4; this->port_ = port; + compatible = 0; + online = 0; + changed = 0; + in_blacklist = 0; } PalInfo::PalInfo(const string& ipv4, uint16_t port) - : segdes(NULL), photo(NULL), sign(NULL), packetn(0), rpacketn(0), flags(0) { + : segdes(NULL), photo(NULL), sign(NULL), packetn(0), rpacketn(0) { this->ipv4_ = inAddrFromString(ipv4); this->port_ = port; + compatible = 0; + online = 0; + changed = 0; + in_blacklist = 0; } PalInfo::~PalInfo() { @@ -45,41 +53,29 @@ PalInfo::~PalInfo() { } bool PalInfo::isCompatible() const { - return FLAG_ISSET(this->flags, 0); + return compatible; } bool PalInfo::isOnline() const { - return FLAG_ISSET(this->flags, 1); + return online; } bool PalInfo::isChanged() const { - return FLAG_ISSET(this->flags, 2); + return changed; } PalInfo& PalInfo::setCompatible(bool value) { - if (value) { - FLAG_SET(this->flags, 0); - } else { - FLAG_CLR(this->flags, 0); - } + this->compatible = value; return *this; } PalInfo& PalInfo::setOnline(bool value) { - if (value) { - FLAG_SET(this->flags, 1); - } else { - FLAG_CLR(this->flags, 1); - } + this->online = value; return *this; } PalInfo& PalInfo::setChanged(bool value) { - if (value) { - FLAG_SET(this->flags, 2); - } else { - FLAG_CLR(this->flags, 2); - } + this->changed = value; return *this; } @@ -116,11 +112,12 @@ PalInfo& PalInfo::setGroup(const std::string& group) { string PalInfo::toString() const { return stringFormat( "PalInfo(IP=%s,name=%s,segdes=%s,version=%s,user=%s,host=%s,group=%s," - "photo=%s,sign=%s,iconfile=%s,encode=%s,packetn=%d,rpacketn=%d,flags=%d)", + "photo=%s,sign=%s,iconfile=%s,encode=%s,packetn=%d,rpacketn=%d," + "compatible=%d,online=%d,changed=%d,in_blacklist=%d)", inAddrToString(ipv4()).c_str(), name.c_str(), segdes, version.c_str(), user.c_str(), host.c_str(), group.c_str(), photo ? photo : "(NULL)", sign ? sign : "(NULL)", icon_file_.c_str(), encode.c_str(), int(packetn), - int(rpacketn), int(flags)); + int(rpacketn), compatible, online, changed, in_blacklist); } FileInfo::FileInfo() diff --git a/src/iptux-core/ProgramData.cpp b/src/iptux-core/ProgramData.cpp index f6528f14..2b988209 100644 --- a/src/iptux-core/ProgramData.cpp +++ b/src/iptux-core/ProgramData.cpp @@ -18,7 +18,7 @@ static const char* CONFIG_SHARED_FILE_LIST = "shared_file_list"; * 类构造函数. */ ProgramData::ProgramData(shared_ptr config) - : palicon(NULL), font(NULL), config(config), flags(0) { + : palicon(NULL), font(NULL), config(config), need_restart_(0) { gettimeofday(×tamp, NULL); InitSublayer(); } @@ -59,14 +59,14 @@ void ProgramData::WriteProgData() { config->SetString("pal_icon", palicon); config->SetString("panel_font", font); - config->SetBool("open_chat", FLAG_ISSET(flags, 7)); - config->SetBool("hide_startup", FLAG_ISSET(flags, 6)); - config->SetBool("open_transmission", FLAG_ISSET(flags, 5)); - config->SetBool("use_enter_key", FLAG_ISSET(flags, 4)); - config->SetBool("clearup_history", FLAG_ISSET(flags, 3)); - config->SetBool("record_log", FLAG_ISSET(flags, 2)); - config->SetBool("open_blacklist", FLAG_ISSET(flags, 1)); - config->SetBool("proof_shared", FLAG_ISSET(flags, 0)); + config->SetBool("open_chat", open_chat); + config->SetBool("hide_startup", hide_startup); + config->SetBool("open_transmission", open_transmission); + config->SetBool("use_enter_key", use_enter_key); + config->SetBool("clearup_history", clearup_history); + config->SetBool("record_log", record_log); + config->SetBool("open_blacklist", open_blacklist); + config->SetBool("proof_shared", proof_shared); config->SetString("access_shared_limit", passwd); config->SetInt("send_message_retry_in_us", send_message_retry_in_us); @@ -133,14 +133,14 @@ void ProgramData::ReadProgData() { palicon = g_strdup(config->GetString("pal_icon", "icon-qq.png").c_str()); font = g_strdup(config->GetString("panel_font", "Sans Serif 10").c_str()); - FLAG_SET(flags, 7, config->GetBool("open_chat")); - FLAG_SET(flags, 6, config->GetBool("hide_startup")); - FLAG_SET(flags, 5, config->GetBool("open_transmission")); - FLAG_SET(flags, 4, config->GetBool("use_enter_key")); - FLAG_SET(flags, 3, config->GetBool("clearup_history")); - FLAG_SET(flags, 2, config->GetBool("record_log", true)); - FLAG_SET(flags, 1, config->GetBool("open_blacklist")); - FLAG_SET(flags, 0, config->GetBool("proof_shared")); + open_chat = config->GetBool("open_chat"); + hide_startup = config->GetBool("hide_startup"); + open_transmission = config->GetBool("open_transmission"); + use_enter_key = config->GetBool("use_enter_key"); + clearup_history = config->GetBool("clearup_history"); + record_log = config->GetBool("record_log", true); + open_blacklist = config->GetBool("open_blacklist"); + proof_shared = config->GetBool("proof_shared"); passwd = config->GetString("access_shared_limit"); send_message_retry_in_us = @@ -207,43 +207,35 @@ void ProgramData::Unlock() { mutex.unlock(); } -bool ProgramData::IsAutoOpenCharDialog() const { - return FLAG_ISSET(flags, 7); +bool ProgramData::IsAutoOpenChatDialog() const { + return open_chat; } bool ProgramData::IsAutoHidePanelAfterLogin() const { - return FLAG_ISSET(flags, 6); + return hide_startup; } bool ProgramData::IsAutoOpenFileTrans() const { - return FLAG_ISSET(flags, 5); + return open_transmission; } bool ProgramData::IsEnterSendMessage() const { - return FLAG_ISSET(flags, 4); + return use_enter_key; } bool ProgramData::IsAutoCleanChatHistory() const { - return FLAG_ISSET(flags, 3); + return clearup_history; } bool ProgramData::IsSaveChatHistory() const { - return FLAG_ISSET(flags, 2); + return record_log; } bool ProgramData::IsUsingBlacklist() const { - return FLAG_ISSET(flags, 1); + return open_blacklist; } bool ProgramData::IsFilterFileShareRequest() const { - return FLAG_ISSET(flags, 0); -} - -void ProgramData::SetFlag(int idx, bool flag) { - if (flag) { - FLAG_SET(flags, idx); - } else { - FLAG_CLR(flags, idx); - } + return proof_shared; } ProgramData& ProgramData::SetUsingBlacklist(bool value) { - SetFlag(1, value); + open_blacklist = value; return *this; } diff --git a/src/iptux-core/internal/UdpDataServiceTest.cpp b/src/iptux-core/internal/UdpDataServiceTest.cpp index 6d77450e..59b955f4 100644 --- a/src/iptux-core/internal/UdpDataServiceTest.cpp +++ b/src/iptux-core/internal/UdpDataServiceTest.cpp @@ -37,8 +37,9 @@ TEST(UdpDataService, CreatePalInfo) { ASSERT_EQ(pal->toString(), "PalInfo(IP=127.0.0.1,name=中��,segdes=,version=1_iptux " "0.8.0-b1,user=lidaobing,host=LIs-MacBook-Pro.local," - "group=,photo=(NULL),sign=(NULL),iconfile=icon-qq.png,encode=utf-" - "8,packetn=0,rpacketn=0,flags=2)"); + "group=,photo=(NULL),sign=(NULL),iconfile=icon-qq.png," + "encode=utf-8,packetn=0,rpacketn=0,compatible=0,online=1," + "changed=0,in_blacklist=0)"); } { const char* data = @@ -53,6 +54,7 @@ TEST(UdpDataService, CreatePalInfo) { "PalInfo(IP=127.0.0.1,name=中��,segdes=,version=1_iptux " "0.8.0-b1,user=中��,host=LIs-MacBook-Pro.local," "group=,photo=(NULL),sign=(NULL),iconfile=icon-qq.png,encode=utf-" - "8,packetn=0,rpacketn=0,flags=2)"); + "8,packetn=0,rpacketn=0,compatible=0,online=1,changed=0,in_" + "blacklist=0)"); } } diff --git a/src/iptux-utils/UtilsTest.cpp b/src/iptux-utils/UtilsTest.cpp index 7bb3c43a..b09ca424 100644 --- a/src/iptux-utils/UtilsTest.cpp +++ b/src/iptux-utils/UtilsTest.cpp @@ -7,16 +7,6 @@ using namespace iptux; using namespace std; -TEST(Utils, FLAG_SET) { - uint8_t a = 1; - FLAG_SET(a, 0, false); - EXPECT_EQ(a, 0); - - a = 1; - FLAG_SET(a, 1, true); - EXPECT_EQ(a, 3); -} - TEST(Utils, numeric_to_size) { EXPECT_STREQ(numeric_to_size(0), "0B"); EXPECT_STREQ(numeric_to_size(1), "1B"); diff --git a/src/iptux-utils/utils.cpp b/src/iptux-utils/utils.cpp index 3b2e8e75..0e1dd737 100644 --- a/src/iptux-utils/utils.cpp +++ b/src/iptux-utils/utils.cpp @@ -532,18 +532,6 @@ char* ipmsg_get_pathname_full(const char* path, const char* name) { return g_strdup(filename); } -void FLAG_SET(uint8_t& num, int bit) { - ((num) |= (1 << (bit))); -} - -void FLAG_SET(uint8_t& num, int bit, bool value) { - if (value) { - ((num) |= (1 << (bit))); - } else { - ((num) &= (~(1 << (bit)))); - } -} - std::string inAddrToString(in_addr inAddr) { char res[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &inAddr.s_addr, res, INET_ADDRSTRLEN); diff --git a/src/iptux-utils/utils.h b/src/iptux-utils/utils.h index def26622..9482df5a 100644 --- a/src/iptux-utils/utils.h +++ b/src/iptux-utils/utils.h @@ -25,11 +25,6 @@ namespace iptux { 1000000.0f) #define percent(num1, num2) (100.0f * (num1) / (num2)) -#define FLAG_ISSET(num, bit) ((num) & (1 << (bit))) -void FLAG_SET(uint8_t& num, int bit); -void FLAG_SET(uint8_t& num, int bit, bool value); -#define FLAG_CLR(num, bit) ((num) &= (~(1 << (bit)))) - #define URL_REGEX \ "(http|ftp|https|sftp):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+" \ "([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?" diff --git a/src/iptux/DataSettings.cpp b/src/iptux/DataSettings.cpp index 842121ad..14b3ba1a 100644 --- a/src/iptux/DataSettings.cpp +++ b/src/iptux/DataSettings.cpp @@ -553,7 +553,7 @@ void DataSettings::SetSystemValue() { gtk_font_chooser_set_font(GTK_FONT_CHOOSER(widget), g_progdt->font); widget = GTK_WIDGET(g_datalist_get_data(&widset, "chat-check-widget")); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), - g_progdt->IsAutoOpenCharDialog()); + g_progdt->IsAutoOpenChatDialog()); widget = GTK_WIDGET(g_datalist_get_data(&widset, "statusicon-check-widget")); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), g_progdt->IsAutoHidePanelAfterLogin()); @@ -919,22 +919,30 @@ string DataSettings::ObtainSystemValue(bool dryrun) { g_strdup(gtk_font_chooser_get_font(GTK_FONT_CHOOSER(widget))); widget = GTK_WIDGET(g_datalist_get_data(&widset, "chat-check-widget")); - g_progdt->SetFlag(7, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); + g_progdt->setOpenChat( + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); widget = GTK_WIDGET(g_datalist_get_data(&widset, "statusicon-check-widget")); - g_progdt->SetFlag(6, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); + g_progdt->setHideStartup( + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); widget = GTK_WIDGET(g_datalist_get_data(&widset, "transmission-check-widget")); - g_progdt->SetFlag(5, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); + g_progdt->setOpenTransmission( + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); widget = GTK_WIDGET(g_datalist_get_data(&widset, "enterkey-check-widget")); - g_progdt->SetFlag(4, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); + g_progdt->setUseEnterKey( + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); widget = GTK_WIDGET(g_datalist_get_data(&widset, "history-check-widget")); - g_progdt->SetFlag(3, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); + g_progdt->setClearupHistory( + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); widget = GTK_WIDGET(g_datalist_get_data(&widset, "log-check-widget")); - g_progdt->SetFlag(2, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); + g_progdt->setRecordLog( + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); widget = GTK_WIDGET(g_datalist_get_data(&widset, "blacklist-check-widget")); - g_progdt->SetFlag(1, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); + g_progdt->setOpenBlacklist( + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); widget = GTK_WIDGET(g_datalist_get_data(&widset, "shared-check-widget")); - g_progdt->SetFlag(0, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); + g_progdt->setProofShared( + gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))); return oss.str(); } diff --git a/src/iptux/MainWindow.cpp b/src/iptux/MainWindow.cpp index 5abb2d44..933955f5 100644 --- a/src/iptux/MainWindow.cpp +++ b/src/iptux/MainWindow.cpp @@ -1671,7 +1671,7 @@ void MainWindow::ProcessEvent(shared_ptr _event) { switch (para.btype) { case GROUP_BELONG_TYPE_REGULAR: grpinf = coreThread.GetPalRegularItem(para.getPal().get()); - if (coreThread.getProgramData()->IsAutoOpenCharDialog()) { + if (coreThread.getProgramData()->IsAutoOpenChatDialog()) { if (!(grpinf->getDialog())) { DialogPeer::PeerDialogEntry(this->app, grpinf); } else {