16e80583aSopenharmony_ci/**
26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46e80583aSopenharmony_ci * you may not use this file except in compliance with the License.
56e80583aSopenharmony_ci * You may obtain a copy of the License at
66e80583aSopenharmony_ci *
76e80583aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86e80583aSopenharmony_ci *
96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126e80583aSopenharmony_ci * See the License for the specific language governing permissions and
136e80583aSopenharmony_ci * limitations under the License.
146e80583aSopenharmony_ci */
156e80583aSopenharmony_ci
166e80583aSopenharmony_ciimport FileUtils from '../utils/FileUtils';
176e80583aSopenharmony_ci
186e80583aSopenharmony_ci/**
196e80583aSopenharmony_ci * Layout configuration base class,
206e80583aSopenharmony_ci * which defines the interfaces that all configuration objects need to implement.
216e80583aSopenharmony_ci */
226e80583aSopenharmony_ciexport abstract class ILayoutConfig {
236e80583aSopenharmony_ci
246e80583aSopenharmony_ci  private static readonly COMMON_FEATURE_NAME = 'featureCommon';
256e80583aSopenharmony_ci
266e80583aSopenharmony_ci  protected constructor() {
276e80583aSopenharmony_ci  }
286e80583aSopenharmony_ci
296e80583aSopenharmony_ci  /**
306e80583aSopenharmony_ci   * Initialize the configuration,
316e80583aSopenharmony_ci   * each configuration must recreate this function.
326e80583aSopenharmony_ci   */
336e80583aSopenharmony_ci  abstract initConfig(): void;
346e80583aSopenharmony_ci
356e80583aSopenharmony_ci  /**
366e80583aSopenharmony_ci   * The current configuration level,
376e80583aSopenharmony_ci   * each configuration must reset this function.
386e80583aSopenharmony_ci   */
396e80583aSopenharmony_ci  abstract getConfigLevel(): string;
406e80583aSopenharmony_ci
416e80583aSopenharmony_ci  /**
426e80583aSopenharmony_ci   * The current configuration type,
436e80583aSopenharmony_ci   * each configuration must recreate this function.
446e80583aSopenharmony_ci   */
456e80583aSopenharmony_ci  abstract getConfigType(): number;
466e80583aSopenharmony_ci
476e80583aSopenharmony_ci  /**
486e80583aSopenharmony_ci   * The current configuration name,
496e80583aSopenharmony_ci   * each configuration must recreate this function.
506e80583aSopenharmony_ci   */
516e80583aSopenharmony_ci  abstract getConfigName(): string;
526e80583aSopenharmony_ci
536e80583aSopenharmony_ci  /**
546e80583aSopenharmony_ci   * The JSON string corresponding to the current configuration.
556e80583aSopenharmony_ci   */
566e80583aSopenharmony_ci  protected abstract getPersistConfigJson(): string;
576e80583aSopenharmony_ci
586e80583aSopenharmony_ci  /**
596e80583aSopenharmony_ci   * load configuration.
606e80583aSopenharmony_ci   */
616e80583aSopenharmony_ci  protected loadPersistConfig(): any {
626e80583aSopenharmony_ci    let defaultConfig = this.getPersistConfigJson();
636e80583aSopenharmony_ci    return JSON.parse(defaultConfig);
646e80583aSopenharmony_ci  }
656e80583aSopenharmony_ci
666e80583aSopenharmony_ci  /**
676e80583aSopenharmony_ci   * load configuration from file.
686e80583aSopenharmony_ci   */
696e80583aSopenharmony_ci  loadPersistConfigFromFile(): any {
706e80583aSopenharmony_ci    const configFromFile = FileUtils.readStringFromFile(this.getConfigFileAbsPath());
716e80583aSopenharmony_ci    return configFromFile;
726e80583aSopenharmony_ci  }
736e80583aSopenharmony_ci
746e80583aSopenharmony_ci  /**
756e80583aSopenharmony_ci   * Check if the current property already exists.
766e80583aSopenharmony_ci   */
776e80583aSopenharmony_ci  isConfigExist(): boolean {
786e80583aSopenharmony_ci    return FileUtils.isExist(this.getConfigFileAbsPath());
796e80583aSopenharmony_ci  }
806e80583aSopenharmony_ci
816e80583aSopenharmony_ci  /**
826e80583aSopenharmony_ci   * Force reload of configuration values.
836e80583aSopenharmony_ci   */
846e80583aSopenharmony_ci  forceReloadConfig(): void {
856e80583aSopenharmony_ci    this.initConfig();
866e80583aSopenharmony_ci  }
876e80583aSopenharmony_ci
886e80583aSopenharmony_ci  /**
896e80583aSopenharmony_ci   * Persistent configuration values.
906e80583aSopenharmony_ci   */
916e80583aSopenharmony_ci  persistConfig(): void {
926e80583aSopenharmony_ci    FileUtils.writeStringToFile(this.getPersistConfigJson(), this.getConfigFileAbsPath());
936e80583aSopenharmony_ci  }
946e80583aSopenharmony_ci
956e80583aSopenharmony_ci  /**
966e80583aSopenharmony_ci   * delete configuration value.
976e80583aSopenharmony_ci   */
986e80583aSopenharmony_ci  deleteConfig(): void {
996e80583aSopenharmony_ci    FileUtils.deleteConfigFile(this.getConfigFileAbsPath());
1006e80583aSopenharmony_ci  }
1016e80583aSopenharmony_ci
1026e80583aSopenharmony_ci  /**
1036e80583aSopenharmony_ci   * Get the current configuration name.
1046e80583aSopenharmony_ci   */
1056e80583aSopenharmony_ci  getFeatureName(): string {
1066e80583aSopenharmony_ci    return ILayoutConfig.COMMON_FEATURE_NAME;
1076e80583aSopenharmony_ci  }
1086e80583aSopenharmony_ci
1096e80583aSopenharmony_ci  /**
1106e80583aSopenharmony_ci   * Get the absolute path of the current configuration file.
1116e80583aSopenharmony_ci   */
1126e80583aSopenharmony_ci  getConfigFileAbsPath(): string {
1136e80583aSopenharmony_ci    let filesDir = globalThis.desktopContext.filesDir + '/'
1146e80583aSopenharmony_ci    return filesDir + this.getConfigName() + '.json';
1156e80583aSopenharmony_ci  }
1166e80583aSopenharmony_ci}