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 | * |
| 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 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) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 56 | |
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); |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame^] | 75 | m_menu->addAction(m_entryEnableCli); |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 76 | 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)), |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 80 | this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 81 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
| 82 | m_tray->show(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TrayMenu::~TrayMenu() |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | Q_INVOKABLE void |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 90 | TrayMenu::autoConfig() |
| 91 | { |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 92 | std::cout << "auto config" << std::endl; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 93 | QProcess* proc = new QProcess(); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 94 | connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 95 | // proc->start(NFD_AUTOCONFIG_COMMAND); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
| 99 | TrayMenu::quitApp() |
| 100 | { |
| 101 | QCoreApplication::exit(0); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | TrayMenu::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 Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 118 | Q_INVOKABLE void |
| 119 | TrayMenu::startStopNfd() |
| 120 | { |
| 121 | if (!m_isNfdRunning) { |
| 122 | startNfd(); |
| 123 | } |
| 124 | else { |
| 125 | stopNfd(); |
| 126 | } |
| 127 | } |
| 128 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 129 | void |
| 130 | TrayMenu::startNfd() |
| 131 | { |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 132 | // 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 |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
| 144 | TrayMenu::stopNfd() |
| 145 | { |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 146 | // 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 |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 155 | } |
| 156 | |
susmit | 4fe3cb9 | 2016-03-20 17:08:41 -0700 | [diff] [blame] | 157 | Q_INVOKABLE void |
| 158 | TrayMenu::addDeleteRoute() |
| 159 | { |
| 160 | addRoute(); |
| 161 | } |
| 162 | |
| 163 | Q_INVOKABLE void |
| 164 | TrayMenu::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 | |
| 177 | void |
| 178 | TrayMenu::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 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 190 | void |
| 191 | TrayMenu::updateNfdActivityIcon(bool isActive) |
| 192 | { |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 193 | m_isNfdRunning = isActive; |
| 194 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 195 | if (isActive) { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 196 | m_tray->setIcon(QIcon(CONNECT_ICON)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 197 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 198 | } |
| 199 | else { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 200 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 201 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame^] | 205 | void |
| 206 | TrayMenu::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 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 262 | } // namespace ndn |