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 | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 26 | m_fileDialogWidget(new QWidget()) |
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 |
| 29 | loadSettings(); |
| 30 | |
| 31 | // create actions that result from clicking a menu option |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 32 | createActions(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 33 | |
| 34 | // create tray icon |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 35 | createTrayIcon(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 36 | |
| 37 | // set icon image |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 38 | setIcon(); |
| 39 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 40 | // show tray icon |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 41 | m_trayIcon->show(); |
| 42 | } |
| 43 | |
| 44 | ChronoShareGui::~ChronoShareGui() |
| 45 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 46 | // cleanup |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 47 | delete m_trayIcon; |
| 48 | delete m_trayIconMenu; |
| 49 | delete m_openFolder; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 50 | delete m_changeFolder; |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 51 | delete m_quitProgram; |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 52 | delete m_fileDialogWidget; |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void ChronoShareGui::createActions() |
| 56 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 57 | // create the "open folder" action |
| 58 | m_openFolder = new QAction(tr("&Open Folder"), this); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 59 | connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder())); |
| 60 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 61 | // create the "change folder" action |
| 62 | m_changeFolder = new QAction(tr("&Change Folder"), this); |
| 63 | connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog())); |
| 64 | |
| 65 | // create the "quit program" action |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 66 | m_quitProgram = new QAction(tr("&Quit"), this); |
| 67 | connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit())); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 68 | |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void ChronoShareGui::createTrayIcon() |
| 72 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 73 | // create a new icon menu |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 74 | m_trayIconMenu = new QMenu(this); |
| 75 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 76 | // add actions to the menu |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 77 | m_trayIconMenu->addAction(m_openFolder); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 78 | m_trayIconMenu->addAction(m_changeFolder); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 79 | m_trayIconMenu->addSeparator(); |
| 80 | m_trayIconMenu->addAction(m_quitProgram); |
| 81 | |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 82 | // create new tray icon |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 83 | m_trayIcon = new QSystemTrayIcon(this); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 84 | |
| 85 | // associate the menu with the tray icon |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 86 | m_trayIcon->setContextMenu(m_trayIconMenu); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 87 | |
| 88 | // handle left click of icon |
| 89 | connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason))); |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void ChronoShareGui::setIcon() |
| 93 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 94 | // set the icon image |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 95 | m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png")); |
| 96 | } |
| 97 | |
| 98 | void ChronoShareGui::openSharedFolder() |
| 99 | { |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 100 | // tell Finder to open the shared folder |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 101 | QStringList scriptArgs; |
| 102 | scriptArgs << QLatin1String("-e") |
| 103 | << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"") |
| 104 | .arg(m_dirPath); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 105 | |
| 106 | // execute the commands to make it happen |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 107 | QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 108 | |
| 109 | // clear command arguments |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 110 | scriptArgs.clear(); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 111 | |
| 112 | // tell Finder to appear |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 113 | scriptArgs << QLatin1String("-e") |
| 114 | << QLatin1String("tell application \"Finder\" to activate"); |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 115 | |
| 116 | // execute the commands to make it happen |
Jared Lindblom | 06405c4 | 2013-01-17 20:48:39 -0800 | [diff] [blame] | 117 | QProcess::execute("/usr/bin/osascript", scriptArgs); |
| 118 | } |
Jared Lindblom | b4032e2 | 2013-01-17 23:50:51 -0800 | [diff] [blame^] | 119 | |
| 120 | void ChronoShareGui::openFileDialog() |
| 121 | { |
| 122 | // prompt user for new directory |
| 123 | m_dirPath = QFileDialog::getExistingDirectory(this, tr("Open Directory"), |
| 124 | m_dirPath, QFileDialog::ShowDirsOnly | |
| 125 | QFileDialog::DontResolveSymlinks); |
| 126 | |
| 127 | qDebug() << m_dirPath; |
| 128 | |
| 129 | // save settings |
| 130 | saveSettings(); |
| 131 | } |
| 132 | |
| 133 | void ChronoShareGui::trayIconClicked(QSystemTrayIcon::ActivationReason reason) |
| 134 | { |
| 135 | // if double clicked, open shared folder |
| 136 | if(reason == QSystemTrayIcon::DoubleClick) |
| 137 | { |
| 138 | openSharedFolder(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void ChronoShareGui::loadSettings() |
| 143 | { |
| 144 | // Load Settings |
| 145 | QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 146 | m_dirPath = settings.value("dirPath", QDir::homePath()).toString(); |
| 147 | } |
| 148 | |
| 149 | void ChronoShareGui::saveSettings() |
| 150 | { |
| 151 | // Save Settings |
| 152 | QSettings settings(m_settingsFilePath, QSettings::NativeFormat); |
| 153 | settings.setValue("dirPath", m_dirPath); |
| 154 | } |
| 155 | |
| 156 | void ChronoShareGui::closeEvent(QCloseEvent* event) |
| 157 | { |
| 158 | qDebug() << "Close Event."; |
| 159 | event->ignore(); // don't let the event propagate to the base class |
| 160 | } |