Session: Add network proxy

pull/21/head
reionwong 4 years ago
parent 2346ed70af
commit 903353d10a

@ -6,6 +6,7 @@ set(SOURCES
main.cpp main.cpp
process.cpp process.cpp
processmanager.cpp processmanager.cpp
networkproxymanager.cpp
powermanager/power.cpp powermanager/power.cpp
powermanager/powerproviders.cpp powermanager/powerproviders.cpp

@ -84,6 +84,7 @@ void setEnvironmentVariable(const QByteArray &name, const QByteArray &value)
Application::Application(int &argc, char **argv) Application::Application(int &argc, char **argv)
: QApplication(argc, argv) : QApplication(argc, argv)
, m_processManager(new ProcessManager(this)) , m_processManager(new ProcessManager(this))
, m_networkProxyManager(new NetworkProxyManager)
, m_wayland(false) , m_wayland(false)
{ {
new SessionAdaptor(this); new SessionAdaptor(this);

@ -23,6 +23,7 @@
#include <QApplication> #include <QApplication>
#include "processmanager.h" #include "processmanager.h"
#include "networkproxymanager.h"
#include "powermanager/power.h" #include "powermanager/power.h"
class Application : public QApplication class Application : public QApplication
@ -35,33 +36,32 @@ public:
bool wayland() const; bool wayland() const;
public slots: public slots:
void logout() void logout() {
{
m_processManager->logout(); m_processManager->logout();
} }
void reboot() void reboot() {
{
m_power.reboot(); m_power.reboot();
QCoreApplication::exit(0); QCoreApplication::exit(0);
} }
void powerOff() void powerOff() {
{
m_power.shutdown(); m_power.shutdown();
QCoreApplication::exit(0); QCoreApplication::exit(0);
} }
void suspend() void suspend() {
{
m_power.suspend(); m_power.suspend();
} }
void startDesktopProcess() void startDesktopProcess() {
{
m_processManager->startDesktopProcess(); m_processManager->startDesktopProcess();
} }
void updateNetworkProxy() {
m_networkProxyManager->update();
}
private: private:
void initEnvironments(); void initEnvironments();
void initLanguage(); void initLanguage();
@ -75,6 +75,7 @@ private:
private: private:
ProcessManager *m_processManager; ProcessManager *m_processManager;
NetworkProxyManager *m_networkProxyManager;
Power m_power; Power m_power;
bool m_wayland; bool m_wayland;

@ -16,5 +16,8 @@
<method name="startDesktopProcess"> <method name="startDesktopProcess">
<annotation name="org.freedesktop.DBus.Method.NoReply" value="true"/> <annotation name="org.freedesktop.DBus.Method.NoReply" value="true"/>
</method> </method>
<method name="updateNetworkProxy">
<annotation name="org.freedesktop.DBus.Method.NoReply" value="true"/>
</method>
</interface> </interface>
</node> </node>

@ -0,0 +1,89 @@
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: Reion Wong <reion@cutefishos.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "networkproxymanager.h"
#include <QDBusPendingCall>
#include <QDBusMessage>
#include <QDBusConnection>
NetworkProxyManager::NetworkProxyManager(QObject *parent)
: QObject(parent)
, m_settings("cutefishos", "network")
{
update();
}
void NetworkProxyManager::update()
{
qunsetenv("HTTP_PROXY");
qunsetenv("FTP_PROXY");
m_settings.sync();
m_flag = m_settings.value("ProxyFlag", 0).toInt();
m_useSameProxy = m_settings.value("UseSameProxy", false).toBool();
m_scriptProxy = m_settings.value("ProxyScriptProxy", "").toString();
m_httpProxy = m_settings.value("HttpProxy", "").toString();
m_ftpProxy = m_settings.value("FtpProxy", "").toString();
m_socksProxy = m_settings.value("SocksProxy", "").toString();
m_httpProxyPort = m_settings.value("HttpProxyPort", "").toString();
m_ftpProxyPort = m_settings.value("FtpProxyPort", "").toString();
m_socksProxyPort = m_settings.value("SocksProxyPort", "").toString();
QMap<QString, QString> dbusActivationEnv;
if (m_flag == 0) {
// No proxy
} else if (m_flag == 1) {
// Use proxy auto configuration URL
} else if (m_flag == 2) {
// Use manually specified proxy configuration
QString httpProxy = QString("http://%1:%2/").arg(m_httpProxy).arg(m_httpProxyPort);
if (!m_httpProxy.isEmpty() && !m_httpProxyPort.isEmpty()) {
qputenv("HTTP_PROXY", httpProxy.toLatin1());
dbusActivationEnv.insert("HTTP_PROXY", httpProxy);
}
if (m_useSameProxy) {
if (!m_ftpProxy.isEmpty() && !m_ftpProxyPort.isEmpty()) {
qputenv("FTP_PROXY", httpProxy.toLatin1());
dbusActivationEnv.insert("FTP_PROXY", httpProxy);
}
} else {
if (!m_ftpProxy.isEmpty() && !m_ftpProxyPort.isEmpty()) {
qputenv("FTP_PROXY", QString("http://%1:%2/").arg(m_ftpProxy).arg(m_ftpProxyPort).toLatin1());
dbusActivationEnv.insert("FTP_PROXY", QString("http://%1:%2/").arg(m_ftpProxy).arg(m_ftpProxyPort).toLatin1());
}
}
// if (!m_socksProxy.isEmpty() && !m_socksProxyPort.isEmpty()) {
// qputenv("ALL_PROXY", QString("socks://%1:%2/").arg(m_socksProxy).arg(m_socksProxyPort).toLatin1());
// }
}
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"),
QStringLiteral("/org/freedesktop/DBus"),
QStringLiteral("org.freedesktop.DBus"),
QStringLiteral("UpdateActivationEnvironment"));
msg.setArguments({QVariant::fromValue(dbusActivationEnv)});
QDBusPendingCall reply = QDBusConnection::sessionBus().asyncCall(msg);
reply.waitForFinished();
}

@ -0,0 +1,51 @@
/*
* Copyright (C) 2021 CutefishOS Team.
*
* Author: Reion Wong <reion@cutefishos.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NETWORKPROXYMANAGER_H
#define NETWORKPROXYMANAGER_H
#include <QObject>
#include <QSettings>
class NetworkProxyManager : public QObject
{
Q_OBJECT
public:
explicit NetworkProxyManager(QObject *parent = nullptr);
void update();
private:
QSettings m_settings;
int m_flag;
bool m_useSameProxy;
QString m_scriptProxy;
QString m_httpProxy;
QString m_ftpProxy;
QString m_socksProxy;
QString m_httpProxyPort;
QString m_ftpProxyPort;
QString m_socksProxyPort;
};
#endif // NETWORKPROXYMANAGER_H
Loading…
Cancel
Save