blob: 2a1b32ac9985a8b97830dc1ae9fd73768f217e11 [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"
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)
taylorchuc27dd482014-05-17 20:06:49 -070056
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 Afanasyev5f14bee2016-03-20 11:38:07 -0700134// QProcess * proc = new QProcess();
135// connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater()));
136// #ifdef __linux__
137// proc->start("gksudo", QStringList() << NFD_START_COMMAND);
138// #else
139// proc->start("osascript", QStringList()
140// << "-e"
141// << "do shell script \"" NFD_START_COMMAND "\" with administrator privileges");
142// #endif
taylorchuc27dd482014-05-17 20:06:49 -0700143}
144
145void
146TrayMenu::stopNfd()
147{
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -0700148// QProcess * proc = new QProcess();
149// connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater()));
150// #ifdef __linux__
151// proc->start("gksudo", QStringList() << NFD_STOP_COMMAND);
152// #else
153// proc->start("osascript", QStringList()
154// << "-e"
155// << "do shell script \"" NFD_STOP_COMMAND "\" with administrator privileges");
156// #endif
taylorchuc27dd482014-05-17 20:06:49 -0700157}
158
susmit4fe3cb92016-03-20 17:08:41 -0700159Q_INVOKABLE void
160TrayMenu::addDeleteRoute()
161{
162 addRoute();
163}
164
165Q_INVOKABLE void
166TrayMenu::addRoute()
167{
168 std::cout << "Adding route" << std::endl;
169 QString cmd = "nfdc register /test tcp4://localhost";
170 QProcess *addNewRoute = new QProcess();
171 connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater()));
172 addNewRoute->start("bash", QStringList() << "-c" << cmd);
173 std::cout << "Done" << std::endl;
174
175
176// QProcess * proc = new QProcess();
177}
178
179void
180TrayMenu::deleteRoute()
181{
182 std::cout << "Deleting route" << std::endl;
183 QString cmd = "nfdc unregister /test tcp4://localhost";
184 QProcess *addNewRoute = new QProcess();
185 connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater()));
186 addNewRoute->start("bash", QStringList() << "-c" << cmd);
187 std::cout << "Done" << std::endl;
188
189}
190
191
taylorchuc27dd482014-05-17 20:06:49 -0700192void
193TrayMenu::updateNfdActivityIcon(bool isActive)
194{
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700195 m_isNfdRunning = isActive;
196
taylorchuc27dd482014-05-17 20:06:49 -0700197 if (isActive) {
Yingdi Yu53c11c12016-03-20 12:56:49 -0700198 m_tray->setIcon(QIcon(CONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700199 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700200 }
201 else {
Yingdi Yu53c11c12016-03-20 12:56:49 -0700202 m_tray->setIcon(QIcon(DISCONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700203 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700204 }
205}
206
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700207void
208TrayMenu::enableCli()
209{
210#ifdef OSX_BUILD
211 AuthorizationRef authorizationRef;
212 OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
213 kAuthorizationFlagDefaults, &authorizationRef);
214 if (status != errAuthorizationSuccess)
215 return;
216
217 AuthorizationItem item = { kAuthorizationRightExecute, 0, 0, 0 };
218 AuthorizationRights rights = { 1, &item };
219 const AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed
220 | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
221
222 status = AuthorizationCopyRights(authorizationRef, &rights, kAuthorizationEmptyEnvironment,
223 flags, 0);
224 if (status != errAuthorizationSuccess)
225 return;
226
227 char const* mkdir_arg[] = { "-p", "/usr/local/bin", nullptr };
228 char const* mkdir = "/bin/mkdir";
229 AuthorizationExecuteWithPrivileges(authorizationRef,
230 mkdir,
231 kAuthorizationFlagDefaults, (char**)mkdir_arg, nullptr);
232
233 std::vector<std::string> arguments = { "-f",
234 QCoreApplication::applicationDirPath().toStdString() + "/../Resources/ndn",
235 "/usr/local/bin/ndn" };
236 std::vector<const char*> args;
237 for (const auto& i : arguments) {
238 args.push_back(i.c_str());
239 }
240 args.push_back(nullptr);
241
242 char const* helperTool = "/bin/cp";
243 AuthorizationExecuteWithPrivileges(authorizationRef,
244 helperTool,
245 kAuthorizationFlagDefaults,
246 (char**)args.data(), NULL);
247
248 // QVector<char *> args;
249 // QVector<QByteArray> utf8Args;
250 // for (const QString &argument : arguments) {
251 // utf8Args.push_back(argument.toUtf8());
252 // args.push_back(utf8Args.last().data());
253 // }
254 // args.push_back(0);
255
256 // const QByteArray utf8Program = program.toUtf8();
257 // status = AuthorizationExecuteWithPrivileges(authorizationRef, utf8Program.data(),
258 // kAuthorizationFlagDefaults, args.data(), 0);
259
260 AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
261#endif
262}
263
taylorchuc27dd482014-05-17 20:06:49 -0700264} // namespace ndn