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 | |
| 144 | void |
| 145 | TrayMenu::updateNfdActivityIcon(bool isActive) |
| 146 | { |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 147 | m_isNfdRunning = isActive; |
| 148 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 149 | if (isActive) { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 150 | m_tray->setIcon(QIcon(CONNECT_ICON)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 151 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 152 | } |
| 153 | else { |
Yingdi Yu | 53c11c1 | 2016-03-20 12:56:49 -0700 | [diff] [blame] | 154 | m_tray->setIcon(QIcon(DISCONNECT_ICON)); |
Alexander Afanasyev | 4086d51 | 2014-07-11 15:56:33 -0700 | [diff] [blame] | 155 | m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD"))); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | } // namespace ndn |