user.vala 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. using Gee;
  6. namespace Crown
  7. {
  8. public class User
  9. {
  10. // Data
  11. public Hashtable _data;
  12. // Signals
  13. public signal void recent_project_added(string source_dir, string name, string time);
  14. public signal void recent_project_touched(string source_dir, string time);
  15. public signal void recent_project_removed(string source_dir);
  16. public User()
  17. {
  18. }
  19. public void decode(Hashtable sjson)
  20. {
  21. _data = sjson;
  22. _data.foreach((ee) => {
  23. if (ee.key == "recent_projects") {
  24. var recent_projects = ee.value as ArrayList<Value?>;
  25. for (int ii = 0; ii < recent_projects.size; ++ii) {
  26. Hashtable rp = recent_projects[ii] as Hashtable;
  27. recent_project_added((string)rp["source_dir"]
  28. , (string)rp["name"]
  29. , (string)rp["mtime"]
  30. );
  31. }
  32. } else {
  33. logw("Unknown key: `%s`".printf(ee.key));
  34. }
  35. return true;
  36. });
  37. }
  38. public Hashtable encode()
  39. {
  40. return _data;
  41. }
  42. public void load(string path)
  43. {
  44. try {
  45. Hashtable sjson = SJSON.load_from_path(path);
  46. decode(sjson);
  47. } catch (JsonSyntaxError e) {
  48. loge(e.message);
  49. }
  50. }
  51. public void save(string path)
  52. {
  53. try {
  54. SJSON.save(encode(), path);
  55. } catch (JsonWriteError e) {
  56. loge(e.message);
  57. }
  58. }
  59. public void add_or_touch_recent_project(string source_dir, string name)
  60. {
  61. Gee.ArrayList<Value?> recent_projects = null;
  62. Hashtable? project = null;
  63. _data.foreach ((ee) => {
  64. if (ee.key == "recent_projects") {
  65. recent_projects = ee.value as ArrayList<Value?>;
  66. for (int ii = 0; ii < recent_projects.size; ++ii) {
  67. Hashtable rp = recent_projects[ii] as Hashtable;
  68. if ((string)rp["source_dir"] == source_dir) {
  69. project = rp;
  70. return false; // break
  71. }
  72. }
  73. }
  74. return true;
  75. });
  76. if (recent_projects == null) {
  77. recent_projects = new ArrayList<Value?>();
  78. _data["recent_projects"] = recent_projects;
  79. }
  80. string mtime = new GLib.DateTime.now_utc().to_unix().to_string();
  81. if (project == null) {
  82. project = new Hashtable();
  83. project["name"] = source_dir; // FIXME: store project name somewhere inside project directory
  84. project["source_dir"] = source_dir;
  85. project["mtime"] = mtime;
  86. recent_projects.add(project);
  87. recent_project_added(source_dir, source_dir, mtime);
  88. } else {
  89. project["mtime"] = mtime;
  90. recent_project_touched(source_dir, mtime);
  91. }
  92. }
  93. public void remove_recent_project(string source_dir)
  94. {
  95. _data.foreach((ee) => {
  96. if (ee.key == "recent_projects") {
  97. var recent_projects = ee.value as ArrayList<Value?>;
  98. var it = recent_projects.iterator();
  99. for (var has_next = it.next(); has_next; has_next = it.next()) {
  100. Hashtable rp = it.get() as Hashtable;
  101. if ((string)rp["source_dir"] == source_dir) {
  102. it.remove();
  103. recent_project_removed(source_dir);
  104. return false; // break
  105. }
  106. }
  107. }
  108. return true;
  109. });
  110. }
  111. }
  112. } /* namespace Crown */