Files
duplicati/Duplicati/Library/Backend/File/FileUI.cs
T

270 lines
11 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;
2009-09-16 19:41:49 +00:00
namespace Duplicati.Library.Backend
{
2009-09-17 17:44:59 +00:00
public partial class FileUI : UserControl
{
2009-09-16 19:41:49 +00:00
IDictionary<string, string> m_options;
2009-09-16 19:41:49 +00:00
private const string DESTINATION_FOLDER = "Destination";
private const string USERNAME = "Username";
private const string PASSWORD = "Password";
2009-09-17 17:44:59 +00:00
private const string CHECKED_EMPTY = "UI: Checked empty";
2011-01-15 15:40:33 +00:00
private const string INITIALPASSWORD = "UI: Temp password";
2009-09-17 17:44:59 +00:00
private bool m_hasCheckedEmpty = false;
private const string DUPLICATI_ACTION_MARKER = "*duplicati-action*";
private string m_uiAction = null;
2009-09-16 19:41:49 +00:00
public FileUI(IDictionary<string, string> options)
: this()
{
m_options = options;
}
2009-09-16 19:41:49 +00:00
private FileUI()
{
2009-09-16 19:41:49 +00:00
InitializeComponent();
}
2009-09-16 19:41:49 +00:00
internal bool Save(bool validate)
{
2011-01-15 15:40:33 +00:00
string initialPwd;
bool hasInitial = m_options.TryGetValue(INITIALPASSWORD, out initialPwd);
string targetpath;
if (UsePath.Checked)
targetpath = TargetFolder.Text;
else
targetpath = TargetDrive.Text + "\\" + Folder.Text;
2009-09-17 17:44:59 +00:00
if (m_hasCheckedEmpty && m_options.ContainsKey(DESTINATION_FOLDER) && targetpath != m_options[DESTINATION_FOLDER])
m_hasCheckedEmpty = false;
2011-01-15 15:40:33 +00:00
2009-09-17 17:44:59 +00:00
m_options.Clear();
m_options[CHECKED_EMPTY] = m_hasCheckedEmpty.ToString();
2009-09-16 19:41:49 +00:00
m_options[DESTINATION_FOLDER] = targetpath;
2011-01-15 15:40:33 +00:00
if (hasInitial)
m_options[INITIALPASSWORD] = initialPwd;
2009-09-16 19:41:49 +00:00
if (UseCredentials.Checked)
{
m_options[USERNAME] = Username.Text;
m_options[PASSWORD] = Password.Text;
}
else
{
m_options[USERNAME] = "";
m_options[PASSWORD] = "";
}
if (!string.IsNullOrEmpty(m_uiAction))
m_options.Add(DUPLICATI_ACTION_MARKER, m_uiAction);
2009-09-16 19:41:49 +00:00
if (!validate)
2011-01-15 15:40:33 +00:00
return false;
2009-09-16 19:41:49 +00:00
2009-01-15 19:42:14 +00:00
if (UseCredentials.Checked)
2011-01-15 15:40:33 +00:00
{
2009-01-15 19:42:14 +00:00
if (!Duplicati.Library.Backend.File.PreAuthenticate(targetpath, Username.Text, Password.Text))
{
2009-09-16 19:41:49 +00:00
MessageBox.Show(this, Strings.FileUI.AuthenticationError, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
2009-01-15 19:42:14 +00:00
}
2011-01-15 15:40:33 +00:00
}
try
{
if (targetpath.Trim().Length == 0)
{
2009-09-16 19:41:49 +00:00
MessageBox.Show(this, Strings.FileUI.EmptyFoldernameError, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
if (!System.IO.Path.IsPathRooted(targetpath))
{
2009-09-16 19:41:49 +00:00
MessageBox.Show(this, Strings.FileUI.NonRootedPathError, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
if (!System.IO.Directory.Exists(targetpath))
{
2009-09-16 19:41:49 +00:00
switch (MessageBox.Show(this, Strings.FileUI.CreateFolderQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
{
case DialogResult.Yes:
System.IO.Directory.CreateDirectory(targetpath);
break;
case DialogResult.Cancel:
2009-09-16 19:41:49 +00:00
return false;
}
}
}
catch (Exception ex)
{
2009-09-16 19:41:49 +00:00
MessageBox.Show(this, string.Format(Strings.FileUI.FolderValidationError, ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
try
{
if (!string.IsNullOrEmpty(m_uiAction))
2010-01-15 22:25:26 +00:00
{
if (string.Equals(m_uiAction, "add", StringComparison.InvariantCultureIgnoreCase))
{
string[] files = System.IO.Directory.GetFileSystemEntries(targetpath);
string[] duplicati_files = System.IO.Directory.GetFiles(targetpath, "duplicati-*");
if (duplicati_files.Length > 0)
if (MessageBox.Show(this, string.Format(Interface.CommonStrings.ExistingBackupDetectedQuestion), Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3) != DialogResult.Yes)
return false;
if (!m_hasCheckedEmpty && files.Length > 0)
if (MessageBox.Show(this, Strings.FileUI.FolderNotEmptyWarning, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) != DialogResult.Yes)
return false;
}
else if (string.Equals(m_uiAction, "restore", StringComparison.InvariantCultureIgnoreCase))
{
if (System.IO.Directory.GetFileSystemEntries(targetpath).Length == 0)
if (MessageBox.Show(this, Strings.FileUI.FolderEmptyError, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) != DialogResult.Yes)
return false;
}
m_hasCheckedEmpty = true;
m_options[CHECKED_EMPTY] = "true";
2010-01-15 22:25:26 +00:00
}
}
catch (Exception ex)
{
2009-09-16 19:41:49 +00:00
MessageBox.Show(this, string.Format(Strings.FileUI.FolderValidationError, ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
2011-01-15 15:40:33 +00:00
m_options.Remove(INITIALPASSWORD);
2009-09-16 19:41:49 +00:00
return true;
}
private void RescanDrives()
{
TargetDrive.Items.Clear();
if (!Library.Utility.Utility.IsClientLinux)
for (char i = 'A'; i < 'Z'; i++)
{
try
{
System.IO.DriveInfo di = new System.IO.DriveInfo(i.ToString());
if (di.DriveType == System.IO.DriveType.Removable)
TargetDrive.Items.Add(i.ToString() + ":");
}
catch { }
}
}
2009-09-16 19:41:49 +00:00
void FileUI_Load(object sender, EventArgs args)
{
RescanDrives();
2009-09-16 19:41:49 +00:00
UsePath.Checked = true;
if (m_options.ContainsKey(DESTINATION_FOLDER))
TargetFolder.Text = m_options[DESTINATION_FOLDER];
2009-09-16 19:41:49 +00:00
UseCredentials.Checked = m_options.ContainsKey(USERNAME) && !string.IsNullOrEmpty(m_options[USERNAME]);
if (m_options.ContainsKey(USERNAME))
Username.Text = m_options[USERNAME];
if (m_options.ContainsKey(PASSWORD))
Password.Text = m_options[PASSWORD];
2009-09-17 17:44:59 +00:00
2011-01-15 15:40:33 +00:00
if (!m_options.ContainsKey(INITIALPASSWORD))
m_options[INITIALPASSWORD] = m_options.ContainsKey(PASSWORD) ? m_options[PASSWORD] : "";
Password.AskToEnterNewPassword = !string.IsNullOrEmpty(m_options[INITIALPASSWORD]);
Password.InitialPassword = m_options[INITIALPASSWORD];
2010-09-07 21:39:22 +00:00
2009-09-17 17:44:59 +00:00
if (!m_options.ContainsKey(CHECKED_EMPTY) || !bool.TryParse(m_options[CHECKED_EMPTY], out m_hasCheckedEmpty))
m_hasCheckedEmpty = false;
m_options.TryGetValue(DUPLICATI_ACTION_MARKER, out m_uiAction);
}
private void UseCredentials_CheckedChanged(object sender, EventArgs e)
{
Credentials.Enabled = UseCredentials.Checked;
}
private void UsePath_CheckedChanged(object sender, EventArgs e)
{
TargetFolder.Enabled = BrowseTargetFolder.Enabled = UsePath.Checked;
}
private void BrowseTargetFolder_Click(object sender, EventArgs e)
{
if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
TargetFolder.Text = folderBrowserDialog.SelectedPath;
}
private void UseDisk_CheckedChanged(object sender, EventArgs e)
{
TargetDrive.Enabled = Folder.Enabled = FolderLabel.Enabled = UseDisk.Checked;
RescanDrives();
if (TargetDrive.Enabled && TargetDrive.Items.Count == 0)
{
2009-09-16 19:41:49 +00:00
MessageBox.Show(this, Strings.FileUI.NoRemovableDrivesError, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
UsePath.Checked = true;
}
}
2009-09-16 19:41:49 +00:00
public static string GetConfiguration(IDictionary<string, string> guiOptions, IDictionary<string, string> commandlineOptions)
{
if (guiOptions.ContainsKey(USERNAME) && !string.IsNullOrEmpty(guiOptions[USERNAME]))
commandlineOptions["ftp-username"] = guiOptions[USERNAME];
if (guiOptions.ContainsKey(PASSWORD) && !string.IsNullOrEmpty(guiOptions[PASSWORD]))
commandlineOptions["ftp-password"] = guiOptions[PASSWORD];
2009-09-18 16:36:40 +00:00
if (!guiOptions.ContainsKey(DESTINATION_FOLDER))
2010-06-19 21:08:21 +00:00
throw new Exception(string.Format(Interface.CommonStrings.ConfigurationIsMissingItemError, DESTINATION_FOLDER));
2009-09-18 16:36:40 +00:00
2009-09-16 19:41:49 +00:00
return "file://" + guiOptions[DESTINATION_FOLDER];
}
public static string PageTitle
{
get { return Strings.FileUI.PageTitle; }
}
public static string PageDescription
{
get { return Strings.FileUI.PageDescription; }
}
}
}