main.cpp 3.75 KB
#include "mainapp.h"
#include "mainwindow.h"
#include <Windows.h>
#include "grape/zmf.h"
#include <QTextCodec>

#include <QtDebug>
#include <QFile>
#include <QTextStream>
#include <QDateTime>

void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
//    static QMutex mutex;
//    mutex.lock();

    QString text;
    switch(type)
    {
    case QtDebugMsg:
        text = QString("Debug:");
        break;

    case QtWarningMsg:
        text = QString("Warning:");
        break;

    case QtCriticalMsg:
        text = QString("Critical:");
        break;

    case QtFatalMsg:
        text = QString("Fatal:");
    }

    QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
    QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    QString current_date = QString("(%1)").arg(current_date_time);
    QString message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_date);

    QFile file("log.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream text_stream(&file);
    text_stream << message << "\r\n";
    file.flush();
    file.close();

//    mutex.unlock();
}

MainApp::MainApp(int & argc, char ** argv) :QApplication(argc, argv)
{
}

void MainApp::notifyServiceEvent(const QString &name, size_t cookie, const Notification &info)
{
    emit this->serviceEvent(name, cookie, info);
}

void MainApp::notifyAudioEvent(int eventType, const Notification &info)
{
    emit this->audioEvent(eventType, info);
}

void MainApp::notifyVideoEvent(int eventType, const Notification &info)
{
    emit this->videoEvent(eventType, info);
}

bool MainApp::winEventFilter(MSG * msg, long * result)
{
    if (msg->message == ZmfAudioEvent)
    {
        Notification info = Notification::parse((const char *)msg->lParam);
        notifyAudioEvent(msg->wParam, info);
    }
    else if (msg->message == ZmfVideoEvent)
    {
        Notification info = Notification::parse((const char *)msg->lParam);
        notifyVideoEvent(msg->wParam, info);
    }
    else
        return false;

    *result = 0;
    return true;
}
#include "login/widgetLogon.h"
#include "justtalktest.h"

int main(int argc, char *argv[])
{

    Q_INIT_RESOURCE(feitalk);
    //设置编码
    QTextCodec *code = QTextCodec::codecForName("GBK");
    if(code == NULL)
    {
        code = QTextCodec::codecForLocale();
    }
    QTextCodec::setCodecForLocale(code);

//    qInstallMessageHandler(outputMessage);

    qRegisterMetaType<Notification>("IShowView");
    qRegisterMetaType<Notification>("Notification");
    qRegisterMetaType<Notification>("size_t");
    MainApp a(argc, argv);

    QFile qss(":qss/default.qss");
    if(qss.open(QFile::ReadOnly))
    {
        QString text= qss.readAll();
        qApp->setStyleSheet(text);
        qss.close();
    }

    MainWindow window;
    window.addIShow("~Default");
//    window.setWindowFlags(window.windowFlags() | Qt::WindowStaysOnTopHint);

    WidgetLogon *loginPage = new WidgetLogon();
    loginPage->show();
    loginPage->readConfig();
    window.hide();
    QObject::connect(loginPage, SIGNAL(loginSuccess()), &window, SLOT(show()));
    QObject::connect(&window, SIGNAL(justMessage(int)), loginPage, SLOT(loginMessage(int)));

//    JustTalkTest *t = new JustTalkTest();
//    t->show();
    QString log_name = QCoreApplication::applicationDirPath()+"/log.txt";
    QFile log_file(log_name);
//    qDebug("log_file.size() = %d",log_file.size());
    if(log_file.exists() && log_file.size() > 1024*1024*5)
    {
        log_file.remove();
    }

    int ret = a.exec();
    if (ret == 773) {
        QProcess::startDetached(qApp->applicationFilePath(), QStringList());
        return 0;
    }
    return ret;
//    return a.exec();
}