1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
proxy.cpp
Copyright (c) 2011 by Evgeny Khryukin
***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
*/
#include <QApplication>
#include <QByteArray>
#include "options.h"
#include "applicationinfoaccessinghost.h"
#include "optionaccessinghost.h"
static const QString passwordKey = "yandexnarodpluginkey";
Options * Options ::instance_ = 0;
Options ::Options ()
: QObject(QApplication::instance())
, appInfo(0)
, options(0)
{
}
Options ::~Options()
{
}
void Options ::setApplicationInfoAccessingHost(ApplicationInfoAccessingHost *host)
{
appInfo = host;
getProxy();
}
void Options ::setOptionAccessingHost(OptionAccessingHost *host)
{
options = host;
}
Options * Options ::instance()
{
if(!instance_)
instance_ = new Options();
return instance_;
}
void Options ::reset()
{
delete instance_;
instance_ = 0;
}
bool Options ::useProxy() const
{
bool use = false;
if(appInfo) {
Proxy p = appInfo->getProxyFor("Yandex Narod Plugin");
use = !p.host.isEmpty();
}
return use;
}
QNetworkProxy Options ::getProxy() const
{
QNetworkProxy np;
if(appInfo) {
Proxy p = appInfo->getProxyFor("Yandex Narod Plugin");
np = QNetworkProxy(QNetworkProxy::HttpCachingProxy, p.host, p.port, p.user, p.pass);
if(p.type != "http")
np.setType(QNetworkProxy::Socks5Proxy);
}
return np;
}
void Options::setOption(const QString &name, const QVariant &value)
{
if(options) {
options->setPluginOption(name, value);
}
}
QVariant Options::getOption(const QString &name, const QVariant &def)
{
QVariant ret(def);
if(options) {
ret = options->getPluginOption(name, def);
}
return ret;
}
void Options::saveCookies(const QList<QNetworkCookie> &cooks)
{
if(options) {
QByteArray ba;
QDataStream ds(&ba, QIODevice::WriteOnly);
foreach(const QNetworkCookie& cookie, cooks) {
ds << cookie.toRawForm(QNetworkCookie::NameAndValueOnly);
}
options->setPluginOption(CONST_COOKIES, ba);
}
}
QList<QNetworkCookie> Options::loadCookies()
{
QList<QNetworkCookie> ret;
if(options) {
QByteArray ba = options->getPluginOption(CONST_COOKIES, QByteArray()).toByteArray();
if(!ba.isEmpty()) {
QDataStream ds(&ba, QIODevice::ReadOnly);
QByteArray byte;
while(!ds.atEnd()) {
ds >> byte;
QList<QNetworkCookie> list = QNetworkCookie::parseCookies(byte);
ret += list;
}
}
}
return ret;
}
QString Options::message(MessageType type)
{
switch(type) {
case MAuthStart:
return tr("Authorizing...");
case MAuthOk:
return tr("Authorizing OK");
case MAuthError:
return tr("Authorization failed");
case MCancel:
return tr("Canceled");
case MChooseFile:
return tr("Choose file");
case MUploading:
return tr("Uploading");
case MError:
return tr("Error! %1");
case MRemoveCookie:
return tr("Cookies are removed");
}
return QString();
}
QString Options::encodePassword(const QString &pass)
{
QString result;
int n1, n2;
if (passwordKey.length() == 0) {
return pass;
}
for (n1 = 0, n2 = 0; n1 < pass.length(); ++n1) {
ushort x = pass.at(n1).unicode() ^ passwordKey.at(n2++).unicode();
QString hex;
hex.sprintf("%04x", x);
result += hex;
if(n2 >= passwordKey.length()) {
n2 = 0;
}
}
return result;
}
QString Options::decodePassword(const QString &pass)
{
QString result;
int n1, n2;
if (passwordKey.length() == 0) {
return pass;
}
for(n1 = 0, n2 = 0; n1 < pass.length(); n1 += 4) {
ushort x = 0;
if(n1 + 4 > pass.length()) {
break;
}
x += QString(pass.at(n1)).toInt(NULL,16)*4096;
x += QString(pass.at(n1+1)).toInt(NULL,16)*256;
x += QString(pass.at(n1+2)).toInt(NULL,16)*16;
x += QString(pass.at(n1+3)).toInt(NULL,16);
QChar c(x ^ passwordKey.at(n2++).unicode());
result += c;
if(n2 >= passwordKey.length()) {
n2 = 0;
}
}
return result;
}
|