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