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