diff options
author | Fabian Vogt <fvogt@suse.com> | 2016-01-12 15:43:48 +0100 |
---|---|---|
committer | Fabian Vogt <fvogt@suse.com> | 2016-01-12 15:58:30 +0100 |
commit | bd9bf72e15107e6c6133b438c700dd42d79c9d60 (patch) | |
tree | b502d1b4dcec7154065474e7e824120a1d3bdbf7 | |
parent | 922b828214181a10d8bdb01ccbcf4199fd24176a (diff) | |
download | libproxy-git-bd9bf72e15107e6c6133b438c700dd42d79c9d60.tar.gz |
Add generic KDE config module
- Does not depend on Qt, uses kreadconfig/kreadconfig5
-rw-r--r-- | libproxy/cmake/modules.cmk | 4 | ||||
-rw-r--r-- | libproxy/cmake/modules/config_kde.cmk | 6 | ||||
-rw-r--r-- | libproxy/cmake/modules/config_kde4.cmk | 19 | ||||
-rw-r--r-- | libproxy/modules/config_kde.cpp | 147 | ||||
-rw-r--r-- | libproxy/modules/config_kde4.cpp | 108 |
5 files changed, 155 insertions, 129 deletions
diff --git a/libproxy/cmake/modules.cmk b/libproxy/cmake/modules.cmk index 68ff898..891cb07 100644 --- a/libproxy/cmake/modules.cmk +++ b/libproxy/cmake/modules.cmk @@ -11,7 +11,7 @@ include(cmake/pkgconfig.cmk) include(cmake/modules/config_envvar.cmk) include(cmake/modules/config_sysconfig.cmk) include(cmake/modules/config_gnome.cmk) -include(cmake/modules/config_kde4.cmk) +include(cmake/modules/config_kde.cmk) include(cmake/modules/config_macosx.cmk) include(cmake/modules/network_networkmanager.cmk) include(cmake/modules/pacrunner_mozjs.cmk) @@ -32,7 +32,7 @@ px_module(config_envvar "${ENVVAR_FOUND}" 1) px_module(config_sysconfig "${SYSCONFIG_FOUND}" 1) px_module(config_gnome "${GNOME2_FOUND}" 0) px_module(config_gnome3 "${GNOME3_FOUND}" 0) -px_module(config_kde4 "${KDE4_FOUND}" 0 ${KDE4_LIBRARIES}) +px_module(config_kde "${KDE_FOUND}" 0) px_module(config_macosx "${SC_FOUND}" 1 ${SC_LIBRARIES} ${CF_LIBRARIES}) px_module(config_w32reg "${WIN32}" 1) px_module(ignore_domain 1 1) diff --git a/libproxy/cmake/modules/config_kde.cmk b/libproxy/cmake/modules/config_kde.cmk new file mode 100644 index 0000000..d0a66ef --- /dev/null +++ b/libproxy/cmake/modules/config_kde.cmk @@ -0,0 +1,6 @@ +find_program(KDE4_CONF kreadconfig) +find_program(KF5_CONF kreadconfig5) + +if (KDE4_CONF OR KF5_CONF) + set(KDE_FOUND 1) +endif() diff --git a/libproxy/cmake/modules/config_kde4.cmk b/libproxy/cmake/modules/config_kde4.cmk deleted file mode 100644 index 0bf3587..0000000 --- a/libproxy/cmake/modules/config_kde4.cmk +++ /dev/null @@ -1,19 +0,0 @@ -if (NOT WIN32 AND NOT APPLE) - option(WITH_KDE4 "Search for KDE4 package" ON) - if (WITH_KDE4) - find_package(KDE4) - find_package(Qt4) - if(KDE4_FOUND AND QT4_FOUND) - include(KDE4Defaults) - set(KDE4_FOUND 1) - set(KDE4_LIBRARIES ${KDE4_KDECORE_LIBS} ${QT_LIBRARIES}) - include_directories(${KDE4_INCLUDES} ${QT_INCLUDES}) - link_directories(${KDE_LIB_DIR}) - string(REGEX REPLACE " *-fno-exceptions" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) - else() - set(KDE4_FOUND 0) - endif() - else() - set(KDE4_FOUND 0) - endif() -endif()
\ No newline at end of file diff --git a/libproxy/modules/config_kde.cpp b/libproxy/modules/config_kde.cpp new file mode 100644 index 0000000..2211487 --- /dev/null +++ b/libproxy/modules/config_kde.cpp @@ -0,0 +1,147 @@ +/******************************************************************************* + * libproxy - A library for proxy configuration + * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com> + * Copyright (C) 2016 Fabian Vogt <fvogt@suse.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + ******************************************************************************/ + +#include <algorithm> +#include <cstdlib> +#include <cstdio> + +#include "../extension_config.hpp" +using namespace libproxy; + +class kde_config_extension : public config_extension { +public: + kde_config_extension() + { + try { + // Try the KF5 one first + command = "kreadconfig5"; + kde_config_val("proxyType", "-1"); + return; // Worked + } + catch(...) {} + + try { + // The KDE4 one next + command = "kreadconfig"; + kde_config_val("proxyType", "-1"); + return; // Worked + } + catch(...) {} + + // Neither worked, so throw in get_config + command = ""; + } + + vector<url> get_config(const url &dst) throw (runtime_error) { + // See constructor + if(command.empty()) + throw runtime_error("Unable to read configuration"); + + vector<url> response; + + string tmp, proxyType = kde_config_val("ProxyType", "-1"); + + // Just switch on the first byte, either a digit, '-' ("-1") or '\0' + switch(proxyType.c_str()[0]) + { + case '1': + tmp = kde_config_val(dst.get_scheme() + "Proxy", ""); + if(tmp.empty()) { + tmp = kde_config_val("httpProxy", ""); + if(tmp.empty()) { + tmp = kde_config_val("socksProxy", ""); + if(tmp.empty()) + tmp = "direct://"; + } + } + + // KDE uses "http://127.0.0.1 8080" instead of "http://127.0.0.1:8080" + replace(tmp.begin(), tmp.end(), ' ', ':'); + + response.push_back(url(tmp)); + break; + + case '2': + tmp = "pac+" + kde_config_val("Proxy Config Script", ""); + if (url::is_valid(tmp)) + { + response.push_back(url(tmp)); + break; + } + // else fallthrough + + case '3': + response.push_back(url(string("wpad://"))); + break; + + case '4': + throw runtime_error("User config_envvar"); // We'll bypass this config plugin and let the envvar plugin wor + + case '0': + default: // Not set or unknown/illegal + response.push_back(url("direct://")); + break; + } + + return response; + } + + string get_ignore(const url&) { + // See constructor + if(command.empty()) + return ""; + + string proxyType = kde_config_val("ProxyType", "-1"); + if(proxyType.c_str()[0] != '1') + return ""; // Not manual config + + string prefix = kde_config_val("ReversedException", "false") != "false" ? "-" : ""; + return prefix + kde_config_val("NoProxyFor", ""); // Already in the right format + } + +private: + // Neither key nor def must contain ' + string kde_config_val(const string &key, const string &def) throw (runtime_error) { + string cmdline = + command + " --file kioslaverc --group 'Proxy Settings' --key '" + key + "' --default '" + def + "'"; + + FILE *pipe = popen(cmdline.c_str(), "r"); + if (!pipe) + throw runtime_error("Unable to run command"); + + char buffer[128]; + string result = ""; + while (!feof(pipe)) { + if (fgets(buffer, 128, pipe) != NULL) + result += buffer; // TODO: If this throws bad_alloc, pipe is leaked + } + + pclose(pipe); + + // Trim newlines and whitespace at end + result.erase(result.begin() + (result.find_last_not_of(" \n\t")+1), result.end()); + return result; + } + + // Whether to use kreadconfig or kreadconfig5 + string command; +}; + +MM_MODULE_INIT_EZ(kde_config_extension, getenv("KDE_FULL_SESSION"), NULL, NULL); diff --git a/libproxy/modules/config_kde4.cpp b/libproxy/modules/config_kde4.cpp deleted file mode 100644 index f23320d..0000000 --- a/libproxy/modules/config_kde4.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/******************************************************************************* - * libproxy - A library for proxy configuration - * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - ******************************************************************************/ - -#include <KDE/KConfig> -#include <KDE/KConfigGroup> -#include <KDE/KGlobal> - -#include "../extension_config.hpp" -using namespace libproxy; - -static void dummyMessageHandler(QtMsgType, const char *) {} - -class kde_config_extension : public config_extension { -public: - kde_config_extension() { - /* The constructor of KConfig uses qAppName() which asumes a QApplication object to exist. - If not, an error message is written. This error message and all others seems to be disabled for - libraries, but to be sure, we can reemplace temporaly Qt's internal message handler by a - dummy implementation. */ - - // Open the config file - QtMsgHandler oldHandler = qInstallMsgHandler(dummyMessageHandler); - this->cfg = new KConfig("kioslaverc", KConfig::NoGlobals); - this->grp = new KConfigGroup(this->cfg, "Proxy Settings"); - qInstallMsgHandler(oldHandler); - } - - ~kde_config_extension() { - delete this->grp; - delete this->cfg; - } - - vector<url> get_config(const url &dst) throw (runtime_error) { - string tmp; - QString prxy; - vector<url> response; - - switch (this->grp->readEntry("ProxyType", 0)) { - case 1: // Use a manual proxy - prxy = this->grp->readEntry(QString(dst.get_scheme().c_str()) + "Proxy", ""); - if (prxy.isEmpty()) { - prxy = this->grp->readEntry("httpProxy", ""); - if (prxy.isEmpty()) { - prxy = this->grp->readEntry("socksProxy", ""); - if (prxy.isEmpty()) - prxy = "direct://"; - }; - }; - // The result of toLatin1() is undefined for non-Latin1 strings. - // However, KDE saves this entry using IDN and percent-encoding, so no problem... - response.push_back(string(prxy.toLatin1().data())); - break; - case 2: // Use a manual PAC - // The result of toLatin1() is undefined for non-Latin1 strings. - // However, KDE saves this entry using IDN and percent-encoding, so no problem... - tmp = string(this->grp->readEntry("Proxy Config Script", "").toLatin1().data()); - if (url::is_valid("pac+" + tmp)) - response.push_back(url("pac+" + tmp)); - else - response.push_back(string("wpad://")); - break; - case 3: // Use WPAD - response.push_back(string("wpad://")); - break; - case 4: // Use envvar - throw runtime_error("User config_envvar"); // We'll bypass this config plugin and let the envvar plugin work - default: - response.push_back(url("direct://")); - break; - }; - - return response; - } - - string get_ignore(const url& /*dst*/) { - // Apply ignore list only for manual proxy configuration - if (this->grp->readEntry("ProxyType", 0) == 1) { - string prefix = this->grp->readEntry("ReversedException", false) ? "-" : ""; - QStringList list = this->grp->readEntry("NoProxyFor", QStringList()); - for (int i = 0; i < list.size(); ++i) - list[i] = QUrl(list.at(i)).toEncoded(); - return prefix + string(list.join(",").toLatin1().data()); - } - return ""; - } - -private: - KConfig* cfg; - KConfigGroup* grp; -}; - -MM_MODULE_INIT_EZ(kde_config_extension, getenv("KDE_FULL_SESSION"), NULL, NULL); |