taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [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 | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 45 | TrayMenu::TrayMenu(QQmlContext* context) |
| 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) |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 56 | { |
| 57 | connect(m_entryPref, SIGNAL(triggered()), this, SIGNAL(showApp())); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 58 | connect(m_entrySec, SIGNAL(triggered()), m_keyViewerDialog, SLOT(present())); |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 59 | connect(m_entryQuit, SIGNAL(triggered()), this, SLOT(quitApp())); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 61 | #ifdef OSX_BUILD |
| 62 | connect(m_entryEnableCli, SIGNAL(triggered()), this, SLOT(enableCli())); |
| 63 | #endif |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 64 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 65 | connect(this, SIGNAL(nfdActivityUpdate(bool)), this, SLOT(updateNfdActivityIcon(bool)), |
| 66 | Qt::QueuedConnection); |
| 67 | |
| 68 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 70 | // m_menu->addAction(start); |
| 71 | // m_menu->addAction(stop); |
| 72 | m_menu->addAction(m_entryPref); |
| 73 | m_menu->addAction(m_entrySec); |
susmit | 5060791 | 2016-03-21 12:00:15 -0700 | [diff] [blame] | 74 | #ifdef OSX_BUILD |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 75 | m_menu->addAction(m_entryEnableCli); |
susmit | 5060791 | 2016-03-21 12:00:15 -0700 | [diff] [blame] | 76 | #endif |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 77 | 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)), |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 81 | this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 82 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
| 83 | m_tray->show(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TrayMenu::~TrayMenu() |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | Q_INVOKABLE void |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 91 | TrayMenu::autoConfig() |
| 92 | { |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 93 | std::cout << "auto config" << std::endl; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 94 | QProcess* proc = new QProcess(); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 95 | connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 96 | // proc->start(NFD_AUTOCONFIG_COMMAND); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
| 100 | TrayMenu::quitApp() |
| 101 | { |
| 102 | QCoreApplication::exit(0); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | TrayMenu::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 Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 119 | Q_INVOKABLE void |
| 120 | TrayMenu::startStopNfd() |
| 121 | { |
| 122 | if (!m_isNfdRunning) { |
| 123 | startNfd(); |
| 124 | } |
| 125 | else { |
| 126 | stopNfd(); |
| 127 | } |
| 128 | } |
| 129 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 130 | void |
| 131 | TrayMenu::startNfd() |
| 132 | { |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 133 | #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 Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 141 | // QProcess * proc = new QProcess(); |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 142 | // connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 143 | // #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 Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 149 | #endif |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void |
| 153 | TrayMenu::stopNfd() |
| 154 | { |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 155 | #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 |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 160 | } |
| 161 | |
susmit | 4fe3cb9 | 2016-03-20 17:08:41 -0700 | [diff] [blame] | 162 | Q_INVOKABLE void |
| 163 | TrayMenu::addDeleteRoute() |
| 164 | { |
| 165 | addRoute(); |
| 166 | } |
| 167 | |
| 168 | Q_INVOKABLE void |
| 169 | TrayMenu::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 | |
| 182 | void |
| 183 | TrayMenu::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 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 195 | void |
| 196 | TrayMenu::updateNfdActivityIcon(bool isActive) |
| 197 | { |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 198 | m_isNfdRunning = isActive; |
| 199 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 200 | if (isActive) { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 201 | m_tray->setIcon(QIcon(CONNECT_ICON)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 202 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 203 | } |
| 204 | else { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 205 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 206 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 210 | void |
| 211 | TrayMenu::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 Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 251 | AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights); |
| 252 | #endif |
| 253 | } |
| 254 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 255 | } // namespace ndn |