blob: 13c3a51b4739d07cb04579f11d44d9076afa78ae [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 Afanasyev9e5a4702013-01-24 13:15:23 -080024
25
Alexander Afanasyev83a53002013-01-24 11:12:01 -080026INIT_LOGGER ("Gui");
27
28ChronoShareGui::ChronoShareGui(QWidget *parent)
29 : QWidget(parent)
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080030 , m_watcher (0)
Alexander Afanasyev83a53002013-01-24 11:12:01 -080031 // , m_settingsFilePath(QDir::homePath() + "/.chronoshare")
Jared Lindblom06405c42013-01-17 20:48:39 -080032{
Alexander Afanasyev83a53002013-01-24 11:12:01 -080033 // load settings
34 if(!loadSettings())
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080035 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -080036 // prompt user to choose folder
37 openMessageBox("First Time Setup", "Please select your shared folder location.");
38 openFileDialog();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080039 }
Jared Lindblomb4032e22013-01-17 23:50:51 -080040
Alexander Afanasyev83a53002013-01-24 11:12:01 -080041 // create actions that result from clicking a menu option
42 createActions();
Jared Lindblomb4032e22013-01-17 23:50:51 -080043
Alexander Afanasyev83a53002013-01-24 11:12:01 -080044 // create tray icon
45 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -080046
Alexander Afanasyev83a53002013-01-24 11:12:01 -080047 // set icon image
48 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -080049
Alexander Afanasyev83a53002013-01-24 11:12:01 -080050 // show tray icon
51 m_trayIcon->show();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080052
53 // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix,
54 // const std::string &sharedFolder, const boost::filesystem::path &rootDir,
55 // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2);
56
57 m_watcher = new FsWatcher (m_dirPath);
Jared Lindblom06405c42013-01-17 20:48:39 -080058}
59
60ChronoShareGui::~ChronoShareGui()
61{
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080062 if (!m_watcher)
63 {
64 delete m_watcher;
65 }
66
Alexander Afanasyev83a53002013-01-24 11:12:01 -080067 // cleanup
68 delete m_trayIcon;
69 delete m_trayIconMenu;
70 delete m_openFolder;
71 delete m_viewSettings;
72 delete m_changeFolder;
73 delete m_quitProgram;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080074}
75
76void ChronoShareGui::openMessageBox(QString title, QString text)
77{
Alexander Afanasyev83a53002013-01-24 11:12:01 -080078 QMessageBox messageBox(this);
79 messageBox.setWindowTitle(title);
80 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080081
Alexander Afanasyev83a53002013-01-24 11:12:01 -080082 messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png"));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080083
Alexander Afanasyev83a53002013-01-24 11:12:01 -080084 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080085}
86
87void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
88{
Alexander Afanasyev83a53002013-01-24 11:12:01 -080089 QMessageBox messageBox(this);
90 messageBox.setWindowTitle(title);
91 messageBox.setText(text);
92 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080093
Alexander Afanasyev83a53002013-01-24 11:12:01 -080094 messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png"));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080095
Alexander Afanasyev83a53002013-01-24 11:12:01 -080096 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -080097}
98
99void ChronoShareGui::createActions()
100{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800101 // create the "open folder" action
102 m_openFolder = new QAction(tr("&Open Folder"), this);
103 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800104
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800105 // create the "view settings" action
106 m_viewSettings = new QAction(tr("&View Settings"), this);
107 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800108
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800109 // create the "change folder" action
110 m_changeFolder = new QAction(tr("&Change Folder"), this);
111 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800112
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800113 // create the "quit program" action
114 m_quitProgram = new QAction(tr("&Quit"), this);
115 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800116}
117
118void ChronoShareGui::createTrayIcon()
119{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800120 // create a new icon menu
121 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800122
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800123 // add actions to the menu
124 m_trayIconMenu->addAction(m_openFolder);
125 m_trayIconMenu->addSeparator();
126 m_trayIconMenu->addAction(m_viewSettings);
127 m_trayIconMenu->addAction(m_changeFolder);
128 m_trayIconMenu->addSeparator();
129 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800130
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800131 // create new tray icon
132 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800133
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800134 // associate the menu with the tray icon
135 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800136
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800137 // handle left click of icon
138 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800139}
140
141void ChronoShareGui::setIcon()
142{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800143 // set the icon image
144 m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png"));
Jared Lindblom06405c42013-01-17 20:48:39 -0800145}
146
147void ChronoShareGui::openSharedFolder()
148{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800149 // Alex: isn't there an OS-independent way in QT for this?
Jared Lindblomb4032e22013-01-17 23:50:51 -0800150
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800151 // tell Finder to open the shared folder
152 QStringList scriptArgs;
153 scriptArgs << QLatin1String("-e")
154 << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
155 .arg(m_dirPath);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800156
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800157 // execute the commands to make it happen
158 QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800159
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800160 // clear command arguments
161 scriptArgs.clear();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800162
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800163 // tell Finder to appear
164 scriptArgs << QLatin1String("-e")
165 << QLatin1String("tell application \"Finder\" to activate");
166
167 // execute the commands to make it happen
168 QProcess::execute("/usr/bin/osascript", scriptArgs);
Jared Lindblom06405c42013-01-17 20:48:39 -0800169}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800170
171void ChronoShareGui::openFileDialog()
172{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800173 // prompt user for new directory
174 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800175 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
176 QFileInfo qFileInfo (tempPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800177
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800178 if(qFileInfo.isDir())
179 m_dirPath = tempPath;
180 else
181 openMessageBox ("Error", "Not a valid folder, Ignoring.");
Jared Lindblomb4032e22013-01-17 23:50:51 -0800182
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800183 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800184
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800185 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.");
186
187 // save settings
188 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800189}
190
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800191void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800192{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800193 // if double clicked, open shared folder
194 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800195 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800196 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800197 }
198}
199
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800200void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800201{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800202 // simple for now
203 openMessageBox("Chronoshare Settings", "CurrentFolder:\n" + m_dirPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800204}
205
206bool ChronoShareGui::loadSettings()
207{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800208 bool successful = false;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800209
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800210 // Load Settings
211 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
212 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800213
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800214 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
215
216 if(settings.contains("dirPath"))
217 {
218 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
219 successful = true;
220 }
221 else
222 {
223 m_dirPath = QDir::homePath();
224 successful = false;
225 }
226
227 _LOG_DEBUG ("Found configured path: " << (successful? m_dirPath.toStdString () : std::string("no")));
228
229 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800230}
231
232void ChronoShareGui::saveSettings()
233{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800234 // Save Settings
235 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
236 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
237 settings.setValue("dirPath", m_dirPath);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800238}
239
240void ChronoShareGui::closeEvent(QCloseEvent* event)
241{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800242 _LOG_DEBUG ("Close Event")
243 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800244}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800245
246#if WAF
247#include "chronosharegui.moc"
248#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800249#endif