blob: 8df191fdd0bcf4e10635ce6e39d381d9bab83962 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Jared Lindblom06405c42013-01-17 20:48:39 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Jared Lindblom06405c42013-01-17 20:48:39 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Jared Lindblom06405c42013-01-17 20:48:39 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Jared Lindblom06405c42013-01-17 20:48:39 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Jared Lindblom06405c42013-01-17 20:48:39 -080019 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "chronosharegui.hpp"
Lijing Wang152449c2015-05-31 16:25:16 -070022#include "core/logging.hpp"
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080023
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080024#include <QDesktopServices>
Zhenkai Zhuce04cb42013-02-25 15:29:31 -080025#include <QDir>
Zhenkai Zhu25e13582013-02-27 15:33:01 -080026#include <QFileInfo>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080027#include <QValidator>
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080028
Lijing Wang152449c2015-05-31 16:25:16 -070029#include <thread>
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080030
Lijing Wang152449c2015-05-31 16:25:16 -070031namespace ndn {
32namespace chronoshare {
Jared Lindblom06405c42013-01-17 20:48:39 -080033
Lijing Wang152449c2015-05-31 16:25:16 -070034namespace fs = boost::filesystem;
35
36static const std::string HTTP_SERVER_ADDRESS = "localhost";
37static const std::string HTTP_SERVER_PORT = "9001";
Zhenkai Zhu6b53f932013-03-02 20:46:02 -080038#ifdef _DEBUG
Lijing Wang152449c2015-05-31 16:25:16 -070039static const std::string DOC_ROOT = "gui/html";
Zhenkai Zhu6b53f932013-03-02 20:46:02 -080040#else
Lijing Wang152449c2015-05-31 16:25:16 -070041static const std::string DOC_ROOT = ":/html";
Zhenkai Zhu6b53f932013-03-02 20:46:02 -080042#endif
Alexander Afanasyevcf771142013-03-19 11:15:27 -070043static const QString ICON_BIG_FILE(":/images/chronoshare-big.png");
44static const QString ICON_TRAY_FILE(":/images/" TRAY_ICON);
Zhenkai Zhud9429222013-02-25 22:34:09 -080045
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080046_LOG_INIT(Gui);
Alexander Afanasyev83a53002013-01-24 11:12:01 -080047
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080048ChronoShareGui::ChronoShareGui(QWidget* parent)
Jared Lindblomc8ffd592013-01-25 00:00:30 -080049 : QDialog(parent)
Zhenkai Zhud9429222013-02-25 22:34:09 -080050 , m_httpServer(0)
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080051#ifdef SPARKLE_SUPPORTED
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080052 , m_autoUpdate(
53 new SparkleAutoUpdate(tr("http://irl.cs.ucla.edu/~zhenkai/chronoshare_dist/chronoshare.xml")))
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080054#endif
Jared Lindblom06405c42013-01-17 20:48:39 -080055{
Jared Lindblomf4b900e2013-02-07 18:08:47 -080056 setWindowTitle("Settings");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080057 setMinimumWidth(600);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080058
Lijing Wang152449c2015-05-31 16:25:16 -070059 labelUsername = new QLabel("Username(hint: /<username>)");
Jared Lindblomc8ffd592013-01-25 00:00:30 -080060 labelSharedFolder = new QLabel("Shared Folder Name");
61 labelSharedFolderPath = new QLabel("Shared Folder Path");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080062
Zhenkai Zhud5756312013-01-31 13:49:45 -080063 QRegExp regex("(/[^/]+)+$");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080064 QValidator* prefixValidator = new QRegExpValidator(regex, this);
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080065
Jared Lindblomc8ffd592013-01-25 00:00:30 -080066 editUsername = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080067 editUsername->setValidator(prefixValidator);
68
69 QRegExp noPureWhiteSpace("^\\S+.*$");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080070 QValidator* wsValidator = new QRegExpValidator(noPureWhiteSpace, this);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080071 editSharedFolder = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080072 editSharedFolder->setValidator(wsValidator);
73
Jared Lindblomc8ffd592013-01-25 00:00:30 -080074 editSharedFolderPath = new QLineEdit();
75 editSharedFolderPath->setReadOnly(true);
76 QPalette pal = editSharedFolderPath->palette();
77 pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Disabled, QPalette::Base));
78 editSharedFolderPath->setPalette(pal);
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -080079 button = new QPushButton("Save and apply settings");
Zhenkai Zhu630d0f12013-02-28 17:21:54 -080080
81 QString versionString = QString("Version: ChronoShare v%1").arg(CHRONOSHARE_VERSION);
82 label = new QLabel(versionString, this);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080083
84 connect(button, SIGNAL(clicked()), this, SLOT(changeSettings()));
85
Lijing Wang152449c2015-05-31 16:25:16 -070086 mainLayout = new QVBoxLayout; // vertically
Jared Lindblomc8ffd592013-01-25 00:00:30 -080087 mainLayout->addWidget(labelUsername);
88 mainLayout->addWidget(editUsername);
89 mainLayout->addWidget(labelSharedFolder);
90 mainLayout->addWidget(editSharedFolder);
91 mainLayout->addWidget(labelSharedFolderPath);
92 mainLayout->addWidget(editSharedFolderPath);
93 mainLayout->addWidget(button);
94 mainLayout->addWidget(label);
95 setLayout(mainLayout);
96
Alexander Afanasyev83a53002013-01-24 11:12:01 -080097 // create actions that result from clicking a menu option
Zhenkai Zhu25e13582013-02-27 15:33:01 -080098 createActionsAndMenu();
Jared Lindblomb4032e22013-01-17 23:50:51 -080099
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800100 // create tray icon
101 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800102
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800103 // set icon image
104 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -0800105
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800106 // show tray icon
107 m_trayIcon->show();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800108
Zhenkai Zhud5756312013-01-31 13:49:45 -0800109 // load settings
Yukai Tu05126792016-10-11 15:03:30 -0700110 if (!loadSettings() || m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
111 m_sharedFolderName == "" || m_dirPath.isNull() || m_dirPath == "") {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800112 // prompt user to choose folder
113 openMessageBox("First Time Setup",
114 "Please enter a username, shared folder name and choose the shared folder path on your local filesystem.");
115 viewSettings();
116 openFileDialog();
117 viewSettings();
118 }
119 else {
Yukai Tu05126792016-10-11 15:03:30 -0700120 startBackend();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800121 }
Zhenkai Zhud5756312013-01-31 13:49:45 -0800122}
123
124void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800125ChronoShareGui::startBackend(bool restart /*=false*/)
Zhenkai Zhud5756312013-01-31 13:49:45 -0800126{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800127 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
128 m_sharedFolderName == "" || m_dirPath.isNull() || m_dirPath == "") {
129 _LOG_DEBUG("Don't start backend, because settings are in progress or incomplete");
130 return;
131 }
132
133 if (m_watcher != 0 && m_dispatcher != 0) {
134 if (!restart) {
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800135 return;
136 }
137
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800138 _LOG_DEBUG("Restarting Dispatcher and FileWatcher for the new location or new username");
Lijing Wang152449c2015-05-31 16:25:16 -0700139 m_watcher.reset(); // stop filewatching ASAP
140 m_dispatcher.reset(); // stop dispatcher ASAP, but after watcher(to prevent triggering callbacks
141 // on deleted object)
Yukai Tu05126792016-10-11 15:03:30 -0700142 m_ioServiceWork.reset();
143 m_ioSerciceManager->handle_stop();
144 m_NetworkThread.join();
145 delete m_ioSerciceManager;
Zhenkai Zhud5756312013-01-31 13:49:45 -0800146 }
147
Lijing Wang152449c2015-05-31 16:25:16 -0700148 fs::path realPathToFolder(m_dirPath.toStdString());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800149 realPathToFolder /= m_sharedFolderName.toStdString();
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800150
Lijing Wang152449c2015-05-31 16:25:16 -0700151 std::cout << "m_dispatcher username:" << m_username.toStdString()
152 << " m_sharedFolderName:" << m_sharedFolderName.toStdString()
153 << " realPathToFolder: " << realPathToFolder << std::endl;
154
155 m_ioService.reset(new boost::asio::io_service());
156 m_face.reset(new Face(*m_ioService));
157 m_dispatcher.reset(new Dispatcher(m_username.toStdString(), m_sharedFolderName.toStdString(),
158 realPathToFolder, *m_face));
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800159
160 // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
Lijing Wang152449c2015-05-31 16:25:16 -0700161 m_watcher.reset(new FsWatcher(*m_ioService, realPathToFolder.string().c_str(),
162 bind(&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher.get(), _1),
163 bind(&Dispatcher::Did_LocalFile_Delete, m_dispatcher.get(), _1)));
Yukai Tu05126792016-10-11 15:03:30 -0700164 try {
165 m_ioSerciceManager = new IoServiceManager(*m_ioService);
166 m_NetworkThread = std::thread(&IoServiceManager::run, m_ioSerciceManager);
167 }
168 catch (const std::exception& e) {
169 _LOG_ERROR("Start IO service or Face failed");
170 openWarningMessageBox("", "WARNING: Cannot allocate thread for face and io_service!",
171 QString("Starting chronoshare failed"
172 "Exception caused: %1")
173 .arg(e.what()));
174 // stop filewatching ASAP
175 m_watcher.reset();
176 m_dispatcher.reset();
177 return;
178 }
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800179
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800180 if (m_httpServer != 0) {
181 // no need to restart webserver if it already exists
182 return;
183 }
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800184
Zhenkai Zhud9429222013-02-25 22:34:09 -0800185 QFileInfo indexHtmlInfo(":/html/index.html");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800186 if (indexHtmlInfo.exists()) {
187 try {
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800188 m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT);
Lijing Wang152449c2015-05-31 16:25:16 -0700189 m_httpServerThread = std::thread(&http::server::server::run, m_httpServer);
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800190 }
Yukai Tu05126792016-10-11 15:03:30 -0700191 catch (const std::exception& e) {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800192 _LOG_ERROR("Start http server failed");
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800193 m_httpServer = 0; // just to make sure
Yukai Tu05126792016-10-11 15:03:30 -0700194 openWarningMessageBox("WARNING", "WARNING: Cannot start http server!",
195 QString("Starting http server failed. You will "
196 "not be able to check history from web "
197 "brower. Exception caused: %1")
198 .arg(e.what()));
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800199 }
Zhenkai Zhud9429222013-02-25 22:34:09 -0800200 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800201 else {
202 _LOG_ERROR("Http server doc root dir does not exist!");
Zhenkai Zhud9429222013-02-25 22:34:09 -0800203 }
Jared Lindblom06405c42013-01-17 20:48:39 -0800204}
205
206ChronoShareGui::~ChronoShareGui()
207{
Lijing Wang152449c2015-05-31 16:25:16 -0700208 // stop filewatching ASAP
209 m_watcher.reset();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800210
Lijing Wang152449c2015-05-31 16:25:16 -0700211 // stop dispatcher ASAP, but after watcher to prevent triggering callbacks on the deleted object
212 m_dispatcher.reset();
213
Yukai Tu05126792016-10-11 15:03:30 -0700214 m_ioServiceWork.reset();
215 m_ioSerciceManager->handle_stop();
216 m_chronoshareThread.join();
217 m_NetworkThread.join();
218 delete m_ioSerciceManager;
219
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800220 if (m_httpServer != 0) {
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800221 m_httpServer->handle_stop();
222 m_httpServerThread.join();
223 delete m_httpServer;
224 }
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800225
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800226 // cleanup
227 delete m_trayIcon;
228 delete m_trayIconMenu;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800229#ifdef SPARKLE_SUPPORTED
230 delete m_autoUpdate;
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800231 delete m_checkForUpdates;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800232#endif
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800233 delete m_openFolder;
234 delete m_viewSettings;
235 delete m_changeFolder;
236 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800237
238 delete labelUsername;
239 delete labelSharedFolder;
240 delete editUsername;
241 delete editSharedFolder;
242 delete button;
243 delete label;
244 delete mainLayout;
Lijing Wang152449c2015-05-31 16:25:16 -0700245
246 // to avoid `private field 'm_checkForUpdates' is not used` warning/error
247 (void)(m_checkForUpdates);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800248}
249
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800250void
251ChronoShareGui::openMessageBox(QString title, QString text)
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800252{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800253 QMessageBox messageBox(this);
254 messageBox.setWindowTitle(title);
255 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800256
Alexander Afanasyevcf771142013-03-19 11:15:27 -0700257 messageBox.setIconPixmap(QPixmap(ICON_BIG_FILE));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800258
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800259 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800260}
261
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800262void
263ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800264{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800265 QMessageBox messageBox(this);
266 messageBox.setWindowTitle(title);
267 messageBox.setText(text);
268 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800269
Alexander Afanasyevcf771142013-03-19 11:15:27 -0700270 messageBox.setIconPixmap(QPixmap(ICON_BIG_FILE));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800271
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800272 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800273}
274
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800275void
Yukai Tu05126792016-10-11 15:03:30 -0700276ChronoShareGui::openWarningMessageBox(QString title, QString text, QString infotext)
277{
278 QMessageBox messageBox(this);
279 messageBox.setWindowTitle(title);
280 messageBox.setText(text);
281 messageBox.setInformativeText(infotext);
282
283 messageBox.setIcon(QMessageBox::Warning);
284 messageBox.setStandardButtons(QMessageBox::Ok);
285
286 messageBox.exec();
287}
288
289void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800290ChronoShareGui::createActionsAndMenu()
Jared Lindblom06405c42013-01-17 20:48:39 -0800291{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800292 _LOG_DEBUG("Create actions");
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800293
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800294 // create the "open folder" action
295 m_openFolder = new QAction(tr("&Open Folder"), this);
296 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800297
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800298 m_openWeb = new QAction(tr("Open in &Browser"), this);
299 connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser()));
300
301 m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800302 for (int i = 0; i < 5; i++) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800303 m_fileActions[i] = new QAction(this);
304 m_fileActions[i]->setVisible(false);
305 connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile()));
306 m_recentFilesMenu->addAction(m_fileActions[i]);
307 }
308 connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu()));
309
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800310 // create the "view settings" action
311 m_viewSettings = new QAction(tr("&View Settings"), this);
312 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800313
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800314 // create the "change folder" action
315 m_changeFolder = new QAction(tr("&Change Folder"), this);
316 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800317
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800318#ifdef SPARKLE_SUPPORTED
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800319 m_checkForUpdates = new QAction(tr("Check For Updates"), this);
320 connect(m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates()));
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800321#endif
322
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800323 // create the "quit program" action
324 m_quitProgram = new QAction(tr("&Quit"), this);
325 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800326}
327
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800328void
329ChronoShareGui::createTrayIcon()
Jared Lindblom06405c42013-01-17 20:48:39 -0800330{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800331 // create a new icon menu
332 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800333
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800334 // add actions to the menu
335 m_trayIconMenu->addAction(m_openFolder);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800336 m_trayIconMenu->addAction(m_openWeb);
337 m_trayIconMenu->addMenu(m_recentFilesMenu);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800338 m_trayIconMenu->addSeparator();
339 m_trayIconMenu->addAction(m_viewSettings);
340 m_trayIconMenu->addAction(m_changeFolder);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800341
342#ifdef SPARKLE_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800343 m_trayIconMenu->addSeparator();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800344 m_trayIconMenu->addAction(m_checkForUpdates);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800345#endif
346
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800347 m_trayIconMenu->addSeparator();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800348 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800349
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800350 // create new tray icon
351 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800352
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800353 // associate the menu with the tray icon
354 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800355
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800356 // handle left click of icon
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800357 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
358 SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800359}
360
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800361void
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800362ChronoShareGui::onCheckForUpdates()
363{
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800364#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800365 cout << "+++++++++++ trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800366 m_autoUpdate->checkForUpdates();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800367 cout << "+++++++++++ end trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800368#endif
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800369}
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800370
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800371void
372ChronoShareGui::setIcon()
Jared Lindblom06405c42013-01-17 20:48:39 -0800373{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800374 // set the icon image
Alexander Afanasyevcf771142013-03-19 11:15:27 -0700375 m_trayIcon->setIcon(QIcon(ICON_TRAY_FILE));
Jared Lindblom06405c42013-01-17 20:48:39 -0800376}
377
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800378void
379ChronoShareGui::openSharedFolder()
Jared Lindblom06405c42013-01-17 20:48:39 -0800380{
Lijing Wang152449c2015-05-31 16:25:16 -0700381 fs::path realPathToFolder(m_dirPath.toStdString());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800382 realPathToFolder /= m_sharedFolderName.toStdString();
Zhenkai Zhu872cc592013-03-04 14:20:46 -0800383 QString path = QDir::toNativeSeparators(realPathToFolder.string().c_str());
Zhenkai Zhuce04cb42013-02-25 15:29:31 -0800384 QDesktopServices::openUrl(QUrl("file:///" + path));
Jared Lindblom06405c42013-01-17 20:48:39 -0800385}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800386
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800387void
388ChronoShareGui::openInWebBrowser()
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800389{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800390 QUrl url("http://localhost:9001/");
391 url.setFragment("folderHistory&"
392 "user=" +
393 QUrl::toPercentEncoding(m_username) + "&"
394 "folder=" +
395 QUrl::toPercentEncoding(m_sharedFolderName));
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800396
397 // i give up. there is double encoding and I have no idea how to fight it...
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800398 QDesktopServices::openUrl(url);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800399}
400
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800401void
402ChronoShareGui::openFile()
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800403{
404 // figure out who sent the signal
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800405 QAction* pAction = qobject_cast<QAction*>(sender());
406 if (pAction->isEnabled()) {
407// we stored full path of the file in this toolTip field
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800408#ifdef Q_WS_MAC
409 // we do some hack so we could show the file in Finder highlighted
410 QStringList args;
411 args << "-e";
412 args << "tell application \"Finder\"";
413 args << "-e";
414 args << "activate";
415 args << "-e";
416 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
417 args << "-e";
418 args << "end tell";
419 QProcess::startDetached("osascript", args);
420#else
Lijing Wang152449c2015-05-31 16:25:16 -0700421 // too bad qt couldn't do highlighting for Linux(or Mac)
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800422 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
423#endif
424 }
425}
426
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800427void
428ChronoShareGui::updateRecentFilesMenu()
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800429{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800430 for (int i = 0; i < 5; i++) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800431 m_fileActions[i]->setVisible(false);
432 }
Lijing Wang152449c2015-05-31 16:25:16 -0700433 m_dispatcher->LookupRecentFileActions(std::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3),
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800434 5);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800435}
436
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800437void
438ChronoShareGui::checkFileAction(const std::string& filename, int action, int index)
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800439{
Lijing Wang152449c2015-05-31 16:25:16 -0700440 fs::path realPathToFolder(m_dirPath.toStdString());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800441 realPathToFolder /= m_sharedFolderName.toStdString();
Zhenkai Zhu872cc592013-03-04 14:20:46 -0800442 realPathToFolder /= filename;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800443 QString fullPath = realPathToFolder.string().c_str();
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800444 QFileInfo fileInfo(fullPath);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800445 if (fileInfo.exists()) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800446 // This is a hack, we just use some field to store the path
447 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
448 m_fileActions[index]->setEnabled(true);
449 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800450 else {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800451 // after half an hour frustrating test and search around,
452 // I think it's the problem of Qt.
453 // According to the Qt doc, the action cannot be clicked
454 // and the area would be grey, but it didn't happen
455 // User can still trigger the action, and not greyed
456 // added check in SLOT to see if the action is "enalbed"
457 // as a remedy
458 // Give up at least for now
459 m_fileActions[index]->setEnabled(false);
460 // UPDATE, file not fetched yet
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800461 if (action == 0) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800462 QFont font;
463 // supposed by change the font, didn't happen
464 font.setWeight(QFont::Light);
465 m_fileActions[index]->setFont(font);
466 m_fileActions[index]->setToolTip(tr("Fetching..."));
467 }
468 // DELETE
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800469 else {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800470 QFont font;
471 // supposed by change the font, didn't happen
472 font.setStrikeOut(true);
473 m_fileActions[index]->setFont(font);
474 m_fileActions[index]->setToolTip(tr("Deleted..."));
475 }
476 }
477 m_fileActions[index]->setText(fileInfo.fileName());
478 m_fileActions[index]->setVisible(true);
479}
480
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800481void
482ChronoShareGui::changeSettings()
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800483{
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800484 QString oldUsername = m_username;
485 QString oldSharedFolderName = m_sharedFolderName;
486
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800487 if (!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800488 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800489 else
490 editUsername->setText(m_username);
491
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800492 if (!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800493 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800494 else
495 editSharedFolder->setText(m_sharedFolderName);
496
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800497 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
498 m_sharedFolderName == "") {
499 openMessageBox("Error", "Username and shared folder name cannot be empty");
500 }
501 else {
502 saveSettings();
503 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800504
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800505 if (m_username != oldUsername || oldSharedFolderName != m_sharedFolderName) {
506 startBackend(true); // restart dispatcher/fswatcher
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800507 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800508 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800509}
510
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800511void
512ChronoShareGui::openFileDialog()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800513{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800514 while (true) {
515 // prompt user for new directory
516 QString tempPath =
517 QFileDialog::getExistingDirectory(this, tr("Choose ChronoShare folder"), m_dirPath,
518 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
519 if (tempPath.isNull()) {
520 if (m_dirPath.isNull()) {
521 openMessageBox("Notice",
522 "ChronoShare will use [" + QDir::homePath() +
523 "/ChronoShare]. \n\nYou can change it later selecting \"Change Folder\" menu.");
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800524
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800525 m_dirPath = QDir::homePath() + "/ChronoShare";
526 editSharedFolderPath->setText(m_dirPath);
527 break;
528 }
529 else {
530 // user just cancelled, no need to do anything else
531 return;
532 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800533 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800534
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800535 QFileInfo qFileInfo(tempPath);
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800536
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800537 if (!qFileInfo.isDir()) {
538 openMessageBox("Error", "Please select an existing folder or create a new one.");
539 continue;
540 }
541
542 if (m_dirPath == tempPath) {
543 // user selected the same directory, no need to do anything
544 return;
545 }
546
547 m_dirPath = tempPath;
548 editSharedFolderPath->setText(m_dirPath);
549 break;
550 }
551
552 _LOG_DEBUG("Selected path: " << m_dirPath.toStdString());
553 // save settings
554 saveSettings();
555
556 startBackend(true); // restart dispatcher/fswatcher
Jared Lindblomb4032e22013-01-17 23:50:51 -0800557}
558
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800559void
560ChronoShareGui::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800561{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800562 // if double clicked, open shared folder
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800563 if (reason == QSystemTrayIcon::DoubleClick) {
564 openSharedFolder();
565 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800566}
567
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800568void
569ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800570{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800571 //simple for now
572 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800573 this->raise();
574 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800575}
576
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800577bool
578ChronoShareGui::loadSettings()
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800579{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800580 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800581
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800582 // Load Settings
583 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800584 QSettings settings(QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800585
Lijing Wang152449c2015-05-31 16:25:16 -0700586 // _LOG_DEBUG(lexical_cast<string>(settings.allKeys()));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800587
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800588 if (settings.contains("username")) {
589 m_username = settings.value("username", "admin").toString();
590 }
591 else {
592 successful = false;
593 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800594
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800595 editUsername->setText(m_username);
596
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800597 if (settings.contains("sharedfoldername")) {
598 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
599 }
600 else {
601 successful = false;
602 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800603
604 editSharedFolder->setText(m_sharedFolderName);
605
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800606 if (settings.contains("dirPath")) {
607 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
608 }
609 else {
610 successful = false;
611 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800612
613 editSharedFolderPath->setText(m_dirPath);
614
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800615 _LOG_DEBUG("Found configured path: " << (successful ? m_dirPath.toStdString() : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800616
617 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800618}
619
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800620void
621ChronoShareGui::saveSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800622{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800623 // Save Settings
624 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800625 QSettings settings(QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800626
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800627 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800628 settings.setValue("username", m_username);
629 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800630}
631
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800632void
633ChronoShareGui::closeEvent(QCloseEvent* event)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800634{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800635 _LOG_DEBUG("Close Event");
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800636
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800637 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
638 m_sharedFolderName == "") {
639 openMessageBox("ChronoShare is not active",
Lijing Wang152449c2015-05-31 16:25:16 -0700640 "Username and/or shared folder name are not set.\n\n To activate ChronoShare, "
641 "open Settings menu and configure your username and shared folder name");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800642 }
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800643
644 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800645 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800646}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800647
Lijing Wang152449c2015-05-31 16:25:16 -0700648} // namespace chronoshare
649} // namespace ndn
650
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800651#include "chronosharegui.moc"