summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Borisov <borisov.alexandr@rambler.ru>2011-10-23 04:10:55 +0300
committerAlex Borisov <borisov.alexandr@rambler.ru>2011-10-23 04:10:55 +0300
commit21ad7eac963bc329395fa893300fb5c8f721fd77 (patch)
tree10dda6e6170071332aab490faeeb599f10ff6e03
parent3eef0376635624c354a5e1b8a2680b74eb23a423 (diff)
Logging facility, Config class, tray icon, custom QApplication subclass
-rw-r--r--client/Config.cpp91
-rw-r--r--client/Config.h31
-rw-r--r--client/Dialog.cpp (renamed from client/dialog.cpp)15
-rw-r--r--client/Dialog.h28
-rw-r--r--client/Logger.cpp98
-rw-r--r--client/Logger.h55
-rw-r--r--client/Proxy.cpp (renamed from client/proxy.cpp)41
-rw-r--r--client/Proxy.h (renamed from client/proxy.h)18
-rw-r--r--client/ProxyClientApp.cpp47
-rw-r--r--client/ProxyClientApp.h19
-rw-r--r--client/client.h5
-rw-r--r--client/client.pro6
-rw-r--r--client/config/static_proxy_list.cfg~7
-rw-r--r--client/config_samples/proxy_list.cfg20
-rw-r--r--client/config_samples/static_proxy_list.cfg2
-rw-r--r--client/dialog.h29
-rw-r--r--client/icon.pngbin0 -> 5003 bytes
-rw-r--r--client/main.cpp10
18 files changed, 439 insertions, 83 deletions
diff --git a/client/Config.cpp b/client/Config.cpp
new file mode 100644
index 0000000..472fdf4
--- /dev/null
+++ b/client/Config.cpp
@@ -0,0 +1,91 @@
+
+#include <fstream>
+#include "client.h"
+#include "Config.h"
+#include "Proxy.h"
+
+using namespace std;
+
+
+Config* Config::self = NULL;
+
+Config *Config::CurrentConfig()
+{
+ if (self == NULL)
+ self = new Config();
+ return self;
+}
+
+Config::Config(): configValid(0)
+{
+}
+
+void Config::AcquireConfig()
+{
+ configValid = false;
+ if (ReadGenericProxy())
+ return ;
+ if (ReadStaticProxy())
+ return ;
+ configValid = true;
+}
+
+bool Config::IsConfigValid()
+{
+ return configValid;
+}
+
+
+int Config::ReadGenericProxy()
+{
+ Logger::Info("Parsing generic proxy list\n");
+
+ ifstream proxyFile("./config/proxy_list.cfg", std::ios::in);
+ if (!proxyFile)
+ {
+ Logger::Error("Can't open file ./config/proxy_list.cfg");
+ return -1;
+ }
+
+ const int str_size = 512;
+ char str[str_size] = {0};
+ while (!proxyFile.eof())
+ {
+ proxyFile.getline(str, str_size, ';');
+ if (proxyFile.eof())
+ break;
+ string entry = str;
+ ProxyEntryGeneric *proxy = new ProxyEntryGeneric;
+ proxy->Parse(entry);
+ genericProxy.push_back(*proxy);
+ }
+ proxyFile.close();
+ return 0;
+}
+
+int Config::ReadStaticProxy()
+{
+ Logger::Info("Parsing static proxy list\n");
+
+ ifstream proxyFile("./config/static_proxy_list.cfg", std::ios::in);
+ if (!proxyFile)
+ {
+ Logger::Error("Can't open file ./config/static_proxy_list.cfg\n");
+ return -1;
+ }
+
+ const int str_size = 512;
+ char str[str_size] = {0};
+ while (!proxyFile.eof())
+ {
+ proxyFile.getline(str, str_size, ';');
+ if (proxyFile.eof())
+ break;
+ string entry = str;
+ ProxyEntryStatic *proxy = new ProxyEntryStatic;
+ proxy->Parse(entry);
+ staticProxy.push_back(*proxy);
+ }
+ proxyFile.close();
+ return 0;
+} \ No newline at end of file
diff --git a/client/Config.h b/client/Config.h
new file mode 100644
index 0000000..9b8a53e
--- /dev/null
+++ b/client/Config.h
@@ -0,0 +1,31 @@
+
+
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#include <vector>
+#include "Proxy.h"
+
+class ProxyEntryGeneric;
+class ProxyEntryStatic;
+
+class Config
+{
+public:
+ static Config *CurrentConfig();
+ void AcquireConfig();
+ bool IsConfigValid();
+protected:
+ Config();
+private:
+ static Config *self;
+ bool configValid;
+ std::vector<ProxyEntryGeneric> genericProxy;
+ std::vector<ProxyEntryStatic> staticProxy;
+
+ int ReadGenericProxy();
+ int ReadStaticProxy();
+};
+
+
+#endif \ No newline at end of file
diff --git a/client/dialog.cpp b/client/Dialog.cpp
index 35e18be..375fd98 100644
--- a/client/dialog.cpp
+++ b/client/Dialog.cpp
@@ -1,9 +1,22 @@
#include <QtGui>
-#include "dialog.h"
+#include "client.h"
+#include "Config.h"
+#include "Dialog.h"
+#include "Proxy.h"
+
ProxyDialog::ProxyDialog(QWidget *parent): QDialog(parent)
{
+ //WIPE it out!!! COnfig shoud be reread by timer event!
+ Config *cfg = Config::CurrentConfig();
+ cfg->AcquireConfig();
+ if (! cfg->IsConfigValid())
+ {
+ Logger::Fatal("No valid configuration found! Can't proceed.");
+ return ;
+ }
+
topLabel = new QLabel(tr("Top Panel"));
countryBox = new QComboBox;
countryBox->addItem(tr("Ukraine"));
diff --git a/client/Dialog.h b/client/Dialog.h
new file mode 100644
index 0000000..d8cd7a9
--- /dev/null
+++ b/client/Dialog.h
@@ -0,0 +1,28 @@
+
+#ifndef PROXY_DIALOG
+#define PROXY_DIALOG
+
+#include <QDialog>
+#include <vector>
+
+class QLabel;
+class QComboBox;
+class ProxyEntryGeneric;
+class ProxyEntryStatic;
+
+class ProxyDialog: public QDialog
+{
+ Q_OBJECT
+
+public:
+ ProxyDialog(QWidget *parent = 0);
+
+private:
+ QLabel *topLabel;
+ QLabel *bottomLabel;
+ QComboBox *countryBox;
+ QComboBox *stateBox;
+ QComboBox *cityBox;
+};
+
+#endif \ No newline at end of file
diff --git a/client/Logger.cpp b/client/Logger.cpp
new file mode 100644
index 0000000..4cbb8aa
--- /dev/null
+++ b/client/Logger.cpp
@@ -0,0 +1,98 @@
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <time.h>
+#include "Logger.h"
+
+
+LogLevelType Logger::LogLevel = TraceLevel;
+
+#ifdef DEBUG
+
+void Logger::Trace(char* msgfmt, ...)
+{
+ va_list args;
+ va_start(args, msgfmt);
+ Logger::Log(TraceLevel, msgfmt, args);
+}
+
+void Logger::Debug(char* msgfmt, ...)
+{
+ va_list args;
+ va_start(args, msgfmt);
+ Logger::Log(DebugLevel, msgfmt, args);
+}
+
+void Logger::Info(char* msgfmt, ...)
+{
+ va_list args;
+ va_start(args, msgfmt);
+ Logger::Log(InfoLevel, msgfmt, args);
+}
+
+void Logger::Warn(char* msgfmt, ...)
+{
+ va_list args;
+ va_start(args, msgfmt);
+ Logger::Log(WarnLevel, msgfmt, args);
+}
+
+void Logger::Error(char* msgfmt, ...)
+{
+ va_list args;
+ va_start(args, msgfmt);
+ Logger::Log(ErrorLevel, msgfmt, args);
+}
+
+void Logger::Fatal(char* msgfmt, ...)
+{
+ va_list args;
+ va_start(args, msgfmt);
+ Logger::Log(FatalLevel, msgfmt, args);
+}
+
+void Logger::Log(LogLevelType logLevel, char* msgfmt, ...)
+{
+ va_list args;
+ va_start(args, msgfmt);
+ Logger::Log(logLevel, msgfmt, args);
+}
+
+void Logger::Log(LogLevelType logLevel, char* msgfmt, va_list args)
+{
+ time_t rawtime = time(NULL);
+ struct tm *lctime = localtime(&rawtime);
+ char time_str[10] = {0};
+ strftime(time_str, 10, "%H:%M:%S", lctime);
+
+ char log_level_str[6] = {0};
+ switch (logLevel)
+ {
+ default:
+ case TraceLevel:
+ strcpy(log_level_str, "Trace");
+ break;
+ case DebugLevel:
+ strcpy(log_level_str, "Debug");
+ break;
+ case InfoLevel:
+ strcpy(log_level_str, "Info");
+ break;
+ case WarnLevel:
+ strcpy(log_level_str, "Warn");
+ break;
+ case ErrorLevel:
+ strcpy(log_level_str, "Error");
+ break;
+ case FatalLevel:
+ strcpy(log_level_str, "Fatal");
+ break;
+ }
+
+ char *newfmt = new char[strlen(msgfmt) + 24];
+ sprintf(newfmt, "[%s] %-6s: %s", time_str, log_level_str, msgfmt);
+ vfprintf(stderr, newfmt, args);
+}
+
+#endif \ No newline at end of file
diff --git a/client/Logger.h b/client/Logger.h
new file mode 100644
index 0000000..c5cf70f
--- /dev/null
+++ b/client/Logger.h
@@ -0,0 +1,55 @@
+
+
+#ifndef LOG_H
+#define LOG_H
+
+enum LogLevelType
+{
+ TraceLevel = 0,
+ DebugLevel,
+ InfoLevel,
+ WarnLevel,
+ ErrorLevel,
+ FatalLevel
+};
+
+
+#ifdef DEBUG
+
+#include <stdarg.h>
+
+class Logger
+{
+public:
+ static LogLevelType LogLevel;
+
+ static void Trace(char* msgfmt, ...);
+ static void Debug(char* msgfmt, ...);
+ static void Info(char* msgfmt, ...);
+ static void Warn(char* msgfmt, ...);
+ static void Error(char* msgfmt, ...);
+ static void Fatal(char* msgfmt, ...);
+ static void Log(LogLevelType logLevel, char* msgfmt, ...);
+private:
+ static void Log(LogLevelType logLevel, char* msgfmt, va_list arsg);
+};
+
+#else
+
+class Logger
+{
+public:
+ static LogLevelType LogLevel;
+
+ static void Trace(char* msgfmt, ...) {}
+ static void Debug(char* msgfmt, ...) {}
+ static void Info(char* msgfmt, ...) {}
+ static void Warn(char* msgfmt, ...) {}
+ static void Error(char* msgfmt, ...) {}
+ static void Fatal(char* msgfmt, ...) {}
+ static void Log(LogLevelType logLevel, char* msgfmt, ...) {}
+};
+
+#endif
+
+#endif \ No newline at end of file
diff --git a/client/proxy.cpp b/client/Proxy.cpp
index 1857231..46a580e 100644
--- a/client/proxy.cpp
+++ b/client/Proxy.cpp
@@ -1,24 +1,26 @@
#include <stdlib.h>
-#include "proxy.h"
+#include "client.h"
+#include "Proxy.h"
/**************************************
* Proxy methods
**************************************/
- //
- // Parse proxy string and set calss members according to it's contents
- // Some examples:
- // user:password@server.com:1234
- // server.com:1234
+//
+// Parse proxy string and set calss members according to it's contents
+// Some examples:
+// user:password@server.com:1234
+// server.com:1234
void Proxy::Parse(std::string entry)
{
+ Logger::Trace("Parsing proxy string: %s\n", entry.c_str());
+
login.clear();
password.clear();
host.clear();
size_t lp1 = 0, lp2 = 0;
- int port = 0;
if(entry.find('@') != std::string::npos)
{
lp2 = entry.find(':', lp1);
@@ -45,8 +47,10 @@ void Proxy::Parse(std::string entry)
/**************************************
* ProxyEntry methods
**************************************/
-void ProxyEntry::Parse(std::string entry)
+void ProxyEntryGeneric::Parse(std::string entry)
{
+ Logger::Trace("Parsing generic proxy string: %s\n", entry.c_str());
+
country.clear();
state.clear();
city.clear();
@@ -74,6 +78,25 @@ void ProxyEntry::Parse(std::string entry)
**************************************/
void ProxyEntryStatic::Parse(std::string entry)
{
-
+ Logger::Trace("Parsing static proxy string: %s\n", entry.c_str());
+
+ name.clear();
+ size_t lp1 = 0, lp2 = 0;
+ //extract Proxy part
+ lp2 = entry.find(" ", lp1);
+ std::string proxy = entry.substr(lp1, lp2-lp1);
+ Proxy::Parse(proxy);
+
+ //estract name, position and speed
+ lp1 = lp2+2;
+ lp2 = entry.find('"', lp1);
+ name = entry.substr(lp1, lp2-lp1);
+
+ lp1 = lp2+2;
+ lp2 = entry.find(" ", lp1);
+ position = atoi(entry.substr(lp1, lp2-lp1).c_str());
+
+ lp1 = lp2+1;
+ speed = atoi(entry.substr(lp1).c_str());
}
\ No newline at end of file
diff --git a/client/proxy.h b/client/Proxy.h
index 321922a..0194db4 100644
--- a/client/proxy.h
+++ b/client/Proxy.h
@@ -1,5 +1,8 @@
-#include <client.h>
+#ifndef PROXY_H
+#define PROXY_H
+
+#include <string>
class Proxy
{
@@ -14,15 +17,15 @@ public:
};
-class ProxyEntry: public Proxy
+class ProxyEntryGeneric: public Proxy
{
public:
std::string country;
std::string state;
std::string city;
- ProxyEntry(): Proxy() {}
- void Parse(std::string entry);
+ ProxyEntryGeneric(): Proxy() {}
+ virtual void Parse(std::string entry);
};
@@ -31,7 +34,10 @@ class ProxyEntryStatic: public Proxy
public:
std::string name;
int position;
+ int speed; //kb per second
- ProxyEntryStatic(): Proxy(), position(0) {}
- void Parse(std::string entry);
+ ProxyEntryStatic(): Proxy(), position(0), speed(0) {}
+ virtual void Parse(std::string entry);
};
+
+#endif
diff --git a/client/ProxyClientApp.cpp b/client/ProxyClientApp.cpp
new file mode 100644
index 0000000..030f260
--- /dev/null
+++ b/client/ProxyClientApp.cpp
@@ -0,0 +1,47 @@
+
+#include <QSystemTrayIcon>
+#include <QMessageBox>
+#include <QMenu>
+
+#include "client.h"
+#include "Dialog.h"
+#include "ProxyClientApp.h"
+
+ProxyClientApp::ProxyClientApp(int &argc, char *argv[]): QApplication(argc, argv)
+{
+ if (!QSystemTrayIcon::isSystemTrayAvailable())
+ {
+ Logger::Fatal("No system tray available! Terminating.\n");
+ return;
+ }
+ QAction *showAction = new QAction(tr("&Show"), this);
+ showAction->setStatusTip(tr("Open dialog to choose a proxy"));
+ connect(showAction, SIGNAL(triggered()), this, SLOT(showProxyDialog()));
+ QAction *quitAction = new QAction(tr("&Quit"), this);
+ quitAction->setStatusTip(tr("Close this application"));
+ connect(quitAction, SIGNAL(triggered()), this, SLOT(quitApp()));
+ QMenu *trayMenu = new QMenu;
+ trayMenu->addAction(showAction);
+ trayMenu->addAction(quitAction);
+
+ QIcon *icon = new QIcon("icon.png");
+ QSystemTrayIcon *trayIcon = new QSystemTrayIcon(*icon);
+ trayIcon->setContextMenu(trayMenu);
+ trayIcon->show();
+ delete icon;
+
+ QApplication::setQuitOnLastWindowClosed(false);
+}
+
+void ProxyClientApp::showProxyDialog()
+{
+ Logger::Trace("Creating proxy dialog.\n");
+ ProxyDialog *dialog = new ProxyDialog();
+ dialog->show();
+}
+
+void ProxyClientApp::quitApp()
+{
+ Logger::Info("Terminating\n");
+ QApplication::exit(0);
+}
diff --git a/client/ProxyClientApp.h b/client/ProxyClientApp.h
new file mode 100644
index 0000000..7369bad
--- /dev/null
+++ b/client/ProxyClientApp.h
@@ -0,0 +1,19 @@
+
+
+#ifndef PROXY_CLIENT_APPLICATION_H
+#define PROXY_CLIENT_APPLICATION_H
+
+#include <QApplication>
+
+class ProxyClientApp: public QApplication
+{
+ Q_OBJECT
+public:
+ ProxyClientApp(int &argc, char *argv[]);
+private slots:
+ void showProxyDialog();
+ void quitApp();
+};
+
+
+#endif \ No newline at end of file
diff --git a/client/client.h b/client/client.h
index 4d35e2e..5e01be9 100644
--- a/client/client.h
+++ b/client/client.h
@@ -8,7 +8,10 @@
#include <unistd.h>
#endif
-#include <iostream>
#include <string>
+#include <vector>
+
+#include "Logger.h"
+
#endif
diff --git a/client/client.pro b/client/client.pro
index 050e6b1..d81d28d 100644
--- a/client/client.pro
+++ b/client/client.pro
@@ -2,11 +2,13 @@
# Automatically generated by qmake (2.01a) Wed Oct 19 03:05:59 2011
######################################################################
+QMAKE_CXXFLAGS= -DDEBUG -g
+
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
-HEADERS += client.h proxy.h dialog.h
-SOURCES += dialog.cpp main.cpp proxy.cpp
+HEADERS += client.h Proxy.h Dialog.h ProxyClientApp.h Logger.h Config.h
+SOURCES += Dialog.cpp main.cpp Proxy.cpp ProxyClientApp.cpp Logger.cpp Config.cpp
diff --git a/client/config/static_proxy_list.cfg~ b/client/config/static_proxy_list.cfg~
deleted file mode 100644
index d0bda77..0000000
--- a/client/config/static_proxy_list.cfg~
+++ /dev/null
@@ -1,7 +0,0 @@
-asdsad:qweqwe@qweqwe.de:3204 "label" 1 17;
-sdfsdf:234 "asdd" 2 35;
-user:password@server.com:123 "Ukraine 1" 1 14;
-server.com:123 "Ukraine 2" 3 54;
-user:password@server.com:123 "Ukraine 3" 3 137;
-user:password@server.com:123 "Russia 1" 1 95;
-user:password@server.com:123 "Russia 2" 2 26;
diff --git a/client/config_samples/proxy_list.cfg b/client/config_samples/proxy_list.cfg
deleted file mode 100644
index 0c35bf4..0000000
--- a/client/config_samples/proxy_list.cfg
+++ /dev/null
@@ -1,20 +0,0 @@
-user:password@server.com:123 "Ukraine" "Kharkovskaya obl." "Kharkov";
-server.com:123 "Ukraine" "Kharkovskaya obl." "Izum";
-user:password@server.com:123 "Ukraine" "Kharkovskaya obl." "Balakleya";
-user:password@server.com:123 "Ukraine" "AR Krym" "Alushta";
-user:password@server.com:123 "Ukraine" "AR Krym" "Simferorpol";
-user:password@server.com:123 "Ukraine" "AR Krym" "Sevastopol";
-user:password@server.com:123 "Ukraine" "AR Krym" "Evpatoria";
-server.com:123 "Ukraine" "AR Krym" "Kerch";
-user:password@server.com:123 "Ukraine" "AR Krym" "Yalta";
-user:password@server.com:123 "Ukraine" "AR Krym" "Sudak";
-server.net:213 "Russia" "-" "Moscow";
-server.net:213 "Russia" "-" "St. Petersburg";
-server.net:213 "Russia" "-" "Perm";
-server.net:213 "Russia" "-" "Vlasivostok";
-server.net:213 "Russia" "-" "Belgorod";
-server.net:213 "Russia" "-" "Bryansk";
-server.net:213 "Russia" "-" "Sochi";
-server.net:213 "USA" "California" "San francisco";
-sdf:wer@sfds.info:666 "China" "-" "Shanghai";
-
diff --git a/client/config_samples/static_proxy_list.cfg b/client/config_samples/static_proxy_list.cfg
deleted file mode 100644
index 4bc40bd..0000000
--- a/client/config_samples/static_proxy_list.cfg
+++ /dev/null
@@ -1,2 +0,0 @@
-asdsad:qweqwe@qweqwe.df:324 "lbl" 1;
-sdfsdf:234 "asdd" 2;
diff --git a/client/dialog.h b/client/dialog.h
deleted file mode 100644
index f841f2b..0000000
--- a/client/dialog.h
+++ /dev/null
@@ -1,29 +0,0 @@
-
-#ifndef PROXY_DIALOG
-#define PROXY_DIALOG
-
-#include <QDialog>
-
-class QLabel;
-class QComboBox;
-
-class ProxyDialog: public QDialog
-{
- Q_OBJECT
-
- public:
- ProxyDialog(QWidget *parent = 0);
-
- signals:
- //move to special Proxy class
- void AddProxy(QString proxy);
-
- private:
- QLabel *topLabel;
- QComboBox *countryBox;
- QComboBox *stateBox;
- QComboBox *cityBox;
- QLabel *bottomLabel;
-};
-
-#endif \ No newline at end of file
diff --git a/client/icon.png b/client/icon.png
new file mode 100644
index 0000000..4d1557d
--- /dev/null
+++ b/client/icon.png
Binary files differ
diff --git a/client/main.cpp b/client/main.cpp
index 0c36979..2752eff 100644
--- a/client/main.cpp
+++ b/client/main.cpp
@@ -1,13 +1,11 @@
-#include <QApplication>
+#include "ProxyClientApp.h"
#include "client.h"
-#include "proxy.h"
#include "dialog.h"
int main(int argc, char *argv[])
{
- QApplication app(argc, argv);
- ProxyDialog *dialog = new ProxyDialog;
- dialog->show();
+ Logger::Info("Starting client application\n");
+ ProxyClientApp app(argc, argv);
return app.exec();
-} \ No newline at end of file
+}