From 5e8d5ecd69ea727fab50e6abb4c3bf7e26e161e5 Mon Sep 17 00:00:00 2001 From: weberonede Date: Mon, 18 Mar 2019 21:29:46 +0100 Subject: [PATCH] init --- .gitignore | 3 + KPSimpleBackup.sln | 25 +++ KPSimpleBackup/KPSimpleBackup.cs | 103 ++++++++++ KPSimpleBackup/KPSimpleBackup.csproj | 77 ++++++++ KPSimpleBackup/KPSimpleBackupConfig.cs | 58 ++++++ KPSimpleBackup/Properties/AssemblyInfo.cs | 39 ++++ .../Properties/Settings.Designer.cs | 26 +++ KPSimpleBackup/Properties/Settings.settings | 6 + KPSimpleBackup/SettingsForm.Designer.cs | 182 ++++++++++++++++++ KPSimpleBackup/SettingsForm.cs | 51 +++++ KPSimpleBackup/SettingsForm.resx | 120 ++++++++++++ KPSimpleBackup/app.config | 3 + README.md | 7 + 13 files changed, 700 insertions(+) create mode 100644 .gitignore create mode 100644 KPSimpleBackup.sln create mode 100644 KPSimpleBackup/KPSimpleBackup.cs create mode 100644 KPSimpleBackup/KPSimpleBackup.csproj create mode 100644 KPSimpleBackup/KPSimpleBackupConfig.cs create mode 100644 KPSimpleBackup/Properties/AssemblyInfo.cs create mode 100644 KPSimpleBackup/Properties/Settings.Designer.cs create mode 100644 KPSimpleBackup/Properties/Settings.settings create mode 100644 KPSimpleBackup/SettingsForm.Designer.cs create mode 100644 KPSimpleBackup/SettingsForm.cs create mode 100644 KPSimpleBackup/SettingsForm.resx create mode 100644 KPSimpleBackup/app.config diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d47aa4c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vs +/KPSimpleBackup/bin +/KPSimpleBackup/obj diff --git a/KPSimpleBackup.sln b/KPSimpleBackup.sln new file mode 100644 index 0000000..e9dd5ed --- /dev/null +++ b/KPSimpleBackup.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.489 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KPSimpleBackup", "KPSimpleBackup\KPSimpleBackup.csproj", "{90AE5E95-FAC2-46D6-8B6B-BAB535323BB1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {90AE5E95-FAC2-46D6-8B6B-BAB535323BB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {90AE5E95-FAC2-46D6-8B6B-BAB535323BB1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {90AE5E95-FAC2-46D6-8B6B-BAB535323BB1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {90AE5E95-FAC2-46D6-8B6B-BAB535323BB1}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B99C9C95-947A-4F53-9A66-BC0C4C747404} + EndGlobalSection +EndGlobal diff --git a/KPSimpleBackup/KPSimpleBackup.cs b/KPSimpleBackup/KPSimpleBackup.cs new file mode 100644 index 0000000..2495205 --- /dev/null +++ b/KPSimpleBackup/KPSimpleBackup.cs @@ -0,0 +1,103 @@ +using System; +using System.Windows.Forms; +using KeePass.Forms; +using KeePass.Plugins; +using KeePassLib; +using KeePassLib.Serialization; + +namespace KPSimpleBackup +{ + public sealed class KPSimpleBackupExt : Plugin + { + private IPluginHost m_host = null; + private KPSimpleBackupConfig m_config = null; + + public override bool Initialize(IPluginHost host) + { + if (host == null) return false; + + m_host = host; + m_config = new KPSimpleBackupConfig(m_host.CustomConfig); + + // add backup action event handler + m_host.MainWindow.FileSaved += this.OnSaveAction; + + // initialization successful + return true; + } + + public override void Terminate() + { + // Remove event handler + m_host.MainWindow.FileSaved -= this.OnSaveAction; + } + + public override string UpdateUrl + { + get + { + return "https://www.weberone.de/kpsimplebackup.version"; + } + } + + public override ToolStripMenuItem GetMenuItem(PluginMenuType t) + { + // Provide a menu item for the main location(s) + if (t == PluginMenuType.Main) + { + ToolStripMenuItem tsmi = new ToolStripMenuItem(); + + tsmi.Text = "KPSimpleBackup"; + //tsmi.Click += this.OnOptionsClicked; + + ToolStripMenuItem backupNowItem = new ToolStripMenuItem(); + backupNowItem.Text = "Backup Database now!"; + backupNowItem.Click += this.OnMenuBackupNow; + + ToolStripMenuItem openSettings = new ToolStripMenuItem(); + openSettings.Text = "Settings"; + openSettings.Click += this.OnMenuSettings; + + + tsmi.DropDownItems.Add(backupNowItem); + tsmi.DropDownItems.Add(openSettings); + + return tsmi; + } + + return null; // No menu items in other locations + } + + private void OnMenuSettings(object sender, EventArgs e) + { + SettingsForm settingsWindow = new SettingsForm(this.m_config); + settingsWindow.ShowDialog(); + settingsWindow.Dispose(); + settingsWindow = null; + } + + private void OnSaveAction(object sender, FileSavedEventArgs e) + { + this.BackupAction(e.Database); + } + + private void BackupAction(PwDatabase database) + { + string dbName = database.Name; + string time = DateTime.Now.ToString(@"yyyy\.MM\.dd\_H\.mm\.ss"); + string backupFolderPath = this.m_config.BackupPath; + string path = backupFolderPath + dbName + "_" + time + ".kdbx"; + IOConnectionInfo connection = IOConnectionInfo.FromPath(path); + + // save database TODO add Logger + database.SaveAs(connection, false, null); + } + + private void OnMenuBackupNow(object sender, EventArgs e) + { + this.BackupAction(m_host.Database); + } + + + } +} \ No newline at end of file diff --git a/KPSimpleBackup/KPSimpleBackup.csproj b/KPSimpleBackup/KPSimpleBackup.csproj new file mode 100644 index 0000000..d80f9de --- /dev/null +++ b/KPSimpleBackup/KPSimpleBackup.csproj @@ -0,0 +1,77 @@ + + + + + Debug + AnyCPU + {90AE5E95-FAC2-46D6-8B6B-BAB535323BB1} + Library + Properties + KPSimpleBackup + KPSimpleBackup + v4.6.1 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + D:\Downloads\KeePass-2.41\KeePass.exe + + + + + + + + + + + + + + + + + + True + True + Settings.settings + + + Form + + + SettingsForm.cs + + + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + SettingsForm.cs + + + + \ No newline at end of file diff --git a/KPSimpleBackup/KPSimpleBackupConfig.cs b/KPSimpleBackup/KPSimpleBackupConfig.cs new file mode 100644 index 0000000..10eebe3 --- /dev/null +++ b/KPSimpleBackup/KPSimpleBackupConfig.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using KeePass.App.Configuration; + +namespace KPSimpleBackup +{ + public class KPSimpleBackupConfig + { + private AceCustomConfig customConfig; + + public KPSimpleBackupConfig(AceCustomConfig customConfig) + { + this.customConfig = customConfig; + } + + public bool BackupConfigured + { + get + { + return this.customConfig.GetBool("KPSimpleBackupConfig_backupConfigured", false); + } + + set + { + this.customConfig.SetBool("KPSimpleBackupConfig_backupConfigured", value); + } + } + + public String BackupPath + { + get + { + return this.customConfig.GetString("KPSimpleBackupConfig_backupPath"); + } + + set + { + this.customConfig.SetString("KPSimpleBackupConfig_backupPath", value); + } + } + + public String BackupName + { + get + { + return this.customConfig.GetString("KPSimpleBackupConfig_backupName", ""); + } + + set + { + this.customConfig.SetString("KPSimpleBackupConfig_backupName", value); + } + } + } +} diff --git a/KPSimpleBackup/Properties/AssemblyInfo.cs b/KPSimpleBackup/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..50d85f8 --- /dev/null +++ b/KPSimpleBackup/Properties/AssemblyInfo.cs @@ -0,0 +1,39 @@ +using System.Resources; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// 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: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Marvin Weber (weberone)")] +[assembly: AssemblyProduct("KeePass Plugin")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("90ae5e95-fac2-46d6-8b6b-bab535323bb1")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// 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: NeutralResourcesLanguage("en")] + diff --git a/KPSimpleBackup/Properties/Settings.Designer.cs b/KPSimpleBackup/Properties/Settings.Designer.cs new file mode 100644 index 0000000..16945ca --- /dev/null +++ b/KPSimpleBackup/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// Dieser Code wurde von einem Tool generiert. +// Laufzeitversion:4.0.30319.42000 +// +// Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn +// der Code erneut generiert wird. +// +//------------------------------------------------------------------------------ + +namespace KPSimpleBackup.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/KPSimpleBackup/Properties/Settings.settings b/KPSimpleBackup/Properties/Settings.settings new file mode 100644 index 0000000..049245f --- /dev/null +++ b/KPSimpleBackup/Properties/Settings.settings @@ -0,0 +1,6 @@ + + + + + + diff --git a/KPSimpleBackup/SettingsForm.Designer.cs b/KPSimpleBackup/SettingsForm.Designer.cs new file mode 100644 index 0000000..e92926b --- /dev/null +++ b/KPSimpleBackup/SettingsForm.Designer.cs @@ -0,0 +1,182 @@ +namespace KPSimpleBackup +{ + partial class SettingsForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.panel1 = new System.Windows.Forms.Panel(); + this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.panel2 = new System.Windows.Forms.Panel(); + this.labelSelectedBackupPath = new System.Windows.Forms.Label(); + this.buttonSelectFolder = new System.Windows.Forms.Button(); + this.buttonSave = new System.Windows.Forms.Button(); + this.labelSettingsIntro = new System.Windows.Forms.Label(); + this.numericUpDown1 = new System.Windows.Forms.NumericUpDown(); + this.panel1.SuspendLayout(); + this.tableLayoutPanel1.SuspendLayout(); + this.panel2.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit(); + this.SuspendLayout(); + // + // panel1 + // + this.panel1.Controls.Add(this.tableLayoutPanel1); + this.panel1.Controls.Add(this.buttonSave); + this.panel1.Controls.Add(this.labelSettingsIntro); + this.panel1.Location = new System.Drawing.Point(12, 12); + this.panel1.Name = "panel1"; + this.panel1.Size = new System.Drawing.Size(400, 140); + this.panel1.TabIndex = 0; + // + // tableLayoutPanel1 + // + this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single; + this.tableLayoutPanel1.ColumnCount = 2; + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 40.81081F)); + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 59.18919F)); + this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0); + this.tableLayoutPanel1.Controls.Add(this.panel2, 1, 1); + this.tableLayoutPanel1.Controls.Add(this.numericUpDown1, 1, 0); + this.tableLayoutPanel1.Controls.Add(this.label2, 0, 1); + this.tableLayoutPanel1.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.FixedSize; + this.tableLayoutPanel1.Location = new System.Drawing.Point(6, 16); + this.tableLayoutPanel1.Name = "tableLayoutPanel1"; + this.tableLayoutPanel1.RowCount = 2; + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 30.43478F)); + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 69.56522F)); + this.tableLayoutPanel1.Size = new System.Drawing.Size(387, 92); + this.tableLayoutPanel1.TabIndex = 3; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(4, 1); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(142, 26); + this.label1.TabIndex = 1; + this.label1.Text = "Number of backups to keep:\r\n(not working yet)"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(4, 44); + this.label2.Margin = new System.Windows.Forms.Padding(3, 15, 3, 0); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(114, 13); + this.label2.TabIndex = 3; + this.label2.Text = "Folder for backup files:"; + // + // panel2 + // + this.panel2.Controls.Add(this.labelSelectedBackupPath); + this.panel2.Controls.Add(this.buttonSelectFolder); + this.panel2.Location = new System.Drawing.Point(161, 32); + this.panel2.Name = "panel2"; + this.panel2.Size = new System.Drawing.Size(222, 56); + this.panel2.TabIndex = 4; + // + // labelSelectedBackupPath + // + this.labelSelectedBackupPath.AutoSize = true; + this.labelSelectedBackupPath.Location = new System.Drawing.Point(4, 33); + this.labelSelectedBackupPath.Name = "labelSelectedBackupPath"; + this.labelSelectedBackupPath.Size = new System.Drawing.Size(29, 13); + this.labelSelectedBackupPath.TabIndex = 3; + this.labelSelectedBackupPath.Text = "Path"; + // + // buttonSelectFolder + // + this.buttonSelectFolder.Location = new System.Drawing.Point(3, 3); + this.buttonSelectFolder.Name = "buttonSelectFolder"; + this.buttonSelectFolder.Size = new System.Drawing.Size(75, 23); + this.buttonSelectFolder.TabIndex = 2; + this.buttonSelectFolder.Text = "Select folder"; + this.buttonSelectFolder.UseVisualStyleBackColor = true; + this.buttonSelectFolder.Click += new System.EventHandler(this.buttonSelectFolder_Click); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(322, 114); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(75, 23); + this.buttonSave.TabIndex = 2; + this.buttonSave.Text = "Save"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); + // + // labelSettingsIntro + // + this.labelSettingsIntro.AutoSize = true; + this.labelSettingsIntro.Location = new System.Drawing.Point(3, 0); + this.labelSettingsIntro.Name = "labelSettingsIntro"; + this.labelSettingsIntro.Size = new System.Drawing.Size(390, 13); + this.labelSettingsIntro.TabIndex = 1; + this.labelSettingsIntro.Text = "Please specify following options (at least the path is needed for backups to work" + + "):"; + // + // numericUpDown1 + // + this.numericUpDown1.Location = new System.Drawing.Point(161, 4); + this.numericUpDown1.Name = "numericUpDown1"; + this.numericUpDown1.Size = new System.Drawing.Size(120, 20); + this.numericUpDown1.TabIndex = 5; + // + // SettingsForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(417, 168); + this.Controls.Add(this.panel1); + this.Name = "SettingsForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Settings"; + this.panel1.ResumeLayout(false); + this.panel1.PerformLayout(); + this.tableLayoutPanel1.ResumeLayout(false); + this.tableLayoutPanel1.PerformLayout(); + this.panel2.ResumeLayout(false); + this.panel2.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + private System.Windows.Forms.Panel panel1; + private System.Windows.Forms.Label labelSettingsIntro; + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; + private System.Windows.Forms.Button buttonSave; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Button buttonSelectFolder; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Panel panel2; + private System.Windows.Forms.Label labelSelectedBackupPath; + private System.Windows.Forms.NumericUpDown numericUpDown1; + } +} \ No newline at end of file diff --git a/KPSimpleBackup/SettingsForm.cs b/KPSimpleBackup/SettingsForm.cs new file mode 100644 index 0000000..0f8053d --- /dev/null +++ b/KPSimpleBackup/SettingsForm.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace KPSimpleBackup +{ + public partial class SettingsForm : Form + { + private KPSimpleBackupConfig appConfig; + + public SettingsForm(KPSimpleBackupConfig config) + { + this.appConfig = config; + + InitializeComponent(); + + // load (already) configured values + labelSelectedBackupPath.Text = this.appConfig.BackupPath; + } + + private void buttonSelectFolder_Click(object sender, EventArgs e) + { + System.Windows.Forms.FolderBrowserDialog objDialog = new FolderBrowserDialog(); + objDialog.Description = "Backup Path"; + objDialog.SelectedPath = @"C:\"; + DialogResult objResult = objDialog.ShowDialog(this); + if (objResult == DialogResult.OK) + { + string pathSelected = objDialog.SelectedPath; + pathSelected = pathSelected.Replace("\\", "/"); + + string newPath = "file:///" + pathSelected + "/"; + + this.appConfig.BackupPath = newPath; + this.appConfig.BackupConfigured = true; + this.labelSelectedBackupPath.Text = newPath; + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + this.Close(); + } + } +} diff --git a/KPSimpleBackup/SettingsForm.resx b/KPSimpleBackup/SettingsForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/KPSimpleBackup/SettingsForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/KPSimpleBackup/app.config b/KPSimpleBackup/app.config new file mode 100644 index 0000000..49cc43e --- /dev/null +++ b/KPSimpleBackup/app.config @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/README.md b/README.md index 886d684..843f23d 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ # KPSimpleBackup + +## Planned features +This is the first working version of this simple plugin. Following features will be added in the future: +* Keep x backup-files and delete older ones +* Use database file name for backup file name (instead of Database name set in the database settings) + +~ WIP ~ \ No newline at end of file