blob: 3159699cca06df92d31f612c1570866fbd0fcebe [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
Yukai Tu73287f52017-02-06 15:46:40 -080029#include <boost/throw_exception.hpp>
Lijing Wang152449c2015-05-31 16:25:16 -070030#include <thread>
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080031
Lijing Wang152449c2015-05-31 16:25:16 -070032namespace ndn {
33namespace chronoshare {
Jared Lindblom06405c42013-01-17 20:48:39 -080034
Lijing Wang152449c2015-05-31 16:25:16 -070035namespace fs = boost::filesystem;
36
37static const std::string HTTP_SERVER_ADDRESS = "localhost";
38static const std::string HTTP_SERVER_PORT = "9001";
Zhenkai Zhu6b53f932013-03-02 20:46:02 -080039#ifdef _DEBUG
Lijing Wang152449c2015-05-31 16:25:16 -070040static const std::string DOC_ROOT = "gui/html";
Zhenkai Zhu6b53f932013-03-02 20:46:02 -080041#else
Lijing Wang152449c2015-05-31 16:25:16 -070042static const std::string DOC_ROOT = ":/html";
Zhenkai Zhu6b53f932013-03-02 20:46:02 -080043#endif
Alexander Afanasyevcf771142013-03-19 11:15:27 -070044static const QString ICON_BIG_FILE(":/images/chronoshare-big.png");
45static const QString ICON_TRAY_FILE(":/images/" TRAY_ICON);
Zhenkai Zhud9429222013-02-25 22:34:09 -080046
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080047_LOG_INIT(Gui);
Alexander Afanasyev83a53002013-01-24 11:12:01 -080048
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080049ChronoShareGui::ChronoShareGui(QWidget* parent)
Jared Lindblomc8ffd592013-01-25 00:00:30 -080050 : QDialog(parent)
Zhenkai Zhud9429222013-02-25 22:34:09 -080051 , m_httpServer(0)
Alexander Afanasyev1b51bb52017-02-10 19:52:32 -080052#ifdef AUTOUPDATE
53 , m_sparkle(CHRONOSHARE_APPCAST)
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
Yukai Tu73287f52017-02-06 15:46:40 -0800113 _LOG_DEBUG("First Time Setup");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800114 openMessageBox("First Time Setup",
115 "Please enter a username, shared folder name and choose the shared folder path on your local filesystem.");
116 viewSettings();
117 openFileDialog();
118 viewSettings();
119 }
120 else {
Yukai Tu05126792016-10-11 15:03:30 -0700121 startBackend();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800122 }
Zhenkai Zhud5756312013-01-31 13:49:45 -0800123}
124
Yukai Tu73287f52017-02-06 15:46:40 -0800125ChronoShareGui::ChronoShareGui(QString dirPath, QString username, QString sharedFolderName, QWidget* parent)
126 : QDialog(parent)
127 , m_dirPath(dirPath)
128 , m_username(username)
129 , m_sharedFolderName(sharedFolderName)
Alexander Afanasyev1b51bb52017-02-10 19:52:32 -0800130#ifdef AUTOUPDATE
131 , m_sparkle(CHRONOSHARE_APPCAST)
132#endif
Yukai Tu73287f52017-02-06 15:46:40 -0800133{
134 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
135 m_sharedFolderName == "" || m_dirPath.isNull() || m_dirPath == "") {
136 // prompt user to choose folder
137 BOOST_THROW_EXCEPTION(Error("Some error with init info"));
138 }
139 else {
140 startBackend();
141 }
142}
143
Zhenkai Zhud5756312013-01-31 13:49:45 -0800144void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800145ChronoShareGui::startBackend(bool restart /*=false*/)
Zhenkai Zhud5756312013-01-31 13:49:45 -0800146{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800147 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
148 m_sharedFolderName == "" || m_dirPath.isNull() || m_dirPath == "") {
149 _LOG_DEBUG("Don't start backend, because settings are in progress or incomplete");
150 return;
151 }
152
153 if (m_watcher != 0 && m_dispatcher != 0) {
154 if (!restart) {
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800155 return;
156 }
157
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800158 _LOG_DEBUG("Restarting Dispatcher and FileWatcher for the new location or new username");
Lijing Wang152449c2015-05-31 16:25:16 -0700159 m_watcher.reset(); // stop filewatching ASAP
160 m_dispatcher.reset(); // stop dispatcher ASAP, but after watcher(to prevent triggering callbacks
161 // on deleted object)
Yukai Tu05126792016-10-11 15:03:30 -0700162 m_ioServiceWork.reset();
Yukai Tu73287f52017-02-06 15:46:40 -0800163 m_ioServiceManager->handle_stop();
Yukai Tu05126792016-10-11 15:03:30 -0700164 m_NetworkThread.join();
Yukai Tu73287f52017-02-06 15:46:40 -0800165 delete m_ioServiceManager;
Zhenkai Zhud5756312013-01-31 13:49:45 -0800166 }
167
Lijing Wang152449c2015-05-31 16:25:16 -0700168 fs::path realPathToFolder(m_dirPath.toStdString());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800169 realPathToFolder /= m_sharedFolderName.toStdString();
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800170
Yukai Tu729b2892017-02-09 21:15:48 -0800171 _LOG_DEBUG("m_dispatcher username:" << m_username.toStdString()
Lijing Wang152449c2015-05-31 16:25:16 -0700172 << " m_sharedFolderName:" << m_sharedFolderName.toStdString()
Yukai Tu729b2892017-02-09 21:15:48 -0800173 << " realPathToFolder: " << realPathToFolder);
Lijing Wang152449c2015-05-31 16:25:16 -0700174
175 m_ioService.reset(new boost::asio::io_service());
176 m_face.reset(new Face(*m_ioService));
177 m_dispatcher.reset(new Dispatcher(m_username.toStdString(), m_sharedFolderName.toStdString(),
178 realPathToFolder, *m_face));
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800179
180 // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
Lijing Wang152449c2015-05-31 16:25:16 -0700181 m_watcher.reset(new FsWatcher(*m_ioService, realPathToFolder.string().c_str(),
182 bind(&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher.get(), _1),
183 bind(&Dispatcher::Did_LocalFile_Delete, m_dispatcher.get(), _1)));
Yukai Tu05126792016-10-11 15:03:30 -0700184 try {
Yukai Tu73287f52017-02-06 15:46:40 -0800185 m_ioServiceManager = new IoServiceManager(*m_ioService);
186 m_NetworkThread = std::thread(&IoServiceManager::run, m_ioServiceManager);
Yukai Tu05126792016-10-11 15:03:30 -0700187 }
188 catch (const std::exception& e) {
189 _LOG_ERROR("Start IO service or Face failed");
190 openWarningMessageBox("", "WARNING: Cannot allocate thread for face and io_service!",
191 QString("Starting chronoshare failed"
192 "Exception caused: %1")
193 .arg(e.what()));
194 // stop filewatching ASAP
195 m_watcher.reset();
196 m_dispatcher.reset();
197 return;
198 }
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800199
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800200 if (m_httpServer != 0) {
201 // no need to restart webserver if it already exists
202 return;
203 }
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800204
Zhenkai Zhud9429222013-02-25 22:34:09 -0800205 QFileInfo indexHtmlInfo(":/html/index.html");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800206 if (indexHtmlInfo.exists()) {
207 try {
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800208 m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT);
Lijing Wang152449c2015-05-31 16:25:16 -0700209 m_httpServerThread = std::thread(&http::server::server::run, m_httpServer);
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800210 }
Yukai Tu05126792016-10-11 15:03:30 -0700211 catch (const std::exception& e) {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800212 _LOG_ERROR("Start http server failed");
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800213 m_httpServer = 0; // just to make sure
Yukai Tu05126792016-10-11 15:03:30 -0700214 openWarningMessageBox("WARNING", "WARNING: Cannot start http server!",
215 QString("Starting http server failed. You will "
216 "not be able to check history from web "
217 "brower. Exception caused: %1")
218 .arg(e.what()));
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800219 }
Zhenkai Zhud9429222013-02-25 22:34:09 -0800220 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800221 else {
222 _LOG_ERROR("Http server doc root dir does not exist!");
Zhenkai Zhud9429222013-02-25 22:34:09 -0800223 }
Jared Lindblom06405c42013-01-17 20:48:39 -0800224}
225
226ChronoShareGui::~ChronoShareGui()
227{
Lijing Wang152449c2015-05-31 16:25:16 -0700228 // stop filewatching ASAP
229 m_watcher.reset();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800230
Lijing Wang152449c2015-05-31 16:25:16 -0700231 // stop dispatcher ASAP, but after watcher to prevent triggering callbacks on the deleted object
232 m_dispatcher.reset();
233
Yukai Tu05126792016-10-11 15:03:30 -0700234 m_ioServiceWork.reset();
Yukai Tu73287f52017-02-06 15:46:40 -0800235 m_ioServiceManager->handle_stop();
Yukai Tu05126792016-10-11 15:03:30 -0700236 m_NetworkThread.join();
Yukai Tu73287f52017-02-06 15:46:40 -0800237 delete m_ioServiceManager;
Yukai Tu05126792016-10-11 15:03:30 -0700238
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800239 if (m_httpServer != 0) {
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800240 m_httpServer->handle_stop();
241 m_httpServerThread.join();
242 delete m_httpServer;
243 }
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800244
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800245 // cleanup
246 delete m_trayIcon;
247 delete m_trayIconMenu;
Alexander Afanasyev1b51bb52017-02-10 19:52:32 -0800248
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800249 delete m_openFolder;
250 delete m_viewSettings;
251 delete m_changeFolder;
252 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800253
254 delete labelUsername;
255 delete labelSharedFolder;
256 delete editUsername;
257 delete editSharedFolder;
258 delete button;
259 delete label;
260 delete mainLayout;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800261}
262
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800263void
264ChronoShareGui::openMessageBox(QString title, QString text)
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800265{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800266 QMessageBox messageBox(this);
267 messageBox.setWindowTitle(title);
268 messageBox.setText(text);
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 Lindblom5d7e51e2013-01-18 00:59:34 -0800273}
274
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800275void
276ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800277{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800278 QMessageBox messageBox(this);
279 messageBox.setWindowTitle(title);
280 messageBox.setText(text);
281 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800282
Alexander Afanasyevcf771142013-03-19 11:15:27 -0700283 messageBox.setIconPixmap(QPixmap(ICON_BIG_FILE));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800284
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800285 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800286}
287
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800288void
Yukai Tu05126792016-10-11 15:03:30 -0700289ChronoShareGui::openWarningMessageBox(QString title, QString text, QString infotext)
290{
291 QMessageBox messageBox(this);
292 messageBox.setWindowTitle(title);
293 messageBox.setText(text);
294 messageBox.setInformativeText(infotext);
295
296 messageBox.setIcon(QMessageBox::Warning);
297 messageBox.setStandardButtons(QMessageBox::Ok);
298
299 messageBox.exec();
300}
301
302void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800303ChronoShareGui::createActionsAndMenu()
Jared Lindblom06405c42013-01-17 20:48:39 -0800304{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800305 _LOG_DEBUG("Create actions");
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800306
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800307 // create the "open folder" action
308 m_openFolder = new QAction(tr("&Open Folder"), this);
309 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800310
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800311 m_openWeb = new QAction(tr("Open in &Browser"), this);
312 connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser()));
313
314 m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800315 for (int i = 0; i < 5; i++) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800316 m_fileActions[i] = new QAction(this);
317 m_fileActions[i]->setVisible(false);
318 connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile()));
319 m_recentFilesMenu->addAction(m_fileActions[i]);
320 }
321 connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu()));
322
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800323 // create the "view settings" action
324 m_viewSettings = new QAction(tr("&View Settings"), this);
325 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800326
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800327 // create the "change folder" action
328 m_changeFolder = new QAction(tr("&Change Folder"), this);
329 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800330
Alexander Afanasyev1b51bb52017-02-10 19:52:32 -0800331#ifdef AUTOUPDATE
332 m_checkForUpdates = new QAction("Check for updates", this);
333 connect(m_checkForUpdates, SIGNAL(triggered()), this, SLOT(checkForUpdates()));
334#endif // AUTOUPDATE
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800335
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800336 // create the "quit program" action
337 m_quitProgram = new QAction(tr("&Quit"), this);
338 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800339}
340
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800341void
342ChronoShareGui::createTrayIcon()
Jared Lindblom06405c42013-01-17 20:48:39 -0800343{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800344 // create a new icon menu
345 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800346
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800347 // add actions to the menu
348 m_trayIconMenu->addAction(m_openFolder);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800349 m_trayIconMenu->addAction(m_openWeb);
350 m_trayIconMenu->addMenu(m_recentFilesMenu);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800351 m_trayIconMenu->addSeparator();
352 m_trayIconMenu->addAction(m_viewSettings);
353 m_trayIconMenu->addAction(m_changeFolder);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800354
Alexander Afanasyev1b51bb52017-02-10 19:52:32 -0800355#ifdef AUTOUPDATE
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800356 m_trayIconMenu->addSeparator();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800357 m_trayIconMenu->addAction(m_checkForUpdates);
Alexander Afanasyev1b51bb52017-02-10 19:52:32 -0800358#endif // AUTOUPDATE
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800359
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800360 m_trayIconMenu->addSeparator();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800361 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800362
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800363 // create new tray icon
364 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800365
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800366 // associate the menu with the tray icon
367 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800368
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800369 // handle left click of icon
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800370 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
371 SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800372}
373
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800374void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800375ChronoShareGui::setIcon()
Jared Lindblom06405c42013-01-17 20:48:39 -0800376{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800377 // set the icon image
Alexander Afanasyevcf771142013-03-19 11:15:27 -0700378 m_trayIcon->setIcon(QIcon(ICON_TRAY_FILE));
Jared Lindblom06405c42013-01-17 20:48:39 -0800379}
380
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800381void
382ChronoShareGui::openSharedFolder()
Jared Lindblom06405c42013-01-17 20:48:39 -0800383{
Lijing Wang152449c2015-05-31 16:25:16 -0700384 fs::path realPathToFolder(m_dirPath.toStdString());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800385 realPathToFolder /= m_sharedFolderName.toStdString();
Zhenkai Zhu872cc592013-03-04 14:20:46 -0800386 QString path = QDir::toNativeSeparators(realPathToFolder.string().c_str());
Zhenkai Zhuce04cb42013-02-25 15:29:31 -0800387 QDesktopServices::openUrl(QUrl("file:///" + path));
Jared Lindblom06405c42013-01-17 20:48:39 -0800388}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800389
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800390void
391ChronoShareGui::openInWebBrowser()
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800392{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800393 QUrl url("http://localhost:9001/");
394 url.setFragment("folderHistory&"
395 "user=" +
396 QUrl::toPercentEncoding(m_username) + "&"
397 "folder=" +
398 QUrl::toPercentEncoding(m_sharedFolderName));
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800399
400 // i give up. there is double encoding and I have no idea how to fight it...
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800401 QDesktopServices::openUrl(url);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800402}
403
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800404void
405ChronoShareGui::openFile()
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800406{
407 // figure out who sent the signal
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800408 QAction* pAction = qobject_cast<QAction*>(sender());
409 if (pAction->isEnabled()) {
410// we stored full path of the file in this toolTip field
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800411#ifdef Q_WS_MAC
412 // we do some hack so we could show the file in Finder highlighted
413 QStringList args;
414 args << "-e";
415 args << "tell application \"Finder\"";
416 args << "-e";
417 args << "activate";
418 args << "-e";
419 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
420 args << "-e";
421 args << "end tell";
422 QProcess::startDetached("osascript", args);
423#else
Lijing Wang152449c2015-05-31 16:25:16 -0700424 // too bad qt couldn't do highlighting for Linux(or Mac)
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800425 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
426#endif
427 }
428}
429
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800430void
431ChronoShareGui::updateRecentFilesMenu()
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800432{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800433 for (int i = 0; i < 5; i++) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800434 m_fileActions[i]->setVisible(false);
435 }
Lijing Wang152449c2015-05-31 16:25:16 -0700436 m_dispatcher->LookupRecentFileActions(std::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3),
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800437 5);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800438}
439
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800440void
441ChronoShareGui::checkFileAction(const std::string& filename, int action, int index)
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800442{
Lijing Wang152449c2015-05-31 16:25:16 -0700443 fs::path realPathToFolder(m_dirPath.toStdString());
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800444 realPathToFolder /= m_sharedFolderName.toStdString();
Zhenkai Zhu872cc592013-03-04 14:20:46 -0800445 realPathToFolder /= filename;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800446 QString fullPath = realPathToFolder.string().c_str();
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800447 QFileInfo fileInfo(fullPath);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800448 if (fileInfo.exists()) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800449 // This is a hack, we just use some field to store the path
450 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
451 m_fileActions[index]->setEnabled(true);
452 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800453 else {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800454 // after half an hour frustrating test and search around,
455 // I think it's the problem of Qt.
456 // According to the Qt doc, the action cannot be clicked
457 // and the area would be grey, but it didn't happen
458 // User can still trigger the action, and not greyed
459 // added check in SLOT to see if the action is "enalbed"
460 // as a remedy
461 // Give up at least for now
462 m_fileActions[index]->setEnabled(false);
463 // UPDATE, file not fetched yet
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800464 if (action == 0) {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800465 QFont font;
466 // supposed by change the font, didn't happen
467 font.setWeight(QFont::Light);
468 m_fileActions[index]->setFont(font);
469 m_fileActions[index]->setToolTip(tr("Fetching..."));
470 }
471 // DELETE
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800472 else {
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800473 QFont font;
474 // supposed by change the font, didn't happen
475 font.setStrikeOut(true);
476 m_fileActions[index]->setFont(font);
477 m_fileActions[index]->setToolTip(tr("Deleted..."));
478 }
479 }
480 m_fileActions[index]->setText(fileInfo.fileName());
481 m_fileActions[index]->setVisible(true);
482}
483
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800484void
485ChronoShareGui::changeSettings()
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800486{
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800487 QString oldUsername = m_username;
488 QString oldSharedFolderName = m_sharedFolderName;
489
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800490 if (!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800491 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800492 else
493 editUsername->setText(m_username);
494
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800495 if (!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800496 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800497 else
498 editSharedFolder->setText(m_sharedFolderName);
499
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800500 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
501 m_sharedFolderName == "") {
502 openMessageBox("Error", "Username and shared folder name cannot be empty");
503 }
504 else {
505 saveSettings();
506 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800507
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800508 if (m_username != oldUsername || oldSharedFolderName != m_sharedFolderName) {
509 startBackend(true); // restart dispatcher/fswatcher
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800510 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800511 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800512}
513
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800514void
515ChronoShareGui::openFileDialog()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800516{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800517 while (true) {
518 // prompt user for new directory
519 QString tempPath =
520 QFileDialog::getExistingDirectory(this, tr("Choose ChronoShare folder"), m_dirPath,
521 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
522 if (tempPath.isNull()) {
523 if (m_dirPath.isNull()) {
524 openMessageBox("Notice",
525 "ChronoShare will use [" + QDir::homePath() +
526 "/ChronoShare]. \n\nYou can change it later selecting \"Change Folder\" menu.");
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800527
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800528 m_dirPath = QDir::homePath() + "/ChronoShare";
529 editSharedFolderPath->setText(m_dirPath);
530 break;
531 }
532 else {
533 // user just cancelled, no need to do anything else
534 return;
535 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800536 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800537
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800538 QFileInfo qFileInfo(tempPath);
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800539
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800540 if (!qFileInfo.isDir()) {
541 openMessageBox("Error", "Please select an existing folder or create a new one.");
542 continue;
543 }
544
545 if (m_dirPath == tempPath) {
546 // user selected the same directory, no need to do anything
547 return;
548 }
549
550 m_dirPath = tempPath;
551 editSharedFolderPath->setText(m_dirPath);
552 break;
553 }
554
555 _LOG_DEBUG("Selected path: " << m_dirPath.toStdString());
556 // save settings
557 saveSettings();
558
559 startBackend(true); // restart dispatcher/fswatcher
Jared Lindblomb4032e22013-01-17 23:50:51 -0800560}
561
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800562void
563ChronoShareGui::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800564{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800565 // if double clicked, open shared folder
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800566 if (reason == QSystemTrayIcon::DoubleClick) {
567 openSharedFolder();
568 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800569}
570
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800571void
572ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800573{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800574 //simple for now
575 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800576 this->raise();
577 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800578}
579
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800580bool
581ChronoShareGui::loadSettings()
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800582{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800583 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800584
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800585 // Load Settings
586 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
Alexander Afanasyev1b51bb52017-02-10 19:52:32 -0800587 QSettings settings;
588
589 _LOG_DEBUG("load settings");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800590
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800591 if (settings.contains("username")) {
592 m_username = settings.value("username", "admin").toString();
593 }
594 else {
595 successful = false;
596 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800597
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800598 editUsername->setText(m_username);
599
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800600 if (settings.contains("sharedfoldername")) {
601 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
602 }
603 else {
604 successful = false;
605 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800606
607 editSharedFolder->setText(m_sharedFolderName);
608
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800609 if (settings.contains("dirPath")) {
610 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
611 }
612 else {
613 successful = false;
614 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800615
616 editSharedFolderPath->setText(m_dirPath);
617
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800618 _LOG_DEBUG("Found configured path: " << (successful ? m_dirPath.toStdString() : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800619
620 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800621}
622
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800623void
624ChronoShareGui::saveSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800625{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800626 // Save Settings
627 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
Alexander Afanasyev1b51bb52017-02-10 19:52:32 -0800628 QSettings settings;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800629
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800630 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800631 settings.setValue("username", m_username);
632 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800633}
634
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800635void
636ChronoShareGui::closeEvent(QCloseEvent* event)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800637{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800638 _LOG_DEBUG("Close Event");
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800639
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800640 if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() ||
641 m_sharedFolderName == "") {
642 openMessageBox("ChronoShare is not active",
Lijing Wang152449c2015-05-31 16:25:16 -0700643 "Username and/or shared folder name are not set.\n\n To activate ChronoShare, "
644 "open Settings menu and configure your username and shared folder name");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800645 }
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800646
647 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800648 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800649}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800650
Alexander Afanasyev1b51bb52017-02-10 19:52:32 -0800651#ifdef AUTOUPDATE
652void
653ChronoShareGui::checkForUpdates()
654{
655 m_sparkle.checkForUpdates();
656}
657#endif // AUTOUPDATE
658
Lijing Wang152449c2015-05-31 16:25:16 -0700659} // namespace chronoshare
660} // namespace ndn
661
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800662#include "chronosharegui.moc"