yhkylin-backup-tools/kybackup/main.cpp

197 lines
6.6 KiB
C++
Raw Normal View History

2021-08-06 10:20:03 +08:00
#include "maindialog.h"
#include <QApplication>
2021-09-16 16:05:46 +08:00
#include <QLibraryInfo>
#include <QTextCodec>
#include <QTranslator>
#include <QCommandLineParser>
#include <QtDBus>
#include <QMessageBox>
#include <QDesktopWidget>
#include <KWindowEffects>
#include <unistd.h>
#include "qtsingleapplication/qtsingleapplication.h"
#include "../common/utils.h"
#include "xatom-helper.h"
2021-12-26 18:15:21 +08:00
#include "globalbackupinfo.h"
2021-09-16 16:05:46 +08:00
// 声明
void initApp(QApplication& a);
bool isManager();
void centerToScreen(QWidget* widget);
2021-08-06 10:20:03 +08:00
int main(int argc, char *argv[])
{
2021-09-16 16:05:46 +08:00
#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);
// 当前只支持管理员用户使用备份还原工具
2021-12-26 18:15:21 +08:00
GlobelBackupInfo::inst().setIsManager(isManager());
if (!GlobelBackupInfo::inst().isManager() && GlobelBackupInfo::inst().hasArgRestore()) {
2021-09-16 16:05:46 +08:00
QMessageBox box(QMessageBox::Warning, QObject::tr("Warning"), QObject::tr("This tool can only be used by administrator."));
box.setStandardButtons(QMessageBox::Ok);
box.setButtonText(QMessageBox::Ok, QObject::tr("OK"));
QIcon titleIcon = QIcon::fromTheme(THEME_YHKYLIN_BACKUP_TOOLS);
box.setWindowIcon(titleIcon);
box.exec();
return EXIT_FAILURE;
}
if (a.isRunning()) {
QString strUid = QString::number(getuid());
QString ack = a.sendMessage(strUid, 3000);
if (strUid != ack) {
QMessageBox box(QMessageBox::Critical, QObject::tr("Error"), QObject::tr("Another user had opened kybackup, you can not start it again."));
box.setStandardButtons(QMessageBox::Ok);
box.setButtonText(QMessageBox::Ok, QObject::tr("OK"));
QIcon titleIcon = QIcon::fromTheme(THEME_YHKYLIN_BACKUP_TOOLS);
box.setWindowIcon(titleIcon);
box.exec();
}
return EXIT_SUCCESS;
}
2021-08-06 10:20:03 +08:00
MainDialog w;
2021-09-16 16:05:46 +08:00
// 居中窗口
centerToScreen(&w);
// 窗口背景透明化setWindowOpacity可使得窗口及其上控件都透明或半透明-w.setWindowOpacity(0.7);
w.setAttribute(Qt::WA_TranslucentBackground);
// 使得窗口无边框
// w.setWindowFlag(Qt::FramelessWindowHint);
// 指示窗口管理器模糊给定窗口后面指定区域的背景(毛玻璃化背景)
2021-09-23 11:21:58 +08:00
KWindowEffects::enableBlurBehind(w.winId(), true);
2021-09-16 16:05:46 +08:00
// 添加窗管协议
MotifWmHints hints;
hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
hints.functions = MWM_FUNC_ALL;
hints.decorations = MWM_DECOR_BORDER;
XAtomHelper::getInstance()->setWindowMotifHint(w.winId(), hints);
a.setWindowIcon(QIcon::fromTheme(THEME_YHKYLIN_BACKUP_TOOLS));
a.setActivationWindow(&w, true);
//如果是第一个实例,则绑定,方便下次调用
QObject::connect(&a,SIGNAL(messageReceived(const QString&)),&w,SLOT(sltMessageReceived(const QString&)));
2021-08-06 10:20:03 +08:00
w.show();
return a.exec();
}
2021-09-16 16:05:46 +08:00
/**
* @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(":/qm/images/qt_zh_CN.qm")) //qtcreator启动后看到在资源目录下
qDebug() << "load translator file failed!";
else
a.installTranslator(translator);
}
// 命令行参数
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);
2021-12-26 18:15:21 +08:00
GlobelBackupInfo::inst().setHasArgRestore(parser.isSet(functionOption));
2021-09-16 16:05:46 +08:00
QString qsAppPath = QCoreApplication::applicationDirPath();
Utils::initSysRootPath(qsAppPath);
}
/**
* @brief
* @return true-false-
*/
bool isManager()
{
QString rootPath = Utils::getSysRootPath();
// 只正常启动程序时需校验是否管理员账号启动
if ("/" != rootPath)
return true;
uid_t uid = getuid();
// root用户
if (0 == uid)
return true;
QString sid = QString::number(uid);
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());
}