KPDataSave / KPDataSave / AccountState.cs

Full commit
    KPDataSave - KeePass plugin which saves database in Dropbox service
    Copyright (C) 2011-2012 HexRx

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <>.

using System;
using KeePass.Plugins;
using Newtonsoft.Json;

namespace KPDataSave
    public class AccountState
        // Ключ под которым сохраняем в конфигах
        private const string Key = "KPDataSave";

        public string Token;
        public string Secret;
        public string Path;

        private static AccountState _account;

        public void Serialize(IPluginHost host)
            // Т.к KeePass сохраняет настройки только при закрытии приложения, необходимо хранить копию в памяти
            _account = this;
            string json = JsonConvert.SerializeObject(this);
            host.CustomConfig.SetString(Key, json);

        public static AccountState Deserialize(IPluginHost host)
            if (_account != null)
                return _account;
            string value = host.CustomConfig.GetString(Key);
            // Если в конфиге пусто - возвращаем null
            if (value.Length == 0)
                return null;
            return JsonConvert.DeserializeObject<AccountState>(value);

        public static void Remove(IPluginHost host)
            _account = null;
            host.CustomConfig.SetString(Key, "");