blob: 5b4990958241821685768696a00ac8ee371b8455 [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 Lindblom9d8a5b12013-01-20 15:21:17 -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 Lindblom9d8a5b12013-01-20 15:21:17 -080098 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -080099}
100
101void ChronoShareGui::createTrayIcon()
102{
Jared Lindblomb4032e22013-01-17 23:50:51 -0800103 // create a new icon menu
Jared Lindblom06405c42013-01-17 20:48:39 -0800104 m_trayIconMenu = new QMenu(this);
105
Jared Lindblomb4032e22013-01-17 23:50:51 -0800106 // add actions to the menu
Jared Lindblom06405c42013-01-17 20:48:39 -0800107 m_trayIconMenu->addAction(m_openFolder);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800108 m_trayIconMenu->addSeparator();
109 m_trayIconMenu->addAction(m_viewSettings);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800110 m_trayIconMenu->addAction(m_changeFolder);
Jared Lindblom06405c42013-01-17 20:48:39 -0800111 m_trayIconMenu->addSeparator();
112 m_trayIconMenu->addAction(m_quitProgram);
113
Jared Lindblomb4032e22013-01-17 23:50:51 -0800114 // create new tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -0800115 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800116
117 // associate the menu with the tray icon
Jared Lindblom06405c42013-01-17 20:48:39 -0800118 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800119
120 // handle left click of icon
121 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800122}
123
124void ChronoShareGui::setIcon()
125{
Jared Lindblomb4032e22013-01-17 23:50:51 -0800126 // set the icon image
Jared Lindblom06405c42013-01-17 20:48:39 -0800127 m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png"));
128}
129
130void ChronoShareGui::openSharedFolder()
131{
Jared Lindblomb4032e22013-01-17 23:50:51 -0800132 // tell Finder to open the shared folder
Jared Lindblom06405c42013-01-17 20:48:39 -0800133 QStringList scriptArgs;
134 scriptArgs << QLatin1String("-e")
135 << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
136 .arg(m_dirPath);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800137
138 // execute the commands to make it happen
Jared Lindblom06405c42013-01-17 20:48:39 -0800139 QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800140
141 // clear command arguments
Jared Lindblom06405c42013-01-17 20:48:39 -0800142 scriptArgs.clear();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800143
144 // tell Finder to appear
Jared Lindblom06405c42013-01-17 20:48:39 -0800145 scriptArgs << QLatin1String("-e")
146 << QLatin1String("tell application \"Finder\" to activate");
Jared Lindblomb4032e22013-01-17 23:50:51 -0800147
148 // execute the commands to make it happen
Jared Lindblom06405c42013-01-17 20:48:39 -0800149 QProcess::execute("/usr/bin/osascript", scriptArgs);
150}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800151
152void ChronoShareGui::openFileDialog()
153{
154 // prompt user for new directory
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800155 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Jared Lindblomb4032e22013-01-17 23:50:51 -0800156 m_dirPath, QFileDialog::ShowDirsOnly |
157 QFileDialog::DontResolveSymlinks);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800158 QFileInfo qFileInfo(tempPath);
159
160 if(qFileInfo.isDir())
161 m_dirPath = tempPath;
162 else
163 openMessageBox("Error", "Not a valid folder, Ignoring.");
Jared Lindblomb4032e22013-01-17 23:50:51 -0800164
165 qDebug() << m_dirPath;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800166 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 -0800167
168 // save settings
169 saveSettings();
170}
171
172void 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 Lindblom5d7e51e2013-01-18 00:59:34 -0800181void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800182{
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800183 // simple for now
184 openMessageBox("Chronoshare Settings", "CurrentFolder:\n" + m_dirPath);
185}
186
187bool ChronoShareGui::loadSettings()
188{
189 bool successful = false;
190
Jared Lindblomb4032e22013-01-17 23:50:51 -0800191 // Load Settings
192 QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
Jared Lindblom9d8a5b12013-01-20 15:21:17 -0800193
194 qDebug() << settings.allKeys();
195
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800196 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 Lindblom9d8a5b12013-01-20 15:21:17 -0800206
207 qDebug() << "success: " << successful;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800208
209 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800210}
211
212void ChronoShareGui::saveSettings()
213{
Jared Lindblom9d8a5b12013-01-20 15:21:17 -0800214 qDebug() << m_settingsFilePath;
215 qDebug() << m_dirPath;
216
Jared Lindblomb4032e22013-01-17 23:50:51 -0800217 // Save Settings
218 QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
219 settings.setValue("dirPath", m_dirPath);
220}
221
222void ChronoShareGui::closeEvent(QCloseEvent* event)
223{
224 qDebug() << "Close Event.";
225 event->ignore(); // don't let the event propagate to the base class
226}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800227
228#if WAF
229#include "chronosharegui.moc"
230#include "chronosharegui.cpp.moc"
Jared Lindblom9d8a5b12013-01-20 15:21:17 -0800231#endif