From 871a14ddc01e25ee436954c50c686e5a68c0b35a Mon Sep 17 00:00:00 2001 From: Jeena Date: Fri, 16 Feb 2024 21:21:05 +0900 Subject: [PATCH] Add NotesList to show notes of first calendar --- README.md | 2 +- src/jnotes.gresource.xml | 1 + src/main.py | 19 ++++++++++++++----- src/meson.build | 1 + src/notes_list.py | 21 +++++++++++++++++++++ src/preferences.py | 6 +++--- src/sidebar.py | 1 + src/sync.py | 27 +++++++++++++++++++++------ src/ui/notes_list.ui | 29 +++++++++++++++++++++++++++++ src/ui/preferences.ui | 2 +- src/ui/sidebar.ui | 8 ++------ src/ui/window.ui | 37 +++---------------------------------- src/window.py | 1 + 13 files changed, 99 insertions(+), 56 deletions(-) create mode 100644 src/notes_list.py create mode 100644 src/ui/notes_list.ui diff --git a/README.md b/README.md index 6aa73de..f0c6aea 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# jnotes +# JNotes This is a simple gtk4 libadwaita Notes program which uses CalDAV as a backend to store the notes in a calendar as VJOURNAL. diff --git a/src/jnotes.gresource.xml b/src/jnotes.gresource.xml index bc6b416..0f6b80e 100644 --- a/src/jnotes.gresource.xml +++ b/src/jnotes.gresource.xml @@ -5,5 +5,6 @@ ui/preferences.ui ui/help-overlay.ui ui/sidebar.ui + ui/notes_list.ui diff --git a/src/main.py b/src/main.py index 6e879da..b056c5c 100644 --- a/src/main.py +++ b/src/main.py @@ -28,6 +28,7 @@ from .window import JnotesWindow from .preferences import PreferencesWindow from .sidebar import Sidebar from .sync import Sync +from .notes_list import NotesList class JnotesApplication(Adw.Application): @@ -53,11 +54,19 @@ class JnotesApplication(Adw.Application): win = JnotesWindow(application=self) win.present() - self.calendar_set = Sync.get_calendar_set() - if not self.calendar_set: - self.on_preferences_action(win, False) - else: - win.sidebar.set_calendars(self.calendar_set) + def callback(calendar_set): + self.calendar_set = calendar_set + if not self.calendar_set: + self.on_preferences_action(win, False) + else: + win.sidebar.set_calendars(self.calendar_set) + def callb(calendar): + win.notes_list.set_calendar(calendar) + Sync.get_calenndar_notes(self.calendar_set[0], callb) + + Sync.set_spinner(win.sidebar.spinner) + Sync.get_calendar_set(callback) + def on_about_action(self, widget, _): """Callback for the app.about action.""" diff --git a/src/meson.build b/src/meson.build index f4fca4d..5f883ca 100644 --- a/src/meson.build +++ b/src/meson.build @@ -34,6 +34,7 @@ jnotes_sources = [ 'gsettings.py', 'sync.py', 'sidebar.py', + 'notes_list.py', ] install_data(jnotes_sources, install_dir: moduledir) diff --git a/src/notes_list.py b/src/notes_list.py new file mode 100644 index 0000000..f33178e --- /dev/null +++ b/src/notes_list.py @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +from gi.repository import Adw +from gi.repository import Gtk + +@Gtk.Template(resource_path='/net/jeena/jnotes/ui/notes_list.ui') +class NotesList(Gtk.ScrolledWindow): + __gtype_name__ = 'NotesList' + + notes_list = Gtk.Template.Child() + + def __init__(self, **kwargs): + super().__init__(**kwargs) + + def set_calendar(self, calendar): + self.notes_list.bind_model(calendar, self.create_item_widget) + + def create_item_widget(self, note): + print(note.summary) + return Gtk.Label(label=note.summary, halign="start", + hexpand=True, ellipsize=3) diff --git a/src/preferences.py b/src/preferences.py index f509296..20e2646 100644 --- a/src/preferences.py +++ b/src/preferences.py @@ -31,7 +31,7 @@ class PreferencesWindow(Adw.PreferencesWindow): server_url = Gtk.Template.Child() username = Gtk.Template.Child() password = Gtk.Template.Child() - spinner = Gtk.Template.Child() + preferences_spinner = Gtk.Template.Child() test_connection_row = Gtk.Template.Child() test_connection = Gtk.Template.Child() @@ -46,7 +46,7 @@ class PreferencesWindow(Adw.PreferencesWindow): server_url = self.server_url.get_text() username = self.username.get_text() password = self.password.get_text() - GLib.idle_add(self.spinner.start) + GLib.idle_add(self.preferences_spinner.start) GLib.idle_add(self.deactivate_test_button) Sync.test_connection(server_url, username, password, self.sync_ok_callback) @@ -72,7 +72,7 @@ class PreferencesWindow(Adw.PreferencesWindow): def sync_ok_callback(self, ok, e=None): self.activate_test_button() - self.spinner.stop() + self.preferences_spinner.stop() if ok: self.test_connection_row.set_icon_name("emblem-ok-symbolic") else: diff --git a/src/sidebar.py b/src/sidebar.py index 2404528..99c2425 100644 --- a/src/sidebar.py +++ b/src/sidebar.py @@ -25,6 +25,7 @@ class Sidebar(Adw.NavigationPage): __gtype_name__ = 'Sidebar' calendar_set = Gtk.Template.Child() + spinner = Gtk.Template.Child() def __init__(self, **kwargs): super().__init__(**kwargs) diff --git a/src/sync.py b/src/sync.py index 3ce508a..82fb6c8 100644 --- a/src/sync.py +++ b/src/sync.py @@ -40,6 +40,11 @@ def threaded(function: Callable): class Sync(): client = None principal = None + spinner = None + + @classmethod + def set_spinner(self, spinner): + self.spinner = spinner @classmethod @threaded @@ -70,8 +75,10 @@ class Sync(): logging.warning(e) @classmethod - def get_calendar_set(self): + @threaded + def get_calendar_set(self, callback): calendar_set = CalendarSet() + self.spinner.start() if not self.client: self.init() if self.principal: @@ -79,13 +86,21 @@ class Sync(): for remote_calendar in remote_calendars: if "VJOURNAL" in remote_calendar.get_supported_components(): calendar = Calendar(remote_calendar.get_display_name(), remote_calendar.url) - for journal in remote_calendar.journals(): - summary = journal.icalendar_component.get("summary", "") - description = journal.icalendar_component.get("description", "") - calendar.add_note(Note(calendar, summary, description)) calendar_set.add_calendar(calendar) + self.spinner.stop() + callback(calendar_set) - return calendar_set + @classmethod + @threaded + def get_calenndar_notes(self, calendar, callback): + self.spinner.start() + remote_calendar = self.principal.calendar(calendar.displayname) + for journal in remote_calendar.journals(): + summary = journal.icalendar_component.get("summary", "") + description = journal.icalendar_component.get("description", "") + calendar.add_note(Note(calendar, summary, description)) + self.spinner.stop() + callback(calendar) class CalendarSet(Gio.ListStore): def add_calendar(self, calendar): diff --git a/src/ui/notes_list.ui b/src/ui/notes_list.ui new file mode 100644 index 0000000..a225633 --- /dev/null +++ b/src/ui/notes_list.ui @@ -0,0 +1,29 @@ + + + + + diff --git a/src/ui/preferences.ui b/src/ui/preferences.ui index ad60865..993fe3e 100644 --- a/src/ui/preferences.ui +++ b/src/ui/preferences.ui @@ -33,7 +33,7 @@ Test Connection - + diff --git a/src/ui/sidebar.ui b/src/ui/sidebar.ui index bb07cd8..131fa2d 100644 --- a/src/ui/sidebar.ui +++ b/src/ui/sidebar.ui @@ -7,12 +7,8 @@ - - - list-add-symbolic - New Collection - win.new-collection - + + diff --git a/src/ui/window.ui b/src/ui/window.ui index e58f2cf..b6d534f 100644 --- a/src/ui/window.ui +++ b/src/ui/window.ui @@ -36,9 +36,7 @@ 200 - - - + @@ -58,37 +56,7 @@ - - - - 400 - 300 - - - vertical - 12 - 12 - 12 - - - Create new Note… - list-add-symbolic - - - - - False - none - - - - - - - - + @@ -99,3 +67,4 @@ + diff --git a/src/window.py b/src/window.py index 3e39749..758cc0e 100644 --- a/src/window.py +++ b/src/window.py @@ -25,6 +25,7 @@ class JnotesWindow(Adw.ApplicationWindow): __gtype_name__ = 'JnotesWindow' sidebar = Gtk.Template.Child() + notes_list = Gtk.Template.Child() def __init__(self, **kwargs): super().__init__(**kwargs)