blob: 8a30b05f5ff6801469e1dad7c1902f86c3092dbb [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>
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080019 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
21 * Ilya Moiseenko <iliamo@ucla.edu>
Jared Lindblom06405c42013-01-17 20:48:39 -080022 */
23
24#include "chronosharegui.h"
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080025
Alexander Afanasyev83a53002013-01-24 11:12:01 -080026#include "logging.h"
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080027#include "ccnx-wrapper.h"
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080028#include <QValidator>
Zhenkai Zhuce04cb42013-02-25 15:29:31 -080029#include <QDir>
Zhenkai Zhu25e13582013-02-27 15:33:01 -080030#include <QFileInfo>
Zhenkai Zhuce04cb42013-02-25 15:29:31 -080031#include <QDesktopServices>
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -080032
33#include <boost/make_shared.hpp>
34
35using namespace boost;
36using namespace Ccnx;
Jared Lindblom06405c42013-01-17 20:48:39 -080037
Zhenkai Zhud9429222013-02-25 22:34:09 -080038static const string HTTP_SERVER_ADDRESS = "localhost";
39static const string HTTP_SERVER_PORT = "9001";
Zhenkai Zhu6b53f932013-03-02 20:46:02 -080040#ifdef _DEBUG
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080041static const string DOC_ROOT = "gui/html";
Zhenkai Zhu6b53f932013-03-02 20:46:02 -080042#else
43static const string DOC_ROOT = ":/html";
44#endif
Zhenkai Zhud9429222013-02-25 22:34:09 -080045static const QString ICON_PICTURE_QSTRING(":/images/friends-group-icon.png");
46
Alexander Afanasyev83a53002013-01-24 11:12:01 -080047INIT_LOGGER ("Gui");
48
49ChronoShareGui::ChronoShareGui(QWidget *parent)
Jared Lindblomc8ffd592013-01-25 00:00:30 -080050 : QDialog(parent)
Zhenkai Zhud5756312013-01-31 13:49:45 -080051 , m_watcher(0)
52 , m_dispatcher(0)
Zhenkai Zhud9429222013-02-25 22:34:09 -080053 , m_httpServer(0)
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080054#ifdef ADHOC_SUPPORTED
55 , m_executor (1)
56#endif
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080057#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080058 , m_autoUpdate(new SparkleAutoUpdate(tr("http://irl.cs.ucla.edu/~zhenkai/chronoshare_dist/chronoshare.xml")))
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080059#endif
Jared Lindblom06405c42013-01-17 20:48:39 -080060{
Jared Lindblomf4b900e2013-02-07 18:08:47 -080061 setWindowTitle("Settings");
Jared Lindblomc8ffd592013-01-25 00:00:30 -080062
Jared Lindblomd8b76ad2013-02-07 20:37:37 -080063 labelUsername = new QLabel("Username (hint: /<username>)");
Jared Lindblomc8ffd592013-01-25 00:00:30 -080064 labelSharedFolder = new QLabel("Shared Folder Name");
65 labelSharedFolderPath = new QLabel("Shared Folder Path");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080066
Zhenkai Zhud5756312013-01-31 13:49:45 -080067 QRegExp regex("(/[^/]+)+$");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080068 QValidator *prefixValidator = new QRegExpValidator(regex, this);
69
Jared Lindblomc8ffd592013-01-25 00:00:30 -080070 editUsername = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080071 editUsername->setValidator(prefixValidator);
72
73 QRegExp noPureWhiteSpace("^\\S+.*$");
74 QValidator *wsValidator = new QRegExpValidator(noPureWhiteSpace, this);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080075 editSharedFolder = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080076 editSharedFolder->setValidator(wsValidator);
77
Jared Lindblomc8ffd592013-01-25 00:00:30 -080078 editSharedFolderPath = new QLineEdit();
79 editSharedFolderPath->setReadOnly(true);
80 QPalette pal = editSharedFolderPath->palette();
81 pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Disabled, QPalette::Base));
82 editSharedFolderPath->setPalette(pal);
83 button = new QPushButton("Submit");
Zhenkai Zhu630d0f12013-02-28 17:21:54 -080084
85 QString versionString = QString("Version: ChronoShare v%1").arg(CHRONOSHARE_VERSION);
86 label = new QLabel(versionString, this);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080087
88 connect(button, SIGNAL(clicked()), this, SLOT(changeSettings()));
89
90 mainLayout = new QVBoxLayout; //vertically
91 mainLayout->addWidget(labelUsername);
92 mainLayout->addWidget(editUsername);
93 mainLayout->addWidget(labelSharedFolder);
94 mainLayout->addWidget(editSharedFolder);
95 mainLayout->addWidget(labelSharedFolderPath);
96 mainLayout->addWidget(editSharedFolderPath);
97 mainLayout->addWidget(button);
98 mainLayout->addWidget(label);
99 setLayout(mainLayout);
100
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800101 // create actions that result from clicking a menu option
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800102 createActionsAndMenu();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800103
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800104 // create tray icon
105 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800106
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800107 // set icon image
108 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -0800109
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800110 // show tray icon
111 m_trayIcon->show();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800112
113 // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix,
114 // const std::string &sharedFolder, const boost::filesystem::path &rootDir,
115 // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2);
116
Zhenkai Zhud5756312013-01-31 13:49:45 -0800117 // load settings
118 if(!loadSettings())
119 {
120 // prompt user to choose folder
121 openMessageBox("First Time Setup", "Please enter a username, shared folder name and choose the shared folder path on your local filesystem.");
122 viewSettings();
123 openFileDialog();
124 viewSettings();
125 }
126 else
127 {
128 startBackend();
129 }
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800130
131#ifdef ADHOC_SUPPORTED
132 m_executor.start ();
133#endif
Zhenkai Zhud5756312013-01-31 13:49:45 -0800134}
135
136void
137ChronoShareGui::startBackend()
138{
139 if (m_watcher != 0 && m_dispatcher != 0)
140 {
141 return;
142 }
143
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800144 m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (),
145 m_dirPath.toStdString (), make_shared<CcnxWrapper> ());
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800146
147 // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800148 m_watcher = new FsWatcher (m_dirPath,
149 bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1),
Zhenkai Zhud1756272013-02-01 17:02:18 -0800150 bind (&Dispatcher::Did_LocalFile_Delete, m_dispatcher, _1));
Zhenkai Zhud9429222013-02-25 22:34:09 -0800151 QFileInfo indexHtmlInfo(":/html/index.html");
152 if (indexHtmlInfo.exists())
153 {
154 m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT);
155 m_httpServerThread = boost::thread(&http::server::server::run, m_httpServer);
156 }
157 else
158 {
159 _LOG_ERROR ("Http server doc root dir does not exist!");
160 // shall we bail here?
161 }
Jared Lindblom06405c42013-01-17 20:48:39 -0800162}
163
164ChronoShareGui::~ChronoShareGui()
165{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800166#ifdef ADHOC_SUPPORTED
167 m_executor.shutdown ();
168#endif
169
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800170 delete m_watcher; // stop filewatching ASAP
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800171 delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object)
Zhenkai Zhu377cbeb2013-02-28 16:36:26 -0800172 m_httpServer->handle_stop();
173 m_httpServerThread.join();
Zhenkai Zhud9429222013-02-25 22:34:09 -0800174 delete m_httpServer;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800175
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800176 // cleanup
177 delete m_trayIcon;
178 delete m_trayIconMenu;
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800179#ifdef ADHOC_SUPPORTED
180 delete m_wifiAction;
181#endif
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800182#ifdef SPARKLE_SUPPORTED
183 delete m_autoUpdate;
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800184 delete m_checkForUpdates;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800185#endif
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800186 delete m_openFolder;
187 delete m_viewSettings;
188 delete m_changeFolder;
189 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800190
191 delete labelUsername;
192 delete labelSharedFolder;
193 delete editUsername;
194 delete editSharedFolder;
195 delete button;
196 delete label;
197 delete mainLayout;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800198}
199
200void ChronoShareGui::openMessageBox(QString title, QString text)
201{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800202 QMessageBox messageBox(this);
203 messageBox.setWindowTitle(title);
204 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800205
Zhenkai Zhud9429222013-02-25 22:34:09 -0800206 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800207
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800208 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800209}
210
211void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
212{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800213 QMessageBox messageBox(this);
214 messageBox.setWindowTitle(title);
215 messageBox.setText(text);
216 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800217
Zhenkai Zhud9429222013-02-25 22:34:09 -0800218 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800219
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800220 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800221}
222
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800223void ChronoShareGui::createActionsAndMenu()
Jared Lindblom06405c42013-01-17 20:48:39 -0800224{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800225 _LOG_DEBUG ("Create actions");
226
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800227 // create the "open folder" action
228 m_openFolder = new QAction(tr("&Open Folder"), this);
229 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800230
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800231 m_openWeb = new QAction(tr("Open in &Browser"), this);
232 connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser()));
233
234 m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this);
235 for (int i = 0; i < 5; i++)
236 {
237 m_fileActions[i] = new QAction(this);
238 m_fileActions[i]->setVisible(false);
239 connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile()));
240 m_recentFilesMenu->addAction(m_fileActions[i]);
241 }
242 connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu()));
243
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800244 // create the "view settings" action
245 m_viewSettings = new QAction(tr("&View Settings"), this);
246 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800247
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800248 // create the "change folder" action
249 m_changeFolder = new QAction(tr("&Change Folder"), this);
250 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800251
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800252#ifdef ADHOC_SUPPORTED
253 // create "AdHoc Wifi" action
254 m_wifiAction = new QAction (tr("Enable AdHoc &WiFI"), m_trayIconMenu);
255 m_wifiAction->setChecked (false);
256 m_wifiAction->setCheckable (true);
257 connect (m_wifiAction, SIGNAL (toggled(bool)), this, SLOT(onAdHocChange(bool)));
258#endif
259
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800260#ifdef SPARKLE_SUPPORTED
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800261 m_checkForUpdates = new QAction (tr("Check For Updates"), this);
262 connect (m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates()));
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800263#endif
264
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800265 // create the "quit program" action
266 m_quitProgram = new QAction(tr("&Quit"), this);
267 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800268}
269
270void ChronoShareGui::createTrayIcon()
271{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800272 // create a new icon menu
273 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800274
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800275 // add actions to the menu
276 m_trayIconMenu->addAction(m_openFolder);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800277 m_trayIconMenu->addAction(m_openWeb);
278 m_trayIconMenu->addMenu(m_recentFilesMenu);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800279 m_trayIconMenu->addSeparator();
280 m_trayIconMenu->addAction(m_viewSettings);
281 m_trayIconMenu->addAction(m_changeFolder);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800282
283#ifdef SPARKLE_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800284 m_trayIconMenu->addSeparator();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800285 m_trayIconMenu->addAction(m_checkForUpdates);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800286#endif
287
288#ifdef ADHOC_SUPPORTED
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800289 m_trayIconMenu->addSeparator();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800290 m_trayIconMenu->addAction(m_wifiAction);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800291#endif
292
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800293 m_trayIconMenu->addSeparator();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800294 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800295
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800296 // create new tray icon
297 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800298
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800299#ifdef ADHOC_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800300 // associate the menu with the tray icon
301 m_trayIcon->setContextMenu(m_trayIconMenu);
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800302#endif
Jared Lindblomb4032e22013-01-17 23:50:51 -0800303
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800304 // handle left click of icon
305 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800306}
307
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800308void
309ChronoShareGui::onAdHocChange (bool state)
310{
311#ifdef ADHOC_SUPPORTED
312 if (m_wifiAction->isChecked ())
313 {
314 QMessageBox msgBox;
315 msgBox.setText ("WARNING: your WiFi will be disconnected");
316 msgBox.setIcon (QMessageBox::Warning);
317 msgBox.setInformativeText("AdHoc WiFi will disconnect your current WiFi.\n Are you sure that you are OK with that?");
318 msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
319 msgBox.setDefaultButton(QMessageBox::Cancel);
320 int ret = msgBox.exec();
321
322 if (ret == QMessageBox::Cancel)
323 {
324 m_wifiAction->setChecked (false);
325 }
326 else
327 {
328
329 m_wifiAction->setText (tr("Disable AdHoc WiFI"));
330
331 // create adhoc
332 m_executor.execute (Adhoc::CreateAdhoc);
333 }
334 }
335 else
336 {
337 m_wifiAction->setText (tr("Enable AdHoc WiFI"));
338
339 // disable adhoc
340 m_executor.execute (Adhoc::DestroyAdhoc);
341
342 // a trick in DestroyAdhoc ensures that WiFi will be reconnected to a default WiFi
343 }
344#endif
345}
346
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800347void
348ChronoShareGui::onCheckForUpdates()
349{
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800350#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800351 cout << "+++++++++++ trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800352 m_autoUpdate->checkForUpdates();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800353 cout << "+++++++++++ end trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800354#endif
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800355}
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800356
Jared Lindblom06405c42013-01-17 20:48:39 -0800357void ChronoShareGui::setIcon()
358{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800359 // set the icon image
Zhenkai Zhud9429222013-02-25 22:34:09 -0800360 m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING));
Jared Lindblom06405c42013-01-17 20:48:39 -0800361}
362
363void ChronoShareGui::openSharedFolder()
364{
Zhenkai Zhuce04cb42013-02-25 15:29:31 -0800365 QString path = QDir::toNativeSeparators(m_dirPath);
366 QDesktopServices::openUrl(QUrl("file:///" + path));
Jared Lindblom06405c42013-01-17 20:48:39 -0800367}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800368
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800369void ChronoShareGui::openInWebBrowser()
370{
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800371 QUrl url ("http://localhost:9001/");
372 url.setFragment ("fileList&"
373 "user=" + QUrl::toPercentEncoding (m_username) + "&"
374 "folder=" + QUrl::toPercentEncoding (m_sharedFolderName));
375
376 // i give up. there is double encoding and I have no idea how to fight it...
377 QDesktopServices::openUrl (url);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800378}
379
380void ChronoShareGui::openFile()
381{
382 // figure out who sent the signal
383 QAction *pAction = qobject_cast<QAction*>(sender());
384 if (pAction->isEnabled())
385 {
386 // we stored full path of the file in this toolTip field
387#ifdef Q_WS_MAC
388 // we do some hack so we could show the file in Finder highlighted
389 QStringList args;
390 args << "-e";
391 args << "tell application \"Finder\"";
392 args << "-e";
393 args << "activate";
394 args << "-e";
395 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
396 args << "-e";
397 args << "end tell";
398 QProcess::startDetached("osascript", args);
399#else
400 // too bad qt couldn't do highlighting for Linux (or Mac)
401 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
402#endif
403 }
404}
405
406void ChronoShareGui::updateRecentFilesMenu()
407{
408 for (int i = 0; i < 5; i++)
409 {
410 m_fileActions[i]->setVisible(false);
411 }
412 m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5);
413}
414
415void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index)
416{
417 QString fullPath = m_dirPath + "/" + filename.c_str();
418 QFileInfo fileInfo(fullPath);
419 if (fileInfo.exists())
420 {
421 // This is a hack, we just use some field to store the path
422 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
423 m_fileActions[index]->setEnabled(true);
424 }
425 else
426 {
427 // after half an hour frustrating test and search around,
428 // I think it's the problem of Qt.
429 // According to the Qt doc, the action cannot be clicked
430 // and the area would be grey, but it didn't happen
431 // User can still trigger the action, and not greyed
432 // added check in SLOT to see if the action is "enalbed"
433 // as a remedy
434 // Give up at least for now
435 m_fileActions[index]->setEnabled(false);
436 // UPDATE, file not fetched yet
437 if (action == 0)
438 {
439 QFont font;
440 // supposed by change the font, didn't happen
441 font.setWeight(QFont::Light);
442 m_fileActions[index]->setFont(font);
443 m_fileActions[index]->setToolTip(tr("Fetching..."));
444 }
445 // DELETE
446 else
447 {
448 QFont font;
449 // supposed by change the font, didn't happen
450 font.setStrikeOut(true);
451 m_fileActions[index]->setFont(font);
452 m_fileActions[index]->setToolTip(tr("Deleted..."));
453 }
454 }
455 m_fileActions[index]->setText(fileInfo.fileName());
456 m_fileActions[index]->setVisible(true);
457}
458
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800459void ChronoShareGui::changeSettings()
460{
461 if(!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800462 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800463 else
464 editUsername->setText(m_username);
465
466 if(!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800467 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800468 else
469 editSharedFolder->setText(m_sharedFolderName);
470
471 saveSettings();
472 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800473
474 startBackend();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800475}
476
Jared Lindblomb4032e22013-01-17 23:50:51 -0800477void ChronoShareGui::openFileDialog()
478{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800479 // prompt user for new directory
480 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800481 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
482 QFileInfo qFileInfo (tempPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800483
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800484 if(qFileInfo.isDir())
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800485 {
486 m_dirPath = tempPath;
487 editSharedFolderPath->setText(m_dirPath);
488 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800489 else
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800490 {
491 openMessageBox ("Error", "Not a valid folder, Ignoring.");
492 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800493
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800494 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800495
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800496 // save settings
497 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800498}
499
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800500void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800501{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800502 // if double clicked, open shared folder
503 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800504 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800505 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800506 }
507}
508
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800509void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800510{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800511 //simple for now
512 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800513 this->raise();
514 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800515}
516
517bool ChronoShareGui::loadSettings()
518{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800519 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800520
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800521 // Load Settings
522 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
523 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800524
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800525 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
526
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800527 if(settings.contains("username"))
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800528 {
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800529 m_username = settings.value("username", "admin").toString();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800530 }
531 else
532 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800533 successful = false;
534 }
535
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800536 editUsername->setText(m_username);
537
538 if(settings.contains("sharedfoldername"))
539 {
540 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
541 }
542 else
543 {
544 successful = false;
545 }
546
547 editSharedFolder->setText(m_sharedFolderName);
548
549 if(settings.contains("dirPath"))
550 {
551 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
552 }
553 else
554 {
555 successful = false;
556 }
557
558 editSharedFolderPath->setText(m_dirPath);
559
560 _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800561
562 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800563}
564
565void ChronoShareGui::saveSettings()
566{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800567 // Save Settings
568 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
569 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800570
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800571 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800572 settings.setValue("username", m_username);
573 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800574}
575
576void ChronoShareGui::closeEvent(QCloseEvent* event)
577{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800578 _LOG_DEBUG ("Close Event")
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800579 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800580 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800581}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800582
583#if WAF
584#include "chronosharegui.moc"
585#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800586#endif