Source

KPDataSave / KPDataSave / Forms / States / Accept.cs

/*
    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 System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace KPDataSave.Forms.States
{
    public class Accept : AbstractForm
    {
        private readonly StateControl _stateControl;

        public Accept(StateControl stateControl)
        {
            _stateControl = stateControl;
            // Устанавливаем заголовок окна
            Text = "Accept. Step 2/3";
            // Кнопка подтверждения
            Button btnAccept = new Button();
            btnAccept.Dock = DockStyle.Fill;
            btnAccept.TabIndex = 0;
            btnAccept.Text = "Accept";
            btnAccept.UseVisualStyleBackColor = true;
            btnAccept.Click += new System.EventHandler(BtnAcceptClick);
            btnAccept.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            tableLayout.Controls.Add(btnAccept, 1, 1);

            Button btnCancel = new Button();
            btnCancel.Dock = DockStyle.Fill;
            btnCancel.TabIndex = 1;
            btnCancel.Text = "Cancel";
            btnCancel.UseVisualStyleBackColor = true;
            btnCancel.Click += new System.EventHandler(BtnCancelClick);
            btnCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            tableLayout.Controls.Add(btnCancel, 1, 2);
        }

        // Кнопка подтверждения
        private void BtnAcceptClick(object sender, EventArgs e)
        {
            try
            {
                // Получение токенов
                _stateControl.DropboxClient.Login_RequestAccessToken();
            }
            // Если пользователь не подтвердил или произошла неизвестная ошибка
            catch (DropboxAPI.Exceptions.DropboxClientException exc)
            {
                switch (exc.HttpStatusCode)
                {
                    // Если нет инета
                    case 0:
                        MessageBox.Show("Error executing query.\nCheck your Internet connection!", "", MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                        return;
                    // Если приложение не подтверждено на сайте dropbox
                    default:
                        // Повторение попытки
                        if (MessageBox.Show("You are not have connected this application in the web browser!\nTrying again?", "Accept", 
                            MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                        {
                            BtnAcceptClick(sender, e);
                            return;
                        }
                        // Отмена операции
                        else
                        {
                            // Переход в состояние входа
                            _stateControl.SetState(_stateControl.SignInState);
                            return;
                        }
                        break;
                }
            }
            
            // Переход в состояние выбора директории для сохранения
            _stateControl.SetState(_stateControl.SelectFolderState);
        }

        // Кнопка отмены
        private void BtnCancelClick(object sender, EventArgs e)
        {
            // Переход в состояние входа
            _stateControl.SetState(_stateControl.SignInState);
        }
    }
}