blob: f1a5cf31c2001088a71e4f07dc98b246bdd989fe [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"
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080023#include "ccnx-wrapper.h"
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080024#include <QValidator>
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080025
26#include <boost/make_shared.hpp>
27
28using namespace boost;
29using namespace Ccnx;
Jared Lindblom06405c42013-01-17 20:48:39 -080030
Alexander Afanasyev83a53002013-01-24 11:12:01 -080031INIT_LOGGER ("Gui");
32
33ChronoShareGui::ChronoShareGui(QWidget *parent)
Jared Lindblomc8ffd592013-01-25 00:00:30 -080034 : QDialog(parent)
Zhenkai Zhud5756312013-01-31 13:49:45 -080035 , m_watcher(0)
36 , m_dispatcher(0)
Jared Lindblom06405c42013-01-17 20:48:39 -080037{
Jared Lindblomc8ffd592013-01-25 00:00:30 -080038
39 setWindowTitle("Preferences");
40
41 labelUsername = new QLabel("Username");
42 labelSharedFolder = new QLabel("Shared Folder Name");
43 labelSharedFolderPath = new QLabel("Shared Folder Path");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080044
Zhenkai Zhud5756312013-01-31 13:49:45 -080045 QRegExp regex("(/[^/]+)+$");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080046 QValidator *prefixValidator = new QRegExpValidator(regex, this);
47
Jared Lindblomc8ffd592013-01-25 00:00:30 -080048 editUsername = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080049 editUsername->setValidator(prefixValidator);
50
51 QRegExp noPureWhiteSpace("^\\S+.*$");
52 QValidator *wsValidator = new QRegExpValidator(noPureWhiteSpace, this);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080053 editSharedFolder = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080054 editSharedFolder->setValidator(wsValidator);
55
Jared Lindblomc8ffd592013-01-25 00:00:30 -080056 editSharedFolderPath = new QLineEdit();
57 editSharedFolderPath->setReadOnly(true);
58 QPalette pal = editSharedFolderPath->palette();
59 pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Disabled, QPalette::Base));
60 editSharedFolderPath->setPalette(pal);
61 button = new QPushButton("Submit");
62 label = new QLabel();
63
64 connect(button, SIGNAL(clicked()), this, SLOT(changeSettings()));
65
66 mainLayout = new QVBoxLayout; //vertically
67 mainLayout->addWidget(labelUsername);
68 mainLayout->addWidget(editUsername);
69 mainLayout->addWidget(labelSharedFolder);
70 mainLayout->addWidget(editSharedFolder);
71 mainLayout->addWidget(labelSharedFolderPath);
72 mainLayout->addWidget(editSharedFolderPath);
73 mainLayout->addWidget(button);
74 mainLayout->addWidget(label);
75 setLayout(mainLayout);
76
Alexander Afanasyev83a53002013-01-24 11:12:01 -080077 // create actions that result from clicking a menu option
78 createActions();
Jared Lindblomb4032e22013-01-17 23:50:51 -080079
Alexander Afanasyev83a53002013-01-24 11:12:01 -080080 // create tray icon
81 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -080082
Alexander Afanasyev83a53002013-01-24 11:12:01 -080083 // set icon image
84 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -080085
Alexander Afanasyev83a53002013-01-24 11:12:01 -080086 // show tray icon
87 m_trayIcon->show();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080088
89 // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix,
90 // const std::string &sharedFolder, const boost::filesystem::path &rootDir,
91 // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2);
92
Zhenkai Zhud5756312013-01-31 13:49:45 -080093 // load settings
94 if(!loadSettings())
95 {
96 // prompt user to choose folder
97 openMessageBox("First Time Setup", "Please enter a username, shared folder name and choose the shared folder path on your local filesystem.");
98 viewSettings();
99 openFileDialog();
100 viewSettings();
101 }
102 else
103 {
104 startBackend();
105 }
106}
107
108void
109ChronoShareGui::startBackend()
110{
111 if (m_watcher != 0 && m_dispatcher != 0)
112 {
113 return;
114 }
115
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800116 m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (),
117 m_dirPath.toStdString (), make_shared<CcnxWrapper> ());
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800118
119 // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800120 m_watcher = new FsWatcher (m_dirPath,
121 bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1),
Zhenkai Zhud1756272013-02-01 17:02:18 -0800122 bind (&Dispatcher::Did_LocalFile_Delete, m_dispatcher, _1));
Jared Lindblom06405c42013-01-17 20:48:39 -0800123}
124
125ChronoShareGui::~ChronoShareGui()
126{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800127 delete m_watcher; // stop filewatching ASAP
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800128 delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object)
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800129
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800130 // cleanup
131 delete m_trayIcon;
132 delete m_trayIconMenu;
133 delete m_openFolder;
134 delete m_viewSettings;
135 delete m_changeFolder;
136 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800137
138 delete labelUsername;
139 delete labelSharedFolder;
140 delete editUsername;
141 delete editSharedFolder;
142 delete button;
143 delete label;
144 delete mainLayout;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800145}
146
147void ChronoShareGui::openMessageBox(QString title, QString text)
148{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800149 QMessageBox messageBox(this);
150 messageBox.setWindowTitle(title);
151 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800152
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800153 messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png"));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800154
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800155 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800156}
157
158void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
159{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800160 QMessageBox messageBox(this);
161 messageBox.setWindowTitle(title);
162 messageBox.setText(text);
163 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800164
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800165 messageBox.setIconPixmap(QPixmap(":/images/friends-group-icon.png"));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800166
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800167 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800168}
169
170void ChronoShareGui::createActions()
171{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800172 // create the "open folder" action
173 m_openFolder = new QAction(tr("&Open Folder"), this);
174 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800175
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800176 // create the "view settings" action
177 m_viewSettings = new QAction(tr("&View Settings"), this);
178 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800179
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800180 // create the "change folder" action
181 m_changeFolder = new QAction(tr("&Change Folder"), this);
182 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800183
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800184 // create the "quit program" action
185 m_quitProgram = new QAction(tr("&Quit"), this);
186 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800187}
188
189void ChronoShareGui::createTrayIcon()
190{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800191 // create a new icon menu
192 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800193
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800194 // add actions to the menu
195 m_trayIconMenu->addAction(m_openFolder);
196 m_trayIconMenu->addSeparator();
197 m_trayIconMenu->addAction(m_viewSettings);
198 m_trayIconMenu->addAction(m_changeFolder);
199 m_trayIconMenu->addSeparator();
200 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800201
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800202 // create new tray icon
203 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800204
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800205 // associate the menu with the tray icon
206 m_trayIcon->setContextMenu(m_trayIconMenu);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800207
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800208 // handle left click of icon
209 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800210}
211
212void ChronoShareGui::setIcon()
213{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800214 // set the icon image
215 m_trayIcon->setIcon(QIcon(":/images/friends-group-icon.png"));
Jared Lindblom06405c42013-01-17 20:48:39 -0800216}
217
218void ChronoShareGui::openSharedFolder()
219{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800220 // Alex: isn't there an OS-independent way in QT for this?
Jared Lindblomb4032e22013-01-17 23:50:51 -0800221
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800222 // tell Finder to open the shared folder
223 QStringList scriptArgs;
224 scriptArgs << QLatin1String("-e")
225 << QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
226 .arg(m_dirPath);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800227
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800228 // execute the commands to make it happen
229 QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800230
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800231 // clear command arguments
232 scriptArgs.clear();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800233
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800234 // tell Finder to appear
235 scriptArgs << QLatin1String("-e")
236 << QLatin1String("tell application \"Finder\" to activate");
237
238 // execute the commands to make it happen
239 QProcess::execute("/usr/bin/osascript", scriptArgs);
Jared Lindblom06405c42013-01-17 20:48:39 -0800240}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800241
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800242void ChronoShareGui::changeSettings()
243{
244 if(!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800245 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800246 else
247 editUsername->setText(m_username);
248
249 if(!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800250 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800251 else
252 editSharedFolder->setText(m_sharedFolderName);
253
254 saveSettings();
255 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800256
257 startBackend();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800258}
259
Jared Lindblomb4032e22013-01-17 23:50:51 -0800260void ChronoShareGui::openFileDialog()
261{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800262 // prompt user for new directory
263 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800264 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
265 QFileInfo qFileInfo (tempPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800266
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800267 if(qFileInfo.isDir())
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800268 {
269 m_dirPath = tempPath;
270 editSharedFolderPath->setText(m_dirPath);
271 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800272 else
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800273 {
274 openMessageBox ("Error", "Not a valid folder, Ignoring.");
275 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800276
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800277 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800278
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800279 // save settings
280 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800281}
282
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800283void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800284{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800285 // if double clicked, open shared folder
286 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800287 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800288 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800289 }
290}
291
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800292void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800293{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800294 //simple for now
295 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800296 this->raise();
297 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800298}
299
300bool ChronoShareGui::loadSettings()
301{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800302 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800303
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800304 // Load Settings
305 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
306 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800307
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800308 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
309
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800310 if(settings.contains("username"))
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800311 {
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800312 m_username = settings.value("username", "admin").toString();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800313 }
314 else
315 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800316 successful = false;
317 }
318
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800319 editUsername->setText(m_username);
320
321 if(settings.contains("sharedfoldername"))
322 {
323 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
324 }
325 else
326 {
327 successful = false;
328 }
329
330 editSharedFolder->setText(m_sharedFolderName);
331
332 if(settings.contains("dirPath"))
333 {
334 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
335 }
336 else
337 {
338 successful = false;
339 }
340
341 editSharedFolderPath->setText(m_dirPath);
342
343 _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800344
345 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800346}
347
348void ChronoShareGui::saveSettings()
349{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800350 // Save Settings
351 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
352 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800353
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800354 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800355 settings.setValue("username", m_username);
356 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800357}
358
359void ChronoShareGui::closeEvent(QCloseEvent* event)
360{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800361 _LOG_DEBUG ("Close Event")
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800362 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800363 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800364}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800365
366#if WAF
367#include "chronosharegui.moc"
368#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800369#endif