@@ -57,7 +57,8 @@ SOURCES += main.cpp\ | |||||
sendtextedit.cpp \ | sendtextedit.cpp \ | ||||
feiqwin.cpp \ | feiqwin.cpp \ | ||||
plugin/unreadchecker.cpp \ | plugin/unreadchecker.cpp \ | ||||
plugin/iplugin.cpp | |||||
plugin/iplugin.cpp \ | |||||
plugin/rankuser.cpp | |||||
HEADERS += mainwindow.h \ | HEADERS += mainwindow.h \ | ||||
@@ -94,7 +95,8 @@ HEADERS += mainwindow.h \ | |||||
sendtextedit.h \ | sendtextedit.h \ | ||||
plugin/iplugin.h \ | plugin/iplugin.h \ | ||||
feiqwin.h \ | feiqwin.h \ | ||||
plugin/unreadchecker.h | |||||
plugin/unreadchecker.h \ | |||||
plugin/rankuser.h | |||||
FORMS += mainwindow.ui \ | FORMS += mainwindow.ui \ | ||||
searchfellowdlg.ui \ | searchfellowdlg.ui \ | ||||
@@ -9,7 +9,7 @@ UniqueId::UniqueId() | |||||
IdType UniqueId::get() | IdType UniqueId::get() | ||||
{ | { | ||||
auto id = ++mId; | auto id = ++mId; | ||||
if (id >= INT_MAX || id < 0) | |||||
if (id >= ULONG_LONG_MAX) | |||||
mId=1; | mId=1; | ||||
return mId; | return mId; | ||||
@@ -46,7 +46,10 @@ void FeiqWin::init(MainWindow *mainWin) | |||||
loadPlugins(); | loadPlugins(); | ||||
for (auto plugin : mPlugins) | for (auto plugin : mPlugins) | ||||
plugin->init(this); | |||||
{ | |||||
plugin->setFeiqWin(this); | |||||
plugin->init(); | |||||
} | |||||
} | } | ||||
void FeiqWin::unInit() | void FeiqWin::unInit() | ||||
@@ -17,8 +17,9 @@ void FellowListWidget::update(const Fellow &fellow) | |||||
auto item = findFirstItem(fellow); | auto item = findFirstItem(fellow); | ||||
if (item == nullptr) | if (item == nullptr) | ||||
{ | { | ||||
mWidget->addItem(fellowText(fellow)); | |||||
item = mWidget->item(mWidget->count()-1); | |||||
int row = requestRow(fellow); | |||||
mWidget->insertItem(row, fellowText(fellow)); | |||||
item = mWidget->item(row); | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
@@ -72,12 +73,17 @@ void FellowListWidget::mark(const Fellow &fellow, const QString &info) | |||||
} | } | ||||
} | } | ||||
void FellowListWidget::setRankPredict(FellowListWidget::RankPredict predict) | |||||
{ | |||||
mRankPredict = predict; | |||||
} | |||||
void FellowListWidget::itemChosen(QListWidgetItem *item) | void FellowListWidget::itemChosen(QListWidgetItem *item) | ||||
{ | { | ||||
if (item == nullptr) | if (item == nullptr) | ||||
return; | return; | ||||
auto fellow = static_cast<const Fellow*>(item->data(Qt::UserRole).value<void*>()); | |||||
auto fellow = getFellow(item); | |||||
emit select(fellow); | emit select(fellow); | ||||
} | } | ||||
@@ -97,10 +103,34 @@ QListWidgetItem *FellowListWidget::findFirstItem(const Fellow &fellow) | |||||
for (int i = 0; i < count; i++) | for (int i = 0; i < count; i++) | ||||
{ | { | ||||
auto widget = mWidget->item(i); | auto widget = mWidget->item(i); | ||||
auto f = static_cast<const Fellow*>(widget->data(Qt::UserRole).value<void*>()); | |||||
auto f = getFellow(widget); | |||||
if (f->getIp() == fellow.getIp()) | if (f->getIp() == fellow.getIp()) | ||||
return widget; | return widget; | ||||
} | } | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
int FellowListWidget::requestRow(const Fellow &fellow) | |||||
{ | |||||
auto count = mWidget->count(); | |||||
if (!mRankPredict) | |||||
return count; | |||||
int row = count; | |||||
for (; row > 0; row--) | |||||
{ | |||||
auto f = getFellow(mWidget->item(row-1)); | |||||
auto ret = mRankPredict(*f, fellow); | |||||
if (ret >= 0) | |||||
break; | |||||
} | |||||
return row; | |||||
} | |||||
const Fellow *FellowListWidget::getFellow(const QListWidgetItem *item) | |||||
{ | |||||
return static_cast<const Fellow*>(item->data(Qt::UserRole).value<void*>()); | |||||
} |
@@ -11,6 +11,8 @@ class FellowListWidget : public QObject | |||||
Q_OBJECT | Q_OBJECT | ||||
public: | public: | ||||
typedef std::function<int (const Fellow&, const Fellow&)> RankPredict; | |||||
FellowListWidget(); | FellowListWidget(); | ||||
void bindTo(QListWidget* widget); | void bindTo(QListWidget* widget); | ||||
@@ -19,6 +21,7 @@ public: | |||||
void top(const Fellow& fellow); | void top(const Fellow& fellow); | ||||
void topSecond(const Fellow& fellow); | void topSecond(const Fellow& fellow); | ||||
void mark(const Fellow& fellow, const QString &info); | void mark(const Fellow& fellow, const QString &info); | ||||
void setRankPredict(RankPredict predict); | |||||
signals: | signals: | ||||
void select(const Fellow* fellow); | void select(const Fellow* fellow); | ||||
@@ -29,8 +32,11 @@ private slots: | |||||
private: | private: | ||||
QString fellowText(const Fellow& fellow); | QString fellowText(const Fellow& fellow); | ||||
QListWidgetItem* findFirstItem(const Fellow& fellow); | QListWidgetItem* findFirstItem(const Fellow& fellow); | ||||
int requestRow(const Fellow& fellow); | |||||
const Fellow* getFellow(const QListWidgetItem* item); | |||||
private: | private: | ||||
RankPredict mRankPredict; | |||||
QListWidget* mWidget; | QListWidget* mWidget; | ||||
}; | }; | ||||
@@ -170,7 +170,7 @@ void MainWindow::handleFeiqViewEvent(shared_ptr<ViewEvent> event) | |||||
} | } | ||||
else if (event->what == ViewEventType::SEND_TIMEO || event->what == ViewEventType::MESSAGE) | else if (event->what == ViewEventType::SEND_TIMEO || event->what == ViewEventType::MESSAGE) | ||||
{ | { | ||||
//地球人都知道这个分支中的ViewEvent集成自FellowViewEvent | |||||
//地球人都知道这个分支中的ViewEvent继承自FellowViewEvent | |||||
auto e = static_cast<FellowViewEvent*>(event.get()); | auto e = static_cast<FellowViewEvent*>(event.get()); | ||||
auto fellow = e->fellow.get(); | auto fellow = e->fellow.get(); | ||||
@@ -5,11 +5,20 @@ IPlugin::~IPlugin() | |||||
} | } | ||||
void IPlugin::init(FeiqWin *feiqWin) | |||||
void IPlugin::setFeiqWin(FeiqWin *feiqWin) | |||||
{ | { | ||||
mFeiq = feiqWin; | mFeiq = feiqWin; | ||||
} | } | ||||
void IPlugin::init() | |||||
{ | |||||
} | |||||
void IPlugin::unInit() | |||||
{ | |||||
} | |||||
PluginManager::PluginManager() | PluginManager::PluginManager() | ||||
{ | { | ||||
@@ -9,9 +9,9 @@ class IPlugin | |||||
{ | { | ||||
public: | public: | ||||
virtual ~IPlugin(); | virtual ~IPlugin(); | ||||
virtual void init(FeiqWin* feiqWin); | |||||
virtual void unInit() = 0; | |||||
void setFeiqWin(FeiqWin* feiqWin); | |||||
virtual void init(); | |||||
virtual void unInit(); | |||||
protected: | protected: | ||||
FeiqWin* mFeiq; | FeiqWin* mFeiq; | ||||
@@ -0,0 +1,47 @@ | |||||
#include "rankuser.h" | |||||
#define PLUGIN_NAME "rank_user" | |||||
REGISTER_PLUGIN(PLUGIN_NAME, RankUser) | |||||
RankUser::RankUser() | |||||
{ | |||||
} | |||||
void RankUser::init() | |||||
{ | |||||
IPlugin::init(); | |||||
connect(mFeiq->fellowListWidget(), SIGNAL(select(const Fellow*)), this, SLOT(onTalkTo(const Fellow*))); | |||||
mFeiq->fellowListWidget()->setRankPredict(std::bind(&RankUser::compare, this, placeholders::_1, placeholders::_2)); | |||||
} | |||||
void RankUser::unInit() | |||||
{ | |||||
IPlugin::unInit(); | |||||
mFeiq->settings()->sync(); | |||||
} | |||||
void RankUser::onTalkTo(const Fellow *fellow) | |||||
{ | |||||
mFeiq->settings()->setValue(fellowKey(*fellow), weightOfFellow(*fellow)+1); | |||||
} | |||||
int RankUser::compare(const Fellow &f1, const Fellow &f2) | |||||
{ | |||||
return weightOfFellow(f1)-weightOfFellow(f2); | |||||
} | |||||
QString RankUser::fellowId(const Fellow &f) | |||||
{ | |||||
QString ip(f.getIp().c_str()); | |||||
return ip; | |||||
} | |||||
QString RankUser::fellowKey(const Fellow &f) | |||||
{ | |||||
return QString(PLUGIN_NAME)+"/"+fellowId(f); | |||||
} | |||||
int RankUser::weightOfFellow(const Fellow &f) | |||||
{ | |||||
return mFeiq->settings()->value(fellowKey(f), "0").toInt(); | |||||
} |
@@ -0,0 +1,28 @@ | |||||
#ifndef RANKUSER_H | |||||
#define RANKUSER_H | |||||
#include "iplugin.h" | |||||
#include <QObject> | |||||
#include "feiqlib/fellow.h" | |||||
class RankUser : public QObject, public IPlugin | |||||
{ | |||||
Q_OBJECT | |||||
public: | |||||
RankUser(); | |||||
void init() override; | |||||
void unInit() override; | |||||
private slots: | |||||
void onTalkTo(const Fellow* fellow); | |||||
private: | |||||
int compare(const Fellow& f1, const Fellow& f2); | |||||
QString fellowId(const Fellow& f); | |||||
QString fellowKey(const Fellow& f); | |||||
int weightOfFellow(const Fellow& f); | |||||
}; | |||||
#endif // RANKUSER_H |
@@ -19,9 +19,9 @@ void UnreadChecker::timerEvent(QTimerEvent *event) | |||||
} | } | ||||
} | } | ||||
void UnreadChecker::init(FeiqWin *feiqWin) | |||||
void UnreadChecker::init() | |||||
{ | { | ||||
IPlugin::init(feiqWin); | |||||
IPlugin::init(); | |||||
auto settings = mFeiq->settings(); | auto settings = mFeiq->settings(); | ||||
mUnreadTimerInterval = settings->value(PLUGIN_NAME"/timer", "0").toInt(); | mUnreadTimerInterval = settings->value(PLUGIN_NAME"/timer", "0").toInt(); | ||||
@@ -10,7 +10,7 @@ class UnreadChecker : public QObject, public IPlugin | |||||
public: | public: | ||||
UnreadChecker(); | UnreadChecker(); | ||||
void init(FeiqWin* feiqWin) override; | |||||
void init() override; | |||||
void unInit() override; | void unInit() override; | ||||
protected: | protected: | ||||