blob: 43c572707e0ad7283e53efcf076e27df9deca25b [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
42TrayMenu::TrayMenu()
43{
44 menu = new QMenu(this);
45 pref = new QAction("Preferences...", menu);
46 quit = new QAction("Quit", menu);
47
48 // connect(start, SIGNAL(triggered()), this, SLOT(startNfd()));
49 // connect(stop, SIGNAL(triggered()), this, SLOT(stopNfd()));
50 connect(pref, SIGNAL(triggered()), this, SIGNAL(showApp()));
51 connect(quit, SIGNAL(triggered()), this, SLOT(quitApp()));
52
53 connect(this, SIGNAL(nfdActivityUpdate(bool)), this, SLOT(updateNfdActivityIcon(bool)));
54
55 // menu->addAction(start);
56 // menu->addAction(stop);
57 menu->addAction(pref);
58 menu->addAction(quit);
59 tray = new QSystemTrayIcon(this);
60 tray->setContextMenu(menu);
61 connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
62 this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
63 tray->setIcon(QIcon(DISCONNECT_ICON));
64 tray->show();
65}
66
67TrayMenu::~TrayMenu()
68{
69}
70
71Q_INVOKABLE void
72TrayMenu::checkNfdRunning()
73{
74 // Face face;
75 // Interest interest("/localhost/nfd/status");
76 // face.expressInterest(interest, 0, 0);
77 // try {
78 // face.processEvents();
79 // tray->setIcon(QIcon(CONNECT_ICON));
80 // } catch (Tlv::Error) {
81 // tray->setIcon(QIcon(DISCONNECT_ICON));
82 // }
83}
84
85Q_INVOKABLE void
86TrayMenu::autoConfig()
87{
88 std::cout << "auto config" <<std::endl;
89 QProcess* proc = new QProcess();
90 connect(proc,SIGNAL(finished(int)), proc, SLOT(deleteLater()));
91 proc->start(NFD_AUTOCONFIG_COMMAND);
92}
93
94void
95TrayMenu::quitApp()
96{
97 QCoreApplication::exit(0);
98}
99
100void
101TrayMenu::iconActivated(QSystemTrayIcon::ActivationReason reason)
102{
103 switch (reason) {
104 // case QSystemTrayIcon::Trigger:
105 // emit showApp();
106 // break;
107 case QSystemTrayIcon::Context:
108 break;
109 default:
110 break;
111 }
112}
113
114void
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{
145 if (isActive) {
146 tray->setIcon(QIcon(CONNECT_ICON));
147 }
148 else {
149 tray->setIcon(QIcon(DISCONNECT_ICON));
150 }
151}
152
153} // namespace ndn