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