proxyinfo.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2008-2010  Lukas Sommer < SommerLuk at gmail dot com >
00003 
00004     This program is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU General Public License as
00006     published by the Free Software Foundation; either version 2 of
00007     the License or (at your option) version 3 or any later version
00008     accepted by the membership of KDE e.V. (or its successor approved
00009     by the membership of KDE e.V.), which shall act as a proxy
00010     defined in Section 14 of version 3 of the license.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 #include "proxyinfo.h"
00022 #include "settings_general.h"
00023 #include <KUrl>
00024 #include <QByteArray>
00025 
00026 // defination of static member (in the class declaration in header file, it is
00027 //                              only declared, not defined)
00028 proxyinfo proxyinfo::internal_instance;
00029 
00030 proxyinfo::proxyinfo()
00031 {
00032 #ifdef LIBPROXY_FOUND
00033   libproxy_factory = px_proxy_factory_new();
00034 #endif
00035 }
00036 
00037 proxyinfo::~proxyinfo()
00038 {
00039 #ifdef LIBPROXY_FOUND
00040   px_proxy_factory_free(libproxy_factory);
00041 #endif
00042 }
00043 
00044 QStringList proxyinfo::proxyserver(const QString & url)
00045 {
00046   return proxyinfo::internal_instance.internal_proxyserver(url);
00047 }
00048 
00049 #ifdef LIBPROXY_FOUND
00050 QStringList proxyinfo::internal_proxyserver(const QString & url) const
00051 {
00052   if (settings_general::proxyConfiguration() ==
00053       settings_general::EnumProxyConfiguration::automatic) {
00054     QStringList returnValue;
00055     // C string of the encoded (domain in punycode, rest is percent-encoded) ASCII-only URL
00056     QByteArray c_encoded_url = KUrl(url).toEncoded();
00057     // get proxy list (A NULL-terminated array of strings) from libproxy
00058     char **proxies = px_proxy_factory_get_proxies(proxyinfo::libproxy_factory,
00059                                                   c_encoded_url.data());
00060     // construct the QStringList from this array
00061     for (int i=0; proxies[i]; ++i) {
00062       returnValue.append(proxies[i]);
00063     };
00064     if (returnValue.size() < 1) {
00065       // makes sure that there is at least 1 entry, without relaing on the external lib for this
00066       returnValue.append("direct://");
00067     };
00068     // free the array
00069     for (int i=0; proxies[i]; ++i) {
00070       free(proxies[i]);
00071     };
00072     free(proxies);
00073     return returnValue;
00074   } else {
00075     return manual_proxyserver();
00076   };
00077 }
00078 #else
00079 QStringList proxyinfo::internal_proxyserver(const QString &) const
00080 {
00081   return manual_proxyserver();
00082 }
00083 #endif
00084 
00085 QStringList proxyinfo::manual_proxyserver() const
00086 {
00087   QStringList returnValue;
00088   QString temp_server = settings_general::proxyServer();
00089   if (temp_server.isEmpty()) {
00090     returnValue.append("direct://");
00091   } else {
00092     returnValue.append(temp_server);
00093   };
00094   return returnValue;
00095 }

doxygen