Skip to content

Commit

Permalink
Merge pull request #2 from weberonede/v1.0.0
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
marvinweber authored Mar 20, 2019
2 parents 3cae722 + 35150cc commit eb4cbbf
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 87 deletions.
64 changes: 59 additions & 5 deletions KPSimpleBackup/KPSimpleBackup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Linq;
using System.IO;
using System.Windows.Forms;
using KeePass.Forms;
using KeePass.Plugins;
using KeePassLib;
using KeePassLib.Serialization;
using KeePassLib.Utility;
using Microsoft.VisualBasic.FileIO;

namespace KPSimpleBackup
{
Expand Down Expand Up @@ -36,7 +40,7 @@ public override string UpdateUrl
{
get
{
return "https://www.weberone.de/kpsimplebackup.version";
return "https://raw.githubusercontent.com/weberonede/KPSimpleBackup/master/kpsimplebackup.version";
}
}

Expand Down Expand Up @@ -81,23 +85,73 @@ private void OnSaveAction(object sender, FileSavedEventArgs e)
this.BackupAction(e.Database);
}

private void OnMenuBackupNow(object sender, EventArgs e)
{
// show warning and abort if configuration isn't finished
if (!this.m_config.BackupConfigured)
{
MessageService.ShowWarning(
"Database cannot be backuped, because configuration is not finished.",
"Goto Tools -> KPSimpleBackup -> Settings and finish configuration!"
);
return;
}
this.BackupAction(m_host.Database);
}

private void BackupAction(PwDatabase database)
{
string dbName = database.Name;
// don't perform backup if configuration isn't finished
if (!this.m_config.BackupConfigured)
{
return;
}

string dbBackupFileName = this.GetBackupFileName(database);
string time = DateTime.Now.ToString(@"yyyy\.MM\.dd\_H\.mm\.ss");
string backupFolderPath = this.m_config.BackupPath;
string path = backupFolderPath + dbName + "_" + time + ".kdbx";
string path = "file:///" + backupFolderPath + dbBackupFileName + "_" + time + ".kdbx";
IOConnectionInfo connection = IOConnectionInfo.FromPath(path);

// save database TODO add Logger
database.SaveAs(connection, false, null);

// Cleanup
this.Cleanup(backupFolderPath, dbBackupFileName);
}

private void OnMenuBackupNow(object sender, EventArgs e)
private String GetBackupFileName(PwDatabase database)
{
this.BackupAction(m_host.Database);
// start with database name as 'fallback' / default
string backupFileName = database.Name;

// get file name if database name shouldn't be used
if (!this.m_config.UseDatabaseNameForBackupFiles)
{
string path = database.IOConnectionInfo.Path;
backupFileName = Path.GetFileNameWithoutExtension(path);
}

return backupFileName;
}

private void Cleanup(String path, String fileNamePrefix)
{
int filesToKeepAmount = (int)this.m_config.FileAmountToKeep;
// read from settings wether to use recycle bin or delete files permanently
var recycleOption = this.m_config.UseRecycleBinDeletedBackups ? RecycleOption.SendToRecycleBin : RecycleOption.DeletePermanently;

String searchPattern = fileNamePrefix + "*" + ".kdbx";
String[] fileList = Directory.GetFiles(path, searchPattern).OrderBy(f => f).Reverse().ToArray();

// if more backup files available than needed loop through the unnecessary ones and remove them
if (fileList.Count() > filesToKeepAmount)
{
for (int i = filesToKeepAmount; i < fileList.Count(); i++)
{
FileSystem.DeleteFile(fileList[i], UIOption.OnlyErrorDialogs, recycleOption);
}
}
}
}
}
1 change: 1 addition & 0 deletions KPSimpleBackup/KPSimpleBackup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Reference Include="KeePass">
<HintPath>D:\Downloads\KeePass-2.41\KeePass.exe</HintPath>
</Reference>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
Expand Down
47 changes: 35 additions & 12 deletions KPSimpleBackup/KPSimpleBackupConfig.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KeePass.App.Configuration;

namespace KPSimpleBackup
Expand All @@ -11,6 +7,12 @@ public class KPSimpleBackupConfig
{
private AceCustomConfig customConfig;

// default config values
private static readonly String DEFAULT_BACKUP_PATH = "";
private static readonly long DEFAULT_FILE_AMOUNT_TO_KEEP = 15;
private static readonly bool DEFAULT_USE_DATABASE_NAMES_FOR_BACKUP_FILES = false;
private static readonly bool DEFAULT_USE_RECYCLE_BIN_DELETED_BACKUPS = true;

public KPSimpleBackupConfig(AceCustomConfig customConfig)
{
this.customConfig = customConfig;
Expand All @@ -20,38 +22,59 @@ public bool BackupConfigured
{
get
{
return this.customConfig.GetBool("KPSimpleBackupConfig_backupConfigured", false);
return this.BackupPath != DEFAULT_BACKUP_PATH;
}
}

public String BackupPath
{
get
{
return this.customConfig.GetString("KPSimpleBackupConfig_backupPath", DEFAULT_BACKUP_PATH);
}

set
{
this.customConfig.SetBool("KPSimpleBackupConfig_backupConfigured", value);
this.customConfig.SetString("KPSimpleBackupConfig_backupPath", value);
}
}

public String BackupPath
public long FileAmountToKeep
{
get
{
return this.customConfig.GetString("KPSimpleBackupConfig_backupPath");
return this.customConfig.GetLong("KPSimpleBackupConfig_fileAmountToKeep", DEFAULT_FILE_AMOUNT_TO_KEEP);
}

set
{
this.customConfig.SetString("KPSimpleBackupConfig_backupPath", value);
this.customConfig.SetLong("KPSimpleBackupConfig_fileAmountToKeep", value);
}
}

public bool UseDatabaseNameForBackupFiles
{
get
{
return this.customConfig.GetBool("KPSimpleBackupConfig_useDatabaseNameForBackupFiles", DEFAULT_USE_DATABASE_NAMES_FOR_BACKUP_FILES);
}

set
{
this.customConfig.SetBool("KPSimpleBackupConfig_useDatabaseNameForBackupFiles", value);
}
}

public String BackupName
public bool UseRecycleBinDeletedBackups
{
get
{
return this.customConfig.GetString("KPSimpleBackupConfig_backupName", "");
return this.customConfig.GetBool("KPSimpleBackupConfig_useRecycleBinDeletedBackups", DEFAULT_USE_RECYCLE_BIN_DELETED_BACKUPS);
}

set
{
this.customConfig.SetString("KPSimpleBackupConfig_backupName", value);
this.customConfig.SetBool("KPSimpleBackupConfig_useRecycleBinDeletedBackups", value);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions KPSimpleBackup/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("KPSimpleBackup")]
[assembly: AssemblyDescription("Simple Plugin for KeePass-Backups (also working together with IOProtocol Plugin)")]
[assembly: AssemblyDescription("Simple Plugin for KeePass-Backups (working together with IOProtocol Plugin as well!)")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Marvin Weber (weberone)")]
[assembly: AssemblyCompany("Marvin Weber (weberone.de)")]
[assembly: AssemblyProduct("KeePass Plugin")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
Expand All @@ -33,7 +33,7 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.0.0")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: NeutralResourcesLanguage("en")]

Loading

0 comments on commit eb4cbbf

Please sign in to comment.