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