Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor groupinfo #487

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
7 changes: 5 additions & 2 deletions src/api/iptux-core/Models.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class PalKey {
PalKey(in_addr ipv4, int port);

bool operator==(const PalKey& rhs) const;
bool operator<(const PalKey& rhs) const;

in_addr GetIpv4() const { return ipv4; }
std::string GetIpv4String() const;
Expand Down Expand Up @@ -133,10 +134,12 @@ class PalInfo {
};

/// pointer to PalInfo
using PPalInfo = std::shared_ptr<PalInfo>;
using PalInfo_S = std::shared_ptr<PalInfo>;
using PPalInfo = PalInfo_S;

/// const pointer to PalInfo
using CPPalInfo = std::shared_ptr<const PalInfo>;
using PalInfo_SC = std::shared_ptr<const PalInfo>;
using CPPalInfo = PalInfo_SC;

enum class FileAttr : std::uint32_t { UNKNOWN, REGULAR, DIRECTORY };

Expand Down
3 changes: 3 additions & 0 deletions src/api/iptux-core/ProgramData.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ class ProgramData {
void WriteNetSegment();
void ReadNetSegment();
};

using ProgramData_S = std::shared_ptr<ProgramData>;

} // namespace iptux

#endif // IPTUX_PROGRAMDATACORE_H
10 changes: 10 additions & 0 deletions src/iptux-core/Models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@
return ipv4Equal(this->ipv4, rhs.ipv4) && this->port == rhs.port;
}

bool PalKey::operator<(const PalKey& rhs) const {
if (ipv4Compare(this->ipv4, rhs.ipv4) < 0) {
return true;
} else if (ipv4Compare(this->ipv4, rhs.ipv4) > 0) {
return false;

Check warning on line 274 in src/iptux-core/Models.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux-core/Models.cpp#L270-L274

Added lines #L270 - L274 were not covered by tests
} else {
return this->port < rhs.port;

Check warning on line 276 in src/iptux-core/Models.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux-core/Models.cpp#L276

Added line #L276 was not covered by tests
}
}

string PalKey::ToString() const {
return stringFormat("%s:%d", inAddrToString(ipv4).c_str(), port);
}
Expand Down
7 changes: 2 additions & 5 deletions src/iptux/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void onWhatsNew() {
iptux_open_url("https://github.com/iptux-src/iptux/blob/master/NEWS.md");
}

void iptux_init(LogSystem* logSystem) {
void iptux_init(LogSystem_S logSystem) {
signal(SIGPIPE, SIG_IGN);
logSystem->systemLog("%s", _("Loading the process successfully!"));
}
Expand Down Expand Up @@ -99,9 +99,6 @@ Application::~Application() {
if (eventAdaptor) {
delete eventAdaptor;
}
if (logSystem) {
delete logSystem;
}
delete window;
delete notificationService;
}
Expand All @@ -124,7 +121,7 @@ void Application::onStartup(Application& self) {
self.menuBuilder =
gtk_builder_new_from_resource(IPTUX_RESOURCE "gtk/menus.ui");
self.data = make_shared<ProgramData>(self.config);
self.logSystem = new LogSystem(self.data);
self.logSystem = make_shared<LogSystem>(self.data);
self.cthrd = make_shared<UiCoreThread>(&self, self.data);
self.window = new MainWindow(&self, *self.cthrd);
self.eventAdaptor = new EventAdaptor(
Expand Down
4 changes: 2 additions & 2 deletions src/iptux/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Application {
TransModel* getTransModel() { return transModel; }
MainWindow* getMainWindow() { return window; }
GtkBuilder* getMenuBuilder() { return menuBuilder; }
LogSystem* getLogSystem() { return logSystem; }
LogSystem_S getLogSystem() { return logSystem; }
std::shared_ptr<ProgramData> getProgramData() { return data; }
std::shared_ptr<UiCoreThread> getCoreThread() { return cthrd; }
void refreshTransTasks();
Expand All @@ -50,7 +50,7 @@ class Application {
ShareFile* shareFile = 0;
TransWindow* transWindow = 0;
EventAdaptor* eventAdaptor = 0;
LogSystem* logSystem = 0;
LogSystem_S logSystem;
NotificationService* notificationService = 0;
bool started{false};

Expand Down
1 change: 1 addition & 0 deletions src/iptux/DialogBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "iptux-core/Models.h"
#include "iptux/Application.h"
#include "iptux/GroupInfo.h"
#include "iptux/UiModels.h"

namespace iptux {
Expand Down
1 change: 1 addition & 0 deletions src/iptux/DialogPeer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "iptux-core/Models.h"
#include "iptux/Application.h"
#include "iptux/DialogBase.h"
#include "iptux/GroupInfo.h"

namespace iptux {

Expand Down
117 changes: 117 additions & 0 deletions src/iptux/GroupInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include "config.h"
#include "GroupInfo.h"

#include "iptux-utils/output.h"
#include "iptux/DialogBase.h"

using namespace std;

namespace iptux {

GroupInfo::GroupInfo(iptux::GroupBelongType t,
const vector<PPalInfo>& pals,
CPPalInfo me,
LogSystem_S logSystem)
: grpid(0),
buffer(NULL),
dialogBase(NULL),
me(me),
members(pals),
type(t),
logSystem(logSystem) {
inputBuffer = gtk_text_buffer_new(NULL);
}

GroupInfo::GroupInfo(PPalInfo pal, CPPalInfo me, LogSystem_S logSystem)
: grpid(0),
buffer(NULL),
dialogBase(NULL),
me(me),
type(GROUP_BELONG_TYPE_REGULAR),
logSystem(logSystem) {
members.push_back(pal);
inputBuffer = gtk_text_buffer_new(NULL);
}

GroupInfo::~GroupInfo() {
g_object_unref(buffer);
}

bool GroupInfo::hasPal(PalInfo* pal) const {
for (auto i : members) {
if (i.get() == pal) {
return true;

Check warning on line 43 in src/iptux/GroupInfo.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/GroupInfo.cpp#L42-L43

Added lines #L42 - L43 were not covered by tests
}
}
return false;
}

bool GroupInfo::hasPal(PPalInfo pal) const {
return hasPal(pal.get());
}

GtkWidget* GroupInfo::getDialog() const {
return dialogBase ? GTK_WIDGET(dialogBase->getWindow()) : nullptr;
}

GroupInfo::KeyType GroupInfo::getKey() const {
if (type == GROUP_BELONG_TYPE_REGULAR) {
return make_pair(type, getMembers()[0]->GetKey().ToString());
}
return make_pair(type, name);
}

GroupInfo::KeyType GroupInfo::genKey(const PalInfo* pal) {
return make_pair(GROUP_BELONG_TYPE_REGULAR, pal->GetKey().ToString());
}

bool GroupInfo::addPal(PPalInfo pal) {
if (type == GROUP_BELONG_TYPE_REGULAR) {
LOG_WARN("should not call addPal on GROUP_BELONG_TYPE_REGULAR");
return false;

Check warning on line 71 in src/iptux/GroupInfo.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/GroupInfo.cpp#L70-L71

Added lines #L70 - L71 were not covered by tests
}
if (hasPal(pal)) {
return false;

Check warning on line 74 in src/iptux/GroupInfo.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/GroupInfo.cpp#L74

Added line #L74 was not covered by tests
}
members.push_back(pal);
return true;
}

bool GroupInfo::delPal(PalInfo* pal) {
if (type == GROUP_BELONG_TYPE_REGULAR) {
LOG_WARN("should not call delPal on GROUP_BELONG_TYPE_REGULAR");
return false;

Check warning on line 83 in src/iptux/GroupInfo.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/GroupInfo.cpp#L80-L83

Added lines #L80 - L83 were not covered by tests
}

for (auto it = members.begin(); it != members.end(); ++it) {
if (it->get() == pal) {
members.erase(it);
return true;

Check warning on line 89 in src/iptux/GroupInfo.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/GroupInfo.cpp#L86-L89

Added lines #L86 - L89 were not covered by tests
}
}
return false;

Check warning on line 92 in src/iptux/GroupInfo.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/GroupInfo.cpp#L92

Added line #L92 was not covered by tests
}

void GroupInfo::newFileReceived() {
this->signalNewFileReceived.emit(this);

Check warning on line 96 in src/iptux/GroupInfo.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/GroupInfo.cpp#L95-L96

Added lines #L95 - L96 were not covered by tests
}

void GroupInfo::addMsgCount(int i) {
int oldCount = getUnreadMsgCount();
allMsgCount += i;
signalUnreadMsgCountUpdated.emit(this, oldCount, getUnreadMsgCount());

Check warning on line 102 in src/iptux/GroupInfo.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/GroupInfo.cpp#L99-L102

Added lines #L99 - L102 were not covered by tests
}

void GroupInfo::readAllMsg() {
int oldCount = getUnreadMsgCount();
if (oldCount != 0) {
readMsgCount = allMsgCount;
signalUnreadMsgCountUpdated.emit(this, oldCount, getUnreadMsgCount());

Check warning on line 109 in src/iptux/GroupInfo.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/GroupInfo.cpp#L105-L109

Added lines #L105 - L109 were not covered by tests
}
}

int GroupInfo::getUnreadMsgCount() const {
g_assert(allMsgCount >= readMsgCount);
return allMsgCount - readMsgCount;

Check warning on line 115 in src/iptux/GroupInfo.cpp

View check run for this annotation

Codecov / codecov/patch

src/iptux/GroupInfo.cpp#L113-L115

Added lines #L113 - L115 were not covered by tests
}
} // namespace iptux
89 changes: 89 additions & 0 deletions src/iptux/GroupInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifndef IPTUX_GROUP_INFO_H
#define IPTUX_GROUP_INFO_H

#include <gtk/gtk.h>
#include <sigc++/signal.h>

#include "iptux-core/Models.h"
#include "iptux/LogSystem.h"

namespace iptux {
/**
* 群组信息.
*/
class DialogBase;
class GroupInfoManager;
class GroupInfo {
private:
// only for friend class GroupInfoManager
GroupInfo(PPalInfo pal, CPPalInfo me, LogSystem_S logSystem);

public:
GroupInfo(GroupBelongType type,
const std::vector<PPalInfo>& pals,
CPPalInfo me,
LogSystem_S logSystem);

~GroupInfo();

const std::vector<PPalInfo>& getMembers() const { return members; }
GroupBelongType getType() const { return type; }

using KeyType = std::pair<GroupBelongType, std::string>;
KeyType getKey() const;
static KeyType genKey(const PalInfo* pal);

/** return true if successful added, noop for regular group */
bool addPal(PPalInfo pal);

/** return true if successful deleted, noop for regular group */
bool delPal(PalInfo* pal);

/** return true if successful deleted, noop for regulat group */
bool delPal(PPalInfo pal);

bool hasPal(PalInfo* pal) const;
bool hasPal(PPalInfo pal) const;

void addMsgPara(const MsgPara& msg);
void readAllMsg();
int getUnreadMsgCount() const;
void newFileReceived();

GtkTextBuffer* getInputBuffer() const { return inputBuffer; }

void setDialogBase(DialogBase* dialogBase) { this->dialogBase = dialogBase; }
GtkWidget* getDialog() const;
void clearDialog() { dialogBase = nullptr; }

public:
sigc::signal<void(GroupInfo*, int, int)> signalUnreadMsgCountUpdated;
sigc::signal<void(GroupInfo*)> signalNewFileReceived;

public:
GQuark grpid; ///< 唯一标识
std::string name; ///< 群组名称 *
GtkTextBuffer* buffer; ///< 历史消息缓冲区 *

private:
DialogBase* dialogBase;
GtkTextBuffer* inputBuffer; /// 输入缓冲

private:
CPPalInfo me;
std::vector<PPalInfo> members;
GroupBelongType type; ///< 群组类型
LogSystem_S logSystem;
int allMsgCount = 0; /* all received message count */
int readMsgCount = 0; /* already read message count */

private:
void addMsgCount(int i);
friend GroupInfoManager;
};

using GroupInfo_S = std::shared_ptr<GroupInfo>;

} // namespace iptux

#endif
49 changes: 49 additions & 0 deletions src/iptux/GroupInfoManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "config.h"
#include "GroupInfoManager.h"

#include "iptux-utils/utils.h"
#include "iptux/UiCoreThread.h"

using namespace std;

namespace iptux {

GroupInfoManager::GroupInfoManager(UiCoreThread* coreThread,
LogSystem_S logSystem)
: core_thread_(coreThread), logSystem(logSystem) {}

GroupInfo_S GroupInfoManager::addPal(PalInfo_S pal, PalInfo_SC me) {
GroupInfo_S grpinf(new GroupInfo(pal, me, logSystem));
grpinf->grpid = inAddrToUint32(pal->ipv4);
grpinf->name = pal->getName();
grpinf->buffer = gtk_text_buffer_new(core_thread_->tag_table());
grpinf->clearDialog();
addGroupInfo(grpinf);
return grpinf;
}

GroupInfo_S GroupInfoManager::getGroupInfo(const GroupInfo::KeyType& key) {
return groupInfos[key];
}

GroupInfo_S GroupInfoManager::addGroup(GroupBelongType type,
PalInfo_SC me,
std::string name) {
GroupInfo_S grpinf(new GroupInfo(type, vector<PPalInfo>(), me, logSystem));
grpinf->grpid = g_quark_from_static_string(name.c_str());
grpinf->name = name;
grpinf->buffer = gtk_text_buffer_new(core_thread_->tag_table());
grpinf->clearDialog();
addGroupInfo(grpinf);
return grpinf;
}

GroupInfo_S GroupInfoManager::getGroupInfo(const PalInfo* pal) {
return groupInfos[GroupInfo::genKey(pal)];
}

void GroupInfoManager::addGroupInfo(GroupInfo_S groupInfo) {
groupInfos[groupInfo->getKey()] = groupInfo;
}

} // namespace iptux
Loading
Loading