blob: efa5f4196870d8a68777779f5773297349b0aca3 [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
39#include "adhoc.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080040#include "dispatcher.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080041#include "fs-watcher.hpp"
Zhenkai Zhud9429222013-02-25 22:34:09 -080042#include "server.hpp"
Lijing Wang152449c2015-05-31 16:25:16 -070043#endif // Q_MOC_RUN
44
45#if __APPLE__ && HAVE_SPARKLE
46#define SPARKLE_SUPPORTED 1
47#include "sparkle-auto-update.hpp"
48#endif
49
50#include <thread>
51
52namespace ndn {
53namespace chronoshare {
Alexander Afanasyev9e5a4702013-01-24 13:15:23 -080054
Jared Lindblomc8ffd592013-01-25 00:00:30 -080055class ChronoShareGui : public QDialog
Jared Lindblom06405c42013-01-17 20:48:39 -080056{
Alexander Afanasyev83a53002013-01-24 11:12:01 -080057 Q_OBJECT
Jared Lindblom06405c42013-01-17 20:48:39 -080058
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059public:
Alexander Afanasyev83a53002013-01-24 11:12:01 -080060 // constructor
61 explicit ChronoShareGui(QWidget* parent = 0);
62
63 // destructor
64 ~ChronoShareGui();
Jared Lindblomb4032e22013-01-17 23:50:51 -080065
Jared Lindblom06405c42013-01-17 20:48:39 -080066private slots:
Alexander Afanasyev83a53002013-01-24 11:12:01 -080067 // open the shared folder
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080068 void
69 openSharedFolder();
Jared Lindblomb4032e22013-01-17 23:50:51 -080070
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080071 void
72 openFile();
Zhenkai Zhu25e13582013-02-27 15:33:01 -080073
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080074 void
75 openInWebBrowser();
Zhenkai Zhu25e13582013-02-27 15:33:01 -080076
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080077 void
78 updateRecentFilesMenu();
Zhenkai Zhu25e13582013-02-27 15:33:01 -080079
Alexander Afanasyev83a53002013-01-24 11:12:01 -080080 // open file dialog
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080081 void
82 openFileDialog();
Jared Lindblomb4032e22013-01-17 23:50:51 -080083
Alexander Afanasyev83a53002013-01-24 11:12:01 -080084 // handle left click of tray icon
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080085 void
86 trayIconClicked(QSystemTrayIcon::ActivationReason reason);
Jared Lindblom06405c42013-01-17 20:48:39 -080087
Alexander Afanasyev83a53002013-01-24 11:12:01 -080088 // view chronoshare settings
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080089 void
90 viewSettings();
Jared Lindblom5d7e51e2013-01-18 00:59:34 -080091
Jared Lindblomc8ffd592013-01-25 00:00:30 -080092 // change chronoshare settings
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080093 void
94 changeSettings();
Jared Lindblomc8ffd592013-01-25 00:00:30 -080095
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080096 void
97 onCheckForUpdates();
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -080098
Jared Lindblom06405c42013-01-17 20:48:39 -080099private:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100 void
101 checkFileAction(const std::string&, int, int);
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800102 // create actions that result from clicking a menu option
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 void
104 createActionsAndMenu();
Jared Lindblom06405c42013-01-17 20:48:39 -0800105
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800106 // create tray icon
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107 void
108 createTrayIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -0800109
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800110 // set icon image
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800111 void
112 setIcon();
Jared Lindblom06405c42013-01-17 20:48:39 -0800113
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800114 // load persistent settings
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800115 bool
116 loadSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800117
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800118 // save persistent settings
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800119 void
120 saveSettings();
Jared Lindblomb4032e22013-01-17 23:50:51 -0800121
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800122 // prompt user dialog box
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800123 void
124 openMessageBox(QString title, QString text);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800125
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800126 // overload
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800127 void
128 openMessageBox(QString title, QString text, QString infotext);
Jared Lindblom5d7e51e2013-01-18 00:59:34 -0800129
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800130 // capture close event
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800131 void
132 closeEvent(QCloseEvent* event);
Jared Lindblom06405c42013-01-17 20:48:39 -0800133
Alexander Afanasyevd437ffc2013-03-04 12:29:23 -0800134 // starts/restarts fs watcher and dispatcher
Zhenkai Zhud5756312013-01-31 13:49:45 -0800135 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800136 startBackend(bool restart = false);
Zhenkai Zhud5756312013-01-31 13:49:45 -0800137
Jared Lindblom06405c42013-01-17 20:48:39 -0800138private:
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800139 QSystemTrayIcon* m_trayIcon; // tray icon
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800140 QMenu* m_trayIconMenu; // tray icon menu
Jared Lindblom06405c42013-01-17 20:48:39 -0800141
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800142 QAction* m_openFolder; // open the shared folder action
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800143 QAction* m_viewSettings; // chronoShare settings
144 QAction* m_changeFolder; // change the shared folder action
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800145 QAction* m_quitProgram; // quit program action
146 QAction* m_checkForUpdates;
147 QAction* m_openWeb;
148 QMenu* m_recentFilesMenu;
149 QAction* m_fileActions[5];
Jared Lindblom06405c42013-01-17 20:48:39 -0800150
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800151 QAction* m_wifiAction;
Alexander Afanasyev85a4ba02013-02-24 16:30:17 -0800152
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800153 QString m_dirPath; // shared directory
154 QString m_username; // username
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800155 QString m_sharedFolderName; // shared folder name
Jared Lindblomb4032e22013-01-17 23:50:51 -0800156
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800157 http::server::server* m_httpServer;
Lijing Wang152449c2015-05-31 16:25:16 -0700158 std::thread m_httpServerThread;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800159
160 QLabel* labelUsername;
161 QPushButton* button;
162 QLabel* labelSharedFolder;
163 QLabel* labelSharedFolderPath;
164 QLineEdit* editUsername;
165 QLineEdit* editSharedFolder;
166 QLineEdit* editSharedFolderPath;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800167 QLabel* label;
168 QVBoxLayout* mainLayout;
Jared Lindblomc8ffd592013-01-25 00:00:30 -0800169
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800170#ifdef SPARKLE_SUPPORTED
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800171 AutoUpdate* m_autoUpdate;
Zhenkai Zhuaca6cb52013-02-24 23:34:03 -0800172#endif
Alexander Afanasyev83a53002013-01-24 11:12:01 -0800173 // QString m_settingsFilePath; // settings file path
174 // QString m_settings;
Lijing Wang152449c2015-05-31 16:25:16 -0700175
176 std::thread m_chronoshareThread;
177 std::unique_ptr<boost::asio::io_service> m_ioService;
178 std::unique_ptr<Face> m_face;
179 std::unique_ptr<FsWatcher> m_watcher;
180 std::unique_ptr<Dispatcher> m_dispatcher;
Jared Lindblom06405c42013-01-17 20:48:39 -0800181};
182
Lijing Wang152449c2015-05-31 16:25:16 -0700183} // namespace chronoshare
184} // namespace ndn
185
186#endif // CHRONOSHARE_GUI_CHRONOSHAREGUI_HPP