Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --pin, --upload, --accept-on-select to CLI #1970

Merged
merged 17 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions flameshot.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
;; Whether the savePath is a fixed path (bool)
;savePathFixed=false
;
;; Default file extension for screenshots
;setSaveAsFileExtension=.png
;
;; Main UI color
;; Color is any valid hex code or W3C color name
;uiColor=#740096
Expand Down
64 changes: 55 additions & 9 deletions src/core/capturerequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors

#include "capturerequest.h"
#include "confighandler.h"
#include "controller.h"
#include "imguruploader.h"
#include "pinwidget.h"
#include "src/utils/screenshotsaver.h"
#include "systemnotification.h"
#include <QApplication>
#include <QClipboard>
#include <QDateTime>
#include <QVector>
#include <stdexcept>
Expand Down Expand Up @@ -98,29 +105,68 @@ CaptureRequest::ExportTask CaptureRequest::tasks() const

void CaptureRequest::addTask(CaptureRequest::ExportTask task)
{
if (task == SAVE_TASK) {
throw std::logic_error("SAVE_TASK must be added using addSaveTask");
if (task == SAVE) {
throw std::logic_error("SAVE task must be added using addSaveTask");
}
m_tasks |= task;
}

void CaptureRequest::addSaveTask(const QString& path)
{
m_tasks |= SAVE_TASK;
m_tasks |= SAVE;
m_path = path;
}

void CaptureRequest::exportCapture(const QPixmap& p)
void CaptureRequest::addPinTask(const QRect& pinWindowGeometry)
{
if ((m_tasks & ExportTask::SAVE_TASK) != ExportTask::NO_TASK) {
m_tasks |= PIN;
m_pinWindowGeometry = pinWindowGeometry;
}

void CaptureRequest::exportCapture(const QPixmap& capture)
{
if (m_tasks & SAVE) {
if (m_path.isEmpty()) {
ScreenshotSaver(m_id).saveToFilesystemGUI(p);
ScreenshotSaver(m_id).saveToFilesystemGUI(capture);
} else {
ScreenshotSaver(m_id).saveToFilesystem(p, m_path);
ScreenshotSaver(m_id).saveToFilesystem(capture, m_path);
}
}

if (m_tasks & COPY) {
ScreenshotSaver().saveToClipboard(capture);
}

if (m_tasks & PIN) {
QWidget* widget = new PinWidget(capture, m_pinWindowGeometry);
widget->show();
widget->activateWindow();
if (m_mode == SCREEN_MODE || m_mode == FULLSCREEN_MODE) {
SystemNotification().sendMessage(
QObject::tr("Full screen screenshot pinned to screen"));
}
}

if ((m_tasks & ExportTask::COPY_TASK) != ExportTask::NO_TASK) {
ScreenshotSaver().saveToClipboard(p);
if (m_tasks & UPLOAD) {
ImgurUploader* widget = new ImgurUploader(capture);
widget->show();
widget->activateWindow();
// NOTE: lambda can't capture 'this' because it might be destroyed later
ExportTask tasks = m_tasks;
QObject::connect(
widget, &ImgurUploader::uploadOk, [widget, tasks](const QUrl& url) {
if (ConfigHandler().copyAndCloseAfterUpload()) {
if (!(tasks & COPY)) {
QApplication::clipboard()->setText(url.toString());
SystemNotification().sendMessage(
QObject::tr("URL copied to clipboard."));
widget->close();
} else {
widget->showPostUploadDialog();
}
} else {
widget->showPostUploadDialog();
}
});
}
}
15 changes: 10 additions & 5 deletions src/core/capturerequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ class CaptureRequest
enum ExportTask
{
NO_TASK = 0,
COPY_TASK = 1,
SAVE_TASK = 2,
PRINT_RAW_TASK = 4,
PRINT_GEOMETRY_TASK = 8,
COPY = 1,
SAVE = 2,
PRINT_RAW = 4,
PRINT_GEOMETRY = 8,
PIN = 16,
UPLOAD = 32,
ACCEPT_ON_SELECT = 64,
};

CaptureRequest(CaptureMode mode,
Expand All @@ -44,14 +47,16 @@ class CaptureRequest

void addTask(ExportTask task);
void addSaveTask(const QString& path = QString());
void exportCapture(const QPixmap& p);
void addPinTask(const QRect& pinWindowGeometry);
void exportCapture(const QPixmap& capture);

private:
CaptureMode m_mode;
uint m_delay;
QString m_path;
ExportTask m_tasks;
QVariant m_data;
QRect m_pinWindowGeometry;

bool m_forcedID;
uint m_id;
Expand Down
49 changes: 23 additions & 26 deletions src/core/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

Controller::Controller()
: m_captureWindow(nullptr)
, m_history(nullptr)
, m_trayIcon(nullptr)
, m_trayIconMenu(nullptr)
, m_networkCheckUpdates(nullptr)
Expand Down Expand Up @@ -101,7 +100,7 @@ Controller::Controller()
QObject::connect(m_HotkeyScreenshotHistory,
&QHotkey::activated,
qApp,
[&]() { this->showRecentScreenshots(); });
[&]() { this->showRecentUploads(); });
#endif

if (ConfigHandler().checkForUpdates()) {
Expand All @@ -111,7 +110,6 @@ Controller::Controller()

Controller::~Controller()
{
delete m_history;
delete m_trayIconMenu;
}

Expand Down Expand Up @@ -330,8 +328,13 @@ void Controller::startScreenGrab(const uint id, const int screenNumber)
}
QPixmap p(ScreenGrabber().grabScreen(n, ok));
if (ok) {
QRect selection; // `flameshot screen` does not support --selection
emit captureTaken(id, p, selection);
CaptureRequest& request = *requests().find(id);
QRect geometry = ScreenGrabber().screenGeometry(n);
if (request.tasks() & CaptureRequest::PIN) {
// change geometry for pin task
request.addPinTask(geometry);
}
emit captureTaken(id, p, geometry);
} else {
emit captureFailed(id);
}
Expand Down Expand Up @@ -421,8 +424,7 @@ void Controller::enableTrayIcon()

// recent screenshots
QAction* recentAction = new QAction(tr("&Latest Uploads"), this);
connect(
recentAction, SIGNAL(triggered()), this, SLOT(showRecentScreenshots()));
connect(recentAction, SIGNAL(triggered()), this, SLOT(showRecentUploads()));

// generate menu
m_trayIconMenu->addAction(captureAction);
Expand Down Expand Up @@ -528,25 +530,20 @@ void Controller::updateConfigComponents()
}
}

void Controller::updateRecentScreenshots()
{
if (nullptr != m_history) {
if (m_history->isVisible()) {
m_history->loadHistory();
}
}
}

void Controller::showRecentScreenshots()
void Controller::showRecentUploads()
{
if (nullptr == m_history) {
m_history = new HistoryWidget();
static HistoryWidget* historyWidget = nullptr;
if (nullptr == historyWidget) {
historyWidget = new HistoryWidget();
connect(historyWidget, &QObject::destroyed, this, []() {
historyWidget = nullptr;
});
}
m_history->loadHistory();
m_history->show();
historyWidget->loadHistory();
historyWidget->show();
#if defined(Q_OS_MACOS)
m_history->activateWindow();
m_history->raise();
historyWidget->activateWindow();
historyWidget->raise();
#endif
}

Expand All @@ -560,14 +557,14 @@ void Controller::startFullscreenCapture(const uint id)
bool ok = true;
QPixmap p(ScreenGrabber().grabEntireDesktop(ok));
if (ok) {
QRect selection; // `flameshot full` does not support --selection
emit captureTaken(id, p, selection);
// selection parameter is unused here
emit captureTaken(id, p, {});
} else {
emit captureFailed(id);
}
}

void Controller::handleCaptureTaken(uint id, QPixmap p, QRect selection)
void Controller::handleCaptureTaken(uint id, QPixmap p)
{
auto it = m_requestMap.find(id);
if (it != m_requestMap.end()) {
Expand Down
8 changes: 3 additions & 5 deletions src/core/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ class Controller : public QObject
void operator=(const Controller&) = delete;

void enableExports();
void updateRecentScreenshots();

void setCheckForUpdatesEnabled(const bool enabled);

QMap<uint, CaptureRequest>& requests();

signals:
void captureTaken(uint id, QPixmap p, QRect selection);
void captureTaken(uint id, QPixmap p, const QRect& selection);
void captureFailed(uint id);
void captureSaved(uint id, QString savePath);

Expand All @@ -65,7 +64,7 @@ public slots:

void updateConfigComponents();

void showRecentScreenshots();
void showRecentUploads();

void sendCaptureSaved(uint id, const QString& savePath);

Expand All @@ -75,7 +74,7 @@ private slots:
const QString& forcedSavePath = QString());
void startScreenGrab(const uint id = 0, const int screenNumber = -1);

void handleCaptureTaken(uint id, QPixmap p, QRect selection);
void handleCaptureTaken(uint id, QPixmap p);
void handleCaptureFailed(uint id);

void handleReplyCheckUpdates(QNetworkReply* reply);
Expand All @@ -101,7 +100,6 @@ private slots:
QPointer<ConfigWindow> m_configWindow;
QPointer<QSystemTrayIcon> m_trayIcon;

HistoryWidget* m_history;
QMenu* m_trayIconMenu;

QNetworkAccessManager* m_networkCheckUpdates;
Expand Down
3 changes: 1 addition & 2 deletions src/core/flameshotdbusadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ FlameshotDBusAdapter::~FlameshotDBusAdapter() {}
void FlameshotDBusAdapter::requestCapture(const QByteArray& requestData)
{
CaptureRequest req = CaptureRequest::deserialize(requestData);
req.setStaticID(req.id());
Controller::getInstance()->requestCapture(req);
}

Expand Down Expand Up @@ -65,7 +64,7 @@ void FlameshotDBusAdapter::autostartEnabled(bool enabled)

void FlameshotDBusAdapter::handleCaptureTaken(uint id,
const QPixmap& p,
QRect selection)
const QRect& selection)
{
QByteArray byteArray;
QBuffer buffer(&byteArray);
Expand Down
2 changes: 1 addition & 1 deletion src/core/flameshotdbusadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ public slots:
Q_NOREPLY void autostartEnabled(bool enabled);

private slots:
void handleCaptureTaken(uint id, const QPixmap& p, QRect selection);
void handleCaptureTaken(uint id, const QPixmap& p, const QRect& selection);
};
2 changes: 1 addition & 1 deletion src/core/globalshortcutfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bool GlobalShortcutFilter::nativeEventFilter(const QByteArray& eventType,

// Show screenshots history
if (VK_SNAPSHOT == keycode && MOD_SHIFT == modifiers) {
Controller::getInstance()->showRecentScreenshots();
Controller::getInstance()->showRecentUploads();
}

// Capture screen
Expand Down
Loading