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> |
| 19 | */ |
| 20 | |
| 21 | #include "chronosharegui.h" |
| 22 | #include "ui_chronosharegui.h" |
| 23 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 24 | ChronoShareGui::ChronoShareGui(QWidget *parent) : |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 25 | QWidget(parent), |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 26 | m_settingsFilePath(QDir::homePath() + ".cronoShare") |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 27 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 28 | // load settings |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 29 | if(!loadSettings()) |
| 30 | { |
| 31 | // prompt user to choose folder |
| 32 | openMessageBox("First Time Setup", "Please select your shared folder location."); |
| 33 | openFileDialog(); |
| 34 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 35 | |
| 36 | // create actions that result from clicking a menu option |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 37 | createActions(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 38 | |
| 39 | // create tray icon |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 40 | createTrayIcon(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 41 | |
| 42 | // set icon image |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 43 | setIcon(); |
| 44 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 45 | // show tray icon |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 46 | m_trayIcon->show(); |
| 47 | } |
| 48 | |
| 49 | ChronoShareGui::~ChronoShareGui() |
| 50 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 51 | // cleanup |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 52 | delete m_trayIcon; |
| 53 | delete m_trayIconMenu; |
| 54 | delete m_openFolder; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 55 | delete m_viewSettings; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 56 | delete m_changeFolder; |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 57 | delete m_quitProgram; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 58 | } |
| 59 | |
| 60 | void ChronoShareGui::openMessageBox(QString title, QString text) |
| 61 | { |
| 62 | QMessageBox messageBox(this); |
| 63 | messageBox.setWindowTitle(title); |
| 64 | messageBox.setText(text); |
| 65 | |
| 66 | messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png")); |
| 67 | |
| 68 | messageBox.exec(); |
| 69 | } |
| 70 | |
| 71 | void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext) |
| 72 | { |
| 73 | QMessageBox messageBox(this); |
| 74 | messageBox.setWindowTitle(title); |
| 75 | messageBox.setText(text); |
| 76 | messageBox.setInformativeText(infotext); |
| 77 | |
| 78 | messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png")); |
| 79 | |
| 80 | messageBox.exec(); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void ChronoShareGui::createActions() |
| 84 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 85 | // create the "open folder" action |
| 86 | m_openFolder = new QAction(tr("&Open Folder"), this); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 87 | connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder())); |
| 88 | |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 89 | // create the "view settings" action |
| 90 | m_viewSettings = new QAction(tr("&View Settings"), this); |
| 91 | connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings())); |
| 92 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 93 | // create the "change folder" action |
| 94 | m_changeFolder = new QAction(tr("&Change Folder"), this); |
| 95 | connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog())); |
| 96 | |
| 97 | // create the "quit program" action |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 98 | m_quitProgram = new QAction(tr("&Quit"), this); |
| 99 | connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit())); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 100 | |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void ChronoShareGui::createTrayIcon() |
| 104 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 105 | // create a new icon menu |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 106 | m_trayIconMenu = new QMenu(this); |
| 107 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 108 | // add actions to the menu |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 109 | m_trayIconMenu->addAction(m_openFolder); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 110 | m_trayIconMenu->addSeparator(); |
| 111 | m_trayIconMenu->addAction(m_viewSettings); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 112 | m_trayIconMenu->addAction(m_changeFolder); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 113 | m_trayIconMenu->addSeparator(); |
| 114 | m_trayIconMenu->addAction(m_quitProgram); |
| 115 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 116 | // create new tray icon |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 117 | m_trayIcon = new QSystemTrayIcon(this); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 118 | |
| 119 | // associate the menu with the tray icon |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 120 | m_trayIcon->setContextMenu(m_trayIconMenu); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 121 | |
| 122 | // handle left click of icon |
| 123 | connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void ChronoShareGui::setIcon() |
| 127 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 128 | // set the icon image |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 129 | m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png")); |
| 130 | } |
| 131 | |
| 132 | void ChronoShareGui::openSharedFolder() |
| 133 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 134 | // tell Finder to open the shared folder |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 135 | QStringList scriptArgs; |
| 136 | scriptArgs << QLatin1String("-e") |
| 137 | << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"") |
| 138 | .arg(m_dirPath); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 139 | |
| 140 | // execute the commands to make it happen |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 141 | QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 142 | |
| 143 | // clear command arguments |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 144 | scriptArgs.clear(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 145 | |
| 146 | // tell Finder to appear |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 147 | scriptArgs << QLatin1String("-e") |
| 148 | << QLatin1String("tell application \"Finder\" to activate"); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 149 | |
| 150 | // execute the commands to make it happen |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 151 | QProcess::execute("/usr/bin/osascript", scriptArgs); |
| 152 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 153 | |
| 154 | void ChronoShareGui::openFileDialog() |
| 155 | { |
| 156 | // prompt user for new directory |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 157 | QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"), |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 158 | m_dirPath, QFileDialog::ShowDirsOnly | |
| 159 | QFileDialog::DontResolveSymlinks); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 160 | QFileInfo qFileInfo(tempPath); |
| 161 | |
| 162 | if(qFileInfo.isDir()) |
| 163 | m_dirPath = tempPath; |
| 164 | else |
| 165 | openMessageBox("Error", "Not a valid folder, Ignoring."); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 166 | |
| 167 | qDebug() << m_dirPath; |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 168 | openMessageBox("Current Folder", "Current Shared Folder:\n" + m_dirPath, "You may change the folder by selecting \"change folder\" from the icon in the system tray."); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 169 | |
| 170 | // save settings |
| 171 | saveSettings(); |
| 172 | } |
| 173 | |
| 174 | void ChronoShareGui::trayIconClicked(QSystemTrayIcon::ActivationReason reason) |
| 175 | { |
| 176 | // if double clicked, open shared folder |
| 177 | if(reason == QSystemTrayIcon::DoubleClick) |
| 178 | { |
| 179 | openSharedFolder(); |
| 180 | } |
| 181 | } |
| 182 | |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 183 | void ChronoShareGui::viewSettings() |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 184 | { |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 185 | // simple for now |
| 186 | openMessageBox("Chronoshare Settings", "CurrentFolder:\n" + m_dirPath); |
| 187 | } |
| 188 | |
| 189 | bool ChronoShareGui::loadSettings() |
| 190 | { |
| 191 | bool successful = false; |
| 192 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 193 | // Load Settings |
| 194 | QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
Jared Lindblom | 5d7e51e | 2013-01-18 00:59:34 -0800 | [diff] [blame^] | 195 | if(settings.contains("dirPath")) |
| 196 | { |
| 197 | m_dirPath = settings.value("dirPath", QDir::homePath()).toString(); |
| 198 | successful = true; |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | m_dirPath = QDir::homePath(); |
| 203 | successful = false; |
| 204 | } |
| 205 | |
| 206 | return successful; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void ChronoShareGui::saveSettings() |
| 210 | { |
| 211 | // Save Settings |
| 212 | QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 213 | settings.setValue("dirPath", m_dirPath); |
| 214 | } |
| 215 | |
| 216 | void ChronoShareGui::closeEvent(QCloseEvent* event) |
| 217 | { |
| 218 | qDebug() << "Close Event."; |
| 219 | event->ignore(); // don't let the event propagate to the base class |
| 220 | } |