Skip to content

Commit e9b0664

Browse files
author
bayashi
committed
Change app config file dir from mclocks to com.bayashi.mclocks
1 parent b9aa45d commit e9b0664

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ For Linux, you can get `.deb`, `.rpm` or `.AppImage`.
2424

2525
The `config.json` file allows you to configure the clocks according to your preferences.
2626

27+
There is no GUI(Graphical User Interface) for editing the `config.json` file in `com.bayashi.mclocks`. You will need to manually create and open and edit it using your text editor.
28+
2729
The `config.json` file should be located in the following directories:
2830

29-
* Windows: `C:\Users\{USER}\AppData\Roaming\mclocks\`
30-
* Mac: `/Users/{USER}/Library/Application Support/mclocks/`
31-
* Linux: `/home/{USER}/.config/mclocks/`
31+
* Windows: `C:\Users\{USER}\AppData\Roaming\com.bayashi.mclocks\`
32+
* Mac: `/Users/{USER}/Library/Application Support/com.bayashi.mclocks/`
33+
* Linux: `/home/{USER}/.config/com.bayashi.mclocks/`
34+
35+
### Backwards Compatibility Notes
36+
37+
The directory of the `config.json` file has been changed to `com.bayashi.mclocks` from just `mclocks` after version 0.2.9. Please create and move your old `mclocks/config.json` to new directory `com.bayashi.mclocks/config.json` if necessary.
3238

33-
There is no GUI(Graphical User Interface) for editing the `config.json` file in `mclocks`. You will need to open and edit it directly using your text editor.
39+
(Actually, the `mclocks` can load old config path so far though)
3440

3541
### Example of config.json
3642

src-tauri/src/lib.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use directories::BaseDirs;
22
use serde::{Deserialize, Serialize};
33
use std::fs;
44
use tauri::Manager;
5+
use tauri::Config;
56

67
const IS_DEV: bool = tauri::is_dev();
78

@@ -17,7 +18,7 @@ struct Clock {
1718
}
1819

1920
#[derive(Serialize, Deserialize, Debug)]
20-
struct Config {
21+
struct AppConfig {
2122
#[serde(default)]
2223
clocks: Vec<Clock>,
2324
#[serde(default)]
@@ -38,7 +39,7 @@ struct Config {
3839

3940
#[derive(Serialize, Deserialize, Debug)]
4041
#[serde(rename_all = "camelCase")]
41-
struct OldConfig {
42+
struct OldAppConfig {
4243
#[serde(default)]
4344
clocks: Vec<Clock>,
4445
#[serde(default)]
@@ -53,15 +54,34 @@ struct OldConfig {
5354
always_on_top: bool,
5455
}
5556

56-
const CONFIG_DIR: &str = if IS_DEV { "mclocks.dev" } else { "mclocks" };
57-
const CONFIG_FILE: &str = "config.json";
57+
fn get_config_file() -> String {
58+
let config_file = "config.json";
59+
if IS_DEV {
60+
format!("dev.{}", config_file)
61+
} else {
62+
config_file.to_string()
63+
}
64+
}
65+
66+
const OLD_CONFIG_DIR: String = if IS_DEV { "mclocks.dev".to_string() } else { "mclocks".to_string() };
67+
68+
fn get_old_config_app_path() -> String {
69+
vec![OLD_CONFIG_DIR, get_config_file()].join("/")
70+
}
5871

5972
fn get_config_app_path() -> String {
60-
vec![CONFIG_DIR, CONFIG_FILE].join("/")
73+
vec![get_app_identifier().as_str(), get_config_file().as_str()].join("/")
74+
}
75+
76+
fn get_app_identifier() -> String {
77+
let context: tauri::Context<tauri::Wry> = tauri::generate_context!();
78+
let config: &Config = context.config();
79+
80+
config.identifier.clone()
6181
}
6282

63-
fn merge_configs(old: OldConfig, new: Config) -> Config {
64-
Config {
83+
fn merge_configs(old: OldAppConfig, new: AppConfig) -> AppConfig {
84+
AppConfig {
6585
clocks: if new.clocks.len() > 0 {
6686
new.clocks
6787
} else if old.clocks.len() > 0 {
@@ -117,17 +137,21 @@ fn merge_configs(old: OldConfig, new: Config) -> Config {
117137
}
118138

119139
#[tauri::command]
120-
fn load_config() -> Result<Config, String> {
140+
fn load_config() -> Result<AppConfig, String> {
121141
let base_dir = BaseDirs::new().ok_or("Failed to get base dir")?;
122-
let config_path = base_dir.config_dir().join(get_config_app_path());
142+
let mut config_path = base_dir.config_dir().join(get_config_app_path());
123143

124144
if !config_path.exists() {
125-
return Err(format!("Config file `{}` not found", config_path.display()));
145+
let old_config_path = base_dir.config_dir().join(get_old_config_app_path());
146+
if !old_config_path.exists() {
147+
return Err(format!("Config file `{}` not found", config_path.display()));
148+
}
149+
config_path = old_config_path;
126150
}
127151

128152
let json = fs::read_to_string(config_path).map_err(|e| e.to_string())?;
129-
let old_config: OldConfig = serde_json::from_str(&json).map_err(|e| e.to_string())?;
130-
let new_config: Config = serde_json::from_str(&json).map_err(|e| e.to_string())?;
153+
let old_config: OldAppConfig = serde_json::from_str(&json).map_err(|e| e.to_string())?;
154+
let new_config: AppConfig = serde_json::from_str(&json).map_err(|e| e.to_string())?;
131155

132156
Ok(merge_configs(old_config, new_config))
133157
}

0 commit comments

Comments
 (0)