blob: 1dbb4023eafb33475c50a00983edbb65713b3de5 [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 Zhu5c2475b2013-02-26 22:57:31 -080040//static const string DOC_ROOT = ":/html";
41static const string DOC_ROOT = "gui/html";
Zhenkai Zhud9429222013-02-25 22:34:09 -080042static const QString ICON_PICTURE_QSTRING(":/images/friends-group-icon.png");
43
Alexander Afanasyev83a53002013-01-24 11:12:01 -080044INIT_LOGGER ("Gui");
45
46ChronoShareGui::ChronoShareGui(QWidget *parent)
Jared Lindblomc8ffd592013-01-25 00:00:30 -080047 : QDialog(parent)
Zhenkai Zhud5756312013-01-31 13:49:45 -080048 , m_watcher(0)
49 , m_dispatcher(0)
Zhenkai Zhud9429222013-02-25 22:34:09 -080050 , m_httpServer(0)
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080051#ifdef ADHOC_SUPPORTED
52 , m_executor (1)
53#endif
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080054#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080055 , m_autoUpdate(new SparkleAutoUpdate(tr("http://irl.cs.ucla.edu/~zhenkai/chronoshare_dist/chronoshare.xml")))
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080056#endif
Jared Lindblom06405c42013-01-17 20:48:39 -080057{
Jared Lindblomf4b900e2013-02-07 18:08:47 -080058 setWindowTitle("Settings");
Jared Lindblomc8ffd592013-01-25 00:00:30 -080059
Jared Lindblomd8b76ad2013-02-07 20:37:37 -080060 labelUsername = new QLabel("Username (hint: /<username>)");
Jared Lindblomc8ffd592013-01-25 00:00:30 -080061 labelSharedFolder = new QLabel("Shared Folder Name");
62 labelSharedFolderPath = new QLabel("Shared Folder Path");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080063
Zhenkai Zhud5756312013-01-31 13:49:45 -080064 QRegExp regex("(/[^/]+)+$");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080065 QValidator *prefixValidator = new QRegExpValidator(regex, this);
66
Jared Lindblomc8ffd592013-01-25 00:00:30 -080067 editUsername = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080068 editUsername->setValidator(prefixValidator);
69
70 QRegExp noPureWhiteSpace("^\\S+.*$");
71 QValidator *wsValidator = new QRegExpValidator(noPureWhiteSpace, this);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080072 editSharedFolder = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080073 editSharedFolder->setValidator(wsValidator);
74
Jared Lindblomc8ffd592013-01-25 00:00:30 -080075 editSharedFolderPath = new QLineEdit();
76 editSharedFolderPath->setReadOnly(true);
77 QPalette pal = editSharedFolderPath->palette();
78 pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Disabled, QPalette::Base));
79 editSharedFolderPath->setPalette(pal);
80 button = new QPushButton("Submit");
81 label = new QLabel();
82
83 connect(button, SIGNAL(clicked()), this, SLOT(changeSettings()));
84
85 mainLayout = new QVBoxLayout; //vertically
86 mainLayout->addWidget(labelUsername);
87 mainLayout->addWidget(editUsername);
88 mainLayout->addWidget(labelSharedFolder);
89 mainLayout->addWidget(editSharedFolder);
90 mainLayout->addWidget(labelSharedFolderPath);
91 mainLayout->addWidget(editSharedFolderPath);
92 mainLayout->addWidget(button);
93 mainLayout->addWidget(label);
94 setLayout(mainLayout);
95
Alexander Afanasyev83a53002013-01-24 11:12:01 -080096 // create actions that result from clicking a menu option
Zhenkai Zhu25e13582013-02-27 15:33:01 -080097 createActionsAndMenu();
Jared Lindblomb4032e22013-01-17 23:50:51 -080098
Alexander Afanasyev83a53002013-01-24 11:12:01 -080099 // create tray icon
100 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800101
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800102 // set icon image
103 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -0800104
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800105 // show tray icon
106 m_trayIcon->show();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800107
108 // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix,
109 // const std::string &sharedFolder, const boost::filesystem::path &rootDir,
110 // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2);
111
Zhenkai Zhud5756312013-01-31 13:49:45 -0800112 // load settings
113 if(!loadSettings())
114 {
115 // prompt user to choose folder
116 openMessageBox("First Time Setup", "Please enter a username, shared folder name and choose the shared folder path on your local filesystem.");
117 viewSettings();
118 openFileDialog();
119 viewSettings();
120 }
121 else
122 {
123 startBackend();
124 }
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800125
126#ifdef ADHOC_SUPPORTED
127 m_executor.start ();
128#endif
Zhenkai Zhud5756312013-01-31 13:49:45 -0800129}
130
131void
132ChronoShareGui::startBackend()
133{
134 if (m_watcher != 0 && m_dispatcher != 0)
135 {
136 return;
137 }
138
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800139 m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (),
140 m_dirPath.toStdString (), make_shared<CcnxWrapper> ());
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800141
142 // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800143 m_watcher = new FsWatcher (m_dirPath,
144 bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1),
Zhenkai Zhud1756272013-02-01 17:02:18 -0800145 bind (&Dispatcher::Did_LocalFile_Delete, m_dispatcher, _1));
Zhenkai Zhud9429222013-02-25 22:34:09 -0800146 QFileInfo indexHtmlInfo(":/html/index.html");
147 if (indexHtmlInfo.exists())
148 {
149 m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT);
150 m_httpServerThread = boost::thread(&http::server::server::run, m_httpServer);
151 }
152 else
153 {
154 _LOG_ERROR ("Http server doc root dir does not exist!");
155 // shall we bail here?
156 }
Jared Lindblom06405c42013-01-17 20:48:39 -0800157}
158
159ChronoShareGui::~ChronoShareGui()
160{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800161#ifdef ADHOC_SUPPORTED
162 m_executor.shutdown ();
163#endif
164
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800165 delete m_watcher; // stop filewatching ASAP
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800166 delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object)
Zhenkai Zhud9429222013-02-25 22:34:09 -0800167 delete m_httpServer;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800168
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800169 // cleanup
170 delete m_trayIcon;
171 delete m_trayIconMenu;
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800172#ifdef ADHOC_SUPPORTED
173 delete m_wifiAction;
174#endif
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800175#ifdef SPARKLE_SUPPORTED
176 delete m_autoUpdate;
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800177 delete m_checkForUpdates;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800178#endif
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800179 delete m_openFolder;
180 delete m_viewSettings;
181 delete m_changeFolder;
182 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800183
184 delete labelUsername;
185 delete labelSharedFolder;
186 delete editUsername;
187 delete editSharedFolder;
188 delete button;
189 delete label;
190 delete mainLayout;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800191}
192
193void ChronoShareGui::openMessageBox(QString title, QString text)
194{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800195 QMessageBox messageBox(this);
196 messageBox.setWindowTitle(title);
197 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800198
Zhenkai Zhud9429222013-02-25 22:34:09 -0800199 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800200
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800201 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800202}
203
204void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
205{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800206 QMessageBox messageBox(this);
207 messageBox.setWindowTitle(title);
208 messageBox.setText(text);
209 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800210
Zhenkai Zhud9429222013-02-25 22:34:09 -0800211 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800212
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800213 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800214}
215
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800216void ChronoShareGui::createActionsAndMenu()
Jared Lindblom06405c42013-01-17 20:48:39 -0800217{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800218 _LOG_DEBUG ("Create actions");
219
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800220 // create the "open folder" action
221 m_openFolder = new QAction(tr("&Open Folder"), this);
222 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800223
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800224 m_openWeb = new QAction(tr("Open in &Browser"), this);
225 connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser()));
226
227 m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this);
228 for (int i = 0; i < 5; i++)
229 {
230 m_fileActions[i] = new QAction(this);
231 m_fileActions[i]->setVisible(false);
232 connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile()));
233 m_recentFilesMenu->addAction(m_fileActions[i]);
234 }
235 connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu()));
236
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800237 // create the "view settings" action
238 m_viewSettings = new QAction(tr("&View Settings"), this);
239 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800240
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800241 // create the "change folder" action
242 m_changeFolder = new QAction(tr("&Change Folder"), this);
243 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800244
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800245#ifdef ADHOC_SUPPORTED
246 // create "AdHoc Wifi" action
247 m_wifiAction = new QAction (tr("Enable AdHoc &WiFI"), m_trayIconMenu);
248 m_wifiAction->setChecked (false);
249 m_wifiAction->setCheckable (true);
250 connect (m_wifiAction, SIGNAL (toggled(bool)), this, SLOT(onAdHocChange(bool)));
251#endif
252
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800253#ifdef SPARKLE_SUPPORTED
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800254 m_checkForUpdates = new QAction (tr("Check For Updates"), this);
255 connect (m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates()));
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800256#endif
257
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800258 // create the "quit program" action
259 m_quitProgram = new QAction(tr("&Quit"), this);
260 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800261}
262
263void ChronoShareGui::createTrayIcon()
264{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800265 // create a new icon menu
266 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800267
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800268 // add actions to the menu
269 m_trayIconMenu->addAction(m_openFolder);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800270 m_trayIconMenu->addAction(m_openWeb);
271 m_trayIconMenu->addMenu(m_recentFilesMenu);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800272 m_trayIconMenu->addSeparator();
273 m_trayIconMenu->addAction(m_viewSettings);
274 m_trayIconMenu->addAction(m_changeFolder);
275 m_trayIconMenu->addSeparator();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800276 m_trayIconMenu->addAction(m_checkForUpdates);
277 m_trayIconMenu->addSeparator();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800278 m_trayIconMenu->addAction(m_wifiAction);
279 m_trayIconMenu->addSeparator();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800280 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800281
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800282 // create new tray icon
283 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800284
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800285#ifdef ADHOC_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800286 // associate the menu with the tray icon
287 m_trayIcon->setContextMenu(m_trayIconMenu);
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800288#endif
Jared Lindblomb4032e22013-01-17 23:50:51 -0800289
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800290 // handle left click of icon
291 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800292}
293
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800294void
295ChronoShareGui::onAdHocChange (bool state)
296{
297#ifdef ADHOC_SUPPORTED
298 if (m_wifiAction->isChecked ())
299 {
300 QMessageBox msgBox;
301 msgBox.setText ("WARNING: your WiFi will be disconnected");
302 msgBox.setIcon (QMessageBox::Warning);
303 msgBox.setInformativeText("AdHoc WiFi will disconnect your current WiFi.\n Are you sure that you are OK with that?");
304 msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
305 msgBox.setDefaultButton(QMessageBox::Cancel);
306 int ret = msgBox.exec();
307
308 if (ret == QMessageBox::Cancel)
309 {
310 m_wifiAction->setChecked (false);
311 }
312 else
313 {
314
315 m_wifiAction->setText (tr("Disable AdHoc WiFI"));
316
317 // create adhoc
318 m_executor.execute (Adhoc::CreateAdhoc);
319 }
320 }
321 else
322 {
323 m_wifiAction->setText (tr("Enable AdHoc WiFI"));
324
325 // disable adhoc
326 m_executor.execute (Adhoc::DestroyAdhoc);
327
328 // a trick in DestroyAdhoc ensures that WiFi will be reconnected to a default WiFi
329 }
330#endif
331}
332
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800333void
334ChronoShareGui::onCheckForUpdates()
335{
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800336#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800337 cout << "+++++++++++ trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800338 m_autoUpdate->checkForUpdates();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800339 cout << "+++++++++++ end trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800340#endif
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800341}
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800342
Jared Lindblom06405c42013-01-17 20:48:39 -0800343void ChronoShareGui::setIcon()
344{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800345 // set the icon image
Zhenkai Zhud9429222013-02-25 22:34:09 -0800346 m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING));
Jared Lindblom06405c42013-01-17 20:48:39 -0800347}
348
349void ChronoShareGui::openSharedFolder()
350{
Zhenkai Zhuce04cb42013-02-25 15:29:31 -0800351 QString path = QDir::toNativeSeparators(m_dirPath);
352 QDesktopServices::openUrl(QUrl("file:///" + path));
Jared Lindblom06405c42013-01-17 20:48:39 -0800353}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800354
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800355void ChronoShareGui::openInWebBrowser()
356{
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800357 QUrl url ("http://localhost:9001/");
358 url.setFragment ("fileList&"
359 "user=" + QUrl::toPercentEncoding (m_username) + "&"
360 "folder=" + QUrl::toPercentEncoding (m_sharedFolderName));
361
362 // i give up. there is double encoding and I have no idea how to fight it...
363 QDesktopServices::openUrl (url);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800364}
365
366void ChronoShareGui::openFile()
367{
368 // figure out who sent the signal
369 QAction *pAction = qobject_cast<QAction*>(sender());
370 if (pAction->isEnabled())
371 {
372 // we stored full path of the file in this toolTip field
373#ifdef Q_WS_MAC
374 // we do some hack so we could show the file in Finder highlighted
375 QStringList args;
376 args << "-e";
377 args << "tell application \"Finder\"";
378 args << "-e";
379 args << "activate";
380 args << "-e";
381 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
382 args << "-e";
383 args << "end tell";
384 QProcess::startDetached("osascript", args);
385#else
386 // too bad qt couldn't do highlighting for Linux (or Mac)
387 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
388#endif
389 }
390}
391
392void ChronoShareGui::updateRecentFilesMenu()
393{
394 for (int i = 0; i < 5; i++)
395 {
396 m_fileActions[i]->setVisible(false);
397 }
398 m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5);
399}
400
401void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index)
402{
403 QString fullPath = m_dirPath + "/" + filename.c_str();
404 QFileInfo fileInfo(fullPath);
405 if (fileInfo.exists())
406 {
407 // This is a hack, we just use some field to store the path
408 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
409 m_fileActions[index]->setEnabled(true);
410 }
411 else
412 {
413 // after half an hour frustrating test and search around,
414 // I think it's the problem of Qt.
415 // According to the Qt doc, the action cannot be clicked
416 // and the area would be grey, but it didn't happen
417 // User can still trigger the action, and not greyed
418 // added check in SLOT to see if the action is "enalbed"
419 // as a remedy
420 // Give up at least for now
421 m_fileActions[index]->setEnabled(false);
422 // UPDATE, file not fetched yet
423 if (action == 0)
424 {
425 QFont font;
426 // supposed by change the font, didn't happen
427 font.setWeight(QFont::Light);
428 m_fileActions[index]->setFont(font);
429 m_fileActions[index]->setToolTip(tr("Fetching..."));
430 }
431 // DELETE
432 else
433 {
434 QFont font;
435 // supposed by change the font, didn't happen
436 font.setStrikeOut(true);
437 m_fileActions[index]->setFont(font);
438 m_fileActions[index]->setToolTip(tr("Deleted..."));
439 }
440 }
441 m_fileActions[index]->setText(fileInfo.fileName());
442 m_fileActions[index]->setVisible(true);
443}
444
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800445void ChronoShareGui::changeSettings()
446{
447 if(!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800448 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800449 else
450 editUsername->setText(m_username);
451
452 if(!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800453 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800454 else
455 editSharedFolder->setText(m_sharedFolderName);
456
457 saveSettings();
458 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800459
460 startBackend();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800461}
462
Jared Lindblomb4032e22013-01-17 23:50:51 -0800463void ChronoShareGui::openFileDialog()
464{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800465 // prompt user for new directory
466 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800467 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
468 QFileInfo qFileInfo (tempPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800469
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800470 if(qFileInfo.isDir())
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800471 {
472 m_dirPath = tempPath;
473 editSharedFolderPath->setText(m_dirPath);
474 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800475 else
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800476 {
477 openMessageBox ("Error", "Not a valid folder, Ignoring.");
478 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800479
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800480 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800481
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800482 // save settings
483 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800484}
485
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800486void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800487{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800488 // if double clicked, open shared folder
489 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800490 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800491 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800492 }
493}
494
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800495void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800496{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800497 //simple for now
498 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800499 this->raise();
500 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800501}
502
503bool ChronoShareGui::loadSettings()
504{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800505 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800506
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800507 // Load Settings
508 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
509 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800510
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800511 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
512
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800513 if(settings.contains("username"))
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800514 {
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800515 m_username = settings.value("username", "admin").toString();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800516 }
517 else
518 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800519 successful = false;
520 }
521
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800522 editUsername->setText(m_username);
523
524 if(settings.contains("sharedfoldername"))
525 {
526 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
527 }
528 else
529 {
530 successful = false;
531 }
532
533 editSharedFolder->setText(m_sharedFolderName);
534
535 if(settings.contains("dirPath"))
536 {
537 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
538 }
539 else
540 {
541 successful = false;
542 }
543
544 editSharedFolderPath->setText(m_dirPath);
545
546 _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800547
548 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800549}
550
551void ChronoShareGui::saveSettings()
552{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800553 // Save Settings
554 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
555 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800556
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800557 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800558 settings.setValue("username", m_username);
559 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800560}
561
562void ChronoShareGui::closeEvent(QCloseEvent* event)
563{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800564 _LOG_DEBUG ("Close Event")
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800565 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800566 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800567}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800568
569#if WAF
570#include "chronosharegui.moc"
571#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800572#endif