197 lines
6.4 KiB
C++
Executable File
197 lines
6.4 KiB
C++
Executable File
#include "maindialog.h"
|
||
|
||
#include <QApplication>
|
||
#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"
|
||
|
||
// 声明
|
||
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 (!isManager()) {
|
||
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;
|
||
}
|
||
|
||
MainDialog w;
|
||
// 居中窗口
|
||
centerToScreen(&w);
|
||
// 窗口背景透明化;setWindowOpacity可使得窗口及其上控件都透明或半透明-w.setWindowOpacity(0.7);
|
||
w.setAttribute(Qt::WA_TranslucentBackground);
|
||
// 使得窗口无边框
|
||
// w.setWindowFlag(Qt::FramelessWindowHint);
|
||
// 指示窗口管理器模糊给定窗口后面指定区域的背景(毛玻璃化背景)
|
||
KWindowEffects::enableBlurBehind(w.winId(), true);
|
||
|
||
// 添加窗管协议
|
||
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&)));
|
||
|
||
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(":/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);
|
||
bool isRestore = parser.isSet(functionOption);
|
||
*/
|
||
|
||
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());
|
||
}
|