user.vala 3.0 KB

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