summaryrefslogtreecommitdiff
path: root/client/ProxyClientApp.cpp
blob: 3b703d86d02c99b5e90f1843b80c251258439f74 (plain)
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

#include <QtGui>

#include "client.h"
#include "Proxifier.h"
#include "ProxyClientApp.h"
#include "UpdatedConfig.h"

using namespace std;

ProxyClientApp::ProxyClientApp(int &argc, char *argv[]): QApplication(argc, argv)
{
    dialog = NULL;
    closeTime = 0;

	/* initiates UpdatedConfig singleton that start sending configuration requests */
	UpdatedConfig *cfg = UpdatedConfig::CurrentConfig();
	connect(cfg, SIGNAL(updated()),
			this, SLOT(configUpdated()));
	
	if (!QSystemTrayIcon::isSystemTrayAvailable())
	{
		Logger::Fatal("No system tray available! Terminating.\n");
        return;
	}
	QApplication::setQuitOnLastWindowClosed(false);	
	
	// create system tray menu
	QMenu *trayMenu = new QMenu;
	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()));
	trayMenu->addAction(showAction);
	trayMenu->addAction(quitAction);
	
	QIcon *icon = new QIcon(":/icon.png");
	trayIcon = new QSystemTrayIcon(*icon);
	connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
			this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
	trayIcon->setContextMenu(trayMenu);
	trayIcon->show();
	delete icon;
	
	QString cssFile = applicationDirPath()+ "/style.qss";	
	QFileInfo cssFIleInfo(cssFile);
	if (cssFIleInfo.exists())
	{
		Logger::Info("CSS stylesheet found. Trying to apply.\n");		
		QFile file(cssFile);
		file.open(QFile::ReadOnly);
		QString cssStr = QString::fromLocal8Bit(file.readAll());
		file.close();
		setStyleSheet(cssStr);
	}
	else
	{
		Logger::Info("CSS stylesheet file 'style.qss' wasn't found.\n");
	}	
	
	/**
	 * @todo	FIX: trying to access cfg while it is being updated
	 * 			(note that initial config is still read from config.cfg)
	 */
	QString msg = QString::fromLocal8Bit(cfg->WelcomeMsg.c_str());
	QMessageBox welcomeMsg;
	welcomeMsg.setText(msg);
	welcomeMsg.setWindowTitle("Information");
	welcomeMsg.setStandardButtons(QMessageBox::Ok);
	welcomeMsg.setIcon(QMessageBox::Information);
	welcomeMsg.exec();
}

void ProxyClientApp::trayActivated(QSystemTrayIcon::ActivationReason reason)
{
    if (reason != QSystemTrayIcon::Trigger)
	{
        return;
    }

    if (dialog == NULL)
    {
        // part of tray click workaround
        quint64 time = QDateTime::currentMSecsSinceEpoch();
        if (time - closeTime < 400)
        {
            return;
        }
        closeTime = 0;

        showProxyDialog();
    }
    else
    {
        dialog->reject();
        delete dialog;
        dialog = NULL;
    }
}

void ProxyClientApp::showProxyDialog()
{
    dialog = new ProxyDialog();
    dialog->setAttribute(Qt::WA_DeleteOnClose);
    connect(dialog, SIGNAL(finished(int)), this, SLOT(DialogClosed(int)));
    dialog->show();
    dialog->activateWindow();

    closeTime = 0;
}

void ProxyClientApp::DialogClosed(int result)
{
    // part of tray click workaround
    closeTime = QDateTime::currentMSecsSinceEpoch();

    Logger::Trace("Dialog closed\n");
    dialog = NULL;
    Proxifier *proxifier = Proxifier::GetInstance();
    if (!proxifier->IsValid())
    {
        Logger::Error("No valid proxifier configuration file found!\n");
        return;	// do not restart if no config found
    }
    if (!proxifier->ReloadConfig())
    {
        Logger::Error("Unable to restart Proxifier process!\n");
    }
}

void ProxyClientApp::configUpdated()
{
	// start downloading files in seperate thread
	// (all necessary check are done by the thread)
	fileOpThread.start(QThread::NormalPriority);
	
	UpdatedConfig *cfg = UpdatedConfig::CurrentConfig();
	Proxifier *proxifier = Proxifier::GetInstance();
	//update Proxifier Rules
	if (proxifier->IsValid())
	{
		proxifier->ApplyFirewallRules(cfg->GetFirewallList());
        if (!proxifier->ReloadConfig())
		{
            Logger::Error("Unable to reload Proxifier's configuration!\n");
		}
	}
	else
	{
        Logger::Error("No valid proxifier configuration file found!\n");		
	}	
	
	// show message when config updated
    /*
	QString msg = QString::fromLocal8Bit(cfg->ConfigLoadedMsg.c_str());
	QMessageBox updatedMsg;
	updatedMsg.setText(msg);
	updatedMsg.setWindowTitle("Information");
	updatedMsg.setStandardButtons(QMessageBox::Ok);
	updatedMsg.setIcon(QMessageBox::Information);
	updatedMsg.exec();
    */
}

void ProxyClientApp::quitApp()
{
	Logger::Info("Terminating\n");
        trayIcon->hide();
	QApplication::exit(0);
}