这是一个用来监控本地文件夹变化的服务
w1146869587
2021-11-08 17b4d2e75514f4e8019e015b2eb3dbc6301ea83a
watch
15个文件已添加
6个文件已修改
553 ■■■■■ 已修改文件
bak/mb_watch_user.db 补丁 | 查看 | 原始文档 | blame | 历史
bak/mbwatch.txt 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbcommfunc.cpp 18 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbcommfunc.h 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbfileutil.cpp 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbfileutil.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatch.pro 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchconst.cpp 43 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchconst.h 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchfile.cpp 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchfile.h 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchmain.cpp 56 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchmain.h 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchmgr.cpp 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchmgr.h 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchuser.cpp 61 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchuser.h 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchuserdb.cpp 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchuserdb.h 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchusermgr.cpp 69 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
mbwatch/mbwatchusermgr.h 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
bak/mb_watch_user.db
Binary files differ
bak/mbwatch.txt
@@ -1,7 +1,11 @@
CREATE TABLE TN_WATCH_USER (
    CN_S_LOGIN              VARCHAR( 128 )        NOT NULL,
    CN_S_SERVER_ID        VARCHAR( 128 )        NOT NULL,
    CN_S_BACKUP_DIR     VARCHAR( 512 )        NOT NULL,
    CN_S_SERVER_PATH   VARCHAR( 512 )        NOT NULL,
    primary key(CN_S_LOGIN,CN_S_SERVER_ID)
CREATE TABLE TN_WATCH_USER (
    CN_S_LOGIN       VARCHAR (128) NOT NULL,
    CN_S_SERVER_ID   VARCHAR (128) NOT NULL,
    CN_S_BACKUP_DIR  VARCHAR (512) NOT NULL,
    CN_S_SERVER_PATH VARCHAR (512) NOT NULL,
    CN_S_ISLOGIN     CHAR (2)      DEFAULT N,
    PRIMARY KEY (
        CN_S_LOGIN,
        CN_S_SERVER_ID
    )
);
mbwatch/mbcommfunc.cpp
New file
@@ -0,0 +1,18 @@
#include "mbcommfunc.h"
#include <QCoreApplication>
CMBCommFunc::CMBCommFunc()
{
}
CMBCommFunc::~CMBCommFunc()
{
}
QString CMBCommFunc::exePath()
{
    QString exePath = QCoreApplication::applicationDirPath();
    return exePath;
}
mbwatch/mbcommfunc.h
New file
@@ -0,0 +1,14 @@
#ifndef MBCOMMFUNC_H
#define MBCOMMFUNC_H
#include <QObject>
class CMBCommFunc
{
public:
    CMBCommFunc();
    ~CMBCommFunc();
public:
    static QString exePath();
};
#endif // MBCOMMFUNC_H
mbwatch/mbfileutil.cpp
@@ -1,6 +1,7 @@
#include "mbfileutil.h"
#include <QFileInfo>
#include <QDateTime>
#include <QDir>
CMBFileUtil::CMBFileUtil()
{
@@ -21,3 +22,11 @@
    QString strLastTime =  lastModifiedTime.toString("yyyy.MM.dd hh:mm:ss");
    return strLastTime;
}
bool CMBFileUtil::createLevelDir(QString path)
{
    QDir folder;
    return folder.mkpath(path);
}
mbwatch/mbfileutil.h
@@ -15,6 +15,7 @@
    ~CMBFileUtil();
public:
    static QString lastModifiedTime(QString fileName);
    static bool    createLevelDir(QString path);
};
#endif // MBFILEUTIL_H
mbwatch/mbwatch.pro
@@ -13,9 +13,16 @@
SOURCES += \
        main.cpp \
        mbcommfunc.cpp \
        mbfileutil.cpp \
        mbwatchapp.cpp \
        mbwatchmain.cpp
        mbwatchconst.cpp \
        mbwatchfile.cpp \
        mbwatchmain.cpp \
        mbwatchmgr.cpp \
        mbwatchuser.cpp \
        mbwatchuserdb.cpp \
        mbwatchusermgr.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
@@ -27,7 +34,14 @@
include(qtservice/qtservice.pri)
HEADERS += \
    mbcommfunc.h \
    mbfileutil.h \
    mbwatchapp.h \
    mbwatchmain.h
    mbwatchconst.h \
    mbwatchfile.h \
    mbwatchmain.h \
    mbwatchmgr.h \
    mbwatchuser.h \
    mbwatchuserdb.h \
    mbwatchusermgr.h
mbwatch/mbwatchconst.cpp
New file
@@ -0,0 +1,43 @@
#include "mbwatchconst.h"
CMBWatchConst::CMBWatchConst()
{
}
CMBWatchConst::~CMBWatchConst()
{
}
void CMBWatchConst::setExePath(QString exePath)
{
    m_exePath = exePath;
}
QString CMBWatchConst::getExePath()
{
    return m_exePath;
}
QString CMBWatchConst::getDataPath()
{
    QString path = m_exePath + "/" + "data";
    path = path.replace("//","/");
    return  path;
}
QString CMBWatchConst::getWatchPath()
{
    QString path = m_exePath + "/" + "watch";
    path = path.replace("//","/");
    return  path;
}
QString CMBWatchConst::getWatchUserDB()
{
    QString data = getDataPath();
    QString watchUser = data + "/mb_watch_user.db";
    watchUser = watchUser.replace("//","/");
    return watchUser;
}
mbwatch/mbwatchconst.h
New file
@@ -0,0 +1,22 @@
#ifndef MBWATCHCONST_H
#define MBWATCHCONST_H
#include <QObject>
class CMBWatchConst
{
public:
    CMBWatchConst();
    ~CMBWatchConst();
private:
    QString m_exePath;
public:
    void    setExePath(QString exePath);
public:
    QString getExePath();
public:
    QString getDataPath();
    QString getWatchPath();
    QString getWatchUserDB();
};
#endif // MBWATCHCONST_H
mbwatch/mbwatchfile.cpp
New file
@@ -0,0 +1,36 @@
#include "mbwatchfile.h"
#include <QDir>
CMBWatchFile::CMBWatchFile()
{
    m_pSystemWatcher = NULL;
}
CMBWatchFile::~CMBWatchFile()
{
    if( m_pSystemWatcher ){
        delete m_pSystemWatcher;
        m_pSystemWatcher = NULL;
    }
}
void CMBWatchFile::addWatchPath(QString path)
{
    if( m_pSystemWatcher == NULL ){
        m_pSystemWatcher = new QFileSystemWatcher();
        connect(m_pSystemWatcher, SIGNAL(fileChanged(QString)), this, SLOT(fileUpdated(QString)));
    }
    // 添加监控路径
    m_pSystemWatcher->addPath(path);
}
void CMBWatchFile::fileUpdated(const QString &path)
{
    emit sigFileChange(path);
}
mbwatch/mbwatchfile.h
New file
@@ -0,0 +1,23 @@
#ifndef MBWATCHFILE_H
#define MBWATCHFILE_H
#include <QObject>
#include <QFileSystemWatcher>
#include <QMap>
class CMBWatchFile : public QObject
{
    Q_OBJECT
public:
    CMBWatchFile();
    ~CMBWatchFile();
public:
    void addWatchPath(QString path);
signals:
    void  sigFileChange(QString fileName);
public slots:
    void fileUpdated(const QString &path);   // 文件被修改时调用,path是监控的路径
private:
    QFileSystemWatcher *m_pSystemWatcher;  // QFileSystemWatcher变量
};
#endif // MBWATCHFILE_H
mbwatch/mbwatchmain.cpp
@@ -1,10 +1,6 @@
#include "mbwatchmain.h"
// mb_watch_user.db
// TN_WATCH_USER
// CN_S_LOGIN       联合主键
// CN_S_SERVER_ID   联合主键
// CN_S_BACKUP_DIR
// CN_S_SERVER_PATH
#include "mbcommfunc.h"
#include <iostream>
// mb_watch_log_xxxx.db( xxx---> serverid login )
// CN_S_ID          唯一值,由 CN_S_FILE_NAME ---> md5化成
@@ -16,17 +12,39 @@
CMBWatchMain::CMBWatchMain()
{
    m_pMgr       = new CMBWatchMgr();
    m_pWatchFile = new CMBWatchFile();
    conn();
}
CMBWatchMain::~CMBWatchMain()
{
    disConn();
    if(m_pWatchFile){
        delete m_pWatchFile;
        m_pWatchFile = NULL;
    }
    if( m_pMgr ){
        delete m_pMgr;
        m_pMgr = NULL;
    }
}
void CMBWatchMain::conn()
{
     connect(m_pWatchFile,SIGNAL(sigFileChange(QString)),this,SLOT(onSigFileChange(QString)));
}
void CMBWatchMain::disConn()
{
    disconnect(m_pWatchFile);
}
void CMBWatchMain::start()
{
    init();
}
void CMBWatchMain::stop()
@@ -43,3 +61,27 @@
{
}
void CMBWatchMain::init()
{
    // 获取exe path
    QString exePath = CMBCommFunc::exePath();
    m_pMgr->getConst()->setExePath(exePath);
    // 监控userdb
    QString watchUserDB = m_pMgr->getConst()->getWatchUserDB();
    m_pWatchFile->addWatchPath(watchUserDB);
    QString errInfo;
    if(!m_pMgr->init(errInfo)){
        std::cout << errInfo.toStdString() << std::endl;
        return ;
    }
}
void CMBWatchMain::onSigFileChange(QString fileName)
{
}
mbwatch/mbwatchmain.h
@@ -1,6 +1,8 @@
#ifndef MBWATCHMAIN_H
#define MBWATCHMAIN_H
#include <QObject>
#include "mbwatchfile.h"
#include "mbwatchmgr.h"
class CMBWatchMain : public QObject
{
@@ -8,11 +10,21 @@
public:
    CMBWatchMain();
    ~CMBWatchMain();
private:
    CMBWatchMgr   *m_pMgr;
    CMBWatchFile  *m_pWatchFile;
private:
    void conn();
    void disConn();
public:
    void start();
    void stop();
    void pause();
    void resume();
private:
    void init();
public slots:
    void onSigFileChange(QString fileName);
};
#endif // MBWATCHMAIN_H
mbwatch/mbwatchmgr.cpp
New file
@@ -0,0 +1,35 @@
#include "mbwatchmgr.h"
CMBWatchMgr::CMBWatchMgr()
{
    m_pConst     = new CMBWatchConst();
    m_pUserDb    = new CMBWatchUserDb();
}
CMBWatchMgr::~CMBWatchMgr()
{
    if( m_pConst ){
        delete m_pConst;
        m_pConst = NULL;
    }
    if( m_pUserDb ){
        delete m_pUserDb;
        m_pUserDb = NULL;
    }
}
CMBWatchConst *CMBWatchMgr::getConst()
{
    return m_pConst;
}
bool CMBWatchMgr::init(QString &errInfo)
{
     QString watchUserDB = m_pConst->getWatchUserDB();
     if(!m_pUserDb->init(watchUserDB,errInfo))
         return false;
     return true;
}
mbwatch/mbwatchmgr.h
New file
@@ -0,0 +1,21 @@
#ifndef MBWATCHMGR_H
#define MBWATCHMGR_H
#include "mbwatchconst.h"
#include "mbwatchuserdb.h"
class CMBWatchMgr
{
public:
    CMBWatchMgr();
    ~CMBWatchMgr();
private:
    CMBWatchConst  *m_pConst;
private:
    CMBWatchUserDb *m_pUserDb;
public:
    CMBWatchConst *getConst();
public:
    bool           init(QString &errInfo);
};
#endif // MBWATCHMGR_H
mbwatch/mbwatchuser.cpp
New file
@@ -0,0 +1,61 @@
#include "mbwatchuser.h"
CMBWatchUser::CMBWatchUser()
{
}
CMBWatchUser::~CMBWatchUser()
{
}
void CMBWatchUser::setLogin(QString login)
{
    m_login = login;
}
void CMBWatchUser::setSvrID(QString svrID)
{
    m_svrID = svrID;
}
void CMBWatchUser::setBackupDir(QString backupDir)
{
    m_backupDir = backupDir;
}
void CMBWatchUser::setSvrPath(QString svrPath)
{
    m_svrPath = svrPath;
}
void CMBWatchUser::setIsLogin(QString isLogin)
{
    m_isLogin = isLogin;
}
QString CMBWatchUser::getLogin()
{
    return m_login;
}
QString CMBWatchUser::getSvrID()
{
    return m_svrID;
}
QString CMBWatchUser::getBackupDir()
{
    return m_backupDir;
}
QString CMBWatchUser::getSvrPath()
{
    return m_svrPath;
}
QString CMBWatchUser::getIsLogin()
{
    return m_isLogin;
}
mbwatch/mbwatchuser.h
New file
@@ -0,0 +1,41 @@
#ifndef MBWATCHUSER_H
#define MBWATCHUSER_H
#include <QObject>
class CMBWatchUser;
typedef QVector<CMBWatchUser *>   CMBWatchUserVector;
// mb_watch_user.db
// TN_WATCH_USER
// CN_S_LOGIN       联合主键
// CN_S_SERVER_ID   联合主键
// CN_S_BACKUP_DIR
// CN_S_SERVER_PATH
// CN_S_ISLOGIN
class CMBWatchUser
{
public:
    CMBWatchUser();
    ~CMBWatchUser();
private:
    QString m_login;
    QString m_svrID;
    QString m_backupDir;
    QString m_svrPath;
    QString m_isLogin;
public:
    void     setLogin(QString login);
    void     setSvrID(QString svrID);
    void     setBackupDir(QString backupDir);
    void     setSvrPath(QString svrPath);
    void     setIsLogin(QString isLogin);
public:
    QString  getLogin();
    QString  getSvrID();
    QString  getBackupDir();
    QString  getSvrPath();
    QString  getIsLogin();
};
#endif // MBWATCHUSER_H
mbwatch/mbwatchuserdb.cpp
New file
@@ -0,0 +1,22 @@
#include "mbwatchuserdb.h"
CMBWatchUserDb::CMBWatchUserDb()
{
    m_pMgr = new CMBWatchUserMgr();
}
CMBWatchUserDb::~CMBWatchUserDb()
{
    if( m_pMgr ){
        delete m_pMgr;
        m_pMgr = NULL;
    }
}
bool CMBWatchUserDb::init(QString path,QString &errInfo)
{
    m_pMgr->removeAll();
    return true;
}
mbwatch/mbwatchuserdb.h
New file
@@ -0,0 +1,17 @@
#ifndef MBWATCHUSERDB_H
#define MBWATCHUSERDB_H
#include "mbwatchusermgr.h"
#include <QObject>
class CMBWatchUserDb
{
public:
    CMBWatchUserDb();
    ~CMBWatchUserDb();
private:
    CMBWatchUserMgr *m_pMgr;
public:
    bool init(QString path,QString &errInfo);
};
#endif // MBWATCHUSERDB_H
mbwatch/mbwatchusermgr.cpp
New file
@@ -0,0 +1,69 @@
#include "mbwatchusermgr.h"
CMBWatchUserMgr::CMBWatchUserMgr()
{
}
CMBWatchUserMgr::~CMBWatchUserMgr()
{
}
bool CMBWatchUserMgr::add(CMBWatchUser *pUser)
{
    if( NULL == pUser )
        return false;
    QString login = pUser->getLogin();
    QString svrID = pUser->getSvrID();
    if(is(login, svrID))
        return false;
    m_vector.append(pUser);
    return true;
}
CMBWatchUser *CMBWatchUserMgr::get(QString login, QString svrID)
{
    login = login.trimmed();
    svrID = svrID.trimmed();
    if( login.isEmpty() || svrID.isEmpty() )
        return NULL;
    CMBWatchUserVector::iterator    it;
    CMBWatchUser *pUser = NULL;
    for( it = m_vector.begin();it != m_vector.end();it++ )
    {
        pUser = *it;
        QString tmpLogin = pUser->getLogin();
        QString tmpSvrID = pUser->getSvrID();
        tmpLogin = tmpLogin.trimmed();
        tmpSvrID = tmpSvrID.trimmed();
        if( tmpLogin == login && tmpSvrID == svrID )
            return pUser;
    }
    return NULL;
}
bool CMBWatchUserMgr::removeAll()
{
    qDeleteAll(m_vector.begin(),m_vector.end());
    m_vector.clear();
    return true;
}
bool CMBWatchUserMgr::is(QString login, QString svrID)
{
    CMBWatchUser *pUser = get(login, svrID);
    if( NULL == pUser )
        return false;
    return true;
}
mbwatch/mbwatchusermgr.h
New file
@@ -0,0 +1,19 @@
#ifndef MBWATCHUSERMGR_H
#define MBWATCHUSERMGR_H
#include "mbwatchuser.h"
class CMBWatchUserMgr
{
public:
    CMBWatchUserMgr();
    ~CMBWatchUserMgr();
private:
    CMBWatchUserVector m_vector;
public:
    bool                add(CMBWatchUser *pUser );        // 添加数据
    CMBWatchUser       *get(QString login,QString svrID);
    bool                removeAll();                    // 移除
    bool                is(QString login,QString svrID);
};
#endif // MBWATCHUSERMGR_H