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 | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 26 | #include "logging.h" |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 27 | #include "ccnx-wrapper.h" |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 28 | #include <QValidator> |
Zhenkai Zhu | ce04cb4 | 2013-02-25 15:29:31 -0800 | [diff] [blame] | 29 | #include <QDir> |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 30 | #include <QFileInfo> |
Zhenkai Zhu | ce04cb4 | 2013-02-25 15:29:31 -0800 | [diff] [blame] | 31 | #include <QDesktopServices> |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 32 | |
| 33 | #include <boost/make_shared.hpp> |
| 34 | |
| 35 | using namespace boost; |
| 36 | using namespace Ccnx; |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 37 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 38 | static const string HTTP_SERVER_ADDRESS = "localhost"; |
| 39 | static const string HTTP_SERVER_PORT = "9001"; |
Zhenkai Zhu | 6b53f93 | 2013-03-02 20:46:02 -0800 | [diff] [blame] | 40 | #ifdef _DEBUG |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 41 | static const string DOC_ROOT = "gui/html"; |
Zhenkai Zhu | 6b53f93 | 2013-03-02 20:46:02 -0800 | [diff] [blame] | 42 | #else |
| 43 | static const string DOC_ROOT = ":/html"; |
| 44 | #endif |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 45 | static const QString ICON_PICTURE_QSTRING(":/images/friends-group-icon.png"); |
| 46 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 47 | INIT_LOGGER ("Gui"); |
| 48 | |
| 49 | ChronoShareGui::ChronoShareGui(QWidget *parent) |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 50 | : QDialog(parent) |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 51 | , m_watcher(0) |
| 52 | , m_dispatcher(0) |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 53 | , m_httpServer(0) |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 54 | #ifdef ADHOC_SUPPORTED |
| 55 | , m_executor (1) |
| 56 | #endif |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 57 | #ifdef SPARKLE_SUPPORTED |
Zhenkai Zhu | 8e3e907 | 2013-02-28 15:40:51 -0800 | [diff] [blame] | 58 | , 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] | 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 | d437ffc | 2013-03-04 12:29:23 -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("(/[^/]+)+$"); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 69 | QValidator *prefixValidator = new QRegExpValidator(regex, this); |
| 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+.*$"); |
| 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 |
| 115 | if(!loadSettings()) |
| 116 | { |
| 117 | // prompt user to choose folder |
| 118 | openMessageBox("First Time Setup", "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 | { |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 125 | if (m_username.isNull () || m_username == "" || |
| 126 | m_sharedFolderName.isNull () || m_sharedFolderName == "") |
| 127 | { |
| 128 | openMessageBox("First Time Setup", "To activate ChronoShare, please configure your username and shared folder name."); |
| 129 | viewSettings(); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | startBackend(); |
| 134 | } |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 135 | } |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 136 | |
| 137 | #ifdef ADHOC_SUPPORTED |
| 138 | m_executor.start (); |
| 139 | #endif |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 143 | ChronoShareGui::startBackend (bool restart/*=false*/) |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 144 | { |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 145 | if (m_username.isNull () || m_username=="" || |
| 146 | m_sharedFolderName.isNull () || m_sharedFolderName=="" || |
| 147 | m_dirPath.isNull () || m_dirPath=="") |
| 148 | { |
| 149 | _LOG_DEBUG ("Don't start backend, because settings are in progress or incomplete"); |
| 150 | return; |
| 151 | } |
| 152 | |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 153 | if (m_watcher != 0 && m_dispatcher != 0) |
| 154 | { |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 155 | if (!restart) |
| 156 | { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | _LOG_DEBUG ("Restarting Dispatcher and FileWatcher for the new location or new username"); |
| 161 | delete m_watcher; // stop filewatching ASAP |
| 162 | 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] | 163 | } |
| 164 | |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 165 | filesystem::path realPathToFolder (m_dirPath.toStdString ()); |
| 166 | realPathToFolder /= m_sharedFolderName.toStdString (); |
| 167 | |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 168 | m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (), |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 169 | realPathToFolder, make_shared<CcnxWrapper> ()); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 170 | |
| 171 | // Alex: this **must** be here, otherwise m_dirPath will be uninitialized |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 172 | m_watcher = new FsWatcher (realPathToFolder.string ().c_str (), |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 173 | bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1), |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 174 | bind (&Dispatcher::Did_LocalFile_Delete, m_dispatcher, _1)); |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 175 | |
| 176 | if (m_httpServer != 0) |
| 177 | { |
| 178 | // no need to restart webserver if it already exists |
| 179 | return; |
| 180 | } |
| 181 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 182 | QFileInfo indexHtmlInfo(":/html/index.html"); |
| 183 | if (indexHtmlInfo.exists()) |
| 184 | { |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 185 | try |
| 186 | { |
| 187 | m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT); |
| 188 | m_httpServerThread = boost::thread(&http::server::server::run, m_httpServer); |
| 189 | } |
| 190 | catch (std::exception &e) |
| 191 | { |
| 192 | _LOG_ERROR ("Start http server failed"); |
| 193 | m_httpServer = 0; // just to make sure |
| 194 | QMessageBox msgBox; |
| 195 | msgBox.setText ("WARNING: Cannot start http server!"); |
| 196 | msgBox.setIcon (QMessageBox::Warning); |
| 197 | msgBox.setInformativeText(QString("Starting http server failed. You will not be able to check history from web brower. Exception caused: %1").arg(e.what())); |
| 198 | msgBox.setStandardButtons(QMessageBox::Ok); |
| 199 | msgBox.exec(); |
| 200 | } |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 201 | } |
| 202 | else |
| 203 | { |
| 204 | _LOG_ERROR ("Http server doc root dir does not exist!"); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 205 | } |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | ChronoShareGui::~ChronoShareGui() |
| 209 | { |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 210 | #ifdef ADHOC_SUPPORTED |
| 211 | m_executor.shutdown (); |
| 212 | #endif |
| 213 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 214 | delete m_watcher; // stop filewatching ASAP |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 215 | 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] | 216 | if (m_httpServer != 0) |
| 217 | { |
| 218 | m_httpServer->handle_stop(); |
| 219 | m_httpServerThread.join(); |
| 220 | delete m_httpServer; |
| 221 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 222 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 223 | // cleanup |
| 224 | delete m_trayIcon; |
| 225 | delete m_trayIconMenu; |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 226 | #ifdef ADHOC_SUPPORTED |
| 227 | delete m_wifiAction; |
| 228 | #endif |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 229 | #ifdef SPARKLE_SUPPORTED |
| 230 | delete m_autoUpdate; |
Zhenkai Zhu | bb170d4 | 2013-02-25 13:48:59 -0800 | [diff] [blame] | 231 | delete m_checkForUpdates; |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 232 | #endif |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 233 | delete m_openFolder; |
| 234 | delete m_viewSettings; |
| 235 | delete m_changeFolder; |
| 236 | delete m_quitProgram; |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 237 | |
| 238 | delete labelUsername; |
| 239 | delete labelSharedFolder; |
| 240 | delete editUsername; |
| 241 | delete editSharedFolder; |
| 242 | delete button; |
| 243 | delete label; |
| 244 | delete mainLayout; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void ChronoShareGui::openMessageBox(QString title, QString text) |
| 248 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 249 | QMessageBox messageBox(this); |
| 250 | messageBox.setWindowTitle(title); |
| 251 | messageBox.setText(text); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 252 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 253 | messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 254 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 255 | messageBox.exec(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext) |
| 259 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 260 | QMessageBox messageBox(this); |
| 261 | messageBox.setWindowTitle(title); |
| 262 | messageBox.setText(text); |
| 263 | messageBox.setInformativeText(infotext); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 264 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 265 | messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 266 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 267 | messageBox.exec(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 270 | void ChronoShareGui::createActionsAndMenu() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 271 | { |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 272 | _LOG_DEBUG ("Create actions"); |
| 273 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 274 | // create the "open folder" action |
| 275 | m_openFolder = new QAction(tr("&Open Folder"), this); |
| 276 | connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 277 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 278 | m_openWeb = new QAction(tr("Open in &Browser"), this); |
| 279 | connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser())); |
| 280 | |
| 281 | m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this); |
| 282 | for (int i = 0; i < 5; i++) |
| 283 | { |
| 284 | m_fileActions[i] = new QAction(this); |
| 285 | m_fileActions[i]->setVisible(false); |
| 286 | connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile())); |
| 287 | m_recentFilesMenu->addAction(m_fileActions[i]); |
| 288 | } |
| 289 | connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu())); |
| 290 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 291 | // create the "view settings" action |
| 292 | m_viewSettings = new QAction(tr("&View Settings"), this); |
| 293 | connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings())); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 294 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 295 | // create the "change folder" action |
| 296 | m_changeFolder = new QAction(tr("&Change Folder"), this); |
| 297 | connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog())); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 298 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 299 | #ifdef ADHOC_SUPPORTED |
| 300 | // create "AdHoc Wifi" action |
| 301 | m_wifiAction = new QAction (tr("Enable AdHoc &WiFI"), m_trayIconMenu); |
| 302 | m_wifiAction->setChecked (false); |
| 303 | m_wifiAction->setCheckable (true); |
| 304 | connect (m_wifiAction, SIGNAL (toggled(bool)), this, SLOT(onAdHocChange(bool))); |
| 305 | #endif |
| 306 | |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 307 | #ifdef SPARKLE_SUPPORTED |
Zhenkai Zhu | bb170d4 | 2013-02-25 13:48:59 -0800 | [diff] [blame] | 308 | m_checkForUpdates = new QAction (tr("Check For Updates"), this); |
| 309 | connect (m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates())); |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 310 | #endif |
| 311 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 312 | // create the "quit program" action |
| 313 | m_quitProgram = new QAction(tr("&Quit"), this); |
| 314 | connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void ChronoShareGui::createTrayIcon() |
| 318 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 319 | // create a new icon menu |
| 320 | m_trayIconMenu = new QMenu(this); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 321 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 322 | // add actions to the menu |
| 323 | m_trayIconMenu->addAction(m_openFolder); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 324 | m_trayIconMenu->addAction(m_openWeb); |
| 325 | m_trayIconMenu->addMenu(m_recentFilesMenu); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 326 | m_trayIconMenu->addSeparator(); |
| 327 | m_trayIconMenu->addAction(m_viewSettings); |
| 328 | m_trayIconMenu->addAction(m_changeFolder); |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 329 | |
| 330 | #ifdef SPARKLE_SUPPORTED |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 331 | m_trayIconMenu->addSeparator(); |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 332 | m_trayIconMenu->addAction(m_checkForUpdates); |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 333 | #endif |
| 334 | |
| 335 | #ifdef ADHOC_SUPPORTED |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 336 | m_trayIconMenu->addSeparator(); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 337 | m_trayIconMenu->addAction(m_wifiAction); |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 338 | #endif |
| 339 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 340 | m_trayIconMenu->addSeparator(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 341 | m_trayIconMenu->addAction(m_quitProgram); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 342 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 343 | // create new tray icon |
| 344 | m_trayIcon = new QSystemTrayIcon(this); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 345 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 346 | #ifdef ADHOC_SUPPORTED |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 347 | // associate the menu with the tray icon |
| 348 | m_trayIcon->setContextMenu(m_trayIconMenu); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 349 | #endif |
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 |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 407 | m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void ChronoShareGui::openSharedFolder() |
| 411 | { |
Zhenkai Zhu | ce04cb4 | 2013-02-25 15:29:31 -0800 | [diff] [blame] | 412 | QString path = QDir::toNativeSeparators(m_dirPath); |
| 413 | QDesktopServices::openUrl(QUrl("file:///" + path)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 414 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 415 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 416 | void ChronoShareGui::openInWebBrowser() |
| 417 | { |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 418 | QUrl url ("http://localhost:9001/"); |
| 419 | url.setFragment ("fileList&" |
| 420 | "user=" + QUrl::toPercentEncoding (m_username) + "&" |
| 421 | "folder=" + QUrl::toPercentEncoding (m_sharedFolderName)); |
| 422 | |
| 423 | // i give up. there is double encoding and I have no idea how to fight it... |
| 424 | QDesktopServices::openUrl (url); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void ChronoShareGui::openFile() |
| 428 | { |
| 429 | // figure out who sent the signal |
| 430 | QAction *pAction = qobject_cast<QAction*>(sender()); |
| 431 | if (pAction->isEnabled()) |
| 432 | { |
| 433 | // we stored full path of the file in this toolTip field |
| 434 | #ifdef Q_WS_MAC |
| 435 | // we do some hack so we could show the file in Finder highlighted |
| 436 | QStringList args; |
| 437 | args << "-e"; |
| 438 | args << "tell application \"Finder\""; |
| 439 | args << "-e"; |
| 440 | args << "activate"; |
| 441 | args << "-e"; |
| 442 | args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\""; |
| 443 | args << "-e"; |
| 444 | args << "end tell"; |
| 445 | QProcess::startDetached("osascript", args); |
| 446 | #else |
| 447 | // too bad qt couldn't do highlighting for Linux (or Mac) |
| 448 | QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip())); |
| 449 | #endif |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void ChronoShareGui::updateRecentFilesMenu() |
| 454 | { |
| 455 | for (int i = 0; i < 5; i++) |
| 456 | { |
| 457 | m_fileActions[i]->setVisible(false); |
| 458 | } |
| 459 | m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5); |
| 460 | } |
| 461 | |
| 462 | void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index) |
| 463 | { |
| 464 | QString fullPath = m_dirPath + "/" + filename.c_str(); |
| 465 | QFileInfo fileInfo(fullPath); |
| 466 | if (fileInfo.exists()) |
| 467 | { |
| 468 | // This is a hack, we just use some field to store the path |
| 469 | m_fileActions[index]->setToolTip(fileInfo.absolutePath()); |
| 470 | m_fileActions[index]->setEnabled(true); |
| 471 | } |
| 472 | else |
| 473 | { |
| 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 |
| 484 | if (action == 0) |
| 485 | { |
| 486 | QFont font; |
| 487 | // supposed by change the font, didn't happen |
| 488 | font.setWeight(QFont::Light); |
| 489 | m_fileActions[index]->setFont(font); |
| 490 | m_fileActions[index]->setToolTip(tr("Fetching...")); |
| 491 | } |
| 492 | // DELETE |
| 493 | else |
| 494 | { |
| 495 | QFont font; |
| 496 | // supposed by change the font, didn't happen |
| 497 | font.setStrikeOut(true); |
| 498 | m_fileActions[index]->setFont(font); |
| 499 | m_fileActions[index]->setToolTip(tr("Deleted...")); |
| 500 | } |
| 501 | } |
| 502 | m_fileActions[index]->setText(fileInfo.fileName()); |
| 503 | m_fileActions[index]->setVisible(true); |
| 504 | } |
| 505 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 506 | void ChronoShareGui::changeSettings() |
| 507 | { |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 508 | QString oldUsername = m_username; |
| 509 | QString oldSharedFolderName = m_sharedFolderName; |
| 510 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 511 | if(!editUsername->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 512 | m_username = editUsername->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 513 | else |
| 514 | editUsername->setText(m_username); |
| 515 | |
| 516 | if(!editSharedFolder->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 517 | m_sharedFolderName = editSharedFolder->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 518 | else |
| 519 | editSharedFolder->setText(m_sharedFolderName); |
| 520 | |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 521 | if (m_username.isNull () || m_username=="" || |
| 522 | m_sharedFolderName.isNull () || m_sharedFolderName=="") |
| 523 | { |
| 524 | openMessageBox ("Error", |
| 525 | "Username and shared folder name cannot be empty"); |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | saveSettings(); |
| 530 | this->hide(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 531 | |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 532 | if (m_username != oldUsername || |
| 533 | oldSharedFolderName != m_sharedFolderName) |
| 534 | { |
| 535 | startBackend (true); // restart dispatcher/fswatcher |
| 536 | } |
| 537 | } |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 540 | void ChronoShareGui::openFileDialog() |
| 541 | { |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 542 | while (true) |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 543 | { |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 544 | // prompt user for new directory |
| 545 | QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose ChronoShare folder"), |
| 546 | m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 547 | if (tempPath.isNull ()) |
| 548 | { |
| 549 | if (m_dirPath.isNull ()) |
| 550 | { |
| 551 | openMessageBox ("Notice", "ChronoShare will use [" + QDir::homePath () + "/ChronoShare]. \n\nYou can change it later selecting \"Change Folder\" menu."); |
| 552 | |
| 553 | m_dirPath = QDir::homePath () + "/ChronoShare"; |
| 554 | editSharedFolderPath->setText (m_dirPath); |
| 555 | break; |
| 556 | } |
| 557 | else |
| 558 | { |
| 559 | // user just cancelled, no need to do anything else |
| 560 | return; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | QFileInfo qFileInfo (tempPath); |
| 565 | |
| 566 | if (!qFileInfo.isDir()) |
| 567 | { |
| 568 | openMessageBox ("Error", "Please select an existing folder or create a new one."); |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | if (m_dirPath == tempPath) |
| 573 | { |
| 574 | // user selected the same directory, no need to do anything |
| 575 | return; |
| 576 | } |
| 577 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 578 | m_dirPath = tempPath; |
| 579 | editSharedFolderPath->setText(m_dirPath); |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 580 | break; |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 581 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 582 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 583 | _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ()); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 584 | // save settings |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 585 | saveSettings (); |
| 586 | |
| 587 | startBackend (true); // restart dispatcher/fswatcher |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 590 | void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 591 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 592 | // if double clicked, open shared folder |
| 593 | if(reason == QSystemTrayIcon::DoubleClick) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 594 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 595 | openSharedFolder(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 599 | void ChronoShareGui::viewSettings() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 600 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 601 | //simple for now |
| 602 | this->show(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 603 | this->raise(); |
| 604 | this->activateWindow(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | bool ChronoShareGui::loadSettings() |
| 608 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 609 | bool successful = true; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 610 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 611 | // Load Settings |
| 612 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 613 | QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare"); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 614 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 615 | // _LOG_DEBUG (lexical_cast<string> (settings.allKeys())); |
| 616 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 617 | if(settings.contains("username")) |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 618 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 619 | m_username = settings.value("username", "admin").toString(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 620 | } |
| 621 | else |
| 622 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 623 | successful = false; |
| 624 | } |
| 625 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 626 | editUsername->setText(m_username); |
| 627 | |
| 628 | if(settings.contains("sharedfoldername")) |
| 629 | { |
| 630 | m_sharedFolderName = settings.value("sharedfoldername", "shared").toString(); |
| 631 | } |
| 632 | else |
| 633 | { |
| 634 | successful = false; |
| 635 | } |
| 636 | |
| 637 | editSharedFolder->setText(m_sharedFolderName); |
| 638 | |
| 639 | if(settings.contains("dirPath")) |
| 640 | { |
| 641 | m_dirPath = settings.value("dirPath", QDir::homePath()).toString(); |
| 642 | } |
| 643 | else |
| 644 | { |
| 645 | successful = false; |
| 646 | } |
| 647 | |
| 648 | editSharedFolderPath->setText(m_dirPath); |
| 649 | |
| 650 | _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no"))); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 651 | |
| 652 | return successful; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | void ChronoShareGui::saveSettings() |
| 656 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 657 | // Save Settings |
| 658 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 659 | QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare"); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 660 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 661 | settings.setValue("dirPath", m_dirPath); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 662 | settings.setValue("username", m_username); |
| 663 | settings.setValue("sharedfoldername", m_sharedFolderName); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void ChronoShareGui::closeEvent(QCloseEvent* event) |
| 667 | { |
Alexander Afanasyev | d437ffc | 2013-03-04 12:29:23 -0800 | [diff] [blame^] | 668 | _LOG_DEBUG ("Close Event"); |
| 669 | |
| 670 | if (m_username.isNull () || m_username == "" || |
| 671 | m_sharedFolderName.isNull () || m_sharedFolderName == "") |
| 672 | { |
| 673 | 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"); |
| 674 | } |
| 675 | |
| 676 | this->hide(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 677 | event->ignore(); // don't let the event propagate to the base class |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 678 | } |
Jared Lindblom | dc845f0 | 2013-01-18 17:29:40 -0800 | [diff] [blame] | 679 | |
| 680 | #if WAF |
| 681 | #include "chronosharegui.moc" |
| 682 | #include "chronosharegui.cpp.moc" |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 683 | #endif |