iridium-window.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* iridium-window.c
  2. *
  3. * Copyright 2018 Matthias Vogelgesang
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <gtksourceview/gtksource.h>
  19. #include <webkit2/webkit2.h>
  20. #include "iridium-config.h"
  21. #include "iridium-markdown.h"
  22. #include "iridium-note.h"
  23. #include "iridium-note-row.h"
  24. #include "iridium-standard-file.h"
  25. #include "iridium-tag-row.h"
  26. #include "iridium-window.h"
  27. struct _IridiumWindow
  28. {
  29. GtkApplicationWindow parent_instance;
  30. GtkHeaderBar *header_bar;
  31. GtkListBox *tag_list;
  32. GtkListBox *note_list;
  33. GtkWidget *title_entry;
  34. GtkWidget *source_view;
  35. GtkWidget *main_pane;
  36. GtkSearchBar *search_bar;
  37. GtkSearchEntry *search_entry;
  38. GtkToggleButton *toggle_html_view;
  39. GtkRevealer *notification_revealer;
  40. WebKitWebView *html_view;
  41. GBinding *title_binding;
  42. GBinding *content_binding;
  43. IridiumMarkdown *markdown;
  44. IridiumStandardFile *client;
  45. };
  46. G_DEFINE_TYPE (IridiumWindow, iridium_window, GTK_TYPE_APPLICATION_WINDOW)
  47. static void
  48. on_tag_selected (IridiumWindow *self, IridiumTagRow *row, gpointer user_data)
  49. {
  50. gtk_list_box_invalidate_filter (self->note_list);
  51. }
  52. static void
  53. on_search_changed (IridiumWindow *self, GtkSearchEntry *entry)
  54. {
  55. gtk_list_box_invalidate_filter (self->note_list);
  56. }
  57. static void
  58. on_markdown_compiled (GObject *object,
  59. GAsyncResult *result,
  60. gpointer user_data)
  61. {
  62. IridiumWindow *window;
  63. gchar *html;
  64. GError *error = NULL;
  65. window = IRIDIUM_WINDOW (user_data);
  66. html = iridium_markdown_compile_finish (object, result, &error);
  67. webkit_web_view_load_html (window->html_view, html, NULL);
  68. g_free (html);
  69. }
  70. static void
  71. on_note_selected (IridiumWindow *self, IridiumNoteRow *row, gpointer user_data)
  72. {
  73. IridiumNote *note;
  74. GtkTextBuffer *buffer;
  75. GBindingFlags flags;
  76. GtkTextIter start;
  77. GtkTextIter end;
  78. gchar *text;
  79. if (self->title_binding != NULL)
  80. g_clear_object (&self->title_binding);
  81. if (self->content_binding != NULL)
  82. g_clear_object (&self->content_binding);
  83. if (row == NULL)
  84. return;
  85. note = iridium_note_row_get_note (row);
  86. flags = G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE;
  87. buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view));
  88. self->title_binding = g_object_bind_property (note, "title", self->title_entry, "text", flags);
  89. self->content_binding = g_object_bind_property (note, "content", buffer, "text", flags);
  90. gtk_text_buffer_get_start_iter (buffer, &start);
  91. gtk_text_buffer_get_end_iter (buffer, &end);
  92. text = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
  93. iridium_markdown_compile_async (self->markdown, text, G_PRIORITY_DEFAULT, NULL,
  94. on_markdown_compiled, self);
  95. g_free (text);
  96. }
  97. static void
  98. search_activated (GAction *action, GVariant *param, IridiumWindow *self)
  99. {
  100. gtk_search_bar_set_search_mode (self->search_bar, !gtk_search_bar_get_search_mode (self->search_bar));
  101. }
  102. static gboolean
  103. note_visible (IridiumNoteRow *row, IridiumWindow *window)
  104. {
  105. IridiumNote *note;
  106. IridiumTagRow *tag_row;
  107. tag_row = IRIDIUM_TAG_ROW (gtk_list_box_get_selected_row (window->tag_list));
  108. if (tag_row == NULL)
  109. return TRUE;
  110. note = iridium_note_row_get_note (row);
  111. if (!iridium_note_has_tag (note, iridium_tag_row_get_tag (tag_row))) {
  112. return FALSE;
  113. }
  114. if (gtk_search_bar_get_search_mode (window->search_bar) &&
  115. gtk_entry_get_text_length (GTK_ENTRY (window->search_entry)) > 0) {
  116. return iridium_note_matches_fuzzy (note, gtk_entry_get_text (GTK_ENTRY (window->search_entry)));
  117. }
  118. return TRUE;
  119. }
  120. static gint
  121. note_date_cmp (IridiumNoteRow *row1,
  122. IridiumNoteRow *row2,
  123. gpointer user_data)
  124. {
  125. GDateTime *dt1;
  126. GDateTime *dt2;
  127. dt1 = iridium_note_get_last_modified (iridium_note_row_get_note (row1));
  128. dt2 = iridium_note_get_last_modified (iridium_note_row_get_note (row2));
  129. /* sort later before earlier notes */
  130. return g_date_time_compare (dt2, dt1);
  131. }
  132. static void
  133. on_standard_file_connected (GObject *object,
  134. GAsyncResult *result,
  135. gpointer user_data)
  136. {
  137. IridiumWindow *self;
  138. GList *notes;
  139. IridiumTag *tag_all;
  140. self = IRIDIUM_WINDOW (user_data);
  141. tag_all = iridium_tag_new ("All");
  142. /* TODO: implement iridium_standard_file_connect_finish */
  143. notes = iridium_standard_file_load (self->client, "sn.json");
  144. for (GList *it = g_list_first (notes); it != NULL; it = g_list_next (it)) {
  145. IridiumNote *note;
  146. note = IRIDIUM_NOTE (it->data);
  147. iridium_note_add_tag (note, tag_all);
  148. gtk_list_box_insert (self->note_list, iridium_note_row_new (note), -1);
  149. }
  150. gtk_list_box_insert (self->tag_list, iridium_tag_row_new (tag_all), -1);
  151. g_list_free (notes);
  152. gtk_widget_show_all (GTK_WIDGET (self->note_list));
  153. gtk_widget_show_all (GTK_WIDGET (self->tag_list));
  154. gtk_revealer_set_reveal_child (self->notification_revealer, FALSE);
  155. }
  156. static void
  157. on_notification_close_clicked (IridiumWindow *self,
  158. GtkButton *button)
  159. {
  160. gtk_revealer_set_reveal_child (self->notification_revealer, FALSE);
  161. }
  162. static void
  163. iridium_window_dispose (GObject *object)
  164. {
  165. IridiumWindow *self;
  166. self = IRIDIUM_WINDOW (object);
  167. g_clear_object (&self->title_binding);
  168. g_clear_object (&self->content_binding);
  169. g_clear_object (&self->markdown);
  170. g_clear_object (&self->client);
  171. G_OBJECT_CLASS (iridium_window_parent_class)->dispose (object);
  172. }
  173. static void
  174. iridium_window_class_init (IridiumWindowClass *klass)
  175. {
  176. GObjectClass *oclass;
  177. GtkWidgetClass *widget_class;
  178. oclass = G_OBJECT_CLASS (klass);
  179. widget_class = GTK_WIDGET_CLASS (klass);
  180. oclass->dispose = iridium_window_dispose;
  181. gtk_widget_class_set_template_from_resource (widget_class, "/net/bloerg/Iridium/iridium-window.ui");
  182. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, header_bar);
  183. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, tag_list);
  184. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, note_list);
  185. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, source_view);
  186. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, title_entry);
  187. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, main_pane);
  188. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, search_bar);
  189. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, search_entry);
  190. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, html_view);
  191. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, toggle_html_view);
  192. gtk_widget_class_bind_template_child (widget_class, IridiumWindow, notification_revealer);
  193. gtk_widget_class_bind_template_callback (widget_class, on_tag_selected);
  194. gtk_widget_class_bind_template_callback (widget_class, on_note_selected);
  195. gtk_widget_class_bind_template_callback (widget_class, on_search_changed);
  196. gtk_widget_class_bind_template_callback (widget_class, on_notification_close_clicked);
  197. }
  198. static void
  199. iridium_window_init (IridiumWindow *self)
  200. {
  201. GtkSourceLanguageManager *manager;
  202. GtkStyleProvider *provider;
  203. GtkSourceBuffer *buffer;
  204. GSimpleAction *action;
  205. const gchar *server;
  206. const gchar *email;
  207. gchar *password;
  208. GError *error = NULL;
  209. gtk_widget_init_template (GTK_WIDGET (self));
  210. self->title_binding = NULL;
  211. self->content_binding = NULL;
  212. self->markdown = iridium_markdown_new ();
  213. action = g_simple_action_new ("search", NULL);
  214. g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (action));
  215. g_signal_connect (action, "activate", G_CALLBACK (search_activated), self);
  216. g_object_unref (action);
  217. g_object_bind_property (self->toggle_html_view, "active",
  218. self->html_view, "visible", G_BINDING_DEFAULT);
  219. gtk_search_bar_connect_entry (self->search_bar, GTK_ENTRY (self->search_entry));
  220. manager = gtk_source_language_manager_get_default ();
  221. buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view)));
  222. gtk_source_buffer_set_language (buffer, gtk_source_language_manager_get_language (manager, "markdown"));
  223. g_object_unref (manager);
  224. provider = GTK_STYLE_PROVIDER (gtk_css_provider_get_default ());
  225. gtk_css_provider_load_from_resource (GTK_CSS_PROVIDER (provider), "/net/bloerg/Iridium/iridium.css");
  226. gtk_style_context_add_provider_for_screen (gtk_window_get_screen (GTK_WINDOW (self)), provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
  227. self->client = iridium_standard_file_new ();
  228. server = "https://sf.bloerg.net";
  229. email = "matthias.vogelgesang@gmail.com";
  230. /* TODO: add dialog */
  231. password = secret_password_lookup_sync (STANDARD_FILE_SCHEMA, NULL, &error,
  232. "email", email, "server", server, NULL);
  233. gtk_revealer_set_reveal_child (self->notification_revealer, TRUE);
  234. iridium_standard_file_connect_async (self->client, server, email, password, NULL,
  235. on_standard_file_connected, self);
  236. secret_password_free (password);
  237. gtk_list_box_set_filter_func (self->note_list, (GtkListBoxFilterFunc) note_visible, self, NULL);
  238. gtk_list_box_set_sort_func (self->note_list, (GtkListBoxSortFunc) note_date_cmp, NULL, NULL);
  239. gtk_widget_show_all (GTK_WIDGET (self->tag_list));
  240. gtk_widget_show_all (GTK_WIDGET (self->note_list));
  241. }