blob: 16960b350c223ce3a849db674eac7024f1045b39 [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";
40static const string DOC_ROOT = ":/html";
41static const QString ICON_PICTURE_QSTRING(":/images/friends-group-icon.png");
42
Alexander Afanasyev83a53002013-01-24 11:12:01 -080043INIT_LOGGER ("Gui");
44
45ChronoShareGui::ChronoShareGui(QWidget *parent)
Jared Lindblomc8ffd592013-01-25 00:00:30 -080046 : QDialog(parent)
Zhenkai Zhud5756312013-01-31 13:49:45 -080047 , m_watcher(0)
48 , m_dispatcher(0)
Zhenkai Zhud9429222013-02-25 22:34:09 -080049 , m_httpServer(0)
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080050#ifdef ADHOC_SUPPORTED
51 , m_executor (1)
52#endif
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080053#ifdef SPARKLE_SUPPORTED
Zhenkai Zhubb170d42013-02-25 13:48:59 -080054 , m_autoUpdate(new SparkleAutoUpdate(tr("http://no-url.org")))
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080055#endif
Jared Lindblom06405c42013-01-17 20:48:39 -080056{
Jared Lindblomf4b900e2013-02-07 18:08:47 -080057 setWindowTitle("Settings");
Jared Lindblomc8ffd592013-01-25 00:00:30 -080058
Jared Lindblomd8b76ad2013-02-07 20:37:37 -080059 labelUsername = new QLabel("Username (hint: /<username>)");
Jared Lindblomc8ffd592013-01-25 00:00:30 -080060 labelSharedFolder = new QLabel("Shared Folder Name");
61 labelSharedFolderPath = new QLabel("Shared Folder Path");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080062
Zhenkai Zhud5756312013-01-31 13:49:45 -080063 QRegExp regex("(/[^/]+)+$");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080064 QValidator *prefixValidator = new QRegExpValidator(regex, this);
65
Jared Lindblomc8ffd592013-01-25 00:00:30 -080066 editUsername = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080067 editUsername->setValidator(prefixValidator);
68
69 QRegExp noPureWhiteSpace("^\\S+.*$");
70 QValidator *wsValidator = new QRegExpValidator(noPureWhiteSpace, this);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080071 editSharedFolder = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080072 editSharedFolder->setValidator(wsValidator);
73
Jared Lindblomc8ffd592013-01-25 00:00:30 -080074 editSharedFolderPath = new QLineEdit();
75 editSharedFolderPath->setReadOnly(true);
76 QPalette pal = editSharedFolderPath->palette();
77 pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Disabled, QPalette::Base));
78 editSharedFolderPath->setPalette(pal);
79 button = new QPushButton("Submit");
80 label = new QLabel();
81
82 connect(button, SIGNAL(clicked()), this, SLOT(changeSettings()));
83
84 mainLayout = new QVBoxLayout; //vertically
85 mainLayout->addWidget(labelUsername);
86 mainLayout->addWidget(editUsername);
87 mainLayout->addWidget(labelSharedFolder);
88 mainLayout->addWidget(editSharedFolder);
89 mainLayout->addWidget(labelSharedFolderPath);
90 mainLayout->addWidget(editSharedFolderPath);
91 mainLayout->addWidget(button);
92 mainLayout->addWidget(label);
93 setLayout(mainLayout);
94
Alexander Afanasyev83a53002013-01-24 11:12:01 -080095 // create actions that result from clicking a menu option
Zhenkai Zhu25e13582013-02-27 15:33:01 -080096 createActionsAndMenu();
Jared Lindblomb4032e22013-01-17 23:50:51 -080097
Alexander Afanasyev83a53002013-01-24 11:12:01 -080098 // create tray icon
99 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800100
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800101 // set icon image
102 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -0800103
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800104 // show tray icon
105 m_trayIcon->show();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800106
107 // Dispatcher(const boost::filesystem::path &path, const std::string &localUserName, const Ccnx::Name &localPrefix,
108 // const std::string &sharedFolder, const boost::filesystem::path &rootDir,
109 // Ccnx::CcnxWrapperPtr ccnx, SchedulerPtr scheduler, int poolSize = 2);
110
Zhenkai Zhud5756312013-01-31 13:49:45 -0800111 // load settings
112 if(!loadSettings())
113 {
114 // prompt user to choose folder
115 openMessageBox("First Time Setup", "Please enter a username, shared folder name and choose the shared folder path on your local filesystem.");
116 viewSettings();
117 openFileDialog();
118 viewSettings();
119 }
120 else
121 {
122 startBackend();
123 }
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800124
125#ifdef ADHOC_SUPPORTED
126 m_executor.start ();
127#endif
Zhenkai Zhud5756312013-01-31 13:49:45 -0800128}
129
130void
131ChronoShareGui::startBackend()
132{
133 if (m_watcher != 0 && m_dispatcher != 0)
134 {
135 return;
136 }
137
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800138 m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (),
139 m_dirPath.toStdString (), make_shared<CcnxWrapper> ());
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800140
141 // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800142 m_watcher = new FsWatcher (m_dirPath,
143 bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1),
Zhenkai Zhud1756272013-02-01 17:02:18 -0800144 bind (&Dispatcher::Did_LocalFile_Delete, m_dispatcher, _1));
Zhenkai Zhud9429222013-02-25 22:34:09 -0800145 QFileInfo indexHtmlInfo(":/html/index.html");
146 if (indexHtmlInfo.exists())
147 {
148 m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT);
149 m_httpServerThread = boost::thread(&http::server::server::run, m_httpServer);
150 }
151 else
152 {
153 _LOG_ERROR ("Http server doc root dir does not exist!");
154 // shall we bail here?
155 }
Jared Lindblom06405c42013-01-17 20:48:39 -0800156}
157
158ChronoShareGui::~ChronoShareGui()
159{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800160#ifdef ADHOC_SUPPORTED
161 m_executor.shutdown ();
162#endif
163
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800164 delete m_watcher; // stop filewatching ASAP
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800165 delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object)
Zhenkai Zhud9429222013-02-25 22:34:09 -0800166 delete m_httpServer;
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800167
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800168 // cleanup
169 delete m_trayIcon;
170 delete m_trayIconMenu;
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800171#ifdef ADHOC_SUPPORTED
172 delete m_wifiAction;
173#endif
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800174#ifdef SPARKLE_SUPPORTED
175 delete m_autoUpdate;
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800176 delete m_checkForUpdates;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800177#endif
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800178 delete m_openFolder;
179 delete m_viewSettings;
180 delete m_changeFolder;
181 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800182
183 delete labelUsername;
184 delete labelSharedFolder;
185 delete editUsername;
186 delete editSharedFolder;
187 delete button;
188 delete label;
189 delete mainLayout;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800190}
191
192void ChronoShareGui::openMessageBox(QString title, QString text)
193{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800194 QMessageBox messageBox(this);
195 messageBox.setWindowTitle(title);
196 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800197
Zhenkai Zhud9429222013-02-25 22:34:09 -0800198 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800199
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800200 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800201}
202
203void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
204{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800205 QMessageBox messageBox(this);
206 messageBox.setWindowTitle(title);
207 messageBox.setText(text);
208 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800209
Zhenkai Zhud9429222013-02-25 22:34:09 -0800210 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800211
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800212 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800213}
214
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800215void ChronoShareGui::createActionsAndMenu()
Jared Lindblom06405c42013-01-17 20:48:39 -0800216{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800217 _LOG_DEBUG ("Create actions");
218
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800219 // create the "open folder" action
220 m_openFolder = new QAction(tr("&Open Folder"), this);
221 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800222
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800223 m_openWeb = new QAction(tr("Open in &Browser"), this);
224 connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser()));
225
226 m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this);
227 for (int i = 0; i < 5; i++)
228 {
229 m_fileActions[i] = new QAction(this);
230 m_fileActions[i]->setVisible(false);
231 connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile()));
232 m_recentFilesMenu->addAction(m_fileActions[i]);
233 }
234 connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu()));
235
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800236 // create the "view settings" action
237 m_viewSettings = new QAction(tr("&View Settings"), this);
238 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800239
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800240 // create the "change folder" action
241 m_changeFolder = new QAction(tr("&Change Folder"), this);
242 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800243
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800244#ifdef ADHOC_SUPPORTED
245 // create "AdHoc Wifi" action
246 m_wifiAction = new QAction (tr("Enable AdHoc &WiFI"), m_trayIconMenu);
247 m_wifiAction->setChecked (false);
248 m_wifiAction->setCheckable (true);
249 connect (m_wifiAction, SIGNAL (toggled(bool)), this, SLOT(onAdHocChange(bool)));
250#endif
251
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800252#ifdef SPARKLE_SUPPORTED
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800253 m_checkForUpdates = new QAction (tr("Check For Updates"), this);
254 connect (m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates()));
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800255#endif
256
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800257 // create the "quit program" action
258 m_quitProgram = new QAction(tr("&Quit"), this);
259 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800260}
261
262void ChronoShareGui::createTrayIcon()
263{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800264 // create a new icon menu
265 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800266
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800267 // add actions to the menu
268 m_trayIconMenu->addAction(m_openFolder);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800269 m_trayIconMenu->addAction(m_openWeb);
270 m_trayIconMenu->addMenu(m_recentFilesMenu);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800271 m_trayIconMenu->addSeparator();
272 m_trayIconMenu->addAction(m_viewSettings);
273 m_trayIconMenu->addAction(m_changeFolder);
274 m_trayIconMenu->addSeparator();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800275 m_trayIconMenu->addAction(m_wifiAction);
276 m_trayIconMenu->addSeparator();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800277 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800278
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800279 // create new tray icon
280 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800281
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800282#ifdef ADHOC_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800283 // associate the menu with the tray icon
284 m_trayIcon->setContextMenu(m_trayIconMenu);
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800285#endif
Jared Lindblomb4032e22013-01-17 23:50:51 -0800286
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800287 // handle left click of icon
288 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800289}
290
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800291void
292ChronoShareGui::onAdHocChange (bool state)
293{
294#ifdef ADHOC_SUPPORTED
295 if (m_wifiAction->isChecked ())
296 {
297 QMessageBox msgBox;
298 msgBox.setText ("WARNING: your WiFi will be disconnected");
299 msgBox.setIcon (QMessageBox::Warning);
300 msgBox.setInformativeText("AdHoc WiFi will disconnect your current WiFi.\n Are you sure that you are OK with that?");
301 msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
302 msgBox.setDefaultButton(QMessageBox::Cancel);
303 int ret = msgBox.exec();
304
305 if (ret == QMessageBox::Cancel)
306 {
307 m_wifiAction->setChecked (false);
308 }
309 else
310 {
311
312 m_wifiAction->setText (tr("Disable AdHoc WiFI"));
313
314 // create adhoc
315 m_executor.execute (Adhoc::CreateAdhoc);
316 }
317 }
318 else
319 {
320 m_wifiAction->setText (tr("Enable AdHoc WiFI"));
321
322 // disable adhoc
323 m_executor.execute (Adhoc::DestroyAdhoc);
324
325 // a trick in DestroyAdhoc ensures that WiFi will be reconnected to a default WiFi
326 }
327#endif
328}
329
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800330void
331ChronoShareGui::onCheckForUpdates()
332{
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800333#ifdef SPARKLE_SUPPORTED
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800334 m_autoUpdate->checkForUpdates();
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800335#endif
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800336}
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800337
Jared Lindblom06405c42013-01-17 20:48:39 -0800338void ChronoShareGui::setIcon()
339{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800340 // set the icon image
Zhenkai Zhud9429222013-02-25 22:34:09 -0800341 m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING));
Jared Lindblom06405c42013-01-17 20:48:39 -0800342}
343
344void ChronoShareGui::openSharedFolder()
345{
Zhenkai Zhuce04cb42013-02-25 15:29:31 -0800346 QString path = QDir::toNativeSeparators(m_dirPath);
347 QDesktopServices::openUrl(QUrl("file:///" + path));
Jared Lindblom06405c42013-01-17 20:48:39 -0800348}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800349
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800350void ChronoShareGui::openInWebBrowser()
351{
352 QDesktopServices::openUrl(QUrl("http://localhost:9001"));
353}
354
355void ChronoShareGui::openFile()
356{
357 // figure out who sent the signal
358 QAction *pAction = qobject_cast<QAction*>(sender());
359 if (pAction->isEnabled())
360 {
361 // we stored full path of the file in this toolTip field
362#ifdef Q_WS_MAC
363 // we do some hack so we could show the file in Finder highlighted
364 QStringList args;
365 args << "-e";
366 args << "tell application \"Finder\"";
367 args << "-e";
368 args << "activate";
369 args << "-e";
370 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
371 args << "-e";
372 args << "end tell";
373 QProcess::startDetached("osascript", args);
374#else
375 // too bad qt couldn't do highlighting for Linux (or Mac)
376 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
377#endif
378 }
379}
380
381void ChronoShareGui::updateRecentFilesMenu()
382{
383 for (int i = 0; i < 5; i++)
384 {
385 m_fileActions[i]->setVisible(false);
386 }
387 m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5);
388}
389
390void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index)
391{
392 QString fullPath = m_dirPath + "/" + filename.c_str();
393 QFileInfo fileInfo(fullPath);
394 if (fileInfo.exists())
395 {
396 // This is a hack, we just use some field to store the path
397 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
398 m_fileActions[index]->setEnabled(true);
399 }
400 else
401 {
402 // after half an hour frustrating test and search around,
403 // I think it's the problem of Qt.
404 // According to the Qt doc, the action cannot be clicked
405 // and the area would be grey, but it didn't happen
406 // User can still trigger the action, and not greyed
407 // added check in SLOT to see if the action is "enalbed"
408 // as a remedy
409 // Give up at least for now
410 m_fileActions[index]->setEnabled(false);
411 // UPDATE, file not fetched yet
412 if (action == 0)
413 {
414 QFont font;
415 // supposed by change the font, didn't happen
416 font.setWeight(QFont::Light);
417 m_fileActions[index]->setFont(font);
418 m_fileActions[index]->setToolTip(tr("Fetching..."));
419 }
420 // DELETE
421 else
422 {
423 QFont font;
424 // supposed by change the font, didn't happen
425 font.setStrikeOut(true);
426 m_fileActions[index]->setFont(font);
427 m_fileActions[index]->setToolTip(tr("Deleted..."));
428 }
429 }
430 m_fileActions[index]->setText(fileInfo.fileName());
431 m_fileActions[index]->setVisible(true);
432}
433
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800434void ChronoShareGui::changeSettings()
435{
436 if(!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800437 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800438 else
439 editUsername->setText(m_username);
440
441 if(!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800442 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800443 else
444 editSharedFolder->setText(m_sharedFolderName);
445
446 saveSettings();
447 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800448
449 startBackend();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800450}
451
Jared Lindblomb4032e22013-01-17 23:50:51 -0800452void ChronoShareGui::openFileDialog()
453{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800454 // prompt user for new directory
455 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose a new folder"),
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800456 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
457 QFileInfo qFileInfo (tempPath);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800458
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800459 if(qFileInfo.isDir())
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800460 {
461 m_dirPath = tempPath;
462 editSharedFolderPath->setText(m_dirPath);
463 }
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800464 else
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800465 {
466 openMessageBox ("Error", "Not a valid folder, Ignoring.");
467 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800468
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800469 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Jared Lindblomb4032e22013-01-17 23:50:51 -0800470
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800471 // save settings
472 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800473}
474
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800475void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800476{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800477 // if double clicked, open shared folder
478 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800479 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800480 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800481 }
482}
483
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800484void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800485{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800486 //simple for now
487 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800488 this->raise();
489 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800490}
491
492bool ChronoShareGui::loadSettings()
493{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800494 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800495
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800496 // Load Settings
497 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
498 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800499
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800500 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
501
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800502 if(settings.contains("username"))
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800503 {
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800504 m_username = settings.value("username", "admin").toString();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800505 }
506 else
507 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800508 successful = false;
509 }
510
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800511 editUsername->setText(m_username);
512
513 if(settings.contains("sharedfoldername"))
514 {
515 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
516 }
517 else
518 {
519 successful = false;
520 }
521
522 editSharedFolder->setText(m_sharedFolderName);
523
524 if(settings.contains("dirPath"))
525 {
526 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
527 }
528 else
529 {
530 successful = false;
531 }
532
533 editSharedFolderPath->setText(m_dirPath);
534
535 _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800536
537 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800538}
539
540void ChronoShareGui::saveSettings()
541{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800542 // Save Settings
543 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
544 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800545
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800546 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800547 settings.setValue("username", m_username);
548 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800549}
550
551void ChronoShareGui::closeEvent(QCloseEvent* event)
552{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800553 _LOG_DEBUG ("Close Event")
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800554 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800555 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800556}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800557
558#if WAF
559#include "chronosharegui.moc"
560#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800561#endif