blob: 3d3afe73a1b33039d64039bb3d355c5892196eb4 [file] [log] [blame]
taylorchuc27dd482014-05-17 20:06:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Qi Zhao0e043e52016-12-05 18:27:09 -08003 * Copyright (c) 2013-2016, Regents of the University of California.
taylorchuc27dd482014-05-17 20:06:49 -07004 *
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 *
Qi Zhao0e043e52016-12-05 18:27:09 -080016 * 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 Afanasyev81509f32016-03-21 17:02:38 -070045TrayMenu::TrayMenu(QQmlContext* context, Face& face)
Alexander Afanasyev4086d512014-07-11 15:56:33 -070046 : 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)
Alexander Afanasyev81509f32016-03-21 17:02:38 -070056 , m_face(face)
Yingdi Yu53c11c12016-03-20 12:56:49 -070057{
58 connect(m_entryPref, SIGNAL(triggered()), this, SIGNAL(showApp()));
Yingdi Yu1f824642016-03-20 17:07:22 -070059 connect(m_entrySec, SIGNAL(triggered()), m_keyViewerDialog, SLOT(present()));
Yingdi Yu53c11c12016-03-20 12:56:49 -070060 connect(m_entryQuit, SIGNAL(triggered()), this, SLOT(quitApp()));
taylorchuc27dd482014-05-17 20:06:49 -070061
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -070062#ifdef OSX_BUILD
63 connect(m_entryEnableCli, SIGNAL(triggered()), this, SLOT(enableCli()));
64#endif
Yingdi Yu1f824642016-03-20 17:07:22 -070065
Alexander Afanasyev4086d512014-07-11 15:56:33 -070066 connect(this, SIGNAL(nfdActivityUpdate(bool)), this, SLOT(updateNfdActivityIcon(bool)),
67 Qt::QueuedConnection);
68
69 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD")));
taylorchuc27dd482014-05-17 20:06:49 -070070
Yingdi Yu53c11c12016-03-20 12:56:49 -070071 // m_menu->addAction(start);
72 // m_menu->addAction(stop);
73 m_menu->addAction(m_entryPref);
74 m_menu->addAction(m_entrySec);
susmit50607912016-03-21 12:00:15 -070075#ifdef OSX_BUILD
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -070076 m_menu->addAction(m_entryEnableCli);
susmit50607912016-03-21 12:00:15 -070077#endif
Yingdi Yu53c11c12016-03-20 12:56:49 -070078 m_menu->addAction(m_entryQuit);
79 m_tray = new QSystemTrayIcon(this);
80 m_tray->setContextMenu(m_menu);
81 connect(m_tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
taylorchuc27dd482014-05-17 20:06:49 -070082 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
Yingdi Yu53c11c12016-03-20 12:56:49 -070083 m_tray->setIcon(QIcon(DISCONNECT_ICON));
84 m_tray->show();
taylorchuc27dd482014-05-17 20:06:49 -070085}
86
87TrayMenu::~TrayMenu()
88{
89}
90
91Q_INVOKABLE void
taylorchuc27dd482014-05-17 20:06:49 -070092TrayMenu::autoConfig()
93{
Alexander Afanasyev4086d512014-07-11 15:56:33 -070094 std::cout << "auto config" << std::endl;
taylorchuc27dd482014-05-17 20:06:49 -070095 QProcess* proc = new QProcess();
Alexander Afanasyev4086d512014-07-11 15:56:33 -070096 connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -070097 // proc->start(NFD_AUTOCONFIG_COMMAND);
taylorchuc27dd482014-05-17 20:06:49 -070098}
99
100void
101TrayMenu::quitApp()
102{
103 QCoreApplication::exit(0);
104}
105
106void
107TrayMenu::iconActivated(QSystemTrayIcon::ActivationReason reason)
108{
109 switch (reason) {
110 // case QSystemTrayIcon::Trigger:
111 // emit showApp();
112 // break;
113 case QSystemTrayIcon::Context:
114 break;
115 default:
116 break;
117 }
118}
119
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700120Q_INVOKABLE void
121TrayMenu::startStopNfd()
122{
123 if (!m_isNfdRunning) {
124 startNfd();
125 }
126 else {
127 stopNfd();
128 }
129}
130
taylorchuc27dd482014-05-17 20:06:49 -0700131void
132TrayMenu::startNfd()
133{
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700134#ifdef OSX_BUILD
135 QProcess* proc = new QProcess();
136 connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
137 proc->startDetached((QCoreApplication::applicationDirPath().toStdString() + "/../Platform/nfd").c_str(),
138 QStringList()
139 << "--config"
Alexander Afanasyev964feb92016-03-22 13:03:11 -0700140 << (QCoreApplication::applicationDirPath().toStdString() + "/../etc/ndn/nfd.conf").c_str());
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700141// #endif
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -0700142// QProcess * proc = new QProcess();
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700143// connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -0700144// #ifdef __linux__
145// proc->start("gksudo", QStringList() << NFD_START_COMMAND);
146// #else
147// proc->start("osascript", QStringList()
148// << "-e"
149// << "do shell script \"" NFD_START_COMMAND "\" with administrator privileges");
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700150#endif
taylorchuc27dd482014-05-17 20:06:49 -0700151}
152
153void
154TrayMenu::stopNfd()
155{
Alexander Afanasyev8e986f82016-03-21 14:19:15 -0700156#ifdef OSX_BUILD
157 QProcess* proc = new QProcess();
158 connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
159 proc->startDetached("killall", QStringList() << "nfd");
160#endif
taylorchuc27dd482014-05-17 20:06:49 -0700161}
162
susmit4fe3cb92016-03-20 17:08:41 -0700163Q_INVOKABLE void
164TrayMenu::addDeleteRoute()
165{
166 addRoute();
167}
168
169Q_INVOKABLE void
170TrayMenu::addRoute()
171{
172 std::cout << "Adding route" << std::endl;
173 QString cmd = "nfdc register /test tcp4://localhost";
174 QProcess *addNewRoute = new QProcess();
175 connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater()));
176 addNewRoute->start("bash", QStringList() << "-c" << cmd);
177 std::cout << "Done" << std::endl;
178
179
180// QProcess * proc = new QProcess();
181}
182
183void
184TrayMenu::deleteRoute()
185{
186 std::cout << "Deleting route" << std::endl;
187 QString cmd = "nfdc unregister /test tcp4://localhost";
188 QProcess *addNewRoute = new QProcess();
189 connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater()));
190 addNewRoute->start("bash", QStringList() << "-c" << cmd);
191 std::cout << "Done" << std::endl;
192
193}
194
195
taylorchuc27dd482014-05-17 20:06:49 -0700196void
197TrayMenu::updateNfdActivityIcon(bool isActive)
198{
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700199 m_isNfdRunning = isActive;
200
taylorchuc27dd482014-05-17 20:06:49 -0700201 if (isActive) {
Yingdi Yu53c11c12016-03-20 12:56:49 -0700202 m_tray->setIcon(QIcon(CONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700203 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700204 }
205 else {
Yingdi Yu53c11c12016-03-20 12:56:49 -0700206 m_tray->setIcon(QIcon(DISCONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700207 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700208 }
209}
210
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700211void
212TrayMenu::enableCli()
213{
214#ifdef OSX_BUILD
215 AuthorizationRef authorizationRef;
216 OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
217 kAuthorizationFlagDefaults, &authorizationRef);
218 if (status != errAuthorizationSuccess)
219 return;
220
221 AuthorizationItem item = { kAuthorizationRightExecute, 0, 0, 0 };
222 AuthorizationRights rights = { 1, &item };
223 const AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed
224 | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
225
226 status = AuthorizationCopyRights(authorizationRef, &rights, kAuthorizationEmptyEnvironment,
227 flags, 0);
228 if (status != errAuthorizationSuccess)
229 return;
230
231 char const* mkdir_arg[] = { "-p", "/usr/local/bin", nullptr };
232 char const* mkdir = "/bin/mkdir";
233 AuthorizationExecuteWithPrivileges(authorizationRef,
234 mkdir,
235 kAuthorizationFlagDefaults, (char**)mkdir_arg, nullptr);
236
Alexander Afanasyev964feb92016-03-22 13:03:11 -0700237 std::vector<std::string> arguments = { "-f", "-s",
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700238 QCoreApplication::applicationDirPath().toStdString() + "/../Resources/ndn",
239 "/usr/local/bin/ndn" };
240 std::vector<const char*> args;
241 for (const auto& i : arguments) {
242 args.push_back(i.c_str());
243 }
244 args.push_back(nullptr);
245
Qi Zhao0e043e52016-12-05 18:27:09 -0800246 char const* helperTool = "/bin/ln";
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700247 AuthorizationExecuteWithPrivileges(authorizationRef,
248 helperTool,
249 kAuthorizationFlagDefaults,
250 (char**)args.data(), NULL);
251
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700252 AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
253#endif
254}
255
taylorchuc27dd482014-05-17 20:06:49 -0700256} // namespace ndn