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