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