settings.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * This file is part of the Forge Window Manager extension for Gnome 3
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. // Gnome imports
  19. import Gio from "gi://Gio";
  20. import GLib from "gi://GLib";
  21. import GObject from "gi://GObject";
  22. import { Logger } from "./logger.js";
  23. // Dev or Prod mode, see Makefile:debug
  24. export const production = true;
  25. export class ConfigManager extends GObject.Object {
  26. static {
  27. GObject.registerClass(this);
  28. }
  29. #confDir = GLib.get_user_config_dir();
  30. constructor({ dir }) {
  31. super();
  32. this.extensionPath = dir.get_path();
  33. }
  34. get confDir() {
  35. return `${this.#confDir}/forge`;
  36. }
  37. get defaultStylesheetFile() {
  38. const defaultStylesheet = GLib.build_filenamev([this.extensionPath, `stylesheet.css`]);
  39. Logger.trace(`default-stylesheet: ${defaultStylesheet}`);
  40. const defaultStylesheetFile = Gio.File.new_for_path(defaultStylesheet);
  41. if (defaultStylesheetFile.query_exists(null)) {
  42. return defaultStylesheetFile;
  43. }
  44. return null;
  45. }
  46. get stylesheetFile() {
  47. const profileSettingPath = `${this.confDir}/stylesheet/forge`;
  48. const settingFile = "stylesheet.css";
  49. const defaultSettingFile = this.defaultStylesheetFile;
  50. return this.loadFile(profileSettingPath, settingFile, defaultSettingFile);
  51. }
  52. get defaultWindowConfigFile() {
  53. const defaultWindowConfig = GLib.build_filenamev([
  54. this.extensionPath,
  55. `config`,
  56. `windows.json`,
  57. ]);
  58. Logger.trace(`default-window-config: ${defaultWindowConfig}`);
  59. const defaultWindowConfigFile = Gio.File.new_for_path(defaultWindowConfig);
  60. if (defaultWindowConfigFile.query_exists(null)) {
  61. return defaultWindowConfigFile;
  62. }
  63. return null;
  64. }
  65. loadDefaultWindowConfigContents() {
  66. const defaultSettingFile = this.defaultWindowConfigFile;
  67. if (defaultSettingFile) {
  68. const contents = this.loadFileContents(defaultSettingFile);
  69. if (contents) {
  70. return JSON.parse(contents);
  71. }
  72. }
  73. return null;
  74. }
  75. get windowConfigFile() {
  76. const profileSettingPath = `${this.confDir}/config`;
  77. const settingFile = "windows.json";
  78. const defaultSettingFile = this.defaultWindowConfigFile;
  79. return this.loadFile(profileSettingPath, settingFile, defaultSettingFile);
  80. }
  81. loadFile(path, file, defaultFile) {
  82. const customSetting = GLib.build_filenamev([path, file]);
  83. Logger.trace(`custom-setting-file: ${customSetting}`);
  84. const customSettingFile = Gio.File.new_for_path(customSetting);
  85. if (customSettingFile.query_exists(null)) {
  86. return customSettingFile;
  87. } else {
  88. const profileCustomSettingDir = Gio.File.new_for_path(path);
  89. if (!profileCustomSettingDir.query_exists(null)) {
  90. if (profileCustomSettingDir.make_directory_with_parents(null)) {
  91. const createdStream = customSettingFile.create(Gio.FileCreateFlags.NONE, null);
  92. const defaultContents = this.loadFileContents(defaultFile);
  93. Logger.trace(defaultContents);
  94. createdStream.write_all(defaultContents, null);
  95. }
  96. }
  97. }
  98. return null;
  99. }
  100. loadFileContents(configFile) {
  101. let [success, contents] = configFile.load_contents(null);
  102. if (success) {
  103. const stringContents = imports.byteArray.toString(contents);
  104. return stringContents;
  105. }
  106. }
  107. get windowProps() {
  108. let windowConfigFile = this.windowConfigFile;
  109. let windowProps = null;
  110. // if (!windowConfigFile || !production) {
  111. if (!windowConfigFile) {
  112. windowConfigFile = this.defaultWindowConfigFile;
  113. }
  114. let [success, contents] = windowConfigFile.load_contents(null);
  115. if (success) {
  116. const windowConfigContents = imports.byteArray.toString(contents);
  117. Logger.trace(`${windowConfigContents}`);
  118. windowProps = JSON.parse(windowConfigContents);
  119. }
  120. return windowProps;
  121. }
  122. set windowProps(props) {
  123. let windowConfigFile = this.windowConfigFile;
  124. // if (!windowConfigFile || !production) {
  125. if (!windowConfigFile) {
  126. windowConfigFile = this.defaultWindowConfigFile;
  127. }
  128. let windowConfigContents = JSON.stringify(props, null, 4);
  129. const PERMISSIONS_MODE = 0o744;
  130. if (GLib.mkdir_with_parents(windowConfigFile.get_parent().get_path(), PERMISSIONS_MODE) === 0) {
  131. let [_, _tag] = windowConfigFile.replace_contents(
  132. windowConfigContents,
  133. null,
  134. false,
  135. Gio.FileCreateFlags.REPLACE_DESTINATION,
  136. null
  137. );
  138. }
  139. }
  140. }