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