Skip to content

Commit 5e2d7d6

Browse files
authored
MI0698-1385 Add cli proxy parameters and parsing
1 parent d880d89 commit 5e2d7d6

File tree

7 files changed

+337
-1
lines changed

7 files changed

+337
-1
lines changed

src/libs/installer/commandlineparser.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,33 @@ CommandLineParser::CommandLineParser()
120120
<< CommandLineOptions::scNoProxyShort << CommandLineOptions::scNoProxyLong,
121121
QLatin1String("Do not use system proxy.")));
122122

123+
// Starting mode options
124+
addOption(QCommandLineOption(QStringList()
125+
<< CommandLineOptions::scManualProxyShort << CommandLineOptions::scManualProxyLong,
126+
QLatin1String("Use manual proxy.")));
127+
128+
// Custom Proxy options
129+
addOption(QCommandLineOption(QStringList()
130+
<< CommandLineOptions::scHttpProxyHostNameShort << CommandLineOptions::scHttpProxyHostNameLong,
131+
QLatin1String("Use Http proxy."),
132+
QLatin1String("ProxyName")));
133+
addOption(QCommandLineOption(QStringList()
134+
<< CommandLineOptions::scFtpProxyHostNameShort << CommandLineOptions::scFtpProxyHostNameLong,
135+
QLatin1String("Use Ftp proxy."),
136+
QLatin1String("ProxyName")));
137+
addOption(QCommandLineOption(QStringList()
138+
<< CommandLineOptions::scPortIdShort << CommandLineOptions::scPortIdLong,
139+
QLatin1String("Port id."),
140+
QLatin1String("PortId")));
141+
addOption(QCommandLineOption(QStringList()
142+
<< CommandLineOptions::scProxyUserNameShort << CommandLineOptions::scProxyUserNameLong,
143+
QLatin1String("Proxy Username."),
144+
QLatin1String("Username")));
145+
addOption(QCommandLineOption(QStringList()
146+
<< CommandLineOptions::scProxyPasswordShort << CommandLineOptions::scProxyPasswordLong,
147+
QLatin1String("Proxy password."),
148+
QLatin1String("Password")));
149+
123150
// Starting mode options
124151
addOption(QCommandLineOption(QStringList()
125152
<< CommandLineOptions::scStartUpdaterShort << CommandLineOptions::scStartUpdaterLong,

src/libs/installer/constants.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,20 @@ static const QLatin1String scSystemProxyShort("sp");
235235
static const QLatin1String scSystemProxyLong("system-proxy");
236236
static const QLatin1String scNoProxyShort("np");
237237
static const QLatin1String scNoProxyLong("no-proxy");
238+
static const QLatin1String scManualProxyShort("mp");
239+
static const QLatin1String scManualProxyLong("manual-proxy");
240+
241+
// Custom Proxy options
242+
static const QLatin1String scHttpProxyHostNameShort("hph");
243+
static const QLatin1String scHttpProxyHostNameLong("http-proxy-host");
244+
static const QLatin1String scFtpProxyHostNameShort("fph");
245+
static const QLatin1String scFtpProxyHostNameLong("ftp-proxy-host");
246+
static const QLatin1String scPortIdShort("pi");
247+
static const QLatin1String scPortIdLong("port-id");
248+
static const QLatin1String scProxyUserNameShort("un");
249+
static const QLatin1String scProxyUserNameLong("user-name");
250+
static const QLatin1String scProxyPasswordShort("pp");
251+
static const QLatin1String scProxyPasswordLong("proxy-password");
238252

239253
// Starting mode options
240254
static const QLatin1String scStartUpdaterShort("su");

src/libs/installer/packagemanagercore.cpp

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3851,6 +3851,194 @@ QString PackageManagerCore::value(const QString &key, const QString &defaultValu
38513851
return d->m_data.value(key, defaultValue, static_cast<QSettings::Format>(format)).toString();
38523852
}
38533853

3854+
QString PackageManagerCore::getProxyMode() const
3855+
{
3856+
const Settings &settings = d->m_data.settings();
3857+
switch (settings.proxyType()) {
3858+
case Settings::NoProxy:
3859+
return QStringLiteral("no");
3860+
break;
3861+
case Settings::SystemProxy:
3862+
return QStringLiteral("system");
3863+
break;
3864+
case Settings::UserDefinedProxy:
3865+
return QStringLiteral("manual");
3866+
break;
3867+
}
3868+
}
3869+
3870+
void PackageManagerCore::setProxyMode(const QString &proxyType)
3871+
{
3872+
if(proxyType == QStringLiteral("no"))
3873+
{
3874+
d->m_data.settings().setProxyType(QInstaller::Settings::NoProxy);
3875+
}
3876+
else if(proxyType == QStringLiteral("system"))
3877+
{
3878+
d->m_data.settings().setProxyType(QInstaller::Settings::SystemProxy);
3879+
}
3880+
else if(proxyType == QStringLiteral("manual"))
3881+
{
3882+
d->m_data.settings().setProxyType(QInstaller::Settings::UserDefinedProxy);
3883+
}
3884+
else
3885+
{
3886+
qWarning() << "proxy mode does not exist";
3887+
}
3888+
3889+
}
3890+
3891+
QString PackageManagerCore::getHttpProxyHost() const
3892+
{
3893+
QNetworkProxy proxy = d->m_data.settings().httpProxy();
3894+
qInfo() << "installer value: proxy hostname" << proxy.hostName();
3895+
3896+
return proxy.hostName();
3897+
}
3898+
3899+
void PackageManagerCore::setHttpProxyHost(const QString &hostName)
3900+
{
3901+
QNetworkProxy proxy = d->m_data.settings().httpProxy();
3902+
proxy.setHostName(hostName);
3903+
qInfo() << "set proxy hostname" << proxy.hostName();
3904+
}
3905+
3906+
QString PackageManagerCore::getHttpProxyPort() const
3907+
{
3908+
QNetworkProxy proxy = d->m_data.settings().httpProxy();
3909+
qInfo() << "installer value: proxy port" << proxy.port();
3910+
3911+
return QString::number(proxy.port());
3912+
}
3913+
3914+
void PackageManagerCore::setHttpProxyPort(const QString &port)
3915+
{
3916+
QNetworkProxy proxy = d->m_data.settings().httpProxy();
3917+
bool boolVal;
3918+
proxy.setPort(port.toInt(&boolVal));
3919+
qInfo() << "set proxy port" << proxy.port();
3920+
}
3921+
3922+
QString PackageManagerCore::getHttpProxyUser() const
3923+
{
3924+
QNetworkProxy proxy = d->m_data.settings().httpProxy();
3925+
qInfo() << "installer value: proxy user" << proxy.user();
3926+
3927+
return proxy.user();
3928+
}
3929+
3930+
void PackageManagerCore::setHttpProxyUser(const QString &userName)
3931+
{
3932+
QNetworkProxy proxy = d->m_data.settings().httpProxy();
3933+
proxy.setUser(userName);
3934+
qInfo() << "set proxy port" << proxy.user();
3935+
}
3936+
3937+
QString PackageManagerCore::getHttpProxyPwd() const
3938+
{
3939+
QNetworkProxy proxy = d->m_data.settings().httpProxy();
3940+
qInfo() << "installer value: proxy password" << proxy.password();
3941+
3942+
return proxy.password();
3943+
}
3944+
3945+
void PackageManagerCore::setHttpProxyPwd(const QString &password)
3946+
{
3947+
QNetworkProxy proxy = d->m_data.settings().httpProxy();
3948+
proxy.setPassword(password);
3949+
qInfo() << "set proxy port" << proxy.password();
3950+
}
3951+
3952+
bool PackageManagerCore::getHttpProxyAuth() const
3953+
{
3954+
QString userStr = getHttpProxyUser();
3955+
QString pwdStr = getHttpProxyPwd();
3956+
if(userStr.isEmpty() || pwdStr.isEmpty())
3957+
{
3958+
return false;
3959+
}
3960+
else
3961+
{
3962+
return true;
3963+
}
3964+
3965+
}
3966+
3967+
QString PackageManagerCore::getFtpProxyHost() const
3968+
{
3969+
QNetworkProxy proxy = d->m_data.settings().ftpProxy();
3970+
qInfo() << "installer value: proxy hostname" << proxy.hostName();
3971+
3972+
return proxy.hostName();
3973+
}
3974+
3975+
void PackageManagerCore::setFtpProxyHost(const QString &hostName)
3976+
{
3977+
QNetworkProxy proxy = d->m_data.settings().ftpProxy();
3978+
proxy.setHostName(hostName);
3979+
qInfo() << "set proxy hostname" << proxy.hostName();
3980+
}
3981+
3982+
QString PackageManagerCore::getFtpProxyPort() const
3983+
{
3984+
QNetworkProxy proxy = d->m_data.settings().ftpProxy();
3985+
qInfo() << "installer value: proxy port" << proxy.port();
3986+
3987+
return QString::number(proxy.port());
3988+
}
3989+
3990+
void PackageManagerCore::setFtpProxyPort(const QString &port)
3991+
{
3992+
QNetworkProxy proxy = d->m_data.settings().ftpProxy();
3993+
bool boolVal;
3994+
proxy.setPort(port.toInt(&boolVal));
3995+
qInfo() << "set proxy port" << proxy.port();
3996+
}
3997+
3998+
QString PackageManagerCore::getFtpProxyUser() const
3999+
{
4000+
QNetworkProxy proxy = d->m_data.settings().ftpProxy();
4001+
qInfo() << "installer value: proxy user" << proxy.user();
4002+
4003+
return proxy.user();
4004+
}
4005+
4006+
void PackageManagerCore::setFtpProxyUser(const QString &userName)
4007+
{
4008+
QNetworkProxy proxy = d->m_data.settings().ftpProxy();
4009+
proxy.setUser(userName);
4010+
qInfo() << "set proxy port" << proxy.user();
4011+
}
4012+
4013+
QString PackageManagerCore::getFtpProxyPwd() const
4014+
{
4015+
QNetworkProxy proxy = d->m_data.settings().ftpProxy();
4016+
qInfo() << "installer value: proxy password" << proxy.password();
4017+
4018+
return proxy.password();
4019+
}
4020+
4021+
void PackageManagerCore::setFtpProxyPwd(const QString &password)
4022+
{
4023+
QNetworkProxy proxy = d->m_data.settings().ftpProxy();
4024+
proxy.setPassword(password);
4025+
qInfo() << "set proxy port" << proxy.password();
4026+
}
4027+
4028+
bool PackageManagerCore::getFtpProxyAuth() const
4029+
{
4030+
QString userStr = getFtpProxyUser();
4031+
QString pwdStr = getFtpProxyPwd();
4032+
if(userStr.isEmpty() || pwdStr.isEmpty())
4033+
{
4034+
return false;
4035+
}
4036+
else
4037+
{
4038+
return true;
4039+
}
4040+
}
4041+
38544042
/*!
38554043
Returns the installer value for \a key. If \a key is not known to the system, \a defaultValue is
38564044
returned. Additionally, on Windows, \a key can be a registry key.

src/libs/installer/packagemanagercore.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ class INSTALLER_EXPORT PackageManagerCore : public QObject
190190
Q_INVOKABLE bool containsValue(const QString &key) const;
191191
Q_INVOKABLE void setValue(const QString &key, const QString &value);
192192
Q_INVOKABLE QString value(const QString &key, const QString &defaultValue = QString(), const int &format = QSettings::NativeFormat) const;
193+
Q_INVOKABLE QString getProxyMode() const;
194+
Q_INVOKABLE void setProxyMode(const QString &proxyType);
195+
Q_INVOKABLE QString getHttpProxyHost() const;
196+
Q_INVOKABLE void setHttpProxyHost(const QString &hostName);
197+
Q_INVOKABLE QString getHttpProxyPort() const;
198+
Q_INVOKABLE void setHttpProxyPort(const QString &port);
199+
Q_INVOKABLE QString getHttpProxyUser() const;
200+
Q_INVOKABLE void setHttpProxyUser(const QString &userName);
201+
Q_INVOKABLE QString getHttpProxyPwd() const;
202+
Q_INVOKABLE void setHttpProxyPwd(const QString &password);
203+
Q_INVOKABLE bool getHttpProxyAuth() const;
204+
205+
Q_INVOKABLE QString getFtpProxyHost() const;
206+
Q_INVOKABLE void setFtpProxyHost(const QString &hostName);
207+
Q_INVOKABLE QString getFtpProxyPort() const;
208+
Q_INVOKABLE void setFtpProxyPort(const QString &port);
209+
Q_INVOKABLE QString getFtpProxyUser() const;
210+
Q_INVOKABLE void setFtpProxyUser(const QString &userName);
211+
Q_INVOKABLE QString getFtpProxyPwd() const;
212+
Q_INVOKABLE void setFtpProxyPwd(const QString &password);
213+
Q_INVOKABLE bool getFtpProxyAuth() const;
193214
Q_INVOKABLE QStringList values(const QString &key, const QStringList &defaultValue = QStringList()) const;
194215
Q_INVOKABLE QString key(const QString &value) const;
195216

src/sdk/main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ int main(int argc, char *argv[])
164164
<< CommandLineOptions::scStartServerLong
165165
<< CommandLineOptions::scStartClientLong);
166166
}
167+
168+
if (mutually.isEmpty()) {
169+
mutually = QInstaller::checkMutualOptions(parser, QStringList()
170+
<< CommandLineOptions::scHttpProxyHostNameLong
171+
<< CommandLineOptions::scFtpProxyHostNameLong);
172+
}
173+
167174
if (!mutually.isEmpty()) {
168175
sanityMessage = QString::fromLatin1("The following options are mutually exclusive: %1.")
169176
.arg(mutually.join(QLatin1String(", ")));
@@ -309,8 +316,12 @@ int main(int argc, char *argv[])
309316
// Make sure we honor the system's proxy settings
310317
QNetworkProxyFactory::setUseSystemConfiguration(true);
311318
}
319+
else //make sure the system config set to false when no proxy para passed otherwise it may interfere with settings read from network.xml
320+
{
321+
QNetworkProxyFactory::setUseSystemConfiguration(false);
322+
}
312323

313-
if (parser.isSet(CommandLineOptions::scNoProxyLong))
324+
if ((parser.isSet(CommandLineOptions::scNoProxyLong)) || (parser.isSet(CommandLineOptions::scManualProxyLong)))
314325
QNetworkProxyFactory::setUseSystemConfiguration(false);
315326

316327
const SelfRestarter restarter(argc, argv);

src/sdk/sdkapp.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,71 @@ class SDKApp : public T
272272
} else if (QNetworkProxyFactory::usesSystemConfiguration()) {
273273
m_core->settings().setProxyType(QInstaller::Settings::SystemProxy);
274274
KDUpdater::FileDownloaderFactory::instance().setProxyFactory(m_core->proxyFactory());
275+
} else if(m_parser.isSet(CommandLineOptions::scManualProxyLong)) {
276+
m_core->settings().setProxyType(QInstaller::Settings::UserDefinedProxy);
277+
// get proxy type, name, port
278+
if ((m_parser.isSet(CommandLineOptions::scHttpProxyHostNameLong)) && (m_parser.isSet(CommandLineOptions::scPortIdLong))) {
279+
const QString httpProxyName = m_parser.value(CommandLineOptions::scHttpProxyHostNameLong);
280+
bool isValid;
281+
const quint16 portId = m_parser.value(CommandLineOptions::scPortIdLong).toInt(&isValid);
282+
if(httpProxyName.isEmpty())
283+
{
284+
errorMessage = QObject::tr("Manual proxy name empty.");
285+
return false;
286+
}
287+
if(isValid == false)
288+
{
289+
errorMessage = QObject::tr("Manual proxy portId empty.");
290+
return false;
291+
}
292+
m_core->settings().setHttpProxy(QNetworkProxy(QNetworkProxy::HttpProxy, httpProxyName, portId));
293+
KDUpdater::FileDownloaderFactory::instance().setProxyFactory(m_core->proxyFactory());
294+
295+
// get username & password
296+
if ((m_parser.isSet(CommandLineOptions::scProxyUserNameLong)) && (m_parser.isSet(CommandLineOptions::scProxyPasswordLong))) {
297+
const QString userName = m_parser.value(CommandLineOptions::scProxyUserNameLong);
298+
const QString password = m_parser.value(CommandLineOptions::scProxyPasswordLong);
299+
QNetworkProxy setParaProxy = m_core->settings().httpProxy();
300+
setParaProxy.setUser(userName);
301+
setParaProxy.setPassword(password);
302+
m_core->settings().setHttpProxy(setParaProxy); //have to reset the QNetworkProxy, for the value stored in settings' m_data is a copy,it won't change
303+
m_core->proxyFactory()->setProxyCredentials(m_core->settings().httpProxy(),userName,password);
304+
}
305+
//if username & password not specified, it will require it in AuthenticationRequiredException, so not return false here if username/password not set
306+
307+
}else if((m_parser.isSet(CommandLineOptions::scFtpProxyHostNameLong))&& (m_parser.isSet(CommandLineOptions::scPortIdLong))){
308+
const QString ftpProxyName = m_parser.value(CommandLineOptions::scFtpProxyHostNameLong);
309+
bool isValid;
310+
const quint16 portId = m_parser.value(CommandLineOptions::scPortIdLong).toInt(&isValid);
311+
if(ftpProxyName.isEmpty())
312+
{
313+
errorMessage = QObject::tr("Manual proxy name empty.");
314+
return false;
315+
}
316+
if(isValid == false)
317+
{
318+
errorMessage = QObject::tr("Manual proxy portId empty.");
319+
return false;
320+
}
321+
m_core->settings().setFtpProxy(QNetworkProxy(QNetworkProxy::HttpProxy, ftpProxyName, portId));
322+
KDUpdater::FileDownloaderFactory::instance().setProxyFactory(m_core->proxyFactory());
323+
324+
// get username & password
325+
if ((m_parser.isSet(CommandLineOptions::scProxyUserNameLong)) && (m_parser.isSet(CommandLineOptions::scProxyPasswordLong))) {
326+
const QString userName = m_parser.value(CommandLineOptions::scProxyUserNameLong);
327+
const QString password = m_parser.value(CommandLineOptions::scProxyPasswordLong);
328+
QNetworkProxy setParaProxy = m_core->settings().ftpProxy();
329+
setParaProxy.setUser(userName);
330+
setParaProxy.setPassword(password);
331+
m_core->settings().setFtpProxy(setParaProxy); //have to reset the QNetworkProxy, for the value stored in settings' m_data is a copy,it won't change
332+
m_core->proxyFactory()->setProxyCredentials(m_core->settings().ftpProxy(),userName,password);
333+
}
334+
335+
}else {
336+
errorMessage = QObject::tr("Manual proxy name and portId need to be specifed.");
337+
return false;
338+
}
339+
275340
}
276341

277342
if (m_parser.isSet(CommandLineOptions::scLocalCachePathLong)) {

src/sdk/tabcontroller.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ TabController::TabController(QObject *parent)
8686

8787
TabController::~TabController()
8888
{
89+
//try writing config file before writeMaintenanceTool 250324
90+
bool gainedAdminRights = false;
91+
if (!d->m_core->directoryWritable(d->m_core->value(scTargetDir))) {
92+
d->m_core->gainAdminRights();
93+
gainedAdminRights = true;
94+
}
95+
d->m_core->writeMaintenanceConfigFiles();
96+
if (gainedAdminRights)
97+
d->m_core->dropAdminRights();
98+
8999
d->m_core->writeMaintenanceTool();
90100
delete d;
91101
}

0 commit comments

Comments
 (0)