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