blob: 898f6c4349ae0cd2e45a8b0c2d3ae0c0a6117fcb [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{
358 QDesktopServices::openUrl(QUrl("http://localhost:9001"));
359}
360
361void ChronoShareGui::openFile()
362{
363 // figure out who sent the signal
364 QAction *pAction = qobject_cast<QAction*>(sender());
365 if (pAction->isEnabled())
366 {
367 // we stored full path of the file in this toolTip field
368#ifdef Q_WS_MAC
369 // we do some hack so we could show the file in Finder highlighted
370 QStringList args;
371 args << "-e";
372 args << "tell application \"Finder\"";
373 args << "-e";
374 args << "activate";
375 args << "-e";
376 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
377 args << "-e";
378 args << "end tell";
379 QProcess::startDetached("osascript", args);
380#else
381 // too bad qt couldn't do highlighting for Linux (or Mac)
382 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
383#endif
384 }
385}
386
387void ChronoShareGui::updateRecentFilesMenu()
388{
389 for (int i = 0; i < 5; i++)
390 {
391 m_fileActions[i]->setVisible(false);
392 }
393 m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5);
394}
395
396void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index)
397{
398 QString fullPath = m_dirPath + "/" + filename.c_str();
399 QFileInfo fileInfo(fullPath);
400 if (fileInfo.exists())
401 {
402 // This is a hack, we just use some field to store the path
403 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
404 m_fileActions[index]->setEnabled(true);
405 }
406 else
407 {
408 // after half an hour frustrating test and search around,
409 // I think it's the problem of Qt.
410 // According to the Qt doc, the action cannot be clicked
411 // and the area would be grey, but it didn't happen
412 // User can still trigger the action, and not greyed
413 // added check in SLOT to see if the action is "enalbed"
414 // as a remedy
415 // Give up at least for now
416 m_fileActions[index]->setEnabled(false);
417 // UPDATE, file not fetched yet
418 if (action == 0)
419 {
420 QFont font;
421 // supposed by change the font, didn't happen
422 font.setWeight(QFont::Light);
423 m_fileActions[index]->setFont(font);
424 m_fileActions[index]->setToolTip(tr("Fetching..."));
425 }
426 // DELETE
427 else
428 {
429 QFont font;
430 // supposed by change the font, didn't happen
431 font.setStrikeOut(true);
432 m_fileActions[index]->setFont(font);
433 m_fileActions[index]->setToolTip(tr("Deleted..."));
434 }
435 }
436 m_fileActions[index]->setText(fileInfo.fileName());
437 m_fileActions[index]->setVisible(true);
438}
439
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800440void ChronoShareGui::changeSettings()
441{
442 if(!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800443 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800444 else
445 editUsername->setText(m_username);
446
447 if(!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800448 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800449 else
450 editSharedFolder->setText(m_sharedFolderName);
451
452 saveSettings();
453 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800454
455 startBackend();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800456}
457
Jared Lindblomb4032e22013-01-17 23:50:51 -0800458void ChronoShareGui::openFileDialog()
459{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800460 // prompt user for new directory
461 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800462 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
463 QFileInfo qFileInfo (tempPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800464
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800465 if(qFileInfo.isDir())
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800466 {
467 m_dirPath = tempPath;
468 editSharedFolderPath->setText(m_dirPath);
469 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800470 else
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800471 {
472 openMessageBox ("Error", "Not a valid folder, Ignoring.");
473 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800474
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800475 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800476
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800477 // save settings
478 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800479}
480
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800481void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800482{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800483 // if double clicked, open shared folder
484 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800485 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800486 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800487 }
488}
489
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800490void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800491{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800492 //simple for now
493 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800494 this->raise();
495 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800496}
497
498bool ChronoShareGui::loadSettings()
499{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800500 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800501
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800502 // Load Settings
503 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
504 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800505
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800506 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
507
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800508 if(settings.contains("username"))
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800509 {
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800510 m_username = settings.value("username", "admin").toString();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800511 }
512 else
513 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800514 successful = false;
515 }
516
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800517 editUsername->setText(m_username);
518
519 if(settings.contains("sharedfoldername"))
520 {
521 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
522 }
523 else
524 {
525 successful = false;
526 }
527
528 editSharedFolder->setText(m_sharedFolderName);
529
530 if(settings.contains("dirPath"))
531 {
532 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
533 }
534 else
535 {
536 successful = false;
537 }
538
539 editSharedFolderPath->setText(m_dirPath);
540
541 _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800542
543 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800544}
545
546void ChronoShareGui::saveSettings()
547{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800548 // Save Settings
549 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
550 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800551
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800552 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800553 settings.setValue("username", m_username);
554 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800555}
556
557void ChronoShareGui::closeEvent(QCloseEvent* event)
558{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800559 _LOG_DEBUG ("Close Event")
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800560 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800561 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800562}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800563
564#if WAF
565#include "chronosharegui.moc"
566#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800567#endif