317 lines
12 KiB
C++
Executable File
317 lines
12 KiB
C++
Executable File
#include "maindialog.h"
|
||
#include "ui_maindialog.h"
|
||
|
||
#include <QMenu>
|
||
#include <QProcess>
|
||
#include <QMessageBox>
|
||
#include <KWindowSystem>
|
||
#include <unistd.h>
|
||
|
||
#include "../common/mydefine.h"
|
||
#include "module/systembackup.h"
|
||
#include "module/systemrestore.h"
|
||
#include "module/databackup.h"
|
||
#include "backup_manager_interface.h"
|
||
#include "globalbackupinfo.h"
|
||
|
||
MainDialog::MainDialog(QWidget *parent)
|
||
: QMainWindow(parent)
|
||
, ui(new Ui::MainDialog)
|
||
{
|
||
ui->setupUi(this);
|
||
initUI();
|
||
initConnect();
|
||
|
||
// 注册BackupWrapper类型,之后qdbus接口才能使用
|
||
BackupWrapper::registerMetaType();
|
||
mountBackupPartition();
|
||
}
|
||
|
||
MainDialog::~MainDialog()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void MainDialog::initUI()
|
||
{
|
||
m_totalHLayout = new QHBoxLayout(ui->centralwidget);
|
||
m_totalHLayout->setSpacing(0);
|
||
m_totalHLayout->setObjectName(QString::fromUtf8("m_totalHLayout"));
|
||
m_totalHLayout->setContentsMargins(0, 0, 0, 0);
|
||
ui->centralwidget->setLayout(m_totalHLayout);
|
||
|
||
if (GlobelBackupInfo::inst().isManager())
|
||
m_leftSiderBarWidget = new LeftsiderbarWidget(ui->centralwidget);
|
||
else
|
||
m_leftSiderBarWidget = new LeftsiderbarWidget(ui->centralwidget, LeftsiderbarWidget::StartMode::std_user);
|
||
m_leftSiderBarWidget->setObjectName(QString::fromUtf8("m_leftSiderBarWidget"));
|
||
m_leftSiderBarWidget->setGeometry(QRect(0, 0, 200, 640));
|
||
m_leftSiderBarWidget->setFixedSize(QSize(200, 640));
|
||
|
||
m_totalHLayout->addWidget(m_leftSiderBarWidget);
|
||
|
||
m_rightVLayout = new QVBoxLayout();
|
||
m_rightVLayout->setObjectName(QString::fromUtf8("m_rightVLayout"));
|
||
m_rightVLayout->setContentsMargins(0, 0, 0, 0);
|
||
|
||
m_titleWidget = new QWidget(ui->centralwidget);
|
||
m_titleWidget->setObjectName(QString::fromUtf8("m_titleWidget"));
|
||
m_titleWidget->setGeometry(QRect(201, 0, 760, 40));
|
||
m_rightVLayout->addWidget(m_titleWidget);
|
||
|
||
if (!GlobelBackupInfo::inst().isManager())
|
||
selected(FuncTypeConverter::FunType::BACKUP_DATA);
|
||
else if (GlobelBackupInfo::inst().hasArgRestore())
|
||
selected(FuncTypeConverter::FunType::RESTORE_SYSTEM);
|
||
else
|
||
selected(FuncTypeConverter::FunType::BACKUP_SYSTEM);
|
||
|
||
m_totalHLayout->addLayout(m_rightVLayout);
|
||
|
||
initTileBar();
|
||
initStyleSheet();
|
||
}
|
||
|
||
void MainDialog::initTileBar()
|
||
{
|
||
m_titleLayout = new QHBoxLayout;
|
||
m_titleWidget->setLayout(m_titleLayout);
|
||
m_titleLayout->setContentsMargins(8, 4, 4, 0);
|
||
m_titleLayout->setSpacing(0);
|
||
|
||
m_menuOptionBtn = new QToolButton(m_titleWidget);
|
||
m_minBtn = new QPushButton(m_titleWidget);
|
||
m_closeBtn = new QPushButton(m_titleWidget);
|
||
|
||
m_menuOptionBtn->setToolTip(tr("Main menu"));
|
||
m_minBtn->setToolTip(tr("Minimize"));
|
||
m_closeBtn->setToolTip(tr("Close"));
|
||
|
||
m_menuOptionBtn->setFixedSize(30, 30);
|
||
m_minBtn->setFixedSize(30, 30);
|
||
m_closeBtn->setFixedSize(30, 30);
|
||
|
||
QMenu* backupMain = new QMenu(this);
|
||
backupMain->setObjectName("mainMenu");
|
||
m_menuOptionBtn->setMenu(backupMain);
|
||
|
||
QAction* backupTheme = new QAction(tr("Theme"),this);
|
||
backupMain->addAction(backupTheme);
|
||
QMenu* selectTheme = new QMenu(this);
|
||
selectTheme->setObjectName("selectTheme");
|
||
backupTheme->setMenu(selectTheme);
|
||
QAction* defaultTheme = new QAction(tr("Auto"),this);
|
||
selectTheme->addAction(defaultTheme);
|
||
QAction* darkTheme = new QAction(tr("Dark"),this);
|
||
selectTheme->addAction(darkTheme);
|
||
QAction* lightTheme = new QAction(tr("Light"),this);
|
||
selectTheme->addAction(lightTheme);
|
||
|
||
m_backupHelp = new QAction(tr("Help"),this);
|
||
backupMain->addAction(m_backupHelp);
|
||
m_backupAbout = new QAction(tr("About"),this);
|
||
backupMain->addAction(m_backupAbout);
|
||
m_backupExit = new QAction(tr("Exit"),this);
|
||
backupMain->addAction(m_backupExit);
|
||
|
||
m_titleLayout->addStretch();
|
||
m_titleLayout->addWidget(m_menuOptionBtn);
|
||
m_titleLayout->addSpacing(4);
|
||
m_titleLayout->addWidget(m_minBtn);
|
||
m_titleLayout->addSpacing(4);
|
||
m_titleLayout->addWidget(m_closeBtn);
|
||
}
|
||
|
||
void MainDialog::initStyleSheet()
|
||
{
|
||
// 主窗口
|
||
// qApp->setWindowIcon(QIcon::fromTheme(THEME_YHKYLIN_BACKUP_TOOLS));
|
||
this->setWindowTitle(tr("Backup & Restore"));
|
||
|
||
// 工作区(此处代码加不加一个样,注释掉,留着学习,知道有这么回事)
|
||
//ui->centralwidget->setStyleSheet("QWidget#centralWidget{background: palette(base); border-radius: 6px;}");
|
||
//ui->centralwidget->setAttribute(Qt::WA_TranslucentBackground);
|
||
|
||
// 右侧title
|
||
// setStyleSheet方式設置樣式不能跟隨主題
|
||
// m_titleWidget->setStyleSheet("QWidget#m_titleWidget{background-color:palette(base)}");
|
||
m_titleWidget->setAutoFillBackground(true);
|
||
QPalette palette = m_titleWidget->palette();
|
||
palette.setColor(QPalette::Background, palette.color(QPalette::Base));
|
||
m_titleWidget->setPalette(palette);
|
||
|
||
// m_menuOptionBtn->setStyleSheet("background-color: palette(base);");
|
||
m_menuOptionBtn->setProperty("isWindowButton", 0x1);
|
||
m_menuOptionBtn->setProperty("useIconHighlightEffect", 0x2);
|
||
m_menuOptionBtn->setIcon(QIcon::fromTheme("open-menu-symbolic"));
|
||
m_menuOptionBtn->setAutoRaise(true);
|
||
m_menuOptionBtn->setPopupMode(QToolButton::InstantPopup);
|
||
// m_menuOptionBtn->setProperty("setIconHighlightEffectDefaultColor", m_closeBtn->palette().color(QPalette::Active, QPalette::Base));
|
||
|
||
m_minBtn->setProperty("isWindowButton", 0x1);
|
||
m_minBtn->setProperty("useIconHighlightEffect", 0x2);
|
||
m_minBtn->setFlat(true);
|
||
m_minBtn->setIcon(QIcon::fromTheme("window-minimize-symbolic"));
|
||
// m_minBtn->setProperty("setIconHighlightEffectDefaultColor", m_closeBtn->palette().color(QPalette::Active, QPalette::Base));
|
||
|
||
m_closeBtn->setProperty("isWindowButton", 0x2);
|
||
m_closeBtn->setProperty("useIconHighlightEffect", 0x8);
|
||
m_closeBtn->setFlat(true);
|
||
m_closeBtn->setIcon(QIcon::fromTheme("window-close-symbolic"));
|
||
// m_closeBtn->setProperty("setIconHighlightEffectDefaultColor", m_closeBtn->palette().color(QPalette::Active, QPalette::Base));
|
||
|
||
}
|
||
|
||
void MainDialog::initConnect()
|
||
{
|
||
// 左侧功能选择栏
|
||
connect(m_leftSiderBarWidget, &LeftsiderbarWidget::selected, this, &MainDialog::selected);
|
||
|
||
// 标题栏右侧按钮区域
|
||
// 退出
|
||
connect(m_backupExit, &QAction::triggered, this, &MainDialog::closeBtn);
|
||
|
||
// 关于
|
||
connect(m_backupAbout, &QAction::triggered, this, [=] {
|
||
// UkccAbout *ukcc = new UkccAbout(this);
|
||
// ukcc->exec();
|
||
});
|
||
|
||
connect(m_backupHelp, &QAction::triggered, this, [=] {
|
||
QProcess process(this);
|
||
process.startDetached("kylin-user-guide -A kybackup");
|
||
});
|
||
|
||
//最小化按钮
|
||
connect(m_minBtn, &QPushButton::clicked, this, &MainDialog::showMinimized);
|
||
|
||
//关闭按钮
|
||
connect(m_closeBtn, &QPushButton::clicked, this, &MainDialog::closeBtn);
|
||
}
|
||
|
||
/**
|
||
* @brief 左侧功能选择栏响应槽
|
||
* @param func_type,功能类型
|
||
*/
|
||
void MainDialog::selected(int func_type)
|
||
{
|
||
if (GlobelBackupInfo::inst().getFuncType() == func_type)
|
||
return ;
|
||
|
||
if (m_stackedWidget) {
|
||
m_rightVLayout->removeWidget(m_stackedWidget);
|
||
delete m_stackedWidget;
|
||
}
|
||
|
||
switch (func_type) {
|
||
case FuncTypeConverter::FunType::BACKUP_SYSTEM:
|
||
m_stackedWidget = new SystemBackup(ui->centralwidget);
|
||
GlobelBackupInfo::inst().setFuncType(FuncTypeConverter::FunType::BACKUP_SYSTEM);
|
||
break;
|
||
case FuncTypeConverter::FunType::RESTORE_SYSTEM:
|
||
m_stackedWidget = new SystemRestore(ui->centralwidget);
|
||
GlobelBackupInfo::inst().setFuncType(FuncTypeConverter::FunType::RESTORE_SYSTEM);
|
||
break;
|
||
case FuncTypeConverter::FunType::BACKUP_DATA:
|
||
m_stackedWidget = new DataBackup(ui->centralwidget);
|
||
GlobelBackupInfo::inst().setFuncType(FuncTypeConverter::FunType::BACKUP_DATA);
|
||
break;
|
||
default:
|
||
m_stackedWidget = new SystemBackup(ui->centralwidget);
|
||
GlobelBackupInfo::inst().setFuncType(FuncTypeConverter::FunType::BACKUP_SYSTEM);
|
||
break;
|
||
}
|
||
|
||
m_stackedWidget->setObjectName(QString::fromUtf8("m_stackedWidget"));
|
||
m_stackedWidget->setGeometry(QRect(201, 40, 760, 600));
|
||
m_rightVLayout->addWidget(m_stackedWidget);
|
||
|
||
// 右侧内容区域
|
||
// m_stackedWidget->setStyleSheet("QStackedWidget#m_stackedWidget{background: palette(base); border-bottom-left-radius: 6px; border-bottom-right-radius: 6px;}");
|
||
m_stackedWidget->setAutoFillBackground(true);
|
||
QPalette palette = m_stackedWidget->palette();
|
||
palette.setColor(QPalette::Background, palette.color(QPalette::Base));
|
||
m_stackedWidget->setPalette(palette);
|
||
}
|
||
|
||
/**
|
||
* @brief 多例启动信息槽
|
||
* @param msg 信息,里面存放新启动实例的账户id
|
||
*/
|
||
void MainDialog::sltMessageReceived(const QString &msg)
|
||
{
|
||
QString user = QString::number(getuid());
|
||
if (msg == user) {
|
||
// this->setWindowFlag(Qt::WindowStaysOnTopHint,true);
|
||
// this->setWindowFlag(Qt::WindowStaysOnTopHint,false);
|
||
KWindowSystem::forceActiveWindow(this->winId());
|
||
this->show();
|
||
}
|
||
}
|
||
|
||
void MainDialog::mountBackupPartition()
|
||
{
|
||
// 挂载/backup分区
|
||
ComKylinBackupManagerInterface intf("com.kylin.backup", "/", QDBusConnection::systemBus());
|
||
QDBusPendingReply<int> reply = intf.Mount_backup_partition();
|
||
reply.waitForFinished();
|
||
if (reply.isError()) {
|
||
// 挂载分区时出现异常
|
||
QMessageBox box(QMessageBox::Critical, QObject::tr("Error"), QObject::tr("An exception occurred when mounting backup partition."), QMessageBox::Ok, this);
|
||
// box.setStandardButtons(QMessageBox::Ok);
|
||
box.setButtonText(QMessageBox::Ok, QObject::tr("OK"));
|
||
QIcon titleIcon = QIcon::fromTheme(THEME_YHKYLIN_BACKUP_TOOLS);
|
||
box.setWindowIcon(titleIcon);
|
||
box.exec();
|
||
|
||
closeBtn();
|
||
} else if (int(MountResult::CANNOT_GET_BACKUPUUID) == reply.value()) {
|
||
// 没有找到备份分区,只能备份到移动设备中
|
||
QMessageBox box(QMessageBox::Critical, QObject::tr("Error"), QObject::tr("There's no backup partition, can only backup to removable device."), QMessageBox::Ok, this);
|
||
box.setButtonText(QMessageBox::Ok, QObject::tr("OK"));
|
||
QIcon titleIcon = QIcon::fromTheme(THEME_YHKYLIN_BACKUP_TOOLS);
|
||
box.setWindowIcon(titleIcon);
|
||
box.exec();
|
||
|
||
GlobelBackupInfo::inst().setHasBackupPartition(false);
|
||
} else if (int(MountResult::MOUNTED) != reply.value()) {
|
||
// 挂载备份分区失败
|
||
QMessageBox box(QMessageBox::Critical, QObject::tr("Error"), QObject::tr("Failed to mount backup partition."), QMessageBox::Ok, this);
|
||
// box.setStandardButtons(QMessageBox::Ok);
|
||
box.setButtonText(QMessageBox::Ok, QObject::tr("OK"));
|
||
QIcon titleIcon = QIcon::fromTheme(THEME_YHKYLIN_BACKUP_TOOLS);
|
||
box.setWindowIcon(titleIcon);
|
||
box.exec();
|
||
|
||
closeBtn();
|
||
}
|
||
}
|
||
|
||
void MainDialog::umountBackupPartition()
|
||
{
|
||
// 卸载/backup分区
|
||
ComKylinBackupManagerInterface intf("com.kylin.backup", "/", QDBusConnection::systemBus());
|
||
intf.umountBackupPartition();
|
||
}
|
||
|
||
void MainDialog::closeBtn()
|
||
{
|
||
if (GlobelBackupInfo::inst().isBusy()) {
|
||
QMessageBox box(QMessageBox::Critical, QObject::tr("Error"), QObject::tr("It's busy, please wait"), QMessageBox::Ok, this);
|
||
// 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;
|
||
}
|
||
|
||
if (GlobelBackupInfo::inst().hasBackupPartition())
|
||
umountBackupPartition();
|
||
|
||
this->close();
|
||
qApp->quit();
|
||
}
|
||
|