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