[go: up one dir, main page]

File: main.cpp

package info (click to toggle)
kylin-nm 3.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,312 kB
  • sloc: cpp: 15,740; ansic: 901; makefile: 21
file content (154 lines) | stat: -rw-r--r-- 5,857 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (C) 2020 Tianjin KYLIN Information Technology Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
 *
 */

#include "mainwindow.h"
#include "ksimplenm.h"
#include "kylin-network-interface.h"
#include "wireless-security/dlghidewifi.h"
#include "dbusadaptor.h"
#include <QTranslator>
#include <QLocale>
//#include <QApplication>
#include "qt-single-application.h"
#include <QDebug>
#include <QDesktopWidget>
#include <X11/Xlib.h>
#include <QFile>

#define LOG_IDENT "ukui_kylin_nm"

void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    QByteArray currentTime = QTime::currentTime().toString().toLocal8Bit();

    QDateTime dateTime = QDateTime::currentDateTime();
    //QLocale locale = QLocale::Chinese;//指定中文显示
    QLocale locale = QLocale::English;//指定英文显示
    //QLocale locale = QLocale::Japanese;//指定日文显示
    //QString strFormat = "当前时间为:yyyy-MM-dd hh:mm:ss dddd";
    QString strFormat = "Now : yyyy-MM-dd";
    QString strDateTime = locale.toString(dateTime, strFormat);
    //qDebug() << "当前时间为 : " << strDateTime;

    bool showDebug = true;
    QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/ukui/kylin-nm.log";
    //若不需要自动创建日志文件,请放开此注释
//    if (!QFile::exists(logFilePath)) {
//        showDebug = false;
//    }
    FILE *log_file = nullptr;

    if (showDebug) {
        log_file = fopen(logFilePath.toLocal8Bit().constData(), "a+");
    }

    const char *file = context.file ? context.file : "";
    const char *function = context.function ? context.function : "";
    switch (type) {
    case QtDebugMsg:
        if (!log_file) {
            break;
        }
        fprintf(log_file, "Debug: %s %s: %s (%s:%u, %s)\n", strDateTime.toUtf8().data(), currentTime.constData(), localMsg.constData(), file, context.line, function);
        break;
    case QtInfoMsg:
        fprintf(log_file? log_file: stdout, "Info: %s %s %s: %s (%s:%u, %s)\n", strDateTime.toUtf8().data(),currentTime.constData(), localMsg.constData(), file, context.line, function);
        break;
    case QtWarningMsg:
        fprintf(log_file? log_file: stderr, "Warning: %s %s: %s (%s:%u, %s)\n", strDateTime.toUtf8().data(),currentTime.constData(), localMsg.constData(), file, context.line, function);
        break;
    case QtCriticalMsg:
        fprintf(log_file? log_file: stderr, "Critical: %s %s: %s (%s:%u, %s)\n", strDateTime.toUtf8().data(),currentTime.constData(), localMsg.constData(), file, context.line, function);
        break;
    case QtFatalMsg:
        fprintf(log_file? log_file: stderr, "Fatal: %s %s: %s (%s:%u, %s)\n", strDateTime.toUtf8().data(),currentTime.constData(), localMsg.constData(), file, context.line, function);
        break;
    }

    if (log_file)
        fclose(log_file);
}

int main(int argc, char *argv[])
{
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

    //QApplication a(argc, argv);
    QString id = QString("kylin-nm"+ QLatin1String(getenv("DISPLAY")));
    QtSingleApplication a(id, argc, argv);
    qInstallMessageHandler(messageOutput);
    if (a.isRunning()) {
        auto connection = QDBusConnection::sessionBus();
        QDBusInterface iface("com.kylin.network",
                                       "/com/kylin/network",
                                       "com.kylin.network",
                                       connection);
        iface.call("showMainWindow");
        return 0;
    }

    qDebug()<<"Kylin Network Manager Is Already Launched";

//    int loopNum = 0;
//    while (!QSystemTrayIcon::isSystemTrayAvailable()) {
//        if (loopNum == 15) return 1;
//        qDebug()<<"I couldn't detect any system tray on this system now";
//        loopNum += 1;
//        sleep(1);
//    }
    QApplication::setQuitOnLastWindowClosed(false);

    // Internationalization
    QString locale = QLocale::system().name();
    QTranslator trans_global;
    if (locale == "zh_CN") {
        trans_global.load(":/translations/kylin-nm_zh_CN.qm");
        //trans_global.load(":/translations/kylin-nm_bo.qm");
        a.installTranslator(&trans_global);
    }
    if (locale == "tr_TR") {
        trans_global.load(":/translations/kylin-nm_tr.qm");
        a.installTranslator(&trans_global);
    }
    qDebug()<<"Translations Are Already Loaded";

    MainWindow w;
    qDebug()<<"Mainwindow is Already Registered";

    DbusAdaptor adaptor(&w);
    Q_UNUSED(adaptor);

    auto connection = QDBusConnection::sessionBus();
    if (!connection.registerService("com.kylin.network") || !connection.registerObject("/com/kylin/network", &w)) {
        qCritical() << "QDbus register service failed reason:" << connection.lastError();
    }
//        QDBusInterface iface("com.kylin.network",
//                                       "/com/kylin/network",
//                                       "com.kylin.network",
//                                       connection);
//        iface.call("showMainWindow");

//        return 0;
//    }

    w.justShowTrayIcon();

    return a.exec();
}