137 lines
3.6 KiB
C++
137 lines
3.6 KiB
C++
#include "Backup.h"
|
|
#include "ui_Backup.h"
|
|
#include <QFileDialog>
|
|
#include <QProcess>
|
|
|
|
CBackup::CBackup(QWidget *parent) :
|
|
QWidget(parent),
|
|
ui(new Ui::CBackup)
|
|
{
|
|
ui->setupUi(this);
|
|
backup_path = "";
|
|
|
|
}
|
|
|
|
CBackup::~CBackup()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void CBackup::on_pushButton_path_clicked()
|
|
{
|
|
QString dirpath = QFileDialog::getExistingDirectory(this, QStringLiteral("选择目录"), "./", QFileDialog::ShowDirsOnly);
|
|
if(dirpath.isEmpty()) dirpath = QDir::currentPath();
|
|
ui->lineEdit_filepath->setText(dirpath + "/");
|
|
|
|
backup_path = dirpath;
|
|
}
|
|
|
|
|
|
void CBackup::on_pushButton_confirm_clicked()
|
|
{
|
|
|
|
if(ui->lineEdit_filepath->text() == ""){
|
|
QMessageBox::information(this,tr("提示"),tr("请选择导出文件路径!"));
|
|
return;
|
|
}
|
|
#ifdef Q_OS_WIN32
|
|
backup_path = backup_path + "\\config\\";
|
|
copyDirectory(QCoreApplication::applicationDirPath() + "\\config\\", backup_path);
|
|
#endif
|
|
#ifdef Q_OS_LINUX
|
|
backup_path = backup_path + "/config/";
|
|
copyDirectory(QCoreApplication::applicationDirPath() + "/config/", backup_path);
|
|
#endif
|
|
QFile file(backup_path+"macbackup");
|
|
if (!file.open(QIODevice::ReadWrite)) {
|
|
qWarning("Couldn't open file for writing.");
|
|
return;
|
|
}
|
|
QTextStream out(&file);
|
|
out << MAC << endl;
|
|
file.close();
|
|
QMessageBox::information(this,tr("导出"),tr("导出成功!"));
|
|
}
|
|
|
|
int CBackup::ExeSqlData(QString& strSql)
|
|
{
|
|
QSqlQuery sql_query;
|
|
int iRet = -1;
|
|
qDebug() << "strSql" << strSql << endl;
|
|
if(!sql_query.exec(strSql))
|
|
{
|
|
qDebug() << sql_query.lastError();
|
|
}
|
|
else
|
|
{
|
|
while(sql_query.next())
|
|
{
|
|
iRet = sql_query.value(0).toInt();
|
|
}
|
|
}
|
|
return iRet;
|
|
}
|
|
bool CBackup::copyFile(const QString &sourceFile, const QString &destinationFile)
|
|
{
|
|
QFile srcFile(sourceFile);
|
|
QFile dstFile(destinationFile);
|
|
|
|
if (!srcFile.exists()) {
|
|
qDebug() << "Source file does not exist:" << sourceFile;
|
|
return false;
|
|
}
|
|
|
|
if (!srcFile.open(QIODevice::ReadOnly)) {
|
|
qDebug() << "Unable to open source file for reading:" << sourceFile;
|
|
return false;
|
|
}
|
|
|
|
if (!dstFile.open(QIODevice::WriteOnly)) {
|
|
srcFile.close();
|
|
qDebug() << "Unable to open destination file for writing:" << destinationFile;
|
|
return false;
|
|
}
|
|
|
|
dstFile.write(srcFile.readAll());
|
|
srcFile.close();
|
|
dstFile.close();
|
|
return true;
|
|
}
|
|
bool CBackup::copyDirectory(const QString &sourceDir, const QString &destinationDir) {
|
|
QDir srcDir(sourceDir);
|
|
QDir destDir(destinationDir);
|
|
|
|
// 如果目标文件夹已存在,则删除它
|
|
if (destDir.exists()) {
|
|
destDir.removeRecursively();
|
|
}
|
|
|
|
// 创建目标文件夹
|
|
if (!destDir.mkpath(".")) {
|
|
qDebug() << "无法创建目标文件夹";
|
|
return false;
|
|
}
|
|
|
|
// 复制文件
|
|
foreach (const QFileInfo &fileInfo, srcDir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot)) {
|
|
QString fileName = fileInfo.fileName();
|
|
QString srcFilePath = srcDir.absoluteFilePath(fileName);
|
|
QString destFilePath = destDir.absoluteFilePath(fileName);
|
|
|
|
if (fileInfo.isDir()) {
|
|
// 递归复制子文件夹
|
|
if (!copyDirectory(srcFilePath, destFilePath)) {
|
|
return false;
|
|
}
|
|
} else {
|
|
// 复制文件
|
|
if (!QFile::copy(srcFilePath, destFilePath)) {
|
|
qDebug() << "无法复制文件" << srcFilePath << "到" << destFilePath;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|