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