blob: 2254a4e26430552d75f950ef13db43eca829ac81 [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);
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -070075 m_menu->addAction(m_entryEnableCli);
Yingdi Yu53c11c12016-03-20 12:56:49 -070076 m_menu->addAction(m_entryQuit);
77 m_tray = new QSystemTrayIcon(this);
78 m_tray->setContextMenu(m_menu);
79 connect(m_tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
taylorchuc27dd482014-05-17 20:06:49 -070080 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
Yingdi Yu53c11c12016-03-20 12:56:49 -070081 m_tray->setIcon(QIcon(DISCONNECT_ICON));
82 m_tray->show();
taylorchuc27dd482014-05-17 20:06:49 -070083}
84
85TrayMenu::~TrayMenu()
86{
87}
88
89Q_INVOKABLE void
taylorchuc27dd482014-05-17 20:06:49 -070090TrayMenu::autoConfig()
91{
Alexander Afanasyev4086d512014-07-11 15:56:33 -070092 std::cout << "auto config" << std::endl;
taylorchuc27dd482014-05-17 20:06:49 -070093 QProcess* proc = new QProcess();
Alexander Afanasyev4086d512014-07-11 15:56:33 -070094 connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -070095 // proc->start(NFD_AUTOCONFIG_COMMAND);
taylorchuc27dd482014-05-17 20:06:49 -070096}
97
98void
99TrayMenu::quitApp()
100{
101 QCoreApplication::exit(0);
102}
103
104void
105TrayMenu::iconActivated(QSystemTrayIcon::ActivationReason reason)
106{
107 switch (reason) {
108 // case QSystemTrayIcon::Trigger:
109 // emit showApp();
110 // break;
111 case QSystemTrayIcon::Context:
112 break;
113 default:
114 break;
115 }
116}
117
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700118Q_INVOKABLE void
119TrayMenu::startStopNfd()
120{
121 if (!m_isNfdRunning) {
122 startNfd();
123 }
124 else {
125 stopNfd();
126 }
127}
128
taylorchuc27dd482014-05-17 20:06:49 -0700129void
130TrayMenu::startNfd()
131{
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -0700132// QProcess * proc = new QProcess();
133// connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater()));
134// #ifdef __linux__
135// proc->start("gksudo", QStringList() << NFD_START_COMMAND);
136// #else
137// proc->start("osascript", QStringList()
138// << "-e"
139// << "do shell script \"" NFD_START_COMMAND "\" with administrator privileges");
140// #endif
taylorchuc27dd482014-05-17 20:06:49 -0700141}
142
143void
144TrayMenu::stopNfd()
145{
Alexander Afanasyev5f14bee2016-03-20 11:38:07 -0700146// QProcess * proc = new QProcess();
147// connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater()));
148// #ifdef __linux__
149// proc->start("gksudo", QStringList() << NFD_STOP_COMMAND);
150// #else
151// proc->start("osascript", QStringList()
152// << "-e"
153// << "do shell script \"" NFD_STOP_COMMAND "\" with administrator privileges");
154// #endif
taylorchuc27dd482014-05-17 20:06:49 -0700155}
156
susmit4fe3cb92016-03-20 17:08:41 -0700157Q_INVOKABLE void
158TrayMenu::addDeleteRoute()
159{
160 addRoute();
161}
162
163Q_INVOKABLE void
164TrayMenu::addRoute()
165{
166 std::cout << "Adding route" << std::endl;
167 QString cmd = "nfdc register /test tcp4://localhost";
168 QProcess *addNewRoute = new QProcess();
169 connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater()));
170 addNewRoute->start("bash", QStringList() << "-c" << cmd);
171 std::cout << "Done" << std::endl;
172
173
174// QProcess * proc = new QProcess();
175}
176
177void
178TrayMenu::deleteRoute()
179{
180 std::cout << "Deleting route" << std::endl;
181 QString cmd = "nfdc unregister /test tcp4://localhost";
182 QProcess *addNewRoute = new QProcess();
183 connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater()));
184 addNewRoute->start("bash", QStringList() << "-c" << cmd);
185 std::cout << "Done" << std::endl;
186
187}
188
189
taylorchuc27dd482014-05-17 20:06:49 -0700190void
191TrayMenu::updateNfdActivityIcon(bool isActive)
192{
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700193 m_isNfdRunning = isActive;
194
taylorchuc27dd482014-05-17 20:06:49 -0700195 if (isActive) {
Yingdi Yu53c11c12016-03-20 12:56:49 -0700196 m_tray->setIcon(QIcon(CONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700197 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700198 }
199 else {
Yingdi Yu53c11c12016-03-20 12:56:49 -0700200 m_tray->setIcon(QIcon(DISCONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700201 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700202 }
203}
204
Alexander Afanasyev11ae34d2016-03-21 11:55:16 -0700205void
206TrayMenu::enableCli()
207{
208#ifdef OSX_BUILD
209 AuthorizationRef authorizationRef;
210 OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
211 kAuthorizationFlagDefaults, &authorizationRef);
212 if (status != errAuthorizationSuccess)
213 return;
214
215 AuthorizationItem item = { kAuthorizationRightExecute, 0, 0, 0 };
216 AuthorizationRights rights = { 1, &item };
217 const AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed
218 | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
219
220 status = AuthorizationCopyRights(authorizationRef, &rights, kAuthorizationEmptyEnvironment,
221 flags, 0);
222 if (status != errAuthorizationSuccess)
223 return;
224
225 char const* mkdir_arg[] = { "-p", "/usr/local/bin", nullptr };
226 char const* mkdir = "/bin/mkdir";
227 AuthorizationExecuteWithPrivileges(authorizationRef,
228 mkdir,
229 kAuthorizationFlagDefaults, (char**)mkdir_arg, nullptr);
230
231 std::vector<std::string> arguments = { "-f",
232 QCoreApplication::applicationDirPath().toStdString() + "/../Resources/ndn",
233 "/usr/local/bin/ndn" };
234 std::vector<const char*> args;
235 for (const auto& i : arguments) {
236 args.push_back(i.c_str());
237 }
238 args.push_back(nullptr);
239
240 char const* helperTool = "/bin/cp";
241 AuthorizationExecuteWithPrivileges(authorizationRef,
242 helperTool,
243 kAuthorizationFlagDefaults,
244 (char**)args.data(), NULL);
245
246 // QVector<char *> args;
247 // QVector<QByteArray> utf8Args;
248 // for (const QString &argument : arguments) {
249 // utf8Args.push_back(argument.toUtf8());
250 // args.push_back(utf8Args.last().data());
251 // }
252 // args.push_back(0);
253
254 // const QByteArray utf8Program = program.toUtf8();
255 // status = AuthorizationExecuteWithPrivileges(authorizationRef, utf8Program.data(),
256 // kAuthorizationFlagDefaults, args.data(), 0);
257
258 AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
259#endif
260}
261
taylorchuc27dd482014-05-17 20:06:49 -0700262} // namespace ndn