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 | { |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 154 | try |
| 155 | { |
| 156 | m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT); |
| 157 | m_httpServerThread = boost::thread(&http::server::server::run, m_httpServer); |
| 158 | } |
| 159 | catch (std::exception &e) |
| 160 | { |
| 161 | _LOG_ERROR ("Start http server failed"); |
| 162 | m_httpServer = 0; // just to make sure |
| 163 | QMessageBox msgBox; |
| 164 | msgBox.setText ("WARNING: Cannot start http server!"); |
| 165 | msgBox.setIcon (QMessageBox::Warning); |
| 166 | msgBox.setInformativeText(QString("Starting http server failed. You will not be able to check history from web brower. Exception caused: %1").arg(e.what())); |
| 167 | msgBox.setStandardButtons(QMessageBox::Ok); |
| 168 | msgBox.exec(); |
| 169 | } |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 170 | } |
| 171 | else |
| 172 | { |
| 173 | _LOG_ERROR ("Http server doc root dir does not exist!"); |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 174 | } |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | ChronoShareGui::~ChronoShareGui() |
| 178 | { |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 179 | #ifdef ADHOC_SUPPORTED |
| 180 | m_executor.shutdown (); |
| 181 | #endif |
| 182 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 183 | delete m_watcher; // stop filewatching ASAP |
Alexander Afanasyev | 9ca444e | 2013-01-25 16:29:35 -0800 | [diff] [blame] | 184 | delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object) |
Zhenkai Zhu | 8da3365 | 2013-03-04 10:52:03 -0800 | [diff] [blame] | 185 | if (m_httpServer != 0) |
| 186 | { |
| 187 | m_httpServer->handle_stop(); |
| 188 | m_httpServerThread.join(); |
| 189 | delete m_httpServer; |
| 190 | } |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 191 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 192 | // cleanup |
| 193 | delete m_trayIcon; |
| 194 | delete m_trayIconMenu; |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 195 | #ifdef ADHOC_SUPPORTED |
| 196 | delete m_wifiAction; |
| 197 | #endif |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 198 | #ifdef SPARKLE_SUPPORTED |
| 199 | delete m_autoUpdate; |
Zhenkai Zhu | bb170d4 | 2013-02-25 13:48:59 -0800 | [diff] [blame] | 200 | delete m_checkForUpdates; |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 201 | #endif |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 202 | delete m_openFolder; |
| 203 | delete m_viewSettings; |
| 204 | delete m_changeFolder; |
| 205 | delete m_quitProgram; |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 206 | |
| 207 | delete labelUsername; |
| 208 | delete labelSharedFolder; |
| 209 | delete editUsername; |
| 210 | delete editSharedFolder; |
| 211 | delete button; |
| 212 | delete label; |
| 213 | delete mainLayout; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void ChronoShareGui::openMessageBox(QString title, QString text) |
| 217 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 218 | QMessageBox messageBox(this); |
| 219 | messageBox.setWindowTitle(title); |
| 220 | messageBox.setText(text); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 221 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 222 | messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 223 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 224 | messageBox.exec(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext) |
| 228 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 229 | QMessageBox messageBox(this); |
| 230 | messageBox.setWindowTitle(title); |
| 231 | messageBox.setText(text); |
| 232 | messageBox.setInformativeText(infotext); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 233 | |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 234 | messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 235 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 236 | messageBox.exec(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 239 | void ChronoShareGui::createActionsAndMenu() |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 240 | { |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 241 | _LOG_DEBUG ("Create actions"); |
| 242 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 243 | // create the "open folder" action |
| 244 | m_openFolder = new QAction(tr("&Open Folder"), this); |
| 245 | connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 246 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 247 | m_openWeb = new QAction(tr("Open in &Browser"), this); |
| 248 | connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser())); |
| 249 | |
| 250 | m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this); |
| 251 | for (int i = 0; i < 5; i++) |
| 252 | { |
| 253 | m_fileActions[i] = new QAction(this); |
| 254 | m_fileActions[i]->setVisible(false); |
| 255 | connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile())); |
| 256 | m_recentFilesMenu->addAction(m_fileActions[i]); |
| 257 | } |
| 258 | connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu())); |
| 259 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 260 | // create the "view settings" action |
| 261 | m_viewSettings = new QAction(tr("&View Settings"), this); |
| 262 | connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings())); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 263 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 264 | // create the "change folder" action |
| 265 | m_changeFolder = new QAction(tr("&Change Folder"), this); |
| 266 | connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog())); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 267 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 268 | #ifdef ADHOC_SUPPORTED |
| 269 | // create "AdHoc Wifi" action |
| 270 | m_wifiAction = new QAction (tr("Enable AdHoc &WiFI"), m_trayIconMenu); |
| 271 | m_wifiAction->setChecked (false); |
| 272 | m_wifiAction->setCheckable (true); |
| 273 | connect (m_wifiAction, SIGNAL (toggled(bool)), this, SLOT(onAdHocChange(bool))); |
| 274 | #endif |
| 275 | |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 276 | #ifdef SPARKLE_SUPPORTED |
Zhenkai Zhu | bb170d4 | 2013-02-25 13:48:59 -0800 | [diff] [blame] | 277 | m_checkForUpdates = new QAction (tr("Check For Updates"), this); |
| 278 | connect (m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates())); |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 279 | #endif |
| 280 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 281 | // create the "quit program" action |
| 282 | m_quitProgram = new QAction(tr("&Quit"), this); |
| 283 | connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit())); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void ChronoShareGui::createTrayIcon() |
| 287 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 288 | // create a new icon menu |
| 289 | m_trayIconMenu = new QMenu(this); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 290 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 291 | // add actions to the menu |
| 292 | m_trayIconMenu->addAction(m_openFolder); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 293 | m_trayIconMenu->addAction(m_openWeb); |
| 294 | m_trayIconMenu->addMenu(m_recentFilesMenu); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 295 | m_trayIconMenu->addSeparator(); |
| 296 | m_trayIconMenu->addAction(m_viewSettings); |
| 297 | m_trayIconMenu->addAction(m_changeFolder); |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 298 | |
| 299 | #ifdef SPARKLE_SUPPORTED |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 300 | m_trayIconMenu->addSeparator(); |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 301 | m_trayIconMenu->addAction(m_checkForUpdates); |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 302 | #endif |
| 303 | |
| 304 | #ifdef ADHOC_SUPPORTED |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 305 | m_trayIconMenu->addSeparator(); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 306 | m_trayIconMenu->addAction(m_wifiAction); |
Alexander Afanasyev | e8496a3 | 2013-03-03 16:10:43 -0800 | [diff] [blame] | 307 | #endif |
| 308 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 309 | m_trayIconMenu->addSeparator(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 310 | m_trayIconMenu->addAction(m_quitProgram); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 311 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 312 | // create new tray icon |
| 313 | m_trayIcon = new QSystemTrayIcon(this); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 314 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 315 | #ifdef ADHOC_SUPPORTED |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 316 | // associate the menu with the tray icon |
| 317 | m_trayIcon->setContextMenu(m_trayIconMenu); |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 318 | #endif |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 319 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 320 | // handle left click of icon |
| 321 | connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Alexander Afanasyev | 85a4ba0 | 2013-02-24 16:30:17 -0800 | [diff] [blame] | 324 | void |
| 325 | ChronoShareGui::onAdHocChange (bool state) |
| 326 | { |
| 327 | #ifdef ADHOC_SUPPORTED |
| 328 | if (m_wifiAction->isChecked ()) |
| 329 | { |
| 330 | QMessageBox msgBox; |
| 331 | msgBox.setText ("WARNING: your WiFi will be disconnected"); |
| 332 | msgBox.setIcon (QMessageBox::Warning); |
| 333 | msgBox.setInformativeText("AdHoc WiFi will disconnect your current WiFi.\n Are you sure that you are OK with that?"); |
| 334 | msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); |
| 335 | msgBox.setDefaultButton(QMessageBox::Cancel); |
| 336 | int ret = msgBox.exec(); |
| 337 | |
| 338 | if (ret == QMessageBox::Cancel) |
| 339 | { |
| 340 | m_wifiAction->setChecked (false); |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | |
| 345 | m_wifiAction->setText (tr("Disable AdHoc WiFI")); |
| 346 | |
| 347 | // create adhoc |
| 348 | m_executor.execute (Adhoc::CreateAdhoc); |
| 349 | } |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | m_wifiAction->setText (tr("Enable AdHoc WiFI")); |
| 354 | |
| 355 | // disable adhoc |
| 356 | m_executor.execute (Adhoc::DestroyAdhoc); |
| 357 | |
| 358 | // a trick in DestroyAdhoc ensures that WiFi will be reconnected to a default WiFi |
| 359 | } |
| 360 | #endif |
| 361 | } |
| 362 | |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 363 | void |
| 364 | ChronoShareGui::onCheckForUpdates() |
| 365 | { |
Zhenkai Zhu | 249fbc7 | 2013-02-27 21:50:25 -0800 | [diff] [blame] | 366 | #ifdef SPARKLE_SUPPORTED |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 367 | cout << "+++++++++++ trying to update +++++++ " << endl; |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 368 | m_autoUpdate->checkForUpdates(); |
Zhenkai Zhu | 3857d71 | 2013-02-27 22:51:25 -0800 | [diff] [blame] | 369 | cout << "+++++++++++ end trying to update +++++++ " << endl; |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 370 | #endif |
Zhenkai Zhu | 249fbc7 | 2013-02-27 21:50:25 -0800 | [diff] [blame] | 371 | } |
Zhenkai Zhu | aca6cb5 | 2013-02-24 23:34:03 -0800 | [diff] [blame] | 372 | |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 373 | void ChronoShareGui::setIcon() |
| 374 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 375 | // set the icon image |
Zhenkai Zhu | d942922 | 2013-02-25 22:34:09 -0800 | [diff] [blame] | 376 | m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void ChronoShareGui::openSharedFolder() |
| 380 | { |
Zhenkai Zhu | ce04cb4 | 2013-02-25 15:29:31 -0800 | [diff] [blame] | 381 | QString path = QDir::toNativeSeparators(m_dirPath); |
| 382 | QDesktopServices::openUrl(QUrl("file:///" + path)); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 383 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 384 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 385 | void ChronoShareGui::openInWebBrowser() |
| 386 | { |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 387 | QUrl url ("http://localhost:9001/"); |
| 388 | url.setFragment ("fileList&" |
| 389 | "user=" + QUrl::toPercentEncoding (m_username) + "&" |
| 390 | "folder=" + QUrl::toPercentEncoding (m_sharedFolderName)); |
| 391 | |
| 392 | // i give up. there is double encoding and I have no idea how to fight it... |
| 393 | QDesktopServices::openUrl (url); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void ChronoShareGui::openFile() |
| 397 | { |
| 398 | // figure out who sent the signal |
| 399 | QAction *pAction = qobject_cast<QAction*>(sender()); |
| 400 | if (pAction->isEnabled()) |
| 401 | { |
| 402 | // we stored full path of the file in this toolTip field |
| 403 | #ifdef Q_WS_MAC |
| 404 | // we do some hack so we could show the file in Finder highlighted |
| 405 | QStringList args; |
| 406 | args << "-e"; |
| 407 | args << "tell application \"Finder\""; |
| 408 | args << "-e"; |
| 409 | args << "activate"; |
| 410 | args << "-e"; |
| 411 | args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\""; |
| 412 | args << "-e"; |
| 413 | args << "end tell"; |
| 414 | QProcess::startDetached("osascript", args); |
| 415 | #else |
| 416 | // too bad qt couldn't do highlighting for Linux (or Mac) |
| 417 | QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip())); |
| 418 | #endif |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void ChronoShareGui::updateRecentFilesMenu() |
| 423 | { |
| 424 | for (int i = 0; i < 5; i++) |
| 425 | { |
| 426 | m_fileActions[i]->setVisible(false); |
| 427 | } |
| 428 | m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5); |
| 429 | } |
| 430 | |
| 431 | void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index) |
| 432 | { |
| 433 | QString fullPath = m_dirPath + "/" + filename.c_str(); |
| 434 | QFileInfo fileInfo(fullPath); |
| 435 | if (fileInfo.exists()) |
| 436 | { |
| 437 | // This is a hack, we just use some field to store the path |
| 438 | m_fileActions[index]->setToolTip(fileInfo.absolutePath()); |
| 439 | m_fileActions[index]->setEnabled(true); |
| 440 | } |
| 441 | else |
| 442 | { |
| 443 | // after half an hour frustrating test and search around, |
| 444 | // I think it's the problem of Qt. |
| 445 | // According to the Qt doc, the action cannot be clicked |
| 446 | // and the area would be grey, but it didn't happen |
| 447 | // User can still trigger the action, and not greyed |
| 448 | // added check in SLOT to see if the action is "enalbed" |
| 449 | // as a remedy |
| 450 | // Give up at least for now |
| 451 | m_fileActions[index]->setEnabled(false); |
| 452 | // UPDATE, file not fetched yet |
| 453 | if (action == 0) |
| 454 | { |
| 455 | QFont font; |
| 456 | // supposed by change the font, didn't happen |
| 457 | font.setWeight(QFont::Light); |
| 458 | m_fileActions[index]->setFont(font); |
| 459 | m_fileActions[index]->setToolTip(tr("Fetching...")); |
| 460 | } |
| 461 | // DELETE |
| 462 | else |
| 463 | { |
| 464 | QFont font; |
| 465 | // supposed by change the font, didn't happen |
| 466 | font.setStrikeOut(true); |
| 467 | m_fileActions[index]->setFont(font); |
| 468 | m_fileActions[index]->setToolTip(tr("Deleted...")); |
| 469 | } |
| 470 | } |
| 471 | m_fileActions[index]->setText(fileInfo.fileName()); |
| 472 | m_fileActions[index]->setVisible(true); |
| 473 | } |
| 474 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 475 | void ChronoShareGui::changeSettings() |
| 476 | { |
| 477 | if(!editUsername->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 478 | m_username = editUsername->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 479 | else |
| 480 | editUsername->setText(m_username); |
| 481 | |
| 482 | if(!editSharedFolder->text().isEmpty()) |
Zhenkai Zhu | f3ea97a | 2013-01-30 15:20:52 -0800 | [diff] [blame] | 483 | m_sharedFolderName = editSharedFolder->text().trimmed(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 484 | else |
| 485 | editSharedFolder->setText(m_sharedFolderName); |
| 486 | |
| 487 | saveSettings(); |
| 488 | this->hide(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 489 | |
| 490 | startBackend(); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 493 | void ChronoShareGui::openFileDialog() |
| 494 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 495 | // prompt user for new directory |
| 496 | QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"), |
Alexander Afanasyev | 9e5a470 | 2013-01-24 13:15:23 -0800 | [diff] [blame] | 497 | m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
| 498 | QFileInfo qFileInfo (tempPath); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 499 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 500 | if(qFileInfo.isDir()) |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 501 | { |
| 502 | m_dirPath = tempPath; |
| 503 | editSharedFolderPath->setText(m_dirPath); |
| 504 | } |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 505 | else |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 506 | { |
| 507 | openMessageBox ("Error", "Not a valid folder, Ignoring."); |
| 508 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 509 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 510 | _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ()); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 511 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 512 | // save settings |
| 513 | saveSettings(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 516 | void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 517 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 518 | // if double clicked, open shared folder |
| 519 | if(reason == QSystemTrayIcon::DoubleClick) |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 520 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 521 | openSharedFolder(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 525 | void ChronoShareGui::viewSettings() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 526 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 527 | //simple for now |
| 528 | this->show(); |
Zhenkai Zhu | d575631 | 2013-01-31 13:49:45 -0800 | [diff] [blame] | 529 | this->raise(); |
| 530 | this->activateWindow(); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | bool ChronoShareGui::loadSettings() |
| 534 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 535 | bool successful = true; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 536 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 537 | // Load Settings |
| 538 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 539 | QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare"); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame] | 540 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 541 | // _LOG_DEBUG (lexical_cast<string> (settings.allKeys())); |
| 542 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 543 | if(settings.contains("username")) |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 544 | { |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 545 | m_username = settings.value("username", "admin").toString(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 546 | } |
| 547 | else |
| 548 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 549 | successful = false; |
| 550 | } |
| 551 | |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 552 | editUsername->setText(m_username); |
| 553 | |
| 554 | if(settings.contains("sharedfoldername")) |
| 555 | { |
| 556 | m_sharedFolderName = settings.value("sharedfoldername", "shared").toString(); |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | successful = false; |
| 561 | } |
| 562 | |
| 563 | editSharedFolder->setText(m_sharedFolderName); |
| 564 | |
| 565 | if(settings.contains("dirPath")) |
| 566 | { |
| 567 | m_dirPath = settings.value("dirPath", QDir::homePath()).toString(); |
| 568 | } |
| 569 | else |
| 570 | { |
| 571 | successful = false; |
| 572 | } |
| 573 | |
| 574 | editSharedFolderPath->setText(m_dirPath); |
| 575 | |
| 576 | _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no"))); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 577 | |
| 578 | return successful; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | void ChronoShareGui::saveSettings() |
| 582 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 583 | // Save Settings |
| 584 | // QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 585 | QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare"); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 586 | |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 587 | settings.setValue("dirPath", m_dirPath); |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 588 | settings.setValue("username", m_username); |
| 589 | settings.setValue("sharedfoldername", m_sharedFolderName); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void ChronoShareGui::closeEvent(QCloseEvent* event) |
| 593 | { |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 594 | _LOG_DEBUG ("Close Event") |
Jared Lindblom | c8ffd59 | 2013-01-25 00:00:30 -0800 | [diff] [blame] | 595 | this->hide(); |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 596 | event->ignore(); // don't let the event propagate to the base class |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 597 | } |
Jared Lindblom | dc845f0 | 2013-01-18 17:29:40 -0800 | [diff] [blame] | 598 | |
| 599 | #if WAF |
| 600 | #include "chronosharegui.moc" |
| 601 | #include "chronosharegui.cpp.moc" |
Alexander Afanasyev | 83a5300 | 2013-01-24 11:12:01 -0800 | [diff] [blame] | 602 | #endif |