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" |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 30 | #else |
Alexander Afanasyev | 0621cec | 2016-03-20 23:18:27 -0700 | [diff] [blame] | 31 | #define CONNECT_ICON ":/res/icon-connected-white.png" |
| 32 | #define DISCONNECT_ICON ":/res/icon-disconnected-white.png" |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | #ifdef WAF |
| 36 | #include "tray-menu.moc" |
| 37 | // #include "tray-menu.cpp.moc" |
| 38 | #endif |
| 39 | |
| 40 | namespace ndn { |
| 41 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 42 | TrayMenu::TrayMenu(QQmlContext* context) |
| 43 | : m_context(context) |
| 44 | , m_isNfdRunning(false) |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 45 | , m_menu(new QMenu(this)) |
| 46 | , m_entryPref(new QAction("Preferences...", m_menu)) |
| 47 | , m_entrySec(new QAction("Security", m_menu)) |
| 48 | , m_entryQuit(new QAction("Quit", m_menu)) |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 49 | , m_keyViewerDialog(new ncc::KeyViewerDialog) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 50 | |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 51 | { |
| 52 | connect(m_entryPref, SIGNAL(triggered()), this, SIGNAL(showApp())); |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 53 | connect(m_entrySec, SIGNAL(triggered()), m_keyViewerDialog, SLOT(present())); |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 54 | connect(m_entryQuit, SIGNAL(triggered()), this, SLOT(quitApp())); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 55 | |
Yingdi Yu | 1f82464 | 2016-03-20 17:07:22 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 57 | connect(this, SIGNAL(nfdActivityUpdate(bool)), this, SLOT(updateNfdActivityIcon(bool)), |
| 58 | Qt::QueuedConnection); |
| 59 | |
| 60 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 61 | |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 62 | // m_menu->addAction(start); |
| 63 | // m_menu->addAction(stop); |
| 64 | m_menu->addAction(m_entryPref); |
| 65 | m_menu->addAction(m_entrySec); |
| 66 | m_menu->addAction(m_entryQuit); |
| 67 | m_tray = new QSystemTrayIcon(this); |
| 68 | m_tray->setContextMenu(m_menu); |
| 69 | connect(m_tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 70 | this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 71 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
| 72 | m_tray->show(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TrayMenu::~TrayMenu() |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | Q_INVOKABLE void |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 80 | TrayMenu::autoConfig() |
| 81 | { |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 82 | std::cout << "auto config" << std::endl; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 83 | QProcess* proc = new QProcess(); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 84 | connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 85 | // proc->start(NFD_AUTOCONFIG_COMMAND); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void |
| 89 | TrayMenu::quitApp() |
| 90 | { |
| 91 | QCoreApplication::exit(0); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | TrayMenu::iconActivated(QSystemTrayIcon::ActivationReason reason) |
| 96 | { |
| 97 | switch (reason) { |
| 98 | // case QSystemTrayIcon::Trigger: |
| 99 | // emit showApp(); |
| 100 | // break; |
| 101 | case QSystemTrayIcon::Context: |
| 102 | break; |
| 103 | default: |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 108 | Q_INVOKABLE void |
| 109 | TrayMenu::startStopNfd() |
| 110 | { |
| 111 | if (!m_isNfdRunning) { |
| 112 | startNfd(); |
| 113 | } |
| 114 | else { |
| 115 | stopNfd(); |
| 116 | } |
| 117 | } |
| 118 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 119 | void |
| 120 | TrayMenu::startNfd() |
| 121 | { |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 122 | // QProcess * proc = new QProcess(); |
| 123 | // connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
| 124 | // #ifdef __linux__ |
| 125 | // proc->start("gksudo", QStringList() << NFD_START_COMMAND); |
| 126 | // #else |
| 127 | // proc->start("osascript", QStringList() |
| 128 | // << "-e" |
| 129 | // << "do shell script \"" NFD_START_COMMAND "\" with administrator privileges"); |
| 130 | // #endif |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
| 134 | TrayMenu::stopNfd() |
| 135 | { |
Alexander Afanasyev | 5f14bee | 2016-03-20 11:38:07 -0700 | [diff] [blame] | 136 | // QProcess * proc = new QProcess(); |
| 137 | // connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater())); |
| 138 | // #ifdef __linux__ |
| 139 | // proc->start("gksudo", QStringList() << NFD_STOP_COMMAND); |
| 140 | // #else |
| 141 | // proc->start("osascript", QStringList() |
| 142 | // << "-e" |
| 143 | // << "do shell script \"" NFD_STOP_COMMAND "\" with administrator privileges"); |
| 144 | // #endif |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 145 | } |
| 146 | |
susmit | 4fe3cb9 | 2016-03-20 17:08:41 -0700 | [diff] [blame] | 147 | Q_INVOKABLE void |
| 148 | TrayMenu::addDeleteRoute() |
| 149 | { |
| 150 | addRoute(); |
| 151 | } |
| 152 | |
| 153 | Q_INVOKABLE void |
| 154 | TrayMenu::addRoute() |
| 155 | { |
| 156 | std::cout << "Adding route" << std::endl; |
| 157 | QString cmd = "nfdc register /test tcp4://localhost"; |
| 158 | QProcess *addNewRoute = new QProcess(); |
| 159 | connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater())); |
| 160 | addNewRoute->start("bash", QStringList() << "-c" << cmd); |
| 161 | std::cout << "Done" << std::endl; |
| 162 | |
| 163 | |
| 164 | // QProcess * proc = new QProcess(); |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | TrayMenu::deleteRoute() |
| 169 | { |
| 170 | std::cout << "Deleting route" << std::endl; |
| 171 | QString cmd = "nfdc unregister /test tcp4://localhost"; |
| 172 | QProcess *addNewRoute = new QProcess(); |
| 173 | connect(addNewRoute,SIGNAL(finished(int)), addNewRoute, SLOT(deleteLater())); |
| 174 | addNewRoute->start("bash", QStringList() << "-c" << cmd); |
| 175 | std::cout << "Done" << std::endl; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 180 | void |
| 181 | TrayMenu::updateNfdActivityIcon(bool isActive) |
| 182 | { |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 183 | m_isNfdRunning = isActive; |
| 184 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 185 | if (isActive) { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 186 | m_tray->setIcon(QIcon(CONNECT_ICON)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 187 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 188 | } |
| 189 | else { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 190 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 191 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | } // namespace ndn |