Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 14 additions & 4 deletions src/server/src/extensions/clipboard/clipboard-extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "extensions/clipboard/clipboard-history-command.hpp"
#include "service-registry.hpp"

bool ClipboardExtension::preservePinnedOnClear = false;

class ClipboardClearCommand : public BuiltinCallbackCommand {
QString id() const override { return "clear"; }
QString name() const override { return "Clear Current Clipboard Data"; }
Expand Down Expand Up @@ -38,10 +40,11 @@ class ClearClipboardHistoryCommand : public BuiltinCallbackCommand {
auto ctx = ctrl->context();
auto clipman = ctx->services->clipman();
auto toast = ctx->services->toastService();
bool preservePinned = ClipboardExtension::preservePinnedOnClear;

ctx->navigation->confirmAlert("Are you sure?", "Your clipboard history will be gone forever :(",
[clipman, toast]() {
if (!clipman->removeAllSelections()) {
[clipman, toast, preservePinned]() {
if (!clipman->removeAllSelections(preservePinned)) {
toast->failure("Failed to clear clipboard history");
return;
}
Expand All @@ -60,8 +63,9 @@ ClipboardExtension::ClipboardExtension() {
void ClipboardExtension::initialized(const QJsonObject &preferences) const {
auto clipman = ServiceRegistry::instance()->clipman();
bool const eraseOnStartup = preferences.value("eraseOnStartup").toBool();
preservePinnedOnClear = preferences.value("preservePinnedOnClear").toBool(false);

if (eraseOnStartup) { clipman->removeAllSelections(); }
if (eraseOnStartup) { clipman->removeAllSelections(preservePinnedOnClear); }
}

void ClipboardExtension::preferenceValuesChanged(const QJsonObject &value) const {
Expand All @@ -70,13 +74,15 @@ void ClipboardExtension::preferenceValuesChanged(const QJsonObject &value) const
clipman->setMonitoring(value.value("monitoring").toBool());
clipman->setEncryption(value.value("encryption").toBool());
clipman->setIgnorePasswords(value.value("ignorePasswords").toBool());
preservePinnedOnClear = value.value("preservePinnedOnClear").toBool();
}

std::vector<Preference> ClipboardExtension::preferences() const {
auto encryption = Preference::makeCheckbox("encryption");
auto monitoring = Preference::makeCheckbox("monitoring");
auto eraseOnStartup = Preference::makeCheckbox("eraseOnStartup");
auto ignorePasswords = Preference::makeCheckbox("ignorePasswords");
auto preservePinnedOnClear = Preference::makeCheckbox("preservePinnedOnClear");

eraseOnStartup.setTitle("Erase on startup");
eraseOnStartup.setDescription("Erase clipboard history every time the vicinae server is started");
Expand All @@ -100,5 +106,9 @@ std::vector<Preference> ClipboardExtension::preferences() const {
"performed while this is turned off will not be recorded.");
monitoring.setDefaultValue(true);

return {monitoring, ignorePasswords, eraseOnStartup, encryption};
preservePinnedOnClear.setTitle("Preserve pinned on clear");
preservePinnedOnClear.setDescription("Keep pinned items when clearing clipboard history");
preservePinnedOnClear.setDefaultValue(false);

return {monitoring, ignorePasswords, eraseOnStartup, encryption, preservePinnedOnClear};
}
2 changes: 2 additions & 0 deletions src/server/src/extensions/clipboard/clipboard-extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ class ClipboardExtension : public BuiltinCommandRepository {
void preferenceValuesChanged(const QJsonObject &value) const override;

ClipboardExtension();

static bool preservePinnedOnClear;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "services/clipboard/clipboard-service.hpp"
#include "services/toast/toast-service.hpp"
#include "ui/alert/alert.hpp"
#include "extensions/clipboard/clipboard-extension.hpp"

class PasteClipboardSelection : public PasteToFocusedWindowAction {
QString m_id;
Expand Down Expand Up @@ -99,7 +100,8 @@ class RemoveAllSelectionsAction : public AbstractAction {
alert->setConfirmCallback([ctx]() {
auto toast = ctx->services->toastService();
auto clipman = ctx->services->clipman();
if (clipman->removeAllSelections()) {
bool preservePinned = ClipboardExtension::preservePinnedOnClear;
if (clipman->removeAllSelections(preservePinned)) {
toast->success("All selections were removed");
} else {
toast->failure("Failed to remove all selections");
Expand Down
13 changes: 13 additions & 0 deletions src/server/src/services/clipboard/clipboard-db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ bool ClipboardDatabase::removeAll() {
query.exec("DELETE FROM selection");
}

bool ClipboardDatabase::removeAll(bool preservePinned) {
QSqlQuery query(m_db);

if (preservePinned) {
return query.exec("DELETE FROM selection_fts") &&
query.exec("DELETE FROM data_offer WHERE selection_id IN (SELECT id FROM selection WHERE "
"pinned_at IS NULL)") &&
query.exec("DELETE FROM selection WHERE pinned_at IS NULL");
}

return removeAll();
}

std::vector<QString> ClipboardDatabase::removeSelection(const QString &selectionId) {
if (!m_db.transaction()) { return {}; }

Expand Down
1 change: 1 addition & 0 deletions src/server/src/services/clipboard/clipboard-db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class ClipboardDatabase {
const ClipboardListSettings &opts = {}) const;

bool removeAll();
bool removeAll(bool preservePinned);

bool setKeywords(const QString &id, const QString &keywords);
std::optional<QString> retrieveKeywords(const QString &id);
Expand Down
18 changes: 18 additions & 0 deletions src/server/src/services/clipboard/clipboard-service.cpp
Comment thread
andersmmg marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,24 @@ bool ClipboardService::removeAllSelections() {
return true;
}

bool ClipboardService::removeAllSelections(bool preservePinned) {
ClipboardDatabase db;

if (!db.removeAll(preservePinned)) {
qWarning() << "Failed to remove clipboard selections";
return false;
}

if (!preservePinned) {
fs::remove_all(m_dataDir);
fs::create_directories(m_dataDir);
}

emit allSelectionsRemoved();

return true;
}

AbstractClipboardServer *ClipboardService::clipboardServer() const { return m_clipboardServer.get(); }

ClipboardService::ClipboardService(const std::filesystem::path &path) {
Expand Down
1 change: 1 addition & 0 deletions src/server/src/services/clipboard/clipboard-service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class ClipboardService : public QObject, public NonCopyable {
static Clipboard::ReadContent readContent();

bool removeAllSelections();
bool removeAllSelections(bool preservePinned);

std::optional<QString> retrieveKeywords(const QString &id);
bool setKeywords(const QString &id, const QString &keywords);
Expand Down