summaryrefslogtreecommitdiff
path: root/client/ProxyClientApp.cpp
blob: b55c33c9dcb2ee0a73a8bae72f0483ffdd3e87b8 (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

#include <QtGui>

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

using namespace std;

ProxyClientApp::ProxyClientApp(int &argc, char *argv[]): QApplication(argc, argv)
{	
	/* 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::DoubleClick)
	{
		showProxyDialog();
	}
}

void ProxyClientApp::showProxyDialog()
{
	Logger::Trace("Creating proxy dialog.\n");
	QDesktopWidget* screen = QApplication::desktop();
	QRect availScreenRect = screen->availableGeometry();
	
	Logger::Debug("Available screen size WxH = %d x %d\n", availScreenRect.width(), availScreenRect.height());
	
	ProxyDialog *dialog = new ProxyDialog();
	dialog->show();
	dialog->move(availScreenRect.width() - dialog->frameSize().width(),
				 availScreenRect.height() - dialog->frameSize().height());
}

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();
	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);
}