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

#include <QMenu>
#include <QMessageBox>
#include <QFileInfo>

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

using namespace std;

ProxyClientApp::ProxyClientApp(int &argc, char *argv[]): QApplication(argc, argv)
{
	// check if initial config exists (config.cfg)
	QString configPath = this_app->applicationDirPath()+ "/config.cfg";
	QFileInfo configInfo(configPath);
	if (! configInfo.exists())
	{
		Logger::Fatal("Initial configuration file (config.cfg) do not exist!\n");
		Logger::Fatal("Terminating!\n");
		return ;
	}
	
	/* 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();
	
	/*
	vector<pair<string, string> > files;
	pair<string, string> file;
	file = make_pair("../main.cpp", "70ba1d68907cc9d3");
	files.push_back(file);

	//QString addr = QString::fromLocal8Bit();
	DownloadClient downloadClient("127.0.0.1");
	downloadClient.Download(files);
	*/
}

void ProxyClientApp::trayActivated(QSystemTrayIcon::ActivationReason reason)
{
	if (reason == QSystemTrayIcon::DoubleClick)
	{
		showProxyDialog();
	}
}

void ProxyClientApp::showProxyDialog()
{
	Logger::Trace("Creating proxy dialog.\n");
	ProxyDialog *dialog = new ProxyDialog();
	dialog->show();
}

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