taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Qi Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016, Regents of the University of California. |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 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 | * |
Qi Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame^] | 16 | * You should have received a copy of the GNU General Public License along with NFD |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 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 Afanasyev | 0621cec | 2016-03-20 23:18:27 -0700 | [diff] [blame] | 28 | #define CONNECT_ICON ":/res/icon-connected-black.png" |
| 29 | #define DISCONNECT_ICON ":/res/icon-disconnected-black.png" |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 30 | |
| 31 | #include <Security/Authorization.h> |
| 32 | #include <Security/AuthorizationTags.h> |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 33 | #else |
Alexander Afanasyev | 0621cec | 2016-03-20 23:18:27 -0700 | [diff] [blame] | 34 | #define CONNECT_ICON ":/res/icon-connected-white.png" |
| 35 | #define DISCONNECT_ICON ":/res/icon-disconnected-white.png" |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 36 | #endif // OSX_BUILD |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 37 | |
| 38 | #ifdef WAF |
| 39 | #include "tray-menu.moc" |
| 40 | // #include "tray-menu.cpp.moc" |
| 41 | #endif |
| 42 | |
| 43 | namespace ndn { |
| 44 | |
Alexander Afanasyev | 81509f3 | 2016-03-21 17:02:38 -0700 | [diff] [blame] | 45 | TrayMenu::TrayMenu(QQmlContext* context, Face& face) |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 46 | : m_context(context) |
| 47 | , m_isNfdRunning(false) |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 48 | , m_menu(new QMenu(this)) |
| 49 | , m_entryPref(new QAction("Preferences...", m_menu)) |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 50 | , 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 Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 54 | , m_entryQuit(new QAction("Quit", m_menu)) |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 55 | , m_keyViewerDialog(new ncc::KeyViewerDialog) |
Alexander Afanasyev | 81509f3 | 2016-03-21 17:02:38 -0700 | [diff] [blame] | 56 | , m_face(face) |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 57 | { |
| 58 | connect(m_entryPref, SIGNAL(triggered()), this, SIGNAL(showApp())); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 59 | connect(m_entrySec, SIGNAL(triggered()), m_keyViewerDialog, SLOT(present())); |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 60 | connect(m_entryQuit, SIGNAL(triggered()), this, SLOT(quitApp())); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 61 | |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 62 | #ifdef OSX_BUILD |
| 63 | connect(m_entryEnableCli, SIGNAL(triggered()), this, SLOT(enableCli())); |
| 64 | #endif |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 66 | connect(this, SIGNAL(nfdActivityUpdate(bool)), this, SLOT(updateNfdActivityIcon(bool)), |
| 67 | Qt::QueuedConnection); |
| 68 | |
| 69 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 70 | |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 71 | // m_menu->addAction(start); |
| 72 | // m_menu->addAction(stop); |
| 73 | m_menu->addAction(m_entryPref); |
| 74 | m_menu->addAction(m_entrySec); |
susmit | 5060791 | 2016-03-21 12:00:15 -0700 | [diff] [blame] | 75 | #ifdef OSX_BUILD |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 76 | m_menu->addAction(m_entryEnableCli); |
susmit | 5060791 | 2016-03-21 12:00:15 -0700 | [diff] [blame] | 77 | #endif |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 78 | 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)), |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 82 | this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 83 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
| 84 | m_tray->show(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TrayMenu::~TrayMenu() |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | Q_INVOKABLE void |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 92 | TrayMenu::autoConfig() |
| 93 | { |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 94 | std::cout << "auto config" << std::endl; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 95 | QProcess* proc = new QProcess(); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 96 | connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 97 | // proc->start(NFD_AUTOCONFIG_COMMAND); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | TrayMenu::quitApp() |
| 102 | { |
| 103 | QCoreApplication::exit(0); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | TrayMenu::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 Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 120 | Q_INVOKABLE void |
| 121 | TrayMenu::startStopNfd() |
| 122 | { |
| 123 | if (!m_isNfdRunning) { |
| 124 | startNfd(); |
| 125 | } |
| 126 | else { |
| 127 | stopNfd(); |
| 128 | } |
| 129 | } |
| 130 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 131 | void |
| 132 | TrayMenu::startNfd() |
| 133 | { |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 134 | #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 Afanasyev | 964feb9 | 2016-03-22 13:03:11 -0700 | [diff] [blame] | 140 | << (QCoreApplication::applicationDirPath().toStdString() + "/../etc/ndn/nfd.conf").c_str()); |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 141 | // #endif |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 142 | // QProcess * proc = new QProcess(); |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 143 | // connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 144 | // #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 Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 150 | #endif |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void |
| 154 | TrayMenu::stopNfd() |
| 155 | { |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 156 | #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 |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 161 | } |
| 162 | |
susmit | 4fe3cb9 | 2016-03-20 17:08:41 -0700 | [diff] [blame] | 163 | Q_INVOKABLE void |
| 164 | TrayMenu::addDeleteRoute() |
| 165 | { |
| 166 | addRoute(); |
| 167 | } |
| 168 | |
| 169 | Q_INVOKABLE void |
| 170 | TrayMenu::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 | |
| 183 | void |
| 184 | TrayMenu::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 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 196 | void |
| 197 | TrayMenu::updateNfdActivityIcon(bool isActive) |
| 198 | { |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 199 | m_isNfdRunning = isActive; |
| 200 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 201 | if (isActive) { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 202 | m_tray->setIcon(QIcon(CONNECT_ICON)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 203 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 204 | } |
| 205 | else { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 206 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 207 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 211 | void |
| 212 | TrayMenu::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 Afanasyev | 964feb9 | 2016-03-22 13:03:11 -0700 | [diff] [blame] | 237 | std::vector<std::string> arguments = { "-f", "-s", |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 238 | 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 Zhao | 0e043e5 | 2016-12-05 18:27:09 -0800 | [diff] [blame^] | 246 | char const* helperTool = "/bin/ln"; |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 247 | AuthorizationExecuteWithPrivileges(authorizationRef, |
| 248 | helperTool, |
| 249 | kAuthorizationFlagDefaults, |
| 250 | (char**)args.data(), NULL); |
| 251 | |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 252 | AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights); |
| 253 | #endif |
| 254 | } |
| 255 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 256 | } // namespace ndn |