Commits

HexRx committed c38a026

Add methods to save and restore authentication information

  • Participants
  • Parent commits faf6564

Comments (0)

Files changed (1)

KPDataSave/AccountState.cs

 /*
     KPDataSave - KeePass plugin which saves database in Dropbox service
-    Copyright (C) 2011 HexRx
+    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
 */
 
 using System;
+using KeePass.Plugins;
+using Newtonsoft.Json;
 
 namespace KPDataSave
 {
-    public struct AccountState
+    [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.Token != null)
+                return _account;
+            string value = host.CustomConfig.GetString(Key);
+            // Если в конфиге пусто - возвращаем null
+            if (value.Length == 0)
+                return null;
+            return (AccountState)JsonConvert.DeserializeObject(value);
+        }
     }
 }