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