blob: 9aa0d8bedb2dd85838f8af52e163ccdb3dd6e76c [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 *
Alexander Afanasyev8e986f82016-03-21 14:19:15 -070016 * You should have received a copy of the GNU General Public License along with NFD
taylorchuc27dd482014-05-17 20:06:49 -070017 * 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"
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -070030
31#include <Security/Authorization.h>
32#include <Security/AuthorizationTags.h>
taylorchuc27dd482014-05-17 20:06:49 -070033#else
Alexander Afanasyev0621cec2016-03-20 23:18:27 -070034#define CONNECT_ICON ":/res/icon-connected-white.png"
35#define DISCONNECT_ICON ":/res/icon-disconnected-white.png"
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -070036#endif // OSX_BUILD
taylorchuc27dd482014-05-17 20:06:49 -070037
38#ifdef WAF
39#include "tray-menu.moc"
40// #include "tray-menu.cpp.moc"
41#endif
42
43namespace ndn {
44
Alexander Afanasyev4086d512014-07-11 15:56:33 -070045TrayMenu::TrayMenu(QQmlContext* context)
46 : m_context(context)
47 , m_isNfdRunning(false)
Yingdi Yu53c11c12016-03-20 12:56:49 -070048 , m_menu(new QMenu(this))
49 , m_entryPref(new QAction("Preferences...", m_menu))
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -070050 , m_entrySec(new QAction("Security...", m_menu))
51#ifdef OSX_BUILD
52 , m_entryEnableCli(new QAction("Enable Command Terminal Usage...", m_menu))
53#endif
Yingdi Yu53c11c12016-03-20 12:56:49 -070054 , m_entryQuit(new QAction("Quit", m_menu))
Yingdi Yu1f824642016-03-20 17:07:22 -070055 , m_keyViewerDialog(new ncc::KeyViewerDialog)
Yingdi Yu53c11c12016-03-20 12:56:49 -070056{
57 connect(m_entryPref, SIGNAL(triggered()), this, SIGNAL(showApp()));
Yingdi Yu1f824642016-03-20 17:07:22 -070058 connect(m_entrySec, SIGNAL(triggered()), m_keyViewerDialog, SLOT(present()));
Yingdi Yu53c11c12016-03-20 12:56:49 -070059 connect(m_entryQuit, SIGNAL(triggered()), this, SLOT(quitApp()));
taylorchuc27dd482014-05-17 20:06:49 -070060
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -070061#ifdef OSX_BUILD
62 connect(m_entryEnableCli, SIGNAL(triggered()), this, SLOT(enableCli()));
63#endif
Yingdi Yu1f824642016-03-20 17:07:22 -070064
Alexander Afanasyev4086d512014-07-11 15:56:33 -070065 connect(this, SIGNAL(nfdActivityUpdate(bool)), this, SLOT(updateNfdActivityIcon(bool)),
66 Qt::QueuedConnection);
67
68 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD")));
taylorchuc27dd482014-05-17 20:06:49 -070069
Yingdi Yu53c11c12016-03-20 12:56:49 -070070 // m_menu->addAction(start);
71 // m_menu->addAction(stop);
72 m_menu->addAction(m_entryPref);
73 m_menu->addAction(m_entrySec);
susmit50607912016-03-21 12:00:15 -070074#ifdef OSX_BUILD
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -070075 m_menu->addAction(m_entryEnableCli);
susmit50607912016-03-21 12:00:15 -070076#endif
Yingdi Yu53c11c12016-03-20 12:56:49 -070077 m_menu->addAction(m_entryQuit);
78 m_tray = new QSystemTrayIcon(this);
79 m_tray->setContextMenu(m_menu);
80 connect(m_tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
taylorchuc27dd482014-05-17 20:06:49 -070081 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
Yingdi Yu53c11c12016-03-20 12:56:49 -070082 m_tray->setIcon(QIcon(DISCONNECT_ICON));
83 m_tray->show();
taylorchuc27dd482014-05-17 20:06:49 -070084}
85
86TrayMenu::~TrayMenu()
87{
88}
89
90Q_INVOKABLE void
taylorchuc27dd482014-05-17 20:06:49 -070091TrayMenu::autoConfig()
92{
Alexander Afanasyev4086d512014-07-11 15:56:33 -070093 std::cout << "auto config" << std::endl;
taylorchuc27dd482014-05-17 20:06:49 -070094 QProcess* proc = new QProcess();
Alexander Afanasyev4086d512014-07-11 15:56:33 -070095 connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -070096 // proc->start(NFD_AUTOCONFIG_COMMAND);
taylorchuc27dd482014-05-17 20:06:49 -070097}
98
99void
100TrayMenu::quitApp()
101{
102 QCoreApplication::exit(0);
103}
104
105void
106TrayMenu::iconActivated(QSystemTrayIcon::ActivationReason reason)
107{
108 switch (reason) {
109 // case QSystemTrayIcon::Trigger:
110 // emit showApp();
111 // break;
112 case QSystemTrayIcon::Context:
113 break;
114 default:
115 break;
116 }
117}
118
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700119Q_INVOKABLE void
120TrayMenu::startStopNfd()
121{
122 if (!m_isNfdRunning) {
123 startNfd();
124 }
125 else {
126 stopNfd();
127 }
128}
129
taylorchuc27dd482014-05-17 20:06:49 -0700130void
131TrayMenu::startNfd()
132{
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700133#ifdef OSX_BUILD
134 QProcess* proc = new QProcess();
135 connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
136 proc->startDetached((QCoreApplication::applicationDirPath().toStdString() + "/../Platform/nfd").c_str(),
137 QStringList()
138 << "--config"
139 << (QCoreApplication::applicationDirPath().toStdString() + "/../etc/nfd.conf").c_str());
140// #endif
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -0700141// QProcess * proc = new QProcess();
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700142// connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -0700143// #ifdef __linux__
144// proc->start("gksudo", QStringList() << NFD_START_COMMAND);
145// #else
146// proc->start("osascript", QStringList()
147// << "-e"
148// << "do shell script \"" NFD_START_COMMAND "\" with administrator privileges");
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700149#endif
taylorchuc27dd482014-05-17 20:06:49 -0700150}
151
152void
153TrayMenu::stopNfd()
154{
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700155#ifdef OSX_BUILD
156 QProcess* proc = new QProcess();
157 connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
158 proc->startDetached("killall", QStringList() << "nfd");
159#endif
taylorchuc27dd482014-05-17 20:06:49 -0700160}
161
susmit4fe3cb92016-03-20 17:08:41 -0700162Q_INVOKABLE void
163TrayMenu::addDeleteRoute()
164{
165 addRoute();
166}
167
168Q_INVOKABLE void
169TrayMenu::addRoute()
170{
171 std::cout << "Adding route" << std::endl;
172 QString cmd = "nfdc register /test tcp4://localhost";
173 QProcess *addNewRoute = new QProcess();
174 connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater()));
175 addNewRoute->start("bash", QStringList() << "-c" << cmd);
176 std::cout << "Done" << std::endl;
177
178
179// QProcess * proc = new QProcess();
180}
181
182void
183TrayMenu::deleteRoute()
184{
185 std::cout << "Deleting route" << std::endl;
186 QString cmd = "nfdc unregister /test tcp4://localhost";
187 QProcess *addNewRoute = new QProcess();
188 connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater()));
189 addNewRoute->start("bash", QStringList() << "-c" << cmd);
190 std::cout << "Done" << std::endl;
191
192}
193
194
taylorchuc27dd482014-05-17 20:06:49 -0700195void
196TrayMenu::updateNfdActivityIcon(bool isActive)
197{
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700198 m_isNfdRunning = isActive;
199
taylorchuc27dd482014-05-17 20:06:49 -0700200 if (isActive) {
Yingdi Yu53c11c12016-03-20 12:56:49 -0700201 m_tray->setIcon(QIcon(CONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700202 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700203 }
204 else {
Yingdi Yu53c11c12016-03-20 12:56:49 -0700205 m_tray->setIcon(QIcon(DISCONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700206 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700207 }
208}
209
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700210void
211TrayMenu::enableCli()
212{
213#ifdef OSX_BUILD
214 AuthorizationRef authorizationRef;
215 OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
216 kAuthorizationFlagDefaults, &authorizationRef);
217 if (status != errAuthorizationSuccess)
218 return;
219
220 AuthorizationItem item = { kAuthorizationRightExecute, 0, 0, 0 };
221 AuthorizationRights rights = { 1, &item };
222 const AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed
223 | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
224
225 status = AuthorizationCopyRights(authorizationRef, &rights, kAuthorizationEmptyEnvironment,
226 flags, 0);
227 if (status != errAuthorizationSuccess)
228 return;
229
230 char const* mkdir_arg[] = { "-p", "/usr/local/bin", nullptr };
231 char const* mkdir = "/bin/mkdir";
232 AuthorizationExecuteWithPrivileges(authorizationRef,
233 mkdir,
234 kAuthorizationFlagDefaults, (char**)mkdir_arg, nullptr);
235
236 std::vector<std::string> arguments = { "-f",
237 QCoreApplication::applicationDirPath().toStdString() + "/../Resources/ndn",
238 "/usr/local/bin/ndn" };
239 std::vector<const char*> args;
240 for (const auto& i : arguments) {
241 args.push_back(i.c_str());
242 }
243 args.push_back(nullptr);
244
245 char const* helperTool = "/bin/cp";
246 AuthorizationExecuteWithPrivileges(authorizationRef,
247 helperTool,
248 kAuthorizationFlagDefaults,
249 (char**)args.data(), NULL);
250
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700251 AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
252#endif
253}
254
taylorchuc27dd482014-05-17 20:06:49 -0700255} // namespace ndn