settings.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. get windowConfigFile() {
  66. const profileSettingPath = `${this.confDir}/config`;
  67. const settingFile = "windows.json";
  68. const defaultSettingFile = this.defaultWindowConfigFile;
  69. return this.loadFile(profileSettingPath, settingFile, defaultSettingFile);
  70. }
  71. loadFile(path, file, defaultFile) {
  72. const customSetting = GLib.build_filenamev([path, file]);
  73. Logger.trace(`custom-setting-file: ${customSetting}`);
  74. const customSettingFile = Gio.File.new_for_path(customSetting);
  75. if (customSettingFile.query_exists(null)) {
  76. return customSettingFile;
  77. } else {
  78. const profileCustomSettingDir = Gio.File.new_for_path(path);
  79. if (!profileCustomSettingDir.query_exists(null)) {
  80. if (profileCustomSettingDir.make_directory_with_parents(null)) {
  81. const createdStream = customSettingFile.create(Gio.FileCreateFlags.NONE, null);
  82. const defaultContents = this.loadFileContents(defaultFile);
  83. Logger.trace(defaultContents);
  84. createdStream.write_all(defaultContents, null);
  85. }
  86. }
  87. }
  88. return null;
  89. }
  90. loadFileContents(configFile) {
  91. let [success, contents] = configFile.load_contents(null);
  92. if (success) {
  93. const stringContents = imports.byteArray.toString(contents);
  94. return stringContents;
  95. }
  96. }
  97. get windowProps() {
  98. let windowConfigFile = this.windowConfigFile;
  99. let windowProps = null;
  100. if (!windowConfigFile || !production) {
  101. windowConfigFile = this.defaultWindowConfigFile;
  102. }
  103. let [success, contents] = windowConfigFile.load_contents(null);
  104. if (success) {
  105. const windowConfigContents = imports.byteArray.toString(contents);
  106. Logger.trace(`${windowConfigContents}`);
  107. windowProps = JSON.parse(windowConfigContents);
  108. }
  109. return windowProps;
  110. }
  111. set windowProps(props) {
  112. let windowConfigFile = this.windowConfigFile;
  113. if (!windowConfigFile || !production) {
  114. windowConfigFile = this.defaultWindowConfigFile;
  115. }
  116. let windowConfigContents = JSON.stringify(props, null, 4);
  117. const PERMISSIONS_MODE = 0o744;
  118. if (GLib.mkdir_with_parents(windowConfigFile.get_parent().get_path(), PERMISSIONS_MODE) === 0) {
  119. let [_, _tag] = windowConfigFile.replace_contents(
  120. windowConfigContents,
  121. null,
  122. false,
  123. Gio.FileCreateFlags.REPLACE_DESTINATION,
  124. null
  125. );
  126. }
  127. }
  128. }