kylin-connectivity/main.cpp

141 lines
4.7 KiB
C++
Raw Normal View History

2022-09-29 20:28:54 +08:00
#include <QApplication>
#include <QLibraryInfo>
#include <QTranslator>
#include <QMessageBox>
#include <QScreen>
#include <QDebug>
#include <QDir>
#include <QStandardPaths>
#include <QLocale>
#include <fcntl.h>
#include <syslog.h>
2022-09-29 20:28:54 +08:00
#include <singleapplication.h>
#include "log.hpp"
#include "windowmanage.hpp"
#include <ui/mainwindow.h>
#include "dbus/dbusvfsinterface.h"
#include "log.hpp"
#include "windowmanage.hpp"
2022-10-08 14:05:24 +08:00
const QString VERSION = "1.0.0";
2022-09-29 20:28:54 +08:00
bool initVLC()
{
QFileInfo file("/bin/vlc");
return file.isExecutable();
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(kabase::Log::logOutput);
setbuf(stdout, NULL);
// set env
#ifdef Q_OS_LINUX
qputenv("ADB_PATH", "/usr/lib/android-sdk/platform-tools/adb");
qputenv("SERVER_PATH", "/usr/share/kylin-connectivity/scrcpy-server.jar");
qputenv("CONFIG_PATH", "/usr/share/kylin-connectivity/config");
qputenv("KEYMAP_PATH", "/usr/share/kylin-connectivity/keymap");
2022-09-29 20:28:54 +08:00
#endif
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
kabase::WindowManage::setScalingProperties();
kdk::QtSingleApplication app(argc, argv);
// update version
app.setApplicationVersion(VERSION);
2023-03-22 14:42:57 +08:00
app.setWindowIcon(QIcon::fromTheme("kylin-connectivity"));
2022-09-29 20:28:54 +08:00
if (app.isRunning()) {
qDebug() << "kylin-connectivity is already running!";
2022-09-29 20:28:54 +08:00
app.sendMessage("running , 4000");
return 0;
}
// 添加单例判断
QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
int singleInstanceFd =
open(QString(homePath.at(0) + "/.config/kylin-connecitvity%1.lock").arg(getenv("DISPLAY")).toUtf8().data(),
O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (singleInstanceFd < 0) {
exit(1);
}
if (lockf(singleInstanceFd, F_TLOCK, 0)) {
syslog(LOG_ERR, "Can't lock single file, kylin-connectivity is already running!");
exit(0);
}
2022-09-29 20:28:54 +08:00
// 加载sdk控件翻译
QTranslator trans;
QString locale = QLocale::system().name();
if (locale == "zh_CN") {
if (trans.load(":/translations/gui_zh_CN.qm")) {
app.installTranslator(&trans);
}
} else if (locale == "bo_CN") {
if (trans.load(":/translations/gui_bo_CN.qm")) {
app.installTranslator(&trans);
}
}
// 加载翻译
// 加载自定义翻译文件
QString tranPath("/usr/share/kylin-connectivity/translations/");
2022-09-29 20:28:54 +08:00
QTranslator tran;
if (tran.load(QLocale(), QString("kylin-connectivity"), QString("_"), tranPath)) {
2022-09-29 20:28:54 +08:00
app.installTranslator(&tran);
} else {
qWarning() << "Waring : load translation file fail";
}
// 加载qt翻译文件 更改fileDialog英文
QString qtTransPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
QTranslator tranQt;
if (tranQt.load(QLocale(), QString("qt"), QString("_"), qtTransPath)) {
app.installTranslator(&tranQt);
} else {
qWarning() << "Waring : load qt translation file fail";
}
MainWindow::getInstance();
// 添加窗管协议
kabase::WindowManage::removeHeader(MainWindow::getInstance());
/* 移动到窗口中间 */
// QRect availableGeometry = qApp->primaryScreen()->availableGeometry();
// MainWindow::getInstance()->move(availableGeometry.width() / 2 - MainWindow::getInstance()->width() / 2,
// availableGeometry.height() / 2 - MainWindow::getInstance()->height() / 2);
2022-09-29 20:28:54 +08:00
app.setActivationWindow(MainWindow::getInstance());
/* wayland 下最小化拉起 */
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) {
QObject::connect(&app, &kdk::QtSingleApplication::messageReceived,
[=]() { kabase::WindowManage::activateWindow(MainWindow::getInstance()->getWinId()); });
}
2022-09-29 20:28:54 +08:00
MainWindow::getInstance()->show();
kabase::WindowManage::setMiddleOfScreen(MainWindow::getInstance());
2022-09-29 20:28:54 +08:00
if (!initVLC()) {
qCritical() << "vlc not installed!";
QMessageBox msgBox;
msgBox.setText(QObject::tr(
"In order to ensure the normal operation of the program, please install the VLC program first!"));
msgBox.setIcon(QMessageBox::Critical);
msgBox.exec();
MainWindow::getInstance()->close();
exit(1);
}
DbusVfsInterface vfsDbus;
QDBusConnection connection = QDBusConnection::sessionBus();
connection.registerService(KYLIN_CONNECTIVITY_SERVICE);
connection.registerObject(KYLIN_CONNECTIVITY_PATH, &vfsDbus,
2022-09-29 20:28:54 +08:00
QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals);
int ret = app.exec();
return ret;
}