yhkylin-backup-tools/kybackup/module/systembackup.cpp

955 lines
35 KiB
C++
Raw Normal View History

2021-09-16 16:05:46 +08:00
#include "systembackup.h"
#include <QLabel>
#include <QPushButton>
2021-10-13 16:31:58 +08:00
#include <QComboBox>
#include <QMovie>
2021-10-26 09:24:05 +08:00
#include <unistd.h>
2021-09-16 16:05:46 +08:00
2021-10-13 16:31:58 +08:00
#include "../component/clicklabel.h"
#include "../component/circlelabel.h"
#include "../component/myiconlabel.h"
#include "../component/mylabel.h"
2021-10-26 09:24:05 +08:00
#include "../component/mylineedit.h"
#include "../component/mypushbutton.h"
2021-10-13 16:31:58 +08:00
#include "../component/linelabel.h"
2021-10-26 09:24:05 +08:00
#include "../component/ringsprogressbar.h"
2021-10-13 16:31:58 +08:00
#include "../../common/utils.h"
2021-10-26 09:24:05 +08:00
#include "../globalbackupinfo.h"
2021-09-16 16:05:46 +08:00
SystemBackup::SystemBackup(QWidget *parent /*= nullptr*/) :
2021-10-13 16:31:58 +08:00
QStackedWidget(parent),
m_udector(new UdiskDetector()),
m_isLocal(true),
2021-10-26 09:24:05 +08:00
m_systemBackupState(SystemBackupState::IDEL)
2021-09-16 16:05:46 +08:00
{
2021-10-13 16:31:58 +08:00
// 界面手写代码创建,作为练手
2021-09-16 16:05:46 +08:00
initFirstWidget();
2021-10-13 16:31:58 +08:00
initSecondWidget();
initThirdWidget();
2021-10-26 09:24:05 +08:00
initForthWidget();
initFifthWidget();
2021-11-09 15:44:26 +08:00
initLastWidget();
2021-09-16 16:05:46 +08:00
}
SystemBackup::~SystemBackup()
2021-10-13 16:31:58 +08:00
{
delete m_udector;
}
2021-09-16 16:05:46 +08:00
2021-10-13 16:31:58 +08:00
/**
* @brief
*/
2021-09-16 16:05:46 +08:00
void SystemBackup::initFirstWidget()
{
QWidget *first = new QWidget;
2021-10-26 09:24:05 +08:00
// 图片
2021-09-16 16:05:46 +08:00
QLabel *imageBackup_firstPage = new QLabel(first);
imageBackup_firstPage->setGeometry(421, 120, 300, 326);
2021-09-23 11:21:58 +08:00
QPixmap pixmap(":/images/system_backup.svg");
2021-09-16 16:05:46 +08:00
imageBackup_firstPage->setPixmap(pixmap);
imageBackup_firstPage->setScaledContents(true);
2021-10-26 09:24:05 +08:00
// 系统备份大字提示
2021-10-13 16:31:58 +08:00
MyLabel *labelBackup_firstPage = new MyLabel(first);
labelBackup_firstPage->setDeplayText(tr("System Backup"));
labelBackup_firstPage->setFixedWidth(500);
2021-09-16 16:05:46 +08:00
labelBackup_firstPage->setFixedHeight(48);
labelBackup_firstPage->move(41, 120);
// 默认水平左对齐,上下居中对齐;故不需要设置
// labelBackup_firstPage->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
// labelBackup_firstPage->setText(tr("System Backup"));
QFont font;
font.setBold(true);
font.setPixelSize(36);
labelBackup_firstPage->setFont(font);
2021-10-13 16:31:58 +08:00
// labelBackup_firstPage->setAttribute(Qt::WA_TranslucentBackground);
labelBackup_firstPage->setScaledContents(true);
2021-09-16 16:05:46 +08:00
labelBackup_firstPage->adjustSize();
2021-10-26 09:24:05 +08:00
// 系统备份说明
2021-10-13 16:31:58 +08:00
MyLabel *labelNote_firstPage = new MyLabel(first);
labelNote_firstPage->setFixedWidth(700);
2021-09-16 16:05:46 +08:00
labelNote_firstPage->setFixedHeight(24);
labelNote_firstPage->move(41, 180);
2021-10-13 16:31:58 +08:00
labelNote_firstPage->setDeplayText(tr("Can be restored when files are damaged or lost"));
2021-09-16 16:05:46 +08:00
font.setBold(false);
font.setPixelSize(18);
labelNote_firstPage->setFont(font);
2021-10-13 16:31:58 +08:00
labelNote_firstPage->setScaledContents(true);
2021-09-16 16:05:46 +08:00
labelNote_firstPage->adjustSize();
2021-10-26 09:24:05 +08:00
// 多点还原
2021-09-16 16:05:46 +08:00
MyIconLabel *iconMultiBackup_firstPage = new MyIconLabel(first);
2021-10-13 16:31:58 +08:00
iconMultiBackup_firstPage->setGeometry(41, 244, 180, 36);
iconMultiBackup_firstPage->setThemeIcon("ukui-bf-many-spot-symbolic", ":/symbos/ukui-bf-many-spot-symbolic.png");
2021-09-16 16:05:46 +08:00
iconMultiBackup_firstPage->setDesplayText(tr("Multi-Spot"));
iconMultiBackup_firstPage->setEnabled(false);
2021-10-26 09:24:05 +08:00
// 体积小
2021-09-16 16:05:46 +08:00
MyIconLabel *iconSmallSize_firstPage = new MyIconLabel(first);
2021-10-13 16:31:58 +08:00
iconSmallSize_firstPage->setGeometry(201, 244, 180, 36);
iconSmallSize_firstPage->setThemeIcon("ukui-bf-volume-symbolic", ":/symbos/ukui-bf-volume-symbolic.png");
2021-09-16 16:05:46 +08:00
iconSmallSize_firstPage->setDesplayText(tr("Small Size"));
iconSmallSize_firstPage->setEnabled(false);
2021-10-26 09:24:05 +08:00
// 安全
2021-09-16 16:05:46 +08:00
MyIconLabel *iconSecurity_firstPage = new MyIconLabel(first);
2021-10-13 16:31:58 +08:00
iconSecurity_firstPage->setGeometry(41, 296, 180, 36);
iconSecurity_firstPage->setThemeIcon("ukui-bf-security-symbolic", ":/symbos/ukui-bf-security-symbolic.png");
2021-09-16 16:05:46 +08:00
iconSecurity_firstPage->setDesplayText(tr("Security"));
iconSecurity_firstPage->setEnabled(false);
2021-10-26 09:24:05 +08:00
// 操作简单
2021-09-16 16:05:46 +08:00
MyIconLabel *iconSimple_firstPage = new MyIconLabel(first);
2021-10-13 16:31:58 +08:00
iconSimple_firstPage->setGeometry(201, 296, 180, 36);
iconSimple_firstPage->setThemeIcon("ukui-bf-simple-symbolic", ":/symbos/ukui-bf-simple-symbolic.png");
2021-09-16 16:05:46 +08:00
iconSimple_firstPage->setDesplayText(tr("Simple"));
iconSimple_firstPage->setEnabled(false);
2021-10-26 09:24:05 +08:00
// 开始备份按钮
MyPushButton *beginBackup = new MyPushButton(first);
2021-09-23 11:21:58 +08:00
beginBackup->setGeometry(41, 372, 180, 52);
beginBackup->setText(tr("Start Backup"));
beginBackup->setEnabled(true);
beginBackup->setAutoRepeat(true);
font.setPixelSize(24);
beginBackup->setFont(font);
2021-10-26 09:24:05 +08:00
connect(beginBackup, &MyPushButton::clicked, this, &SystemBackup::on_next_clicked);
2021-10-13 16:31:58 +08:00
2021-10-26 09:24:05 +08:00
// 备份管理
2021-10-13 16:31:58 +08:00
ClickLabel * backupPointManage = new ClickLabel(tr("Backup Management >>"), first);
backupPointManage->setGeometry(551, 551, 200, 30);
QPalette pal(backupPointManage->palette());
pal.setColor(QPalette::WindowText, QColor(Qt::blue));
backupPointManage->setPalette(pal);
backupPointManage->setToolTip(tr("Backup Management >>"));
connect(backupPointManage, &ClickLabel::clicked, this, &SystemBackup::on_systemBackupManage_clicked);
2021-09-16 16:05:46 +08:00
addWidget(first);
}
2021-10-13 16:31:58 +08:00
/**
* @brief
* @param checked
*/
void SystemBackup::on_pre_clicked(bool checked)
{
Q_UNUSED(checked)
int index = this->currentIndex() - 1;
if (index >= 0) {
this->setCurrentIndex(index);
}
}
/**
* @brief
* @param checked
*/
void SystemBackup::on_next_clicked(bool checked)
{
Q_UNUSED(checked)
int index = this->currentIndex() + 1;
if (index < this->count()) {
this->setCurrentIndex(index);
}
}
/**
* @brief
*/
void SystemBackup::on_systemBackupManage_clicked()
{
// TODO:创建系统备份点管理界面
}
/**
* @brief
*/
void SystemBackup::initSecondWidget()
{
QWidget *second = new QWidget;
2021-10-26 09:24:05 +08:00
// 备份路径选择提示
2021-10-13 16:31:58 +08:00
MyLabel* labelSelect = new MyLabel(second);
labelSelect->setDeplayText(tr("Please select backup position"));
labelSelect->setFixedWidth(600);
labelSelect->setFixedHeight(27);
labelSelect->move(41, 41);
QFont font;
font.setBold(true);
font.setPixelSize(18);
labelSelect->setFont(font);
labelSelect->setScaledContents(true);
labelSelect->adjustSize();
2021-10-26 09:24:05 +08:00
// 备份路径选择框
2021-10-13 16:31:58 +08:00
QComboBox* comboSelect = new QComboBox(second);
comboSelect->setGeometry(41, 84, 680, 36);
// 添加本地默认路径、移动设备目录
connect(m_udector, &UdiskDetector::udiskListChanged, this, [=](QList<QStorageInfo> diskList) {
comboSelect->clear();
QIcon iconFolder = QIcon::fromTheme("insync-folder.png", QIcon(":/images/folder.png"));
2021-10-26 09:24:05 +08:00
// 如果有备份分区,则将本地默认分区备份路径放在第一个
if (GlobelBackupInfo::inst().hasBackupPartition()) {
QString qsLocalDefaultPath = Utils::getSysRootPath() + BACKUP_SNAPSHOTS_PATH;
qsLocalDefaultPath.replace("//", "/");
this->m_prefixDestPath = "";
this->m_udiskPaths << "";
comboSelect->addItem(iconFolder, tr("local default path : ") + qsLocalDefaultPath);
}
2021-10-13 16:31:58 +08:00
QString qsPreDevPath(tr("removable devices path : "));
for (QStorageInfo& disk : diskList) {
2021-10-26 09:24:05 +08:00
this->m_udiskPaths << disk.rootPath();
2021-10-13 16:31:58 +08:00
comboSelect->addItem(iconFolder, qsPreDevPath + disk.rootPath() + BACKUP_SNAPSHOTS_PATH);
}
});
m_udector->getStorageInfo();
2021-10-26 09:24:05 +08:00
// 上一步按钮
MyPushButton *preStep = new MyPushButton(second);
2021-10-13 16:31:58 +08:00
preStep->setGeometry(271, 176, 97, 36);
preStep->setText(tr("back"));
preStep->setEnabled(true);
preStep->setAutoRepeat(true);
2021-10-26 09:24:05 +08:00
connect(preStep, &MyPushButton::clicked, this, &SystemBackup::on_pre_clicked);
2021-10-13 16:31:58 +08:00
2021-10-26 09:24:05 +08:00
// 下一步按钮
MyPushButton *nextStep = new MyPushButton(second);
2021-10-13 16:31:58 +08:00
nextStep->setGeometry(389, 176, 97, 36);
nextStep->setText(tr("next"));
nextStep->setEnabled(true);
nextStep->setAutoRepeat(true);
2021-10-26 09:24:05 +08:00
connect(nextStep, &MyPushButton::clicked, this, [=](bool checked) {
2021-11-09 15:44:26 +08:00
// 备份路径选择索引
int index = comboSelect->currentIndex();
// 第一个选项是本地系统备份
this->m_isLocal = GlobelBackupInfo::inst().hasBackupPartition() && (index == 0);
this->m_prefixDestPath = this->m_udiskPaths.at(index);
2021-10-13 16:31:58 +08:00
this->on_next_clicked(checked);
emit this->startCheckEnv();
});
addWidget(second);
}
/**
* @brief
*/
void SystemBackup::initThirdWidget()
{
QWidget *third = new QWidget;
2021-10-26 09:24:05 +08:00
// 流程进度提示栏
2021-10-13 16:31:58 +08:00
CircleLable *one = new CircleLable("1", third, 24, QColor(COLOR_BLUE));
one->move(QPoint(81, 41));
LineLabel *line1 = new LineLabel(third, QColor(COLOR_BLUE));
line1->move(QPoint(108, 41));
CircleLable *two = new CircleLable("2", third);
two->move(QPoint(261, 41));
LineLabel *line2 = new LineLabel(third);
line2->move(QPoint(288, 41));
CircleLable *three = new CircleLable("3", third);
three->move(QPoint(441, 41));
LineLabel *line3 = new LineLabel(third);
line3->move(QPoint(468, 41));
CircleLable *four = new CircleLable("4", third);
four->move(QPoint(621, 41));
MyLabel *label1 = new MyLabel(tr("checking"), third);
label1->setFontColor(QColor(COLOR_BLUE));
label1->setGeometry(11, 72, 164, 30);
MyLabel *label2 = new MyLabel(tr("preparing"), third);
label2->setGeometry(191, 72, 164, 30);
MyLabel *label3 = new MyLabel(tr("backuping"), third);
label3->setGeometry(371, 72, 164, 30);
MyLabel *label4 = new MyLabel(tr("finished"), third);
label4->setGeometry(551, 72, 164, 30);
2021-10-26 09:24:05 +08:00
// 检测等待图标
2021-10-13 16:31:58 +08:00
QLabel *loadingGif = new QLabel(third);
2021-10-26 09:24:05 +08:00
// 环境检测等待动画
QMovie *movie = new QMovie(":/images/loading.gif", QByteArray(), third);
loadingGif->setMovie(movie);
2021-10-13 16:31:58 +08:00
loadingGif->setGeometry(180, 180, 20, 20);
2021-10-26 09:24:05 +08:00
// 检测结果对错图标
QLabel *resultLogo = new QLabel(third);
resultLogo->setGeometry(180, 180, 20, 20);
// 检测中大标题
MyLabel *bigTitle = new MyLabel(third);
bigTitle->setFontSize(24);
2021-10-13 16:31:58 +08:00
bigTitle->setGeometry(210, 170, 500, 36);
2021-10-26 09:24:05 +08:00
// 检测中的记录:黑点和文字
CircleLable *dot1 = new CircleLable(QString(""), third, 6, Qt::black);
dot1->setGeometry(190, 235, 6, 6);
MyLabel *labelCheck1 = new MyLabel(third);
labelCheck1->setGeometry(210, 220, 500, 30);
CircleLable *dot2 = new CircleLable(QString(""), third, 6, Qt::black);
dot2->setGeometry(190, 265, 6, 6);
MyLabel *labelCheck2 = new MyLabel(third);
labelCheck2->setGeometry(210, 250, 450, 30);
// 上一步按钮
MyPushButton *preStep = new MyPushButton(third);
preStep->setGeometry(221, 330, 97, 36);
preStep->setText(tr("back"));
preStep->setEnabled(true);
preStep->setAutoRepeat(true);
connect(preStep, &MyPushButton::clicked, this, &SystemBackup::on_pre_clicked);
// 下一步按钮
MyPushButton *nextStep = new MyPushButton(third);
nextStep->setGeometry(339, 330, 97, 36);
nextStep->setText(tr("next"));
nextStep->setEnabled(true);
nextStep->setAutoRepeat(true);
connect(nextStep, &MyPushButton::clicked, this, [=](bool checked) {
this->on_next_clicked(checked);
});
// 重新检测按钮
MyPushButton *recheck = new MyPushButton(third);
recheck->setGeometry(339, 330, 97, 36);
recheck->setText(tr("recheck"));
recheck->setEnabled(true);
recheck->setAutoRepeat(true);
connect(recheck, &MyPushButton::clicked, this, [=](bool checked) {
// 为测试下一步骤,这里暂时先改为下一步
// emit this->startCheckEnv();
this->on_next_clicked(checked);
});
// 开始检测
connect(this, &SystemBackup::startCheckEnv, this, [=]() {
this->m_systemBackupState = SystemBackupState::CHECKING;
loadingGif->setVisible(true);
movie->start();
resultLogo->setVisible(false);
// 环境检测中,请等待
bigTitle->setDeplayText(tr("Being checked, wait a moment ..."));
dot1->setBackgroundColor(Qt::black);
dot2->setBackgroundColor(Qt::black);
// 环境检测中
labelCheck1->setDeplayText(tr("The environment is being checked ..."));
labelCheck2->setFontWordWrap(true);
if (this->m_isLocal) {
// 检测备份分区空间是否充足···
labelCheck2->setDeplayText(tr("Check whether the remaining capacity of the backup partition is sufficient"));
} else {
// 检测移动设备空间是否充足···
labelCheck2->setDeplayText(tr("Check whether the remaining capacity of the removable device is sufficient"));
}
preStep->setVisible(false);
nextStep->setVisible(false);
recheck->setVisible(false);
this->on_checkEnv_start();
});
// 检测结果
connect(this, &SystemBackup::checkEnvResult, this, [=](bool result, const QString &errMsg, const QString &errTip) {
loadingGif->setVisible(false);
movie->stop();
if (result) {
QIcon icon = QIcon::fromTheme("ukui-dialog-success", QIcon(":/symbos/ukui-dialog-success.png"));
resultLogo->setPixmap(icon.pixmap(QSize(20,20)));
resultLogo->setVisible(true);
// 检测成功
bigTitle->setDeplayText(tr("Succeeded to check the environment"));
// 备份空间充足
labelCheck1->setDeplayText(tr("The storage for backup is enough"));
dot2->setBackgroundColor(COLOR_YELLOW);
labelCheck2->setFontColor(COLOR_YELLOW);
labelCheck2->setFontWordWrap(true);
// 请确保电脑已连接电源或电量超过60%
labelCheck2->setDeplayText(tr("Make sure the computer is plugged in or the battery level is above 60%"));
dot1->setVisible(true);
dot2->setVisible(true);
labelCheck1->setVisible(true);
labelCheck2->setVisible(true);
nextStep->setVisible(true);
recheck->setVisible(false);
} else {
QIcon icon = QIcon::fromTheme("dialog-error.png", QIcon(":/symbos/dialog-error.png"));
resultLogo->setPixmap(icon.pixmap(QSize(20,20)));
resultLogo->setVisible(true);
// 环境校验失败
bigTitle->setDeplayText(tr("Failed to check the environment"));
labelCheck1->setDeplayText(errMsg);
labelCheck2->setDeplayText(errTip);
if (errMsg.isEmpty()) {
dot1->setVisible(false);
labelCheck1->setVisible(false);
} else {
dot1->setVisible(true);
labelCheck1->setVisible(true);
}
if (errTip.isEmpty()) {
dot2->setVisible(false);
labelCheck2->setVisible(false);
} else {
dot2->setVisible(true);
labelCheck2->setVisible(true);
}
recheck->setVisible(true);
nextStep->setVisible(false);
}
preStep->setVisible(true);
});
2021-10-13 16:31:58 +08:00
addWidget(third);
}
2021-10-26 09:24:05 +08:00
/**
* @brief
*/
void SystemBackup::on_checkEnv_start()
{
GlobelBackupInfo::inst().setIsBusy(true);
m_pInterface = new ComKylinBackupManagerInterface("com.kylin.backup", "/", QDBusConnection::systemBus(), this);
connect(m_pInterface, &ComKylinBackupManagerInterface::sendEnvCheckResult, this, &SystemBackup::on_checkEnv_end);
2021-11-09 15:44:26 +08:00
// 是否已存在备份、还原等操作
bool isActive = false;
if(int(BackupState::BACKUP_STATE_INIT) != m_pInterface->getBackupState(isActive)){
on_checkEnv_end(int(BackupResult::OTHER_BACKUP_OR_RESTORE_RUNNING));
return;
}
2021-10-26 09:24:05 +08:00
BackupWrapper backupWrapper;
backupWrapper.m_type = BackupType::BACKUP_SYSTEM;
backupWrapper.m_iPosition = m_isLocal ? BackupPosition::LOCAL : BackupPosition::UDISK;
2021-11-09 15:44:26 +08:00
QString backupPath = Utils::getSysRootPath();
backupPath += "/";
backupPath.replace("//", "/");
backupWrapper.m_backupPaths << backupPath;
2021-10-26 09:24:05 +08:00
backupWrapper.m_prefixDestPath = m_prefixDestPath;
backupWrapper.m_frontUid = getuid();
backupWrapper.m_gid = getgid();
m_pInterface->checkEnv(backupWrapper);
}
/**
* @brief
* @param result,
*/
void SystemBackup::on_checkEnv_end(int result)
{
m_systemBackupState = SystemBackupState::IDEL;
bool bRst = false;
QString errMsg, errTip;
switch (result) {
case int(BackupResult::LOCK_PROGRAM_FAIL):
// 程序锁定失败,请重试
errMsg = tr("Program lock failed, please retry");
// 可能有其它备份/还原等任务在执行
errTip = tr("There may be other backups or restores being performed");
break;
case int(BackupResult::NO_FOUND_DEALCLASS):
// 不支持的任务类型
errMsg = tr("Unsupported task type");
// 没有找到相应的处理逻辑
errTip = tr("No processing logic was found in the service");
break;
case int(BackupResult::BACKUP_PARTITION_MOUNT_FAIL):
// 备份分区挂载失败
errMsg = tr("Failed to mount the backup partition");
// 检查是否有备份分区
errTip = tr("Check whether there is a backup partition");
break;
case int(BackupResult::BACKUP_CAPACITY_IS_NOT_ENOUGH):
// 备份空间不足
errMsg = tr("The storage for backup is not enough.");
// 建议释放空间后重试
errTip = tr("Retry after release space");
break;
2021-11-09 15:44:26 +08:00
case int(BackupResult::OTHER_BACKUP_OR_RESTORE_RUNNING):
// 其它备份还原等操作正在执行
errMsg = tr("Other backup or restore task is being performed");
// 请稍后重试
errTip = tr("Please try again later");
break;
2021-10-26 09:24:05 +08:00
default:
bRst = true;
break;
}
2021-11-09 15:44:26 +08:00
emit checkEnvResult(bRst, errMsg, errTip);
2021-10-26 09:24:05 +08:00
GlobelBackupInfo::inst().setIsBusy(false);
disconnect(m_pInterface, &ComKylinBackupManagerInterface::sendEnvCheckResult, this, &SystemBackup::on_checkEnv_end);
m_pInterface->deleteLater();
}
/**
* @brief
*/
void SystemBackup::initForthWidget()
{
QWidget *forth = new QWidget;
// 流程进度提示栏
CircleLable *one = new CircleLable("1", forth, 24, QColor(COLOR_BLUE));
one->move(QPoint(81, 41));
LineLabel *line1 = new LineLabel(forth, QColor(COLOR_BLUE));
line1->move(QPoint(108, 41));
CircleLable *two = new CircleLable("2", forth, 24, QColor(COLOR_BLUE));
two->move(QPoint(261, 41));
LineLabel *line2 = new LineLabel(forth, QColor(COLOR_BLUE));
line2->move(QPoint(288, 41));
CircleLable *three = new CircleLable("3", forth);
three->move(QPoint(441, 41));
LineLabel *line3 = new LineLabel(forth);
line3->move(QPoint(468, 41));
CircleLable *four = new CircleLable("4", forth);
four->move(QPoint(621, 41));
MyLabel *label1 = new MyLabel(tr("checking"), forth);
label1->setFontColor(QColor(COLOR_BLUE));
label1->setGeometry(11, 72, 164, 30);
MyLabel *label2 = new MyLabel(tr("preparing"), forth);
label2->setFontColor(QColor(COLOR_BLUE));
label2->setGeometry(191, 72, 164, 30);
MyLabel *label3 = new MyLabel(tr("backuping"), forth);
label3->setGeometry(371, 72, 164, 30);
MyLabel *label4 = new MyLabel(tr("finished"), forth);
label4->setGeometry(551, 72, 164, 30);
// 备份名称
MyLabel *labelBackupName = new MyLabel(forth);
labelBackupName->setGeometry(81, 178, 150, 30);
labelBackupName->setAlignment(Qt::AlignRight);
labelBackupName->setDeplayText(tr("Backup Name"));
MyLineEdit *editBackupName = new MyLineEdit(forth);
editBackupName->setGeometry(238, 172, 350, 40);
editBackupName->setMaxLength(100);
editBackupName->setPlaceholderText(QDateTime::currentDateTime().toString("yy-MM-dd hh:mm:ss"));
// 支持输入中英文数字和部分字符
QRegExp regx("^[\u4e00-\u9fa5a-zA-Z0-9~!@#$%^&*()-_+={}':;'\\[\\].<>/? ]+$"); //其中匹配中文[\u4e00-\u9fa5]
QValidator *validator = new QRegExpValidator(regx);
editBackupName->setValidator(validator);
// 备份名称错误提示
MyLabel *labelError = new MyLabel(forth);
labelError->setGeometry(238, 215, 500, 30);
labelError->setFontSize(14);
labelError->setFontColor(Qt::red);
labelError->setVisible(false);
connect(editBackupName, &MyLineEdit::textChanged, this, [=](const QString &text) {
if (this->isExistsBackupName(text)) {
labelError->setDeplayText(tr("Name already exists"));
labelError->setVisible(true);
} else {
labelError->setDeplayText("");
labelError->setVisible(false);
}
});
// 上一步按钮
MyPushButton *preStep = new MyPushButton(forth);
preStep->setGeometry(271, 268, 97, 36);
preStep->setText(tr("back"));
preStep->setEnabled(true);
preStep->setAutoRepeat(true);
connect(preStep, &MyPushButton::clicked, this, &SystemBackup::on_pre_clicked);
// 下一步按钮
MyPushButton *nextStep = new MyPushButton(forth);
nextStep->setGeometry(389, 268, 97, 36);
nextStep->setText(tr("next"));
nextStep->setEnabled(true);
nextStep->setAutoRepeat(true);
connect(nextStep, &MyPushButton::clicked, this, [=](bool checked) {
Q_UNUSED(checked)
if (labelError->isVisible()) {
editBackupName->setFocus();
} else {
this->m_backupName = editBackupName->text();
if (this->m_backupName.isEmpty()) {
this->m_backupName = editBackupName->placeholderText();
}
this->on_next_clicked(checked);
2021-11-09 15:44:26 +08:00
emit this->startBackup();
2021-10-26 09:24:05 +08:00
}
});
addWidget(forth);
}
/**
* @brief
* @return
*/
QList<ParseBackupList::BackupPoint> SystemBackup::getBackupPointList()
{
QList<ParseBackupList::BackupPoint> list;
for (QString backupPath : m_udiskPaths) {
if (backupPath.isEmpty())
backupPath = Utils::getSysRootPath();
backupPath += BACKUP_XML_PATH;
QFile xmlFile(backupPath);
if (!xmlFile.exists() || 0 == xmlFile.size()) {
continue;
}
ParseBackupList parse(backupPath);
list.append(parse.getBackupPointList());
}
return list;
}
/**
* @brief SystemBackup::isExistsBackupName
* @param backupName
* @return bool true-false-
*/
bool SystemBackup::isExistsBackupName(const QString & backupName)
{
QList<ParseBackupList::BackupPoint> backupPointList = getBackupPointList();
QList<BackupWrapper> list = Utils::getBackupLogList();
QSet<QString> setName;
for (BackupWrapper& wraper : list) {
if (wraper.m_backupName == wraper.m_uuid) {
// 说明是此功能之前存在的备份点
for (const ParseBackupList::BackupPoint & backupPonit : backupPointList) {
if (backupPonit.m_uuid == wraper.m_uuid) {
wraper.m_backupName = backupPonit.m_backupName;
}
}
}
// type==8为删除备份点操作
2021-11-09 15:44:26 +08:00
if (BackupType::DELETE_BACKUP == wraper.m_type)
2021-10-26 09:24:05 +08:00
setName.remove(wraper.m_backupName);
else
setName.insert(wraper.m_backupName);
}
return setName.contains(backupName);
}
/**
* @brief
*/
void SystemBackup::initFifthWidget()
{
QWidget *fifth = new QWidget;
// 流程进度提示栏
CircleLable *one = new CircleLable("1", fifth, 24, QColor(COLOR_BLUE));
one->move(QPoint(81, 41));
LineLabel *line1 = new LineLabel(fifth, QColor(COLOR_BLUE));
line1->move(QPoint(108, 41));
CircleLable *two = new CircleLable("2", fifth, 24, QColor(COLOR_BLUE));
two->move(QPoint(261, 41));
LineLabel *line2 = new LineLabel(fifth, QColor(COLOR_BLUE));
line2->move(QPoint(288, 41));
CircleLable *three = new CircleLable("3", fifth, 24, QColor(COLOR_BLUE));
three->move(QPoint(441, 41));
LineLabel *line3 = new LineLabel(fifth, QColor(COLOR_BLUE));
line3->move(QPoint(468, 41));
CircleLable *four = new CircleLable("4", fifth);
four->move(QPoint(621, 41));
MyLabel *label1 = new MyLabel(tr("checking"), fifth);
label1->setFontColor(QColor(COLOR_BLUE));
label1->setGeometry(11, 72, 164, 30);
MyLabel *label2 = new MyLabel(tr("preparing"), fifth);
label2->setFontColor(QColor(COLOR_BLUE));
label2->setGeometry(191, 72, 164, 30);
MyLabel *label3 = new MyLabel(tr("backuping"), fifth);
label3->setFontColor(QColor(COLOR_BLUE));
label3->setGeometry(371, 72, 164, 30);
MyLabel *label4 = new MyLabel(tr("finished"), fifth);
label4->setGeometry(551, 72, 164, 30);
2021-11-15 13:36:00 +08:00
// 检测等待图标
QLabel *loadingGif = new QLabel(fifth);
// 环境检测等待动画
QMovie *movie = new QMovie(":/images/loading.gif", QByteArray(), fifth);
loadingGif->setMovie(movie);
loadingGif->setGeometry(260, 190, 20, 20);
2021-10-26 09:24:05 +08:00
// 进度条
RingsProgressbar *progressBar = new RingsProgressbar(fifth);
progressBar->setGeometry(301, 151, 100, 100);
// 提醒
MyLabel *labelTip = new MyLabel(fifth);
labelTip->setGeometry(101, 261, 520, 30);
labelTip->setAlignment(Qt::AlignCenter);
labelTip->setDeplayText(tr("Do not use computers in case of data loss"));
2021-11-09 15:44:26 +08:00
// 取消按钮
MyPushButton *cancel = new MyPushButton(fifth);
cancel->setGeometry(310, 330, 97, 36);
cancel->setText(tr("cancel"));
cancel->setEnabled(true);
cancel->setAutoRepeat(true);
// 开始备份
connect(this, &SystemBackup::startBackup, this, [=] {
progressBar->setPersent(0);
2021-11-15 13:36:00 +08:00
movie->start();
2021-11-09 15:44:26 +08:00
// 开始备份
this->on_backup_start();
});
// 进度
connect(this, &SystemBackup::progress, this, [=](int state, int rate) {
Q_UNUSED(state)
progressBar->setPersent(rate);
});
// 取消备份
connect(cancel, &MyPushButton::clicked, this, [=](bool checked) {
Q_UNUSED(checked)
// TODO
});
2021-10-26 09:24:05 +08:00
addWidget(fifth);
}
2021-11-09 15:44:26 +08:00
/**
* @brief
*/
void SystemBackup::on_backup_start()
{
GlobelBackupInfo::inst().setIsBusy(true);
m_pInterface = new ComKylinBackupManagerInterface("com.kylin.backup", "/", QDBusConnection::systemBus(), this);
connect(m_pInterface, &ComKylinBackupManagerInterface::sendEnvCheckResult, this, &SystemBackup::on_checkBackup_end);
connect(m_pInterface, &ComKylinBackupManagerInterface::progress, this, &SystemBackup::progress);
connect(m_pInterface, &ComKylinBackupManagerInterface::backupFinished, this, &SystemBackup::on_backup_end);
// 是否已存在备份、还原等操作
bool isActive = false;
if(int(BackupState::BACKUP_STATE_INIT) != m_pInterface->getBackupState(isActive)){
on_checkEnv_end(int(BackupResult::OTHER_BACKUP_OR_RESTORE_RUNNING));
return;
}
BackupWrapper backupWrapper;
backupWrapper.m_type = BackupType::BACKUP_SYSTEM;
backupWrapper.m_iPosition = m_isLocal ? BackupPosition::LOCAL : BackupPosition::UDISK;
QString backupPath = Utils::getSysRootPath();
backupPath += "/";
backupPath.replace("//", "/");
backupWrapper.m_backupPaths << backupPath;
backupWrapper.m_prefixDestPath = m_prefixDestPath;
backupWrapper.m_frontUid = getuid();
backupWrapper.m_gid = getgid();
m_pInterface->goBackup(backupWrapper);
}
/**
* @brief
* @param result
*/
void SystemBackup::on_checkBackup_end(int result)
{
m_systemBackupState = SystemBackupState::IDEL;
bool bRst = false;
QString errMsg, errTip;
switch (result) {
case int(BackupResult::LOCK_PROGRAM_FAIL):
// 程序锁定失败,请重试
errMsg = tr("Program lock failed, please retry");
// 可能有其它备份/还原等任务在执行
errTip = tr("There may be other backups or restores being performed");
break;
case int(BackupResult::NO_FOUND_DEALCLASS):
// 不支持的任务类型
errMsg = tr("Unsupported task type");
// 没有找到相应的处理逻辑
errTip = tr("No processing logic was found in the service");
break;
case int(BackupResult::BACKUP_PARTITION_MOUNT_FAIL):
// 备份分区挂载失败
errMsg = tr("Failed to mount the backup partition");
// 检查是否有备份分区
errTip = tr("Check whether there is a backup partition");
break;
case int(BackupResult::BACKUP_CAPACITY_IS_NOT_ENOUGH):
// 备份空间不足
errMsg = tr("The storage for backup is not enough.");
// 建议释放空间后重试
errTip = tr("Retry after release space");
break;
case int(BackupResult::OTHER_BACKUP_OR_RESTORE_RUNNING):
// 其它备份还原等操作正在执行
errMsg = tr("Other backup or restore task is being performed");
// 请稍后重试
errTip = tr("Please try again later");
break;
case int(BackupResult::WRITE_BACKUP_PATHS_TO_USER_FAILED):
case int(BackupResult::WRITE_STORAGEINFO_ADD_ITEM_FAIL):
case int(BackupResult::WRITE_STORAGEINFO_UPDATE_ITEM_FAIL):
// 创建备份点目录失败
errMsg = tr("Failed to create the backup point directory.");
// 请检查备份目录是否有写权限
errTip = tr("Please check backup partition permissions");
break;
default:
bRst = true;
break;
}
this->on_next_clicked(true);
emit checkEnvResult(bRst, errMsg, errTip);
GlobelBackupInfo::inst().setIsBusy(false);
disconnect(m_pInterface, &ComKylinBackupManagerInterface::sendEnvCheckResult, this, &SystemBackup::on_checkEnv_end);
disconnect(m_pInterface, &ComKylinBackupManagerInterface::progress, this, &SystemBackup::progress);
disconnect(m_pInterface, &ComKylinBackupManagerInterface::backupFinished, this, &SystemBackup::on_backup_end);
m_pInterface->deleteLater();
}
/**
* @brief
* @param result-false ; true
*/
void SystemBackup::on_backup_end(bool result)
{
m_systemBackupState = SystemBackupState::IDEL;
this->on_next_clicked(true);
if (result) {
emit checkEnvResult(result);
} else {
// 备份过程中出现错误
QString errMsg = tr("An error occurred during backup");
// 错误信息参考日志文件:/var/log/backup.log
QString errTip = tr("Error messages refer to log file : /var/log/backup.log");
emit checkEnvResult(result, errMsg, errTip);
}
GlobelBackupInfo::inst().setIsBusy(false);
disconnect(m_pInterface, &ComKylinBackupManagerInterface::sendEnvCheckResult, this, &SystemBackup::on_checkEnv_end);
disconnect(m_pInterface, &ComKylinBackupManagerInterface::backupFinished, this, &SystemBackup::on_backup_end);
m_pInterface->deleteLater();
}
/**
* @brief
*/
void SystemBackup::initLastWidget()
{
QWidget *last = new QWidget;
// 流程进度提示栏
CircleLable *one = new CircleLable("1", last, 24, QColor(COLOR_BLUE));
one->move(QPoint(81, 41));
LineLabel *line1 = new LineLabel(last, QColor(COLOR_BLUE));
line1->move(QPoint(108, 41));
CircleLable *two = new CircleLable("2", last, 24, QColor(COLOR_BLUE));
two->move(QPoint(261, 41));
LineLabel *line2 = new LineLabel(last, QColor(COLOR_BLUE));
line2->move(QPoint(288, 41));
CircleLable *three = new CircleLable("3", last, 24, QColor(COLOR_BLUE));
three->move(QPoint(441, 41));
LineLabel *line3 = new LineLabel(last, QColor(COLOR_BLUE));
line3->move(QPoint(468, 41));
CircleLable *four = new CircleLable("4", last, 24, QColor(COLOR_BLUE));
four->move(QPoint(621, 41));
MyLabel *label1 = new MyLabel(tr("checking"), last);
label1->setFontColor(QColor(COLOR_BLUE));
label1->setGeometry(11, 72, 164, 30);
MyLabel *label2 = new MyLabel(tr("preparing"), last);
label2->setFontColor(QColor(COLOR_BLUE));
label2->setGeometry(191, 72, 164, 30);
MyLabel *label3 = new MyLabel(tr("backuping"), last);
label3->setFontColor(QColor(COLOR_BLUE));
label3->setGeometry(371, 72, 164, 30);
MyLabel *label4 = new MyLabel(tr("finished"), last);
label4->setFontColor(QColor(COLOR_BLUE));
label4->setGeometry(551, 72, 164, 30);
// --------------失败界面--------------
// 备份结果对错图标
QLabel *resultLogo = new QLabel(last);
resultLogo->setGeometry(220, 180, 20, 20);
// 备份结果大标题
MyLabel *bigTitle = new MyLabel(last);
bigTitle->setFontSize(24);
bigTitle->setGeometry(251, 170, 500, 36);
// 备份结果错误提示:黑点和文字
CircleLable *dot1 = new CircleLable(QString(""), last, 6, Qt::black);
dot1->setGeometry(231, 235, 6, 6);
MyLabel *labelError1 = new MyLabel(last);
labelError1->setGeometry(251, 220, 300, 30);
CircleLable *dot2 = new CircleLable(QString(""), last, 6, Qt::black);
dot2->setGeometry(231, 265, 6, 6);
MyLabel *labelError2 = new MyLabel(last);
labelError2->setGeometry(251, 250, 300, 30);
// 再试一次
MyPushButton *retry = new MyPushButton(last);
retry->setGeometry(310, 330, 97, 36);
retry->setText(tr("retry"));
retry->setEnabled(true);
retry->setAutoRepeat(true);
// --------------成功界面--------------
// 返回首页
MyPushButton *homePage = new MyPushButton(last);
homePage->setGeometry(310, 330, 97, 36);
homePage->setText(tr("home page"));
homePage->setEnabled(true);
homePage->setAutoRepeat(true);
// 备份检测结果
connect(this, &SystemBackup::checkEnvResult, this, [=](bool result, const QString &errMsg, const QString &errTip) {
if (result) {
QIcon icon = QIcon::fromTheme("ukui-dialog-success", QIcon(":/symbos/ukui-dialog-success.png"));
resultLogo->setPixmap(icon.pixmap(QSize(20,20)));
resultLogo->setVisible(true);
// 备份成功
bigTitle->setDeplayText(tr("The backup is successful"));
dot1->setVisible(false);
dot2->setVisible(false);
labelError1->setVisible(false);
labelError2->setVisible(false);
retry->setVisible(false);
homePage->setVisible(true);
} else {
QIcon icon = QIcon::fromTheme("dialog-error.png", QIcon(":/symbos/dialog-error.png"));
resultLogo->setPixmap(icon.pixmap(QSize(20,20)));
resultLogo->setVisible(true);
// 环境校验失败
bigTitle->setDeplayText(tr("The backup is failed"));
labelError1->setDeplayText(errMsg);
labelError2->setDeplayText(errTip);
retry->setVisible(true);
homePage->setVisible(false);
}
});
// 再试一次
connect(retry, &MyPushButton::clicked, this, [=](bool checked) {
Q_UNUSED(checked)
this->setCurrentIndex(SystemBackupPage::NAME_BACKUP_PAGE);
});
// 返回首页
connect(homePage, &MyPushButton::clicked, this, [=](bool checked) {
Q_UNUSED(checked)
this->setCurrentIndex(SystemBackupPage::HOME_PAGE);
});
addWidget(last);
}