blob: cdc5dc0405b7625155328db07e860c97fade700e [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 Zhu3857d712013-02-27 22:51:25 -080055 , m_autoUpdate(new SparkleAutoUpdate(tr("http://irl.cs.ucla.edu")))
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 Zhu907cbaf2013-02-27 22:29:23 -0800167 m_httpServer->handle_stop();
Zhenkai Zhud9429222013-02-25 22:34:09 -0800168 delete m_httpServer;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800169
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800170 // cleanup
171 delete m_trayIcon;
172 delete m_trayIconMenu;
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800173#ifdef ADHOC_SUPPORTED
174 delete m_wifiAction;
175#endif
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800176#ifdef SPARKLE_SUPPORTED
177 delete m_autoUpdate;
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800178 delete m_checkForUpdates;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800179#endif
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800180 delete m_openFolder;
181 delete m_viewSettings;
182 delete m_changeFolder;
183 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800184
185 delete labelUsername;
186 delete labelSharedFolder;
187 delete editUsername;
188 delete editSharedFolder;
189 delete button;
190 delete label;
191 delete mainLayout;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800192}
193
194void ChronoShareGui::openMessageBox(QString title, QString text)
195{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800196 QMessageBox messageBox(this);
197 messageBox.setWindowTitle(title);
198 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800199
Zhenkai Zhud9429222013-02-25 22:34:09 -0800200 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800201
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800202 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800203}
204
205void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
206{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800207 QMessageBox messageBox(this);
208 messageBox.setWindowTitle(title);
209 messageBox.setText(text);
210 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800211
Zhenkai Zhud9429222013-02-25 22:34:09 -0800212 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800213
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800214 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800215}
216
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800217void ChronoShareGui::createActionsAndMenu()
Jared Lindblom06405c42013-01-17 20:48:39 -0800218{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800219 _LOG_DEBUG ("Create actions");
220
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800221 // create the "open folder" action
222 m_openFolder = new QAction(tr("&Open Folder"), this);
223 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800224
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800225 m_openWeb = new QAction(tr("Open in &Browser"), this);
226 connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser()));
227
228 m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this);
229 for (int i = 0; i < 5; i++)
230 {
231 m_fileActions[i] = new QAction(this);
232 m_fileActions[i]->setVisible(false);
233 connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile()));
234 m_recentFilesMenu->addAction(m_fileActions[i]);
235 }
236 connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu()));
237
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800238 // create the "view settings" action
239 m_viewSettings = new QAction(tr("&View Settings"), this);
240 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800241
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800242 // create the "change folder" action
243 m_changeFolder = new QAction(tr("&Change Folder"), this);
244 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800245
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800246#ifdef ADHOC_SUPPORTED
247 // create "AdHoc Wifi" action
248 m_wifiAction = new QAction (tr("Enable AdHoc &WiFI"), m_trayIconMenu);
249 m_wifiAction->setChecked (false);
250 m_wifiAction->setCheckable (true);
251 connect (m_wifiAction, SIGNAL (toggled(bool)), this, SLOT(onAdHocChange(bool)));
252#endif
253
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800254#ifdef SPARKLE_SUPPORTED
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800255 m_checkForUpdates = new QAction (tr("Check For Updates"), this);
256 connect (m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates()));
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800257#endif
258
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800259 // create the "quit program" action
260 m_quitProgram = new QAction(tr("&Quit"), this);
261 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800262}
263
264void ChronoShareGui::createTrayIcon()
265{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800266 // create a new icon menu
267 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800268
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800269 // add actions to the menu
270 m_trayIconMenu->addAction(m_openFolder);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800271 m_trayIconMenu->addAction(m_openWeb);
272 m_trayIconMenu->addMenu(m_recentFilesMenu);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800273 m_trayIconMenu->addSeparator();
274 m_trayIconMenu->addAction(m_viewSettings);
275 m_trayIconMenu->addAction(m_changeFolder);
276 m_trayIconMenu->addSeparator();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800277 m_trayIconMenu->addAction(m_checkForUpdates);
278 m_trayIconMenu->addSeparator();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800279 m_trayIconMenu->addAction(m_wifiAction);
280 m_trayIconMenu->addSeparator();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800281 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800282
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800283 // create new tray icon
284 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800285
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800286#ifdef ADHOC_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800287 // associate the menu with the tray icon
288 m_trayIcon->setContextMenu(m_trayIconMenu);
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800289#endif
Jared Lindblomb4032e22013-01-17 23:50:51 -0800290
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800291 // handle left click of icon
292 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800293}
294
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800295void
296ChronoShareGui::onAdHocChange (bool state)
297{
298#ifdef ADHOC_SUPPORTED
299 if (m_wifiAction->isChecked ())
300 {
301 QMessageBox msgBox;
302 msgBox.setText ("WARNING: your WiFi will be disconnected");
303 msgBox.setIcon (QMessageBox::Warning);
304 msgBox.setInformativeText("AdHoc WiFi will disconnect your current WiFi.\n Are you sure that you are OK with that?");
305 msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
306 msgBox.setDefaultButton(QMessageBox::Cancel);
307 int ret = msgBox.exec();
308
309 if (ret == QMessageBox::Cancel)
310 {
311 m_wifiAction->setChecked (false);
312 }
313 else
314 {
315
316 m_wifiAction->setText (tr("Disable AdHoc WiFI"));
317
318 // create adhoc
319 m_executor.execute (Adhoc::CreateAdhoc);
320 }
321 }
322 else
323 {
324 m_wifiAction->setText (tr("Enable AdHoc WiFI"));
325
326 // disable adhoc
327 m_executor.execute (Adhoc::DestroyAdhoc);
328
329 // a trick in DestroyAdhoc ensures that WiFi will be reconnected to a default WiFi
330 }
331#endif
332}
333
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800334void
335ChronoShareGui::onCheckForUpdates()
336{
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800337#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800338 cout << "+++++++++++ trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800339 m_autoUpdate->checkForUpdates();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800340 cout << "+++++++++++ end trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800341#endif
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800342}
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800343
Jared Lindblom06405c42013-01-17 20:48:39 -0800344void ChronoShareGui::setIcon()
345{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800346 // set the icon image
Zhenkai Zhud9429222013-02-25 22:34:09 -0800347 m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING));
Jared Lindblom06405c42013-01-17 20:48:39 -0800348}
349
350void ChronoShareGui::openSharedFolder()
351{
Zhenkai Zhuce04cb42013-02-25 15:29:31 -0800352 QString path = QDir::toNativeSeparators(m_dirPath);
353 QDesktopServices::openUrl(QUrl("file:///" + path));
Jared Lindblom06405c42013-01-17 20:48:39 -0800354}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800355
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800356void ChronoShareGui::openInWebBrowser()
357{
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800358 QUrl url ("http://localhost:9001/");
359 url.setFragment ("fileList&"
360 "user=" + QUrl::toPercentEncoding (m_username) + "&"
361 "folder=" + QUrl::toPercentEncoding (m_sharedFolderName));
362
363 // i give up. there is double encoding and I have no idea how to fight it...
364 QDesktopServices::openUrl (url);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800365}
366
367void ChronoShareGui::openFile()
368{
369 // figure out who sent the signal
370 QAction *pAction = qobject_cast<QAction*>(sender());
371 if (pAction->isEnabled())
372 {
373 // we stored full path of the file in this toolTip field
374#ifdef Q_WS_MAC
375 // we do some hack so we could show the file in Finder highlighted
376 QStringList args;
377 args << "-e";
378 args << "tell application \"Finder\"";
379 args << "-e";
380 args << "activate";
381 args << "-e";
382 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
383 args << "-e";
384 args << "end tell";
385 QProcess::startDetached("osascript", args);
386#else
387 // too bad qt couldn't do highlighting for Linux (or Mac)
388 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
389#endif
390 }
391}
392
393void ChronoShareGui::updateRecentFilesMenu()
394{
395 for (int i = 0; i < 5; i++)
396 {
397 m_fileActions[i]->setVisible(false);
398 }
399 m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5);
400}
401
402void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index)
403{
404 QString fullPath = m_dirPath + "/" + filename.c_str();
405 QFileInfo fileInfo(fullPath);
406 if (fileInfo.exists())
407 {
408 // This is a hack, we just use some field to store the path
409 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
410 m_fileActions[index]->setEnabled(true);
411 }
412 else
413 {
414 // after half an hour frustrating test and search around,
415 // I think it's the problem of Qt.
416 // According to the Qt doc, the action cannot be clicked
417 // and the area would be grey, but it didn't happen
418 // User can still trigger the action, and not greyed
419 // added check in SLOT to see if the action is "enalbed"
420 // as a remedy
421 // Give up at least for now
422 m_fileActions[index]->setEnabled(false);
423 // UPDATE, file not fetched yet
424 if (action == 0)
425 {
426 QFont font;
427 // supposed by change the font, didn't happen
428 font.setWeight(QFont::Light);
429 m_fileActions[index]->setFont(font);
430 m_fileActions[index]->setToolTip(tr("Fetching..."));
431 }
432 // DELETE
433 else
434 {
435 QFont font;
436 // supposed by change the font, didn't happen
437 font.setStrikeOut(true);
438 m_fileActions[index]->setFont(font);
439 m_fileActions[index]->setToolTip(tr("Deleted..."));
440 }
441 }
442 m_fileActions[index]->setText(fileInfo.fileName());
443 m_fileActions[index]->setVisible(true);
444}
445
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800446void ChronoShareGui::changeSettings()
447{
448 if(!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800449 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800450 else
451 editUsername->setText(m_username);
452
453 if(!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800454 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800455 else
456 editSharedFolder->setText(m_sharedFolderName);
457
458 saveSettings();
459 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800460
461 startBackend();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800462}
463
Jared Lindblomb4032e22013-01-17 23:50:51 -0800464void ChronoShareGui::openFileDialog()
465{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800466 // prompt user for new directory
467 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800468 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
469 QFileInfo qFileInfo (tempPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800470
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800471 if(qFileInfo.isDir())
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800472 {
473 m_dirPath = tempPath;
474 editSharedFolderPath->setText(m_dirPath);
475 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800476 else
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800477 {
478 openMessageBox ("Error", "Not a valid folder, Ignoring.");
479 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800480
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800481 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800482
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800483 // save settings
484 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800485}
486
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800487void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800488{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800489 // if double clicked, open shared folder
490 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800491 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800492 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800493 }
494}
495
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800496void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800497{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800498 //simple for now
499 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800500 this->raise();
501 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800502}
503
504bool ChronoShareGui::loadSettings()
505{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800506 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800507
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800508 // Load Settings
509 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
510 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800511
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800512 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
513
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800514 if(settings.contains("username"))
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800515 {
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800516 m_username = settings.value("username", "admin").toString();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800517 }
518 else
519 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800520 successful = false;
521 }
522
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800523 editUsername->setText(m_username);
524
525 if(settings.contains("sharedfoldername"))
526 {
527 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
528 }
529 else
530 {
531 successful = false;
532 }
533
534 editSharedFolder->setText(m_sharedFolderName);
535
536 if(settings.contains("dirPath"))
537 {
538 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
539 }
540 else
541 {
542 successful = false;
543 }
544
545 editSharedFolderPath->setText(m_dirPath);
546
547 _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800548
549 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800550}
551
552void ChronoShareGui::saveSettings()
553{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800554 // Save Settings
555 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
556 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800557
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800558 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800559 settings.setValue("username", m_username);
560 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800561}
562
563void ChronoShareGui::closeEvent(QCloseEvent* event)
564{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800565 _LOG_DEBUG ("Close Event")
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800566 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800567 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800568}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800569
570#if WAF
571#include "chronosharegui.moc"
572#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800573#endif