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