user.vala 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2012-2024 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. Hashtable sjson = SJSON.load_from_path(path);
  45. decode(sjson);
  46. }
  47. public void save(string path)
  48. {
  49. SJSON.save(encode(), path);
  50. }
  51. public void add_or_touch_recent_project(string source_dir, string name)
  52. {
  53. Gee.ArrayList<Value?> recent_projects = null;
  54. Hashtable? project = null;
  55. _data.foreach ((ee) => {
  56. if (ee.key == "recent_projects") {
  57. recent_projects = ee.value as ArrayList<Value?>;
  58. for (int ii = 0; ii < recent_projects.size; ++ii) {
  59. Hashtable rp = recent_projects[ii] as Hashtable;
  60. if ((string)rp["source_dir"] == source_dir) {
  61. project = rp;
  62. return false; // break
  63. }
  64. }
  65. }
  66. return true;
  67. });
  68. if (recent_projects == null) {
  69. recent_projects = new ArrayList<Value?>();
  70. _data["recent_projects"] = recent_projects;
  71. }
  72. string mtime = new GLib.DateTime.now_utc().to_unix().to_string();
  73. if (project == null) {
  74. project = new Hashtable();
  75. project["name"] = source_dir; // FIXME: store project name somewhere inside project directory
  76. project["source_dir"] = source_dir;
  77. project["mtime"] = mtime;
  78. recent_projects.add(project);
  79. recent_project_added(source_dir, source_dir, mtime);
  80. } else {
  81. project["mtime"] = mtime;
  82. recent_project_touched(source_dir, mtime);
  83. }
  84. }
  85. public void remove_recent_project(string source_dir)
  86. {
  87. _data.foreach((ee) => {
  88. if (ee.key == "recent_projects") {
  89. var recent_projects = ee.value as ArrayList<Value?>;
  90. var it = recent_projects.iterator();
  91. for (var has_next = it.next(); has_next; has_next = it.next()) {
  92. Hashtable rp = it.get() as Hashtable;
  93. if ((string)rp["source_dir"] == source_dir) {
  94. it.remove();
  95. recent_project_removed(source_dir);
  96. return false; // break
  97. }
  98. }
  99. }
  100. return true;
  101. });
  102. }
  103. }
  104. } /* namespace Crown */