Source

KPDataSave / DropboxSave / Config.cs

Full commit
/*
    DropboxSave - KeePass plugin which save database in Dropbox service
    Copyright (C) 2011 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 System.Collections.Generic;
using System.Linq;
using System.Text;

using KeePass.Plugins;

namespace DropboxSave
{
    public static class Config
    {
        private static string key = "DropboxSave";

        // Сохраняем значения токенов и email
        public static void SaveAccountState(IPluginHost host, string email, string token, string secret)
        {
            string param = String.Format("{0};{1}", email, ToSString(token, secret));
            host.CustomConfig.SetString(key, param);
        }

        // Получение данных аккаунта
        public static AccountState GetAccountState(IPluginHost host)
        {
            AccountState acc;
            string state = host.CustomConfig.GetString(key);
            // Если есть запись, но она пустая
            if (state.Length == 0)
                throw new NullReferenceException();
            // Получем части строки
            string[] parts = state.Split(';');
            // Токены
            string[] tokens = ToDString(parts[1]);
            acc.Email = parts[0];
            acc.Token = tokens[0];
            acc.Secret = tokens[1];
            return acc;
        }

        // Удаление инфомации об аккаунте
        public static void RemoveAccountState(IPluginHost host)
        {
            host.CustomConfig.SetString(key, "");
        }

        // Проверка существования записи в конфигах
        public static bool IsAccountState(IPluginHost host)
        {
            string state = host.CustomConfig.GetString(key);
            if (state == null)
                return false;
            // Проверка на валидность
            if (state.IndexOf(';') == -1)
                return false;
            return true;
        }

        // Две строки в одну
        private static string ToSString(string str1, string str2)
        {
            // Две строки должны быть одинаковой длинны
            if (str1.Length != str2.Length)
                throw new PlgxException("String length not equal");
            char[] arr_out = new char[str1.Length * 2];
            int[] stri = new int[2];
            for (int i = 0; i < str1.Length * 2; i++)
            {
                if (i % 2 == 0)
                {
                    arr_out[i] = str1[stri[0]];
                    stri[0]++;
                }
                else
                {
                    arr_out[i] = str2[stri[1]];
                    stri[1]++;
                }
            }
            return new String(arr_out);
        }

        // Одна строка => две
        private static string[] ToDString(string str)
        {
            char[] arr1 = new char[str.Length / 2];
            char[] arr2 = new char[str.Length / 2];
            int[] stri = new int[2];
            for (int i = 0; i < str.Length; i++)
            {
                if (i % 2 == 0)
                {
                    arr1[stri[0]] = str[i];
                    stri[0]++;
                }
                else
                {
                    arr2[stri[1]] = str[i];
                    stri[1]++;
                }
            }
            // Возвращаем две строки
            return new String[2] { new String(arr1), new String(arr2) };
        }
    }
}