blob: 8f4636390436b086c1df192d604088063c79176b [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"
Alexander Afanasyev83a53002013-01-24 11:12:01 -080022#include "logging.h"
Jared Lindblom06405c42013-01-17 20:48:39 -080023
Alexander Afanasyev83a53002013-01-24 11:12:01 -080024INIT_LOGGER ("Gui");
25
26ChronoShareGui::ChronoShareGui(QWidget *parent)
27 : QWidget(parent)
28 // , m_settingsFilePath(QDir::homePath() + "/.chronoshare")
Jared Lindblom06405c42013-01-17 20:48:39 -080029{
Alexander Afanasyev83a53002013-01-24 11:12:01 -080030 // load settings
31 if(!loadSettings())
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080032 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -080033 // prompt user to choose folder
34 openMessageBox("First Time Setup", "Please select your shared folder location.");
35 openFileDialog();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080036 }
Jared Lindblomb4032e22013-01-17 23:50:51 -080037
Alexander Afanasyev83a53002013-01-24 11:12:01 -080038 // create actions that result from clicking a menu option
39 createActions();
Jared Lindblomb4032e22013-01-17 23:50:51 -080040
Alexander Afanasyev83a53002013-01-24 11:12:01 -080041 // create tray icon
42 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -080043
Alexander Afanasyev83a53002013-01-24 11:12:01 -080044 // set icon image
45 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -080046
Alexander Afanasyev83a53002013-01-24 11:12:01 -080047 // show tray icon
48 m_trayIcon->show();
Jared Lindblom06405c42013-01-17 20:48:39 -080049}
50
51ChronoShareGui::~ChronoShareGui()
52{
Alexander Afanasyev83a53002013-01-24 11:12:01 -080053 // 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 Lindblom5d7e51e2013-01-18 00:59:34 -080060}
61
62void ChronoShareGui::openMessageBox(QString title, QString text)
63{
Alexander Afanasyev83a53002013-01-24 11:12:01 -080064 QMessageBox messageBox(this);
65 messageBox.setWindowTitle(title);
66 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080067
Alexander Afanasyev83a53002013-01-24 11:12:01 -080068 messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png"));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080069
Alexander Afanasyev83a53002013-01-24 11:12:01 -080070 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080071}
72
73void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
74{
Alexander Afanasyev83a53002013-01-24 11:12:01 -080075 QMessageBox messageBox(this);
76 messageBox.setWindowTitle(title);
77 messageBox.setText(text);
78 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080079
Alexander Afanasyev83a53002013-01-24 11:12:01 -080080 messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png"));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080081
Alexander Afanasyev83a53002013-01-24 11:12:01 -080082 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -080083}
84
85void ChronoShareGui::createActions()
86{
Alexander Afanasyev83a53002013-01-24 11:12:01 -080087 // create the "open folder" action
88 m_openFolder = new QAction(tr("&Open Folder"), this);
89 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -080090
Alexander Afanasyev83a53002013-01-24 11:12:01 -080091 // create the "view settings" action
92 m_viewSettings = new QAction(tr("&View Settings"), this);
93 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080094
Alexander Afanasyev83a53002013-01-24 11:12:01 -080095 // create the "change folder" action
96 m_changeFolder = new QAction(tr("&Change Folder"), this);
97 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -080098
Alexander Afanasyev83a53002013-01-24 11:12:01 -080099 // create the "quit program" action
100 m_quitProgram = new QAction(tr("&Quit"), this);
101 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800102}
103
104void ChronoShareGui::createTrayIcon()
105{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800106 // create a new icon menu
107 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800108
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800109 // 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 Lindblom06405c42013-01-17 20:48:39 -0800116
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800117 // create new tray icon
118 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800119
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800120 // associate the menu with the tray icon
121 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800122
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800123 // handle left click of icon
124 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800125}
126
127void ChronoShareGui::setIcon()
128{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800129 // set the icon image
130 m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png"));
Jared Lindblom06405c42013-01-17 20:48:39 -0800131}
132
133void ChronoShareGui::openSharedFolder()
134{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800135 // Alex: isn't there an OS-independent way in QT for this?
Jared Lindblomb4032e22013-01-17 23:50:51 -0800136
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800137 // 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 Lindblomb4032e22013-01-17 23:50:51 -0800142
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800143 // execute the commands to make it happen
144 QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800145
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800146 // clear command arguments
147 scriptArgs.clear();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800148
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800149 // 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 Lindblom06405c42013-01-17 20:48:39 -0800155}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800156
157void ChronoShareGui::openFileDialog()
158{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800159 // 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 Lindblom5d7e51e2013-01-18 00:59:34 -0800164
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800165 if(qFileInfo.isDir())
166 m_dirPath = tempPath;
167 else
168 openMessageBox ("Error", "Not a valid folder, Ignoring.");
Jared Lindblomb4032e22013-01-17 23:50:51 -0800169
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800170 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800171
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800172 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 Lindblomb4032e22013-01-17 23:50:51 -0800176}
177
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800178void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800179{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800180 // if double clicked, open shared folder
181 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800182 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800183 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800184 }
185}
186
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800187void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800188{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800189 // simple for now
190 openMessageBox("Chronoshare Settings", "CurrentFolder:\n" + m_dirPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800191}
192
193bool ChronoShareGui::loadSettings()
194{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800195 bool successful = false;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800196
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800197 // Load Settings
198 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
199 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800200
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800201 // _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 Lindblomb4032e22013-01-17 23:50:51 -0800217}
218
219void ChronoShareGui::saveSettings()
220{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800221 // 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 Lindblomb4032e22013-01-17 23:50:51 -0800225}
226
227void ChronoShareGui::closeEvent(QCloseEvent* event)
228{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800229 _LOG_DEBUG ("Close Event")
230 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800231}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800232
233#if WAF
234#include "chronosharegui.moc"
235#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800236#endif