Skip to content

Commit

Permalink
Create shortcut on the desktop when update finished
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 committed Feb 20, 2017
1 parent 3bfd01c commit 5c94e99
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 8 deletions.
18 changes: 17 additions & 1 deletion src/updater/downloaddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ DownloadDialog::DownloadDialog(const QString urlString, const QString installDir
url_string(urlString), file_name(QFileInfo(urlString).fileName()), download_dir(QDir::tempPath() + "/CCR-Plus/"), install_dir(installDir),
file_downloaded(new QFile(download_dir + file_name, this)), file_tmp(new QFile(download_dir + file_name + ".download", this)),
last_bytes(0), current_bytes(0), total_bytes(0),
download_ok(false), uncompress_ok(false), need_redownload(false)
download_ok(false), uncompress_ok(false), need_redownload(false), need_reuncompress(false), create_shortcut(false)
{
ui->setupUi(this);
this->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);

ui->checkBox->hide();
ui->buttonBox->addButton(redownload_button, QDialogButtonBox::AcceptRole);
redownload_button->hide();
this->setFixedHeight(this->sizeHint().height());
Expand All @@ -47,6 +48,17 @@ DownloadDialog::~DownloadDialog()
delete ui;
}

void DownloadDialog::done(int r)
{
if (need_reuncompress)
{
need_reuncompress = false;
return;
}
if (uncompress_ok) create_shortcut = ui->checkBox->isChecked();
QDialog::done(r);
}



void DownloadDialog::showError(const QString& msg, bool isBlocked)
Expand Down Expand Up @@ -87,6 +99,7 @@ void DownloadDialog::updateSuccessed()
ui->label_info->setText("更新成功!");
ui->label_size->hide();
ui->progressBar->hide();
ui->checkBox->show();
ui->buttonBox->setStandardButtons(QDialogButtonBox::Close);
redownload_button->setText("立即启动(&S)");
this->setFixedHeight(120);
Expand Down Expand Up @@ -209,5 +222,8 @@ void DownloadDialog::onDownloadFinished()
void DownloadDialog::on_buttonBox_clicked(QAbstractButton* button)
{
if (ui->buttonBox->standardButton(button) == QDialogButtonBox::Retry)
{
if (uncompress()) updateSuccessed();
need_reuncompress = true;
}
}
8 changes: 7 additions & 1 deletion src/updater/downloaddialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class DownloadDialog : public QDialog
/// 需要重新下载
bool NeedRedownload() const { return need_redownload; }

/// 是否创建快捷方式
bool ShouldCreateShortcut() const { return create_shortcut; }

public slots:
virtual void done(int r) override;

private:
Ui::DownloadDialog* ui;
QPushButton* redownload_button;
Expand All @@ -35,7 +41,7 @@ class DownloadDialog : public QDialog
QString url_string, file_name, download_dir, install_dir;
QFile *file_downloaded, *file_tmp;
qint64 last_bytes, current_bytes, total_bytes;
bool download_ok, uncompress_ok, need_redownload;
bool download_ok, uncompress_ok, need_redownload, need_reuncompress, create_shortcut;

/// 显示错误
void showError(const QString& msg, bool isBlocked = true);
Expand Down
10 changes: 10 additions & 0 deletions src/updater/downloaddialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ QMainWindow
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>创建桌面快捷方式</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
Expand Down
58 changes: 52 additions & 6 deletions src/updater/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QProcess>
#include <QTranslator>
#include <QApplication>
#include <QStandardPaths>
#include <iostream>

#include "../common/version.h"
Expand All @@ -14,27 +15,68 @@
const QString tempPath = QDir::tempPath() + "/" + Updater::APP_NAME + "/";
const QString tempUpdaterPath = tempPath + Updater::UPDATER_NAME;

/// 杀死进程 pid
#if defined(Q_OS_WIN)

#define INITGUID
#include <shobjidl.h>
#include <shlguid.h>
#include <windows.h>

/// 杀死进程 pid
void killProcess(int pid)
{

}

/// 创建快捷方式
bool createShortcut(const QString& installDir, const QString& destinationDir)
{
CoInitialize(NULL);
IShellLink* pShellLink = NULL;

HRESULT hres;
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLink, (void**)&pShellLink);

LPCTSTR appPath = (LPCTSTR)QDir(installDir).absoluteFilePath(Updater::RUN_APP_CMD).utf16();
LPCTSTR lnkPath = (LPCTSTR)QDir(destinationDir).absoluteFilePath("CCR Plus 测评器.lnk").utf16();

if (SUCCEEDED(hres))
{
pShellLink->SetPath(appPath);
pShellLink->SetDescription(L"CCR Plus 测评器");
pShellLink->SetWorkingDirectory((LPCTSTR)installDir.utf16());

IPersistFile* pPersistFile;
hres = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile);

if (SUCCEEDED(hres))
{
hres = pPersistFile->Save(lnkPath, TRUE);
pPersistFile->Release();
}
pShellLink->Release();
}
CoUninitialize();

return SUCCEEDED(hres);
}

#elif defined(Q_OS_LINUX)

/// 杀死进程 pid
void killProcess(int pid)
{

}
#endif

/// 创建快捷方式
void createLink()
bool createShortcut(const QString& installDir, const QString& destinationDir)
{

return !QProcess::execute(QString("%1/install.sh").arg(installDir), { installDir, destinationDir });
}

#endif

void cleanTemp()
{
QDir(tempPath).removeRecursively();
Expand Down Expand Up @@ -68,6 +110,8 @@ int main(int argc, char* argv[])
// {
// if (dialog.NeedRedownload()) continue;
// }
// if (dialog.ShouldCreateShortcut())
// createShortcut("/home/equation/Desktop/a", QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));

// return 0;
// }
Expand Down Expand Up @@ -140,9 +184,11 @@ int main(int argc, char* argv[])
if (dialog.exec() == QDialog::Accepted)
{
if (dialog.NeedRedownload()) continue;
createLink();
QProcess::startDetached(dir + "/" + Updater::RUN_APP_CMD, {}, dir);
QProcess::startDetached(QDir(dir).absoluteFilePath(Updater::RUN_APP_CMD), {}, dir);
}
if (dialog.ShouldCreateShortcut())
createShortcut(dir, QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));

break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/updater/updater.pro
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ RESOURCES = ../../resources/image.qrc \
../../resources/trans.qrc

RC_FILE = ../../resources/updater_icon.rc

win32: LIBS += -lole32

0 comments on commit 5c94e99

Please sign in to comment.