Skip to content
Draft
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
13 changes: 13 additions & 0 deletions Pinta.Core/Classes/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// THE SOFTWARE.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Cairo;

Expand Down Expand Up @@ -140,6 +141,7 @@ public Gio.File? File {
/// <summary>
/// Whether the document is associated with a file on disk.
/// </summary>
[MemberNotNullWhen(true, nameof(File))]
public bool HasFile => file is not null;

/// Whether or not the document has been saved to the file that it is currently associated with in the
Expand Down Expand Up @@ -445,4 +447,15 @@ private void OnSelectionChanged ()
public event EventHandler? Renamed;
public event LayerCloneEvent? LayerCloned;
public event EventHandler? SelectionChanged;

private bool is_autosave_dirty;
public bool IsAutosaveDirty {
get => is_autosave_dirty;
set => is_autosave_dirty = value;
}
private uint autosave_files_index;
public uint AutosaveFilesIndex {
get => autosave_files_index;
set => autosave_files_index = value;
}
}
4 changes: 3 additions & 1 deletion Pinta.Core/Classes/DocumentHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ public void PushNewItem (BaseHistoryItem newItem)
history.Add (newItem);
Pointer = history.Count - 1;

if (newItem.CausesDirty)
if (newItem.CausesDirty) {
document.IsDirty = true;
document.IsAutosaveDirty = true;
}

HistoryItemAdded?.Invoke (this, new HistoryItemAddedEventArgs (newItem));
}
Expand Down
88 changes: 87 additions & 1 deletion Pinta.Core/Managers/WorkspaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,17 @@ public sealed class WorkspaceManager : IWorkspaceService
public WorkspaceManager (
SystemManager systemManager,
ChromeManager chromeManager,
ImageConverterManager imageFormats)
ImageConverterManager imageFormats,
SettingsManager settings)
{
open_documents = [];
OpenDocuments = new ReadOnlyCollection<Document> (open_documents);
SelectionHandler = new SelectionModeHandler (systemManager);

chrome_manager = chromeManager;
image_formats = imageFormats;

StartAutosaveTimer(settings);
}

public int ActiveDocumentIndex
Expand Down Expand Up @@ -465,6 +468,89 @@ private Task ShowFilePermissionErrorDialog (
return chrome_manager.ShowMessageDialog (parent, message, details);
}

private uint autosave_timer_id;
private uint autosave_files;
private int autosave_history;
private readonly HashSet<string> autosaveFiles = new();
private void StartAutosaveTimer (SettingsManager settings)
{
int interval = settings.GetSetting("autosave-interval", 0);
if (interval > 0) {
autosave_files = Math.Max(1, (uint)settings.GetSetting("autosave-files", 1));
autosave_history = settings.GetSetting("autosave-history", 0);
autosave_timer_id = GLib.Functions.TimeoutAdd(GLib.Constants.PRIORITY_DEFAULT, (uint)interval * 1000, OnAutosaveTick);
}
}
private bool OnAutosaveTick ()
{
if (!HasOpenDocuments)
return true;

var format = PintaCore.ImageFormats.GetFormatByExtension ("ora");
if (format?.Exporter is null) {
autosave_timer_id = 0;
return false;
}

foreach (var doc in OpenDocuments) {
if (!doc.HasFile)
continue;
if (!doc.IsAutosaveDirty)
continue;

string autosaveFile = doc.File.GetParseName() + ".pinta_autosave_" + doc.AutosaveFilesIndex + ".ora";
doc.AutosaveFilesIndex += 1;
if ( doc.AutosaveFilesIndex == autosave_files ) doc.AutosaveFilesIndex = 0;

format.Exporter.Export (doc, Gio.FileHelper.NewForPath (autosaveFile), PintaCore.Chrome.MainWindow);

autosaveFiles.Add(autosaveFile);
DumpLastHistorySteps(doc);
doc.IsAutosaveDirty = false;
}
if ( autosave_history != 0 ) {
Console.WriteLine($"Date: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
}

return true;
}
public void StopAutosave () {
if (autosave_timer_id != 0)
{
GLib.Source.Remove(autosave_timer_id);
}
foreach (var file_string in autosaveFiles)
{
try
{
var file = Gio.FileHelper.NewForPath(file_string);
if (file.QueryExists(null))
file.Delete(null);
}
catch
{
// ignore cleanup failures
}
}
autosaveFiles.Clear();
}
private void DumpLastHistorySteps(Document doc)
{
if ( autosave_history != 0 ) {
Console.WriteLine("=== Last History Steps ===");

var history = doc.History;
var n = history.Items.Count();
for (var i = Math.Max(0, n - autosave_history); i < n; i++)
{
var item = history.Items.ElementAt(i);
Console.WriteLine($"[{i}] {item.Text}");
}

Console.WriteLine("==========================");
}
}

public event EventHandler? LayerAdded;
public event EventHandler? LayerRemoved;
public event EventHandler? SelectedLayerChanged;
Expand Down
2 changes: 1 addition & 1 deletion Pinta.Core/PintaCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static PintaCore ()
// --- Services that depend on other services

ImageConverterManager imageFormats = new (settings);
WorkspaceManager workspace = new (system, chrome, imageFormats);
WorkspaceManager workspace = new (system, chrome, imageFormats, settings);
ToolManager tools = new (workspace, chrome);
PaletteManager palette = new (settings, paletteFormats);
ActionManager actions = new (chrome, imageFormats, paletteFormats, palette, recentFiles, system, tools, workspace);
Expand Down
2 changes: 2 additions & 0 deletions Pinta/Actions/File/ExitAction.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//

Check failure on line 1 in Pinta/Actions/File/ExitAction.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu (10.0.x)

Fix whitespace formatting. Replace 1 characters with '\n'.
// ExitProgramAction.cs

Check failure on line 2 in Pinta/Actions/File/ExitAction.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu (10.0.x)

Fix whitespace formatting. Replace 1 characters with '\n'.
//

Check failure on line 3 in Pinta/Actions/File/ExitAction.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu (10.0.x)

Fix whitespace formatting. Replace 1 characters with '\n'.
// Author:

Check failure on line 4 in Pinta/Actions/File/ExitAction.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu (10.0.x)

Fix whitespace formatting. Replace 1 characters with '\n'.
// Jonathan Pobst <monkey@jpobst.com>

Check failure on line 5 in Pinta/Actions/File/ExitAction.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu (10.0.x)

Fix whitespace formatting. Replace 1 characters with '\n'.
//

Check failure on line 6 in Pinta/Actions/File/ExitAction.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu (10.0.x)

Fix whitespace formatting. Replace 1 characters with '\n'.
// Copyright (c) 2010 Jonathan Pobst

Check failure on line 7 in Pinta/Actions/File/ExitAction.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu (10.0.x)

Fix whitespace formatting. Replace 1 characters with '\n'.
//

Check failure on line 8 in Pinta/Actions/File/ExitAction.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu (10.0.x)

Fix whitespace formatting. Replace 1 characters with '\n'.
// Permission is hereby granted, free of charge, to any person obtaining a copy

Check failure on line 9 in Pinta/Actions/File/ExitAction.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu (10.0.x)

Fix whitespace formatting. Replace 1 characters with '\n'.
// of this software and associated documentation files (the "Software"), to deal

Check failure on line 10 in Pinta/Actions/File/ExitAction.cs

View workflow job for this annotation

GitHub Actions / build-ubuntu (10.0.x)

Fix whitespace formatting. Replace 1 characters with '\n'.
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
Expand Down Expand Up @@ -67,6 +67,8 @@
return;
}

PintaCore.Workspace.StopAutosave();

// Let everyone know we are quitting
actions.App.RaiseBeforeQuit ();

Expand Down
Loading