blob: 6faa80bdce060c204712568a6979a40ff49172d7 [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 Lindblom5d7e51e2013-01-18 00:59:34 -080026 m_settingsFilePath(QDir::homePath() + ".cronoShare")
Jared Lindblom06405c42013-01-17 20:48:39 -080027{
Jared Lindblomb4032e22013-01-17 23:50:51 -080028 // load settings
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080029 if(!loadSettings())
30 {
31 // prompt user to choose folder
32 openMessageBox("First Time Setup", "Please select your shared folder location.");
33 openFileDialog();
34 }
Jared Lindblomb4032e22013-01-17 23:50:51 -080035
36 // create actions that result from clicking a menu option
Jared Lindblom06405c42013-01-17 20:48:39 -080037 createActions();
Jared Lindblomb4032e22013-01-17 23:50:51 -080038
39 // create tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -080040 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -080041
42 // set icon image
Jared Lindblom06405c42013-01-17 20:48:39 -080043 setIcon();
44
Jared Lindblomb4032e22013-01-17 23:50:51 -080045 // show tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -080046 m_trayIcon->show();
47}
48
49ChronoShareGui::~ChronoShareGui()
50{
Jared Lindblomb4032e22013-01-17 23:50:51 -080051 // cleanup
Jared Lindblom06405c42013-01-17 20:48:39 -080052 delete m_trayIcon;
53 delete m_trayIconMenu;
54 delete m_openFolder;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080055 delete m_viewSettings;
Jared Lindblomb4032e22013-01-17 23:50:51 -080056 delete m_changeFolder;
Jared Lindblom06405c42013-01-17 20:48:39 -080057 delete m_quitProgram;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080058}
59
60void 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
71void 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 Lindblom06405c42013-01-17 20:48:39 -080081}
82
83void ChronoShareGui::createActions()
84{
Jared Lindblomb4032e22013-01-17 23:50:51 -080085 // create the "open folder" action
86 m_openFolder = new QAction(tr("&Open Folder"), this);
Jared Lindblom06405c42013-01-17 20:48:39 -080087 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
88
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080089 // 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 Lindblomb4032e22013-01-17 23:50:51 -080093 // 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 Lindblom06405c42013-01-17 20:48:39 -080098 m_quitProgram = new QAction(tr("&Quit"), this);
99 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800100
Jared Lindblom06405c42013-01-17 20:48:39 -0800101}
102
103void ChronoShareGui::createTrayIcon()
104{
Jared Lindblomb4032e22013-01-17 23:50:51 -0800105 // create a new icon menu
Jared Lindblom06405c42013-01-17 20:48:39 -0800106 m_trayIconMenu = new QMenu(this);
107
Jared Lindblomb4032e22013-01-17 23:50:51 -0800108 // add actions to the menu
Jared Lindblom06405c42013-01-17 20:48:39 -0800109 m_trayIconMenu->addAction(m_openFolder);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800110 m_trayIconMenu->addSeparator();
111 m_trayIconMenu->addAction(m_viewSettings);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800112 m_trayIconMenu->addAction(m_changeFolder);
Jared Lindblom06405c42013-01-17 20:48:39 -0800113 m_trayIconMenu->addSeparator();
114 m_trayIconMenu->addAction(m_quitProgram);
115
Jared Lindblomb4032e22013-01-17 23:50:51 -0800116 // create new tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -0800117 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800118
119 // associate the menu with the tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -0800120 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800121
122 // handle left click of icon
123 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800124}
125
126void ChronoShareGui::setIcon()
127{
Jared Lindblomb4032e22013-01-17 23:50:51 -0800128 // set the icon image
Jared Lindblom06405c42013-01-17 20:48:39 -0800129 m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png"));
130}
131
132void ChronoShareGui::openSharedFolder()
133{
Jared Lindblomb4032e22013-01-17 23:50:51 -0800134 // tell Finder to open the shared folder
Jared Lindblom06405c42013-01-17 20:48:39 -0800135 QStringList scriptArgs;
136 scriptArgs << QLatin1String("-e")
137 << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
138 .arg(m_dirPath);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800139
140 // execute the commands to make it happen
Jared Lindblom06405c42013-01-17 20:48:39 -0800141 QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800142
143 // clear command arguments
Jared Lindblom06405c42013-01-17 20:48:39 -0800144 scriptArgs.clear();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800145
146 // tell Finder to appear
Jared Lindblom06405c42013-01-17 20:48:39 -0800147 scriptArgs << QLatin1String("-e")
148 << QLatin1String("tell application \"Finder\" to activate");
Jared Lindblomb4032e22013-01-17 23:50:51 -0800149
150 // execute the commands to make it happen
Jared Lindblom06405c42013-01-17 20:48:39 -0800151 QProcess::execute("/usr/bin/osascript", scriptArgs);
152}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800153
154void ChronoShareGui::openFileDialog()
155{
156 // prompt user for new directory
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800157 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Jared Lindblomb4032e22013-01-17 23:50:51 -0800158 m_dirPath, QFileDialog::ShowDirsOnly |
159 QFileDialog::DontResolveSymlinks);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800160 QFileInfo qFileInfo(tempPath);
161
162 if(qFileInfo.isDir())
163 m_dirPath = tempPath;
164 else
165 openMessageBox("Error", "Not a valid folder, Ignoring.");
Jared Lindblomb4032e22013-01-17 23:50:51 -0800166
167 qDebug() << m_dirPath;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800168 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 Lindblomb4032e22013-01-17 23:50:51 -0800169
170 // save settings
171 saveSettings();
172}
173
174void 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 Lindblom5d7e51e2013-01-18 00:59:34 -0800183void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800184{
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800185 // simple for now
186 openMessageBox("Chronoshare Settings", "CurrentFolder:\n" + m_dirPath);
187}
188
189bool ChronoShareGui::loadSettings()
190{
191 bool successful = false;
192
Jared Lindblomb4032e22013-01-17 23:50:51 -0800193 // Load Settings
194 QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800195 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 Lindblomb4032e22013-01-17 23:50:51 -0800207}
208
209void ChronoShareGui::saveSettings()
210{
211 // Save Settings
212 QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
213 settings.setValue("dirPath", m_dirPath);
214}
215
216void ChronoShareGui::closeEvent(QCloseEvent* event)
217{
218 qDebug() << "Close Event.";
219 event->ignore(); // don't let the event propagate to the base class
220}