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