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