Browse Source

增加插件系统

tags/v1.0
chenyong 8 years ago
parent
commit
6c3933ccf4
9 changed files with 212 additions and 30 deletions
  1. +7
    -2
      feiq.pro
  2. +65
    -0
      feiqwin.cpp
  3. +43
    -0
      feiqwin.h
  4. +5
    -10
      main.cpp
  5. +7
    -14
      mainwindow.cpp
  6. +6
    -4
      mainwindow.h
  7. +21
    -0
      plugin/iplugin.h
  8. +34
    -0
      plugin/unreadchecker.cpp
  9. +24
    -0
      plugin/unreadchecker.h

+ 7
- 2
feiq.pro View File

@@ -54,7 +54,9 @@ SOURCES += main.cpp\
chooseemojidlg.cpp \ chooseemojidlg.cpp \
platformdepend.cpp \ platformdepend.cpp \
chooseemojiwidget.cpp \ chooseemojiwidget.cpp \
sendtextedit.cpp
sendtextedit.cpp \
feiqwin.cpp \
plugin/unreadchecker.cpp




HEADERS += mainwindow.h \ HEADERS += mainwindow.h \
@@ -88,7 +90,10 @@ HEADERS += mainwindow.h \
chooseemojidlg.h \ chooseemojidlg.h \
platformdepend.h \ platformdepend.h \
chooseemojiwidget.h \ chooseemojiwidget.h \
sendtextedit.h
sendtextedit.h \
plugin/iplugin.h \
feiqwin.h \
plugin/unreadchecker.h


FORMS += mainwindow.ui \ FORMS += mainwindow.ui \
searchfellowdlg.ui \ searchfellowdlg.ui \


+ 65
- 0
feiqwin.cpp View File

@@ -0,0 +1,65 @@
#include "feiqwin.h"
#include "mainwindow.h"
#include "plugin/iplugin.h"

#include "plugin/unreadchecker.h"

FeiqWin::FeiqWin()
{

}

RecvTextEdit *FeiqWin::recvTextEdit()
{
return mMainWin->mRecvTextEdit;
}

SendTextEdit *FeiqWin::sendTextEdit()
{
return mMainWin->mSendTextEdit;
}

FellowListWidget *FeiqWin::fellowListWidget()
{
return &(mMainWin->mFellowList);
}

const FeiqModel *FeiqWin::feiqModel()
{
return &(mMainWin->mFeiq.getModel());
}

QSettings *FeiqWin::settings()
{
return mMainWin->mSettings;
}

int FeiqWin::getUnreadCount()
{
return mMainWin->getUnreadCount();
}

void FeiqWin::init(MainWindow *mainWin)
{
mMainWin = mainWin;

loadPlugins();

for (auto plugin : mPlugins)
plugin->init(this);
}

void FeiqWin::unInit()
{
for (auto plugin : mPlugins)
{
plugin->unInit();
delete plugin;
}
mPlugins.clear();
}

void FeiqWin::loadPlugins()
{
mPlugins.append(new UnreadChecker());
}

+ 43
- 0
feiqwin.h View File

@@ -0,0 +1,43 @@
#ifndef FEIQWIN_H
#define FEIQWIN_H

#include "recvtextedit.h"
#include "sendtextedit.h"
#include "qsettings.h"
#include "fellowlistwidget.h"
#include "feiqlib/feiqmodel.h"

class MainWindow;
class IPlugin;

/**
* @brief The FeiqWin class
* 其实就是MainWindow的代言人,用来隔离插件和MainWindow,方便控制MainWindow对插件的可见性
*/
class FeiqWin
{
public:
FeiqWin();

public:
RecvTextEdit* recvTextEdit();
SendTextEdit* sendTextEdit();
FellowListWidget* fellowListWidget();
const FeiqModel* feiqModel();
QSettings* settings();

public:
int getUnreadCount();

private:
void init(MainWindow* mainWin);
void unInit();
void loadPlugins();

private:
friend class MainWindow;
MainWindow* mMainWin;
QList<IPlugin*> mPlugins;
};

#endif // FEIQWIN_H

+ 5
- 10
main.cpp View File

@@ -1,21 +1,16 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication> #include <QApplication>
#include <QWindow>
#include "feiqwin.h"


//待测:
//*无当前交谈用户,且有未读消息时,未读消息能否正确置顶?

//应用图标
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
a.setWindowIcon(QIcon(":/default/res/icon.png")); a.setWindowIcon(QIcon(":/default/res/icon.png"));

MainWindow w; MainWindow w;
FeiqWin feiqWin;
w.setFeiqWin(&feiqWin);

w.show(); w.show();
return a.exec(); return a.exec();
} }

//表情->html
//查通讯录功能
//查ip占用功能
//美化用户列表

+ 7
- 14
mainwindow.cpp View File

@@ -11,6 +11,7 @@
#include "addfellowdialog.h" #include "addfellowdialog.h"
#include <QProcess> #include <QProcess>
#include "platformdepend.h" #include "platformdepend.h"
#include "feiqwin.h"


MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), QMainWindow(parent),
@@ -68,10 +69,6 @@ MainWindow::MainWindow(QWidget *parent) :
mTitle = mSettings->value("app/title", "mac飞秋").toString(); mTitle = mSettings->value("app/title", "mac飞秋").toString();
setWindowTitle(mTitle); setWindowTitle(mTitle);


mUnreadTimerInterval = mSettings->value("app/unread_timer", "0").toInt();
if (mUnreadTimerInterval > 0)
mUnreadTimerId = startTimer(mUnreadTimerInterval*1000, Qt::VeryCoarseTimer);

//初始化飞秋引擎 //初始化飞秋引擎
connect(this, SIGNAL(feiqViewEvent(shared_ptr<ViewEvent>)), this, SLOT(handleFeiqViewEvent(shared_ptr<ViewEvent>))); connect(this, SIGNAL(feiqViewEvent(shared_ptr<ViewEvent>)), this, SLOT(handleFeiqViewEvent(shared_ptr<ViewEvent>)));


@@ -90,6 +87,12 @@ MainWindow::~MainWindow()
delete ui; delete ui;
} }


void MainWindow::setFeiqWin(FeiqWin *feiqWin)
{
mFeiqWin = feiqWin;
mFeiqWin->init(this);
}

void MainWindow::enterEvent(QEvent *event) void MainWindow::enterEvent(QEvent *event)
{ {
auto fellow = mRecvTextEdit->curFellow(); auto fellow = mRecvTextEdit->curFellow();
@@ -102,16 +105,6 @@ void MainWindow::enterEvent(QEvent *event)
PlatformDepend::instance().hideAllNotify(); PlatformDepend::instance().hideAllNotify();
} }


void MainWindow::timerEvent(QTimerEvent *event)
{
if (event->timerId() == mUnreadTimerId)
{
auto count = getUnreadCount();
if (count > 0)
PlatformDepend::instance().showNotify("未读提醒", QString("还有%1条未读消息").arg(count));
}
}

void MainWindow::openChartTo(const Fellow *fellow) void MainWindow::openChartTo(const Fellow *fellow)
{ {
mFellowList.top(*fellow); mFellowList.top(*fellow);


+ 6
- 4
mainwindow.h View File

@@ -19,18 +19,22 @@ namespace Ui {
class MainWindow; class MainWindow;
} }


class FeiqWin;

Q_DECLARE_METATYPE(shared_ptr<ViewEvent>) Q_DECLARE_METATYPE(shared_ptr<ViewEvent>)
class MainWindow : public QMainWindow, IFeiqView class MainWindow : public QMainWindow, IFeiqView
{ {
Q_OBJECT Q_OBJECT


friend class FeiqWin;
public: public:
explicit MainWindow(QWidget *parent = 0); explicit MainWindow(QWidget *parent = 0);
~MainWindow(); ~MainWindow();


void setFeiqWin(FeiqWin* feiqWin);

protected: protected:
void enterEvent(QEvent *event); void enterEvent(QEvent *event);
void timerEvent(QTimerEvent *event);


signals: signals:
void showErrorAndQuit(const QString& text); void showErrorAndQuit(const QString& text);
@@ -92,9 +96,7 @@ private:
SendTextEdit* mSendTextEdit; SendTextEdit* mSendTextEdit;
QString mTitle; QString mTitle;
unordered_map<const Fellow*, list<shared_ptr<ViewEvent>>> mUnreadEvents; unordered_map<const Fellow*, list<shared_ptr<ViewEvent>>> mUnreadEvents;

int mUnreadTimerInterval;
int mUnreadTimerId;
FeiqWin* mFeiqWin = nullptr;
}; };


#endif // MAINWINDOW_H #endif // MAINWINDOW_H

+ 21
- 0
plugin/iplugin.h View File

@@ -0,0 +1,21 @@
#ifndef IPLUGIN_H
#define IPLUGIN_H

#include "../feiqwin.h"

class IPlugin
{
public:
virtual ~IPlugin(){}
virtual void init(FeiqWin* feiqWin)
{
mFeiq = feiqWin;
}

virtual void unInit() = 0;

protected:
FeiqWin* mFeiq;
};

#endif // IPLUGIN_H

+ 34
- 0
plugin/unreadchecker.cpp View File

@@ -0,0 +1,34 @@
#include "unreadchecker.h"
#include <QTimerEvent>
#include "platformdepend.h"

UnreadChecker::UnreadChecker()
{

}

void UnreadChecker::timerEvent(QTimerEvent *event)
{
if (event->timerId() == mUnreadTimerId)
{
auto count = mFeiq->getUnreadCount();
if (count > 0)
PlatformDepend::instance().showNotify("未读提醒", QString("还有%1条未读消息").arg(count));
}
}

void UnreadChecker::init(FeiqWin *feiqWin)
{
IPlugin::init(feiqWin);

auto settings = mFeiq->settings();
mUnreadTimerInterval = settings->value("app/unread_timer", "0").toInt();
if (mUnreadTimerInterval > 0)
mUnreadTimerId = startTimer(mUnreadTimerInterval*1000, Qt::VeryCoarseTimer);
}

void UnreadChecker::unInit()
{
if (mUnreadTimerId > 0)
killTimer(mUnreadTimerId);
}

+ 24
- 0
plugin/unreadchecker.h View File

@@ -0,0 +1,24 @@
#ifndef UNREADCHECKER_H
#define UNREADCHECKER_H

#include "iplugin.h"
#include <QObject>

class UnreadChecker : public QObject, public IPlugin
{
Q_OBJECT

public:
UnreadChecker();
void init(FeiqWin* feiqWin) override;
void unInit() override;

protected:
void timerEvent(QTimerEvent *event) override;

private:
int mUnreadTimerInterval;
int mUnreadTimerId=-1;
};

#endif // UNREADCHECKER_H

Loading…
Cancel
Save