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 publish action for PDF export #2618

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
39 changes: 35 additions & 4 deletions src/control/Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "jobs/AutosaveJob.h"
#include "jobs/CustomExportJob.h"
#include "jobs/PdfExportJob.h"
#include "jobs/PdfPublishJob.h"
#include "jobs/SaveJob.h"
#include "layer/LayerController.h"
#include "model/StrokeStyle.h"
Expand Down Expand Up @@ -122,6 +123,7 @@ Control::Control(GApplication* gtkApp, GladeSearchpath* gladeSearchPath): gtkApp
Control::~Control() {
g_source_remove(this->changeTimout);
this->enableAutosave(false);
this->enablePublish(false);

deleteLastAutosaveFile("");
this->scheduler->stop();
Expand Down Expand Up @@ -296,6 +298,7 @@ void Control::initWindow(MainWindow* win) {
this->clipboardHandler = new ClipboardHandler(this, win->getXournal()->getWidget());

this->enableAutosave(settings->isAutosaveEnabled());
this->enablePublish(settings->isPublishEnabled());

win->setFontButtonFont(settings->getFont());

Expand Down Expand Up @@ -324,6 +327,13 @@ auto Control::autosaveCallback(Control* control) -> bool {
return true;
}

auto Control::publishCallback(Control* control) -> bool {
// always publish, no check for isChangedAutosave because that event might have been triggered just before this one
g_message("Info: publish PDF...");
control->publishAsPdf();
return true;
}

void Control::enableAutosave(bool enable) {
if (this->autosaveTimeout) {
g_source_remove(this->autosaveTimeout);
Expand All @@ -336,6 +346,18 @@ void Control::enableAutosave(bool enable) {
}
}

void Control::enablePublish(bool enable) {
if (this->publishTimeout) {
g_source_remove(this->publishTimeout);
this->publishTimeout = 0;
}

if (enable) {
int timeout = settings->getPublishTimeout();
this->publishTimeout = g_timeout_add_seconds(timeout, reinterpret_cast<GSourceFunc>(publishCallback), this);
}
}

void Control::updatePageNumbers(size_t page, size_t pdfPage) {
if (this->win == nullptr) {
return;
Expand Down Expand Up @@ -388,6 +410,9 @@ void Control::actionPerformed(ActionType type, ActionGroup group, GdkEvent* even
case ACTION_EXPORT_AS_PDF:
exportAsPdf();
break;
case ACTION_PUBLISH_AS_PDF:
publishAsPdf();
break;
case ACTION_EXPORT_AS:
exportAs();
break;
Expand Down Expand Up @@ -1892,6 +1917,7 @@ void Control::showSettings() {
win->updateScrollbarSidebarPosition();

enableAutosave(settings->isAutosaveEnabled());
enablePublish(settings->isPublishEnabled());

this->zoom->setZoomStep(settings->getZoomStep() / 100.0);
this->zoom->setZoomStepScroll(settings->getZoomStepScroll() / 100.0);
Expand Down Expand Up @@ -2352,16 +2378,21 @@ void Control::updateWindowTitle() {

void Control::exportAsPdf() {
this->clearSelectionEndText();
exportBase(new PdfExportJob(this));
exportBase(new PdfExportJob(this), false);
}

void Control::publishAsPdf() {
this->clearSelectionEndText();
exportBase(new PdfPublishJob(this, this->settings->getPublishScript()), true);
}

void Control::exportAs() {
this->clearSelectionEndText();
exportBase(new CustomExportJob(this));
exportBase(new CustomExportJob(this), false);
}

void Control::exportBase(BaseExportJob* job) {
if (job->showFilechooser()) {
void Control::exportBase(BaseExportJob* job, bool silent) {
if (job->showFilechooser(silent)) {
this->scheduler->addJob(job, JOB_PRIORITY_NONE);
} else {
// The job blocked, so we have to unblock, because the job unblocks only after run
Expand Down
10 changes: 9 additions & 1 deletion src/control/Control.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ class Control:
bool annotatePdf(fs::path filepath, bool attachPdf, bool attachToDocument);
void print();
void exportAsPdf();
void publishAsPdf();
void exportAs();
void exportBase(BaseExportJob* job);
void exportBase(BaseExportJob* job, bool silent);
void quit(bool allowCancel = true);

/**
Expand Down Expand Up @@ -202,6 +203,7 @@ class Control:
void setCopyPasteEnabled(bool enabled);

void enableAutosave(bool enable);
void enablePublish(bool enable);

void clearSelectionEndText();

Expand Down Expand Up @@ -305,6 +307,7 @@ class Control:

static bool checkChangedDocument(Control* control);
static bool autosaveCallback(Control* control);
static bool publishCallback(Control* control);

void fontChanged();
/**
Expand Down Expand Up @@ -385,6 +388,11 @@ class Control:
int autosaveTimeout = 0;
fs::path lastAutosaveFilename;

/**
* The publish handler ID
*/
int publishTimeout = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is confusing; I initially thought this was the publication interval. Can you rename this to something like autoPublishHandler?


XournalScheduler* scheduler;

/**
Expand Down
44 changes: 26 additions & 18 deletions src/control/jobs/BaseExportJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ auto BaseExportJob::getFilterName() const -> string {
return gtk_file_filter_get_name(filter);
}

auto BaseExportJob::showFilechooser() -> bool {
auto BaseExportJob::showFilechooser(bool silent) -> bool {
initDialog();
addFilterToDialog();

Expand All @@ -57,29 +57,37 @@ auto BaseExportJob::showFilechooser() -> bool {
fs::path name = doc->createSaveFilename(Document::PDF, settings->getDefaultSaveName());
doc->unlock();

gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), true);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), Util::toGFilename(folder).c_str());
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), Util::toGFilename(name).c_str());

gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(this->control->getWindow()->getWindow()));

while (true) {
if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_OK) {
gtk_widget_destroy(dialog);
if (silent) {
// act as if user has pressed ok everytime
fs::path suggestedname = folder;
suggestedname /= name;
Util::clearExtensions(suggestedname);
if (!testAndSetFilepath(std::move(suggestedname))) {
return false;
}
auto file = Util::fromGFilename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
Util::clearExtensions(file);
// Since we add the extension after the OK button, we have to check manually on existing files
if (testAndSetFilepath(std::move(file)) && control->askToReplace(this->filepath)) {
break;
} else {
gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), true);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), Util::toGFilename(folder).c_str());
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), Util::toGFilename(name).c_str());

gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(this->control->getWindow()->getWindow()));

while (true) {
if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_OK) {
gtk_widget_destroy(dialog);
return false;
}
auto file = Util::fromGFilename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
Util::clearExtensions(file);
// Since we add the extension after the OK button, we have to check manually on existing files
if (testAndSetFilepath(std::move(file)) && control->askToReplace(this->filepath)) {
break;
}
}
gtk_widget_destroy(dialog);
}

settings->setLastSavePath(this->filepath.parent_path());

gtk_widget_destroy(dialog);

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/control/jobs/BaseExportJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BaseExportJob: public BlockingJob {
virtual void afterRun();

public:
virtual bool showFilechooser();
virtual bool showFilechooser(bool silent);
string getFilterName() const;

protected:
Expand Down
4 changes: 2 additions & 2 deletions src/control/jobs/CustomExportJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ auto CustomExportJob::testAndSetFilepath(fs::path file) -> bool {
return checkOverwriteBackgroundPDF(filepath);
}

auto CustomExportJob::showFilechooser() -> bool {
if (!BaseExportJob::showFilechooser()) {
auto CustomExportJob::showFilechooser(bool silent) -> bool {
if (!BaseExportJob::showFilechooser(silent)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/control/jobs/CustomExportJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CustomExportJob: public BaseExportJob {
void run();

public:
virtual bool showFilechooser();
virtual bool showFilechooser(bool silent);

protected:
virtual void afterRun();
Expand Down
2 changes: 2 additions & 0 deletions src/control/jobs/PdfExportJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void PdfExportJob::run() {
} else {
this->errorMsg = pdfe->getLastError();
}
} else {
afterExport();
}

delete pdfe;
Expand Down
1 change: 1 addition & 0 deletions src/control/jobs/PdfExportJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PdfExportJob: public BaseExportJob {
protected:
virtual void addFilterToDialog();
bool testAndSetFilepath(fs::path file) override;
virtual void afterExport(){};

private:
};
28 changes: 28 additions & 0 deletions src/control/jobs/PdfPublishJob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "PdfPublishJob.h"

#include "control/Control.h"

#include "i18n.h"

PdfPublishJob::PdfPublishJob(Control* control, const string& script): PdfExportJob(control) { this->script = script; }

PdfPublishJob::~PdfPublishJob() = default;

void PdfPublishJob::afterExport() {
PdfExportJob::afterExport();

if (script != "") {
string modscript(script);
string parameter = "$1";
size_t pos = 0;
while (true) {
pos = modscript.find(parameter, pos);
if (pos == string::npos)
break;
modscript.erase(pos, parameter.length());
modscript.insert(pos, string(this->filepath.c_str()));
pos += 1;
}
system(modscript.c_str());
}
}
26 changes: 26 additions & 0 deletions src/control/jobs/PdfPublishJob.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Xournal++
*
* A job to export and publish PDF
*
* @author Xournal++ Team
* https://github.com/xournalpp/xournalpp
*
* @license GNU GPLv2 or later
*/

#pragma once

#include "PdfExportJob.h"

class PdfPublishJob: public PdfExportJob {
public:
PdfPublishJob(Control* control, const string& script);

protected:
virtual void afterExport();
virtual ~PdfPublishJob();

private:
string script;
};
Loading