196 lines
6.1 KiB
C++
Executable File
196 lines
6.1 KiB
C++
Executable File
#include "maindialog.h"
|
||
|
||
#include <QApplication>
|
||
#include <QLibraryInfo>
|
||
#include <QTextCodec>
|
||
#include <QTranslator>
|
||
#include <QCommandLineParser>
|
||
#include <QtDBus>
|
||
#include <QMessageBox>
|
||
#include <QDesktopWidget>
|
||
#include <signal.h>
|
||
#include <unistd.h>
|
||
#include "qtsingleapplication/qtsingleapplication.h"
|
||
#include "../common/utils.h"
|
||
#include "xatom-helper.h"
|
||
#include "globalbackupinfo.h"
|
||
#include "messageboxutils.h"
|
||
|
||
// 声明
|
||
void initApp(QApplication& a);
|
||
bool isManager();
|
||
void centerToScreen(QWidget* widget);
|
||
|
||
int main(int argc, char *argv[])
|
||
{
|
||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
||
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
||
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
|
||
#endif
|
||
|
||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
|
||
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
|
||
#endif
|
||
|
||
QtSingleApplication a ("kybackup", argc, argv);
|
||
initApp(a);
|
||
|
||
if (a.isRunning()) {
|
||
QString strUid = QString::number(getuid());
|
||
QString ack = a.sendMessage(strUid, 3000);
|
||
|
||
if (strUid != ack) {
|
||
MessageBoxUtils::QMESSAGE_BOX_WARNING(nullptr, QObject::tr("Information"),
|
||
QObject::tr("Another user had opened kybackup, you can not start it again."),
|
||
QObject::tr("Ok"));
|
||
}
|
||
|
||
return EXIT_SUCCESS;
|
||
}
|
||
|
||
MainDialog w;
|
||
// 居中窗口
|
||
centerToScreen(&w);
|
||
a.setWindowIcon(QIcon::fromTheme(THEME_YHKYLIN_BACKUP_TOOLS));
|
||
a.setActivationWindow(&w, true);
|
||
|
||
//如果是第一个实例,则绑定,方便下次调用
|
||
QObject::connect(&a,SIGNAL(messageReceived(const QString&)),&w,SLOT(sltMessageReceived(const QString&)));
|
||
|
||
w.show();
|
||
return a.exec();
|
||
}
|
||
|
||
/**
|
||
* @brief 应用初始化
|
||
* @param a
|
||
* @note new出来的两个QTranslator不用删除,需要整个生命周期内有效,应用结束由操作系统收回即可
|
||
*/
|
||
void initApp(QApplication& a)
|
||
{
|
||
// 前端向后端传递QString参数,若参数中含有中文则保证不会乱码
|
||
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
|
||
|
||
// 区分中英文
|
||
QString locale = QLocale::system().name();
|
||
// QT自身标准的翻译
|
||
#ifndef QT_NO_TRANSLATION
|
||
QString translatorFileName = QLatin1String("qt_");
|
||
translatorFileName += locale;
|
||
QTranslator *selfTransOfQt = new QTranslator();
|
||
if (selfTransOfQt->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
|
||
a.installTranslator(selfTransOfQt);
|
||
else
|
||
qDebug() << "load qt translator file failed!";
|
||
#endif
|
||
|
||
// 应用内的翻译
|
||
QTranslator *translator = new QTranslator();
|
||
if (locale == "zh_CN") {
|
||
//中文需要翻译
|
||
if (!translator->load(":/language/qt_zh_CN.qm")) //qtcreator启动后看到在资源目录下
|
||
qDebug() << "load translator file failed!";
|
||
else
|
||
a.installTranslator(translator);
|
||
} else if (locale == "bo_CN") {
|
||
//藏文需要翻译
|
||
if (!translator->load(":/language/qt_bo_CN.qm")) //qtcreator启动后看到在资源目录下
|
||
qDebug() << "load translator file failed!";
|
||
else
|
||
a.installTranslator(translator);
|
||
}
|
||
|
||
// 部分sdk控件用到翻译文件
|
||
QTranslator *transGui = new QTranslator();
|
||
if (locale == "zh_CN")
|
||
{
|
||
if(transGui->load(":/translations/gui_zh_CN.qm"))
|
||
{
|
||
a.installTranslator(transGui);
|
||
}
|
||
} else if (locale == "bo_CN")
|
||
{
|
||
if(transGui->load(":/translations/gui_bo_CN.qm"))
|
||
{
|
||
a.installTranslator(transGui);
|
||
}
|
||
}
|
||
|
||
// 命令行参数
|
||
QCoreApplication::setApplicationName(QObject::tr("kybackup"));
|
||
QCoreApplication::setApplicationVersion("4.0.14");
|
||
|
||
QCommandLineParser parser;
|
||
parser.setApplicationDescription("kybackup helper");
|
||
parser.addHelpOption();
|
||
parser.addVersionOption();
|
||
// 启动后进入的页面
|
||
QCommandLineOption functionOption(QStringList() << "r" << "restore", QCoreApplication::translate("restore", "system restore"));
|
||
parser.addOption(functionOption);
|
||
parser.process(a);
|
||
GlobelBackupInfo::inst().setHasArgRestore(parser.isSet(functionOption));
|
||
|
||
QString qsAppPath = QCoreApplication::applicationDirPath();
|
||
Utils::initSysRootPath(qsAppPath);
|
||
Utils::initSystemInfo();
|
||
// 当前操作员是否管理员用户
|
||
GlobelBackupInfo::inst().setIsManager(isManager());
|
||
}
|
||
|
||
/**
|
||
* @brief 判断启动账号是否管理员账号
|
||
* @return true-相当于是;false-不是
|
||
*/
|
||
bool isManager()
|
||
{
|
||
uid_t uid = getuid();
|
||
QString sid = QString::number(uid);
|
||
GlobelBackupInfo::inst().setCurUserId(sid);
|
||
|
||
QString rootPath = Utils::getSysRootPath();
|
||
// 只正常启动程序时需校验是否管理员账号启动
|
||
if ("/" != rootPath)
|
||
return true;
|
||
|
||
// root用户
|
||
if (0 == uid)
|
||
return true;
|
||
|
||
QString userObject = "/org/freedesktop/Accounts/User" + sid;
|
||
|
||
// 创建QDBusInterface
|
||
QDBusInterface iface("org.freedesktop.Accounts", userObject, "org.freedesktop.DBus.Properties", QDBusConnection::systemBus());
|
||
if (!iface.isValid()) {
|
||
qDebug() << qPrintable(QDBusConnection::systemBus().lastError().message());
|
||
exit(1);
|
||
}
|
||
|
||
int result = 0;
|
||
QDBusReply<QDBusVariant> reply = iface.call("Get", QString("org.freedesktop.Accounts.User"), QString("AccountType"));
|
||
if (reply.isValid()) {
|
||
QVariant val = reply.value().variant();
|
||
result = val.toInt();
|
||
}
|
||
|
||
return 0 == result ? false : true;
|
||
}
|
||
|
||
/**
|
||
* @brief 居中显示窗口
|
||
* @param widget
|
||
*/
|
||
void centerToScreen(QWidget* widget)
|
||
{
|
||
if (!widget)
|
||
return;
|
||
QDesktopWidget* m = QApplication::desktop();
|
||
// QRect desk_rect = m->screenGeometry(m->screenNumber(QCursor::pos()));
|
||
QRect desk_rect = m->screenGeometry(widget);
|
||
int desk_x = desk_rect.width();
|
||
int desk_y = desk_rect.height();
|
||
int x = widget->width();
|
||
int y = widget->height();
|
||
widget->move(desk_x / 2 - x / 2 + desk_rect.left(), desk_y / 2 - y / 2 + desk_rect.top());
|
||
}
|
||
|