diff options
Diffstat (limited to 'client/Proxifier.cpp')
-rw-r--r-- | client/Proxifier.cpp | 112 |
1 files changed, 93 insertions, 19 deletions
diff --git a/client/Proxifier.cpp b/client/Proxifier.cpp index e2185b9..cff907c 100644 --- a/client/Proxifier.cpp +++ b/client/Proxifier.cpp @@ -1,4 +1,5 @@ +#include <algorithm> #include <QtCore> #include <QtXml> #include "Logger.h" @@ -215,7 +216,7 @@ bool Proxifier::TurnProxyOff(Proxy& proxy) return false; } // check if proxy do not exist - int proxyId = 0; + int proxyId = 0; if (!isProxyExists(proxy.host, proxy.port, &proxyId)) { return true; @@ -226,7 +227,6 @@ bool Proxifier::TurnProxyOff(Proxy& proxy) return true; } // try to find proxy in chain - bool isFound = false; for (unsigned i = 0; i < chainList.size(); i++) { if (chainList[i].name.compare(defaultChain) != 0) @@ -237,7 +237,6 @@ bool Proxifier::TurnProxyOff(Proxy& proxy) map<int, bool>::iterator it = chainList[i].proxies.find(proxyId); if (it != chainList[i].proxies.end()) { - isFound = true; // if it was the last proxy in chain - change default rule if (chainList[i].proxies.size() == 1) { @@ -245,19 +244,12 @@ bool Proxifier::TurnProxyOff(Proxy& proxy) SetRuleAction(defaultRule, action); } RemoveProxyFromChain(proxyId, chainId); - /** - * @todo remove proxy entry from Proxifier's config - */ + RemoveProxy(proxyId); // update config to reflect last changes ReadConfig(); break; } } - if (!isFound) - { - return true; - } - return true; } @@ -535,22 +527,32 @@ int Proxifier::GetNextId() { return -1; } - int max = 100; // first default proxifier ID + // acquire all IDs, sort them and return max or spare + vector<int> ids; for (unsigned i = 0; i < proxyList.size(); i++) { - if (proxyList[i].id > max) - { - max = proxyList[i].id; - } + ids.push_back(proxyList[i].id); } for (unsigned i = 0; i < chainList.size(); i++) { - if (chainList[i].id > max) + ids.push_back(chainList[i].id); + } + if (ids.size() == 0) + { + // return default id if there is no one present + return 100; + } + sort(ids.begin(), ids.end()); + int last = ids[0]; + for (unsigned i = 0; i < ids.size(); i++) + { + if (ids[i] - last > 1) { - max = chainList[i].id; + return last + 1; } + last = ids[i]; } - return max + 1; + return last + 1; } bool Proxifier::AddProxy(ProxifierProxy& proxy, int* id) @@ -648,6 +650,78 @@ bool Proxifier::AddProxy(ProxifierProxy& proxy, int* id) return true; } +bool Proxifier::RemoveProxy(int proxyId) +{ + Logger::Trace("Adding new proxy to Proxifier config\n"); + // read XML DOM structure + QDomDocument configDom("config"); + QFile* config = new QFile(filename); + if (!config->open(QIODevice::ReadOnly)) + { + Logger::Error("Can't open Proxifier config file for reading\n"); + return false; + } + if (!configDom.setContent(config)) + { + Logger::Fatal("Invalid Proxifier config file!\n"); + return false; + } + config->close(); + delete config; + + QDomElement root = configDom.documentElement(); + if (root.tagName() != "ProxifierProfile") + { + Logger::Error("Invalid Proxifier config file!\n"); + return false; + } + QDomElement proxyListElem = root.firstChildElement("ProxyList"); + if (proxyListElem.isNull()) + { + return false; + } + // find proxy record + QDomElement proxyElem = proxyListElem.firstChildElement("Proxy"); + while (!proxyElem.isNull()) + { + bool ok; + if (!proxyElem.hasAttribute("id")) + { + Logger::Error("Invalid Proxy entry\n"); + proxyElem = proxyElem.nextSiblingElement("Proxy"); + continue; + } + QString idStr = proxyElem.attribute("id"); + int id = idStr.toInt(&ok); + if (!ok) + { + Logger::Error("Invalid Proxy entry\n"); + proxyElem = proxyElem.nextSiblingElement("Proxy"); + continue; + } + if (id == proxyId) + { + proxyListElem.removeChild(proxyElem); + break; + } + proxyElem = proxyElem.nextSiblingElement("Proxy"); + } + // update state to reflect all the changes + ReadProxyList(root); + + // save new DOM to file + config = new QFile(filename); + if (!config->open(QIODevice::WriteOnly)) + { + Logger::Error("Can't open Proxifier config file for writing\n"); + return false; + } + QTextStream(config) << configDom.toString(indent); + config->close(); + delete config; + return true; +} + bool Proxifier::AddChain(string& name, int *id) { Logger::Trace("Adding new chain to Proxifier config\n"); |