blob: 22366ea3392f2f5af10a1e167872b9d9df399989 [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 Zhu6b53f932013-03-02 20:46:02 -080040#ifdef _DEBUG
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080041static const string DOC_ROOT = "gui/html";
Zhenkai Zhu6b53f932013-03-02 20:46:02 -080042#else
43static const string DOC_ROOT = ":/html";
44#endif
Zhenkai Zhud9429222013-02-25 22:34:09 -080045static const QString ICON_PICTURE_QSTRING(":/images/friends-group-icon.png");
46
Alexander Afanasyev83a53002013-01-24 11:12:01 -080047INIT_LOGGER ("Gui");
48
49ChronoShareGui::ChronoShareGui(QWidget *parent)
Jared Lindblomc8ffd592013-01-25 00:00:30 -080050 : QDialog(parent)
Zhenkai Zhud5756312013-01-31 13:49:45 -080051 , m_watcher(0)
52 , m_dispatcher(0)
Zhenkai Zhud9429222013-02-25 22:34:09 -080053 , m_httpServer(0)
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080054#ifdef ADHOC_SUPPORTED
55 , m_executor (1)
56#endif
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080057#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu8e3e9072013-02-28 15:40:51 -080058 , m_autoUpdate(new SparkleAutoUpdate(tr("http://irl.cs.ucla.edu/~zhenkai/chronoshare_dist/chronoshare.xml")))
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080059#endif
Jared Lindblom06405c42013-01-17 20:48:39 -080060{
Jared Lindblomf4b900e2013-02-07 18:08:47 -080061 setWindowTitle("Settings");
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -080062 setMinimumWidth (600);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080063
Jared Lindblomd8b76ad2013-02-07 20:37:37 -080064 labelUsername = new QLabel("Username (hint: /<username>)");
Jared Lindblomc8ffd592013-01-25 00:00:30 -080065 labelSharedFolder = new QLabel("Shared Folder Name");
66 labelSharedFolderPath = new QLabel("Shared Folder Path");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080067
Zhenkai Zhud5756312013-01-31 13:49:45 -080068 QRegExp regex("(/[^/]+)+$");
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080069 QValidator *prefixValidator = new QRegExpValidator(regex, this);
70
Jared Lindblomc8ffd592013-01-25 00:00:30 -080071 editUsername = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080072 editUsername->setValidator(prefixValidator);
73
74 QRegExp noPureWhiteSpace("^\\S+.*$");
75 QValidator *wsValidator = new QRegExpValidator(noPureWhiteSpace, this);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080076 editSharedFolder = new QLineEdit();
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -080077 editSharedFolder->setValidator(wsValidator);
78
Jared Lindblomc8ffd592013-01-25 00:00:30 -080079 editSharedFolderPath = new QLineEdit();
80 editSharedFolderPath->setReadOnly(true);
81 QPalette pal = editSharedFolderPath->palette();
82 pal.setColor(QPalette::Active, QPalette::Base, pal.color(QPalette::Disabled, QPalette::Base));
83 editSharedFolderPath->setPalette(pal);
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -080084 button = new QPushButton("Save and apply settings");
Zhenkai Zhu630d0f12013-02-28 17:21:54 -080085
86 QString versionString = QString("Version: ChronoShare v%1").arg(CHRONOSHARE_VERSION);
87 label = new QLabel(versionString, this);
Jared Lindblomc8ffd592013-01-25 00:00:30 -080088
89 connect(button, SIGNAL(clicked()), this, SLOT(changeSettings()));
90
91 mainLayout = new QVBoxLayout; //vertically
92 mainLayout->addWidget(labelUsername);
93 mainLayout->addWidget(editUsername);
94 mainLayout->addWidget(labelSharedFolder);
95 mainLayout->addWidget(editSharedFolder);
96 mainLayout->addWidget(labelSharedFolderPath);
97 mainLayout->addWidget(editSharedFolderPath);
98 mainLayout->addWidget(button);
99 mainLayout->addWidget(label);
100 setLayout(mainLayout);
101
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800102 // create actions that result from clicking a menu option
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800103 createActionsAndMenu();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800104
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800105 // create tray icon
106 createTrayIcon();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800107
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800108 // set icon image
109 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -0800110
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800111 // show tray icon
112 m_trayIcon->show();
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800113
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 {
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800125 if (m_username.isNull () || m_username == "" ||
126 m_sharedFolderName.isNull () || m_sharedFolderName == "")
127 {
128 openMessageBox("First Time Setup", "To activate ChronoShare, please configure your username and shared folder name.");
129 viewSettings();
130 }
131 else
132 {
133 startBackend();
134 }
Zhenkai Zhud5756312013-01-31 13:49:45 -0800135 }
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800136
137#ifdef ADHOC_SUPPORTED
138 m_executor.start ();
139#endif
Zhenkai Zhud5756312013-01-31 13:49:45 -0800140}
141
142void
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800143ChronoShareGui::startBackend (bool restart/*=false*/)
Zhenkai Zhud5756312013-01-31 13:49:45 -0800144{
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800145 if (m_username.isNull () || m_username=="" ||
146 m_sharedFolderName.isNull () || m_sharedFolderName=="" ||
147 m_dirPath.isNull () || m_dirPath=="")
148 {
149 _LOG_DEBUG ("Don't start backend, because settings are in progress or incomplete");
150 return;
151 }
152
Zhenkai Zhud5756312013-01-31 13:49:45 -0800153 if (m_watcher != 0 && m_dispatcher != 0)
154 {
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800155 if (!restart)
156 {
157 return;
158 }
159
160 _LOG_DEBUG ("Restarting Dispatcher and FileWatcher for the new location or new username");
161 delete m_watcher; // stop filewatching ASAP
162 delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object)
Zhenkai Zhud5756312013-01-31 13:49:45 -0800163 }
164
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800165 filesystem::path realPathToFolder (m_dirPath.toStdString ());
166 realPathToFolder /= m_sharedFolderName.toStdString ();
167
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800168 m_dispatcher = new Dispatcher (m_username.toStdString (), m_sharedFolderName.toStdString (),
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800169 realPathToFolder, make_shared<CcnxWrapper> ());
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800170
171 // Alex: this **must** be here, otherwise m_dirPath will be uninitialized
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800172 m_watcher = new FsWatcher (realPathToFolder.string ().c_str (),
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800173 bind (&Dispatcher::Did_LocalFile_AddOrModify, m_dispatcher, _1),
Zhenkai Zhud1756272013-02-01 17:02:18 -0800174 bind (&Dispatcher::Did_LocalFile_Delete, m_dispatcher, _1));
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800175
176 if (m_httpServer != 0)
177 {
178 // no need to restart webserver if it already exists
179 return;
180 }
181
Zhenkai Zhud9429222013-02-25 22:34:09 -0800182 QFileInfo indexHtmlInfo(":/html/index.html");
183 if (indexHtmlInfo.exists())
184 {
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800185 try
186 {
187 m_httpServer = new http::server::server(HTTP_SERVER_ADDRESS, HTTP_SERVER_PORT, DOC_ROOT);
188 m_httpServerThread = boost::thread(&http::server::server::run, m_httpServer);
189 }
190 catch (std::exception &e)
191 {
192 _LOG_ERROR ("Start http server failed");
193 m_httpServer = 0; // just to make sure
194 QMessageBox msgBox;
195 msgBox.setText ("WARNING: Cannot start http server!");
196 msgBox.setIcon (QMessageBox::Warning);
197 msgBox.setInformativeText(QString("Starting http server failed. You will not be able to check history from web brower. Exception caused: %1").arg(e.what()));
198 msgBox.setStandardButtons(QMessageBox::Ok);
199 msgBox.exec();
200 }
Zhenkai Zhud9429222013-02-25 22:34:09 -0800201 }
202 else
203 {
204 _LOG_ERROR ("Http server doc root dir does not exist!");
Zhenkai Zhud9429222013-02-25 22:34:09 -0800205 }
Jared Lindblom06405c42013-01-17 20:48:39 -0800206}
207
208ChronoShareGui::~ChronoShareGui()
209{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800210#ifdef ADHOC_SUPPORTED
211 m_executor.shutdown ();
212#endif
213
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800214 delete m_watcher; // stop filewatching ASAP
Alexander Afanasyev9ca444e2013-01-25 16:29:35 -0800215 delete m_dispatcher; // stop dispatcher ASAP, but after watcher (to prevent triggering callbacks on deleted object)
Zhenkai Zhu8da33652013-03-04 10:52:03 -0800216 if (m_httpServer != 0)
217 {
218 m_httpServer->handle_stop();
219 m_httpServerThread.join();
220 delete m_httpServer;
221 }
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -0800222
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800223 // cleanup
224 delete m_trayIcon;
225 delete m_trayIconMenu;
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800226#ifdef ADHOC_SUPPORTED
227 delete m_wifiAction;
228#endif
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800229#ifdef SPARKLE_SUPPORTED
230 delete m_autoUpdate;
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800231 delete m_checkForUpdates;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800232#endif
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800233 delete m_openFolder;
234 delete m_viewSettings;
235 delete m_changeFolder;
236 delete m_quitProgram;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800237
238 delete labelUsername;
239 delete labelSharedFolder;
240 delete editUsername;
241 delete editSharedFolder;
242 delete button;
243 delete label;
244 delete mainLayout;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800245}
246
247void ChronoShareGui::openMessageBox(QString title, QString text)
248{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800249 QMessageBox messageBox(this);
250 messageBox.setWindowTitle(title);
251 messageBox.setText(text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800252
Zhenkai Zhud9429222013-02-25 22:34:09 -0800253 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800254
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800255 messageBox.exec();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800256}
257
258void ChronoShareGui::openMessageBox(QString title, QString text, QString infotext)
259{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800260 QMessageBox messageBox(this);
261 messageBox.setWindowTitle(title);
262 messageBox.setText(text);
263 messageBox.setInformativeText(infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800264
Zhenkai Zhud9429222013-02-25 22:34:09 -0800265 messageBox.setIconPixmap(QPixmap(ICON_PICTURE_QSTRING));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800266
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800267 messageBox.exec();
Jared Lindblom06405c42013-01-17 20:48:39 -0800268}
269
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800270void ChronoShareGui::createActionsAndMenu()
Jared Lindblom06405c42013-01-17 20:48:39 -0800271{
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800272 _LOG_DEBUG ("Create actions");
273
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800274 // create the "open folder" action
275 m_openFolder = new QAction(tr("&Open Folder"), this);
276 connect(m_openFolder, SIGNAL(triggered()), this, SLOT(openSharedFolder()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800277
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800278 m_openWeb = new QAction(tr("Open in &Browser"), this);
279 connect(m_openWeb, SIGNAL(triggered()), this, SLOT(openInWebBrowser()));
280
281 m_recentFilesMenu = new QMenu(tr("Recently Changed Files"), this);
282 for (int i = 0; i < 5; i++)
283 {
284 m_fileActions[i] = new QAction(this);
285 m_fileActions[i]->setVisible(false);
286 connect(m_fileActions[i], SIGNAL(triggered()), this, SLOT(openFile()));
287 m_recentFilesMenu->addAction(m_fileActions[i]);
288 }
289 connect(m_recentFilesMenu, SIGNAL(aboutToShow()), this, SLOT(updateRecentFilesMenu()));
290
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800291 // create the "view settings" action
292 m_viewSettings = new QAction(tr("&View Settings"), this);
293 connect(m_viewSettings, SIGNAL(triggered()), this, SLOT(viewSettings()));
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800294
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800295 // create the "change folder" action
296 m_changeFolder = new QAction(tr("&Change Folder"), this);
297 connect(m_changeFolder, SIGNAL(triggered()), this, SLOT(openFileDialog()));
Jared Lindblomb4032e22013-01-17 23:50:51 -0800298
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800299#ifdef ADHOC_SUPPORTED
300 // create "AdHoc Wifi" action
301 m_wifiAction = new QAction (tr("Enable AdHoc &WiFI"), m_trayIconMenu);
302 m_wifiAction->setChecked (false);
303 m_wifiAction->setCheckable (true);
304 connect (m_wifiAction, SIGNAL (toggled(bool)), this, SLOT(onAdHocChange(bool)));
305#endif
306
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800307#ifdef SPARKLE_SUPPORTED
Zhenkai Zhubb170d42013-02-25 13:48:59 -0800308 m_checkForUpdates = new QAction (tr("Check For Updates"), this);
309 connect (m_checkForUpdates, SIGNAL(triggered()), this, SLOT(onCheckForUpdates()));
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800310#endif
311
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800312 // create the "quit program" action
313 m_quitProgram = new QAction(tr("&Quit"), this);
314 connect(m_quitProgram, SIGNAL(triggered()), qApp, SLOT(quit()));
Jared Lindblom06405c42013-01-17 20:48:39 -0800315}
316
317void ChronoShareGui::createTrayIcon()
318{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800319 // create a new icon menu
320 m_trayIconMenu = new QMenu(this);
Jared Lindblom06405c42013-01-17 20:48:39 -0800321
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800322 // add actions to the menu
323 m_trayIconMenu->addAction(m_openFolder);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800324 m_trayIconMenu->addAction(m_openWeb);
325 m_trayIconMenu->addMenu(m_recentFilesMenu);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800326 m_trayIconMenu->addSeparator();
327 m_trayIconMenu->addAction(m_viewSettings);
328 m_trayIconMenu->addAction(m_changeFolder);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800329
330#ifdef SPARKLE_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800331 m_trayIconMenu->addSeparator();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800332 m_trayIconMenu->addAction(m_checkForUpdates);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800333#endif
334
335#ifdef ADHOC_SUPPORTED
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800336 m_trayIconMenu->addSeparator();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800337 m_trayIconMenu->addAction(m_wifiAction);
Alexander Afanasyeve8496a32013-03-03 16:10:43 -0800338#endif
339
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800340 m_trayIconMenu->addSeparator();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800341 m_trayIconMenu->addAction(m_quitProgram);
Jared Lindblom06405c42013-01-17 20:48:39 -0800342
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800343 // create new tray icon
344 m_trayIcon = new QSystemTrayIcon(this);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800345
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800346#ifdef ADHOC_SUPPORTED
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800347 // associate the menu with the tray icon
348 m_trayIcon->setContextMenu(m_trayIconMenu);
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800349#endif
Jared Lindblomb4032e22013-01-17 23:50:51 -0800350
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800351 // handle left click of icon
352 connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayIconClicked(QSystemTrayIcon::ActivationReason)));
Jared Lindblom06405c42013-01-17 20:48:39 -0800353}
354
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800355void
356ChronoShareGui::onAdHocChange (bool state)
357{
358#ifdef ADHOC_SUPPORTED
359 if (m_wifiAction->isChecked ())
360 {
361 QMessageBox msgBox;
362 msgBox.setText ("WARNING: your WiFi will be disconnected");
363 msgBox.setIcon (QMessageBox::Warning);
364 msgBox.setInformativeText("AdHoc WiFi will disconnect your current WiFi.\n Are you sure that you are OK with that?");
365 msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
366 msgBox.setDefaultButton(QMessageBox::Cancel);
367 int ret = msgBox.exec();
368
369 if (ret == QMessageBox::Cancel)
370 {
371 m_wifiAction->setChecked (false);
372 }
373 else
374 {
375
376 m_wifiAction->setText (tr("Disable AdHoc WiFI"));
377
378 // create adhoc
379 m_executor.execute (Adhoc::CreateAdhoc);
380 }
381 }
382 else
383 {
384 m_wifiAction->setText (tr("Enable AdHoc WiFI"));
385
386 // disable adhoc
387 m_executor.execute (Adhoc::DestroyAdhoc);
388
389 // a trick in DestroyAdhoc ensures that WiFi will be reconnected to a default WiFi
390 }
391#endif
392}
393
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800394void
395ChronoShareGui::onCheckForUpdates()
396{
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800397#ifdef SPARKLE_SUPPORTED
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800398 cout << "+++++++++++ trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800399 m_autoUpdate->checkForUpdates();
Zhenkai Zhu3857d712013-02-27 22:51:25 -0800400 cout << "+++++++++++ end trying to update +++++++ " << endl;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800401#endif
Zhenkai Zhu249fbc72013-02-27 21:50:25 -0800402}
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800403
Jared Lindblom06405c42013-01-17 20:48:39 -0800404void ChronoShareGui::setIcon()
405{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800406 // set the icon image
Zhenkai Zhud9429222013-02-25 22:34:09 -0800407 m_trayIcon->setIcon(QIcon(ICON_PICTURE_QSTRING));
Jared Lindblom06405c42013-01-17 20:48:39 -0800408}
409
410void ChronoShareGui::openSharedFolder()
411{
Zhenkai Zhuce04cb42013-02-25 15:29:31 -0800412 QString path = QDir::toNativeSeparators(m_dirPath);
413 QDesktopServices::openUrl(QUrl("file:///" + path));
Jared Lindblom06405c42013-01-17 20:48:39 -0800414}
Jared Lindblomb4032e22013-01-17 23:50:51 -0800415
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800416void ChronoShareGui::openInWebBrowser()
417{
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800418 QUrl url ("http://localhost:9001/");
419 url.setFragment ("fileList&"
420 "user=" + QUrl::toPercentEncoding (m_username) + "&"
421 "folder=" + QUrl::toPercentEncoding (m_sharedFolderName));
422
423 // i give up. there is double encoding and I have no idea how to fight it...
424 QDesktopServices::openUrl (url);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800425}
426
427void ChronoShareGui::openFile()
428{
429 // figure out who sent the signal
430 QAction *pAction = qobject_cast<QAction*>(sender());
431 if (pAction->isEnabled())
432 {
433 // we stored full path of the file in this toolTip field
434#ifdef Q_WS_MAC
435 // we do some hack so we could show the file in Finder highlighted
436 QStringList args;
437 args << "-e";
438 args << "tell application \"Finder\"";
439 args << "-e";
440 args << "activate";
441 args << "-e";
442 args << "select POSIX file \"" + pAction->toolTip() + "/" + pAction->text() + "\"";
443 args << "-e";
444 args << "end tell";
445 QProcess::startDetached("osascript", args);
446#else
447 // too bad qt couldn't do highlighting for Linux (or Mac)
448 QDesktopServices::openUrl(QUrl("file:///" + pAction->toolTip()));
449#endif
450 }
451}
452
453void ChronoShareGui::updateRecentFilesMenu()
454{
455 for (int i = 0; i < 5; i++)
456 {
457 m_fileActions[i]->setVisible(false);
458 }
459 m_dispatcher->LookupRecentFileActions(boost::bind(&ChronoShareGui::checkFileAction, this, _1, _2, _3), 5);
460}
461
462void ChronoShareGui::checkFileAction (const std::string &filename, int action, int index)
463{
464 QString fullPath = m_dirPath + "/" + filename.c_str();
465 QFileInfo fileInfo(fullPath);
466 if (fileInfo.exists())
467 {
468 // This is a hack, we just use some field to store the path
469 m_fileActions[index]->setToolTip(fileInfo.absolutePath());
470 m_fileActions[index]->setEnabled(true);
471 }
472 else
473 {
474 // after half an hour frustrating test and search around,
475 // I think it's the problem of Qt.
476 // According to the Qt doc, the action cannot be clicked
477 // and the area would be grey, but it didn't happen
478 // User can still trigger the action, and not greyed
479 // added check in SLOT to see if the action is "enalbed"
480 // as a remedy
481 // Give up at least for now
482 m_fileActions[index]->setEnabled(false);
483 // UPDATE, file not fetched yet
484 if (action == 0)
485 {
486 QFont font;
487 // supposed by change the font, didn't happen
488 font.setWeight(QFont::Light);
489 m_fileActions[index]->setFont(font);
490 m_fileActions[index]->setToolTip(tr("Fetching..."));
491 }
492 // DELETE
493 else
494 {
495 QFont font;
496 // supposed by change the font, didn't happen
497 font.setStrikeOut(true);
498 m_fileActions[index]->setFont(font);
499 m_fileActions[index]->setToolTip(tr("Deleted..."));
500 }
501 }
502 m_fileActions[index]->setText(fileInfo.fileName());
503 m_fileActions[index]->setVisible(true);
504}
505
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800506void ChronoShareGui::changeSettings()
507{
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800508 QString oldUsername = m_username;
509 QString oldSharedFolderName = m_sharedFolderName;
510
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800511 if(!editUsername->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800512 m_username = editUsername->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800513 else
514 editUsername->setText(m_username);
515
516 if(!editSharedFolder->text().isEmpty())
Zhenkai Zhuf3ea97a2013-01-30 15:20:52 -0800517 m_sharedFolderName = editSharedFolder->text().trimmed();
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800518 else
519 editSharedFolder->setText(m_sharedFolderName);
520
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800521 if (m_username.isNull () || m_username=="" ||
522 m_sharedFolderName.isNull () || m_sharedFolderName=="")
523 {
524 openMessageBox ("Error",
525 "Username and shared folder name cannot be empty");
526 }
527 else
528 {
529 saveSettings();
530 this->hide();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800531
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800532 if (m_username != oldUsername ||
533 oldSharedFolderName != m_sharedFolderName)
534 {
535 startBackend (true); // restart dispatcher/fswatcher
536 }
537 }
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800538}
539
Jared Lindblomb4032e22013-01-17 23:50:51 -0800540void ChronoShareGui::openFileDialog()
541{
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800542 while (true)
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800543 {
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800544 // prompt user for new directory
545 QString tempPath = QFileDialog::getExistingDirectory(this, tr("Choose ChronoShare folder"),
546 m_dirPath, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
547 if (tempPath.isNull ())
548 {
549 if (m_dirPath.isNull ())
550 {
551 openMessageBox ("Notice", "ChronoShare will use [" + QDir::homePath () + "/ChronoShare]. \n\nYou can change it later selecting \"Change Folder\" menu.");
552
553 m_dirPath = QDir::homePath () + "/ChronoShare";
554 editSharedFolderPath->setText (m_dirPath);
555 break;
556 }
557 else
558 {
559 // user just cancelled, no need to do anything else
560 return;
561 }
562 }
563
564 QFileInfo qFileInfo (tempPath);
565
566 if (!qFileInfo.isDir())
567 {
568 openMessageBox ("Error", "Please select an existing folder or create a new one.");
569 continue;
570 }
571
572 if (m_dirPath == tempPath)
573 {
574 // user selected the same directory, no need to do anything
575 return;
576 }
577
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800578 m_dirPath = tempPath;
579 editSharedFolderPath->setText(m_dirPath);
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800580 break;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800581 }
Jared Lindblomb4032e22013-01-17 23:50:51 -0800582
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800583 _LOG_DEBUG ("Selected path: " << m_dirPath.toStdString ());
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800584 // save settings
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800585 saveSettings ();
586
587 startBackend (true); // restart dispatcher/fswatcher
Jared Lindblomb4032e22013-01-17 23:50:51 -0800588}
589
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800590void ChronoShareGui::trayIconClicked (QSystemTrayIcon::ActivationReason reason)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800591{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800592 // if double clicked, open shared folder
593 if(reason == QSystemTrayIcon::DoubleClick)
Jared Lindblomb4032e22013-01-17 23:50:51 -0800594 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800595 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800596 }
597}
598
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800599void ChronoShareGui::viewSettings()
Jared Lindblomb4032e22013-01-17 23:50:51 -0800600{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800601 //simple for now
602 this->show();
Zhenkai Zhud5756312013-01-31 13:49:45 -0800603 this->raise();
604 this->activateWindow();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800605}
606
607bool ChronoShareGui::loadSettings()
608{
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800609 bool successful = true;
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800610
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800611 // Load Settings
612 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
613 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800614
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800615 // _LOG_DEBUG (lexical_cast<string> (settings.allKeys()));
616
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800617 if(settings.contains("username"))
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800618 {
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800619 m_username = settings.value("username", "admin").toString();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800620 }
621 else
622 {
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800623 successful = false;
624 }
625
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800626 editUsername->setText(m_username);
627
628 if(settings.contains("sharedfoldername"))
629 {
630 m_sharedFolderName = settings.value("sharedfoldername", "shared").toString();
631 }
632 else
633 {
634 successful = false;
635 }
636
637 editSharedFolder->setText(m_sharedFolderName);
638
639 if(settings.contains("dirPath"))
640 {
641 m_dirPath = settings.value("dirPath", QDir::homePath()).toString();
642 }
643 else
644 {
645 successful = false;
646 }
647
648 editSharedFolderPath->setText(m_dirPath);
649
650 _LOG_DEBUG ("Found configured path: " << (successful ? m_dirPath.toStdString () : std::string("no")));
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800651
652 return successful;
Jared Lindblomb4032e22013-01-17 23:50:51 -0800653}
654
655void ChronoShareGui::saveSettings()
656{
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800657 // Save Settings
658 // QSettings settings(m_settingsFilePath, QSettings::NativeFormat);
659 QSettings settings (QSettings::NativeFormat, QSettings::UserScope, "irl.cs.ucla.edu", "ChronoShare");
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800660
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800661 settings.setValue("dirPath", m_dirPath);
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800662 settings.setValue("username", m_username);
663 settings.setValue("sharedfoldername", m_sharedFolderName);
Jared Lindblomb4032e22013-01-17 23:50:51 -0800664}
665
666void ChronoShareGui::closeEvent(QCloseEvent* event)
667{
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800668 _LOG_DEBUG ("Close Event");
669
670 if (m_username.isNull () || m_username == "" ||
671 m_sharedFolderName.isNull () || m_sharedFolderName == "")
672 {
673 openMessageBox ("ChronoShare is not active", "Username and/or shared folder name are not set.\n\n To activate ChronoShare, open Settings menu and configure your username and shared folder name");
674 }
675
676 this->hide();
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800677 event->ignore(); // don't let the event propagate to the base class
Jared Lindblomb4032e22013-01-17 23:50:51 -0800678}
Jared Lindblomdc845f02013-01-18 17:29:40 -0800679
680#if WAF
681#include "chronosharegui.moc"
682#include "chronosharegui.cpp.moc"
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800683#endif