blob: fce2cb1dc7f4f1eb547cad4de6fb59f42bd75e07 [file] [log] [blame]
taylorchuc27dd482014-05-17 20:06:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014, Regents of the University of California,
4 *
5 * This file is part of NFD Control Center. See AUTHORS.md for complete list of NFD
6 * authors and contributors.
7 *
8 * NFD Control Center is free software: you can redistribute it and/or modify it under the
9 * terms of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NFD Control Center is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with NFD
17 * Control Center, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "config.hpp"
21#include "tray-menu.hpp"
22
23#include <ndn-cxx/face.hpp>
24#include <ndn-cxx/interest.hpp>
25
26
27#ifdef OSX_BUILD
Alexander Afanasyev0621cec2016-03-20 23:18:27 -070028#define CONNECT_ICON ":/res/icon-connected-black.png"
29#define DISCONNECT_ICON ":/res/icon-disconnected-black.png"
taylorchuc27dd482014-05-17 20:06:49 -070030#else
Alexander Afanasyev0621cec2016-03-20 23:18:27 -070031#define CONNECT_ICON ":/res/icon-connected-white.png"
32#define DISCONNECT_ICON ":/res/icon-disconnected-white.png"
taylorchuc27dd482014-05-17 20:06:49 -070033#endif
34
35#ifdef WAF
36#include "tray-menu.moc"
37// #include "tray-menu.cpp.moc"
38#endif
39
40namespace ndn {
41
Alexander Afanasyev4086d512014-07-11 15:56:33 -070042TrayMenu::TrayMenu(QQmlContext* context)
43 : m_context(context)
44 , m_isNfdRunning(false)
Yingdi Yu53c11c12016-03-20 12:56:49 -070045 , m_menu(new QMenu(this))
46 , m_entryPref(new QAction("Preferences...", m_menu))
47 , m_entrySec(new QAction("Security", m_menu))
48 , m_entryQuit(new QAction("Quit", m_menu))
taylorchuc27dd482014-05-17 20:06:49 -070049
Yingdi Yu53c11c12016-03-20 12:56:49 -070050{
51 connect(m_entryPref, SIGNAL(triggered()), this, SIGNAL(showApp()));
52 connect(m_entryQuit, SIGNAL(triggered()), this, SLOT(quitApp()));
taylorchuc27dd482014-05-17 20:06:49 -070053
Alexander Afanasyev4086d512014-07-11 15:56:33 -070054 connect(this, SIGNAL(nfdActivityUpdate(bool)), this, SLOT(updateNfdActivityIcon(bool)),
55 Qt::QueuedConnection);
56
57 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD")));
taylorchuc27dd482014-05-17 20:06:49 -070058
Yingdi Yu53c11c12016-03-20 12:56:49 -070059 // m_menu->addAction(start);
60 // m_menu->addAction(stop);
61 m_menu->addAction(m_entryPref);
62 m_menu->addAction(m_entrySec);
63 m_menu->addAction(m_entryQuit);
64 m_tray = new QSystemTrayIcon(this);
65 m_tray->setContextMenu(m_menu);
66 connect(m_tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
taylorchuc27dd482014-05-17 20:06:49 -070067 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
Yingdi Yu53c11c12016-03-20 12:56:49 -070068 m_tray->setIcon(QIcon(DISCONNECT_ICON));
69 m_tray->show();
taylorchuc27dd482014-05-17 20:06:49 -070070}
71
72TrayMenu::~TrayMenu()
73{
74}
75
76Q_INVOKABLE void
taylorchuc27dd482014-05-17 20:06:49 -070077TrayMenu::autoConfig()
78{
Alexander Afanasyev4086d512014-07-11 15:56:33 -070079 std::cout << "auto config" << std::endl;
taylorchuc27dd482014-05-17 20:06:49 -070080 QProcess* proc = new QProcess();
Alexander Afanasyev4086d512014-07-11 15:56:33 -070081 connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -070082 // proc->start(NFD_AUTOCONFIG_COMMAND);
taylorchuc27dd482014-05-17 20:06:49 -070083}
84
85void
86TrayMenu::quitApp()
87{
88 QCoreApplication::exit(0);
89}
90
91void
92TrayMenu::iconActivated(QSystemTrayIcon::ActivationReason reason)
93{
94 switch (reason) {
95 // case QSystemTrayIcon::Trigger:
96 // emit showApp();
97 // break;
98 case QSystemTrayIcon::Context:
99 break;
100 default:
101 break;
102 }
103}
104
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700105Q_INVOKABLE void
106TrayMenu::startStopNfd()
107{
108 if (!m_isNfdRunning) {
109 startNfd();
110 }
111 else {
112 stopNfd();
113 }
114}
115
taylorchuc27dd482014-05-17 20:06:49 -0700116void
117TrayMenu::startNfd()
118{
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -0700119// QProcess * proc = new QProcess();
120// connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater()));
121// #ifdef __linux__
122// proc->start("gksudo", QStringList() << NFD_START_COMMAND);
123// #else
124// proc->start("osascript", QStringList()
125// << "-e"
126// << "do shell script \"" NFD_START_COMMAND "\" with administrator privileges");
127// #endif
taylorchuc27dd482014-05-17 20:06:49 -0700128}
129
130void
131TrayMenu::stopNfd()
132{
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -0700133// QProcess * proc = new QProcess();
134// connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater()));
135// #ifdef __linux__
136// proc->start("gksudo", QStringList() << NFD_STOP_COMMAND);
137// #else
138// proc->start("osascript", QStringList()
139// << "-e"
140// << "do shell script \"" NFD_STOP_COMMAND "\" with administrator privileges");
141// #endif
taylorchuc27dd482014-05-17 20:06:49 -0700142}
143
susmit4fe3cb92016-03-20 17:08:41 -0700144Q_INVOKABLE void
145TrayMenu::addDeleteRoute()
146{
147 addRoute();
148}
149
150Q_INVOKABLE void
151TrayMenu::addRoute()
152{
153 std::cout << "Adding route" << std::endl;
154 QString cmd = "nfdc register /test tcp4://localhost";
155 QProcess *addNewRoute = new QProcess();
156 connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater()));
157 addNewRoute->start("bash", QStringList() << "-c" << cmd);
158 std::cout << "Done" << std::endl;
159
160
161// QProcess * proc = new QProcess();
162}
163
164void
165TrayMenu::deleteRoute()
166{
167 std::cout << "Deleting route" << std::endl;
168 QString cmd = "nfdc unregister /test tcp4://localhost";
169 QProcess *addNewRoute = new QProcess();
170 connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater()));
171 addNewRoute->start("bash", QStringList() << "-c" << cmd);
172 std::cout << "Done" << std::endl;
173
174}
175
176
taylorchuc27dd482014-05-17 20:06:49 -0700177void
178TrayMenu::updateNfdActivityIcon(bool isActive)
179{
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700180 m_isNfdRunning = isActive;
181
taylorchuc27dd482014-05-17 20:06:49 -0700182 if (isActive) {
Yingdi Yu53c11c12016-03-20 12:56:49 -0700183 m_tray->setIcon(QIcon(CONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700184 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700185 }
186 else {
Yingdi Yu53c11c12016-03-20 12:56:49 -0700187 m_tray->setIcon(QIcon(DISCONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700188 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700189 }
190}
191
192} // namespace ndn