diff --git a/src/api/iptux-core/Models.h b/src/api/iptux-core/Models.h index a7cf11e01..51cd5ede0 100644 --- a/src/api/iptux-core/Models.h +++ b/src/api/iptux-core/Models.h @@ -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; @@ -133,10 +134,12 @@ class PalInfo { }; /// pointer to PalInfo -using PPalInfo = std::shared_ptr; +using PalInfo_S = std::shared_ptr; +using PPalInfo = PalInfo_S; /// const pointer to PalInfo -using CPPalInfo = std::shared_ptr; +using PalInfo_SC = std::shared_ptr; +using CPPalInfo = PalInfo_SC; enum class FileAttr : std::uint32_t { UNKNOWN, REGULAR, DIRECTORY }; diff --git a/src/api/iptux-core/ProgramData.h b/src/api/iptux-core/ProgramData.h index 1ad383904..3ef1cbd29 100644 --- a/src/api/iptux-core/ProgramData.h +++ b/src/api/iptux-core/ProgramData.h @@ -104,6 +104,9 @@ class ProgramData { void WriteNetSegment(); void ReadNetSegment(); }; + +using ProgramData_S = std::shared_ptr; + } // namespace iptux #endif // IPTUX_PROGRAMDATACORE_H diff --git a/src/iptux-core/Models.cpp b/src/iptux-core/Models.cpp index 908e3ea2d..e5af8e13e 100644 --- a/src/iptux-core/Models.cpp +++ b/src/iptux-core/Models.cpp @@ -267,6 +267,16 @@ bool PalKey::operator==(const PalKey& rhs) const { 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; + } else { + return this->port < rhs.port; + } +} + string PalKey::ToString() const { return stringFormat("%s:%d", inAddrToString(ipv4).c_str(), port); } diff --git a/src/iptux/Application.cpp b/src/iptux/Application.cpp index 5030f28fb..3d1481362 100644 --- a/src/iptux/Application.cpp +++ b/src/iptux/Application.cpp @@ -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!")); } @@ -99,9 +99,6 @@ Application::~Application() { if (eventAdaptor) { delete eventAdaptor; } - if (logSystem) { - delete logSystem; - } delete window; delete notificationService; } @@ -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(self.config); - self.logSystem = new LogSystem(self.data); + self.logSystem = make_shared(self.data); self.cthrd = make_shared(&self, self.data); self.window = new MainWindow(&self, *self.cthrd); self.eventAdaptor = new EventAdaptor( diff --git a/src/iptux/Application.h b/src/iptux/Application.h index 062607ee3..882261295 100644 --- a/src/iptux/Application.h +++ b/src/iptux/Application.h @@ -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 getProgramData() { return data; } std::shared_ptr getCoreThread() { return cthrd; } void refreshTransTasks(); @@ -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}; diff --git a/src/iptux/DialogBase.h b/src/iptux/DialogBase.h index 78986f92b..cbb9644f8 100644 --- a/src/iptux/DialogBase.h +++ b/src/iptux/DialogBase.h @@ -19,6 +19,7 @@ #include "iptux-core/Models.h" #include "iptux/Application.h" +#include "iptux/GroupInfo.h" #include "iptux/UiModels.h" namespace iptux { diff --git a/src/iptux/DialogPeer.h b/src/iptux/DialogPeer.h index 76d7a59c1..e98ed4f6f 100644 --- a/src/iptux/DialogPeer.h +++ b/src/iptux/DialogPeer.h @@ -17,6 +17,7 @@ #include "iptux-core/Models.h" #include "iptux/Application.h" #include "iptux/DialogBase.h" +#include "iptux/GroupInfo.h" namespace iptux { diff --git a/src/iptux/GroupInfo.cpp b/src/iptux/GroupInfo.cpp new file mode 100644 index 000000000..ed078e6fa --- /dev/null +++ b/src/iptux/GroupInfo.cpp @@ -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& 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; + } + } + 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; + } + if (hasPal(pal)) { + return false; + } + 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; + } + + for (auto it = members.begin(); it != members.end(); ++it) { + if (it->get() == pal) { + members.erase(it); + return true; + } + } + return false; +} + +void GroupInfo::newFileReceived() { + this->signalNewFileReceived.emit(this); +} + +void GroupInfo::addMsgCount(int i) { + int oldCount = getUnreadMsgCount(); + allMsgCount += i; + signalUnreadMsgCountUpdated.emit(this, oldCount, getUnreadMsgCount()); +} + +void GroupInfo::readAllMsg() { + int oldCount = getUnreadMsgCount(); + if (oldCount != 0) { + readMsgCount = allMsgCount; + signalUnreadMsgCountUpdated.emit(this, oldCount, getUnreadMsgCount()); + } +} + +int GroupInfo::getUnreadMsgCount() const { + g_assert(allMsgCount >= readMsgCount); + return allMsgCount - readMsgCount; +} +} // namespace iptux diff --git a/src/iptux/GroupInfo.h b/src/iptux/GroupInfo.h new file mode 100644 index 000000000..6feadfc30 --- /dev/null +++ b/src/iptux/GroupInfo.h @@ -0,0 +1,89 @@ +#ifndef IPTUX_GROUP_INFO_H +#define IPTUX_GROUP_INFO_H + +#include +#include + +#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& pals, + CPPalInfo me, + LogSystem_S logSystem); + + ~GroupInfo(); + + const std::vector& getMembers() const { return members; } + GroupBelongType getType() const { return type; } + + using KeyType = std::pair; + 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 signalUnreadMsgCountUpdated; + sigc::signal signalNewFileReceived; + + public: + GQuark grpid; ///< 唯一标识 + std::string name; ///< 群组名称 * + GtkTextBuffer* buffer; ///< 历史消息缓冲区 * + + private: + DialogBase* dialogBase; + GtkTextBuffer* inputBuffer; /// 输入缓冲 + + private: + CPPalInfo me; + std::vector 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; + +} // namespace iptux + +#endif diff --git a/src/iptux/GroupInfoManager.cpp b/src/iptux/GroupInfoManager.cpp new file mode 100644 index 000000000..2a31ba02a --- /dev/null +++ b/src/iptux/GroupInfoManager.cpp @@ -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(), 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 diff --git a/src/iptux/GroupInfoManager.h b/src/iptux/GroupInfoManager.h new file mode 100644 index 000000000..665d23ba8 --- /dev/null +++ b/src/iptux/GroupInfoManager.h @@ -0,0 +1,32 @@ +#ifndef IPTUX_GROUP_INFO_MANAGER_H +#define IPTUX_GROUP_INFO_MANAGER_H + +#include "iptux/GroupInfo.h" +#include "iptux/LogSystem.h" + +namespace iptux { + +class UiCoreThread; +class GroupInfoManager { + public: + GroupInfoManager(UiCoreThread* coreThread, LogSystem_S logSystem); + + void addGroupInfo(GroupInfo_S groupInfo); + + GroupInfo_S addPal(PalInfo_S pal, PalInfo_SC me); + GroupInfo_S addGroup(GroupBelongType type, PalInfo_SC me, std::string name); + + GroupInfo_S getGroupInfo(const PalInfo* pal); + GroupInfo_S getGroupInfo(const GroupInfo::KeyType& key); + + private: + UiCoreThread* core_thread_; + LogSystem_S logSystem; + std::map groupInfos; +}; + +using GroupInfoManager_U = std::unique_ptr; + +} // namespace iptux + +#endif diff --git a/src/iptux/LogSystem.h b/src/iptux/LogSystem.h index 888faac4d..d2ae07df1 100644 --- a/src/iptux/LogSystem.h +++ b/src/iptux/LogSystem.h @@ -42,6 +42,8 @@ class LogSystem { void InitSublayer(); }; +using LogSystem_S = std::shared_ptr; + } // namespace iptux #endif diff --git a/src/iptux/UiCoreThread.cpp b/src/iptux/UiCoreThread.cpp index ba81493bd..de0066123 100644 --- a/src/iptux/UiCoreThread.cpp +++ b/src/iptux/UiCoreThread.cpp @@ -35,13 +35,12 @@ namespace iptux { UiCoreThread::UiCoreThread(Application* app, shared_ptr data) : CoreThread(data), programData(data), - groupInfos(NULL), - sgmlist(NULL), grplist(NULL), brdlist(NULL), pbn(1), prn(MAX_SHAREDFILE), ecsList(NULL) { + groupInfoManager = make_unique(this, app->getLogSystem()); tag_table_ = CreateTagTable(); CheckIconTheme(); logSystem = app->getLogSystem(); @@ -66,28 +65,6 @@ void UiCoreThread::ClearAllPalFromList() { GroupInfo* grpinf; GSList* tlist; - /* 清空常规模式下所有群组的成员 */ - tlist = groupInfos; - while (tlist) { - grpinf = (GroupInfo*)tlist->data; - if (grpinf->getDialog()) { - session = (SessionAbstract*)g_object_get_data( - G_OBJECT(grpinf->getDialog()), "session-class"); - session->ClearAllPalData(); - } - tlist = g_slist_next(tlist); - } - /* 清空网段模式下所有群组的成员 */ - tlist = sgmlist; - while (tlist) { - grpinf = (GroupInfo*)tlist->data; - if (grpinf->getDialog()) { - session = (SessionAbstract*)g_object_get_data( - G_OBJECT(grpinf->getDialog()), "session-class"); - session->ClearAllPalData(); - } - tlist = g_slist_next(tlist); - } /* 清空分组模式下所有群组的成员 */ tlist = grplist; while (tlist) { @@ -228,16 +205,8 @@ void UiCoreThread::AttachPalToList(shared_ptr pal2) { * @return 群组信息 */ GroupInfo* UiCoreThread::GetPalRegularItem(const PalInfo* pal) { - GSList* tlist; - - tlist = groupInfos; - while (tlist) { - if (((GroupInfo*)tlist->data)->grpid == inAddrToUint32(pal->ipv4)) - break; - tlist = g_slist_next(tlist); - } - - return (GroupInfo*)(tlist ? tlist->data : NULL); + auto res = groupInfoManager->getGroupInfo(pal); + return res ? res.get() : nullptr; } /** @@ -246,21 +215,12 @@ GroupInfo* UiCoreThread::GetPalRegularItem(const PalInfo* pal) { * @return 群组信息 */ GroupInfo* UiCoreThread::GetPalSegmentItem(const PalInfo* pal) { - GSList* tlist; - GQuark grpid; - /* 获取局域网网段ID */ auto name = ipv4_get_lan_name(pal->ipv4); - grpid = g_quark_from_string(name.empty() ? _("Others") : name.c_str()); - - tlist = sgmlist; - while (tlist) { - if (((GroupInfo*)tlist->data)->grpid == grpid) - break; - tlist = g_slist_next(tlist); - } - - return (GroupInfo*)(tlist ? tlist->data : NULL); + auto key = make_pair(GROUP_BELONG_TYPE_SEGMENT, + name.empty() ? _("Others") : name.c_str()); + auto res = groupInfoManager->getGroupInfo(key); + return res ? res.get() : nullptr; } /** @@ -309,12 +269,6 @@ void UiCoreThread::ClearSublayer() { CoreThread::ClearSublayer(); - for (tlist = groupInfos; tlist; tlist = g_slist_next(tlist)) - delete (GroupInfo*)tlist->data; - g_slist_free(groupInfos); - for (tlist = sgmlist; tlist; tlist = g_slist_next(tlist)) - delete (GroupInfo*)tlist->data; - g_slist_free(sgmlist); for (tlist = grplist; tlist; tlist = g_slist_next(tlist)) delete (GroupInfo*)tlist->data; g_slist_free(grplist); @@ -351,17 +305,10 @@ GroupInfo* UiCoreThread::GetPalPrevGroupItem(PalInfo* pal) { * @return 新加入的群组 */ GroupInfo* UiCoreThread::AttachPalRegularItem(PPalInfo pal) { - GroupInfo* grpinf; - - grpinf = new GroupInfo(pal, getMe(), logSystem); - grpinf->grpid = inAddrToUint32(pal->ipv4); - grpinf->name = pal->getName(); - grpinf->buffer = gtk_text_buffer_new(tag_table_); - grpinf->clearDialog(); + auto grpinf = groupInfoManager->addPal(pal, getMe()); grpinf->signalUnreadMsgCountUpdated.connect( sigc::mem_fun(*this, &UiCoreThread::onGroupInfoMsgCountUpdate)); - groupInfos = g_slist_append(groupInfos, grpinf); - return grpinf; + return grpinf.get(); } /** @@ -378,15 +325,18 @@ GroupInfo* UiCoreThread::AttachPalSegmentItem(PPalInfo pal) { name = _("Others"); } - grpinf = new GroupInfo(GROUP_BELONG_TYPE_SEGMENT, vector(), getMe(), - logSystem); - grpinf->grpid = g_quark_from_static_string(name.c_str()); - grpinf->name = name; - grpinf->buffer = gtk_text_buffer_new(tag_table_); - grpinf->clearDialog(); - sgmlist = g_slist_append(sgmlist, grpinf); - - return grpinf; + auto res = + groupInfoManager->addGroup(GROUP_BELONG_TYPE_SEGMENT, getMe(), name); + return res.get(); + + // grpinf = new GroupInfo(GROUP_BELONG_TYPE_SEGMENT, vector(), + // getMe(), + // logSystem); + // grpinf->grpid = g_quark_from_static_string(name.c_str()); + // grpinf->name = name; + // grpinf->buffer = gtk_text_buffer_new(programData->table); + // grpinf->clearDialog(); + // sgmlist = g_slist_append(sgmlist, grpinf); } /** diff --git a/src/iptux/UiCoreThread.h b/src/iptux/UiCoreThread.h index e6e5e723d..073f81e0e 100644 --- a/src/iptux/UiCoreThread.h +++ b/src/iptux/UiCoreThread.h @@ -23,6 +23,8 @@ #include "iptux-core/CoreThread.h" #include "iptux-core/Models.h" #include "iptux/Application.h" +#include "iptux/GroupInfo.h" +#include "iptux/GroupInfoManager.h" #include "iptux/UiModels.h" namespace iptux { @@ -57,7 +59,7 @@ class UiCoreThread : public CoreThread { void PushItemToEnclosureList(FileInfo* file); void PopItemFromEnclosureList(FileInfo* file); - LogSystem* getLogSystem() { return logSystem; } + LogSystem_S getLogSystem() { return logSystem; } GtkTextTagTable* tag_table() { return tag_table_; } @@ -81,10 +83,12 @@ class UiCoreThread : public CoreThread { private: std::shared_ptr programData; - LogSystem* logSystem; + LogSystem_S logSystem; std::queue messages; - GSList *groupInfos, *sgmlist, *grplist, *brdlist; //群组链表(成员不能被删除) + GroupInfoManager_U groupInfoManager; + // GSList *sgmlist; + GSList *grplist, *brdlist; //群组链表(成员不能被删除) uint32_t pbn, prn; //当前已使用的文件编号(共享/私有) GSList* ecsList; //文件链表(好友发过来) diff --git a/src/iptux/UiModels.cpp b/src/iptux/UiModels.cpp index 18438a1e5..2677a5d18 100644 --- a/src/iptux/UiModels.cpp +++ b/src/iptux/UiModels.cpp @@ -309,108 +309,6 @@ void palTreeModelFillFromGroupInfo(GtkTreeModel* model, pango_attr_list_unref(attrs); } -// GroupInfo::GroupInfo() -// : grpid(0), -// type(GROUP_BELONG_TYPE_REGULAR), -// member(NULL), -// buffer(NULL), -// dialog(NULL) {} -GroupInfo::~GroupInfo() { - g_object_unref(buffer); -} - -bool GroupInfo::hasPal(PalInfo* pal) const { - for (auto i : members) { - if (i.get() == pal) { - return true; - } - } - return false; -} - -bool GroupInfo::hasPal(PPalInfo pal) const { - return hasPal(pal.get()); -} - -GroupInfo::GroupInfo(PPalInfo pal, CPPalInfo me, LogSystem* 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(iptux::GroupBelongType t, - const vector& pals, - CPPalInfo me, - LogSystem* logSystem) - : grpid(0), - buffer(NULL), - dialogBase(NULL), - me(me), - members(pals), - type(t), - logSystem(logSystem) { - inputBuffer = gtk_text_buffer_new(NULL); -} - -GtkWidget* GroupInfo::getDialog() const { - return dialogBase ? GTK_WIDGET(dialogBase->getWindow()) : nullptr; -} - -bool GroupInfo::addPal(PPalInfo pal) { - if (type == GROUP_BELONG_TYPE_REGULAR) { - LOG_WARN("should not call addPal on GROUP_BELONG_TYPE_REGULAR"); - return false; - } - if (hasPal(pal)) { - return false; - } - 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; - } - - for (auto it = members.begin(); it != members.end(); ++it) { - if (it->get() == pal) { - members.erase(it); - return true; - } - } - return false; -} - -void GroupInfo::newFileReceived() { - this->signalNewFileReceived.emit(this); -} - -void GroupInfo::addMsgCount(int i) { - int oldCount = getUnreadMsgCount(); - allMsgCount += i; - signalUnreadMsgCountUpdated.emit(this, oldCount, getUnreadMsgCount()); -} - -void GroupInfo::readAllMsg() { - int oldCount = getUnreadMsgCount(); - if (oldCount != 0) { - readMsgCount = allMsgCount; - signalUnreadMsgCountUpdated.emit(this, oldCount, getUnreadMsgCount()); - } -} - -int GroupInfo::getUnreadMsgCount() const { - g_assert(allMsgCount >= readMsgCount); - return allMsgCount - readMsgCount; -} - /** * 插入字符串到TextBuffer(非UI线程安全). * @param buffer text-buffer diff --git a/src/iptux/UiModels.h b/src/iptux/UiModels.h index 1c94388dc..a81e7db19 100644 --- a/src/iptux/UiModels.h +++ b/src/iptux/UiModels.h @@ -2,10 +2,10 @@ #define IPTUX_UIMODELS_H #include -#include #include "iptux-core/Models.h" #include "iptux-core/TransFileModel.h" +#include "iptux/GroupInfo.h" #include "iptux/LogSystem.h" namespace iptux { @@ -33,70 +33,6 @@ class SessionAbstract { virtual void OnNewMessageComing() = 0; ///< 窗口打开情况下有新消息 }; -/** - * 群组信息. - */ -class DialogBase; -class GroupInfo { - public: - GroupInfo(PPalInfo pal, CPPalInfo me, LogSystem* logSystem); - GroupInfo(GroupBelongType type, - const std::vector& pals, - CPPalInfo me, - LogSystem* logSystem); - ~GroupInfo(); - - const std::vector& getMembers() const { return members; } - GroupBelongType getType() const { return type; } - - /** 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 signalUnreadMsgCountUpdated; - sigc::signal signalNewFileReceived; - - public: - GQuark grpid; ///< 唯一标识 - std::string name; ///< 群组名称 * - GtkTextBuffer* buffer; ///< 历史消息缓冲区 * - - private: - DialogBase* dialogBase; - GtkTextBuffer* inputBuffer; /// 输入缓冲 - - private: - CPPalInfo me; - std::vector members; - GroupBelongType type; ///< 群组类型 - LogSystem* logSystem; - int allMsgCount = 0; /* all received message count */ - int readMsgCount = 0; /* already read message count */ - - private: - void addMsgCount(int i); -}; - enum class TransModelColumn { STATUS, TASK, diff --git a/src/iptux/meson.build b/src/iptux/meson.build index 1c38786cc..b98c02fde 100644 --- a/src/iptux/meson.build +++ b/src/iptux/meson.build @@ -16,6 +16,8 @@ sources = files([ 'DialogPeer.cpp', 'EventAdaptor.cpp', 'GioNotificationService.cpp', + 'GroupInfo.cpp', + 'GroupInfoManager.cpp', 'LogSystem.cpp', 'MainWindow.cpp', 'RevisePal.cpp',