* Added file chooser to KP2A

* added functionality to file storage interface and (some) implementations to delete files/folders, create folders, list contents
-> Dropbox functionality implemented
This commit is contained in:
Philipp Crocoll
2013-09-28 07:46:44 +02:00
parent 15b7ca38c8
commit 74acd19092
40 changed files with 5346 additions and 1694 deletions

View File

@@ -30,11 +30,11 @@ namespace keepass2android.Io
}
}
public void DeleteFile(IOConnectionInfo ioc)
public void Delete(IOConnectionInfo ioc)
{
//todo check if directory
IOConnection.DeleteFile(ioc);
}
public bool CheckForFileChangeFast(IOConnectionInfo ioc, string previousFileVersion)
{
if (!ioc.IsLocalFile())
@@ -137,5 +137,17 @@ namespace keepass2android.Io
{
return (!ioc.IsLocalFile()) && (ioc.CredSaveMode != IOCredSaveMode.SaveCred);
}
public void CreateDirectory(IOConnectionInfo ioc)
{
//TODO
throw new NotImplementedException();
}
public IEnumerable<FileDescription> ListContents(IOConnectionInfo ioc)
{
//TODO
throw new NotImplementedException();
}
}
}

View File

@@ -88,7 +88,7 @@ namespace keepass2android.Io
File.Delete(BaseVersionFilePath(ioc));
}
_cachedStorage.DeleteFile(ioc);
_cachedStorage.Delete(ioc);
}
private string CachedFilePath(IOConnectionInfo ioc)
@@ -105,6 +105,11 @@ namespace keepass2android.Io
&& File.Exists(BaseVersionFilePath(ioc));
}
public void Delete(IOConnectionInfo ioc)
{
_cachedStorage.Delete(ioc);
}
public bool CheckForFileChangeFast(IOConnectionInfo ioc, string previousFileVersion)
{
//see comment in GetCurrentFileVersionFast
@@ -416,6 +421,16 @@ namespace keepass2android.Io
return _cachedStorage.RequiresCredentials(ioc);
}
public void CreateDirectory(IOConnectionInfo ioc)
{
_cachedStorage.CreateDirectory(ioc);
}
public IEnumerable<FileDescription> ListContents(IOConnectionInfo ioc)
{
return _cachedStorage.ListContents(ioc);
}
public string GetBaseVersionHash(IOConnectionInfo ioc)
{

View File

@@ -22,6 +22,9 @@ namespace keepass2android.Io
{
}
public override IEnumerable<string> SupportedProtocols { get { yield return "dropbox"; } }
protected override string Protocol
{
get { return "dropbox"; }
}
}
}

View File

@@ -0,0 +1,14 @@
using System;
namespace keepass2android.Io
{
public class FileDescription
{
public string Path { get; set; }
public bool IsDirectory { get; set; }
public DateTime LastModified { get; set; }
public bool CanRead { get; set; }
public bool CanWrite { get; set; }
public long SizeInBytes { get; set; }
}
}

View File

@@ -17,7 +17,7 @@ namespace keepass2android.Io
public class GDriveFileStorage: IFileStorage
{
public IEnumerable<string> SupportedProtocols { get { yield return "gdrive"; } }
public void DeleteFile(IOConnectionInfo ioc)
public void Delete(IOConnectionInfo ioc)
{
throw new NotImplementedException();
}
@@ -63,5 +63,15 @@ namespace keepass2android.Io
{
throw new NotImplementedException();
}
public void CreateDirectory(IOConnectionInfo ioc)
{
throw new NotImplementedException();
}
public IEnumerable<FileDescription> ListContents(IOConnectionInfo convertPathToIoc)
{
throw new NotImplementedException();
}
}
}

View File

@@ -36,9 +36,9 @@ namespace keepass2android.Io
IEnumerable<string> SupportedProtocols { get; }
/// <summary>
/// Deletes the given file.
/// Deletes the given file or directory.
/// </summary>
void DeleteFile(IOConnectionInfo ioc);
void Delete(IOConnectionInfo ioc);
/// <summary>
/// Tests whether the file was changed.
@@ -54,7 +54,7 @@ namespace keepass2android.Io
/// </summary>
/// This string may have a deliberate value (except null) and should not be used by callers except for passing it to
/// CheckForFileChangeFast().
/// <returns>A string which should not be null.</returns>
/// <returns>A string describing the version. Null means, there is no way to get a file version (or it's not implemented).</returns>
string GetCurrentFileVersionFast(IOConnectionInfo ioc);
/// <summary>
@@ -96,6 +96,16 @@ namespace keepass2android.Io
/// Returns true if the the given ioc must be filled with username/password
/// </summary>
bool RequiresCredentials(IOConnectionInfo ioc);
/// <summary>
/// Creates the directory described by ioc
/// </summary>
void CreateDirectory(IOConnectionInfo ioc);
/// <summary>
/// Lists the contents of the given path
/// </summary>
IEnumerable<FileDescription> ListContents(IOConnectionInfo ioc);
}
/// <summary>

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Android.App;
using KeePassLib.Serialization;
using KeePassLib.Utility;
@@ -12,7 +13,8 @@ namespace keepass2android.Io
{
public abstract class JavaFileStorage: IFileStorage
{
public abstract IEnumerable<string> SupportedProtocols { get; }
public IEnumerable<string> SupportedProtocols { get { yield return Protocol; } }
private readonly IJavaFileStorage _jfs;
private readonly IKp2aApp _app;
@@ -23,9 +25,20 @@ namespace keepass2android.Io
_app = app;
}
public void DeleteFile(IOConnectionInfo ioc)
public void Delete(IOConnectionInfo ioc)
{
throw new NotImplementedException();
try
{
Jfs.Delete(IocToPath(ioc));
}
catch (FileNotFoundException e)
{
throw new System.IO.FileNotFoundException(e.Message, e);
}
catch (Java.Lang.Exception e)
{
throw LogAndConvertJavaException(e);
}
}
public bool CheckForFileChangeFast(IOConnectionInfo ioc, string previousFileVersion)
@@ -196,14 +209,68 @@ namespace keepass2android.Io
return false;
}
private static string IocToPath(IOConnectionInfo ioc)
public void CreateDirectory(IOConnectionInfo ioc)
{
int protocolLength = ioc.Path.IndexOf("://", StringComparison.Ordinal);
if (protocolLength < 0)
return ioc.Path;
else
return ioc.Path.Substring(protocolLength + 3);
try
{
Jfs.CreateFolder(IocToPath(ioc));
}
catch (FileNotFoundException e)
{
throw new System.IO.FileNotFoundException(e.Message, e);
}
catch (Java.Lang.Exception e)
{
throw LogAndConvertJavaException(e);
}
}
public IEnumerable<FileDescription> ListContents(IOConnectionInfo ioc)
{
try
{
IList<JavaFileStorageFileEntry> entries = Jfs.ListFiles(IocToPath(ioc));
return entries.Select(
e => new FileDescription
{
CanRead = e.CanRead,
CanWrite = e.CanWrite,
IsDirectory = e.IsDirectory,
LastModified = JavaTimeToCSharp(e.LastModifiedTime),
Path = Protocol + "://" + e.Path,
SizeInBytes = e.SizeInBytes
}
);
}
catch (FileNotFoundException e)
{
throw new System.IO.FileNotFoundException(e.Message, e);
}
catch (Java.Lang.Exception e)
{
throw LogAndConvertJavaException(e);
}
}
private DateTime JavaTimeToCSharp(long javatime)
{
//todo test
return new DateTime(1970, 1, 1).AddMilliseconds(javatime);
}
private string IocToPath(IOConnectionInfo ioc)
{
if (ioc.Path.StartsWith(Protocol + "://"))
return ioc.Path.Substring(Protocol.Length + 3);
else
{
return ioc.Path;
}
}
protected abstract string Protocol { get; }
}
}