2014-01-28 11:19:27 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Newtonsoft.Json;
|
2014-01-29 22:44:57 +08:00
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
2014-01-28 11:19:27 +08:00
|
|
|
|
|
2014-01-29 22:44:57 +08:00
|
|
|
|
namespace Wox.Infrastructure
|
2014-01-28 11:19:27 +08:00
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class CommonStorage
|
|
|
|
|
{
|
|
|
|
|
private static string configPath = Directory.GetCurrentDirectory() + "\\data.json";
|
|
|
|
|
private static object locker = new object();
|
|
|
|
|
private static CommonStorage storage;
|
|
|
|
|
|
|
|
|
|
public UserSetting UserSetting { get; set; }
|
|
|
|
|
public UserSelectedRecords UserSelectedRecords { get; set; }
|
|
|
|
|
|
|
|
|
|
private CommonStorage()
|
|
|
|
|
{
|
2014-01-29 22:44:57 +08:00
|
|
|
|
UserSetting = new UserSetting();
|
2014-01-28 11:19:27 +08:00
|
|
|
|
UserSelectedRecords = new UserSelectedRecords();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
//json is a good choise, readable and flexiable
|
|
|
|
|
string json = JsonConvert.SerializeObject(storage, Formatting.Indented);
|
|
|
|
|
File.WriteAllText(configPath, json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Load()
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(configPath))
|
|
|
|
|
{
|
|
|
|
|
File.Create(configPath).Close();
|
|
|
|
|
}
|
|
|
|
|
string json = File.ReadAllText(configPath);
|
|
|
|
|
if (!string.IsNullOrEmpty(json))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
storage = JsonConvert.DeserializeObject<CommonStorage>(json);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2014-02-02 16:15:34 +08:00
|
|
|
|
LoadDefaultUserSetting();
|
2014-01-28 11:19:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-02 16:15:34 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LoadDefaultUserSetting();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void LoadDefaultUserSetting()
|
|
|
|
|
{
|
|
|
|
|
//default setting
|
|
|
|
|
Instance.UserSetting.Theme = "Default";
|
|
|
|
|
Instance.UserSetting.ReplaceWinR = true;
|
|
|
|
|
Instance.UserSetting.WebSearches = Instance.UserSetting.LoadDefaultWebSearches();
|
2014-02-22 11:55:48 +08:00
|
|
|
|
Instance.UserSetting.Hotkey = "Win + W";
|
2014-01-28 11:19:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CommonStorage Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (storage == null)
|
|
|
|
|
{
|
|
|
|
|
lock (locker)
|
|
|
|
|
{
|
|
|
|
|
if (storage == null)
|
|
|
|
|
{
|
|
|
|
|
storage = new CommonStorage();
|
|
|
|
|
Load();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return storage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|