Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * 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 Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * 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 Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * 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 Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "chronosharegui.hpp" |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 22 | #include "core/logging.hpp" |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 24 | #include <QDesktopServices> |
Zhenkai Zhu | ce04cb4 | 2013-02-25 15:29:31 -0800 | [diff] [blame] | 25 | #include <QDir> |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 26 | #include <QFileInfo> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include <QValidator> |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 28 | |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 29 | #include <boost/throw_exception.hpp> |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 30 | #include <thread> |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 31 | |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 32 | namespace ndn { |
| 33 | namespace chronoshare { |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 34 | |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 35 | namespace fs = boost::filesystem; |
| 36 | |
| 37 | static const std::string HTTP_SERVER_ADDRESS = "localhost"; |
| 38 | static const std::string HTTP_SERVER_PORT = "9001"; |
Zhenkai Zhu | 6b53f93 | 2013-03-02 20:46:02 -0800 | [diff] [blame] | 39 | #ifdef _DEBUG |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 40 | static const std::string DOC_ROOT = "gui/html"; |
Zhenkai Zhu | 6b53f93 | 2013-03-02 20:46:02 -0800 | [diff] [blame] | 41 | #else |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 42 | static const std::string DOC_ROOT = ":/html"; |
Zhenkai Zhu | 6b53f93 | 2013-03-02 20:46:02 -0800 | [diff] [blame] | 43 | #endif |
Alexander Afanasyev | cf77114 | 2013-03-19 11:15:27 -0700 | [diff] [blame] | 44 | static const QString ICON_BIG_FILE(":/images/chronoshare-big.png"); |
| 45 | static const QString ICON_TRAY_FILE(":/images/" TRAY_ICON); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 47 | _LOG_INIT(Gui); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 49 | ChronoShareGui::ChronoShareGui(QWidget* parent) |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 50 | : QDialog(parent) |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 51 | , m_httpServer(0) |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 52 | #ifdef AUTOUPDATE |
| 53 | , m_sparkle(CHRONOSHARE_APPCAST) |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 54 | #endif |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 55 | { |
Jared Lindblom | f4b900e | 2013-02-07 18:08:47 -0800 | [diff] [blame] | 56 | setWindowTitle("Settings"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | setMinimumWidth(600); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 58 | |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 59 | labelUsername = new QLabel("Username(hint: /<username>)"); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 60 | labelSharedFolder = new QLabel("Shared Folder Name"); |
| 61 | labelSharedFolderPath = new QLabel("Shared Folder Path"); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 62 | |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 63 | QRegExp regex("(/[^/]+)+$"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 64 | QValidator* prefixValidator = new QRegExpValidator(regex, this); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 65 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 66 | editUsername = new QLineEdit(); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 67 | editUsername->setValidator(prefixValidator); |
| 68 | |
| 69 | QRegExp noPureWhiteSpace("^\\S+.*$"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 70 | QValidator* wsValidator = new QRegExpValidator(noPureWhiteSpace, this); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 71 | editSharedFolder = new QLineEdit(); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 72 | editSharedFolder->setValidator(wsValidator); |
| 73 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 74 | 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 Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 79 | button = new QPushButton("Save and apply settings"); |
Zhenkai Zhu | 630d0f1 | 2013-02-28 17:21:54 -0800 | [diff] [blame] | 80 | |
| 81 | QString versionString = QString("Version: ChronoShare v%1").arg(CHRONOSHARE_VERSION); |
| 82 | label = new QLabel(versionString, this); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 83 | |
| 84 | connect(button, SIGNAL(clicked()), this, SLOT(changeSettings())); |
| 85 | |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 86 | mainLayout = new QVBoxLayout; // vertically |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 87 | 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 Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 97 | // create actions that result from clicking a menu option |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 98 | createActionsAndMenu(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 100 | // create tray icon |
| 101 | createTrayIcon(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 102 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 103 | // set icon image |
| 104 | setIcon(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 106 | // show tray icon |
| 107 | m_trayIcon->show(); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 108 | |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 109 | // load settings |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 110 | if (!loadSettings() || m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() || |
| 111 | m_sharedFolderName == "" || m_dirPath.isNull() || m_dirPath == "") { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 112 | // prompt user to choose folder |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 113 | _LOG_DEBUG("First Time Setup"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 114 | 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 Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 121 | startBackend(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 122 | } |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 125 | ChronoShareGui::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 Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 130 | #ifdef AUTOUPDATE |
| 131 | , m_sparkle(CHRONOSHARE_APPCAST) |
| 132 | #endif |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 133 | { |
| 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 Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 144 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 145 | ChronoShareGui::startBackend(bool restart /*=false*/) |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 146 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 147 | 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 Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 158 | _LOG_DEBUG("Restarting Dispatcher and FileWatcher for the new location or new username"); |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 159 | 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 Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 162 | m_ioServiceWork.reset(); |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 163 | m_ioServiceManager->handle_stop(); |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 164 | m_NetworkThread.join(); |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 165 | delete m_ioServiceManager; |
Yukai Tu | f6db5bf | 2017-04-14 12:13:03 -0700 | [diff] [blame^] | 166 | |
| 167 | m_face.reset(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 170 | fs::path realPathToFolder(m_dirPath.toStdString()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 171 | realPathToFolder /= m_sharedFolderName.toStdString(); |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 172 | |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame] | 173 | _LOG_DEBUG("m_dispatcher username:" << m_username.toStdString() |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 174 | << " m_sharedFolderName:" << m_sharedFolderName.toStdString() |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame] | 175 | << " realPathToFolder: " << realPathToFolder); |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 176 | |
| 177 | m_ioService.reset(new boost::asio::io_service()); |
| 178 | m_face.reset(new Face(*m_ioService)); |
| 179 | m_dispatcher.reset(new Dispatcher(m_username.toStdString(), m_sharedFolderName.toStdString(), |
| 180 | realPathToFolder, *m_face)); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 181 | |
| 182 | // Alex: this **must** be here, otherwise m_dirPath will be uninitialized |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 183 | m_watcher.reset(new FsWatcher(*m_ioService, realPathToFolder.string().c_str(), |
| 184 | bind(&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher.get(), _1), |
| 185 | bind(&Dispatcher::Did_LocalFile_Delete, m_dispatcher.get(), _1))); |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 186 | try { |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 187 | m_ioServiceManager = new IoServiceManager(*m_ioService); |
| 188 | m_NetworkThread = std::thread(&IoServiceManager::run, m_ioServiceManager); |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 189 | } |
| 190 | catch (const std::exception& e) { |
| 191 | _LOG_ERROR("Start IO service or Face failed"); |
| 192 | openWarningMessageBox("", "WARNING: Cannot allocate thread for face and io_service!", |
| 193 | QString("Starting chronoshare failed" |
| 194 | "Exception caused: %1") |
| 195 | .arg(e.what())); |
| 196 | // stop filewatching ASAP |
| 197 | m_watcher.reset(); |
| 198 | m_dispatcher.reset(); |
| 199 | return; |
| 200 | } |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 201 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 202 | if (m_httpServer != 0) { |
| 203 | // no need to restart webserver if it already exists |
| 204 | return; |
| 205 | } |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 206 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 207 | QFileInfo indexHtmlInfo(":/html/index.html"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 208 | if (indexHtmlInfo.exists()) { |
| 209 | try { |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 210 | m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT); |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 211 | m_httpServerThread = std::thread(&http::server::server::run, m_httpServer); |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 212 | } |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 213 | catch (const std::exception& e) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 214 | _LOG_ERROR("Start http server failed"); |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 215 | m_httpServer = 0; // just to make sure |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 216 | openWarningMessageBox("WARNING", "WARNING: Cannot start http server!", |
| 217 | QString("Starting http server failed. You will " |
| 218 | "not be able to check history from web " |
| 219 | "brower. Exception caused: %1") |
| 220 | .arg(e.what())); |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 221 | } |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 222 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 223 | else { |
| 224 | _LOG_ERROR("Http server doc root dir does not exist!"); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 225 | } |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | ChronoShareGui::~ChronoShareGui() |
| 229 | { |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 230 | // stop filewatching ASAP |
| 231 | m_watcher.reset(); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 232 | |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 233 | // stop dispatcher ASAP, but after watcher to prevent triggering callbacks on the deleted object |
| 234 | m_dispatcher.reset(); |
| 235 | |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 236 | m_ioServiceWork.reset(); |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 237 | m_ioServiceManager->handle_stop(); |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 238 | m_NetworkThread.join(); |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 239 | delete m_ioServiceManager; |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 240 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 241 | if (m_httpServer != 0) { |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 242 | m_httpServer->handle_stop(); |
| 243 | m_httpServerThread.join(); |
| 244 | delete m_httpServer; |
| 245 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 246 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 247 | // cleanup |
| 248 | delete m_trayIcon; |
| 249 | delete m_trayIconMenu; |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 250 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 251 | delete m_openFolder; |
| 252 | delete m_viewSettings; |
| 253 | delete m_changeFolder; |
| 254 | delete m_quitProgram; |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 255 | |
| 256 | delete labelUsername; |
| 257 | delete labelSharedFolder; |
| 258 | delete editUsername; |
| 259 | delete editSharedFolder; |
| 260 | delete button; |
| 261 | delete label; |
| 262 | delete mainLayout; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 265 | void |
| 266 | ChronoShareGui::openMessageBox(QString title, QString text) |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 267 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 268 | QMessageBox messageBox(this); |
| 269 | messageBox.setWindowTitle(title); |
| 270 | messageBox.setText(text); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 271 | |
Alexander Afanasyev | cf77114 | 2013-03-19 11:15:27 -0700 | [diff] [blame] | 272 | messageBox.setIconPixmap(QPixmap(ICON_BIG_FILE)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 273 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 274 | messageBox.exec(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 277 | void |
| 278 | ChronoShareGui::openMessageBox(QString title, QString text, QString infotext) |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 279 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 280 | QMessageBox messageBox(this); |
| 281 | messageBox.setWindowTitle(title); |
| 282 | messageBox.setText(text); |
| 283 | messageBox.setInformativeText(infotext); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 284 | |
Alexander Afanasyev | cf77114 | 2013-03-19 11:15:27 -0700 | [diff] [blame] | 285 | messageBox.setIconPixmap(QPixmap(ICON_BIG_FILE)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 286 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 287 | messageBox.exec(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 290 | void |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 291 | ChronoShareGui::openWarningMessageBox(QString title, QString text, QString infotext) |
| 292 | { |
| 293 | QMessageBox messageBox(this); |
| 294 | messageBox.setWindowTitle(title); |
| 295 | messageBox.setText(text); |
| 296 | messageBox.setInformativeText(infotext); |
| 297 | |
| 298 | messageBox.setIcon(QMessageBox::Warning); |
| 299 | messageBox.setStandardButtons(QMessageBox::Ok); |
| 300 | |
| 301 | messageBox.exec(); |
| 302 | } |
| 303 | |
| 304 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 305 | ChronoShareGui::createActionsAndMenu() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 306 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 307 | _LOG_DEBUG("Create actions"); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 308 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 309 | // create the "open folder" action |
| 310 | m_openFolder = new QAction(tr("&Open Folder"), this); |
| 311 | connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 312 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 313 | m_openWeb = new QAction(tr("Open in &Browser"), this); |
| 314 | connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser())); |
| 315 | |
| 316 | m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 317 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 318 | m_fileActions[i] = new QAction(this); |
| 319 | m_fileActions[i]->setVisible(false); |
| 320 | connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile())); |
| 321 | m_recentFilesMenu->addAction(m_fileActions[i]); |
| 322 | } |
| 323 | connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu())); |
| 324 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 325 | // create the "view settings" action |
| 326 | m_viewSettings = new QAction(tr("&View Settings"), this); |
| 327 | connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings())); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 328 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 329 | // create the "change folder" action |
| 330 | m_changeFolder = new QAction(tr("&Change Folder"), this); |
| 331 | connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog())); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 332 | |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 333 | #ifdef AUTOUPDATE |
| 334 | m_checkForUpdates = new QAction("Check for updates", this); |
| 335 | connect(m_checkForUpdates, SIGNAL(triggered()), this, SLOT(checkForUpdates())); |
| 336 | #endif // AUTOUPDATE |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 337 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 338 | // create the "quit program" action |
| 339 | m_quitProgram = new QAction(tr("&Quit"), this); |
| 340 | connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 343 | void |
| 344 | ChronoShareGui::createTrayIcon() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 345 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 346 | // create a new icon menu |
| 347 | m_trayIconMenu = new QMenu(this); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 348 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 349 | // add actions to the menu |
| 350 | m_trayIconMenu->addAction(m_openFolder); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 351 | m_trayIconMenu->addAction(m_openWeb); |
| 352 | m_trayIconMenu->addMenu(m_recentFilesMenu); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 353 | m_trayIconMenu->addSeparator(); |
| 354 | m_trayIconMenu->addAction(m_viewSettings); |
| 355 | m_trayIconMenu->addAction(m_changeFolder); |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 356 | |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 357 | #ifdef AUTOUPDATE |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 358 | m_trayIconMenu->addSeparator(); |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 359 | m_trayIconMenu->addAction(m_checkForUpdates); |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 360 | #endif // AUTOUPDATE |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 361 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 362 | m_trayIconMenu->addSeparator(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 363 | m_trayIconMenu->addAction(m_quitProgram); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 364 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 365 | // create new tray icon |
| 366 | m_trayIcon = new QSystemTrayIcon(this); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 367 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 368 | // associate the menu with the tray icon |
| 369 | m_trayIcon->setContextMenu(m_trayIconMenu); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 370 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 371 | // handle left click of icon |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 372 | connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, |
| 373 | SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 376 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 377 | ChronoShareGui::setIcon() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 378 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 379 | // set the icon image |
Alexander Afanasyev | cf77114 | 2013-03-19 11:15:27 -0700 | [diff] [blame] | 380 | m_trayIcon->setIcon(QIcon(ICON_TRAY_FILE)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 383 | void |
| 384 | ChronoShareGui::openSharedFolder() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 385 | { |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 386 | fs::path realPathToFolder(m_dirPath.toStdString()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 387 | realPathToFolder /= m_sharedFolderName.toStdString(); |
Zhenkai Zhu | 872cc59 | 2013-03-04 14:20:46 -0800 | [diff] [blame] | 388 | QString path = QDir::toNativeSeparators(realPathToFolder.string().c_str()); |
Zhenkai Zhu | ce04cb4 | 2013-02-25 15:29:31 -0800 | [diff] [blame] | 389 | QDesktopServices::openUrl(QUrl("file:///" + path)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 390 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 391 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 392 | void |
| 393 | ChronoShareGui::openInWebBrowser() |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 394 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 395 | QUrl url("http://localhost:9001/"); |
| 396 | url.setFragment("folderHistory&" |
| 397 | "user=" + |
| 398 | QUrl::toPercentEncoding(m_username) + "&" |
| 399 | "folder=" + |
| 400 | QUrl::toPercentEncoding(m_sharedFolderName)); |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 401 | |
| 402 | // i give up. there is double encoding and I have no idea how to fight it... |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 403 | QDesktopServices::openUrl(url); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 406 | void |
| 407 | ChronoShareGui::openFile() |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 408 | { |
| 409 | // figure out who sent the signal |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 410 | QAction* pAction = qobject_cast<QAction*>(sender()); |
| 411 | if (pAction->isEnabled()) { |
| 412 | // we stored full path of the file in this toolTip field |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 413 | #ifdef Q_WS_MAC |
| 414 | // we do some hack so we could show the file in Finder highlighted |
| 415 | QStringList args; |
| 416 | args << "-e"; |
| 417 | args << "tell application \"Finder\""; |
| 418 | args << "-e"; |
| 419 | args << "activate"; |
| 420 | args << "-e"; |
| 421 | args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\""; |
| 422 | args << "-e"; |
| 423 | args << "end tell"; |
| 424 | QProcess::startDetached("osascript", args); |
| 425 | #else |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 426 | // too bad qt couldn't do highlighting for Linux(or Mac) |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 427 | QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip())); |
| 428 | #endif |
| 429 | } |
| 430 | } |
| 431 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 432 | void |
| 433 | ChronoShareGui::updateRecentFilesMenu() |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 434 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 435 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 436 | m_fileActions[i]->setVisible(false); |
| 437 | } |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 438 | m_dispatcher->LookupRecentFileActions(std::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 439 | 5); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 442 | void |
| 443 | ChronoShareGui::checkFileAction(const std::string& filename, int action, int index) |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 444 | { |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 445 | fs::path realPathToFolder(m_dirPath.toStdString()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 446 | realPathToFolder /= m_sharedFolderName.toStdString(); |
Zhenkai Zhu | 872cc59 | 2013-03-04 14:20:46 -0800 | [diff] [blame] | 447 | realPathToFolder /= filename; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 448 | QString fullPath = realPathToFolder.string().c_str(); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 449 | QFileInfo fileInfo(fullPath); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 450 | if (fileInfo.exists()) { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 451 | // This is a hack, we just use some field to store the path |
| 452 | m_fileActions[index]->setToolTip(fileInfo.absolutePath()); |
| 453 | m_fileActions[index]->setEnabled(true); |
| 454 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 455 | else { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 456 | // after half an hour frustrating test and search around, |
| 457 | // I think it's the problem of Qt. |
| 458 | // According to the Qt doc, the action cannot be clicked |
| 459 | // and the area would be grey, but it didn't happen |
| 460 | // User can still trigger the action, and not greyed |
| 461 | // added check in SLOT to see if the action is "enalbed" |
| 462 | // as a remedy |
| 463 | // Give up at least for now |
| 464 | m_fileActions[index]->setEnabled(false); |
| 465 | // UPDATE, file not fetched yet |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 466 | if (action == 0) { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 467 | QFont font; |
| 468 | // supposed by change the font, didn't happen |
| 469 | font.setWeight(QFont::Light); |
| 470 | m_fileActions[index]->setFont(font); |
| 471 | m_fileActions[index]->setToolTip(tr("Fetching...")); |
| 472 | } |
| 473 | // DELETE |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 474 | else { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 475 | QFont font; |
| 476 | // supposed by change the font, didn't happen |
| 477 | font.setStrikeOut(true); |
| 478 | m_fileActions[index]->setFont(font); |
| 479 | m_fileActions[index]->setToolTip(tr("Deleted...")); |
| 480 | } |
| 481 | } |
| 482 | m_fileActions[index]->setText(fileInfo.fileName()); |
| 483 | m_fileActions[index]->setVisible(true); |
| 484 | } |
| 485 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 486 | void |
| 487 | ChronoShareGui::changeSettings() |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 488 | { |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 489 | QString oldUsername = m_username; |
| 490 | QString oldSharedFolderName = m_sharedFolderName; |
| 491 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 492 | if (!editUsername->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 493 | m_username = editUsername->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 494 | else |
| 495 | editUsername->setText(m_username); |
| 496 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 497 | if (!editSharedFolder->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 498 | m_sharedFolderName = editSharedFolder->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 499 | else |
| 500 | editSharedFolder->setText(m_sharedFolderName); |
| 501 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 502 | if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() || |
| 503 | m_sharedFolderName == "") { |
| 504 | openMessageBox("Error", "Username and shared folder name cannot be empty"); |
| 505 | } |
| 506 | else { |
| 507 | saveSettings(); |
| 508 | this->hide(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 509 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 510 | if (m_username != oldUsername || oldSharedFolderName != m_sharedFolderName) { |
| 511 | startBackend(true); // restart dispatcher/fswatcher |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 512 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 513 | } |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 516 | void |
| 517 | ChronoShareGui::openFileDialog() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 518 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 519 | while (true) { |
| 520 | // prompt user for new directory |
| 521 | QString tempPath = |
| 522 | QFileDialog::getExistingDirectory(this, tr("Choose ChronoShare folder"), m_dirPath, |
| 523 | QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 524 | if (tempPath.isNull()) { |
| 525 | if (m_dirPath.isNull()) { |
| 526 | openMessageBox("Notice", |
| 527 | "ChronoShare will use [" + QDir::homePath() + |
| 528 | "/ChronoShare]. \n\nYou can change it later selecting \"Change Folder\" menu."); |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 529 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 530 | m_dirPath = QDir::homePath() + "/ChronoShare"; |
| 531 | editSharedFolderPath->setText(m_dirPath); |
| 532 | break; |
| 533 | } |
| 534 | else { |
| 535 | // user just cancelled, no need to do anything else |
| 536 | return; |
| 537 | } |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 538 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 539 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 540 | QFileInfo qFileInfo(tempPath); |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 541 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 542 | if (!qFileInfo.isDir()) { |
| 543 | openMessageBox("Error", "Please select an existing folder or create a new one."); |
| 544 | continue; |
| 545 | } |
| 546 | |
| 547 | if (m_dirPath == tempPath) { |
| 548 | // user selected the same directory, no need to do anything |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | m_dirPath = tempPath; |
| 553 | editSharedFolderPath->setText(m_dirPath); |
| 554 | break; |
| 555 | } |
| 556 | |
| 557 | _LOG_DEBUG("Selected path: " << m_dirPath.toStdString()); |
| 558 | // save settings |
| 559 | saveSettings(); |
| 560 | |
| 561 | startBackend(true); // restart dispatcher/fswatcher |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 564 | void |
| 565 | ChronoShareGui::trayIconClicked(QSystemTrayIcon::ActivationReason reason) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 566 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 567 | // if double clicked, open shared folder |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 568 | if (reason == QSystemTrayIcon::DoubleClick) { |
| 569 | openSharedFolder(); |
| 570 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 573 | void |
| 574 | ChronoShareGui::viewSettings() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 575 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 576 | //simple for now |
| 577 | this->show(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 578 | this->raise(); |
| 579 | this->activateWindow(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 582 | bool |
| 583 | ChronoShareGui::loadSettings() |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 584 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 585 | bool successful = true; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 586 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 587 | // Load Settings |
| 588 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 589 | QSettings settings; |
| 590 | |
| 591 | _LOG_DEBUG("load settings"); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 592 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 593 | if (settings.contains("username")) { |
| 594 | m_username = settings.value("username", "admin").toString(); |
| 595 | } |
| 596 | else { |
| 597 | successful = false; |
| 598 | } |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 599 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 600 | editUsername->setText(m_username); |
| 601 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 602 | if (settings.contains("sharedfoldername")) { |
| 603 | m_sharedFolderName = settings.value("sharedfoldername", "shared").toString(); |
| 604 | } |
| 605 | else { |
| 606 | successful = false; |
| 607 | } |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 608 | |
| 609 | editSharedFolder->setText(m_sharedFolderName); |
| 610 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 611 | if (settings.contains("dirPath")) { |
| 612 | m_dirPath = settings.value("dirPath", QDir::homePath()).toString(); |
| 613 | } |
| 614 | else { |
| 615 | successful = false; |
| 616 | } |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 617 | |
| 618 | editSharedFolderPath->setText(m_dirPath); |
| 619 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 620 | _LOG_DEBUG("Found configured path: " << (successful ? m_dirPath.toStdString() : std::string("no"))); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 621 | |
| 622 | return successful; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 625 | void |
| 626 | ChronoShareGui::saveSettings() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 627 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 628 | // Save Settings |
| 629 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 630 | QSettings settings; |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 631 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 632 | settings.setValue("dirPath", m_dirPath); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 633 | settings.setValue("username", m_username); |
| 634 | settings.setValue("sharedfoldername", m_sharedFolderName); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 637 | void |
| 638 | ChronoShareGui::closeEvent(QCloseEvent* event) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 639 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 640 | _LOG_DEBUG("Close Event"); |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 641 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 642 | if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() || |
| 643 | m_sharedFolderName == "") { |
| 644 | openMessageBox("ChronoShare is not active", |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 645 | "Username and/or shared folder name are not set.\n\n To activate ChronoShare, " |
| 646 | "open Settings menu and configure your username and shared folder name"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 647 | } |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 648 | |
| 649 | this->hide(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 650 | event->ignore(); // don't let the event propagate to the base class |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 651 | } |
Jared Lindblom | dc845f0 | 2013-01-18 17:29:40 -0800 | [diff] [blame] | 652 | |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 653 | #ifdef AUTOUPDATE |
| 654 | void |
| 655 | ChronoShareGui::checkForUpdates() |
| 656 | { |
| 657 | m_sparkle.checkForUpdates(); |
| 658 | } |
| 659 | #endif // AUTOUPDATE |
| 660 | |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 661 | } // namespace chronoshare |
| 662 | } // namespace ndn |
| 663 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 664 | #include "chronosharegui.moc" |