taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 3766775 | 2017-02-02 13:52:12 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, 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 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 20 | #include "tray-menu.hpp" |
Alexander Afanasyev | 3766775 | 2017-02-02 13:52:12 -0800 | [diff] [blame] | 21 | #include "tray-menu.moc" |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 22 | |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 23 | #include <chrono> |
| 24 | #include <functional> |
| 25 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 26 | #include <ndn-cxx/face.hpp> |
| 27 | #include <ndn-cxx/interest.hpp> |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 28 | #include <boost/algorithm/string/replace.hpp> |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 29 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 30 | #ifdef OSX_BUILD |
Alexander Afanasyev | 0621cec | 2016-03-20 23:18:27 -0700 | [diff] [blame] | 31 | #define CONNECT_ICON ":/res/icon-connected-black.png" |
| 32 | #define DISCONNECT_ICON ":/res/icon-disconnected-black.png" |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 33 | |
| 34 | #include <Security/Authorization.h> |
| 35 | #include <Security/AuthorizationTags.h> |
Qi Zhao | 3615a10 | 2017-02-02 20:38:34 -0800 | [diff] [blame] | 36 | |
| 37 | #include "build/NFD/build/core/version.hpp" |
| 38 | #include "build/ndn-tools/build/core/version.cpp" |
| 39 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 40 | #else |
Alexander Afanasyev | 0621cec | 2016-03-20 23:18:27 -0700 | [diff] [blame] | 41 | #define CONNECT_ICON ":/res/icon-connected-white.png" |
| 42 | #define DISCONNECT_ICON ":/res/icon-disconnected-white.png" |
Qi Zhao | 3615a10 | 2017-02-02 20:38:34 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 44 | #endif // OSX_BUILD |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 45 | |
Qi Zhao | 3615a10 | 2017-02-02 20:38:34 -0800 | [diff] [blame] | 46 | #include <ndn-cxx/version.hpp> |
| 47 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 48 | namespace ndn { |
Alexander Afanasyev | fda42a8 | 2017-02-01 18:03:39 -0800 | [diff] [blame] | 49 | namespace ncc { |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 50 | |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 51 | /** |
| 52 | * @brief Maximum number of lines that can show up in auto-config status tab view |
| 53 | */ |
| 54 | const int MAX_LINES_IN_AUTOCONF_STATUS = 100; |
| 55 | |
| 56 | #ifdef OSX_BUILD |
| 57 | const QString AUTO_START_SUFFIX = "Library/LaunchAgents/net.named-data.control-center.plist"; |
| 58 | #endif // OSX_BUILD |
| 59 | |
Alexander Afanasyev | 81509f3 | 2016-03-21 17:02:38 -0700 | [diff] [blame] | 60 | TrayMenu::TrayMenu(QQmlContext* context, Face& face) |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 61 | : m_context(context) |
| 62 | , m_isNfdRunning(false) |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 63 | , m_menu(new QMenu(this)) |
| 64 | , m_entryPref(new QAction("Preferences...", m_menu)) |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 65 | , m_entrySec(new QAction("Security...", m_menu)) |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 66 | , m_acProc(nullptr) |
Alexander Afanasyev | fda42a8 | 2017-02-01 18:03:39 -0800 | [diff] [blame] | 67 | , m_settings(new QSettings()) |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 68 | #ifdef OSX_BUILD |
| 69 | , m_entryEnableCli(new QAction("Enable Command Terminal Usage...", m_menu)) |
Alexander Afanasyev | fda42a8 | 2017-02-01 18:03:39 -0800 | [diff] [blame] | 70 | , m_checkForUpdates(new QAction("Check for updates", m_menu)) |
Alexander Afanasyev | e095ed1 | 2017-02-04 23:09:55 -0800 | [diff] [blame] | 71 | , m_sparkle(NCC_APPCAST) |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 72 | #endif |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 73 | , m_entryQuit(new QAction("Quit", m_menu)) |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 74 | , m_keyViewerDialog(new ncc::KeyViewerDialog) |
Alexander Afanasyev | 81509f3 | 2016-03-21 17:02:38 -0700 | [diff] [blame] | 75 | , m_face(face) |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 76 | { |
| 77 | connect(m_entryPref, SIGNAL(triggered()), this, SIGNAL(showApp())); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 78 | connect(m_entrySec, SIGNAL(triggered()), m_keyViewerDialog, SLOT(present())); |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 79 | connect(m_entryQuit, SIGNAL(triggered()), this, SLOT(quitApp())); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 80 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 81 | connect(this, SIGNAL(nfdActivityUpdate(bool)), this, SLOT(updateNfdActivityIcon(bool)), |
| 82 | Qt::QueuedConnection); |
| 83 | |
Qi Zhao | 3615a10 | 2017-02-02 20:38:34 -0800 | [diff] [blame] | 84 | QString nccVersion = QString(NCC_VERSION) + " (ndn-cxx: " + NDN_CXX_VERSION_BUILD_STRING + |
| 85 | ", NFD: " + NFD_VERSION_BUILD_STRING + |
| 86 | ", ndn-tools: " + ::ndn::tools::VERSION + |
| 87 | ")"; |
| 88 | |
| 89 | m_context->setContextProperty("nccVersion", nccVersion); |
| 90 | |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 91 | m_menu->addAction(m_entryPref); |
| 92 | m_menu->addAction(m_entrySec); |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 93 | |
susmit | 5060791 | 2016-03-21 12:00:15 -0700 | [diff] [blame] | 94 | #ifdef OSX_BUILD |
Alexander Afanasyev | fda42a8 | 2017-02-01 18:03:39 -0800 | [diff] [blame] | 95 | connect(m_entryEnableCli, SIGNAL(triggered()), this, SLOT(enableCli())); |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 96 | m_menu->addAction(m_entryEnableCli); |
Alexander Afanasyev | fda42a8 | 2017-02-01 18:03:39 -0800 | [diff] [blame] | 97 | |
| 98 | connect(m_checkForUpdates, SIGNAL(triggered()), this, SLOT(checkForUpdates())); |
| 99 | m_menu->addAction(m_checkForUpdates); |
susmit | 5060791 | 2016-03-21 12:00:15 -0700 | [diff] [blame] | 100 | #endif |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 101 | |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 102 | m_menu->addAction(m_entryQuit); |
| 103 | m_tray = new QSystemTrayIcon(this); |
| 104 | m_tray->setContextMenu(m_menu); |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 105 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
| 106 | m_tray->show(); |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 107 | |
| 108 | // delaying for the case when NFD is already started (to avoid starting / to avoid starting autoconfig |
| 109 | if (isNfdAutoStartEnabled()) { |
| 110 | scheduleDelayedTask(std::chrono::seconds(5), [this] { |
| 111 | if (!m_isNfdRunning) { |
| 112 | startNfd(); |
| 113 | } |
| 114 | }); |
Qi Zhao | 86f2b21 | 2016-12-06 12:44:16 -0800 | [diff] [blame] | 115 | } |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | TrayMenu::~TrayMenu() |
| 119 | { |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 120 | // delete for all new's |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 123 | //////////////////////////////////////////////////////////////////////////////// |
| 124 | // NFD Control Center start on login |
Qi Zhao | 86f2b21 | 2016-12-06 12:44:16 -0800 | [diff] [blame] | 125 | |
| 126 | Q_INVOKABLE bool |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 127 | TrayMenu::isNccAutoStartEnabled() const |
Qi Zhao | 86f2b21 | 2016-12-06 12:44:16 -0800 | [diff] [blame] | 128 | { |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 129 | QFileInfo file(QDir::home().path() + "/" + AUTO_START_SUFFIX); |
| 130 | return file.exists() && file.isFile(); |
Qi Zhao | 86f2b21 | 2016-12-06 12:44:16 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | Q_INVOKABLE void |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 134 | TrayMenu::enableDisableNccAutoStart(bool isEnabled) |
Qi Zhao | 86f2b21 | 2016-12-06 12:44:16 -0800 | [diff] [blame] | 135 | { |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 136 | if (isEnabled) { |
| 137 | QFile file(QDir::home().path() + "/" + AUTO_START_SUFFIX); |
| 138 | file.open(QIODevice::WriteOnly); |
| 139 | |
| 140 | std::string plist = R"PLIST(<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 141 | <?xml version="1.0" encoding="UTF-8"?> |
| 142 | <plist version="1.0"> |
| 143 | <dict> |
| 144 | <key>Label</key> |
| 145 | <string>net.named-data.control-center</string> |
| 146 | <key>ProgramArguments</key> |
| 147 | <array> |
| 148 | <string>%%PATH%%</string> |
| 149 | </array> |
| 150 | <key>RunAtLoad</key> |
| 151 | <true/> |
| 152 | <key>KeepAlive</key> |
| 153 | <false/> |
| 154 | </dict> |
| 155 | </plist> |
| 156 | )PLIST"; //" |
| 157 | boost::replace_all(plist, "%%PATH%%", QCoreApplication::applicationFilePath().toStdString()); |
| 158 | file.write(plist.data(), plist.size()); |
| 159 | file.close(); |
| 160 | } else { |
| 161 | QFile::remove(QDir::home().path() + "/" + AUTO_START_SUFFIX); |
Qi Zhao | 86f2b21 | 2016-12-06 12:44:16 -0800 | [diff] [blame] | 162 | } |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 165 | //////////////////////////////////////////////////////////////////////////////// |
| 166 | // NFD start on launch |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 167 | |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 168 | Q_INVOKABLE bool |
| 169 | TrayMenu::isNfdAutoStartEnabled() const |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 170 | { |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 171 | return m_settings->value("ENABLE_AUTO_START", false).toBool(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 174 | Q_INVOKABLE void |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 175 | TrayMenu::enableDisableNfdAutoStart(bool isEnabled) |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 176 | { |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 177 | m_settings->setValue("ENABLE_AUTO_START", isEnabled); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 178 | if (!m_isNfdRunning) { |
| 179 | startNfd(); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 183 | void |
| 184 | TrayMenu::startNfd() |
| 185 | { |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 186 | #ifdef OSX_BUILD |
| 187 | QProcess* proc = new QProcess(); |
| 188 | connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 189 | proc->startDetached((QCoreApplication::applicationDirPath() + "/../Platform/nfd"), |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 190 | QStringList() |
| 191 | << "--config" |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 192 | << (QCoreApplication::applicationDirPath() + "/../etc/ndn/nfd.conf")); |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 193 | #endif |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void |
| 197 | TrayMenu::stopNfd() |
| 198 | { |
Alexander Afanasyev | 8e986f8 | 2016-03-21 14:19:15 -0700 | [diff] [blame] | 199 | #ifdef OSX_BUILD |
| 200 | QProcess* proc = new QProcess(); |
| 201 | connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
| 202 | proc->startDetached("killall", QStringList() << "nfd"); |
| 203 | #endif |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 206 | //////////////////////////////////////////////////////////////////////////////// |
| 207 | // NDN Autoconfig |
| 208 | |
| 209 | Q_INVOKABLE bool |
| 210 | TrayMenu::isNdnAutoConfigEnabled() const |
| 211 | { |
| 212 | return m_settings->value("ENABLE_NDN_AUTO_CONFIG", false).toBool(); |
| 213 | } |
| 214 | |
| 215 | Q_INVOKABLE void |
| 216 | TrayMenu::enableDisableNdnAutoConfig(bool isEnabled) |
| 217 | { |
| 218 | m_settings->setValue("ENABLE_NDN_AUTO_CONFIG", isEnabled); |
| 219 | |
| 220 | if (isEnabled) { |
| 221 | startNdnAutoConfig(); |
| 222 | } |
| 223 | else { |
| 224 | stopNdnAutoConfig(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | TrayMenu::startNdnAutoConfig() |
| 230 | { |
| 231 | if (m_acProc != nullptr) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | stopNdnAutoConfig(false); // Make sure no more than one auto-config process exist |
| 236 | |
| 237 | if (!m_isNfdRunning) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | m_acProc = new QProcess(); |
| 242 | |
| 243 | scheduleDelayedTask(std::chrono::seconds(2), [this] { |
| 244 | appendNdnAutoConfigStatus("NDN auto configuration starting...\n"); |
| 245 | m_acProc->start(QCoreApplication::applicationDirPath() + "/../Platform/ndn-autoconfig", |
| 246 | QStringList() << "--daemon"); |
| 247 | connect(m_acProc, SIGNAL(readyReadStandardOutput()), this, SLOT(processOutput())); |
| 248 | connect(m_acProc, SIGNAL(readyReadStandardError()), this, SLOT(processOutput())); |
| 249 | }); |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | TrayMenu::stopNdnAutoConfig(bool appendStatus) |
| 254 | { |
| 255 | m_acProc = nullptr; |
| 256 | QProcess* proc = new QProcess(); |
| 257 | connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
| 258 | proc->startDetached("killall", QStringList() << "ndn-autoconfig"); |
| 259 | if (appendStatus) { |
| 260 | appendNdnAutoConfigStatus("NDN auto configuration stopped\n"); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | TrayMenu::appendNdnAutoConfigStatus(const QString& message) |
| 266 | { |
| 267 | if (message == "") { |
| 268 | return; |
| 269 | } |
| 270 | if (m_ndnAutoConfigMsg.count("\n") > MAX_LINES_IN_AUTOCONF_STATUS) { |
| 271 | int end = m_ndnAutoConfigMsg.indexOf("\n"); |
| 272 | m_ndnAutoConfigMsg.remove(0, end + 1); |
| 273 | } |
| 274 | m_ndnAutoConfigMsg.append(message); |
| 275 | |
| 276 | m_context->setContextProperty("ndnAutoConfigText", m_ndnAutoConfigMsg); |
| 277 | } |
| 278 | |
| 279 | void |
| 280 | TrayMenu::processOutput() |
| 281 | { |
| 282 | if (m_acProc == nullptr) { |
| 283 | return; |
| 284 | } |
| 285 | appendNdnAutoConfigStatus(m_acProc->readAllStandardOutput()); |
| 286 | appendNdnAutoConfigStatus(m_acProc->readAllStandardError()); |
| 287 | } |
| 288 | |
| 289 | //////////////////////////////////////////////////////////////////////////////// |
| 290 | // NFD stop on shutdown |
| 291 | |
| 292 | Q_INVOKABLE bool |
| 293 | TrayMenu::isNfdStopOnExitEnabled() const |
| 294 | { |
| 295 | return m_settings->value("ENABLE_SHUTDOWN", false).toBool(); |
| 296 | } |
| 297 | |
| 298 | Q_INVOKABLE void |
| 299 | TrayMenu::enableDisableNfdStopOnExit(bool isEnabled) |
| 300 | { |
| 301 | m_settings->setValue("ENABLE_SHUTDOWN", isEnabled); |
| 302 | } |
| 303 | |
| 304 | //////////////////////////////////////////////////////////////////////////////// |
| 305 | // Misc |
| 306 | |
| 307 | void |
| 308 | TrayMenu::quitApp() |
| 309 | { |
| 310 | if (isNfdStopOnExitEnabled()) { |
| 311 | stopNdnAutoConfig(); |
| 312 | stopNfd(); |
| 313 | } |
| 314 | QCoreApplication::exit(0); |
| 315 | } |
| 316 | |
susmit | 4fe3cb9 | 2016-03-20 17:08:41 -0700 | [diff] [blame] | 317 | Q_INVOKABLE void |
| 318 | TrayMenu::addDeleteRoute() |
| 319 | { |
| 320 | addRoute(); |
| 321 | } |
| 322 | |
| 323 | Q_INVOKABLE void |
| 324 | TrayMenu::addRoute() |
| 325 | { |
| 326 | std::cout << "Adding route" << std::endl; |
| 327 | QString cmd = "nfdc register /test tcp4://localhost"; |
| 328 | QProcess *addNewRoute = new QProcess(); |
| 329 | connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater())); |
| 330 | addNewRoute->start("bash", QStringList() << "-c" << cmd); |
| 331 | std::cout << "Done" << std::endl; |
susmit | 4fe3cb9 | 2016-03-20 17:08:41 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void |
| 335 | TrayMenu::deleteRoute() |
| 336 | { |
| 337 | std::cout << "Deleting route" << std::endl; |
| 338 | QString cmd = "nfdc unregister /test tcp4://localhost"; |
| 339 | QProcess *addNewRoute = new QProcess(); |
| 340 | connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater())); |
| 341 | addNewRoute->start("bash", QStringList() << "-c" << cmd); |
| 342 | std::cout << "Done" << std::endl; |
susmit | 4fe3cb9 | 2016-03-20 17:08:41 -0700 | [diff] [blame] | 343 | } |
| 344 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 345 | void |
| 346 | TrayMenu::updateNfdActivityIcon(bool isActive) |
| 347 | { |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 348 | m_isNfdRunning = isActive; |
| 349 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 350 | if (isActive) { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 351 | m_tray->setIcon(QIcon(CONNECT_ICON)); |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 352 | if (isNdnAutoConfigEnabled()) { |
| 353 | startNdnAutoConfig(); |
| 354 | } |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 355 | } |
| 356 | else { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 357 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 361 | void |
| 362 | TrayMenu::enableCli() |
| 363 | { |
| 364 | #ifdef OSX_BUILD |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 365 | QProcess* proc = new QProcess(); |
| 366 | connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 367 | |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 368 | proc->start("osascript", QStringList() |
| 369 | << "-e" |
| 370 | << "do shell script \"" |
| 371 | "/bin/mkdir -vp /usr/local/bin; " |
| 372 | "/bin/ln -s -f '" + QCoreApplication::applicationDirPath() + |
| 373 | "/../Resources/ndn" + "' /usr/local/bin/ndn;" |
| 374 | "\" with administrator privileges"); |
Alexander Afanasyev | 11ae34d | 2016-03-21 11:55:16 -0700 | [diff] [blame] | 375 | #endif |
| 376 | } |
| 377 | |
Alexander Afanasyev | fda42a8 | 2017-02-01 18:03:39 -0800 | [diff] [blame] | 378 | #ifdef OSX_BUILD |
| 379 | void |
| 380 | TrayMenu::checkForUpdates() |
| 381 | { |
| 382 | m_sparkle.checkForUpdates(); |
| 383 | } |
| 384 | #endif // OSX_BUILD |
| 385 | |
Qi Zhao | ae9c431 | 2017-02-02 11:36:12 -0800 | [diff] [blame] | 386 | void |
| 387 | TrayMenu::scheduleDelayedTask(std::chrono::milliseconds delays, const std::function<void()>& task) |
| 388 | { |
| 389 | auto timer = new QTimer(); |
| 390 | timer->setSingleShot(true); |
| 391 | timer->setInterval(delays); |
| 392 | connect(timer, &QTimer::timeout, |
| 393 | [task, timer] { |
| 394 | task(); |
| 395 | timer->deleteLater(); |
| 396 | }); |
| 397 | timer->start(); |
| 398 | } |
| 399 | |
Alexander Afanasyev | fda42a8 | 2017-02-01 18:03:39 -0800 | [diff] [blame] | 400 | } // namespace ncc |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 401 | } // namespace ndn |