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

Organize File IO #117

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft

Organize File IO #117

wants to merge 1 commit into from

Conversation

danirod
Copy link
Owner

@danirod danirod commented Jan 5, 2025

This PR will replace the src/file.rs mess with a proper GObject-based class hierarchy to manage saving data into files. Here is how it will work.

CarteroSerializable

This is the IDL for the interface:

public interface Cartero.Serializable : Gtk.Widget {
  // Whether the entity has changed since the last time it was read.
  public abstract bool dirty { get; set; }

  // Sets the widget state to the one defined in the given TOML string.
  public abstract void from_toml (contents: string);

  // Converts the widget state into a TOML string and returns it.
  public abstract string to_toml();
}

CarteroEndpointPane

The EndpointPane has been marked as Serializable. It has received a dirty property and it has lost the item_pane connection.

There are implementations for the Serializable methods:

  • from_toml(): converts the given TOML string and sets the pane state to the one in the string.
  • to_toml(): returns a TOML string with the current pane state.

CarteroItemPane

It expects now a child that implements CarteroSerializable. The new serializable() method will downcast the child into a Serializable, and it has been validated that the virtual methods of the interface can be called from the ItemPane now.

New methods:

  • load_pane(): async method that loads the contents of the file stored in the file property (if present), and updates the child pane state by calling from_toml() with the file contents.
  • save_pane(): async method that takes the current child pane state by calling to_toml() and saves the string into the file designated by the file property, if present.

Aside from that, the ItemPane will not care anymore about the type of the child (so it will not cast to EndpointPane directly).

CarteroWindow

It has been updated to use the new integration. The read file and save file actions, triggerable via buttons and menus, now will just call load_pane() or save_pane() on the currently visible pane. If there is no file associated with the ItemPane, it will still offer to assign a file first via the save file dialog.

src/file.rs

Most of the TOML serialization code has been scraped.

There is a new interface called Serializable. It allows a widget to
convert its data into a TOML string and to read data from a TOML string
by using the from_toml() and to_toml() virtual methods.

The EndpointPane has been modified so that it implements this interface
and knows how to persist its data through the RequestFile struct.

The ItemPane is finally using interfaces to communicate. It knows how to
load and save data from Serializable objects. As long as the child is a
Serializable, the load_pane() and save_pane() methods can be used to
load data from a gio::File and to save data into a gio::File by using
the Serializable virtual methods to get or assign the TOML
representation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant