blob: 386a64f73b00a3f2a42a383ca8de49bf95577cf9 [file] [log] [blame]
taylorchuc27dd482014-05-17 20:06:49 -07001/* -*- 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
40namespace ndn {
41
Alexander Afanasyev4086d512014-07-11 15:56:33 -070042TrayMenu::TrayMenu(QQmlContext* context)
43 : m_context(context)
44 , m_isNfdRunning(false)
taylorchuc27dd482014-05-17 20:06:49 -070045{
46 menu = new QMenu(this);
47 pref = new QAction("Preferences...", menu);
48 quit = new QAction("Quit", menu);
49
taylorchuc27dd482014-05-17 20:06:49 -070050 connect(pref, SIGNAL(triggered()), this, SIGNAL(showApp()));
51 connect(quit, SIGNAL(triggered()), this, SLOT(quitApp()));
52
Alexander Afanasyev4086d512014-07-11 15:56:33 -070053 connect(this, SIGNAL(nfdActivityUpdate(bool)), this, SLOT(updateNfdActivityIcon(bool)),
54 Qt::QueuedConnection);
55
56 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD")));
taylorchuc27dd482014-05-17 20:06:49 -070057
58 // menu->addAction(start);
59 // menu->addAction(stop);
60 menu->addAction(pref);
61 menu->addAction(quit);
62 tray = new QSystemTrayIcon(this);
63 tray->setContextMenu(menu);
64 connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
65 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
66 tray->setIcon(QIcon(DISCONNECT_ICON));
67 tray->show();
68}
69
70TrayMenu::~TrayMenu()
71{
72}
73
74Q_INVOKABLE void
taylorchuc27dd482014-05-17 20:06:49 -070075TrayMenu::autoConfig()
76{
Alexander Afanasyev4086d512014-07-11 15:56:33 -070077 std::cout << "auto config" << std::endl;
taylorchuc27dd482014-05-17 20:06:49 -070078 QProcess* proc = new QProcess();
Alexander Afanasyev4086d512014-07-11 15:56:33 -070079 connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
taylorchuc27dd482014-05-17 20:06:49 -070080 proc->start(NFD_AUTOCONFIG_COMMAND);
81}
82
83void
84TrayMenu::quitApp()
85{
86 QCoreApplication::exit(0);
87}
88
89void
90TrayMenu::iconActivated(QSystemTrayIcon::ActivationReason reason)
91{
92 switch (reason) {
93 // case QSystemTrayIcon::Trigger:
94 // emit showApp();
95 // break;
96 case QSystemTrayIcon::Context:
97 break;
98 default:
99 break;
100 }
101}
102
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700103Q_INVOKABLE void
104TrayMenu::startStopNfd()
105{
106 if (!m_isNfdRunning) {
107 startNfd();
108 }
109 else {
110 stopNfd();
111 }
112}
113
taylorchuc27dd482014-05-17 20:06:49 -0700114void
115TrayMenu::startNfd()
116{
117 QProcess * proc = new QProcess();
118 connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater()));
119#ifdef __linux__
120 proc->start("gksudo", QStringList() << NFD_START_COMMAND);
121#else
122 proc->start("osascript", QStringList()
123 << "-e"
124 << "do shell script \"" NFD_START_COMMAND "\" with administrator privileges");
125#endif
126}
127
128void
129TrayMenu::stopNfd()
130{
131 QProcess * proc = new QProcess();
132 connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater()));
133#ifdef __linux__
134 proc->start("gksudo", QStringList() << NFD_STOP_COMMAND);
135#else
136 proc->start("osascript", QStringList()
137 << "-e"
138 << "do shell script \"" NFD_STOP_COMMAND "\" with administrator privileges");
139#endif
140}
141
142void
143TrayMenu::updateNfdActivityIcon(bool isActive)
144{
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700145 m_isNfdRunning = isActive;
146
taylorchuc27dd482014-05-17 20:06:49 -0700147 if (isActive) {
148 tray->setIcon(QIcon(CONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700149 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Stop NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700150 }
151 else {
152 tray->setIcon(QIcon(DISCONNECT_ICON));
Alexander Afanasyev4086d512014-07-11 15:56:33 -0700153 m_context->setContextProperty("startStopButtonText", QVariant::fromValue(QString("Start NFD")));
taylorchuc27dd482014-05-17 20:06:49 -0700154 }
155}
156
157} // namespace ndn