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