svnsync/svnsync/Form1.cs
2017-04-11 17:56:55 +00:00

538 lines
21 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BlubbFish.Utils;
using svnsync.Libraries;
namespace svnsync
{
public partial class Form1 : Form
{
enum HandleSvnType
{
NotInit,
ToMutchChronRuns,
HasVersionitedFiles,
UnexpectedError,
ExternDeletedFiles,
}
private CmdArgs c;
private Svn s;
public Form1(string[] args)
{
InitializeComponent();
/*List<String> l = new List<string>();
l.Add("/abc/Bla/Blubb/");
l.Add("/Blubb/Foo/");
l.Add("/Blubb/Bla/");
l.Add("/Blubb/Bla/Foo");
l.Add("/Blubb/Bla/Blubb");
this.CreateFileList("Löschen", "Diese Daten sind gelöscht worden:", l, new EventHandler(deleteButton_Click));
*/
Dictionary<string, CmdArgs.VaildArguments> pargs = new Dictionary<string, CmdArgs.VaildArguments>();
pargs.Add("-d", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Touple, true));
pargs.Add("-cron", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Single));
pargs.Add("-autoadd", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Single));
pargs.Add("-autodelete", new CmdArgs.VaildArguments(CmdArgs.ArgLength.Single));
this.c = CmdArgs.Instance;
this.c.SetArguments(pargs, args);
this.StartSvn();
if (this.s != null)
{
this.notifyIcon.Text = "SvnSync: " + this.c.GetArgumentData("-d");
}
this.Init();
}
private void Init()
{
if (this.c.HasArgumentType("-cron"))
if (this.cronJob(0, false))
{
this.ShowToolTip("Svn Sync", "Datensicherung Erfolgreich\n" + this.c.GetArgumentData("-d").Replace("\\\\","\\"), ToolTipIcon.Info);
System.Threading.Thread.Sleep(5000);
this.Close();
//Application.Exit();
}
}
private bool cronJob(int run, bool readyToCommit)
{
if (s == null)
return this.HandleSvn(HandleSvnType.NotInit, "");
if (run > 10)
return this.HandleSvn(HandleSvnType.ToMutchChronRuns, "");
if (s.IsError)
return this.HandleSvn(HandleSvnType.UnexpectedError, s.Error);
try
{
s.CheckStatus();
}
catch (NotImplementedException e)
{
return this.HandleSvn(HandleSvnType.UnexpectedError, e.Message);
}
if (s.IsNotOnlyModified)
{
if (s.IsUncheckedFiles)
{
if (c.HasArgumentType("-autoadd"))
s.AddFiles(s.NoVersionFiles);
else
return this.HandleSvn(HandleSvnType.HasVersionitedFiles, "");
}
if (s.IsError)
return this.HandleSvn(HandleSvnType.UnexpectedError, s.Error);
if (s.IsExternDeletedFiles)
{
if (c.HasArgumentType("-autodelete"))
s.DeleteFiles(s.WasDeletedFiles);
else
return this.HandleSvn(HandleSvnType.ExternDeletedFiles, "");
}
if (s.IsError)
return this.HandleSvn(HandleSvnType.UnexpectedError, s.Error);
return this.cronJob(run + 1, true);
}
else
{
if (readyToCommit)
{
s.SetArgCheckIn(this.GetTimeStamp(DateTime.Now));
if (s.IsError)
return this.HandleSvn(HandleSvnType.UnexpectedError, s.Error);
}
else
{
s.Update();
if (s.IsError)
return this.HandleSvn(HandleSvnType.UnexpectedError, s.Error);
return this.cronJob(run + 1, true);
}
}
return true;
}
private bool HandleSvn(HandleSvnType handleSvnType, string p)
{
switch (handleSvnType)
{
case HandleSvnType.NotInit:
this.ShowToolTip("Svn Sync Fehler", "Svn wurde noch nicht inizialisiert", ToolTipIcon.Error);
return false;
case HandleSvnType.ToMutchChronRuns:
this.ShowToolTip("Svn Sync Fehler", "Fehler im Cronjob, mehr als 10 Aufrufe!", ToolTipIcon.Error);
this.CreateSVNOpen("Fehler im Cronjob, mehr als 10 Aufrufe!", "");
return false;
case HandleSvnType.HasVersionitedFiles:
this.ShowToolTip("Svn Sync", "Es sind unversionierte Dateien vorhanden (?)", ToolTipIcon.Info);
this.CreateFileList("Hinzufügen", "Diese Daten sind unversioniert:", s.NoVersionFiles, new EventHandler(addButton_Click));
return false;
case HandleSvnType.UnexpectedError:
this.ShowToolTip("Svn Sync Fehler", "Es ist ein Unvorhersebarer Fehler aufgetreten: " + p, ToolTipIcon.Error);
this.CreateSVNOpen("Es ist ein Unvorhersebarer Fehler aufgetreten", p);
return false;
case HandleSvnType.ExternDeletedFiles:
this.ShowToolTip("Svn Sync", "Es sind extern gelöschte Dateien vorhanden (!)", ToolTipIcon.Info);
this.CreateFileList("Löschen", "Diese Daten sind gelöscht worden:", s.WasDeletedFiles, new EventHandler(deleteButton_Click));
return false;
}
return false;
}
private void CreateFileList(string textlabel, string titel, List<String> list, EventHandler eventHandler)
{
this.Controls.Clear();
list.Sort();
System.Windows.Forms.TreeView tree = new System.Windows.Forms.TreeView();
tree.Location = new System.Drawing.Point(10, 30);
tree.Size = new System.Drawing.Size(350, 180);
tree.TabIndex = 0;
tree.Nodes.AddRange(this.createDirList(list));
tree.CheckBoxes = true;
tree.HideSelection = false;
tree.ImageList = this.getImages(tree.Nodes);
this.Controls.Add(tree);
System.Windows.Forms.Label label = new System.Windows.Forms.Label();
label.AutoSize = true;
label.Location = new System.Drawing.Point(10, 10);
label.Size = new System.Drawing.Size(35, 13);
label.TabIndex = 1;
label.Text = titel;
this.Controls.Add(label);
System.Windows.Forms.Button runButton = new System.Windows.Forms.Button();
runButton.Location = new System.Drawing.Point(10, 217);
runButton.Size = new System.Drawing.Size(120, 23);
runButton.TabIndex = 2;
runButton.Text = textlabel;
runButton.UseVisualStyleBackColor = true;
runButton.Click += eventHandler;
this.Controls.Add(runButton);
System.Windows.Forms.Button doAgainButton = new System.Windows.Forms.Button();
doAgainButton.Location = new System.Drawing.Point(136, 216);
doAgainButton.Size = new System.Drawing.Size(120, 23);
doAgainButton.TabIndex = 3;
doAgainButton.Text = "Wiederholen";
doAgainButton.UseVisualStyleBackColor = true;
doAgainButton.Click += new EventHandler(runagainButton_Click);
this.Controls.Add(doAgainButton);
System.Windows.Forms.Button svnOpenButton = new System.Windows.Forms.Button();
svnOpenButton.Location = new System.Drawing.Point(263, 216);
svnOpenButton.Size = new System.Drawing.Size(100, 23);
svnOpenButton.TabIndex = 4;
svnOpenButton.Text = "SVN Öffnen";
svnOpenButton.UseVisualStyleBackColor = true;
svnOpenButton.Click += new EventHandler(openButton_Click);
this.Controls.Add(svnOpenButton);
}
private ImageList getImages(TreeNodeCollection treeNodeCollection)
{
ImageList li = new ImageList();
this.runThroughNodes(li.Images, treeNodeCollection);
return li;
}
private void runThroughNodes(ImageList.ImageCollection imageCollection, TreeNodeCollection treeNodeCollection)
{
foreach (TreeNode item in treeNodeCollection)
{
imageCollection.Add(this.getImageType(item.Text));
item.ImageIndex = imageCollection.Count - 1;
item.Text = item.Text.Substring(0, item.Text.LastIndexOf("."));
if (item.Nodes.Count > 0)
this.runThroughNodes(imageCollection, item.Nodes);
}
}
private Icon getImageType(string p)
{
switch (p.Substring(p.LastIndexOf(".")))
{
case ".folder": return Properties.Resources.Special_Folder;
case ".file": return Properties.Resources.Special_File;
}
return Properties.Resources.Special_File;
}
class TreePart
{
public TreePart(String name, bool isFolder, List<TreePart> child)
{
this.name = name;
this.child = child;
this.isFolder = isFolder;
}
public string name { get; private set; }
public List<TreePart> child { get; set; }
public bool isFolder { get; set; }
}
private TreeNode[] createDirList(List<string> tree)
{
String root = this.c.GetArgumentData("-d");
List<TreePart> ntree = new List<TreePart>();
foreach (String node in tree)
{
String folder = node;
folder = folder.Replace('\\', '/');
folder = (folder.StartsWith("/")) ? folder.Substring(1) : folder;
folder = (folder.EndsWith("/")) ? folder.Substring(0, folder.Length-1) : folder;
bool isFolder = System.IO.Directory.Exists(root + "/" + folder);
String[] folders = folder.Split('/');
Array.Reverse(folders);
List<TreePart> prev = null;
foreach (String entry in folders)
{
String name = entry;
if (prev == null)
prev = new List<TreePart>() { new TreePart(name, isFolder, new List<TreePart>()) };
else
prev = new List<TreePart>() { new TreePart(name, isFolder, prev) };
if (!isFolder)
isFolder = true;
}
ntree = this.treeMatch(ntree, prev);
}
return this.createListDirRender(ntree);
}
private TreeNode[] createListDirRender(List<TreePart> ntree)
{
TreeNode[] t = new TreeNode[ntree.Count];
int i = 0;
foreach (TreePart item in ntree)
{
String name = item.name + ((item.isFolder) ? ".folder" : (item.name.LastIndexOf('.') == -1) ? ".file" : item.name.Substring(item.name.LastIndexOf('.')));
if(item.child.Count == 0)
t[i++] = new TreeNode(name);
else
t[i++] = new TreeNode(name, this.createListDirRender(item.child));
t[i - 1].Checked = true;
}
return t;
}
private List<TreePart> treeMatch(List<TreePart> ntree, List<TreePart> input)
{
String[] key1 = this.array_keys(input);
if (ntree.Count == 0 || (key1.Length > 0 && !array_key_exists(key1[0], ntree)) || key1.Length == 0)
{
ntree.Add(input[0]);
}
else
{
String[] key2 = array_keys(findName(key1[0], input));
if (key2.Length != 0)
{
if (array_key_exists(key2[0], findName(key1[0], ntree)))
{
/*setChild(key2[0], findName(key1[0], ntree),*/
this.treeMatch(findName(key1[0], ntree), findName(key1[0], input));
}
else
{
findName(key1[0], ntree).Add(findName(key1[0], input)[0]);
}
}
}
return ntree;
}
private void setChild(string key, List<TreePart> input, List<TreePart> list)
{
foreach (TreePart item in input)
{
if (item.name == key)
item.child = list;
}
}
private List<TreePart> findName(string key, List<TreePart> input)
{
foreach (TreePart item in input)
{
if (item.name == key)
return item.child;
}
return null;
}
private bool array_key_exists(string p, List<TreePart> ntree)
{
foreach (TreePart item in ntree)
{
if (item.name == p)
return true;
}
return false;
}
private string[] array_keys(List<TreePart> input)
{
List<String> ret = new List<String>();
foreach (TreePart item in input)
{
ret.Add(item.name);
}
return ret.ToArray();
}
private TreePart treeMatch(TreePart ntree, TreePart prev)
{
throw new NotImplementedException();
}
private void CreateSVNOpen(string title, string message)
{
this.Controls.Clear();
System.Windows.Forms.Label label = new System.Windows.Forms.Label();
label.AutoSize = true;
label.Location = new System.Drawing.Point(10, 10);
label.Size = new System.Drawing.Size(350, 13);
label.TabIndex = 0;
label.Text = title;
this.Controls.Add(label);
System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();
textBox.Location = new System.Drawing.Point(10, 30);
textBox.Multiline = true;
textBox.ReadOnly = true;
textBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
textBox.Size = new System.Drawing.Size(350, 120);
textBox.TabIndex = 0;
textBox.Text = message;
this.Controls.Add(textBox);
System.Windows.Forms.Button openButton = new System.Windows.Forms.Button();
openButton.Location = new System.Drawing.Point(10, 160);
openButton.Size = new System.Drawing.Size(120, 23);
openButton.TabIndex = 0;
openButton.Text = "SVN Öffnen";
openButton.UseVisualStyleBackColor = true;
openButton.Click += new EventHandler(openButton_Click);
this.Controls.Add(openButton);
System.Windows.Forms.Button runagainButton = new System.Windows.Forms.Button();
runagainButton.Location = new System.Drawing.Point(140, 160);
runagainButton.Size = new System.Drawing.Size(120, 23);
runagainButton.TabIndex = 0;
runagainButton.Text = "Wiederholen";
runagainButton.UseVisualStyleBackColor = true;
runagainButton.Click += new EventHandler(runagainButton_Click);
this.Controls.Add(runagainButton);
}
void runagainButton_Click(object sender, EventArgs e)
{
this.Controls.Clear();
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
this.Init();
}
void openButton_Click(object sender, EventArgs e)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.WorkingDirectory = this.c.GetArgumentData("-d");
p.Start();
}
void deleteButton_Click(object sender, EventArgs e)
{
System.Windows.Forms.TreeView t = null;
foreach (var item in this.Controls)
{
if (item is System.Windows.Forms.TreeView)
{
t = (System.Windows.Forms.TreeView)item;
}
}
if (t == null)
return;
List<TreeNode> l = this.getSelectedNodes(t.Nodes);
this.Controls.Clear();
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
s.DeleteFiles(l);
if (s.IsError)
{
this.HandleSvn(HandleSvnType.UnexpectedError, s.Error);
return;
}
this.runagainButton_Click(sender, null);
}
private List<TreeNode> getSelectedNodes(TreeNodeCollection treeNodeCollection)
{
List<TreeNode> l = new List<TreeNode>();
foreach (TreeNode item in treeNodeCollection)
{
if (item.Checked && item.Nodes.Count == 0)
l.Add(item);
if (item.Nodes.Count > 0)
l.AddRange(this.getSelectedNodes(item.Nodes));
}
return l;
}
void addButton_Click(object sender, EventArgs e)
{
System.Windows.Forms.TreeView t = null;
foreach (var item in this.Controls)
{
if (item is System.Windows.Forms.TreeView)
{
t = (System.Windows.Forms.TreeView)item;
}
}
if (t == null)
return;
List<TreeNode> l = this.getSelectedNodes(t.Nodes);
this.Controls.Clear();
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
s.AddFiles(l);
if (s.IsError)
{
this.HandleSvn(HandleSvnType.UnexpectedError, s.Error);
return;
}
this.runagainButton_Click(sender, null);
}
private string GetTimeStamp(DateTime dateTime)
{
String ret = "";
ret += dateTime.Year.ToString().PadLeft(4, '0');
ret += dateTime.Month.ToString().PadLeft(2, '0');
ret += dateTime.Day.ToString().PadLeft(2, '0');
ret += "-";
ret += dateTime.Hour.ToString().PadLeft(2, '0');
ret += dateTime.Minute.ToString().PadLeft(2, '0');
ret += dateTime.Second.ToString().PadLeft(2, '0');
return ret;
}
private void StartSvn()
{
if (!this.c.HasArgumentType("-d"))
{
this.ShowToolTip("Svn Sync Fehler", "Kein Verzeichniss angegeben \"-d\" fehlt", ToolTipIcon.Error);
return;
}
if (this.c.GetArgumentData("-d") == null)
{
this.ShowToolTip("Svn Sync Fehler", "Kein Argument für \"-d\" angegeben", ToolTipIcon.Error);
return;
}
this.s = Svn.Instance;
this.s.SetDirectory(this.c.GetArgumentData("-d"));
}
private void EventHandler_Resize(object sender, System.EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
}
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
foreach (var item in this.Controls)
{
if (item is System.Windows.Forms.TreeView)
{
System.Windows.Forms.TreeView t = (System.Windows.Forms.TreeView)item;
t.ExpandAll();
}
}
}
private void ShowToolTip(string title, string text, ToolTipIcon toolTipIcon)
{
this.notifyIcon.ShowBalloonTip(15000, title, text, toolTipIcon);
this.notifyIcon.BalloonTipClicked += new EventHandler(notifyIcon_BalloonTipClicked);
//Add to Notify Quene
}
private void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
{
this.notifyIcon_MouseDoubleClick(sender, null);
}
}
}