blob: 79d7a62e30b02d1e7601fbe17ba938806a8e081b [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);
282 m_trayIconMenu->addSeparator();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800283 m_trayIconMenu->addAction(m_checkForUpdates);
284 m_trayIconMenu->addSeparator();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800285 m_trayIconMenu->addAction(m_wifiAction);
286 m_trayIconMenu->addSeparator();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800287 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800288
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800289 // create new tray icon
290 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800291
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800292#ifdef ADHOC_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800293 // associate the menu with the tray icon
294 m_trayIcon->setContextMenu(m_trayIconMenu);
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800295#endif
Jared Lindblomb4032e22013-01-17 23:50:51 -0800296
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800297 // handle left click of icon
298 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800299}
300
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800301void
302ChronoShareGui::onAdHocChange (bool state)
303{
304#ifdef ADHOC_SUPPORTED
305 if (m_wifiAction->isChecked ())
306 {
307 QMessageBox msgBox;
308 msgBox.setText ("WARNING: your WiFi will be disconnected");
309 msgBox.setIcon (QMessageBox::Warning);
310 msgBox.setInformativeText("AdHoc WiFi will disconnect your current WiFi.\n Are you sure that you are OK with that?");
311 msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
312 msgBox.setDefaultButton(QMessageBox::Cancel);
313 int ret = msgBox.exec();
314
315 if (ret == QMessageBox::Cancel)
316 {
317 m_wifiAction->setChecked (false);
318 }
319 else
320 {
321
322 m_wifiAction->setText (tr("Disable AdHoc WiFI"));
323
324 // create adhoc
325 m_executor.execute (Adhoc::CreateAdhoc);
326 }
327 }
328 else
329 {
330 m_wifiAction->setText (tr("Enable AdHoc WiFI"));
331
332 // disable adhoc
333 m_executor.execute (Adhoc::DestroyAdhoc);
334
335 // a trick in DestroyAdhoc ensures that WiFi will be reconnected to a default WiFi
336 }
337#endif
338}
339
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800340void
341ChronoShareGui::onCheckForUpdates()
342{
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800343#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800344 cout << "+++++++++++ trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800345 m_autoUpdate->checkForUpdates();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800346 cout << "+++++++++++ end trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800347#endif
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800348}
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800349
Jared Lindblom06405c42013-01-17 20:48:39 -0800350void ChronoShareGui::setIcon()
351{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800352 // set the icon image
Zhenkai Zhud9429222013-02-25 22:34:09 -0800353 m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING));
Jared Lindblom06405c42013-01-17 20:48:39 -0800354}
355
356void ChronoShareGui::openSharedFolder()
357{
Zhenkai Zhuce04cb42013-02-25 15:29:31 -0800358 QString path = QDir::toNativeSeparators(m_dirPath);
359 QDesktopServices::openUrl(QUrl("file:///" + path));
Jared Lindblom06405c42013-01-17 20:48:39 -0800360}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800361
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800362void ChronoShareGui::openInWebBrowser()
363{
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800364 QUrl url ("http://localhost:9001/");
365 url.setFragment ("fileList&"
366 "user=" + QUrl::toPercentEncoding (m_username) + "&"
367 "folder=" + QUrl::toPercentEncoding (m_sharedFolderName));
368
369 // i give up. there is double encoding and I have no idea how to fight it...
370 QDesktopServices::openUrl (url);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800371}
372
373void ChronoShareGui::openFile()
374{
375 // figure out who sent the signal
376 QAction *pAction = qobject_cast<QAction*>(sender());
377 if (pAction->isEnabled())
378 {
379 // we stored full path of the file in this toolTip field
380#ifdef Q_WS_MAC
381 // we do some hack so we could show the file in Finder highlighted
382 QStringList args;
383 args << "-e";
384 args << "tell application \"Finder\"";
385 args << "-e";
386 args << "activate";
387 args << "-e";
388 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
389 args << "-e";
390 args << "end tell";
391 QProcess::startDetached("osascript", args);
392#else
393 // too bad qt couldn't do highlighting for Linux (or Mac)
394 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
395#endif
396 }
397}
398
399void ChronoShareGui::updateRecentFilesMenu()
400{
401 for (int i = 0; i < 5; i++)
402 {
403 m_fileActions[i]->setVisible(false);
404 }
405 m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5);
406}
407
408void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index)
409{
410 QString fullPath = m_dirPath + "/" + filename.c_str();
411 QFileInfo fileInfo(fullPath);
412 if (fileInfo.exists())
413 {
414 // This is a hack, we just use some field to store the path
415 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
416 m_fileActions[index]->setEnabled(true);
417 }
418 else
419 {
420 // after half an hour frustrating test and search around,
421 // I think it's the problem of Qt.
422 // According to the Qt doc, the action cannot be clicked
423 // and the area would be grey, but it didn't happen
424 // User can still trigger the action, and not greyed
425 // added check in SLOT to see if the action is "enalbed"
426 // as a remedy
427 // Give up at least for now
428 m_fileActions[index]->setEnabled(false);
429 // UPDATE, file not fetched yet
430 if (action == 0)
431 {
432 QFont font;
433 // supposed by change the font, didn't happen
434 font.setWeight(QFont::Light);
435 m_fileActions[index]->setFont(font);
436 m_fileActions[index]->setToolTip(tr("Fetching..."));
437 }
438 // DELETE
439 else
440 {
441 QFont font;
442 // supposed by change the font, didn't happen
443 font.setStrikeOut(true);
444 m_fileActions[index]->setFont(font);
445 m_fileActions[index]->setToolTip(tr("Deleted..."));
446 }
447 }
448 m_fileActions[index]->setText(fileInfo.fileName());
449 m_fileActions[index]->setVisible(true);
450}
451
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800452void ChronoShareGui::changeSettings()
453{
454 if(!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800455 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800456 else
457 editUsername->setText(m_username);
458
459 if(!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800460 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800461 else
462 editSharedFolder->setText(m_sharedFolderName);
463
464 saveSettings();
465 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800466
467 startBackend();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800468}
469
Jared Lindblomb4032e22013-01-17 23:50:51 -0800470void ChronoShareGui::openFileDialog()
471{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800472 // prompt user for new directory
473 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800474 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
475 QFileInfo qFileInfo (tempPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800476
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800477 if(qFileInfo.isDir())
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800478 {
479 m_dirPath = tempPath;
480 editSharedFolderPath->setText(m_dirPath);
481 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800482 else
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800483 {
484 openMessageBox ("Error", "Not a valid folder, Ignoring.");
485 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800486
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800487 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800488
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800489 // save settings
490 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800491}
492
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800493void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800494{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800495 // if double clicked, open shared folder
496 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800497 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800498 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800499 }
500}
501
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800502void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800503{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800504 //simple for now
505 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800506 this->raise();
507 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800508}
509
510bool ChronoShareGui::loadSettings()
511{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800512 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800513
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800514 // Load Settings
515 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
516 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800517
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800518 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
519
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800520 if(settings.contains("username"))
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800521 {
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800522 m_username = settings.value("username", "admin").toString();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800523 }
524 else
525 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800526 successful = false;
527 }
528
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800529 editUsername->setText(m_username);
530
531 if(settings.contains("sharedfoldername"))
532 {
533 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
534 }
535 else
536 {
537 successful = false;
538 }
539
540 editSharedFolder->setText(m_sharedFolderName);
541
542 if(settings.contains("dirPath"))
543 {
544 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
545 }
546 else
547 {
548 successful = false;
549 }
550
551 editSharedFolderPath->setText(m_dirPath);
552
553 _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800554
555 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800556}
557
558void ChronoShareGui::saveSettings()
559{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800560 // Save Settings
561 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
562 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800563
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800564 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800565 settings.setValue("username", m_username);
566 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800567}
568
569void ChronoShareGui::closeEvent(QCloseEvent* event)
570{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800571 _LOG_DEBUG ("Close Event")
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800572 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800573 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800574}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800575
576#if WAF
577#include "chronosharegui.moc"
578#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800579#endif