Source

KPDataSave / KPDataSave / AccountState.cs

HexRx 071dadc 

HexRx c38a026 
HexRx 071dadc 















HexRx c38a026 

HexRx 071dadc 


HexRx c38a026 

HexRx 071dadc 
HexRx c38a026 


HexRx 071dadc 

HexRx c38a026 













HexRx 4f8ad76 
HexRx c38a026 

HexRx 8ee28a2 






HexRx c38a026 
HexRx 8ee28a2 






HexRx c38a026 
HexRx 359f1b4 





HexRx 071dadc 
/*
    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
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    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 <http://www.gnu.org/licenses/>.
*/

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

namespace KPDataSave
{
    [Serializable]
    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);
            try
            {
                _account = JsonConvert.DeserializeObject<AccountState>(value);
            }
            // В конфиге пусто
            catch (ArgumentNullException)
            {
                return null;
            }
            // Неправильные данные для десериализации
            catch (JsonReaderException)
            {
                return null;
            }
            return _account;
        }

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