Skip to content

Commit eb15526

Browse files
authored
Storage and Manager rewrite
Storage and Manager rewrite for clearer, more efficient code
2 parents 702beef + 245f333 commit eb15526

File tree

12 files changed

+233
-445
lines changed

12 files changed

+233
-445
lines changed

src/Application.vala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class Jorts.Application : Gtk.Application {
5050
public static bool new_from_clipboard = false;
5151
public static bool show_pref = false;
5252
public static bool reset_settings = false;
53+
public static bool dump_storage = false;
5354

5455
public const string ACTION_PREFIX = "app.";
5556
public const string ACTION_QUIT = "action_quit";
@@ -164,16 +165,17 @@ Please wait while the app remembers all the things...
164165
/* Either we show all sticky notes, or we load everything lol */
165166
if (manager.open_notes.size > 0) {
166167
foreach (var window in manager.open_notes) {
167-
if (window.visible) { window.present ();}
168+
if (window.visible) {window.present ();}
168169
}
169170
} else {
170-
manager.init_all_notes ();
171+
manager.init ();
171172
}
172173

173174
if (new_note) {manager.create_note (); new_note = false;}
174175
if (new_from_clipboard) {manager.from_clipboard (); new_from_clipboard = false;}
175176
if (show_pref) {action_show_preferences (); show_pref = false;}
176177
if (reset_settings) {action_reset_settings (); reset_settings = false;}
178+
if (dump_storage) {manager.dump (); dump_storage = false;}
177179
}
178180

179181
public static int main (string[] args) {
@@ -205,7 +207,7 @@ Please wait while the app remembers all the things...
205207

206208
private void action_save () {
207209
debug ("[ACTION] Saving...");
208-
manager.save_to_stash ();
210+
manager.save_all.begin ();
209211
}
210212

211213
private void action_reset_settings () {
@@ -233,7 +235,8 @@ Please wait while the app remembers all the things...
233235
{"new-note", 'n', OptionFlags.NONE, OptionArg.NONE, ref new_note, "Create a new note", null},
234236
{"new-from-clipboard", 'c', OptionFlags.NONE, OptionArg.NONE, ref new_from_clipboard, "Create a note then paste from clipboard", null},
235237
{"preferences", 'p', OptionFlags.NONE, OptionArg.NONE, ref show_pref, "Show preferences", null},
236-
{"reset-settings", 'r', OptionFlags.NONE, OptionArg.NONE, ref reset_settings, "Reset all settings", null}
238+
{"reset-settings", 'r', OptionFlags.NONE, OptionArg.NONE, ref reset_settings, "Reset all settings", null},
239+
{"dump", 'd', OptionFlags.NONE, OptionArg.NONE, ref dump_storage, "Dump the content of the storage as a pretty JSON", null}
237240
};
238241

239242
// We have to make an extra copy of the array, since .parse assumes

src/Objects/NoteData.vala

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66
*/
77

88

9-
/* Creation methods:
10-
vanilla
11-
from_json ();
12-
from_random ();
13-
14-
Helper:
15-
to_json ();
16-
17-
18-
*/
9+
/*************************************************/
10+
/**
11+
* An object used to package all data conveniently as needed.
12+
*/
1913
public class Jorts.NoteData : Object {
2014

15+
// Will determine properties (or lack thereof) for any new note
16+
public static string? latest_theme = Jorts.Constants.DEFAULT_THEME;
17+
public static int? latest_zoom = Jorts.Constants.DEFAULT_ZOOM;
18+
public static bool latest_mono = Jorts.Constants.DEFAULT_MONO;
19+
2120
public string title;
2221
public string theme;
2322
public string content;
@@ -26,6 +25,10 @@ public class Jorts.NoteData : Object {
2625
public int width;
2726
public int height;
2827

28+
/*************************************************/
29+
/**
30+
* Convert into a Json.Object()
31+
*/
2932
public NoteData (string? title = null, string? theme = null, string? content = null,
3033
bool? monospace = null, int? zoom = null, int? width = null, int? height = null)
3134
{
@@ -39,6 +42,10 @@ public class Jorts.NoteData : Object {
3942
this.height = height ?? Jorts.Constants.DEFAULT_HEIGHT;
4043
}
4144

45+
/*************************************************/
46+
/**
47+
* Parse a node to create an associated NoteData object
48+
*/
4249
public NoteData.from_json (Json.Object node) {
4350
title = node.get_string_member_with_default ("title",(_("Forgot title!")));
4451
theme = node.get_string_member_with_default ("theme",Jorts.Utils.random_theme (null));
@@ -48,12 +55,31 @@ public class Jorts.NoteData : Object {
4855

4956
// Make sure the values are nothing crazy
5057
if (zoom < Jorts.Constants.ZOOM_MIN) { zoom = Jorts.Constants.ZOOM_MIN;}
51-
else if (zoom > Jorts.Constants.ZOOM_MAX) { zoom = Jorts.Constants.ZOOM_MAX;}
58+
else if (zoom > Jorts.Constants.ZOOM_MAX) { zoom = Jorts.Constants.ZOOM_MAX;}
5259

5360
width = (int)node.get_int_member_with_default ("width",Jorts.Constants.DEFAULT_WIDTH);
5461
height = (int)node.get_int_member_with_default ("height",Jorts.Constants.DEFAULT_HEIGHT);
5562
}
5663

64+
/*************************************************/
65+
/**
66+
* Parse a node to create an associated NoteData object
67+
* We skip last chosen theme, and reuse latest set zoom and whether user prefers monospace
68+
*/
69+
public NoteData.from_random () {
70+
title = title ?? Jorts.Utils.random_title ();
71+
theme = theme ?? Jorts.Utils.random_theme (latest_theme);
72+
content = content ?? "";
73+
monospace = latest_mono;
74+
zoom = latest_zoom;
75+
width = Jorts.Constants.DEFAULT_WIDTH;
76+
height = Jorts.Constants.DEFAULT_HEIGHT;
77+
}
78+
79+
/*************************************************/
80+
/**
81+
* Used for storing NoteData inside disk storage
82+
*/
5783
public Json.Object to_json () {
5884
var builder = new Json.Builder ();
5985

src/Objects/Themes.vala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* 2025 Contributions from the ellie_Commons community (github.com/ellie-commons/)
66
*/
77

8+
/*************************************************/
9+
/**
10+
* A register of all themes we have
11+
*/
812
public enum Jorts.Themes {
913
BLUEBERRY,
1014
MINT,
@@ -33,6 +37,22 @@ public enum Jorts.Themes {
3337
}
3438
}
3539

40+
public Themes from_string (string wtf_is_this) {
41+
switch (wtf_is_this.ascii_up ()) {
42+
case "BLUEBERRY": return BLUEBERRY;
43+
case "MINT": return MINT;
44+
case "LIME": return LIME;
45+
case "BANANA": return BANANA;
46+
case "ORANGE": return ORANGE;
47+
case "STRAWBERRY": return STRAWBERRY;
48+
case "BUBBLEGUM": return BUBBLEGUM;
49+
case "GRAPE": return GRAPE;
50+
case "COCOA": return COCOA;
51+
case "SLATE": return SLATE;
52+
default: assert_not_reached ();
53+
}
54+
}
55+
3656
public static Themes[] all () {
3757
return {BLUEBERRY, MINT, LIME, BANANA, ORANGE, STRAWBERRY, BUBBLEGUM, GRAPE, COCOA, SLATE};
3858
}

src/Services/Constants.vala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ namespace Jorts.Constants {
2020
// signature theme
2121
const string DEFAULT_THEME = "BLUEBERRY";
2222
const int DAYS_BETWEEN_BACKUPS = 30;
23-
const string FILENAME_STASH = "saved_state.json";
24-
const string FILENAME_BACKUP = "backup_state.json";
23+
24+
2525

2626
// in ms
2727
const int DEBOUNCE = 1000;

src/Services/Jason.vala

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)