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> |
| 30 | #include <QDesktopServices> |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 31 | |
| 32 | #include <boost/make_shared.hpp> |
| 33 | |
| 34 | using namespace boost; |
| 35 | using namespace Ccnx; |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 36 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 37 | static const string HTTP_SERVER_ADDRESS = "localhost"; |
| 38 | static const string HTTP_SERVER_PORT = "9001"; |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 39 | //static const string DOC_ROOT = ":/html"; |
| 40 | static const string DOC_ROOT = "gui/html"; |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 41 | static const QString ICON_PICTURE_QSTRING(":/images/friends-group-icon.png"); |
| 42 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 43 | INIT_LOGGER ("Gui"); |
| 44 | |
| 45 | ChronoShareGui::ChronoShareGui(QWidget *parent) |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 46 | : QDialog(parent) |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 47 | , m_watcher(0) |
| 48 | , m_dispatcher(0) |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 49 | , m_httpServer(0) |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 50 | #ifdef ADHOC_SUPPORTED |
| 51 | , m_executor (1) |
| 52 | #endif |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 53 | #ifdef SPARKLE_SUPPORTED |
Zhenkai Zhu | bb170d4 | 2013-02-25 13:48:59 -0800 | [diff] [blame] | 54 | , m_autoUpdate(new SparkleAutoUpdate(tr("http://no-url.org"))) |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 55 | #endif |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 56 | { |
Jared Lindblom | f4b900e | 2013-02-07 18:08:47 -0800 | [diff] [blame] | 57 | setWindowTitle("Settings"); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 58 | |
Jared Lindblom | d8b76ad | 2013-02-07 20:37:37 -0800 | [diff] [blame] | 59 | labelUsername = new QLabel("Username (hint: /<username>)"); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 60 | labelSharedFolder = new QLabel("Shared Folder Name"); |
| 61 | labelSharedFolderPath = new QLabel("Shared Folder Path"); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 62 | |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 63 | QRegExp regex("(/[^/]+)+$"); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 64 | QValidator *prefixValidator = new QRegExpValidator(regex, this); |
| 65 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 66 | editUsername = new QLineEdit(); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 67 | editUsername->setValidator(prefixValidator); |
| 68 | |
| 69 | QRegExp noPureWhiteSpace("^\\S+.*$"); |
| 70 | QValidator *wsValidator = new QRegExpValidator(noPureWhiteSpace, this); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 71 | editSharedFolder = new QLineEdit(); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 72 | editSharedFolder->setValidator(wsValidator); |
| 73 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 74 | editSharedFolderPath = new QLineEdit(); |
| 75 | editSharedFolderPath->setReadOnly(true); |
| 76 | QPalette pal = editSharedFolderPath->palette(); |
| 77 | pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Disabled, QPalette::Base)); |
| 78 | editSharedFolderPath->setPalette(pal); |
| 79 | button = new QPushButton("Submit"); |
| 80 | label = new QLabel(); |
| 81 | |
| 82 | connect(button, SIGNAL(clicked()), this, SLOT(changeSettings())); |
| 83 | |
| 84 | mainLayout = new QVBoxLayout; //vertically |
| 85 | mainLayout->addWidget(labelUsername); |
| 86 | mainLayout->addWidget(editUsername); |
| 87 | mainLayout->addWidget(labelSharedFolder); |
| 88 | mainLayout->addWidget(editSharedFolder); |
| 89 | mainLayout->addWidget(labelSharedFolderPath); |
| 90 | mainLayout->addWidget(editSharedFolderPath); |
| 91 | mainLayout->addWidget(button); |
| 92 | mainLayout->addWidget(label); |
| 93 | setLayout(mainLayout); |
| 94 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 95 | // create actions that result from clicking a menu option |
| 96 | createActions(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 98 | // create tray icon |
| 99 | createTrayIcon(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 101 | // set icon image |
| 102 | setIcon(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 104 | // show tray icon |
| 105 | m_trayIcon->show(); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 106 | |
| 107 | // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix, |
| 108 | // const std::string &sharedFolder, const boost::filesystem::path &rootDir, |
| 109 | // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2); |
| 110 | |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 111 | // load settings |
| 112 | if(!loadSettings()) |
| 113 | { |
| 114 | // prompt user to choose folder |
| 115 | openMessageBox("First Time Setup", "Please enter a username, shared folder name and choose the shared folder path on your local filesystem."); |
| 116 | viewSettings(); |
| 117 | openFileDialog(); |
| 118 | viewSettings(); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | startBackend(); |
| 123 | } |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 124 | |
| 125 | #ifdef ADHOC_SUPPORTED |
| 126 | m_executor.start (); |
| 127 | #endif |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void |
| 131 | ChronoShareGui::startBackend() |
| 132 | { |
| 133 | if (m_watcher != 0 && m_dispatcher != 0) |
| 134 | { |
| 135 | return; |
| 136 | } |
| 137 | |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 138 | m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (), |
| 139 | m_dirPath.toStdString (), make_shared<CcnxWrapper> ()); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 140 | |
| 141 | // Alex: this **must** be here, otherwise m_dirPath will be uninitialized |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 142 | m_watcher = new FsWatcher (m_dirPath, |
| 143 | bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1), |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 144 | bind (&Dispatcher::Did_LocalFile_Delete, m_dispatcher, _1)); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 145 | QFileInfo indexHtmlInfo(":/html/index.html"); |
| 146 | if (indexHtmlInfo.exists()) |
| 147 | { |
| 148 | m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT); |
| 149 | m_httpServerThread = boost::thread(&http::server::server::run, m_httpServer); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | _LOG_ERROR ("Http server doc root dir does not exist!"); |
| 154 | // shall we bail here? |
| 155 | } |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | ChronoShareGui::~ChronoShareGui() |
| 159 | { |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 160 | #ifdef ADHOC_SUPPORTED |
| 161 | m_executor.shutdown (); |
| 162 | #endif |
| 163 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 164 | delete m_watcher; // stop filewatching ASAP |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 165 | delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object) |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 166 | delete m_httpServer; |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 167 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 168 | // cleanup |
| 169 | delete m_trayIcon; |
| 170 | delete m_trayIconMenu; |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 171 | #ifdef ADHOC_SUPPORTED |
| 172 | delete m_wifiAction; |
| 173 | #endif |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 174 | #ifdef SPARKLE_SUPPORTED |
| 175 | delete m_autoUpdate; |
Zhenkai Zhu | bb170d4 | 2013-02-25 13:48:59 -0800 | [diff] [blame] | 176 | delete m_checkForUpdates; |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 177 | #endif |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 178 | delete m_openFolder; |
| 179 | delete m_viewSettings; |
| 180 | delete m_changeFolder; |
| 181 | delete m_quitProgram; |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 182 | |
| 183 | delete labelUsername; |
| 184 | delete labelSharedFolder; |
| 185 | delete editUsername; |
| 186 | delete editSharedFolder; |
| 187 | delete button; |
| 188 | delete label; |
| 189 | delete mainLayout; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void ChronoShareGui::openMessageBox(QString title, QString text) |
| 193 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 194 | QMessageBox messageBox(this); |
| 195 | messageBox.setWindowTitle(title); |
| 196 | messageBox.setText(text); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 197 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 198 | messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 199 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 200 | messageBox.exec(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext) |
| 204 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 205 | QMessageBox messageBox(this); |
| 206 | messageBox.setWindowTitle(title); |
| 207 | messageBox.setText(text); |
| 208 | messageBox.setInformativeText(infotext); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 209 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 210 | messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 211 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 212 | messageBox.exec(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void ChronoShareGui::createActions() |
| 216 | { |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 217 | _LOG_DEBUG ("Create actions"); |
| 218 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 219 | // create the "open folder" action |
| 220 | m_openFolder = new QAction(tr("&Open Folder"), this); |
| 221 | connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 222 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 223 | // create the "view settings" action |
| 224 | m_viewSettings = new QAction(tr("&View Settings"), this); |
| 225 | connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings())); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 226 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 227 | // create the "change folder" action |
| 228 | m_changeFolder = new QAction(tr("&Change Folder"), this); |
| 229 | connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog())); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 230 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 231 | #ifdef ADHOC_SUPPORTED |
| 232 | // create "AdHoc Wifi" action |
| 233 | m_wifiAction = new QAction (tr("Enable AdHoc &WiFI"), m_trayIconMenu); |
| 234 | m_wifiAction->setChecked (false); |
| 235 | m_wifiAction->setCheckable (true); |
| 236 | connect (m_wifiAction, SIGNAL (toggled(bool)), this, SLOT(onAdHocChange(bool))); |
| 237 | #endif |
| 238 | |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 239 | #ifdef SPARKLE_SUPPORTED |
Zhenkai Zhu | bb170d4 | 2013-02-25 13:48:59 -0800 | [diff] [blame] | 240 | m_checkForUpdates = new QAction (tr("Check For Updates"), this); |
| 241 | connect (m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates())); |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 242 | #endif |
| 243 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 244 | // create the "quit program" action |
| 245 | m_quitProgram = new QAction(tr("&Quit"), this); |
| 246 | connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void ChronoShareGui::createTrayIcon() |
| 250 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 251 | // create a new icon menu |
| 252 | m_trayIconMenu = new QMenu(this); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 253 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 254 | // add actions to the menu |
| 255 | m_trayIconMenu->addAction(m_openFolder); |
| 256 | m_trayIconMenu->addSeparator(); |
| 257 | m_trayIconMenu->addAction(m_viewSettings); |
| 258 | m_trayIconMenu->addAction(m_changeFolder); |
| 259 | m_trayIconMenu->addSeparator(); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 260 | m_trayIconMenu->addAction(m_wifiAction); |
| 261 | m_trayIconMenu->addSeparator(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 262 | m_trayIconMenu->addAction(m_quitProgram); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 263 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 264 | // create new tray icon |
| 265 | m_trayIcon = new QSystemTrayIcon(this); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 266 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 267 | #ifdef ADHOC_SUPPORTED |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 268 | // associate the menu with the tray icon |
| 269 | m_trayIcon->setContextMenu(m_trayIconMenu); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 270 | #endif |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 271 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 272 | // handle left click of icon |
| 273 | connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 276 | void |
| 277 | ChronoShareGui::onAdHocChange (bool state) |
| 278 | { |
| 279 | #ifdef ADHOC_SUPPORTED |
| 280 | if (m_wifiAction->isChecked ()) |
| 281 | { |
| 282 | QMessageBox msgBox; |
| 283 | msgBox.setText ("WARNING: your WiFi will be disconnected"); |
| 284 | msgBox.setIcon (QMessageBox::Warning); |
| 285 | msgBox.setInformativeText("AdHoc WiFi will disconnect your current WiFi.\n Are you sure that you are OK with that?"); |
| 286 | msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); |
| 287 | msgBox.setDefaultButton(QMessageBox::Cancel); |
| 288 | int ret = msgBox.exec(); |
| 289 | |
| 290 | if (ret == QMessageBox::Cancel) |
| 291 | { |
| 292 | m_wifiAction->setChecked (false); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | |
| 297 | m_wifiAction->setText (tr("Disable AdHoc WiFI")); |
| 298 | |
| 299 | // create adhoc |
| 300 | m_executor.execute (Adhoc::CreateAdhoc); |
| 301 | } |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | m_wifiAction->setText (tr("Enable AdHoc WiFI")); |
| 306 | |
| 307 | // disable adhoc |
| 308 | m_executor.execute (Adhoc::DestroyAdhoc); |
| 309 | |
| 310 | // a trick in DestroyAdhoc ensures that WiFi will be reconnected to a default WiFi |
| 311 | } |
| 312 | #endif |
| 313 | } |
| 314 | |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 315 | #ifdef SPARKLE_SUPPORTED |
| 316 | void |
| 317 | ChronoShareGui::onCheckForUpdates() |
| 318 | { |
| 319 | m_autoUpdate->checkForUpdates(); |
| 320 | } |
| 321 | #endif |
| 322 | |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 323 | void ChronoShareGui::setIcon() |
| 324 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 325 | // set the icon image |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 326 | m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void ChronoShareGui::openSharedFolder() |
| 330 | { |
Zhenkai Zhu | ce04cb4 | 2013-02-25 15:29:31 -0800 | [diff] [blame] | 331 | QString path = QDir::toNativeSeparators(m_dirPath); |
| 332 | QDesktopServices::openUrl(QUrl("file:///" + path)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 333 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 334 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 335 | void ChronoShareGui::changeSettings() |
| 336 | { |
| 337 | if(!editUsername->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 338 | m_username = editUsername->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 339 | else |
| 340 | editUsername->setText(m_username); |
| 341 | |
| 342 | if(!editSharedFolder->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 343 | m_sharedFolderName = editSharedFolder->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 344 | else |
| 345 | editSharedFolder->setText(m_sharedFolderName); |
| 346 | |
| 347 | saveSettings(); |
| 348 | this->hide(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 349 | |
| 350 | startBackend(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 353 | void ChronoShareGui::openFileDialog() |
| 354 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 355 | // prompt user for new directory |
| 356 | QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"), |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 357 | m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 358 | QFileInfo qFileInfo (tempPath); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 359 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 360 | if(qFileInfo.isDir()) |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 361 | { |
| 362 | m_dirPath = tempPath; |
| 363 | editSharedFolderPath->setText(m_dirPath); |
| 364 | } |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 365 | else |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 366 | { |
| 367 | openMessageBox ("Error", "Not a valid folder, Ignoring."); |
| 368 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 369 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 370 | _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ()); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 371 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 372 | // save settings |
| 373 | saveSettings(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 376 | void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 377 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 378 | // if double clicked, open shared folder |
| 379 | if(reason == QSystemTrayIcon::DoubleClick) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 380 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 381 | openSharedFolder(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 385 | void ChronoShareGui::viewSettings() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 386 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 387 | //simple for now |
| 388 | this->show(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 389 | this->raise(); |
| 390 | this->activateWindow(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | bool ChronoShareGui::loadSettings() |
| 394 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 395 | bool successful = true; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 396 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 397 | // Load Settings |
| 398 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 399 | QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare"); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 400 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 401 | // _LOG_DEBUG (lexical_cast<string> (settings.allKeys())); |
| 402 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 403 | if(settings.contains("username")) |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 404 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 405 | m_username = settings.value("username", "admin").toString(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 406 | } |
| 407 | else |
| 408 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 409 | successful = false; |
| 410 | } |
| 411 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 412 | editUsername->setText(m_username); |
| 413 | |
| 414 | if(settings.contains("sharedfoldername")) |
| 415 | { |
| 416 | m_sharedFolderName = settings.value("sharedfoldername", "shared").toString(); |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | successful = false; |
| 421 | } |
| 422 | |
| 423 | editSharedFolder->setText(m_sharedFolderName); |
| 424 | |
| 425 | if(settings.contains("dirPath")) |
| 426 | { |
| 427 | m_dirPath = settings.value("dirPath", QDir::homePath()).toString(); |
| 428 | } |
| 429 | else |
| 430 | { |
| 431 | successful = false; |
| 432 | } |
| 433 | |
| 434 | editSharedFolderPath->setText(m_dirPath); |
| 435 | |
| 436 | _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no"))); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 437 | |
| 438 | return successful; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void ChronoShareGui::saveSettings() |
| 442 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 443 | // Save Settings |
| 444 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 445 | QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare"); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 446 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 447 | settings.setValue("dirPath", m_dirPath); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 448 | settings.setValue("username", m_username); |
| 449 | settings.setValue("sharedfoldername", m_sharedFolderName); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | void ChronoShareGui::closeEvent(QCloseEvent* event) |
| 453 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 454 | _LOG_DEBUG ("Close Event") |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 455 | this->hide(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 456 | event->ignore(); // don't let the event propagate to the base class |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 457 | } |
Jared Lindblom | dc845f0 | 2013-01-18 17:29:40 -0800 | [diff] [blame] | 458 | |
| 459 | #if WAF |
| 460 | #include "chronosharegui.moc" |
| 461 | #include "chronosharegui.cpp.moc" |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 462 | #endif |