-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateapp.cpp
More file actions
157 lines (137 loc) · 4.08 KB
/
updateapp.cpp
File metadata and controls
157 lines (137 loc) · 4.08 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
#include "updateapp.h"
UpdateApp::UpdateApp(QObject *parent):
QObject(parent)
{
}
QRegExp UpdateApp::reg_exp("(['vV']{1,1})([1-9]{1,1}[.][0-9]{1,1})");
const QString UpdateApp::url_inf_file = "http://viewaide.com/update.inf";
const QString UpdateApp::url_win_app_file = "http://viewaide.com/viewaide-win-setup.exe";
const QString UpdateApp::url_mac_app_file = "http://viewaide.com/viewaide-mac-setup.pkg";
const QString UpdateApp::inf_file = "update.inf";
const QString UpdateApp::inf_file_prefix = "Update Version";
const QString UpdateApp::app_win_file_prefix = "WIN";
const QString UpdateApp::app_mac_file_prefix = "MAC";
const QString UpdateApp::begin_url_new_file = "http:";
const QString UpdateApp::APP_VERSION = "1.2";
bool UpdateApp::CheckFile(const QString& path_file)
{
QFile file ( path_file );
if ( !file.open(QIODevice::ReadOnly) )
return false;
return true;
}
bool UpdateApp::slotCheckUpdate()
{
DownloadAnyFile(url_inf_file);
}
void UpdateApp::slotAcceptDownload()
{
QString path_to_file = QDir::homePath();
path_to_file += "/Viewaide/";
path_to_file += inf_file;
#ifdef Q_OS_WIN
DownloadAnyFile(url_win_app_file);
#endif
#ifdef Q_OS_MAC
DownloadAnyFile(url_mac_app_file);
#endif
QFile(path_to_file).remove();
}
void UpdateApp::slotRejectDownload()
{
QString path_to_file = QDir::homePath();
path_to_file += "/Viewaide/";
path_to_file += inf_file;
QFile(path_to_file).remove();
}
bool UpdateApp::CompareVersions(const QString& new_version)
{
if ( APP_VERSION.toDouble() < new_version.toDouble() )
return true;
else
return false;
}
bool UpdateApp::DownloadAnyFile(const QString& url_path)
{
DownloadFile::GetInstance()->emitDownload(url_path);
}
void UpdateApp::slotDoneLoad(const QString& file_name)
{
QString path_to_file = QDir::homePath();
path_to_file += "/Viewaide/";
path_to_file += file_name;
if ( file_name == inf_file )
{
if ( CheckFile(path_to_file) )
new_version = ParseFile(path_to_file);
else
return;
if ( CompareVersions(new_version.at(0)) )
sigUpdateOrReject();
}
#ifdef Q_OS_WIN
else if ( file_name == url_win_app_file.section('/', -1) )
#endif
#ifdef Q_OS_MAC
else if ( file_name == url_mac_app_file.section('/', -1) )
#endif
{
#ifdef Q_OS_WIN
int result = (int)::ShellExecuteA(0, "open", path_to_file.toUtf8().constData(), 0, 0, SW_SHOWNORMAL);
if (SE_ERR_ACCESSDENIED == result)
{
// Requesting elevation
result = (int)::ShellExecuteA(0, "runas", path_to_file.toUtf8().constData(), 0, 0, SW_SHOWNORMAL);
}
if (result <= 32)
{
// error handling
}
#else
QString cmd = QString("open %1").arg(path_to_file);
if (!QProcess::startDetached(cmd))
{
// error handling
}
#endif
qApp->exit();
}
}
QStringList UpdateApp::ParseFile(const QString& path_file)
{
reg_exp.setMinimal(true);
QStringList parsed_file;
QString str;
QFile file ( path_file );
file.open(QIODevice::ReadOnly);
while ( !file.atEnd() )
{
str = file.readLine();
if ( str.startsWith(inf_file_prefix) )
{
if ( reg_exp.indexIn(str) != -1 )
{
QString str_version = reg_exp.cap(2);
parsed_file.push_back(str_version);
}
}
#ifdef Q_OS_WIN
else if ( str.startsWith(app_win_file_prefix))
{
int ind = str.indexOf(begin_url_new_file);
QString str_url = str.mid(ind);
parsed_file.push_back(str_url);
}
#endif
#ifdef Q_OS_MAC
else if ( str.startsWith(app_mac_file_prefix))
{
int ind = str.indexOf(begin_url_new_file);
QString str_url = str.mid(ind);
parsed_file.push_back(str_url);
}
#endif
}
file.close();
return parsed_file;
}