| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef APPLICATION_H |
| 4 | #define APPLICATION_H |
| 5 | |
| 6 | #include "core/feeddownloader.h" |
| 7 | #include "database/databasefactory.h" |
| 8 | #include "definitions/definitions.h" |
| 9 | #include "gui/systemtrayicon.h" |
| 10 | #include "miscellaneous/feedreader.h" |
| 11 | #include "miscellaneous/iofactory.h" |
| 12 | #include "miscellaneous/localization.h" |
| 13 | #include "miscellaneous/nodejs.h" |
| 14 | #include "miscellaneous/notification.h" |
| 15 | #include "miscellaneous/singleapplication.h" |
| 16 | #include "miscellaneous/skinfactory.h" |
| 17 | #include "miscellaneous/systemfactory.h" |
| 18 | #include "network-web/downloadmanager.h" |
| 19 | |
| 20 | #include <functional> |
| 21 | |
| 22 | #include <QCommandLineParser> |
| 23 | #include <QList> |
| 24 | |
| 25 | #if defined(qApp) |
| 26 | #undef qApp |
| 27 | #endif |
| 28 | |
| 29 | // Define new qApp macro. Yeaaaaah. |
| 30 | #define qApp (Application::instance()) |
| 31 | |
| 32 | class FormMain; |
| 33 | class FormLog; |
| 34 | class IconFactory; |
| 35 | class QAction; |
| 36 | class Mutex; |
| 37 | |
| 38 | #if QT_VERSION_MAJOR == 6 |
| 39 | class QWebEngineDownloadRequest; |
| 40 | #else |
| 41 | class QWebEngineDownloadItem; |
| 42 | #endif |
| 43 | |
| 44 | class WebFactory; |
| 45 | class NotificationFactory; |
| 46 | class ToastNotificationsManager; |
| 47 | class WebViewer; |
| 48 | class Settings; |
| 49 | |
| 50 | #if defined(Q_OS_WIN) |
| 51 | struct ITaskbarList4; |
| 52 | #endif |
| 53 | |
| 54 | struct GuiMessage { |
| 55 | public: |
| 56 | GuiMessage() {} |
| 57 | GuiMessage(QString title, |
| 58 | QString message, |
| 59 | QSystemTrayIcon::MessageIcon type = QSystemTrayIcon::MessageIcon::Information) |
| 60 | : m_title(std::move(title)), m_message(std::move(message)), m_type(type) {} |
| 61 | |
| 62 | QString m_title; |
| 63 | QString m_message; |
| 64 | QSystemTrayIcon::MessageIcon m_type; |
| 65 | FeedDownloadResults m_feedFetchResults; |
| 66 | }; |
| 67 | |
| 68 | Q_DECLARE_METATYPE(GuiMessage) |
| 69 | |
| 70 | struct GuiMessageDestination { |
| 71 | public: |
| 72 | GuiMessageDestination(bool tray = true, bool message_box = false, bool status_bar = false) |
| 73 | : m_tray(tray), m_messageBox(message_box), m_statusBar(status_bar) {} |
| 74 | |
| 75 | bool m_tray; |
| 76 | bool m_messageBox; |
| 77 | bool m_statusBar; |
| 78 | }; |
| 79 | |
| 80 | Q_DECLARE_METATYPE(GuiMessageDestination) |
| 81 | |
| 82 | struct GuiAction { |
| 83 | public: |
| 84 | GuiAction(QString title = {}, const std::function<void()>& action = nullptr) |
| 85 | : m_title(std::move(title)), m_action(action) {} |
| 86 | |
| 87 | QString m_title; |
| 88 | std::function<void()> m_action; |
| 89 | }; |
| 90 | |
| 91 | Q_DECLARE_METATYPE(GuiAction) |
| 92 | |
| 93 | class RSSGUARD_DLLSPEC Application : public SingleApplication { |
| 94 | Q_OBJECT |
| 95 | |
| 96 | public: |
| 97 | explicit Application(const QString& id, int& argc, char** argv, const QStringList& raw_cli_args); |
| 98 | virtual ~Application(); |
| 99 | |
| 100 | void reactOnForeignNotifications(); |
| 101 | void hideOrShowMainForm(); |
| 102 | void loadDynamicShortcuts(); |
| 103 | void offerPolls() const; |
| 104 | void offerChanges() const; |
| 105 | |
| 106 | bool isAlreadyRunning(); |
| 107 | |
| 108 | QStringList builtinSounds() const; |
| 109 | |
| 110 | FeedReader* feedReader(); |
| 111 | void setFeedReader(FeedReader* feed_reader); |
| 112 | |
| 113 | // Globally accessible actions. |
| 114 | QList<QAction*> userActions(); |
| 115 | |
| 116 | // Globally accessible actions for the sole purpose of keyboard shortcuts. |
| 117 | QList<QAction*> userAndExtraActions(); |
| 118 | |
| 119 | // Check whether this application starts for the first time (ever). |
| 120 | bool isFirstRun() const; |
| 121 | |
| 122 | // Check whether CURRENT VERSION of the application starts for the first time. |
| 123 | bool isFirstRunCurrentVersion() const; |
| 124 | |
| 125 | QCommandLineParser* cmdParser(); |
| 126 | WebFactory* web() const; |
| 127 | SystemFactory* system(); |
| 128 | SkinFactory* skins(); |
| 129 | Localization* localization(); |
| 130 | DatabaseFactory* database(); |
| 131 | IconFactory* icons(); |
| 132 | DownloadManager* downloadManager(); |
| 133 | Settings* settings() const; |
| 134 | Mutex* feedUpdateLock(); |
| 135 | FormMain* mainForm(); |
| 136 | QWidget* mainFormWidget(); |
| 137 | SystemTrayIcon* trayIcon(); |
| 138 | NotificationFactory* notifications() const; |
| 139 | NodeJs* nodejs() const; |
| 140 | QThreadPool* workHorsePool() const; |
| 141 | ToastNotificationsManager* toastNotifications() const; |
| 142 | |
| 143 | QIcon desktopAwareIcon() const; |
| 144 | |
| 145 | QString tempFolder() const; |
| 146 | QString documentsFolder() const; |
| 147 | QString homeFolder() const; |
| 148 | QString configFolder() const; |
| 149 | |
| 150 | // These return user ready folders. |
| 151 | QString userDataAppFolder() const; |
| 152 | QString userDataHomeFolder() const; |
| 153 | QString customDataFolder() const; |
| 154 | |
| 155 | // Returns the base folder to which store user data, the "data" folder. |
| 156 | // NOTE: Use this to get correct path under which store user data. |
| 157 | QString userDataFolder(); |
| 158 | |
| 159 | QString cacheFolder(); |
| 160 | |
| 161 | int customAdblockPort() const; |
| 162 | |
| 163 | QString replaceUserDataFolderPlaceholder(QString text) const; |
| 164 | QStringList replaceUserDataFolderPlaceholder(QStringList texts) const; |
| 165 | |
| 166 | void setMainForm(FormMain* main_form); |
| 167 | |
| 168 | void backupDatabaseSettings(bool backup_database, |
| 169 | bool backup_settings, |
| 170 | const QString& target_path, |
| 171 | const QString& backup_name); |
| 172 | void restoreDatabaseSettings(bool restore_database, |
| 173 | bool restore_settings, |
| 174 | const QString& source_database_file_path = QString(), |
| 175 | const QString& source_settings_file_path = QString()); |
| 176 | |
| 177 | void showTrayIcon(); |
| 178 | void deleteTrayIcon(); |
| 179 | |
| 180 | QStringList rawCliArgs() const; |
| 181 | |
| 182 | // Displays given simple message in tray icon bubble or OSD |
| 183 | // or in message box if tray icon is disabled. |
| 184 | void showGuiMessage(Notification::Event event, |
| 185 | const GuiMessage& msg, |
| 186 | GuiMessageDestination dest = {}, |
| 187 | const GuiAction& action = {}, |
| 188 | QWidget* parent = nullptr); |
| 189 | |
| 190 | WebViewer* createWebView(); |
| 191 | |
| 192 | bool usingLite() const; |
| 193 | bool isWayland() const; |
| 194 | |
| 195 | #if defined(NO_LITE) |
| 196 | bool forcedLite() const; |
| 197 | #endif |
| 198 | |
| 199 | // Returns pointer to "GOD" application singleton. |
| 200 | static Application* instance(); |
| 201 | |
| 202 | // Custom debug/console log handler. |
| 203 | static void performLogging(QtMsgType type, const QMessageLogContext& context, const QString& msg); |
| 204 | |
| 205 | public slots: |
| 206 | // Restarts the application. |
| 207 | void restart(); |
| 208 | |
| 209 | void reloadCurrentSkin(bool replace_existing_qss = true); |
| 210 | |
| 211 | // Processes incoming message from another RSS Guard instance. |
| 212 | void parseCmdArgumentsFromOtherInstance(const QString& message); |
| 213 | void parseCmdArgumentsFromMyInstance(const QStringList& raw_cli_args, QString& custom_ua); |
| 214 | |
| 215 | void displayLog(); |
| 216 | |
| 217 | void showGuiMessageCore(Notification::Event event, |
| 218 | const GuiMessage& msg, |
| 219 | GuiMessageDestination dest = {}, |
| 220 | const GuiAction& action = {}, |
| 221 | QWidget* parent = nullptr); |
| 222 | |
| 223 | private slots: |
| 224 | void loadMessageToFeedAndArticleList(Feed* feed, const Message& message); |
| 225 | void fillCmdArgumentsParser(QCommandLineParser& parser); |
| 226 | |
| 227 | void onNodeJsPackageUpdateError(const QObject* sndr, |
| 228 | const QList<NodeJs::PackageMetadata>& pkgs, |
| 229 | const QString& error); |
| 230 | void onNodeJsPackageInstalled(const QObject* sndr, |
| 231 | const QList<NodeJs::PackageMetadata>& pkgs, |
| 232 | bool already_up_to_date); |
| 233 | void onCommitData(QSessionManager& manager); |
| 234 | void onSaveState(QSessionManager& manager); |
| 235 | void onAboutToQuit(); |
| 236 | void showMessagesNumber(int unread_messages, bool any_feed_has_new_unread_messages); |
| 237 | void onAdBlockFailure(); |
| 238 | |
| 239 | #if defined(NO_LITE) |
| 240 | #if QT_VERSION_MAJOR == 6 |
| 241 | void downloadRequested(QWebEngineDownloadRequest* download_item); |
| 242 | #else |
| 243 | void downloadRequested(QWebEngineDownloadItem* download_item); |
| 244 | #endif |
| 245 | #endif |
| 246 | |
| 247 | void onFeedUpdatesStarted(); |
| 248 | void onFeedUpdatesProgress(const Feed* feed, int current, int total); |
| 249 | void onFeedUpdatesFinished(const FeedDownloadResults& results); |
| 250 | |
| 251 | signals: |
| 252 | void sendLogToDialog(QString message); |
| 253 | |
| 254 | private: |
| 255 | #if defined(Q_OS_WIN) |
| 256 | QImage generateOverlayIcon(int number) const; |
| 257 | #endif |
| 258 | |
| 259 | void setupFontAntialiasing(); |
| 260 | void setupCustomDataFolder(const QString& data_folder); |
| 261 | void setupWorkHorsePool(); |
| 262 | void determineFirstRuns(); |
| 263 | void eliminateFirstRuns(); |
| 264 | void displayLogMessageInDialog(const QString& message); |
| 265 | |
| 266 | private: |
| 267 | QStringList m_rawCliArgs; |
| 268 | QCommandLineParser m_cmdParser; |
| 269 | FeedReader* m_feedReader; |
| 270 | |
| 271 | bool m_quitLogicDone; |
| 272 | |
| 273 | // This read-write lock is used by application on its close. |
| 274 | // Application locks this lock for WRITING. |
| 275 | // This means that if application locks that lock, then |
| 276 | // no other transaction-critical action can acquire lock |
| 277 | // for reading and won't be executed, so no critical action |
| 278 | // will be running when application quits |
| 279 | // |
| 280 | // EACH critical action locks this lock for READING. |
| 281 | // Several actions can lock this lock for reading. |
| 282 | // But of user decides to close the application (in other words, |
| 283 | // tries to lock the lock for writing), then no other |
| 284 | // action will be allowed to lock for reading. |
| 285 | QScopedPointer<Mutex> m_updateFeedsLock; |
| 286 | |
| 287 | QList<QAction*> m_userActions; |
| 288 | FormMain* m_mainForm; |
| 289 | FormLog* m_logForm; |
| 290 | SystemTrayIcon* m_trayIcon; |
| 291 | Settings* m_settings; |
| 292 | WebFactory* m_webFactory; |
| 293 | SystemFactory* m_system; |
| 294 | SkinFactory* m_skins; |
| 295 | Localization* m_localization; |
| 296 | IconFactory* m_icons; |
| 297 | DatabaseFactory* m_database; |
| 298 | DownloadManager* m_downloadManager; |
| 299 | NotificationFactory* m_notifications; |
| 300 | ToastNotificationsManager* m_toastNotifications; |
| 301 | NodeJs* m_nodejs; |
| 302 | QThreadPool* m_workHorsePool; |
| 303 | bool m_shouldRestart; |
| 304 | bool m_firstRunEver; |
| 305 | bool m_firstRunCurrentVersion; |
| 306 | QString m_customDataFolder; |
| 307 | int m_customAdblockPort; |
| 308 | bool m_allowMultipleInstances; |
| 309 | |
| 310 | #if defined(NO_LITE) |
| 311 | bool m_forcedLite; |
| 312 | #endif |
| 313 | |
| 314 | #if defined(Q_OS_WIN) |
| 315 | ITaskbarList4* m_windowsTaskBar; |
| 316 | #endif |
| 317 | }; |
| 318 | |
| 319 | inline Application* Application::instance() { |
| 320 | return static_cast<Application*>(QCoreApplication::instance()); |
| 321 | } |
| 322 | |
| 323 | #endif // APPLICATION_H |