blob: cc459a5f61aa62f3c90f26e38a82fc73fe700cc6 [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");
Zhenkai Zhu630d0f12013-02-28 17:21:54 -080081
82 QString versionString = QString("Version: ChronoShare v%1").arg(CHRONOSHARE_VERSION);
83 label = new QLabel(versionString, this);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080084
85 connect(button, SIGNAL(clicked()), this, SLOT(changeSettings()));
86
87 mainLayout = new QVBoxLayout; //vertically
88 mainLayout->addWidget(labelUsername);
89 mainLayout->addWidget(editUsername);
90 mainLayout->addWidget(labelSharedFolder);
91 mainLayout->addWidget(editSharedFolder);
92 mainLayout->addWidget(labelSharedFolderPath);
93 mainLayout->addWidget(editSharedFolderPath);
94 mainLayout->addWidget(button);
95 mainLayout->addWidget(label);
96 setLayout(mainLayout);
97
Alexander Afanasyev83a53002013-01-24 11:12:01 -080098 // create actions that result from clicking a menu option
Zhenkai Zhu25e13582013-02-27 15:33:01 -080099 createActionsAndMenu();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800100
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800101 // create tray icon
102 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800103
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800104 // set icon image
105 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -0800106
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800107 // show tray icon
108 m_trayIcon->show();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800109
110 // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix,
111 // const std::string &sharedFolder, const boost::filesystem::path &rootDir,
112 // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2);
113
Zhenkai Zhud5756312013-01-31 13:49:45 -0800114 // load settings
115 if(!loadSettings())
116 {
117 // prompt user to choose folder
118 openMessageBox("First Time Setup", "Please enter a username, shared folder name and choose the shared folder path on your local filesystem.");
119 viewSettings();
120 openFileDialog();
121 viewSettings();
122 }
123 else
124 {
125 startBackend();
126 }
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800127
128#ifdef ADHOC_SUPPORTED
129 m_executor.start ();
130#endif
Zhenkai Zhud5756312013-01-31 13:49:45 -0800131}
132
133void
134ChronoShareGui::startBackend()
135{
136 if (m_watcher != 0 && m_dispatcher != 0)
137 {
138 return;
139 }
140
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800141 m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (),
142 m_dirPath.toStdString (), make_shared<CcnxWrapper> ());
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800143
144 // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800145 m_watcher = new FsWatcher (m_dirPath,
146 bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1),
Zhenkai Zhud1756272013-02-01 17:02:18 -0800147 bind (&Dispatcher::Did_LocalFile_Delete, m_dispatcher, _1));
Zhenkai Zhud9429222013-02-25 22:34:09 -0800148 QFileInfo indexHtmlInfo(":/html/index.html");
149 if (indexHtmlInfo.exists())
150 {
151 m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT);
152 m_httpServerThread = boost::thread(&http::server::server::run, m_httpServer);
153 }
154 else
155 {
156 _LOG_ERROR ("Http server doc root dir does not exist!");
157 // shall we bail here?
158 }
Jared Lindblom06405c42013-01-17 20:48:39 -0800159}
160
161ChronoShareGui::~ChronoShareGui()
162{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800163#ifdef ADHOC_SUPPORTED
164 m_executor.shutdown ();
165#endif
166
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800167 delete m_watcher; // stop filewatching ASAP
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800168 delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object)
Zhenkai Zhu377cbeb2013-02-28 16:36:26 -0800169 m_httpServer->handle_stop();
170 m_httpServerThread.join();
Zhenkai Zhud9429222013-02-25 22:34:09 -0800171 delete m_httpServer;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800172
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800173 // cleanup
174 delete m_trayIcon;
175 delete m_trayIconMenu;
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800176#ifdef ADHOC_SUPPORTED
177 delete m_wifiAction;
178#endif
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800179#ifdef SPARKLE_SUPPORTED
180 delete m_autoUpdate;
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800181 delete m_checkForUpdates;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800182#endif
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800183 delete m_openFolder;
184 delete m_viewSettings;
185 delete m_changeFolder;
186 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800187
188 delete labelUsername;
189 delete labelSharedFolder;
190 delete editUsername;
191 delete editSharedFolder;
192 delete button;
193 delete label;
194 delete mainLayout;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800195}
196
197void ChronoShareGui::openMessageBox(QString title, QString text)
198{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800199 QMessageBox messageBox(this);
200 messageBox.setWindowTitle(title);
201 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800202
Zhenkai Zhud9429222013-02-25 22:34:09 -0800203 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800204
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800205 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800206}
207
208void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
209{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800210 QMessageBox messageBox(this);
211 messageBox.setWindowTitle(title);
212 messageBox.setText(text);
213 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800214
Zhenkai Zhud9429222013-02-25 22:34:09 -0800215 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800216
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800217 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800218}
219
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800220void ChronoShareGui::createActionsAndMenu()
Jared Lindblom06405c42013-01-17 20:48:39 -0800221{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800222 _LOG_DEBUG ("Create actions");
223
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800224 // create the "open folder" action
225 m_openFolder = new QAction(tr("&Open Folder"), this);
226 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800227
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800228 m_openWeb = new QAction(tr("Open in &Browser"), this);
229 connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser()));
230
231 m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this);
232 for (int i = 0; i < 5; i++)
233 {
234 m_fileActions[i] = new QAction(this);
235 m_fileActions[i]->setVisible(false);
236 connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile()));
237 m_recentFilesMenu->addAction(m_fileActions[i]);
238 }
239 connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu()));
240
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800241 // create the "view settings" action
242 m_viewSettings = new QAction(tr("&View Settings"), this);
243 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800244
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800245 // create the "change folder" action
246 m_changeFolder = new QAction(tr("&Change Folder"), this);
247 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800248
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800249#ifdef ADHOC_SUPPORTED
250 // create "AdHoc Wifi" action
251 m_wifiAction = new QAction (tr("Enable AdHoc &WiFI"), m_trayIconMenu);
252 m_wifiAction->setChecked (false);
253 m_wifiAction->setCheckable (true);
254 connect (m_wifiAction, SIGNAL (toggled(bool)), this, SLOT(onAdHocChange(bool)));
255#endif
256
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800257#ifdef SPARKLE_SUPPORTED
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800258 m_checkForUpdates = new QAction (tr("Check For Updates"), this);
259 connect (m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates()));
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800260#endif
261
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800262 // create the "quit program" action
263 m_quitProgram = new QAction(tr("&Quit"), this);
264 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800265}
266
267void ChronoShareGui::createTrayIcon()
268{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800269 // create a new icon menu
270 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800271
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800272 // add actions to the menu
273 m_trayIconMenu->addAction(m_openFolder);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800274 m_trayIconMenu->addAction(m_openWeb);
275 m_trayIconMenu->addMenu(m_recentFilesMenu);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800276 m_trayIconMenu->addSeparator();
277 m_trayIconMenu->addAction(m_viewSettings);
278 m_trayIconMenu->addAction(m_changeFolder);
279 m_trayIconMenu->addSeparator();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800280 m_trayIconMenu->addAction(m_checkForUpdates);
281 m_trayIconMenu->addSeparator();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800282 m_trayIconMenu->addAction(m_wifiAction);
283 m_trayIconMenu->addSeparator();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800284 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800285
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800286 // create new tray icon
287 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800288
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800289#ifdef ADHOC_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800290 // associate the menu with the tray icon
291 m_trayIcon->setContextMenu(m_trayIconMenu);
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800292#endif
Jared Lindblomb4032e22013-01-17 23:50:51 -0800293
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800294 // handle left click of icon
295 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800296}
297
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800298void
299ChronoShareGui::onAdHocChange (bool state)
300{
301#ifdef ADHOC_SUPPORTED
302 if (m_wifiAction->isChecked ())
303 {
304 QMessageBox msgBox;
305 msgBox.setText ("WARNING: your WiFi will be disconnected");
306 msgBox.setIcon (QMessageBox::Warning);
307 msgBox.setInformativeText("AdHoc WiFi will disconnect your current WiFi.\n Are you sure that you are OK with that?");
308 msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
309 msgBox.setDefaultButton(QMessageBox::Cancel);
310 int ret = msgBox.exec();
311
312 if (ret == QMessageBox::Cancel)
313 {
314 m_wifiAction->setChecked (false);
315 }
316 else
317 {
318
319 m_wifiAction->setText (tr("Disable AdHoc WiFI"));
320
321 // create adhoc
322 m_executor.execute (Adhoc::CreateAdhoc);
323 }
324 }
325 else
326 {
327 m_wifiAction->setText (tr("Enable AdHoc WiFI"));
328
329 // disable adhoc
330 m_executor.execute (Adhoc::DestroyAdhoc);
331
332 // a trick in DestroyAdhoc ensures that WiFi will be reconnected to a default WiFi
333 }
334#endif
335}
336
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800337void
338ChronoShareGui::onCheckForUpdates()
339{
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800340#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800341 cout << "+++++++++++ trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800342 m_autoUpdate->checkForUpdates();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800343 cout << "+++++++++++ end trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800344#endif
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800345}
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800346
Jared Lindblom06405c42013-01-17 20:48:39 -0800347void ChronoShareGui::setIcon()
348{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800349 // set the icon image
Zhenkai Zhud9429222013-02-25 22:34:09 -0800350 m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING));
Jared Lindblom06405c42013-01-17 20:48:39 -0800351}
352
353void ChronoShareGui::openSharedFolder()
354{
Zhenkai Zhuce04cb42013-02-25 15:29:31 -0800355 QString path = QDir::toNativeSeparators(m_dirPath);
356 QDesktopServices::openUrl(QUrl("file:///" + path));
Jared Lindblom06405c42013-01-17 20:48:39 -0800357}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800358
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800359void ChronoShareGui::openInWebBrowser()
360{
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800361 QUrl url ("http://localhost:9001/");
362 url.setFragment ("fileList&"
363 "user=" + QUrl::toPercentEncoding (m_username) + "&"
364 "folder=" + QUrl::toPercentEncoding (m_sharedFolderName));
365
366 // i give up. there is double encoding and I have no idea how to fight it...
367 QDesktopServices::openUrl (url);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800368}
369
370void ChronoShareGui::openFile()
371{
372 // figure out who sent the signal
373 QAction *pAction = qobject_cast<QAction*>(sender());
374 if (pAction->isEnabled())
375 {
376 // we stored full path of the file in this toolTip field
377#ifdef Q_WS_MAC
378 // we do some hack so we could show the file in Finder highlighted
379 QStringList args;
380 args << "-e";
381 args << "tell application \"Finder\"";
382 args << "-e";
383 args << "activate";
384 args << "-e";
385 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
386 args << "-e";
387 args << "end tell";
388 QProcess::startDetached("osascript", args);
389#else
390 // too bad qt couldn't do highlighting for Linux (or Mac)
391 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
392#endif
393 }
394}
395
396void ChronoShareGui::updateRecentFilesMenu()
397{
398 for (int i = 0; i < 5; i++)
399 {
400 m_fileActions[i]->setVisible(false);
401 }
402 m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5);
403}
404
405void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index)
406{
407 QString fullPath = m_dirPath + "/" + filename.c_str();
408 QFileInfo fileInfo(fullPath);
409 if (fileInfo.exists())
410 {
411 // This is a hack, we just use some field to store the path
412 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
413 m_fileActions[index]->setEnabled(true);
414 }
415 else
416 {
417 // after half an hour frustrating test and search around,
418 // I think it's the problem of Qt.
419 // According to the Qt doc, the action cannot be clicked
420 // and the area would be grey, but it didn't happen
421 // User can still trigger the action, and not greyed
422 // added check in SLOT to see if the action is "enalbed"
423 // as a remedy
424 // Give up at least for now
425 m_fileActions[index]->setEnabled(false);
426 // UPDATE, file not fetched yet
427 if (action == 0)
428 {
429 QFont font;
430 // supposed by change the font, didn't happen
431 font.setWeight(QFont::Light);
432 m_fileActions[index]->setFont(font);
433 m_fileActions[index]->setToolTip(tr("Fetching..."));
434 }
435 // DELETE
436 else
437 {
438 QFont font;
439 // supposed by change the font, didn't happen
440 font.setStrikeOut(true);
441 m_fileActions[index]->setFont(font);
442 m_fileActions[index]->setToolTip(tr("Deleted..."));
443 }
444 }
445 m_fileActions[index]->setText(fileInfo.fileName());
446 m_fileActions[index]->setVisible(true);
447}
448
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800449void ChronoShareGui::changeSettings()
450{
451 if(!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800452 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800453 else
454 editUsername->setText(m_username);
455
456 if(!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800457 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800458 else
459 editSharedFolder->setText(m_sharedFolderName);
460
461 saveSettings();
462 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800463
464 startBackend();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800465}
466
Jared Lindblomb4032e22013-01-17 23:50:51 -0800467void ChronoShareGui::openFileDialog()
468{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800469 // prompt user for new directory
470 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800471 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
472 QFileInfo qFileInfo (tempPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800473
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800474 if(qFileInfo.isDir())
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800475 {
476 m_dirPath = tempPath;
477 editSharedFolderPath->setText(m_dirPath);
478 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800479 else
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800480 {
481 openMessageBox ("Error", "Not a valid folder, Ignoring.");
482 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800483
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800484 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800485
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800486 // save settings
487 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800488}
489
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800490void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800491{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800492 // if double clicked, open shared folder
493 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800494 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800495 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800496 }
497}
498
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800499void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800500{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800501 //simple for now
502 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800503 this->raise();
504 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800505}
506
507bool ChronoShareGui::loadSettings()
508{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800509 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800510
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800511 // Load Settings
512 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
513 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800514
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800515 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
516
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800517 if(settings.contains("username"))
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800518 {
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800519 m_username = settings.value("username", "admin").toString();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800520 }
521 else
522 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800523 successful = false;
524 }
525
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800526 editUsername->setText(m_username);
527
528 if(settings.contains("sharedfoldername"))
529 {
530 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
531 }
532 else
533 {
534 successful = false;
535 }
536
537 editSharedFolder->setText(m_sharedFolderName);
538
539 if(settings.contains("dirPath"))
540 {
541 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
542 }
543 else
544 {
545 successful = false;
546 }
547
548 editSharedFolderPath->setText(m_dirPath);
549
550 _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800551
552 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800553}
554
555void ChronoShareGui::saveSettings()
556{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800557 // Save Settings
558 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
559 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800560
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800561 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800562 settings.setValue("username", m_username);
563 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800564}
565
566void ChronoShareGui::closeEvent(QCloseEvent* event)
567{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800568 _LOG_DEBUG ("Close Event")
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800569 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800570 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800571}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800572
573#if WAF
574#include "chronosharegui.moc"
575#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800576#endif