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