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; |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 168 | fs::path realPathToFolder(m_dirPath.toStdString()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 169 | realPathToFolder /= m_sharedFolderName.toStdString(); |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 170 | |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame] | 171 | _LOG_DEBUG("m_dispatcher username:" << m_username.toStdString() |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 172 | << " m_sharedFolderName:" << m_sharedFolderName.toStdString() |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame] | 173 | << " realPathToFolder: " << realPathToFolder); |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 174 | |
| 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 Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 179 | |
| 180 | // Alex: this **must** be here, otherwise m_dirPath will be uninitialized |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 181 | 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 Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 184 | try { |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 185 | m_ioServiceManager = new IoServiceManager(*m_ioService); |
| 186 | m_NetworkThread = std::thread(&IoServiceManager::run, m_ioServiceManager); |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 187 | } |
| 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 Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 199 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 200 | if (m_httpServer != 0) { |
| 201 | // no need to restart webserver if it already exists |
| 202 | return; |
| 203 | } |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 204 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 205 | QFileInfo indexHtmlInfo(":/html/index.html"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 206 | if (indexHtmlInfo.exists()) { |
| 207 | try { |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 208 | 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] | 209 | m_httpServerThread = std::thread(&http::server::server::run, m_httpServer); |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 210 | } |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 211 | catch (const std::exception& e) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 212 | _LOG_ERROR("Start http server failed"); |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 213 | m_httpServer = 0; // just to make sure |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 214 | 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 Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 219 | } |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 220 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 221 | else { |
| 222 | _LOG_ERROR("Http server doc root dir does not exist!"); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 223 | } |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | ChronoShareGui::~ChronoShareGui() |
| 227 | { |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 228 | // stop filewatching ASAP |
| 229 | m_watcher.reset(); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 230 | |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 231 | // stop dispatcher ASAP, but after watcher to prevent triggering callbacks on the deleted object |
| 232 | m_dispatcher.reset(); |
| 233 | |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 234 | m_ioServiceWork.reset(); |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 235 | m_ioServiceManager->handle_stop(); |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 236 | m_NetworkThread.join(); |
Yukai Tu | 73287f5 | 2017-02-06 15:46:40 -0800 | [diff] [blame] | 237 | delete m_ioServiceManager; |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 238 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 239 | if (m_httpServer != 0) { |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 240 | m_httpServer->handle_stop(); |
| 241 | m_httpServerThread.join(); |
| 242 | delete m_httpServer; |
| 243 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 244 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 245 | // cleanup |
| 246 | delete m_trayIcon; |
| 247 | delete m_trayIconMenu; |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 248 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 249 | delete m_openFolder; |
| 250 | delete m_viewSettings; |
| 251 | delete m_changeFolder; |
| 252 | delete m_quitProgram; |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 253 | |
| 254 | delete labelUsername; |
| 255 | delete labelSharedFolder; |
| 256 | delete editUsername; |
| 257 | delete editSharedFolder; |
| 258 | delete button; |
| 259 | delete label; |
| 260 | delete mainLayout; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 263 | void |
| 264 | ChronoShareGui::openMessageBox(QString title, QString text) |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 265 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 266 | QMessageBox messageBox(this); |
| 267 | messageBox.setWindowTitle(title); |
| 268 | messageBox.setText(text); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 269 | |
Alexander Afanasyev | cf77114 | 2013-03-19 11:15:27 -0700 | [diff] [blame] | 270 | messageBox.setIconPixmap(QPixmap(ICON_BIG_FILE)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 271 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 272 | messageBox.exec(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 275 | void |
| 276 | ChronoShareGui::openMessageBox(QString title, QString text, QString infotext) |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 277 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 278 | QMessageBox messageBox(this); |
| 279 | messageBox.setWindowTitle(title); |
| 280 | messageBox.setText(text); |
| 281 | messageBox.setInformativeText(infotext); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 282 | |
Alexander Afanasyev | cf77114 | 2013-03-19 11:15:27 -0700 | [diff] [blame] | 283 | messageBox.setIconPixmap(QPixmap(ICON_BIG_FILE)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 284 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 285 | messageBox.exec(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 288 | void |
Yukai Tu | 0512679 | 2016-10-11 15:03:30 -0700 | [diff] [blame] | 289 | ChronoShareGui::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 | |
| 302 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 303 | ChronoShareGui::createActionsAndMenu() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 304 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 305 | _LOG_DEBUG("Create actions"); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 306 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 307 | // create the "open folder" action |
| 308 | m_openFolder = new QAction(tr("&Open Folder"), this); |
| 309 | connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 310 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 311 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 315 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 316 | 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 Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 323 | // create the "view settings" action |
| 324 | m_viewSettings = new QAction(tr("&View Settings"), this); |
| 325 | connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings())); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 326 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 327 | // create the "change folder" action |
| 328 | m_changeFolder = new QAction(tr("&Change Folder"), this); |
| 329 | connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog())); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 330 | |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 331 | #ifdef AUTOUPDATE |
| 332 | m_checkForUpdates = new QAction("Check for updates", this); |
| 333 | connect(m_checkForUpdates, SIGNAL(triggered()), this, SLOT(checkForUpdates())); |
| 334 | #endif // AUTOUPDATE |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 335 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 336 | // create the "quit program" action |
| 337 | m_quitProgram = new QAction(tr("&Quit"), this); |
| 338 | connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 341 | void |
| 342 | ChronoShareGui::createTrayIcon() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 343 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 344 | // create a new icon menu |
| 345 | m_trayIconMenu = new QMenu(this); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 346 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 347 | // add actions to the menu |
| 348 | m_trayIconMenu->addAction(m_openFolder); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 349 | m_trayIconMenu->addAction(m_openWeb); |
| 350 | m_trayIconMenu->addMenu(m_recentFilesMenu); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 351 | m_trayIconMenu->addSeparator(); |
| 352 | m_trayIconMenu->addAction(m_viewSettings); |
| 353 | m_trayIconMenu->addAction(m_changeFolder); |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 354 | |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 355 | #ifdef AUTOUPDATE |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 356 | m_trayIconMenu->addSeparator(); |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 357 | m_trayIconMenu->addAction(m_checkForUpdates); |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 358 | #endif // AUTOUPDATE |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 359 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 360 | m_trayIconMenu->addSeparator(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 361 | m_trayIconMenu->addAction(m_quitProgram); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 362 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 363 | // create new tray icon |
| 364 | m_trayIcon = new QSystemTrayIcon(this); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 365 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 366 | // associate the menu with the tray icon |
| 367 | m_trayIcon->setContextMenu(m_trayIconMenu); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 368 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 369 | // handle left click of icon |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 370 | connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, |
| 371 | SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 374 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 375 | ChronoShareGui::setIcon() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 376 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 377 | // set the icon image |
Alexander Afanasyev | cf77114 | 2013-03-19 11:15:27 -0700 | [diff] [blame] | 378 | m_trayIcon->setIcon(QIcon(ICON_TRAY_FILE)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 381 | void |
| 382 | ChronoShareGui::openSharedFolder() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 383 | { |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 384 | fs::path realPathToFolder(m_dirPath.toStdString()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 385 | realPathToFolder /= m_sharedFolderName.toStdString(); |
Zhenkai Zhu | 872cc59 | 2013-03-04 14:20:46 -0800 | [diff] [blame] | 386 | QString path = QDir::toNativeSeparators(realPathToFolder.string().c_str()); |
Zhenkai Zhu | ce04cb4 | 2013-02-25 15:29:31 -0800 | [diff] [blame] | 387 | QDesktopServices::openUrl(QUrl("file:///" + path)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 388 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 389 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 390 | void |
| 391 | ChronoShareGui::openInWebBrowser() |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 392 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 393 | QUrl url("http://localhost:9001/"); |
| 394 | url.setFragment("folderHistory&" |
| 395 | "user=" + |
| 396 | QUrl::toPercentEncoding(m_username) + "&" |
| 397 | "folder=" + |
| 398 | QUrl::toPercentEncoding(m_sharedFolderName)); |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 399 | |
| 400 | // 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] | 401 | QDesktopServices::openUrl(url); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 404 | void |
| 405 | ChronoShareGui::openFile() |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 406 | { |
| 407 | // figure out who sent the signal |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 408 | QAction* pAction = qobject_cast<QAction*>(sender()); |
| 409 | if (pAction->isEnabled()) { |
| 410 | // we stored full path of the file in this toolTip field |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 411 | #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 Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 424 | // too bad qt couldn't do highlighting for Linux(or Mac) |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 425 | QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip())); |
| 426 | #endif |
| 427 | } |
| 428 | } |
| 429 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 430 | void |
| 431 | ChronoShareGui::updateRecentFilesMenu() |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 432 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 433 | for (int i = 0; i < 5; i++) { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 434 | m_fileActions[i]->setVisible(false); |
| 435 | } |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 436 | m_dispatcher->LookupRecentFileActions(std::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 437 | 5); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 440 | void |
| 441 | ChronoShareGui::checkFileAction(const std::string& filename, int action, int index) |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 442 | { |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 443 | fs::path realPathToFolder(m_dirPath.toStdString()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 444 | realPathToFolder /= m_sharedFolderName.toStdString(); |
Zhenkai Zhu | 872cc59 | 2013-03-04 14:20:46 -0800 | [diff] [blame] | 445 | realPathToFolder /= filename; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 446 | QString fullPath = realPathToFolder.string().c_str(); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 447 | QFileInfo fileInfo(fullPath); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 448 | if (fileInfo.exists()) { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 449 | // 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 453 | else { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 454 | // 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 464 | if (action == 0) { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 465 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 472 | else { |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 473 | 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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 484 | void |
| 485 | ChronoShareGui::changeSettings() |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 486 | { |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 487 | QString oldUsername = m_username; |
| 488 | QString oldSharedFolderName = m_sharedFolderName; |
| 489 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 490 | if (!editUsername->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 491 | m_username = editUsername->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 492 | else |
| 493 | editUsername->setText(m_username); |
| 494 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 495 | if (!editSharedFolder->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 496 | m_sharedFolderName = editSharedFolder->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 497 | else |
| 498 | editSharedFolder->setText(m_sharedFolderName); |
| 499 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 500 | 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 Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 507 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 508 | if (m_username != oldUsername || oldSharedFolderName != m_sharedFolderName) { |
| 509 | startBackend(true); // restart dispatcher/fswatcher |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 510 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 511 | } |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 514 | void |
| 515 | ChronoShareGui::openFileDialog() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 516 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 517 | 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 Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 527 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 528 | 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 Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 536 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 537 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 538 | QFileInfo qFileInfo(tempPath); |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 539 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 540 | 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 Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 562 | void |
| 563 | ChronoShareGui::trayIconClicked(QSystemTrayIcon::ActivationReason reason) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 564 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 565 | // if double clicked, open shared folder |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 566 | if (reason == QSystemTrayIcon::DoubleClick) { |
| 567 | openSharedFolder(); |
| 568 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 571 | void |
| 572 | ChronoShareGui::viewSettings() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 573 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 574 | //simple for now |
| 575 | this->show(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 576 | this->raise(); |
| 577 | this->activateWindow(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 580 | bool |
| 581 | ChronoShareGui::loadSettings() |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 582 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 583 | bool successful = true; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 584 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 585 | // Load Settings |
| 586 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 587 | QSettings settings; |
| 588 | |
| 589 | _LOG_DEBUG("load settings"); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 590 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 591 | if (settings.contains("username")) { |
| 592 | m_username = settings.value("username", "admin").toString(); |
| 593 | } |
| 594 | else { |
| 595 | successful = false; |
| 596 | } |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 597 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 598 | editUsername->setText(m_username); |
| 599 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 600 | if (settings.contains("sharedfoldername")) { |
| 601 | m_sharedFolderName = settings.value("sharedfoldername", "shared").toString(); |
| 602 | } |
| 603 | else { |
| 604 | successful = false; |
| 605 | } |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 606 | |
| 607 | editSharedFolder->setText(m_sharedFolderName); |
| 608 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 609 | if (settings.contains("dirPath")) { |
| 610 | m_dirPath = settings.value("dirPath", QDir::homePath()).toString(); |
| 611 | } |
| 612 | else { |
| 613 | successful = false; |
| 614 | } |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 615 | |
| 616 | editSharedFolderPath->setText(m_dirPath); |
| 617 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 618 | _LOG_DEBUG("Found configured path: " << (successful ? m_dirPath.toStdString() : std::string("no"))); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 619 | |
| 620 | return successful; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 623 | void |
| 624 | ChronoShareGui::saveSettings() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 625 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 626 | // Save Settings |
| 627 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 628 | QSettings settings; |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 629 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 630 | settings.setValue("dirPath", m_dirPath); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 631 | settings.setValue("username", m_username); |
| 632 | settings.setValue("sharedfoldername", m_sharedFolderName); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 635 | void |
| 636 | ChronoShareGui::closeEvent(QCloseEvent* event) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 637 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 638 | _LOG_DEBUG("Close Event"); |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 639 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 640 | if (m_username.isNull() || m_username == "" || m_sharedFolderName.isNull() || |
| 641 | m_sharedFolderName == "") { |
| 642 | openMessageBox("ChronoShare is not active", |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 643 | "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 Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 645 | } |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame] | 646 | |
| 647 | this->hide(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 648 | event->ignore(); // don't let the event propagate to the base class |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 649 | } |
Jared Lindblom | dc845f0 | 2013-01-18 17:29:40 -0800 | [diff] [blame] | 650 | |
Alexander Afanasyev | 1b51bb5 | 2017-02-10 19:52:32 -0800 | [diff] [blame] | 651 | #ifdef AUTOUPDATE |
| 652 | void |
| 653 | ChronoShareGui::checkForUpdates() |
| 654 | { |
| 655 | m_sparkle.checkForUpdates(); |
| 656 | } |
| 657 | #endif // AUTOUPDATE |
| 658 | |
Lijing Wang | 152449c | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 659 | } // namespace chronoshare |
| 660 | } // namespace ndn |
| 661 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 662 | #include "chronosharegui.moc" |