mirror of
https://github.com/fergalmoran/flameshot.git
synced 2025-12-25 11:18:40 +00:00
resolving merge conflicts
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
|
||||
#include "controller.h"
|
||||
#include "src/config/configwindow.h"
|
||||
#include "src/core/QHotkey/QHotkey"
|
||||
#include "src/utils/confighandler.h"
|
||||
#include "src/utils/history.h"
|
||||
#include "src/utils/screengrabber.h"
|
||||
@@ -30,22 +31,42 @@
|
||||
#include <QAction>
|
||||
#include <QApplication>
|
||||
#include <QClipboard>
|
||||
#include <QDesktopServices>
|
||||
#include <QDesktopWidget>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QMenu>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include "src/core/globalshortcutfilter.h"
|
||||
#endif
|
||||
|
||||
#if (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
|
||||
defined(Q_OS_MACX))
|
||||
#include <QOperatingSystemVersion>
|
||||
#include <QScreen>
|
||||
#endif
|
||||
|
||||
// Controller is the core component of Flameshot, creates the trayIcon and
|
||||
// launches the capture widget
|
||||
|
||||
Controller::Controller()
|
||||
: m_captureWindow(nullptr)
|
||||
, m_history(nullptr)
|
||||
, m_trayIconMenu(nullptr)
|
||||
, m_networkCheckUpdates(nullptr)
|
||||
, m_showCheckAppUpdateStatus(false)
|
||||
#if (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
|
||||
defined(Q_OS_MACX))
|
||||
, m_HotkeyScreenshotCapture(nullptr)
|
||||
, m_HotkeyScreenshotHistory(nullptr)
|
||||
#endif
|
||||
{
|
||||
m_history = nullptr;
|
||||
|
||||
m_appLatestVersion = QStringLiteral(APP_VERSION).replace("v", "");
|
||||
qApp->setQuitOnLastWindowClosed(false);
|
||||
|
||||
// set default shortcusts if not set yet
|
||||
@@ -68,11 +89,36 @@ Controller::Controller()
|
||||
|
||||
QString StyleSheet = CaptureButton::globalStyleSheet();
|
||||
qApp->setStyleSheet(StyleSheet);
|
||||
|
||||
#if (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
|
||||
defined(Q_OS_MACX))
|
||||
// Try to take a test screenshot, MacOS will request a "Screen Recording"
|
||||
// permissions on the first run. Otherwise it will be hidden under the
|
||||
// CaptureWidget
|
||||
QScreen* currentScreen = QGuiApplication::screenAt(QCursor::pos());
|
||||
currentScreen->grabWindow(QApplication::desktop()->winId(), 0, 0, 1, 1);
|
||||
|
||||
// set global shortcuts for MacOS
|
||||
m_HotkeyScreenshotCapture =
|
||||
new QHotkey(QKeySequence("Ctrl+Alt+Shift+4"), true, this);
|
||||
QObject::connect(m_HotkeyScreenshotCapture,
|
||||
&QHotkey::activated,
|
||||
qApp,
|
||||
[&]() { this->startVisualCapture(); });
|
||||
m_HotkeyScreenshotHistory =
|
||||
new QHotkey(QKeySequence("Ctrl+Alt+Shift+H"), true, this);
|
||||
QObject::connect(m_HotkeyScreenshotHistory,
|
||||
&QHotkey::activated,
|
||||
qApp,
|
||||
[&]() { this->showRecentScreenshots(); });
|
||||
#endif
|
||||
getLatestAvailableVersion();
|
||||
}
|
||||
|
||||
Controller::~Controller()
|
||||
{
|
||||
delete m_history;
|
||||
delete m_trayIconMenu;
|
||||
}
|
||||
|
||||
Controller* Controller::getInstance()
|
||||
@@ -89,6 +135,67 @@ void Controller::enableExports()
|
||||
this, &Controller::captureFailed, this, &Controller::handleCaptureFailed);
|
||||
}
|
||||
|
||||
void Controller::getLatestAvailableVersion()
|
||||
{
|
||||
// This features is required for MacOS and Windows user and for Linux users
|
||||
// who installed Flameshot not from the repository.
|
||||
m_networkCheckUpdates = new QNetworkAccessManager(this);
|
||||
QNetworkRequest requestCheckUpdates(QUrl(FLAMESHOT_APP_VERSION_URL));
|
||||
connect(m_networkCheckUpdates,
|
||||
&QNetworkAccessManager::finished,
|
||||
this,
|
||||
&Controller::handleReplyCheckUpdates);
|
||||
m_networkCheckUpdates->get(requestCheckUpdates);
|
||||
|
||||
// check for updates each 24 hours
|
||||
doLater(1000 * 60 * 60 * 24, this, [this]() {
|
||||
this->getLatestAvailableVersion();
|
||||
});
|
||||
}
|
||||
|
||||
void Controller::handleReplyCheckUpdates(QNetworkReply* reply)
|
||||
{
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
QJsonDocument response = QJsonDocument::fromJson(reply->readAll());
|
||||
QJsonObject json = response.object();
|
||||
m_appLatestVersion = json["tag_name"].toString().replace("v", "");
|
||||
if (QStringLiteral(APP_VERSION)
|
||||
.replace("v", "")
|
||||
.compare(m_appLatestVersion) < 0) {
|
||||
m_appLatestUrl = json["html_url"].toString();
|
||||
QString newVersion =
|
||||
tr("New version %1 is available").arg(m_appLatestVersion);
|
||||
m_appUpdates->setText(newVersion);
|
||||
if (m_showCheckAppUpdateStatus) {
|
||||
sendTrayNotification(newVersion, "Flameshot");
|
||||
QDesktopServices::openUrl(QUrl(m_appLatestUrl));
|
||||
}
|
||||
} else if (m_showCheckAppUpdateStatus) {
|
||||
sendTrayNotification(tr("You have the latest version"),
|
||||
"Flameshot");
|
||||
}
|
||||
} else {
|
||||
qWarning() << "Failed to get information about the latest version. "
|
||||
<< reply->errorString();
|
||||
if (m_showCheckAppUpdateStatus) {
|
||||
sendTrayNotification(
|
||||
tr("Failed to get information about the latest version."),
|
||||
"Flameshot");
|
||||
}
|
||||
}
|
||||
m_showCheckAppUpdateStatus = false;
|
||||
}
|
||||
|
||||
void Controller::appUpdates()
|
||||
{
|
||||
if (m_appLatestUrl.isEmpty()) {
|
||||
m_showCheckAppUpdateStatus = true;
|
||||
getLatestAvailableVersion();
|
||||
} else {
|
||||
QDesktopServices::openUrl(QUrl(m_appLatestUrl));
|
||||
}
|
||||
}
|
||||
|
||||
void Controller::requestCapture(const CaptureRequest& request)
|
||||
{
|
||||
uint id = request.id();
|
||||
@@ -150,10 +257,21 @@ void Controller::startVisualCapture(const uint id,
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
m_captureWindow->show();
|
||||
#elif (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
|
||||
defined(Q_OS_MACX))
|
||||
// In "Emulate fullscreen mode"
|
||||
m_captureWindow->showFullScreen();
|
||||
m_captureWindow->activateWindow();
|
||||
m_captureWindow->raise();
|
||||
#else
|
||||
m_captureWindow->showFullScreen();
|
||||
// m_captureWindow->show(); // Debug
|
||||
#endif
|
||||
if (!m_appLatestUrl.isEmpty() &&
|
||||
ConfigHandler().ignoreUpdateToVersion().compare(
|
||||
m_appLatestVersion) < 0) {
|
||||
m_captureWindow->showAppUpdateNotification(m_appLatestVersion,
|
||||
m_appLatestUrl);
|
||||
}
|
||||
} else {
|
||||
emit captureFailed(id);
|
||||
}
|
||||
@@ -183,6 +301,11 @@ void Controller::openConfigWindow()
|
||||
if (!m_configWindow) {
|
||||
m_configWindow = new ConfigWindow();
|
||||
m_configWindow->show();
|
||||
#if (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
|
||||
defined(Q_OS_MACX))
|
||||
m_configWindow->activateWindow();
|
||||
m_configWindow->raise();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,6 +314,11 @@ void Controller::openInfoWindow()
|
||||
{
|
||||
if (!m_infoWindow) {
|
||||
m_infoWindow = new InfoWindow();
|
||||
#if (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
|
||||
defined(Q_OS_MACX))
|
||||
m_infoWindow->activateWindow();
|
||||
m_infoWindow->raise();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,6 +328,11 @@ void Controller::openLauncherWindow()
|
||||
m_launcherWindow = new CaptureLauncher();
|
||||
}
|
||||
m_launcherWindow->show();
|
||||
#if (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
|
||||
defined(Q_OS_MACX))
|
||||
m_launcherWindow->activateWindow();
|
||||
m_launcherWindow->raise();
|
||||
#endif
|
||||
}
|
||||
|
||||
void Controller::enableTrayIcon()
|
||||
@@ -207,7 +340,7 @@ void Controller::enableTrayIcon()
|
||||
if (m_trayIcon) {
|
||||
return;
|
||||
}
|
||||
QMenu* trayIconMenu = new QMenu();
|
||||
m_trayIconMenu = new QMenu();
|
||||
|
||||
ConfigHandler().setDisabledTrayIcon(false);
|
||||
QAction* captureAction = new QAction(tr("&Take Screenshot"), this);
|
||||
@@ -225,6 +358,10 @@ void Controller::enableTrayIcon()
|
||||
configAction, &QAction::triggered, this, &Controller::openConfigWindow);
|
||||
QAction* infoAction = new QAction(tr("&About"), this);
|
||||
connect(infoAction, &QAction::triggered, this, &Controller::openInfoWindow);
|
||||
|
||||
m_appUpdates = new QAction(tr("Check for updates"), this);
|
||||
connect(m_appUpdates, &QAction::triggered, this, &Controller::appUpdates);
|
||||
|
||||
QAction* quitAction = new QAction(tr("&Quit"), this);
|
||||
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
|
||||
|
||||
@@ -234,29 +371,60 @@ void Controller::enableTrayIcon()
|
||||
recentAction, SIGNAL(triggered()), this, SLOT(showRecentScreenshots()));
|
||||
|
||||
// generate menu
|
||||
trayIconMenu->addAction(captureAction);
|
||||
trayIconMenu->addAction(launcherAction);
|
||||
trayIconMenu->addSeparator();
|
||||
trayIconMenu->addAction(recentAction);
|
||||
trayIconMenu->addSeparator();
|
||||
trayIconMenu->addAction(configAction);
|
||||
trayIconMenu->addAction(infoAction);
|
||||
trayIconMenu->addSeparator();
|
||||
trayIconMenu->addAction(quitAction);
|
||||
m_trayIconMenu->addAction(captureAction);
|
||||
m_trayIconMenu->addAction(launcherAction);
|
||||
m_trayIconMenu->addSeparator();
|
||||
m_trayIconMenu->addAction(recentAction);
|
||||
m_trayIconMenu->addSeparator();
|
||||
m_trayIconMenu->addAction(configAction);
|
||||
m_trayIconMenu->addSeparator();
|
||||
m_trayIconMenu->addAction(m_appUpdates);
|
||||
m_trayIconMenu->addAction(infoAction);
|
||||
m_trayIconMenu->addSeparator();
|
||||
m_trayIconMenu->addAction(quitAction);
|
||||
|
||||
m_trayIcon = new QSystemTrayIcon();
|
||||
m_trayIcon->setToolTip(QStringLiteral("Flameshot"));
|
||||
m_trayIcon->setContextMenu(trayIconMenu);
|
||||
#if defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
|
||||
defined(Q_OS_MACX)
|
||||
// Because of the following issues on MacOS "Catalina":
|
||||
// https://bugreports.qt.io/browse/QTBUG-86393
|
||||
// https://developer.apple.com/forums/thread/126072
|
||||
auto currentMacOsVersion = QOperatingSystemVersion::current();
|
||||
if (currentMacOsVersion >= currentMacOsVersion.MacOSBigSur) {
|
||||
m_trayIcon->setContextMenu(m_trayIconMenu);
|
||||
}
|
||||
#else
|
||||
m_trayIcon->setContextMenu(m_trayIconMenu);
|
||||
#endif
|
||||
QIcon trayicon =
|
||||
QIcon::fromTheme("flameshot-tray", QIcon(":img/app/flameshot.png"));
|
||||
m_trayIcon->setIcon(trayicon);
|
||||
|
||||
#if defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
|
||||
defined(Q_OS_MACX)
|
||||
if (currentMacOsVersion < currentMacOsVersion.MacOSBigSur) {
|
||||
// Because of the following issues on MacOS "Catalina":
|
||||
// https://bugreports.qt.io/browse/QTBUG-86393
|
||||
// https://developer.apple.com/forums/thread/126072
|
||||
auto trayIconActivated = [this](QSystemTrayIcon::ActivationReason r) {
|
||||
if (m_trayIconMenu->isVisible()) {
|
||||
m_trayIconMenu->hide();
|
||||
} else {
|
||||
m_trayIconMenu->popup(QCursor::pos());
|
||||
}
|
||||
};
|
||||
connect(
|
||||
m_trayIcon, &QSystemTrayIcon::activated, this, trayIconActivated);
|
||||
}
|
||||
#else
|
||||
auto trayIconActivated = [this](QSystemTrayIcon::ActivationReason r) {
|
||||
if (r == QSystemTrayIcon::Trigger) {
|
||||
startVisualCapture();
|
||||
}
|
||||
};
|
||||
connect(m_trayIcon, &QSystemTrayIcon::activated, this, trayIconActivated);
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
// Ensure proper removal of tray icon when program quits on Windows.
|
||||
@@ -310,6 +478,11 @@ void Controller::showRecentScreenshots()
|
||||
}
|
||||
m_history->loadHistory();
|
||||
m_history->show();
|
||||
#if (defined(Q_OS_MAC) || defined(Q_OS_MAC64) || defined(Q_OS_MACOS) || \
|
||||
defined(Q_OS_MACX))
|
||||
m_history->activateWindow();
|
||||
m_history->raise();
|
||||
#endif
|
||||
}
|
||||
|
||||
void Controller::startFullscreenCapture(const uint id)
|
||||
|
||||
Reference in New Issue
Block a user