blob: 0285f782671a46a9b69b69325dba59294a922a8f [file] [log] [blame]
Jared Lindblom06405c42013-01-17 20:48:39 -08001/* -*- 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 Lindblomb4032e22013-01-17 23:50:51 -080024ChronoShareGui::ChronoShareGui(QWidget *parent) :
Jared Lindblom06405c42013-01-17 20:48:39 -080025 QWidget(parent),
Jared Lindblomb4032e22013-01-17 23:50:51 -080026 m_fileDialogWidget(new QWidget())
Jared Lindblom06405c42013-01-17 20:48:39 -080027{
Jared Lindblomb4032e22013-01-17 23:50:51 -080028 // load settings
29 loadSettings();
30
31 // create actions that result from clicking a menu option
Jared Lindblom06405c42013-01-17 20:48:39 -080032 createActions();
Jared Lindblomb4032e22013-01-17 23:50:51 -080033
34 // create tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -080035 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -080036
37 // set icon image
Jared Lindblom06405c42013-01-17 20:48:39 -080038 setIcon();
39
Jared Lindblomb4032e22013-01-17 23:50:51 -080040 // show tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -080041 m_trayIcon->show();
42}
43
44ChronoShareGui::~ChronoShareGui()
45{
Jared Lindblomb4032e22013-01-17 23:50:51 -080046 // cleanup
Jared Lindblom06405c42013-01-17 20:48:39 -080047 delete m_trayIcon;
48 delete m_trayIconMenu;
49 delete m_openFolder;
Jared Lindblomb4032e22013-01-17 23:50:51 -080050 delete m_changeFolder;
Jared Lindblom06405c42013-01-17 20:48:39 -080051 delete m_quitProgram;
Jared Lindblomb4032e22013-01-17 23:50:51 -080052 delete m_fileDialogWidget;
Jared Lindblom06405c42013-01-17 20:48:39 -080053}
54
55void ChronoShareGui::createActions()
56{
Jared Lindblomb4032e22013-01-17 23:50:51 -080057 // create the "open folder" action
58 m_openFolder = new QAction(tr("&Open Folder"), this);
Jared Lindblom06405c42013-01-17 20:48:39 -080059 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
60
Jared Lindblomb4032e22013-01-17 23:50:51 -080061 // 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 Lindblom06405c42013-01-17 20:48:39 -080066 m_quitProgram = new QAction(tr("&Quit"), this);
67 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblomb4032e22013-01-17 23:50:51 -080068
Jared Lindblom06405c42013-01-17 20:48:39 -080069}
70
71void ChronoShareGui::createTrayIcon()
72{
Jared Lindblomb4032e22013-01-17 23:50:51 -080073 // create a new icon menu
Jared Lindblom06405c42013-01-17 20:48:39 -080074 m_trayIconMenu = new QMenu(this);
75
Jared Lindblomb4032e22013-01-17 23:50:51 -080076 // add actions to the menu
Jared Lindblom06405c42013-01-17 20:48:39 -080077 m_trayIconMenu->addAction(m_openFolder);
Jared Lindblomb4032e22013-01-17 23:50:51 -080078 m_trayIconMenu->addAction(m_changeFolder);
Jared Lindblom06405c42013-01-17 20:48:39 -080079 m_trayIconMenu->addSeparator();
80 m_trayIconMenu->addAction(m_quitProgram);
81
Jared Lindblomb4032e22013-01-17 23:50:51 -080082 // create new tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -080083 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -080084
85 // associate the menu with the tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -080086 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -080087
88 // handle left click of icon
89 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -080090}
91
92void ChronoShareGui::setIcon()
93{
Jared Lindblomb4032e22013-01-17 23:50:51 -080094 // set the icon image
Jared Lindblom06405c42013-01-17 20:48:39 -080095 m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png"));
96}
97
98void ChronoShareGui::openSharedFolder()
99{
Jared Lindblomb4032e22013-01-17 23:50:51 -0800100 // tell Finder to open the shared folder
Jared Lindblom06405c42013-01-17 20:48:39 -0800101 QStringList scriptArgs;
102 scriptArgs << QLatin1String("-e")
103 << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
104 .arg(m_dirPath);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800105
106 // execute the commands to make it happen
Jared Lindblom06405c42013-01-17 20:48:39 -0800107 QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800108
109 // clear command arguments
Jared Lindblom06405c42013-01-17 20:48:39 -0800110 scriptArgs.clear();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800111
112 // tell Finder to appear
Jared Lindblom06405c42013-01-17 20:48:39 -0800113 scriptArgs << QLatin1String("-e")
114 << QLatin1String("tell application \"Finder\" to activate");
Jared Lindblomb4032e22013-01-17 23:50:51 -0800115
116 // execute the commands to make it happen
Jared Lindblom06405c42013-01-17 20:48:39 -0800117 QProcess::execute("/usr/bin/osascript", scriptArgs);
118}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800119
120void 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
133void ChronoShareGui::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
134{
135 // if double clicked, open shared folder
136 if(reason == QSystemTrayIcon::DoubleClick)
137 {
138 openSharedFolder();
139 }
140}
141
142void ChronoShareGui::loadSettings()
143{
144 // Load Settings
145 QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
146 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
147}
148
149void ChronoShareGui::saveSettings()
150{
151 // Save Settings
152 QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
153 settings.setValue("dirPath", m_dirPath);
154}
155
156void ChronoShareGui::closeEvent(QCloseEvent* event)
157{
158 qDebug() << "Close Event.";
159 event->ignore(); // don't let the event propagate to the base class
160}