-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateDialog.cpp
More file actions
185 lines (156 loc) · 5.25 KB
/
UpdateDialog.cpp
File metadata and controls
185 lines (156 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "UpdateDialog.h"
#include "UpdateConfig.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTextBrowser>
#include <QPushButton>
#include <QProgressBar>
#include <QDesktopServices>
#include <QUrl>
#include <QApplication>
#include <QStyle>
#include <QMessageBox>
#ifdef Q_OS_MACOS
#include "MacSparkleUpdater.h"
#endif
#ifdef Q_OS_WIN
#include "WinSparkleUpdater.h"
#endif
UpdateDialog::UpdateDialog(const UpdateChecker::UpdateInfo &info, QWidget *parent)
: QDialog(parent)
, updateInfo_(info)
{
setupUi();
}
void UpdateDialog::setupUi() {
setWindowTitle(tr("Update Available"));
setMinimumSize(500, 400);
auto *mainLayout = new QVBoxLayout(this);
// Title
titleLabel_ = new QLabel(this);
titleLabel_->setText(tr("A new version of Phraims is available!"));
QFont titleFont = titleLabel_->font();
titleFont.setPointSize(titleFont.pointSize() + 2);
titleFont.setBold(true);
titleLabel_->setFont(titleFont);
mainLayout->addWidget(titleLabel_);
// Version information
versionLabel_ = new QLabel(this);
const QString versionText = tr("Current version: %1\nLatest version: %2")
.arg(updateInfo_.currentVersion)
.arg(updateInfo_.latestVersion);
versionLabel_->setText(versionText);
mainLayout->addWidget(versionLabel_);
mainLayout->addSpacing(10);
// Release notes section
auto *notesLabel = new QLabel(tr("What's New:"), this);
QFont notesFont = notesLabel->font();
notesFont.setBold(true);
notesLabel->setFont(notesFont);
mainLayout->addWidget(notesLabel);
releaseNotesBrowser_ = new QTextBrowser(this);
releaseNotesBrowser_->setOpenExternalLinks(true);
releaseNotesBrowser_->setMarkdown(updateInfo_.releaseNotes);
mainLayout->addWidget(releaseNotesBrowser_);
// Progress bar (hidden initially, used for Windows download)
progressBar_ = new QProgressBar(this);
progressBar_->setVisible(false);
mainLayout->addWidget(progressBar_);
// Buttons
auto *buttonLayout = new QHBoxLayout();
viewNotesButton_ = new QPushButton(tr("View Full Release Notes"), this);
connect(viewNotesButton_, &QPushButton::clicked, this, &UpdateDialog::onViewReleaseNotesClicked);
buttonLayout->addWidget(viewNotesButton_);
buttonLayout->addStretch();
remindLaterButton_ = new QPushButton(tr("Remind Me Later"), this);
connect(remindLaterButton_, &QPushButton::clicked, this, &QDialog::reject);
buttonLayout->addWidget(remindLaterButton_);
// Platform-specific update button
updateButton_ = new QPushButton(this);
#ifdef Q_OS_MACOS
updateButton_->setText(tr("Check for Update"));
#elif defined(Q_OS_WIN)
updateButton_->setText(tr("Check for Update"));
#else
updateButton_->setText(tr("Download"));
#endif
updateButton_->setDefault(true);
connect(updateButton_, &QPushButton::clicked, this, &UpdateDialog::onUpdateButtonClicked);
buttonLayout->addWidget(updateButton_);
mainLayout->addLayout(buttonLayout);
}
void UpdateDialog::onUpdateButtonClicked() {
#if defined(Q_OS_MACOS)
// On macOS, try Sparkle first, then fallback to manual download
if (!triggerSparkleUpdate()) {
openUrl(updateInfo_.downloadUrl.isEmpty() ? updateInfo_.releaseUrl : updateInfo_.downloadUrl);
accept();
}
#elif defined(Q_OS_WIN)
// On Windows, try WinSparkle first, then fallback to manual download
if (!triggerWinSparkleUpdate()) {
openUrl(updateInfo_.downloadUrl.isEmpty() ? updateInfo_.releaseUrl : updateInfo_.downloadUrl);
accept();
}
#else
// Linux: Open release page for manual download
openUrl(updateInfo_.releaseUrl);
accept();
#endif
}
void UpdateDialog::onViewReleaseNotesClicked() {
openUrl(updateInfo_.releaseUrl);
}
void UpdateDialog::openUrl(const QString &url) {
if (!url.isEmpty()) {
QDesktopServices::openUrl(QUrl(url));
}
}
#ifdef Q_OS_MACOS
bool UpdateDialog::triggerSparkleUpdate() {
// Check if Sparkle is available
if (!MacSparkleUpdater::isAvailable()) {
qDebug() << "Sparkle framework not available, falling back to manual download";
return false;
}
// Create Sparkle updater if not already created
if (!macSparkleUpdater_) {
macSparkleUpdater_ = new MacSparkleUpdater(this);
}
// Trigger update check
if (macSparkleUpdater_->checkForUpdates()) {
// Sparkle will handle the rest - close our dialog
accept();
return true;
}
// If Sparkle check failed, fallback to manual download
return false;
}
#endif // Q_OS_MACOS
#ifdef Q_OS_WIN
bool UpdateDialog::triggerWinSparkleUpdate() {
// Check if WinSparkle is available
if (!WinSparkleUpdater::isAvailable()) {
qDebug() << "WinSparkle library not available, falling back to manual download";
return false;
}
// Create WinSparkle updater if not already created
if (!winSparkleUpdater_) {
winSparkleUpdater_ = new WinSparkleUpdater(this);
// Initialize with appcast URL (same feed as macOS Sparkle)
if (!winSparkleUpdater_->initialize(QString::fromLatin1(UpdateConfig::APPCAST_URL))) {
qWarning() << "Failed to initialize WinSparkle";
return false;
}
}
// Trigger update check
if (winSparkleUpdater_->checkForUpdates()) {
// WinSparkle will handle the rest - close our dialog
accept();
return true;
}
// If WinSparkle check failed, fallback to manual download
return false;
}
#endif // Q_OS_WIN