Files
duplicati/Duplicati/Library/Backend/CloudFiles/CloudFilesUI.cs
T

367 lines
14 KiB
C#
Raw Normal View History

#region Disclaimer / License
2011-05-09 19:52:28 +00:00
// Copyright (C) 2011, Kenneth Skovhede
// http://www.hexad.dk, opensource@hexad.dk
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace Duplicati.Library.Backend
{
public partial class CloudFilesUI : UserControl
{
private const string USERNAME = "Username";
private const string ACCESS_KEY = "Access Key";
private const string CONTAINER_NAME = "Container name";
2011-04-27 19:46:35 +00:00
private const string USE_UK_ACCOUNT = "UK Account";
2011-06-26 09:46:36 +00:00
private const string AUTH_URL = "Auth URL";
private const string HASTESTED = "UI: Has tested";
2011-01-15 15:40:33 +00:00
private const string INITIALPASSWORD = "UI: Temp password";
2011-04-27 19:46:35 +00:00
private const string SIGNUP_PAGE_US = "https://www.rackspacecloud.com/signup";
private const string SIGNUP_PAGE_UK = "http://www.rackspace.co.uk/cloud-hosting/cloud-files/";
private IDictionary<string, string> m_options;
private bool m_hasTested;
private const string DUPLICATI_ACTION_MARKER = "*duplicati-action*";
private string m_uiAction = null;
public CloudFilesUI(IDictionary<string, string> options)
: this()
{
m_options = options;
2011-06-26 09:46:36 +00:00
Servernames.Items.Clear();
foreach (KeyValuePair<string, string> p in CloudFiles.KNOWN_CLOUDFILES_PROVIDERS)
Servernames.Items.Add(new Utility.ComboBoxItemPair<string>(string.Format("{0} ({1})", p.Key, p.Value), p.Value));
}
private CloudFilesUI()
{
InitializeComponent();
}
internal bool Save(bool validate)
{
Save();
if (!validate)
return true;
if (!ValidateForm(true))
return false;
if (!m_hasTested)
2010-06-19 21:08:21 +00:00
switch (MessageBox.Show(this, Interface.CommonStrings.ConfirmTestConnectionQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
{
case DialogResult.Yes:
TestConnection_Click(null, null);
if (!m_hasTested)
return false;
break;
case DialogResult.No:
break;
default: //Cancel
return false;
}
Save();
2011-01-15 15:40:33 +00:00
m_options.Remove(INITIALPASSWORD);
return true;
}
private void Save()
{
2011-01-15 15:40:33 +00:00
string initialPwd;
bool hasInitial = m_options.TryGetValue(INITIALPASSWORD, out initialPwd);
m_options.Clear();
m_options[HASTESTED] = m_hasTested.ToString();
2010-09-07 21:39:22 +00:00
m_options[USERNAME] = Username.Text;
m_options[ACCESS_KEY] = API_KEY.Text;
2011-01-15 15:40:33 +00:00
m_options[CONTAINER_NAME] = ContainerName.Text;
2011-06-26 09:46:36 +00:00
if (Servernames.SelectedItem as Utility.ComboBoxItemPair<string> == null)
m_options[AUTH_URL] = Servernames.Text;
else
m_options[AUTH_URL] = (Servernames.SelectedItem as Utility.ComboBoxItemPair<string>).Value;
2011-01-15 15:40:33 +00:00
if (hasInitial)
m_options[INITIALPASSWORD] = initialPwd;
if (!string.IsNullOrEmpty(m_uiAction))
m_options.Add(DUPLICATI_ACTION_MARKER, m_uiAction);
}
void CloudFilesUI_Load(object sender, EventArgs args)
{
if (m_options.ContainsKey(USERNAME))
2010-09-07 21:39:22 +00:00
Username.Text = m_options[USERNAME];
if (m_options.ContainsKey(ACCESS_KEY))
2010-09-07 21:39:22 +00:00
API_KEY.Text = m_options[ACCESS_KEY];
if (m_options.ContainsKey(CONTAINER_NAME))
2011-01-15 15:40:33 +00:00
ContainerName.Text = m_options[CONTAINER_NAME];
2011-06-26 09:46:36 +00:00
if (m_options.ContainsKey(AUTH_URL))
{
Servernames.Text = m_options[AUTH_URL];
}
else if (m_options.ContainsKey(USE_UK_ACCOUNT))
2011-04-27 19:46:35 +00:00
{
bool useUK;
if (bool.TryParse(m_options[USE_UK_ACCOUNT], out useUK))
2011-06-26 09:46:36 +00:00
Servernames.Text = CloudFiles.AUTH_URL_UK;
2011-04-27 19:46:35 +00:00
else
2011-06-26 09:46:36 +00:00
Servernames.Text = CloudFiles.AUTH_URL_US;
2011-04-27 19:46:35 +00:00
}
2011-06-26 09:46:36 +00:00
else
{
Servernames.Text = CloudFiles.AUTH_URL_US;
}
2011-01-15 15:40:33 +00:00
if (!m_options.ContainsKey(INITIALPASSWORD))
m_options[INITIALPASSWORD] = m_options.ContainsKey(ACCESS_KEY) ? m_options[ACCESS_KEY] : "";
API_KEY.AskToEnterNewPassword = !string.IsNullOrEmpty(m_options[INITIALPASSWORD]);
API_KEY.InitialPassword = m_options[INITIALPASSWORD];
if (!m_options.ContainsKey(HASTESTED) || !bool.TryParse(m_options[HASTESTED], out m_hasTested))
m_hasTested = false;
m_options.TryGetValue(DUPLICATI_ACTION_MARKER, out m_uiAction);
}
private bool ValidateForm(bool checkForBucket)
{
2011-10-30 08:49:15 +00:00
string servername;
if (Servernames.SelectedItem as Utility.ComboBoxItemPair<string> == null)
servername = Servernames.Text;
else
servername = (Servernames.SelectedItem as Utility.ComboBoxItemPair<string>).Value;
if (string.IsNullOrEmpty(servername))
{
MessageBox.Show(this, Strings.CloudFilesUI.EmptyAuthUrlError, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
try { Servernames.Focus(); }
catch { }
return false;
}
Uri tmp;
if (!Uri.TryCreate(servername, UriKind.Absolute, out tmp))
{
MessageBox.Show(this, string.Format(Strings.CloudFilesUI.InvalidAuthUrlError, servername), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
try { Servernames.Focus(); }
catch { }
return false;
}
2010-09-07 21:39:22 +00:00
if (Username.Text.Trim().Length <= 0)
{
MessageBox.Show(this, Strings.CloudFilesUI.EmptyCloudFilesIDError, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
2010-09-07 21:39:22 +00:00
if (API_KEY.Text.Trim().Length <= 0)
{
MessageBox.Show(this, Strings.CloudFilesUI.EmptyAPIKeyError , Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
2011-01-15 15:40:33 +00:00
if (ContainerName.Text.Trim().Length <= 0)
{
MessageBox.Show(this, Strings.CloudFilesUI.EmptyContainerNameError, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
return true;
}
private void SignUpLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
2011-06-26 09:46:36 +00:00
Duplicati.Library.Utility.UrlUtillity.OpenUrl(Servernames.Text.Equals(CloudFiles.AUTH_URL_UK) ? SIGNUP_PAGE_UK : SIGNUP_PAGE_US);
}
private void TestConnection_Click(object sender, EventArgs e)
{
if (ValidateForm(true))
{
Cursor c = this.Cursor;
try
{
this.Cursor = Cursors.WaitCursor;
Save();
Dictionary<string, string> options = new Dictionary<string, string>();
string destination = GetConfiguration(m_options, options);
2011-07-04 17:25:11 +00:00
bool existingBackup = false;
2011-09-24 11:39:50 +00:00
using (CloudFiles cf = new CloudFiles(destination, options))
foreach (Interface.IFileEntry n in cf.List())
if (n.Name.StartsWith("duplicati-"))
{
existingBackup = true;
break;
}
2011-07-04 17:25:11 +00:00
bool isUiAdd = string.IsNullOrEmpty(m_uiAction) || string.Equals(m_uiAction, "add", StringComparison.InvariantCultureIgnoreCase);
if (existingBackup && isUiAdd)
2011-07-04 17:25:11 +00:00
{
if (MessageBox.Show(this, string.Format(Interface.CommonStrings.ExistingBackupDetectedQuestion), Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3) != DialogResult.Yes)
return;
}
else
{
MessageBox.Show(this, Interface.CommonStrings.ConnectionSuccess, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
m_hasTested = true;
}
2010-06-19 21:08:21 +00:00
catch (Interface.FolderMissingException)
{
switch (MessageBox.Show(this, Strings.CloudFilesUI.CreateMissingContainer, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
{
case DialogResult.Yes:
CreateContainer.PerformClick();
TestConnection.PerformClick();
return;
default:
return;
}
}
catch (Exception ex)
{
2010-06-19 21:08:21 +00:00
MessageBox.Show(this, string.Format(Interface.CommonStrings.ConnectionFailure, ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.Cursor = c;
}
}
}
2010-09-07 21:39:22 +00:00
private void Username_TextChanged(object sender, EventArgs e)
{
m_hasTested = false;
}
2010-09-07 21:39:22 +00:00
private void API_KEY_TextChanged(object sender, EventArgs e)
{
m_hasTested = false;
}
2011-01-15 15:40:33 +00:00
private void ContainerName_TextChanged(object sender, EventArgs e)
{
m_hasTested = false;
}
2011-04-27 19:46:35 +00:00
private void UKAccount_CheckedChanged(object sender, EventArgs e)
{
m_hasTested = false;
}
public static string GetConfiguration(IDictionary<string, string> guiOptions, IDictionary<string, string> commandlineOptions)
{
if (guiOptions.ContainsKey(USERNAME) && !string.IsNullOrEmpty(guiOptions[USERNAME]))
commandlineOptions["cloudfiles-username"] = guiOptions[USERNAME];
if (guiOptions.ContainsKey(ACCESS_KEY) && !string.IsNullOrEmpty(guiOptions[ACCESS_KEY]))
commandlineOptions["cloudfiles-accesskey"] = guiOptions[ACCESS_KEY];
2011-06-26 09:46:36 +00:00
if (guiOptions.ContainsKey(AUTH_URL))
{
commandlineOptions["cloudfiles-authentication-url"] = guiOptions[AUTH_URL];
}
else
{
bool useUK;
if (guiOptions.ContainsKey(USE_UK_ACCOUNT) && bool.TryParse(guiOptions[USE_UK_ACCOUNT], out useUK) && useUK)
commandlineOptions["cloudfiles-uk-account"] = "";
}
if (!guiOptions.ContainsKey(CONTAINER_NAME))
2010-06-19 21:08:21 +00:00
throw new Exception(string.Format(Interface.CommonStrings.ConfigurationIsMissingItemError, CONTAINER_NAME));
2011-06-26 09:46:36 +00:00
return "cloudfiles://" + guiOptions[CONTAINER_NAME];
}
public static string PageTitle
{
get { return Strings.CloudFilesUI.PageTitle; }
}
public static string PageDescription
{
get { return Strings.CloudFilesUI.PageDescription; }
}
2010-09-07 21:39:22 +00:00
private void CreateContainer_Click(object sender, EventArgs e)
{
if (ValidateForm(false))
{
Cursor c = this.Cursor;
try
{
this.Cursor = Cursors.WaitCursor;
Save();
Dictionary<string, string> options = new Dictionary<string, string>();
string destination = GetConfiguration(m_options, options);
CloudFiles cf = new CloudFiles(destination, options);
cf.CreateFolder();
2010-06-19 21:08:21 +00:00
MessageBox.Show(this, Interface.CommonStrings.FolderCreated, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
m_hasTested = true;
}
catch (Exception ex)
{
2010-06-19 21:08:21 +00:00
MessageBox.Show(this, string.Format(Interface.CommonStrings.ConnectionFailure, ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Cursor = c;
}
}
2011-06-26 09:46:36 +00:00
private delegate void SetComboTextDelegate(ComboBox el, string text);
private void SetComboText(ComboBox el, string text)
{
el.Text = text;
}
private void Servernames_SelectedIndexChanged(object sender, EventArgs e)
{
if (Servernames.SelectedItem as Utility.ComboBoxItemPair<string> != null)
BeginInvoke(new SetComboTextDelegate(SetComboText), Servernames, (Servernames.SelectedItem as Utility.ComboBoxItemPair<string>).Value);
}
private void Servernames_TextChanged(object sender, EventArgs e)
{
m_hasTested = false;
}
}
}