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> |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 30 | #include <QFileInfo> |
Zhenkai Zhu | ce04cb4 | 2013-02-25 15:29:31 -0800 | [diff] [blame] | 31 | #include <QDesktopServices> |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 32 | |
| 33 | #include <boost/make_shared.hpp> |
| 34 | |
| 35 | using namespace boost; |
| 36 | using namespace Ccnx; |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 37 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 38 | static const string HTTP_SERVER_ADDRESS = "localhost"; |
| 39 | static const string HTTP_SERVER_PORT = "9001"; |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 40 | //static const string DOC_ROOT = ":/html"; |
| 41 | static const string DOC_ROOT = "gui/html"; |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 42 | static const QString ICON_PICTURE_QSTRING(":/images/friends-group-icon.png"); |
| 43 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 44 | INIT_LOGGER ("Gui"); |
| 45 | |
| 46 | ChronoShareGui::ChronoShareGui(QWidget *parent) |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 47 | : QDialog(parent) |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 48 | , m_watcher(0) |
| 49 | , m_dispatcher(0) |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 50 | , m_httpServer(0) |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 51 | #ifdef ADHOC_SUPPORTED |
| 52 | , m_executor (1) |
| 53 | #endif |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 54 | #ifdef SPARKLE_SUPPORTED |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 55 | , m_autoUpdate(new SparkleAutoUpdate(tr("http://irl.cs.ucla.edu"))) |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 56 | #endif |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 57 | { |
Jared Lindblom | f4b900e | 2013-02-07 18:08:47 -0800 | [diff] [blame] | 58 | setWindowTitle("Settings"); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 59 | |
Jared Lindblom | d8b76ad | 2013-02-07 20:37:37 -0800 | [diff] [blame] | 60 | labelUsername = new QLabel("Username (hint: /<username>)"); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 61 | labelSharedFolder = new QLabel("Shared Folder Name"); |
| 62 | labelSharedFolderPath = new QLabel("Shared Folder Path"); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 63 | |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 64 | QRegExp regex("(/[^/]+)+$"); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 65 | QValidator *prefixValidator = new QRegExpValidator(regex, this); |
| 66 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 67 | editUsername = new QLineEdit(); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 68 | editUsername->setValidator(prefixValidator); |
| 69 | |
| 70 | QRegExp noPureWhiteSpace("^\\S+.*$"); |
| 71 | QValidator *wsValidator = new QRegExpValidator(noPureWhiteSpace, this); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 72 | editSharedFolder = new QLineEdit(); |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 73 | editSharedFolder->setValidator(wsValidator); |
| 74 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 75 | editSharedFolderPath = new QLineEdit(); |
| 76 | editSharedFolderPath->setReadOnly(true); |
| 77 | QPalette pal = editSharedFolderPath->palette(); |
| 78 | pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Disabled, QPalette::Base)); |
| 79 | editSharedFolderPath->setPalette(pal); |
| 80 | button = new QPushButton("Submit"); |
| 81 | label = new QLabel(); |
| 82 | |
| 83 | connect(button, SIGNAL(clicked()), this, SLOT(changeSettings())); |
| 84 | |
| 85 | mainLayout = new QVBoxLayout; //vertically |
| 86 | mainLayout->addWidget(labelUsername); |
| 87 | mainLayout->addWidget(editUsername); |
| 88 | mainLayout->addWidget(labelSharedFolder); |
| 89 | mainLayout->addWidget(editSharedFolder); |
| 90 | mainLayout->addWidget(labelSharedFolderPath); |
| 91 | mainLayout->addWidget(editSharedFolderPath); |
| 92 | mainLayout->addWidget(button); |
| 93 | mainLayout->addWidget(label); |
| 94 | setLayout(mainLayout); |
| 95 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 96 | // create actions that result from clicking a menu option |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 97 | createActionsAndMenu(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 99 | // create tray icon |
| 100 | createTrayIcon(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 102 | // set icon image |
| 103 | setIcon(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 105 | // show tray icon |
| 106 | m_trayIcon->show(); |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 107 | |
| 108 | // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix, |
| 109 | // const std::string &sharedFolder, const boost::filesystem::path &rootDir, |
| 110 | // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2); |
| 111 | |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 112 | // load settings |
| 113 | if(!loadSettings()) |
| 114 | { |
| 115 | // prompt user to choose folder |
| 116 | openMessageBox("First Time Setup", "Please enter a username, shared folder name and choose the shared folder path on your local filesystem."); |
| 117 | viewSettings(); |
| 118 | openFileDialog(); |
| 119 | viewSettings(); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | startBackend(); |
| 124 | } |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 125 | |
| 126 | #ifdef ADHOC_SUPPORTED |
| 127 | m_executor.start (); |
| 128 | #endif |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void |
| 132 | ChronoShareGui::startBackend() |
| 133 | { |
| 134 | if (m_watcher != 0 && m_dispatcher != 0) |
| 135 | { |
| 136 | return; |
| 137 | } |
| 138 | |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 139 | m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (), |
| 140 | m_dirPath.toStdString (), make_shared<CcnxWrapper> ()); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 141 | |
| 142 | // Alex: this **must** be here, otherwise m_dirPath will be uninitialized |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 143 | m_watcher = new FsWatcher (m_dirPath, |
| 144 | bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1), |
Zhenkai Zhu | d175627 | 2013-02-01 17:02:18 -0800 | [diff] [blame] | 145 | bind (&Dispatcher::Did_LocalFile_Delete, m_dispatcher, _1)); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 146 | QFileInfo indexHtmlInfo(":/html/index.html"); |
| 147 | if (indexHtmlInfo.exists()) |
| 148 | { |
| 149 | m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT); |
| 150 | m_httpServerThread = boost::thread(&http::server::server::run, m_httpServer); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | _LOG_ERROR ("Http server doc root dir does not exist!"); |
| 155 | // shall we bail here? |
| 156 | } |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | ChronoShareGui::~ChronoShareGui() |
| 160 | { |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 161 | #ifdef ADHOC_SUPPORTED |
| 162 | m_executor.shutdown (); |
| 163 | #endif |
| 164 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 165 | delete m_watcher; // stop filewatching ASAP |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 166 | delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object) |
Zhenkai Zhu | 907cbaf | 2013-02-27 22:29:23 -0800 | [diff] [blame] | 167 | m_httpServer->handle_stop(); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 168 | delete m_httpServer; |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 170 | // cleanup |
| 171 | delete m_trayIcon; |
| 172 | delete m_trayIconMenu; |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 173 | #ifdef ADHOC_SUPPORTED |
| 174 | delete m_wifiAction; |
| 175 | #endif |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 176 | #ifdef SPARKLE_SUPPORTED |
| 177 | delete m_autoUpdate; |
Zhenkai Zhu | bb170d4 | 2013-02-25 13:48:59 -0800 | [diff] [blame] | 178 | delete m_checkForUpdates; |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 179 | #endif |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 180 | delete m_openFolder; |
| 181 | delete m_viewSettings; |
| 182 | delete m_changeFolder; |
| 183 | delete m_quitProgram; |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 184 | |
| 185 | delete labelUsername; |
| 186 | delete labelSharedFolder; |
| 187 | delete editUsername; |
| 188 | delete editSharedFolder; |
| 189 | delete button; |
| 190 | delete label; |
| 191 | delete mainLayout; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void ChronoShareGui::openMessageBox(QString title, QString text) |
| 195 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 196 | QMessageBox messageBox(this); |
| 197 | messageBox.setWindowTitle(title); |
| 198 | messageBox.setText(text); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 199 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 200 | messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 201 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 202 | messageBox.exec(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext) |
| 206 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 207 | QMessageBox messageBox(this); |
| 208 | messageBox.setWindowTitle(title); |
| 209 | messageBox.setText(text); |
| 210 | messageBox.setInformativeText(infotext); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 211 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 212 | messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 213 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 214 | messageBox.exec(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 217 | void ChronoShareGui::createActionsAndMenu() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 218 | { |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 219 | _LOG_DEBUG ("Create actions"); |
| 220 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 221 | // create the "open folder" action |
| 222 | m_openFolder = new QAction(tr("&Open Folder"), this); |
| 223 | connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 224 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 225 | m_openWeb = new QAction(tr("Open in &Browser"), this); |
| 226 | connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser())); |
| 227 | |
| 228 | m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this); |
| 229 | for (int i = 0; i < 5; i++) |
| 230 | { |
| 231 | m_fileActions[i] = new QAction(this); |
| 232 | m_fileActions[i]->setVisible(false); |
| 233 | connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile())); |
| 234 | m_recentFilesMenu->addAction(m_fileActions[i]); |
| 235 | } |
| 236 | connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu())); |
| 237 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 238 | // create the "view settings" action |
| 239 | m_viewSettings = new QAction(tr("&View Settings"), this); |
| 240 | connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings())); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 241 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 242 | // create the "change folder" action |
| 243 | m_changeFolder = new QAction(tr("&Change Folder"), this); |
| 244 | connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog())); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 245 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 246 | #ifdef ADHOC_SUPPORTED |
| 247 | // create "AdHoc Wifi" action |
| 248 | m_wifiAction = new QAction (tr("Enable AdHoc &WiFI"), m_trayIconMenu); |
| 249 | m_wifiAction->setChecked (false); |
| 250 | m_wifiAction->setCheckable (true); |
| 251 | connect (m_wifiAction, SIGNAL (toggled(bool)), this, SLOT(onAdHocChange(bool))); |
| 252 | #endif |
| 253 | |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 254 | #ifdef SPARKLE_SUPPORTED |
Zhenkai Zhu | bb170d4 | 2013-02-25 13:48:59 -0800 | [diff] [blame] | 255 | m_checkForUpdates = new QAction (tr("Check For Updates"), this); |
| 256 | connect (m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates())); |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 257 | #endif |
| 258 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 259 | // create the "quit program" action |
| 260 | m_quitProgram = new QAction(tr("&Quit"), this); |
| 261 | connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void ChronoShareGui::createTrayIcon() |
| 265 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 266 | // create a new icon menu |
| 267 | m_trayIconMenu = new QMenu(this); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 268 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 269 | // add actions to the menu |
| 270 | m_trayIconMenu->addAction(m_openFolder); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 271 | m_trayIconMenu->addAction(m_openWeb); |
| 272 | m_trayIconMenu->addMenu(m_recentFilesMenu); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 273 | m_trayIconMenu->addSeparator(); |
| 274 | m_trayIconMenu->addAction(m_viewSettings); |
| 275 | m_trayIconMenu->addAction(m_changeFolder); |
| 276 | m_trayIconMenu->addSeparator(); |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 277 | m_trayIconMenu->addAction(m_checkForUpdates); |
| 278 | m_trayIconMenu->addSeparator(); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 279 | m_trayIconMenu->addAction(m_wifiAction); |
| 280 | m_trayIconMenu->addSeparator(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 281 | m_trayIconMenu->addAction(m_quitProgram); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 282 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 283 | // create new tray icon |
| 284 | m_trayIcon = new QSystemTrayIcon(this); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 285 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 286 | #ifdef ADHOC_SUPPORTED |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 287 | // associate the menu with the tray icon |
| 288 | m_trayIcon->setContextMenu(m_trayIconMenu); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 289 | #endif |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 290 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 291 | // handle left click of icon |
| 292 | connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 295 | void |
| 296 | ChronoShareGui::onAdHocChange (bool state) |
| 297 | { |
| 298 | #ifdef ADHOC_SUPPORTED |
| 299 | if (m_wifiAction->isChecked ()) |
| 300 | { |
| 301 | QMessageBox msgBox; |
| 302 | msgBox.setText ("WARNING: your WiFi will be disconnected"); |
| 303 | msgBox.setIcon (QMessageBox::Warning); |
| 304 | msgBox.setInformativeText("AdHoc WiFi will disconnect your current WiFi.\n Are you sure that you are OK with that?"); |
| 305 | msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); |
| 306 | msgBox.setDefaultButton(QMessageBox::Cancel); |
| 307 | int ret = msgBox.exec(); |
| 308 | |
| 309 | if (ret == QMessageBox::Cancel) |
| 310 | { |
| 311 | m_wifiAction->setChecked (false); |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | |
| 316 | m_wifiAction->setText (tr("Disable AdHoc WiFI")); |
| 317 | |
| 318 | // create adhoc |
| 319 | m_executor.execute (Adhoc::CreateAdhoc); |
| 320 | } |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | m_wifiAction->setText (tr("Enable AdHoc WiFI")); |
| 325 | |
| 326 | // disable adhoc |
| 327 | m_executor.execute (Adhoc::DestroyAdhoc); |
| 328 | |
| 329 | // a trick in DestroyAdhoc ensures that WiFi will be reconnected to a default WiFi |
| 330 | } |
| 331 | #endif |
| 332 | } |
| 333 | |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 334 | void |
| 335 | ChronoShareGui::onCheckForUpdates() |
| 336 | { |
Zhenkai Zhu | 249fbc7 | 2013-02-27 21:50:25 -0800 | [diff] [blame] | 337 | #ifdef SPARKLE_SUPPORTED |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 338 | cout << "+++++++++++ trying to update +++++++ " << endl; |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 339 | m_autoUpdate->checkForUpdates(); |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 340 | cout << "+++++++++++ end trying to update +++++++ " << endl; |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 341 | #endif |
Zhenkai Zhu | 249fbc7 | 2013-02-27 21:50:25 -0800 | [diff] [blame] | 342 | } |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 343 | |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 344 | void ChronoShareGui::setIcon() |
| 345 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 346 | // set the icon image |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 347 | m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void ChronoShareGui::openSharedFolder() |
| 351 | { |
Zhenkai Zhu | ce04cb4 | 2013-02-25 15:29:31 -0800 | [diff] [blame] | 352 | QString path = QDir::toNativeSeparators(m_dirPath); |
| 353 | QDesktopServices::openUrl(QUrl("file:///" + path)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 354 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 355 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 356 | void ChronoShareGui::openInWebBrowser() |
| 357 | { |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame^] | 358 | QUrl url ("http://localhost:9001/"); |
| 359 | url.setFragment ("fileList&" |
| 360 | "user=" + QUrl::toPercentEncoding (m_username) + "&" |
| 361 | "folder=" + QUrl::toPercentEncoding (m_sharedFolderName)); |
| 362 | |
| 363 | // i give up. there is double encoding and I have no idea how to fight it... |
| 364 | QDesktopServices::openUrl (url); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void ChronoShareGui::openFile() |
| 368 | { |
| 369 | // figure out who sent the signal |
| 370 | QAction *pAction = qobject_cast<QAction*>(sender()); |
| 371 | if (pAction->isEnabled()) |
| 372 | { |
| 373 | // we stored full path of the file in this toolTip field |
| 374 | #ifdef Q_WS_MAC |
| 375 | // we do some hack so we could show the file in Finder highlighted |
| 376 | QStringList args; |
| 377 | args << "-e"; |
| 378 | args << "tell application \"Finder\""; |
| 379 | args << "-e"; |
| 380 | args << "activate"; |
| 381 | args << "-e"; |
| 382 | args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\""; |
| 383 | args << "-e"; |
| 384 | args << "end tell"; |
| 385 | QProcess::startDetached("osascript", args); |
| 386 | #else |
| 387 | // too bad qt couldn't do highlighting for Linux (or Mac) |
| 388 | QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip())); |
| 389 | #endif |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | void ChronoShareGui::updateRecentFilesMenu() |
| 394 | { |
| 395 | for (int i = 0; i < 5; i++) |
| 396 | { |
| 397 | m_fileActions[i]->setVisible(false); |
| 398 | } |
| 399 | m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5); |
| 400 | } |
| 401 | |
| 402 | void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index) |
| 403 | { |
| 404 | QString fullPath = m_dirPath + "/" + filename.c_str(); |
| 405 | QFileInfo fileInfo(fullPath); |
| 406 | if (fileInfo.exists()) |
| 407 | { |
| 408 | // This is a hack, we just use some field to store the path |
| 409 | m_fileActions[index]->setToolTip(fileInfo.absolutePath()); |
| 410 | m_fileActions[index]->setEnabled(true); |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | // after half an hour frustrating test and search around, |
| 415 | // I think it's the problem of Qt. |
| 416 | // According to the Qt doc, the action cannot be clicked |
| 417 | // and the area would be grey, but it didn't happen |
| 418 | // User can still trigger the action, and not greyed |
| 419 | // added check in SLOT to see if the action is "enalbed" |
| 420 | // as a remedy |
| 421 | // Give up at least for now |
| 422 | m_fileActions[index]->setEnabled(false); |
| 423 | // UPDATE, file not fetched yet |
| 424 | if (action == 0) |
| 425 | { |
| 426 | QFont font; |
| 427 | // supposed by change the font, didn't happen |
| 428 | font.setWeight(QFont::Light); |
| 429 | m_fileActions[index]->setFont(font); |
| 430 | m_fileActions[index]->setToolTip(tr("Fetching...")); |
| 431 | } |
| 432 | // DELETE |
| 433 | else |
| 434 | { |
| 435 | QFont font; |
| 436 | // supposed by change the font, didn't happen |
| 437 | font.setStrikeOut(true); |
| 438 | m_fileActions[index]->setFont(font); |
| 439 | m_fileActions[index]->setToolTip(tr("Deleted...")); |
| 440 | } |
| 441 | } |
| 442 | m_fileActions[index]->setText(fileInfo.fileName()); |
| 443 | m_fileActions[index]->setVisible(true); |
| 444 | } |
| 445 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 446 | void ChronoShareGui::changeSettings() |
| 447 | { |
| 448 | if(!editUsername->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 449 | m_username = editUsername->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 450 | else |
| 451 | editUsername->setText(m_username); |
| 452 | |
| 453 | if(!editSharedFolder->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 454 | m_sharedFolderName = editSharedFolder->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 455 | else |
| 456 | editSharedFolder->setText(m_sharedFolderName); |
| 457 | |
| 458 | saveSettings(); |
| 459 | this->hide(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 460 | |
| 461 | startBackend(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 464 | void ChronoShareGui::openFileDialog() |
| 465 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 466 | // prompt user for new directory |
| 467 | QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"), |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 468 | m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 469 | QFileInfo qFileInfo (tempPath); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 470 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 471 | if(qFileInfo.isDir()) |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 472 | { |
| 473 | m_dirPath = tempPath; |
| 474 | editSharedFolderPath->setText(m_dirPath); |
| 475 | } |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 476 | else |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 477 | { |
| 478 | openMessageBox ("Error", "Not a valid folder, Ignoring."); |
| 479 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 480 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 481 | _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ()); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 482 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 483 | // save settings |
| 484 | saveSettings(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 487 | void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 488 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 489 | // if double clicked, open shared folder |
| 490 | if(reason == QSystemTrayIcon::DoubleClick) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 491 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 492 | openSharedFolder(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 496 | void ChronoShareGui::viewSettings() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 497 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 498 | //simple for now |
| 499 | this->show(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 500 | this->raise(); |
| 501 | this->activateWindow(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | bool ChronoShareGui::loadSettings() |
| 505 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 506 | bool successful = true; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 507 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 508 | // Load Settings |
| 509 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 510 | QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare"); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 511 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 512 | // _LOG_DEBUG (lexical_cast<string> (settings.allKeys())); |
| 513 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 514 | if(settings.contains("username")) |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 515 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 516 | m_username = settings.value("username", "admin").toString(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 517 | } |
| 518 | else |
| 519 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 520 | successful = false; |
| 521 | } |
| 522 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 523 | editUsername->setText(m_username); |
| 524 | |
| 525 | if(settings.contains("sharedfoldername")) |
| 526 | { |
| 527 | m_sharedFolderName = settings.value("sharedfoldername", "shared").toString(); |
| 528 | } |
| 529 | else |
| 530 | { |
| 531 | successful = false; |
| 532 | } |
| 533 | |
| 534 | editSharedFolder->setText(m_sharedFolderName); |
| 535 | |
| 536 | if(settings.contains("dirPath")) |
| 537 | { |
| 538 | m_dirPath = settings.value("dirPath", QDir::homePath()).toString(); |
| 539 | } |
| 540 | else |
| 541 | { |
| 542 | successful = false; |
| 543 | } |
| 544 | |
| 545 | editSharedFolderPath->setText(m_dirPath); |
| 546 | |
| 547 | _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no"))); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 548 | |
| 549 | return successful; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | void ChronoShareGui::saveSettings() |
| 553 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 554 | // Save Settings |
| 555 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 556 | QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare"); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 557 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 558 | settings.setValue("dirPath", m_dirPath); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 559 | settings.setValue("username", m_username); |
| 560 | settings.setValue("sharedfoldername", m_sharedFolderName); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | void ChronoShareGui::closeEvent(QCloseEvent* event) |
| 564 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 565 | _LOG_DEBUG ("Close Event") |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 566 | this->hide(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 567 | event->ignore(); // don't let the event propagate to the base class |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 568 | } |
Jared Lindblom | dc845f0 | 2013-01-18 17:29:40 -0800 | [diff] [blame] | 569 | |
| 570 | #if WAF |
| 571 | #include "chronosharegui.moc" |
| 572 | #include "chronosharegui.cpp.moc" |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 573 | #endif |