blob: f2523704756dd16b7aeed126b48a0f98167423c9 [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"
Jared Lindblom06405c42013-01-17 20:48:39 -080022
Jared Lindblomb4032e22013-01-17 23:50:51 -080023ChronoShareGui::ChronoShareGui(QWidget *parent) :
Jared Lindblom06405c42013-01-17 20:48:39 -080024 QWidget(parent),
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080025 m_settingsFilePath(QDir::homePath() + ".cronoShare")
Jared Lindblom06405c42013-01-17 20:48:39 -080026{
Jared Lindblomb4032e22013-01-17 23:50:51 -080027 // load settings
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080028 if(!loadSettings())
29 {
30 // prompt user to choose folder
31 openMessageBox("First Time Setup", "Please select your shared folder location.");
32 openFileDialog();
33 }
Jared Lindblomb4032e22013-01-17 23:50:51 -080034
35 // create actions that result from clicking a menu option
Jared Lindblom06405c42013-01-17 20:48:39 -080036 createActions();
Jared Lindblomb4032e22013-01-17 23:50:51 -080037
38 // create tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -080039 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -080040
41 // set icon image
Jared Lindblom06405c42013-01-17 20:48:39 -080042 setIcon();
43
Jared Lindblomb4032e22013-01-17 23:50:51 -080044 // show tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -080045 m_trayIcon->show();
46}
47
48ChronoShareGui::~ChronoShareGui()
49{
Jared Lindblomb4032e22013-01-17 23:50:51 -080050 // cleanup
Jared Lindblom06405c42013-01-17 20:48:39 -080051 delete m_trayIcon;
52 delete m_trayIconMenu;
53 delete m_openFolder;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080054 delete m_viewSettings;
Jared Lindblomb4032e22013-01-17 23:50:51 -080055 delete m_changeFolder;
Jared Lindblom06405c42013-01-17 20:48:39 -080056 delete m_quitProgram;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080057}
58
59void 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
70void 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 Lindblom06405c42013-01-17 20:48:39 -080080}
81
82void ChronoShareGui::createActions()
83{
Jared Lindblomb4032e22013-01-17 23:50:51 -080084 // create the "open folder" action
85 m_openFolder = new QAction(tr("&Open Folder"), this);
Jared Lindblom06405c42013-01-17 20:48:39 -080086 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
87
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080088 // 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 Lindblomb4032e22013-01-17 23:50:51 -080092 // 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 Lindblom06405c42013-01-17 20:48:39 -080097 m_quitProgram = new QAction(tr("&Quit"), this);
Jared Lindblomdc845f02013-01-18 17:29:40 -080098 connect(m_quitProgram, SIGNAL(triggered()), this, SLOT(quit()));
Jared Lindblomb4032e22013-01-17 23:50:51 -080099
Jared Lindblom06405c42013-01-17 20:48:39 -0800100}
101
102void ChronoShareGui::createTrayIcon()
103{
Jared Lindblomb4032e22013-01-17 23:50:51 -0800104 // create a new icon menu
Jared Lindblom06405c42013-01-17 20:48:39 -0800105 m_trayIconMenu = new QMenu(this);
106
Jared Lindblomb4032e22013-01-17 23:50:51 -0800107 // add actions to the menu
Jared Lindblom06405c42013-01-17 20:48:39 -0800108 m_trayIconMenu->addAction(m_openFolder);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800109 m_trayIconMenu->addSeparator();
110 m_trayIconMenu->addAction(m_viewSettings);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800111 m_trayIconMenu->addAction(m_changeFolder);
Jared Lindblom06405c42013-01-17 20:48:39 -0800112 m_trayIconMenu->addSeparator();
113 m_trayIconMenu->addAction(m_quitProgram);
114
Jared Lindblomb4032e22013-01-17 23:50:51 -0800115 // create new tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -0800116 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800117
118 // associate the menu with the tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -0800119 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800120
121 // handle left click of icon
122 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800123}
124
125void ChronoShareGui::setIcon()
126{
Jared Lindblomb4032e22013-01-17 23:50:51 -0800127 // set the icon image
Jared Lindblom06405c42013-01-17 20:48:39 -0800128 m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png"));
129}
130
131void ChronoShareGui::openSharedFolder()
132{
Jared Lindblomb4032e22013-01-17 23:50:51 -0800133 // tell Finder to open the shared folder
Jared Lindblom06405c42013-01-17 20:48:39 -0800134 QStringList scriptArgs;
135 scriptArgs << QLatin1String("-e")
136 << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
137 .arg(m_dirPath);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800138
139 // execute the commands to make it happen
Jared Lindblom06405c42013-01-17 20:48:39 -0800140 QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800141
142 // clear command arguments
Jared Lindblom06405c42013-01-17 20:48:39 -0800143 scriptArgs.clear();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800144
145 // tell Finder to appear
Jared Lindblom06405c42013-01-17 20:48:39 -0800146 scriptArgs << QLatin1String("-e")
147 << QLatin1String("tell application \"Finder\" to activate");
Jared Lindblomb4032e22013-01-17 23:50:51 -0800148
149 // execute the commands to make it happen
Jared Lindblom06405c42013-01-17 20:48:39 -0800150 QProcess::execute("/usr/bin/osascript", scriptArgs);
151}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800152
153void ChronoShareGui::openFileDialog()
154{
155 // prompt user for new directory
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800156 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Jared Lindblomb4032e22013-01-17 23:50:51 -0800157 m_dirPath, QFileDialog::ShowDirsOnly |
158 QFileDialog::DontResolveSymlinks);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800159 QFileInfo qFileInfo(tempPath);
160
161 if(qFileInfo.isDir())
162 m_dirPath = tempPath;
163 else
164 openMessageBox("Error", "Not a valid folder, Ignoring.");
Jared Lindblomb4032e22013-01-17 23:50:51 -0800165
166 qDebug() << m_dirPath;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800167 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 -0800168
169 // save settings
170 saveSettings();
171}
172
173void ChronoShareGui::trayIconClicked(QSystemTrayIcon::ActivationReason reason)
174{
175 // if double clicked, open shared folder
176 if(reason == QSystemTrayIcon::DoubleClick)
177 {
178 openSharedFolder();
179 }
180}
181
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800182void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800183{
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800184 // simple for now
185 openMessageBox("Chronoshare Settings", "CurrentFolder:\n" + m_dirPath);
186}
187
188bool ChronoShareGui::loadSettings()
189{
190 bool successful = false;
191
Jared Lindblomb4032e22013-01-17 23:50:51 -0800192 // Load Settings
193 QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800194 if(settings.contains("dirPath"))
195 {
196 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
197 successful = true;
198 }
199 else
200 {
201 m_dirPath = QDir::homePath();
202 successful = false;
203 }
204
205 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800206}
207
208void ChronoShareGui::saveSettings()
209{
210 // Save Settings
211 QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
212 settings.setValue("dirPath", m_dirPath);
213}
214
215void ChronoShareGui::closeEvent(QCloseEvent* event)
216{
217 qDebug() << "Close Event.";
218 event->ignore(); // don't let the event propagate to the base class
219}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800220
221#if WAF
222#include "chronosharegui.moc"
223#include "chronosharegui.cpp.moc"
224#endif
225