blob: ef4af73a65754b9dcfe781ae2d423d838fa3975b [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
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 if (!loadSettings()) {
111 // prompt user to choose folder
112 openMessageBox("First Time Setup",
113 "Please enter a username, shared folder name and choose the shared folder path on your local filesystem.");
114 viewSettings();
115 openFileDialog();
116 viewSettings();
117 }
118 else {
119 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
120 m_sharedFolderName == "") {
121 openMessageBox("First Time Setup",
122 "To activate ChronoShare, please configure your username and shared folder name.");
Zhenkai Zhud5756312013-01-31 13:49:45 -0800123 viewSettings();
124 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800125 else {
126 startBackend();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800127 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800128 }
Zhenkai Zhud5756312013-01-31 13:49:45 -0800129}
130
131void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800132ChronoShareGui::startBackend(bool restart /*=false*/)
Zhenkai Zhud5756312013-01-31 13:49:45 -0800133{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800134 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
135 m_sharedFolderName == "" || m_dirPath.isNull() || m_dirPath == "") {
136 _LOG_DEBUG("Don't start backend, because settings are in progress or incomplete");
137 return;
138 }
139
140 if (m_watcher != 0 && m_dispatcher != 0) {
141 if (!restart) {
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800142 return;
143 }
144
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800145 _LOG_DEBUG("Restarting Dispatcher and FileWatcher for the new location or new username");
Lijing Wang152449c2015-05-31 16:25:16 -0700146 m_watcher.reset(); // stop filewatching ASAP
147 m_dispatcher.reset(); // stop dispatcher ASAP, but after watcher(to prevent triggering callbacks
148 // on deleted object)
Zhenkai Zhud5756312013-01-31 13:49:45 -0800149 }
150
Lijing Wang152449c2015-05-31 16:25:16 -0700151 fs::path realPathToFolder(m_dirPath.toStdString());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800152 realPathToFolder /= m_sharedFolderName.toStdString();
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800153
Lijing Wang152449c2015-05-31 16:25:16 -0700154 std::cout << "m_dispatcher username:" << m_username.toStdString()
155 << " m_sharedFolderName:" << m_sharedFolderName.toStdString()
156 << " realPathToFolder: " << realPathToFolder << std::endl;
157
158 m_ioService.reset(new boost::asio::io_service());
159 m_face.reset(new Face(*m_ioService));
160 m_dispatcher.reset(new Dispatcher(m_username.toStdString(), m_sharedFolderName.toStdString(),
161 realPathToFolder, *m_face));
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800162
163 // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
Lijing Wang152449c2015-05-31 16:25:16 -0700164 m_watcher.reset(new FsWatcher(*m_ioService, realPathToFolder.string().c_str(),
165 bind(&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher.get(), _1),
166 bind(&Dispatcher::Did_LocalFile_Delete, m_dispatcher.get(), _1)));
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800167
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800168 if (m_httpServer != 0) {
169 // no need to restart webserver if it already exists
170 return;
171 }
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800172
Zhenkai Zhud9429222013-02-25 22:34:09 -0800173 QFileInfo indexHtmlInfo(":/html/index.html");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800174 if (indexHtmlInfo.exists()) {
175 try {
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800176 m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT);
Lijing Wang152449c2015-05-31 16:25:16 -0700177 m_httpServerThread = std::thread(&http::server::server::run, m_httpServer);
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800178 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800179 catch (std::exception& e) {
180 _LOG_ERROR("Start http server failed");
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800181 m_httpServer = 0; // just to make sure
182 QMessageBox msgBox;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800183 msgBox.setText("WARNING: Cannot start http server!");
184 msgBox.setIcon(QMessageBox::Warning);
185 msgBox.setInformativeText(
Lijing Wang152449c2015-05-31 16:25:16 -0700186 QString("Starting http server failed. You will not be able to check history from web "
187 "brower. Exception caused: %1")
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800188 .arg(e.what()));
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800189 msgBox.setStandardButtons(QMessageBox::Ok);
190 msgBox.exec();
191 }
Zhenkai Zhud9429222013-02-25 22:34:09 -0800192 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800193 else {
194 _LOG_ERROR("Http server doc root dir does not exist!");
Zhenkai Zhud9429222013-02-25 22:34:09 -0800195 }
Jared Lindblom06405c42013-01-17 20:48:39 -0800196}
197
198ChronoShareGui::~ChronoShareGui()
199{
Lijing Wang152449c2015-05-31 16:25:16 -0700200 // stop filewatching ASAP
201 m_watcher.reset();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800202
Lijing Wang152449c2015-05-31 16:25:16 -0700203 // stop dispatcher ASAP, but after watcher to prevent triggering callbacks on the deleted object
204 m_dispatcher.reset();
205
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800206 if (m_httpServer != 0) {
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800207 m_httpServer->handle_stop();
208 m_httpServerThread.join();
209 delete m_httpServer;
210 }
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800211
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800212 // cleanup
213 delete m_trayIcon;
214 delete m_trayIconMenu;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800215#ifdef SPARKLE_SUPPORTED
216 delete m_autoUpdate;
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800217 delete m_checkForUpdates;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800218#endif
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800219 delete m_openFolder;
220 delete m_viewSettings;
221 delete m_changeFolder;
222 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800223
224 delete labelUsername;
225 delete labelSharedFolder;
226 delete editUsername;
227 delete editSharedFolder;
228 delete button;
229 delete label;
230 delete mainLayout;
Lijing Wang152449c2015-05-31 16:25:16 -0700231
232 // to avoid `private field 'm_checkForUpdates' is not used` warning/error
233 (void)(m_checkForUpdates);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800234}
235
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800236void
237ChronoShareGui::openMessageBox(QString title, QString text)
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800238{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800239 QMessageBox messageBox(this);
240 messageBox.setWindowTitle(title);
241 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800242
Alexander Afanasyevcf771142013-03-19 11:15:27 -0700243 messageBox.setIconPixmap(QPixmap(ICON_BIG_FILE));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800244
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800245 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800246}
247
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800248void
249ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800250{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800251 QMessageBox messageBox(this);
252 messageBox.setWindowTitle(title);
253 messageBox.setText(text);
254 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800255
Alexander Afanasyevcf771142013-03-19 11:15:27 -0700256 messageBox.setIconPixmap(QPixmap(ICON_BIG_FILE));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800257
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800258 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800259}
260
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800261void
262ChronoShareGui::createActionsAndMenu()
Jared Lindblom06405c42013-01-17 20:48:39 -0800263{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800264 _LOG_DEBUG("Create actions");
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800265
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800266 // create the "open folder" action
267 m_openFolder = new QAction(tr("&Open Folder"), this);
268 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800269
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800270 m_openWeb = new QAction(tr("Open in &Browser"), this);
271 connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser()));
272
273 m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800274 for (int i = 0; i < 5; i++) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800275 m_fileActions[i] = new QAction(this);
276 m_fileActions[i]->setVisible(false);
277 connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile()));
278 m_recentFilesMenu->addAction(m_fileActions[i]);
279 }
280 connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu()));
281
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800282 // create the "view settings" action
283 m_viewSettings = new QAction(tr("&View Settings"), this);
284 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800285
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800286 // create the "change folder" action
287 m_changeFolder = new QAction(tr("&Change Folder"), this);
288 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800289
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800290#ifdef SPARKLE_SUPPORTED
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800291 m_checkForUpdates = new QAction(tr("Check For Updates"), this);
292 connect(m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates()));
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800293#endif
294
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800295 // create the "quit program" action
296 m_quitProgram = new QAction(tr("&Quit"), this);
297 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800298}
299
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800300void
301ChronoShareGui::createTrayIcon()
Jared Lindblom06405c42013-01-17 20:48:39 -0800302{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800303 // create a new icon menu
304 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800305
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800306 // add actions to the menu
307 m_trayIconMenu->addAction(m_openFolder);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800308 m_trayIconMenu->addAction(m_openWeb);
309 m_trayIconMenu->addMenu(m_recentFilesMenu);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800310 m_trayIconMenu->addSeparator();
311 m_trayIconMenu->addAction(m_viewSettings);
312 m_trayIconMenu->addAction(m_changeFolder);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800313
314#ifdef SPARKLE_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800315 m_trayIconMenu->addSeparator();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800316 m_trayIconMenu->addAction(m_checkForUpdates);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800317#endif
318
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800319 m_trayIconMenu->addSeparator();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800320 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800321
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800322 // create new tray icon
323 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800324
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800325 // associate the menu with the tray icon
326 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800327
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800328 // handle left click of icon
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800329 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
330 SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800331}
332
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800333void
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800334ChronoShareGui::onCheckForUpdates()
335{
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800336#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800337 cout << "+++++++++++ trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800338 m_autoUpdate->checkForUpdates();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800339 cout << "+++++++++++ end trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800340#endif
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800341}
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800342
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800343void
344ChronoShareGui::setIcon()
Jared Lindblom06405c42013-01-17 20:48:39 -0800345{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800346 // set the icon image
Alexander Afanasyevcf771142013-03-19 11:15:27 -0700347 m_trayIcon->setIcon(QIcon(ICON_TRAY_FILE));
Jared Lindblom06405c42013-01-17 20:48:39 -0800348}
349
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800350void
351ChronoShareGui::openSharedFolder()
Jared Lindblom06405c42013-01-17 20:48:39 -0800352{
Lijing Wang152449c2015-05-31 16:25:16 -0700353 fs::path realPathToFolder(m_dirPath.toStdString());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800354 realPathToFolder /= m_sharedFolderName.toStdString();
Zhenkai Zhu872cc592013-03-04 14:20:46 -0800355 QString path = QDir::toNativeSeparators(realPathToFolder.string().c_str());
Zhenkai Zhuce04cb42013-02-25 15:29:31 -0800356 QDesktopServices::openUrl(QUrl("file:///" + path));
Jared Lindblom06405c42013-01-17 20:48:39 -0800357}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800358
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800359void
360ChronoShareGui::openInWebBrowser()
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800361{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800362 QUrl url("http://localhost:9001/");
363 url.setFragment("folderHistory&"
364 "user=" +
365 QUrl::toPercentEncoding(m_username) + "&"
366 "folder=" +
367 QUrl::toPercentEncoding(m_sharedFolderName));
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800368
369 // i give up. there is double encoding and I have no idea how to fight it...
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800370 QDesktopServices::openUrl(url);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800371}
372
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800373void
374ChronoShareGui::openFile()
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800375{
376 // figure out who sent the signal
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800377 QAction* pAction = qobject_cast<QAction*>(sender());
378 if (pAction->isEnabled()) {
379// we stored full path of the file in this toolTip field
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800380#ifdef Q_WS_MAC
381 // we do some hack so we could show the file in Finder highlighted
382 QStringList args;
383 args << "-e";
384 args << "tell application \"Finder\"";
385 args << "-e";
386 args << "activate";
387 args << "-e";
388 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
389 args << "-e";
390 args << "end tell";
391 QProcess::startDetached("osascript", args);
392#else
Lijing Wang152449c2015-05-31 16:25:16 -0700393 // too bad qt couldn't do highlighting for Linux(or Mac)
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800394 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
395#endif
396 }
397}
398
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800399void
400ChronoShareGui::updateRecentFilesMenu()
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800401{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800402 for (int i = 0; i < 5; i++) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800403 m_fileActions[i]->setVisible(false);
404 }
Lijing Wang152449c2015-05-31 16:25:16 -0700405 m_dispatcher->LookupRecentFileActions(std::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3),
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800406 5);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800407}
408
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800409void
410ChronoShareGui::checkFileAction(const std::string& filename, int action, int index)
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800411{
Lijing Wang152449c2015-05-31 16:25:16 -0700412 fs::path realPathToFolder(m_dirPath.toStdString());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800413 realPathToFolder /= m_sharedFolderName.toStdString();
Zhenkai Zhu872cc592013-03-04 14:20:46 -0800414 realPathToFolder /= filename;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800415 QString fullPath = realPathToFolder.string().c_str();
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800416 QFileInfo fileInfo(fullPath);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800417 if (fileInfo.exists()) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800418 // This is a hack, we just use some field to store the path
419 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
420 m_fileActions[index]->setEnabled(true);
421 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800422 else {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800423 // after half an hour frustrating test and search around,
424 // I think it's the problem of Qt.
425 // According to the Qt doc, the action cannot be clicked
426 // and the area would be grey, but it didn't happen
427 // User can still trigger the action, and not greyed
428 // added check in SLOT to see if the action is "enalbed"
429 // as a remedy
430 // Give up at least for now
431 m_fileActions[index]->setEnabled(false);
432 // UPDATE, file not fetched yet
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800433 if (action == 0) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800434 QFont font;
435 // supposed by change the font, didn't happen
436 font.setWeight(QFont::Light);
437 m_fileActions[index]->setFont(font);
438 m_fileActions[index]->setToolTip(tr("Fetching..."));
439 }
440 // DELETE
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800441 else {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800442 QFont font;
443 // supposed by change the font, didn't happen
444 font.setStrikeOut(true);
445 m_fileActions[index]->setFont(font);
446 m_fileActions[index]->setToolTip(tr("Deleted..."));
447 }
448 }
449 m_fileActions[index]->setText(fileInfo.fileName());
450 m_fileActions[index]->setVisible(true);
451}
452
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800453void
454ChronoShareGui::changeSettings()
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800455{
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800456 QString oldUsername = m_username;
457 QString oldSharedFolderName = m_sharedFolderName;
458
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800459 if (!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800460 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800461 else
462 editUsername->setText(m_username);
463
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800464 if (!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800465 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800466 else
467 editSharedFolder->setText(m_sharedFolderName);
468
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800469 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
470 m_sharedFolderName == "") {
471 openMessageBox("Error", "Username and shared folder name cannot be empty");
472 }
473 else {
474 saveSettings();
475 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800476
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800477 if (m_username != oldUsername || oldSharedFolderName != m_sharedFolderName) {
478 startBackend(true); // restart dispatcher/fswatcher
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800479 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800480 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800481}
482
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800483void
484ChronoShareGui::openFileDialog()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800485{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800486 while (true) {
487 // prompt user for new directory
488 QString tempPath =
489 QFileDialog::getExistingDirectory(this, tr("Choose ChronoShare folder"), m_dirPath,
490 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
491 if (tempPath.isNull()) {
492 if (m_dirPath.isNull()) {
493 openMessageBox("Notice",
494 "ChronoShare will use [" + QDir::homePath() +
495 "/ChronoShare]. \n\nYou can change it later selecting \"Change Folder\" menu.");
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800496
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800497 m_dirPath = QDir::homePath() + "/ChronoShare";
498 editSharedFolderPath->setText(m_dirPath);
499 break;
500 }
501 else {
502 // user just cancelled, no need to do anything else
503 return;
504 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800505 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800506
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800507 QFileInfo qFileInfo(tempPath);
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800508
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800509 if (!qFileInfo.isDir()) {
510 openMessageBox("Error", "Please select an existing folder or create a new one.");
511 continue;
512 }
513
514 if (m_dirPath == tempPath) {
515 // user selected the same directory, no need to do anything
516 return;
517 }
518
519 m_dirPath = tempPath;
520 editSharedFolderPath->setText(m_dirPath);
521 break;
522 }
523
524 _LOG_DEBUG("Selected path: " << m_dirPath.toStdString());
525 // save settings
526 saveSettings();
527
528 startBackend(true); // restart dispatcher/fswatcher
Jared Lindblomb4032e22013-01-17 23:50:51 -0800529}
530
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800531void
532ChronoShareGui::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800533{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800534 // if double clicked, open shared folder
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800535 if (reason == QSystemTrayIcon::DoubleClick) {
536 openSharedFolder();
537 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800538}
539
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800540void
541ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800542{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800543 //simple for now
544 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800545 this->raise();
546 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800547}
548
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800549bool
550ChronoShareGui::loadSettings()
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800551{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800552 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800553
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800554 // Load Settings
555 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800556 QSettings settings(QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800557
Lijing Wang152449c2015-05-31 16:25:16 -0700558 // _LOG_DEBUG(lexical_cast<string>(settings.allKeys()));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800559
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800560 if (settings.contains("username")) {
561 m_username = settings.value("username", "admin").toString();
562 }
563 else {
564 successful = false;
565 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800566
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800567 editUsername->setText(m_username);
568
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800569 if (settings.contains("sharedfoldername")) {
570 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
571 }
572 else {
573 successful = false;
574 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800575
576 editSharedFolder->setText(m_sharedFolderName);
577
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800578 if (settings.contains("dirPath")) {
579 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
580 }
581 else {
582 successful = false;
583 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800584
585 editSharedFolderPath->setText(m_dirPath);
586
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800587 _LOG_DEBUG("Found configured path: " << (successful ? m_dirPath.toStdString() : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800588
589 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800590}
591
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800592void
593ChronoShareGui::saveSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800594{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800595 // Save Settings
596 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800597 QSettings settings(QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800598
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800599 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800600 settings.setValue("username", m_username);
601 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800602}
603
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800604void
605ChronoShareGui::closeEvent(QCloseEvent* event)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800606{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800607 _LOG_DEBUG("Close Event");
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800608
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800609 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
610 m_sharedFolderName == "") {
611 openMessageBox("ChronoShare is not active",
Lijing Wang152449c2015-05-31 16:25:16 -0700612 "Username and/or shared folder name are not set.\n\n To activate ChronoShare, "
613 "open Settings menu and configure your username and shared folder name");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800614 }
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800615
616 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800617 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800618}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800619
Lijing Wang152449c2015-05-31 16:25:16 -0700620} // namespace chronoshare
621} // namespace ndn
622
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800623#include "chronosharegui.moc"