Added support for packing.

This commit is contained in:
2017-04-20 19:32:24 +01:00
parent c3ed2192b8
commit 5512243bfd
7 changed files with 353 additions and 33 deletions

View File

@@ -1,3 +1,13 @@
2017-04-20 Natalia Portillo <claunia@claunia.com>
* Core.cs:
* MainWindow.cs:
* packages.config:
* gtk-gui/gui.stetic:
* osrepodbmgr.csproj:
* gtk-gui/MainWindow.cs:
Added support for packing.
2017-04-20 Natalia Portillo <claunia@claunia.com>
* Core.cs:

View File

@@ -29,6 +29,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Ionic.Zip;
namespace osrepodbmgr
{
@@ -41,16 +42,21 @@ namespace osrepodbmgr
public delegate void UpdateProgress2Delegate(string text, string inner, long current, long maximum);
public delegate void FailedDelegate(string text);
public delegate void FinishedWithoutErrorDelegate();
public delegate void FinishedWithTextDelegate(string text);
public delegate void AddEntryDelegate(string filename, string hash, bool known);
public static event UpdateProgressDelegate UpdateProgress;
public static event UpdateProgress2Delegate UpdateProgress2;
public static event FailedDelegate Failed;
public static event FinishedWithoutErrorDelegate Finished;
public static event FinishedWithTextDelegate FinishedWithText;
public static event AddEntryDelegate AddEntry;
static DBCore dbCore;
static int zipCounter;
static string zipCurrentEntryName;
public static void FindFiles()
{
if(string.IsNullOrEmpty(MainClass.path))
@@ -270,5 +276,198 @@ namespace osrepodbmgr
if(dbCore != null)
dbCore.CloseDB();
}
public static void CompressFiles()
{
try
{
if(string.IsNullOrWhiteSpace(MainClass.dbInfo.developer))
{
if(Failed != null)
Failed("Developer cannot be empty");
return;
}
if(string.IsNullOrWhiteSpace(MainClass.dbInfo.product))
{
if(Failed != null)
Failed("Product cannot be empty");
return;
}
if(string.IsNullOrWhiteSpace(MainClass.dbInfo.version))
{
if(Failed != null)
Failed("Version cannot be empty");
return;
}
// Check if repository folder exists
string destinationFolder = Settings.Current.RepositoryPath;
if(!Directory.Exists(destinationFolder))
Directory.CreateDirectory(destinationFolder);
// Check if developer folder exists
destinationFolder = Path.Combine(destinationFolder, MainClass.dbInfo.developer);
if(!Directory.Exists(destinationFolder))
Directory.CreateDirectory(destinationFolder);
// Check if product folder exists
destinationFolder = Path.Combine(destinationFolder, MainClass.dbInfo.product);
if(!Directory.Exists(destinationFolder))
Directory.CreateDirectory(destinationFolder);
// Check if version folder exists
destinationFolder = Path.Combine(destinationFolder, MainClass.dbInfo.version);
if(!Directory.Exists(destinationFolder))
Directory.CreateDirectory(destinationFolder);
if(!string.IsNullOrWhiteSpace(MainClass.dbInfo.languages))
{
// Check if languages folder exists
destinationFolder = Path.Combine(destinationFolder, MainClass.dbInfo.languages);
if(!Directory.Exists(destinationFolder))
Directory.CreateDirectory(destinationFolder);
}
if(!string.IsNullOrWhiteSpace(MainClass.dbInfo.architecture))
{
// Check if architecture folder exists
destinationFolder = Path.Combine(destinationFolder, MainClass.dbInfo.architecture);
if(!Directory.Exists(destinationFolder))
Directory.CreateDirectory(destinationFolder);
}
if(MainClass.dbInfo.oem)
{
// Check if oem folder exists
destinationFolder = Path.Combine(destinationFolder, "oem");
if(!Directory.Exists(destinationFolder))
Directory.CreateDirectory(destinationFolder);
}
if(!string.IsNullOrWhiteSpace(MainClass.dbInfo.machine))
{
// Check if architecture folder exists
destinationFolder = Path.Combine(destinationFolder, "for " + MainClass.dbInfo.machine);
if(!Directory.Exists(destinationFolder))
Directory.CreateDirectory(destinationFolder);
}
string destinationFile = "";
if(!string.IsNullOrWhiteSpace(MainClass.dbInfo.format))
destinationFile += "[" + MainClass.dbInfo.format + "]";
if(MainClass.dbInfo.files)
{
if(destinationFile != "")
destinationFile += "_";
destinationFile += "files";
}
if(MainClass.dbInfo.netinstall)
{
if(destinationFile != "")
destinationFile += "_";
destinationFile += "netinstall";
}
if(MainClass.dbInfo.source)
{
if(destinationFile != "")
destinationFile += "_";
destinationFile += "source";
}
if(MainClass.dbInfo.update)
{
if(destinationFile != "")
destinationFile += "_";
destinationFile += "update";
}
if(MainClass.dbInfo.upgrade)
{
if(destinationFile != "")
destinationFile += "_";
destinationFile += "upgrade";
}
if(!string.IsNullOrWhiteSpace(MainClass.dbInfo.description))
{
if(destinationFile != "")
destinationFile += "_";
destinationFile += MainClass.dbInfo.description;
}
else if(destinationFile == "")
{
destinationFile = "archive";
}
string destination = Path.Combine(destinationFolder, destinationFile) + ".zip";
if(File.Exists(destination))
{
if(Failed != null)
Failed("File already exists");
return;
}
ZipFile zf = new ZipFile(destination);
zf.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
zf.CompressionMethod = CompressionMethod.Deflate;
zf.UseZip64WhenSaving = Zip64Option.AsNecessary;
int counter = 0;
foreach(string file in MainClass.files)
{
if(UpdateProgress != null)
UpdateProgress("Choosing files...", file, counter, MainClass.files.Count);
//FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
FileInfo fi = new FileInfo(file);
ZipEntry ze = zf.AddFile(file);
ze.AccessedTime = fi.LastAccessTimeUtc;
ze.Attributes = fi.Attributes;
ze.CreationTime = fi.CreationTimeUtc;
ze.EmitTimesInUnixFormatWhenSaving = true;
ze.LastModified = fi.LastWriteTimeUtc;
ze.FileName = file.Substring(MainClass.path.Length + 1);
//fs.Close();
if(file == "metadata.json")
File.Copy(file, Path.Combine(destinationFolder, destinationFile) + ".json");
if(file == "metadata.xml")
File.Copy(file, Path.Combine(destinationFolder, destinationFile) + ".xml");
counter++;
}
zipCounter = 0;
zipCurrentEntryName = "";
zf.SaveProgress += Zf_SaveProgress;
if(UpdateProgress != null)
UpdateProgress(null, "Saving...", 0, 0);
zf.Save();
}
catch(Exception ex)
{
if(System.Diagnostics.Debugger.IsAttached)
throw;
if(Failed != null)
Failed(string.Format("Exception {0}\n{1}", ex.Message, ex.InnerException));
}
if(Finished != null)
Finished();
}
static void Zf_SaveProgress(object sender, SaveProgressEventArgs e)
{
if(e.CurrentEntry != null && e.CurrentEntry.FileName != zipCurrentEntryName)
{
zipCurrentEntryName = e.CurrentEntry.FileName;
zipCounter++;
}
if(UpdateProgress != null && e.CurrentEntry != null)
UpdateProgress("Compressing...", e.CurrentEntry.FileName, zipCounter, e.EntriesTotal);
if(UpdateProgress2 != null)
UpdateProgress2(string.Format("{0:P}", e.BytesTransferred / (double)e.TotalBytesToTransfer),
string.Format("{0} / {1}", e.BytesTransferred, e.TotalBytesToTransfer),
e.BytesTransferred, e.TotalBytesToTransfer);
System.Console.WriteLine("{0}", e.EventType);
if(e.EventType == ZipProgressEventType.Error_Saving && Failed != null)
Failed("Failed compression");
if(e.EventType == ZipProgressEventType.Saving_Completed && FinishedWithText != null)
FinishedWithText(e.ArchiveName);
}
}
}

View File

@@ -38,13 +38,30 @@ public partial class MainWindow : Window
Thread thdHashFiles;
Thread thdCheckFiles;
Thread thdAddFiles;
Thread thdPackFiles;
bool stopped;
ListStore view;
public MainWindow() : base(WindowType.Toplevel)
{
Build();
Core.InitDB();
CellRendererText filenameCell = new CellRendererText();
CellRendererText hashCell = new CellRendererText();
CellRendererToggle dbCell = new CellRendererToggle();
TreeViewColumn filenameColumn = new TreeViewColumn("Path", filenameCell, "text", 0, "background", 3, "foreground", 4);
TreeViewColumn hashColumn = new TreeViewColumn("SHA256", hashCell, "text", 1, "background", 3, "foreground", 4);
TreeViewColumn dbColumn = new TreeViewColumn("Known?", dbCell, "active", 2);
view = new ListStore(typeof(string), typeof(string), typeof(bool), typeof(string), typeof(string));
treeFiles.Model = view;
treeFiles.AppendColumn(filenameColumn);
treeFiles.AppendColumn(hashColumn);
treeFiles.AppendColumn(dbColumn);
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
@@ -194,21 +211,6 @@ public partial class MainWindow : Window
prgProgress.Visible = true;
CellRendererText filenameCell = new CellRendererText();
CellRendererText hashCell = new CellRendererText();
CellRendererToggle dbCell = new CellRendererToggle();
TreeViewColumn filenameColumn = new TreeViewColumn("Path", filenameCell, "text", 0, "background", 3, "foreground", 4);
TreeViewColumn hashColumn = new TreeViewColumn("SHA256", hashCell, "text", 1, "background", 3, "foreground", 4);
TreeViewColumn dbColumn = new TreeViewColumn("Known?", dbCell, "active", 2);
view = new ListStore(typeof(string), typeof(string), typeof(bool), typeof(string), typeof(string));
treeFiles.Model = view;
treeFiles.AppendColumn(filenameColumn);
treeFiles.AppendColumn(hashColumn);
treeFiles.AppendColumn(dbColumn);
thdCheckFiles = new Thread(Core.CheckDbForFiles);
Core.Failed += ChkFilesFailed;
Core.Finished += ChkFilesFinished;
@@ -241,11 +243,7 @@ public partial class MainWindow : Window
Core.AddEntry -= AddFile;
thdHashFiles = null;
if(view != null)
{
view.Clear();
treeFiles.Model = null;
view = null;
}
});
}
@@ -269,6 +267,7 @@ public partial class MainWindow : Window
btnSettings.Sensitive = true;
btnAdd.Visible = true;
btnPack.Visible = true;
btnPack.Sensitive = true;
txtFormat.IsEditable = true;
txtMachine.IsEditable = true;
@@ -317,11 +316,7 @@ public partial class MainWindow : Window
btnPack.Visible = false;
btnClose.Visible = false;
if(view != null)
{
view.Clear();
treeFiles.Model = null;
view = null;
}
txtFormat.IsEditable = false;
txtMachine.IsEditable = false;
txtProduct.IsEditable = false;
@@ -425,11 +420,7 @@ public partial class MainWindow : Window
Core.Finished -= FindFilesFinished;
btnStop.Visible = false;
if(view != null)
{
view.Clear();
treeFiles.Model = null;
view = null;
}
}
protected void OnBtnAddClicked(object sender, EventArgs e)
@@ -472,8 +463,8 @@ public partial class MainWindow : Window
MainClass.dbInfo.update = chkUpdate.Active;
MainClass.dbInfo.upgrade = chkUpgrade.Active;
thdHashFiles = new Thread(Core.AddFilesToDb);
thdHashFiles.Start();
thdAddFiles = new Thread(Core.AddFilesToDb);
thdAddFiles.Start();
}
public void AddFilesToDbFinished()
@@ -529,4 +520,120 @@ public partial class MainWindow : Window
frmSettings _frmSettings = new frmSettings();
_frmSettings.Show();
}
protected void OnBtnPackClicked(object sender, EventArgs e)
{
btnAdd.Sensitive = false;
btnPack.Sensitive = false;
btnClose.Sensitive = false;
prgProgress.Visible = true;
prgProgress2.Visible = true;
lblProgress.Visible = true;
lblProgress2.Visible = true;
txtFormat.IsEditable = false;
txtMachine.IsEditable = false;
txtProduct.IsEditable = false;
txtVersion.IsEditable = false;
txtLanguages.IsEditable = false;
txtDeveloper.IsEditable = false;
txtDescription.IsEditable = false;
txtArchitecture.IsEditable = false;
chkOem.Sensitive = false;
chkFiles.Sensitive = false;
chkUpdate.Sensitive = false;
chkUpgrade.Sensitive = false;
chkNetinstall.Sensitive = false;
chkSource.Sensitive = false;
Core.UpdateProgress += UpdateProgress;
Core.UpdateProgress2 += UpdateProgress2;
Core.FinishedWithText += PackFilesFinished;
Core.Failed += PackFilesFailed;
MainClass.dbInfo.architecture = txtArchitecture.Text;
MainClass.dbInfo.description = txtDescription.Text;
MainClass.dbInfo.developer = txtDeveloper.Text;
MainClass.dbInfo.format = txtFormat.Text;
MainClass.dbInfo.languages = txtLanguages.Text;
MainClass.dbInfo.machine = txtMachine.Text;
MainClass.dbInfo.product = txtProduct.Text;
MainClass.dbInfo.version = txtVersion.Text;
MainClass.dbInfo.files = chkFiles.Active;
MainClass.dbInfo.netinstall = chkNetinstall.Active;
MainClass.dbInfo.oem = chkOem.Active;
MainClass.dbInfo.source = chkSource.Active;
MainClass.dbInfo.update = chkUpdate.Active;
MainClass.dbInfo.upgrade = chkUpgrade.Active;
thdPackFiles = new Thread(Core.CompressFiles);
thdPackFiles.Start();
}
public void PackFilesFinished(string text)
{
Application.Invoke(delegate
{
if(thdPackFiles != null)
thdPackFiles.Abort();
Core.UpdateProgress -= UpdateProgress;
Core.UpdateProgress2 -= UpdateProgress2;
Core.FinishedWithText -= PackFilesFinished;
Core.Failed -= PackFilesFailed;
prgProgress.Visible = false;
prgProgress2.Visible = false;
lblProgress.Visible = false;
lblProgress2.Visible = false;
btnPack.Sensitive = false;
btnClose.Sensitive = true;
MessageDialog dlgMsg = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "Correctly packed to " + text);
dlgMsg.Run();
dlgMsg.Destroy();
});
}
public void PackFilesFailed(string text)
{
Application.Invoke(delegate
{
if(!stopped)
{
MessageDialog dlgMsg = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, text);
dlgMsg.Run();
dlgMsg.Destroy();
}
if(thdPackFiles != null)
thdPackFiles.Abort();
Core.UpdateProgress -= UpdateProgress;
Core.UpdateProgress2 -= UpdateProgress2;
Core.FinishedWithText -= PackFilesFinished;
Core.Failed -= PackFilesFailed;
btnAdd.Sensitive = true;
btnPack.Sensitive = true;
btnClose.Sensitive = true;
prgProgress.Visible = false;
prgProgress2.Visible = false;
lblProgress.Visible = false;
lblProgress2.Visible = false;
txtFormat.IsEditable = true;
txtMachine.IsEditable = true;
txtProduct.IsEditable = true;
txtVersion.IsEditable = true;
txtLanguages.IsEditable = true;
txtDeveloper.IsEditable = true;
txtDescription.IsEditable = true;
txtArchitecture.IsEditable = true;
chkOem.Sensitive = true;
chkFiles.Sensitive = true;
chkUpdate.Sensitive = true;
chkUpgrade.Sensitive = true;
chkNetinstall.Sensitive = true;
chkSource.Sensitive = true;
});
}
}

View File

@@ -112,7 +112,7 @@ public partial class MainWindow
global::Stetic.Gui.Initialize(this);
// Widget MainWindow
this.Name = "MainWindow";
this.Title = global::Mono.Unix.Catalog.GetString("MainWindow");
this.Title = global::Mono.Unix.Catalog.GetString("OS Repository DB Manager");
this.WindowPosition = ((global::Gtk.WindowPosition)(4));
// Container child MainWindow.Gtk.Container+ContainerChild
this.vbox1 = new global::Gtk.VBox();
@@ -557,7 +557,6 @@ public partial class MainWindow
w47.Fill = false;
// Container child hbox1.Gtk.Box+BoxChild
this.btnPack = new global::Gtk.Button();
this.btnPack.Sensitive = false;
this.btnPack.CanFocus = true;
this.btnPack.Name = "btnPack";
this.btnPack.UseUnderline = true;
@@ -654,6 +653,7 @@ public partial class MainWindow
this.btnStop.Clicked += new global::System.EventHandler(this.OnBtnStopClicked);
this.btnFolder.Clicked += new global::System.EventHandler(this.OnBtnFolderClicked);
this.btnAdd.Clicked += new global::System.EventHandler(this.OnBtnAddClicked);
this.btnPack.Clicked += new global::System.EventHandler(this.OnBtnPackClicked);
this.btnClose.Clicked += new global::System.EventHandler(this.OnBtnCloseClicked);
this.btnHelp.Clicked += new global::System.EventHandler(this.OnBtnHelpClicked);
this.btnSettings.Clicked += new global::System.EventHandler(this.OnBtnSettingsClicked);

View File

@@ -9,7 +9,7 @@
</import>
<widget class="Gtk.Window" id="MainWindow" design-size="771 544">
<property name="MemberName" />
<property name="Title" translatable="yes">MainWindow</property>
<property name="Title" translatable="yes">OS Repository DB Manager</property>
<property name="WindowPosition">CenterOnParent</property>
<signal name="DeleteEvent" handler="OnDeleteEvent" />
<child>
@@ -606,12 +606,12 @@
<widget class="Gtk.Button" id="btnPack">
<property name="MemberName" />
<property name="Visible">False</property>
<property name="Sensitive">False</property>
<property name="CanFocus">True</property>
<property name="Type">TextAndIcon</property>
<property name="Icon">stock:gtk-save Menu</property>
<property name="Label" translatable="yes">_Pack...</property>
<property name="UseUnderline">True</property>
<signal name="Clicked" handler="OnBtnPackClicked" />
</widget>
<packing>
<property name="PackType">End</property>

View File

@@ -56,6 +56,9 @@
<Reference Include="plist-cil">
<HintPath>..\packages\plist-cil.1.15.0\lib\net40\plist-cil.dll</HintPath>
</Reference>
<Reference Include="DotNetZip">
<HintPath>..\packages\DotNetZip.1.10.1\lib\net20\DotNetZip.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="gtk-gui\gui.stetic">

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetZip" version="1.10.1" targetFramework="net45" />
<package id="plist-cil" version="1.15.0" targetFramework="net45" />
<package id="System.Data.SQLite.Core" version="1.0.105.0" targetFramework="net45" />
</packages>