OfficeAssistant_Win10/OfficeAssistant_msvc/mysettingsdialog.cpp

113 lines
4.5 KiB
C++

#include "mysettingsdialog.h"
#include <Windows.h>
#include <QClipBoard>
#include "netio.h"
#include "sqlitehelper.h"
#include "config.h"
#include "globalvariables.h"
MySettingsDialog::MySettingsDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
setWindowFlags(Qt::WindowCloseButtonHint);
this->setFixedSize(width(), height());
HKEY hRoot = HKEY_CURRENT_USER;
wchar_t *szSubKey = (wchar_t *)"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY hKey;
DWORD dwDisposition = REG_OPENED_EXISTING_KEY; // 如果不存在不创建
LONG lRet = RegCreateKeyEx(hRoot, szSubKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (lRet != ERROR_SUCCESS) {
ui.autostart->setDisabled(true);
}
QString app = QApplication::applicationFilePath();
app.replace("/", "\\");
DWORD size=128+sizeof(wchar_t);
char reg[128] = { 0 };
lRet = RegQueryValueEx(hKey, L"OfficeAssistant", NULL, NULL,(LPBYTE)reg, &size);
if (lRet==0) {
ui.autostart->setChecked(true);
}
copyButton = ui.copy;
connect(copyButton, &QPushButton::clicked, this, &MySettingsDialog::copyToCLipboard);
connect(ui.autostart, &QCheckBox::stateChanged, this, &MySettingsDialog::autoStart);
connect(ui.addshortcut, &QPushButton::clicked, this, &MySettingsDialog::createShortcut);
connect(ui.law_button, &QPushButton::clicked, this, &MySettingsDialog::law);
ui.code->setText(QString(QCryptographicHash::hash(getMachineGUID().toUtf8(), QCryptographicHash::Md5).toHex()));
ui.version->setText(QString(VERSION) + "-" + QString(RELEASE));
}
MySettingsDialog::~MySettingsDialog()
{
}
void MySettingsDialog::autoStart(int state)
{
if (state == Qt::Checked) {
HKEY hRoot = HKEY_CURRENT_USER;
wchar_t *szSubKey = (wchar_t*)L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY hKey;
DWORD dwDisposition = REG_OPENED_EXISTING_KEY; // 如果不存在不创建
LONG lRet = RegCreateKeyEx(hRoot, szSubKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (lRet != ERROR_SUCCESS) {
QMessageBox::critical(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("创建开机启动项失败"));
return;
}
QString app = QApplication::applicationFilePath();
app.replace("/", "\\");
qDebug() << app.toStdWString().c_str();
lRet = RegSetValueEx(hKey, LENG_NAME, 0, REG_SZ, (BYTE*)app.toStdWString().c_str(), app.length()*sizeof(wchar_t));
if (lRet == ERROR_SUCCESS)
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("创建开机启动项成功"));
}
RegCloseKey(hKey);
}
else if (state == Qt::Unchecked) {
HKEY hRoot = HKEY_CURRENT_USER;
wchar_t *szSubKey = (wchar_t*)L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY hKey;
DWORD dwDisposition = REG_OPENED_EXISTING_KEY;
LONG lRet = RegCreateKeyEx(hRoot, szSubKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (lRet != ERROR_SUCCESS) {
QMessageBox::critical(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("移除开机启动项失败。"));
return;
}
QString app = QApplication::applicationFilePath();
lRet = RegDeleteValue(hKey, LENG_NAME);
if (lRet == ERROR_SUCCESS)
{
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("移除开机启动项成功。"));
}
RegCloseKey(hKey);
}
}
void MySettingsDialog::law()
{
QString path = QApplication::applicationDirPath();
QString license = path + "/license.txt";
license.replace("/", "\\");
ShellExecute(GetDesktopWindow(), L"open", L"notepad.exe" , license.toStdWString().c_str(), nullptr, SW_SHOW);
}
void MySettingsDialog::copyToCLipboard() {
QClipboard *clip = QApplication::clipboard();
clip->setText(QString(QCryptographicHash::hash(getMachineGUID().toUtf8(), QCryptographicHash::Md5).toHex()));
}
void MySettingsDialog::createShortcut() {
QString deskTopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
deskTopPath = deskTopPath + QString::fromLocal8Bit("/");
deskTopPath = deskTopPath + QString::fromLocal8Bit(NAME);
deskTopPath = deskTopPath + QString::fromLocal8Bit(".lnk");
QString srcFile = QApplication::applicationFilePath();
bool succeed=QFile::link(srcFile, deskTopPath);
if (succeed) {
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("创建快捷方式成功。"));
}
else {
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("创建快捷方式失败。"));
}
}