blob: 827e7c39aa18f0b47b5560d903e32a3b2df91286 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Lijing Wang152449c2015-05-31 16:25:16 -07003 * Copyright (c) 2013-2017, Regents of the University of California.
Jared Lindblom06405c42013-01-17 20:48:39 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Jared Lindblom06405c42013-01-17 20:48:39 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Jared Lindblom06405c42013-01-17 20:48:39 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Jared Lindblom06405c42013-01-17 20:48:39 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Jared Lindblom06405c42013-01-17 20:48:39 -080019 */
20
Lijing Wang152449c2015-05-31 16:25:16 -070021#ifndef CHRONOSHARE_GUI_CHRONOSHAREGUI_HPP
22#define CHRONOSHARE_GUI_CHRONOSHAREGUI_HPP
Jared Lindblom06405c42013-01-17 20:48:39 -080023
Lijing Wang152449c2015-05-31 16:25:16 -070024#include "core/chronoshare-common.hpp"
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -080025
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080026#include <QApplication>
27#include <QCloseEvent>
Jared Lindblom06405c42013-01-17 20:48:39 -080028#include <QDebug>
Jared Lindblomb4032e22013-01-17 23:50:51 -080029#include <QDir>
30#include <QFileDialog>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080031#include <QMenu>
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080032#include <QMessageBox>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080033#include <QProcess>
34#include <QSettings>
35#include <QSystemTrayIcon>
Lijing Wang152449c2015-05-31 16:25:16 -070036#include <QtWidgets>
Jared Lindblom06405c42013-01-17 20:48:39 -080037
Lijing Wang152449c2015-05-31 16:25:16 -070038#ifndef Q_MOC_RUN
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080039#include "dispatcher.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080040#include "fs-watcher.hpp"
Zhenkai Zhud9429222013-02-25 22:34:09 -080041#include "server.hpp"
Lijing Wang152449c2015-05-31 16:25:16 -070042#endif // Q_MOC_RUN
43
44#if __APPLE__ && HAVE_SPARKLE
45#define SPARKLE_SUPPORTED 1
46#include "sparkle-auto-update.hpp"
47#endif
48
49#include <thread>
50
51namespace ndn {
52namespace chronoshare {
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080053
Jared Lindblomc8ffd592013-01-25 00:00:30 -080054class ChronoShareGui : public QDialog
Jared Lindblom06405c42013-01-17 20:48:39 -080055{
Alexander Afanasyev83a53002013-01-24 11:12:01 -080056 Q_OBJECT
Jared Lindblom06405c42013-01-17 20:48:39 -080057
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058public:
Alexander Afanasyev83a53002013-01-24 11:12:01 -080059 // constructor
60 explicit ChronoShareGui(QWidget* parent = 0);
61
62 // destructor
63 ~ChronoShareGui();
Jared Lindblomb4032e22013-01-17 23:50:51 -080064
Jared Lindblom06405c42013-01-17 20:48:39 -080065private slots:
Alexander Afanasyev83a53002013-01-24 11:12:01 -080066 // open the shared folder
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080067 void
68 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -080069
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080070 void
71 openFile();
Zhenkai Zhu25e13582013-02-27 15:33:01 -080072
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080073 void
74 openInWebBrowser();
Zhenkai Zhu25e13582013-02-27 15:33:01 -080075
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080076 void
77 updateRecentFilesMenu();
Zhenkai Zhu25e13582013-02-27 15:33:01 -080078
Alexander Afanasyev83a53002013-01-24 11:12:01 -080079 // open file dialog
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080080 void
81 openFileDialog();
Jared Lindblomb4032e22013-01-17 23:50:51 -080082
Alexander Afanasyev83a53002013-01-24 11:12:01 -080083 // handle left click of tray icon
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080084 void
85 trayIconClicked(QSystemTrayIcon::ActivationReason reason);
Jared Lindblom06405c42013-01-17 20:48:39 -080086
Alexander Afanasyev83a53002013-01-24 11:12:01 -080087 // view chronoshare settings
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080088 void
89 viewSettings();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080090
Jared Lindblomc8ffd592013-01-25 00:00:30 -080091 // change chronoshare settings
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080092 void
93 changeSettings();
Jared Lindblomc8ffd592013-01-25 00:00:30 -080094
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080095 void
96 onCheckForUpdates();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080097
Jared Lindblom06405c42013-01-17 20:48:39 -080098private:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080099 void
100 checkFileAction(const std::string&, int, int);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800101 // create actions that result from clicking a menu option
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800102 void
103 createActionsAndMenu();
Jared Lindblom06405c42013-01-17 20:48:39 -0800104
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800105 // create tray icon
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800106 void
107 createTrayIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -0800108
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800109 // set icon image
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 void
111 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -0800112
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800113 // load persistent settings
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800114 bool
115 loadSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800116
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800117 // save persistent settings
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800118 void
119 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800120
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800121 // prompt user dialog box
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800122 void
123 openMessageBox(QString title, QString text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800124
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800125 // overload
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800126 void
127 openMessageBox(QString title, QString text, QString infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800128
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800129 // capture close event
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800130 void
131 closeEvent(QCloseEvent* event);
Jared Lindblom06405c42013-01-17 20:48:39 -0800132
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800133 // starts/restarts fs watcher and dispatcher
Zhenkai Zhud5756312013-01-31 13:49:45 -0800134 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800135 startBackend(bool restart = false);
Zhenkai Zhud5756312013-01-31 13:49:45 -0800136
Jared Lindblom06405c42013-01-17 20:48:39 -0800137private:
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800138 QSystemTrayIcon* m_trayIcon; // tray icon
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800139 QMenu* m_trayIconMenu; // tray icon menu
Jared Lindblom06405c42013-01-17 20:48:39 -0800140
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800141 QAction* m_openFolder; // open the shared folder action
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800142 QAction* m_viewSettings; // chronoShare settings
143 QAction* m_changeFolder; // change the shared folder action
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800144 QAction* m_quitProgram; // quit program action
145 QAction* m_checkForUpdates;
146 QAction* m_openWeb;
147 QMenu* m_recentFilesMenu;
148 QAction* m_fileActions[5];
Jared Lindblom06405c42013-01-17 20:48:39 -0800149
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800150 QString m_dirPath; // shared directory
151 QString m_username; // username
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800152 QString m_sharedFolderName; // shared folder name
Jared Lindblomb4032e22013-01-17 23:50:51 -0800153
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800154 http::server::server* m_httpServer;
Lijing Wang152449c2015-05-31 16:25:16 -0700155 std::thread m_httpServerThread;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800156
157 QLabel* labelUsername;
158 QPushButton* button;
159 QLabel* labelSharedFolder;
160 QLabel* labelSharedFolderPath;
161 QLineEdit* editUsername;
162 QLineEdit* editSharedFolder;
163 QLineEdit* editSharedFolderPath;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800164 QLabel* label;
165 QVBoxLayout* mainLayout;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800166
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800167#ifdef SPARKLE_SUPPORTED
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800168 AutoUpdate* m_autoUpdate;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800169#endif
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800170 // QString m_settingsFilePath; // settings file path
171 // QString m_settings;
Lijing Wang152449c2015-05-31 16:25:16 -0700172
173 std::thread m_chronoshareThread;
174 std::unique_ptr<boost::asio::io_service> m_ioService;
175 std::unique_ptr<Face> m_face;
176 std::unique_ptr<FsWatcher> m_watcher;
177 std::unique_ptr<Dispatcher> m_dispatcher;
Jared Lindblom06405c42013-01-17 20:48:39 -0800178};
179
Lijing Wang152449c2015-05-31 16:25:16 -0700180} // namespace chronoshare
181} // namespace ndn
182
183#endif // CHRONOSHARE_GUI_CHRONOSHAREGUI_HPP