Skip to content

Commit

Permalink
remove flags (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidaobing authored Jul 7, 2024
1 parent 047e579 commit dedf3c0
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 109 deletions.
7 changes: 5 additions & 2 deletions src/api/iptux-core/Models.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 23 additions & 10 deletions src/api/iptux-core/ProgramData.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,28 @@ 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;
bool IsAutoCleanChatHistory() const;
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_; }

Expand Down Expand Up @@ -99,10 +105,17 @@ class ProgramData {
std::vector<NetSegment> netseg; // 需要通知登录的IP段
std::shared_ptr<IptuxConfig> config;
std::mutex mutex; //
uint8_t flags; // 6 图标,5 传输:4 enter:3 历史:2 日志:1 黑名单:0 共享
std::string passwd;
std::vector<FileInfo> 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();
Expand Down
41 changes: 19 additions & 22 deletions src/iptux-core/Models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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()
Expand Down
62 changes: 27 additions & 35 deletions src/iptux-core/ProgramData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static const char* CONFIG_SHARED_FILE_LIST = "shared_file_list";
* 类构造函数.
*/
ProgramData::ProgramData(shared_ptr<IptuxConfig> config)
: palicon(NULL), font(NULL), config(config), flags(0) {
: palicon(NULL), font(NULL), config(config), need_restart_(0) {
gettimeofday(&timestamp, NULL);
InitSublayer();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 5 additions & 3 deletions src/iptux-core/internal/UdpDataServiceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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)");
}
}
10 changes: 0 additions & 10 deletions src/iptux-utils/UtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
12 changes: 0 additions & 12 deletions src/iptux-utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions src/iptux-utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#])?"
Expand Down
26 changes: 17 additions & 9 deletions src/iptux/DataSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
}

Expand Down
Loading

0 comments on commit dedf3c0

Please sign in to comment.