File: application.vala

package info (click to toggle)
xfce4-notes-plugin 1.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,292 kB
  • sloc: ansic: 13,235; sh: 5,064; makefile: 303; sed: 16
file content (1089 lines) | stat: -rw-r--r-- 30,812 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
/*
 *  Notes - panel plugin for Xfce Desktop Environment
 *  Copyright (c) 2009-2010  Mike Massonnet <mmassonnet@xfce.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

using Gtk;
using Xfconf;

namespace Xnp {

	public class Application : GLib.Object {

		public string notes_path { get; set construct; }
		public string config_file { get; construct; }
		public bool system_tray_mode = false;

		private SList<Xnp.WindowMonitor> window_monitor_list;
		private SList<Xnp.Window> window_list;
		private SList<Xnp.Window> focus_order;
		private Xfconf.Channel xfconf_channel;
		private bool lock_focus_order = false;
		private uint save_config_timeout = 0;
		private string default_notes_path;
		private Xnp.Theme theme;

		private bool main_instance {
			get {
				if (this.system_tray_mode) {
					return true;
				}

				Gtk.Application app = new Gtk.Application ("org.xfce.Notes", 0);
				app.activate.connect (() => {;});
				try {
					app.register ();
				} catch (GLib.Error e) {
				}

				if (app.is_remote) {
					return false;
				}

				app.run ();
				app.quit ();

				return true;
			}
		}

		private bool _skip_taskbar_hint = true;
		public bool skip_taskbar_hint {
			get {
				return this._skip_taskbar_hint;
			}
			set {
				if (this._skip_taskbar_hint == value)
					return;
				this._skip_taskbar_hint = value;
				foreach (var win in this.window_list)
					win.skip_taskbar_hint = value;
			}
		}

		public Xnp.Window? next_focus {
			get {
				if (this.lock_focus_order)
					return null;
				var win_count = this.focus_order.length ();
				if (win_count < 2)
					return null;
				var window = this.focus_order.nth_data (win_count - 2);
				return window.get_visible () ? window : null;
			}
		}

		construct {
			try {
				Xfce.posix_signal_handler_init ();
				Xfce.posix_signal_handler_set_handler(ProcessSignal.TERM, quit);
				Xfce.posix_signal_handler_set_handler(ProcessSignal.INT, quit);
			}
			catch (GLib.Error e) {
				critical ("Unable to connect to UNIX signals. %s", e.message);
			}

			try {
				Xfconf.init ();
			}
			catch (GLib.Error e) {
				critical ("%s", e.message);
			}

			xfconf_channel = new Xfconf.Channel.with_property_base ("xfce4-panel", "/plugins/notes");
			update_version ();

			theme = new Xnp.Theme ();
			update_color ();
			xfconf_channel.property_changed["/global/background-color"].connect (() => {
				update_color ();
			});

			Xfconf.property_bind (xfconf_channel, "/global/skip-taskbar-hint",
				typeof (bool), this, "skip-taskbar-hint");

			default_notes_path = "%s/notes".printf (GLib.Environment.get_user_data_dir ());
			if (notes_path == null) {
				notes_path = xfconf_channel.get_string ("/global/notes-path", default_notes_path);
			}
			xfconf_channel.property_changed["/global/notes-path"].connect (() => {
				update_notes_path ();
			});

			string name;
			bool found = false;
			try {
				/* Load existing windows */
				SList<string> groups = null;
				var dir = Dir.open (notes_path, 0);
				while ((name = dir.read_name ()) != null) {
					groups.prepend (name);
					found = true;
				}
				sort_groups_by_focus_order (ref groups);
				foreach (var group_name in groups) {
					create_window (group_name);
				}
			}
			catch (GLib.Error e) {
				GLib.DirUtils.create_with_parents (notes_path, 0700);
			}
			if (found == false) {
				/* Create first-run window */
				create_window ();
			}

			Gtk.Window.set_default_icon_name ("org.xfce.notes");
		}

		public Application (string config_file) {
			GLib.Object (config_file: config_file);
		}

		public Application.with_notes_path (string config_file, string notes_path) {
			GLib.Object (config_file: config_file, notes_path: notes_path);
		}

		~Application () {
			xfconf_channel = null;
			Xfconf.shutdown ();
			if (this.save_config_timeout != 0) {
				Source.remove (this.save_config_timeout);
			}
			foreach (var win in this.window_list) {
				win.destroy ();
				win = null;
			}
		}

		private void update_version () {
			var version = xfconf_channel.get_string ("/global/version", "0");
			if (version == Config.PACKAGE_VERSION)
				return;
			if (version < "1.11") {
				try {
					var css = Xfce.resource_save_location (Xfce.ResourceType.CONFIG, "xfce4/xfce4-notes.css", false);
					var css_file = File.new_for_path (css);
					css_file.delete ();
					var old_save_location = Xfce.resource_save_location (Xfce.ResourceType.CONFIG, "xfce4/xfce4-notes.rc", false);
					var old_save_location_file = File.new_for_path (old_save_location);
					if (old_save_location_file.query_exists ()) {
						var save_location = Xfce.resource_save_location (Xfce.ResourceType.CONFIG, "xfce4/notes/xfce4-notes.rc", true);
						var save_location_file = File.new_for_path (save_location);
						old_save_location_file.move (save_location_file, FileCopyFlags.NONE);
					}
				} catch (GLib.Error e) {
				}
			}
			xfconf_channel.set_string ("/global/version", Config.PACKAGE_VERSION);
		}

		private void update_notes_path () {
			var new_notes_path = xfconf_channel.get_string ("/global/notes-path", this.default_notes_path);
			if (this.notes_path == new_notes_path) {
				return;
			}

			/*
			 * Handle the case when panel plugin and system tray
			 * application are running at the same time
			 */
			if (!this.main_instance) {
				this.notes_path = new_notes_path;
				return;
			}

			/* Check that the new path is empty */
			try {
				var dir = Dir.open (new_notes_path, 0);
				if (dir.read_name () != null) {
					notes_path_error (_("The selected directory (%s) for the new notes path already contains files. "
							    + "You must select or create an empty directory.").printf (new_notes_path));
					return;
				}
			}
			catch (GLib.Error e) {
			}

			/* Create/move to the new path */
			var dirname = Path.get_dirname (new_notes_path);
			if (GLib.DirUtils.create_with_parents (dirname, 0700) != 0
			    || GLib.FileUtils.rename (this.notes_path, new_notes_path) != 0) {
				notes_path_error (_("Unable to select directory for new notes path: %s").printf (strerror (errno)));
				return;
			}

			this.notes_path = new_notes_path;
		}

		private void notes_path_error (string message) {
			var error_dialog = new Gtk.MessageDialog (null, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, _("Notes path is unacceptable"));
			error_dialog.format_secondary_text (message);
			error_dialog.icon_name = "gtk-dialog-error";
			error_dialog.title = _("Error");
			error_dialog.run ();
			error_dialog.destroy ();
			if (this.notes_path == this.default_notes_path) {
				xfconf_channel.reset_property ("/global/notes-path", false);
			} else {
				xfconf_channel.set_string ("/global/notes-path", this.notes_path);
			}
		}

		private void update_color () {
			string color = xfconf_channel.get_string ("/global/background-color", "#F7EB96");
			if (color == "GTK+")
				theme.use_gtk_style ();
			else
				theme.use_color (color);
		}

		/**
		 * sort_groups_by_focus_order:
		 *
		 * Sort groups by last memorized windows focus order.
		 */
		private void sort_groups_by_focus_order (ref SList<string> groups) {
			try {
				var last_focus_order = new SList<string> ();
				var keyfile = new GLib.KeyFile ();
				unowned SList<string> entry;
				keyfile.load_from_file (this.config_file, GLib.KeyFileFlags.NONE);
				foreach (var name in keyfile.get_groups ()) {
					last_focus_order.prepend (name);
				}
				foreach (var name in last_focus_order) {
					if ((entry = groups.find_custom (name, strcmp)) != null) {
						groups.prepend (entry.data);
						groups.remove_link (entry);
					}
				}
			}
			catch (GLib.Error e) {
			}
		}

		public void quit () {
			// Save notes before leaving the main loop since it works with GObject signals
			save_notes ();
			save_windows_configuration ();
			Gtk.main_quit ();
		}

		/*
		 * Window management
		 */

		/**
		 * create_window:
		 *
		 * Creates a new Xnp.Window and stores it inside window_list.
		 * If a name is given, it assumes it can load existing notes.
		 */
		public Xnp.Window? create_window (string? name = null) {
			var window = new Xnp.Window (this);

			/* Default settings */
			if (name == null) {
				window.above = xfconf_channel.get_bool ("/new-window/always-on-top", false);
				window.sticky = xfconf_channel.get_bool ("/new-window/sticky", true);
				int width = xfconf_channel.get_int ("/new-window/width", 0);
				int height = xfconf_channel.get_int ("/new-window/height", 0);
				if (width > 0 && height > 0) {
					window.resize (width, height);
				}
			}

			/* Set window name */
			if (name == null) {
				string window_name = _("Notes");
				int len = (int)this.window_list.length ();
				for (int id = 1; id <= len + 1; id++) {
					if (id > 1) {
						window_name = _("Notes %d").printf (id);
					}
					if (!window_name_exists (window_name)) {
						break;
					}
				}
				window.name = window_name;
			}
			else {
				window.name = name;
			}

			/* Add to window_list */
			this.window_list.insert_sorted (window, (GLib.CompareFunc)window.compare_func);
			this.focus_order.append (window);

			/* Insert initial notes */
			string window_path = "%s/%s".printf (notes_path, window.name);
			if (name == null || !GLib.FileUtils.test (window_path, GLib.FileTest.IS_DIR|GLib.FileTest.EXISTS)) {
				try {
					GLib.DirUtils.create_with_parents (window_path, 0700);
					string note_path = "%s/%s".printf (window_path, _("Note %d").printf (1));
					GLib.FileUtils.set_contents (note_path, "", -1);
					this.load_window_data (window);
				}
				catch (FileError e) {
					window.popup_error (e.message);
					destroy_window (window);
					return null;
				}
			}
			else {
				this.load_window_data (window);
			}

			/* Window monitor */
			window_monitor_list_add (window);

			/* Global settings */
			Xfconf.property_bind (xfconf_channel, "/global/tabs-position",
				typeof (int), window, "tabs-position");

			window.skip_taskbar_hint = this.skip_taskbar_hint;

			/* Connect signals */
			window.action.connect ((win, action) => {
				if (action == "hide") {
					if (this.lock_focus_order)
						return;
					int hidden_windows = 0;
					foreach (var w in this.focus_order) {
						if (!w.get_visible ()) {
							hidden_windows++;
						}
					}
					this.focus_order.remove (window);
					this.focus_order.insert (window, hidden_windows - 1);
				}
				else if (action == "rename") {
					rename_window (win);
					set_data_value (win, "internal-change", true);
				}
				else if (action == "delete") {
					delete_window (win);
					set_data_value (win, "internal-change", true);
				}
				else if (action == "create-new-window") {
					var new_win = create_window ();
					if (new_win == null)
						return;
					new_win.show ();
					set_data_value (win, "internal-change", true);
				}
				else if (action == "refresh-notes") {
					refresh_notes (win);
				}
				else if (action == "properties") {
					open_settings_dialog ();
				}
				else if (action == "about") {
					open_about_dialog ();
				}
			});
			window.save_data.connect ((win, note) => {
				if (!get_data_value (win, "external-change")) {
					set_data_value (win, "internal-change", true);
					save_note (win, note);
				}
			});
			window.note_inserted.connect ((win, note) => {
				Xfconf.property_bind (xfconf_channel, "/global/font-description",
					typeof (string), note.text_view, "font");

				string path = "%s/%s/%s".printf (notes_path, win.name, note.name);
				try {
					note.backed = false;
					GLib.FileUtils.set_contents (path, "", -1);
					set_data_value (win, "internal-change", true);
					note.backed = true;
				}
				catch (FileError e) {
					win.popup_error (e.message);
				}
			});
			window.note_deleted.connect ((win, note) => {
				try {
					string path = "%s/%s/%s".printf (notes_path, win.name, note.name);
					File.new_for_path (path).delete ();
					set_data_value (win, "internal-change", true);
					note.backed = false;
				}
				catch (GLib.Error e) {
					win.popup_error (e.message);
				}
			});
			window.note_renamed.connect ((win, note, name) => {
				if (!name_is_valid (name)) {
					return;
				}
				try {
					string path = "%s/%s/%s".printf (notes_path, win.name, note.name);
					var note_file = File.new_for_path (path);
					note_file.set_display_name (name);
					set_data_value (win, "internal-change", true);
					note.name = name;
				}
				catch (GLib.Error e) {
					win.popup_error (e.message);
				}
			});
			/*
			 * When working in system tray mode, save windows configuration
			 * two seconds after going to the background
			 */
			window.notify["is-active"].connect (() => {
				if (this.save_config_timeout > 0) {
					Source.remove (this.save_config_timeout);
					this.save_config_timeout = 0;
				}

				if (window.is_active) {
					this.focus_order.remove (window);
					this.focus_order.append (window);
				}
				else if (this.system_tray_mode) {
					this.save_config_timeout = Timeout.add_seconds (2, save_windows_configuration);
				}
			});
			/* Handle exchange of tabs between windows */
			window.note_moved.connect ((to_win, from_win, note) => {
				string from_path = "%s/%s/%s".printf (notes_path, from_win.name, note.name);
				string to_path = "%s/%s/%s".printf (notes_path, to_win.name, note.name);
				var from_file = File.new_for_path (from_path);
				var to_file = File.new_for_path (to_path);
				try {
					from_file.move (to_file, FileCopyFlags.NONE);
					set_data_value (from_win, "internal-change", true);
					set_data_value (to_win, "internal-change", true);
					var tab_evbox = from_win.get_tab_evbox (note);
					from_win.disconnect_note_signals (note, tab_evbox);
					to_win.connect_note_signals (note, tab_evbox);
					return true;
				}
				catch (GLib.Error e) {
					to_win.popup_error (e.message);
					return false;
				}
			});

			return window;
		}

		/**
		 * load_window_data:
		 *
		 * Load existing notes and configuration inside the window.
		 */
		private void load_window_data (Xnp.Window window) {
			/* Load notes */
			string name;
			string path = "%s/%s".printf (notes_path, window.name);
			try {
				var dir = GLib.Dir.open (path, 0);
				while ((name = dir.read_name ()) != null) {
					try {
						string contents;
						var file = File.new_for_path ("%s/%s".printf (path, name));
						GLib.FileUtils.get_contents (file.get_path (), out contents, null);
						var note = window.insert_note (name);
						note.text = contents;
						Xfconf.property_bind (xfconf_channel, "/global/font-description",
								typeof (string), note.text_view, "font");
						note.backed = true;
					}
					catch (FileError e) {
						warning ("%s", e.message);
					}
				}
			}
			catch (FileError e) {
			}

			/* Load window configuration */
			var keyfile = new GLib.KeyFile ();
			try {
				keyfile.load_from_file (config_file, GLib.KeyFileFlags.NONE);
				int winx = keyfile.get_integer (window.name, "PosX");
				int winy = keyfile.get_integer (window.name, "PosY");
				int width = keyfile.get_integer (window.name, "Width");
				int height = keyfile.get_integer (window.name, "Height");
				string[] tabs_order = keyfile.get_string_list (window.name, "TabsOrder");
				int last_page = keyfile.get_integer (window.name, "LastTab");
				bool above = keyfile.get_boolean (window.name, "Above");
				bool sticky = keyfile.get_boolean (window.name, "Sticky");
				double opacity = 1 - (double)keyfile.get_integer (window.name, "Transparency") / 100;
				bool visible = keyfile.get_boolean (window.name, "Visible");

				window.move (winx, winy);
				window.resize (width, height);
				for (int i = 0; i < tabs_order.length; i++)
					window.move_note (tabs_order[i], i);
				window.set_current_page (last_page);
				window.above = above;
				window.sticky = sticky;
				window.opacity = opacity;
				if (visible)
					window.show ();
			}
			catch (GLib.Error e) {
				window.above = xfconf_channel.get_bool ("/new-window/always-on-top", false);
				window.sticky = xfconf_channel.get_bool ("/new-window/sticky", true);
				int width = xfconf_channel.get_int ("/new-window/width", 0);
				int height = xfconf_channel.get_int ("/new-window/height", 0);
				if (width > 0 && height > 0) {
					window.resize (width, height);
				}
				window.show ();
			}
		}

		/**
		 * save_windows_configuration:
		 *
		 * Save window configuration inside rc file.
		 */
		public bool save_windows_configuration () {
			var keyfile = new GLib.KeyFile ();
			string old_contents;
			try {
				GLib.FileUtils.get_contents (config_file, out old_contents);
			}
			catch (FileError e) {
			}
			try {
				foreach (var win in this.focus_order) {
					int winx, winy, width, height;
					win.get_geometry (out winx, out winy, out width, out height);
					string[] tabs_order = win.get_note_names ();
					int last_page = win.get_current_page ();
					int transparency = (int)((1 - win.opacity) * 100);
					bool visible = win.get_visible ();

					keyfile.set_integer (win.name, "PosX", winx);
					keyfile.set_integer (win.name, "PosY", winy);
					keyfile.set_integer (win.name, "Width", width);
					keyfile.set_integer (win.name, "Height", height);
					keyfile.set_string_list (win.name, "TabsOrder", tabs_order);
					keyfile.set_integer (win.name, "LastTab", last_page);
					keyfile.set_boolean (win.name, "Above", win.above);
					keyfile.set_boolean (win.name, "Sticky", win.sticky);
					keyfile.set_double (win.name, "Transparency", transparency);
					keyfile.set_boolean (win.name, "Visible", visible);
				}
				string contents = keyfile.to_data (null);
				if (contents != old_contents)
					GLib.FileUtils.set_contents (config_file, contents);
			}
			catch (FileError e) {
				message ("Unable to save window configuration from %s: %s", config_file, e.message);
			}
			if (this.save_config_timeout > 0) {
				Source.remove (this.save_config_timeout);
				this.save_config_timeout = 0;
			}
			return false;
		}

		/**
		 * save_notes:
		 *
		 * Save the contents of every existing notes.
		 */
		public void save_notes () {
			foreach (var win in this.window_list) {
				set_data_value (win, "external-change", false);
				win.save_notes ();
			}
		}

		/**
		 * save_note:
		 *
		 * Save the contents of the given note.
		 */
		private void save_note (Xnp.Window window, Xnp.Note note) {
			string path = "%s/%s/%s".printf (notes_path, window.name, note.name);
			string old_contents;
			try {
				GLib.FileUtils.get_contents (path, out old_contents);
			}
			catch (FileError e) {
			}
			try {
				string contents = note.text;
				if (contents != old_contents)
					GLib.FileUtils.set_contents (path, contents, -1);
				note.dirty = false;
				note.backed = true;
			}
			catch (FileError e) {
				window.popup_error (e.message);
			}
		}

		/**
		 * rename_window:
		 *
		 * Rename the window.
		 */
		private void rename_window (Xnp.Window window) {
			var dialog = new Gtk.Dialog.with_buttons (_("Rename group"), window,
					Gtk.DialogFlags.DESTROY_WITH_PARENT,
					"gtk-cancel", Gtk.ResponseType.CANCEL, "gtk-ok", Gtk.ResponseType.OK);
			Gtk.Box content_area = (Gtk.Box)dialog.get_content_area ();
			dialog.set_default_response (Gtk.ResponseType.OK);
			dialog.resizable = false;
			dialog.icon_name = "gtk-edit";
			dialog.border_width = 4;

			var entry = new Gtk.Entry ();
			entry.text = window.name;
			entry.activates_default = true;
			content_area.add (entry);
			content_area.show_all ();

			int res = dialog.run ();
			window.dialog_hide (dialog);
			if (res == Gtk.ResponseType.OK) {
				weak string name = entry.text;
				if (window_name_exists (name)) {
					window.popup_error (_("The name %s is already in use").printf (name));
				}
				else {
					if (!name_is_valid (name)) {
						return;
					}
					try {
						string path = "%s/%s".printf (notes_path, window.name);
						var group_dir = File.new_for_path (path);
						group_dir.set_display_name (name);
						window.name = name;
						this.window_list.sort ((GLib.CompareFunc)window.compare_func);

						window_monitor_list_remove (window);
						window_monitor_list_add (window);
					} catch (GLib.Error e) {
						window.popup_error (e.message);
					}
				}
			}
			dialog.destroy ();
		}

		/**
		 * delete_window:
		 *
		 * Delete the window.
		 */
		private void delete_window (Xnp.Window window) {
			if (!window.empty) {
				var dialog = new Gtk.MessageDialog (window, Gtk.DialogFlags.DESTROY_WITH_PARENT,
						Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, _("Are you sure you want to delete this group?"));
				dialog.icon_name = "gtk-delete";
				dialog.title = window.name;
				int res = dialog.run ();
				window.dialog_destroy (dialog);
				if (res != Gtk.ResponseType.YES)
					return;
			}

			string name;
			string path = "%s/%s".printf (notes_path, window.name);
			try {
				var dir = GLib.Dir.open (path, 0);
				while ((name = dir.read_name ()) != null) {
					File.new_for_path ("%s/%s".printf (path, name)).delete ();
				}
				File.new_for_path (path).delete ();
			}
			catch (GLib.Error e) {
				window.popup_error (e.message);
				name = window.name;
				destroy_window (window);
				var win = create_window (name);
				if (win != null)
					win.show ();
				return;
			}

			if (this.window_list.length () < 2) {
				destroy_window (window);
				var new_win = create_window ();
				if (new_win != null)
					new_win.show ();
			} else {
				var new_focus = this.next_focus;
				if (new_focus != null) {
					new_focus.skip_taskbar_hint = false;
					destroy_window (window);
					new_focus.skip_taskbar_hint = this.skip_taskbar_hint;
				} else {
					destroy_window (window);
				}
			}
		}

		/**
		 * destroy_window:
		 *
		 * Destroy window and forget it exists.
		 */
		private void destroy_window (Xnp.Window window) {
			window_monitor_list_remove (window);
			this.window_list.remove (window);
			this.focus_order.remove (window);
			window.destroy ();
		}

		/**
		 * refresh_notes:
		 *
		 * Prompt for reloading notes from disk.
		 */
		private void refresh_notes (Xnp.Window window) {
			var dialog = new Gtk.MessageDialog (window, Gtk.DialogFlags.DESTROY_WITH_PARENT,
				Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO,
				_("The group \"%s\" has been modified on the disk"), window.name);
			dialog.set_title (window.name);
			dialog.set_icon_name ("org.xfce.notes");
			dialog.format_secondary_text (_("Do you want to reload the group?"));
			var res = dialog.run ();
			window.dialog_destroy (dialog);

			if (res == Gtk.ResponseType.YES) {
				save_windows_configuration ();
				// Delete existing window object
				var name = window.name;
				destroy_window (window);
				// Create new window object
				var win = create_window (name);
				if (win != null)
					win.show ();
			}
			else {
				set_data_value (window, "external-change", false);
				window.show_refresh_button = false;
				window.save_notes ();

			}
		}

		/**
		 * get_window_list:
		 *
		 * Get the window_list property value.
		 */
		public unowned SList<Xnp.Window> get_window_list () {
			return window_list;
		}

		/*
		 * Window monitor list management
		 */

		/**
		 * window_monitor_list_add:
		 *
		 * Creates an Xnp.WindowMonitor object and stores it inside window_monitor_list.
		 */
		private void window_monitor_list_add (Xnp.Window window) {
			var file = File.new_for_path ("%s/%s".printf (notes_path, window.name));
			var monitor = new Xnp.WindowMonitor (window, file);

			monitor.window_updated.connect ((window) => {
				if (get_data_value (window, "internal-change")) {
					set_data_value (window, "internal-change", false);
				}
				else {
					set_data_value (window, "external-change", true);
					window.show_refresh_button = true;
				}
			});

			this.window_monitor_list.prepend (monitor);
		}

		/**
		 * window_monitor_list_remove:
		 *
		 * Removes a monitor from window_monitor_list matching @window.
		 */
		private void window_monitor_list_remove (Xnp.Window window) {
			var monitor = window_monitor_list_lookup (window);
			if (monitor != null) {
				this.window_monitor_list.remove (monitor);
			}
		}

		/**
		 * window_monitor_list_lookup:
		 *
		 * Returns the window_monitor object that contains @window from the window_monitor_list.
		 */
		private Xnp.WindowMonitor window_monitor_list_lookup (Xnp.Window window) {
			Xnp.WindowMonitor window_monitor = null;
			foreach (var monitor in this.window_monitor_list) {
				if (monitor.window == window) {
					window_monitor = monitor;
					break;
				}
			}
			return window_monitor;
		}

		/*
		 * Utility functions
		 */

		/**
		 * get_data_value:
		 *
		 * Convenience function to return a GObject data boolean value.
		 */
		private bool get_data_value (GLib.Object object, string data) {
			return object.get_data<bool> (data);
		}

		/**
		 * set_data_value:
		 *
		 * Convenience function to set a GObject data boolean value.
		 */
		private void set_data_value (GLib.Object object, string data, bool val) {
			object.set_data (data, ((int)val).to_pointer ());
		}

		/**
		 * window_name_exists:
		 *
		 * Verify if the given name already exists in the window list.
		 */
		private bool window_name_exists (string name) {
			foreach (var win in this.window_list) {
				if (win.name == name) {
					return true;
				}
			}
			return false;
		}

		/**
		 * name_is_valid:
		 *
		 * Verify if the given name is valid for window and notes.
		 */
		private bool name_is_valid (string name) {
			bool res = GLib.Regex.match_simple ("^[^*|/\\:\"<>?]+$", name);
			if (!res) {
				var error_dialog = new Gtk.MessageDialog (null, 0,
					Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, _("The name \"%s\" is invalid."), name);
				error_dialog.format_secondary_markup (_("The invalid characters are: %s").printf ("<tt>*|/\\:\"&lt;&gt;?</tt>"));
				error_dialog.icon_name = "gtk-dialog-error";
				error_dialog.title = _("Error");
				error_dialog.run ();
				error_dialog.destroy ();
			}
			return res;
		}

		/**
		 * show_hide_notes:
		 *
		 * Show all the notes or hide them if they are all shown.
		 */
		public void show_hide_notes () {
			bool invisible_found = false;
			bool visible_found = false;
			bool active_found = false;
			foreach (var win in this.window_list) {
				if (win.is_active) {
					active_found = true;
				}
				if (!win.get_visible ()) {
					invisible_found = true;
				}
				else {
					visible_found = true;
				}
			}

			if (!active_found && visible_found) {
				present_windows ();
			}
			else if (invisible_found) {
				show_windows ();
			}
			else {
				hide_windows ();
			}
		}

		/**
		 * present_windows:
		 *
		 * Present visible notes windows.
		 */
		private void present_windows () {
			foreach (var win in this.focus_order) {
				if (win.get_visible ()) {
					win.present ();
				}
			}
		}

		/**
		 * show_windows:
		 *
		 * Show all notes windows.
		 */
		private void show_windows () {
			var focus = this.focus_order.last ().data;
			foreach (var win in this.focus_order) {
				if (win != focus) {
					win.focus_on_map = false;
					win.show ();
					win.focus_on_map = true;
				} else {
					win.show ();
					win.present ();
				}
			}
		}

		/**
		 * hide_windows:
		 *
		 * Hide all notes windows.
		 */
		private void hide_windows () {
			this.lock_focus_order = true;
			foreach (var win in this.focus_order.copy ()) {
				win.hide ();
			}
			this.lock_focus_order = false;
		}

		/**
		 * open_settings_dialog:
		 *
		 * Open the settings dialog.
		 */
		public void open_settings_dialog () {
			try {
				GLib.Process.spawn_command_line_async ("xfce4-notes-settings");
			}
			catch (GLib.Error e) {
				var error_dialog = new Gtk.MessageDialog (null, Gtk.DialogFlags.DESTROY_WITH_PARENT,
						Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, _("Unable to open the settings dialog"));
				error_dialog.format_secondary_text ("%s", e.message);
				error_dialog.icon_name = "gtk-dialog-error";
				error_dialog.title = _("Error");
				error_dialog.run ();
				error_dialog.destroy ();
			}
		}

		/**
		 * open_about_dialog:
		 *
		 * Open the about dialog.
		 */
		public void open_about_dialog () {
			string[] authors = {
					"© 2003 Jakob Henriksson",
					"© 2006 Mike Massonnet",
					"© 2023 Arthur Demchenkov",
					null
				};

			Gtk.show_about_dialog (null,
				"program-name", _("Notes"),
				"logo-icon-name", "org.xfce.notes.logo",
				"comments", _("Ideal for your quick notes"),
				"version", Config.PACKAGE_VERSION,
				"copyright", "Copyright © 2003-2024 The Xfce development team",
				"license", Xfce.get_license_text (Xfce.LicenseTextType.GPL),
				"website", "https://docs.xfce.org/panel-plugins/xfce4-notes-plugin",
				"website-label", "docs.xfce.org",
				"authors", authors,
				"translator-credits", _("translator-credits"),
				null);
		}

		/**
		 * context_menu:
		 *
		 * Provides a GtkMenu to be used for right click context menus
		 * like in trayicons. Its items are destroyed/refreshed every
		 * time the menu is shown.
		 */
		public Gtk.Menu context_menu () {
			var menu = new Gtk.Menu ();

			menu.show.connect (() => {
				// Clean up menu
				menu.@foreach ((w) => {
					w.destroy ();
				});

				// Add fresh items
				foreach (var win in this.window_list) {
					var mi = new Gtk.MenuItem.with_label (win.name);
					mi.set_data ("window", (void*)win);
					mi.activate.connect ((i) => {
						// Jump to win
						var w = i.get_data<Xnp.Window> ("window");
						w.present ();
					});
					menu.insert (mi as Gtk.Widget, -1);
				}

				// New group menu item
				var mi_sep = new Gtk.SeparatorMenuItem ();
				menu.insert (mi_sep as Gtk.Widget, -1);
				var mi_add = new Gtk.ImageMenuItem.with_mnemonic (_("_Add a new group"));
				mi_add.activate.connect (() => {
					var new_win = create_window ();
					if (new_win != null)
						new_win.show ();
				});
				var image = new Gtk.Image.from_icon_name ("gtk-add", Gtk.IconSize.MENU);
				mi_add.set_image (image);
				menu.insert (mi_add as Gtk.Widget, -1);

				// Show all items
				menu.show_all ();
			});

			return menu;
		}

	}

}

/*static int main (string[] args) {
	Gtk.init (ref args);
	var app = new Xnp.Application ("/tmp/notes-conf.rc");
	Gtk.main ();
	app.unref ();
	return 0;
}*/