Move file handling to a different file

This commit is contained in:
Latif Khalifa
2013-12-04 02:10:18 +01:00
parent 809c84f29b
commit c30624a1e1
3 changed files with 99 additions and 90 deletions

View File

@@ -4,11 +4,7 @@ using System.Collections.Concurrent;
using Gtk;
using GridProxyGUI;
using OpenMetaverse.Packets;
using OpenMetaverse.StructuredData;
using System.IO;
using System.IO.Compression;
using System.Timers;
using System.Text;
using System.Text.RegularExpressions;
using Nwc.XmlRpc;
@@ -649,8 +645,6 @@ public partial class MainWindow : Gtk.Window
od.Destroy();
}
string sessionFileName;
protected void OnSaveAsActionActivated(object sender, EventArgs e)
{
var od = new Gtk.FileChooserDialog(null, "Save Session", this, FileChooserAction.Save, "Cancel", ResponseType.Cancel, "Save", ResponseType.Accept);
@@ -658,10 +652,10 @@ public partial class MainWindow : Gtk.Window
if (od.Run() == (int)ResponseType.Accept)
{
sessionFileName = od.Filename;
if (string.IsNullOrEmpty(System.IO.Path.GetExtension(sessionFileName)))
SessionFileName = od.Filename;
if (string.IsNullOrEmpty(System.IO.Path.GetExtension(SessionFileName)))
{
sessionFileName += ".gpz";
SessionFileName += ".gpz";
}
SaveSession();
}
@@ -670,7 +664,7 @@ public partial class MainWindow : Gtk.Window
protected void OnSaveActionActivated(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(sessionFileName))
if (string.IsNullOrEmpty(SessionFileName))
{
OnSaveAsActionActivated(sender, e);
}
@@ -680,86 +674,6 @@ public partial class MainWindow : Gtk.Window
}
}
void SaveSession()
{
OSDMap s = new OSDMap();
OSDArray array = new OSDArray();
foreach (object[] row in messages.Messages)
{
array.Add(((Session)row[0]).Serialize());
}
s["Version"] = "1.0";
s["Messages"] = array;
System.Threading.ThreadPool.QueueUserWorkItem((sync) =>
{
try
{
using (var file = File.OpenWrite(sessionFileName))
{
using (var compressed = new GZipStream(file, CompressionMode.Compress))
{
using (var writer = new System.Xml.XmlTextWriter(compressed, new UTF8Encoding(false)))
{
writer.Formatting = System.Xml.Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement(String.Empty, "llsd", String.Empty);
OSDParser.SerializeLLSDXmlElement(writer, s);
writer.WriteEndElement();
}
}
}
}
catch { }
});
}
void OpenSession(string fileName)
{
System.Threading.ThreadPool.QueueUserWorkItem((sync) =>
{
OSD data;
try
{
using (var file = File.OpenRead(fileName))
{
using (var compressed = new GZipStream(file, CompressionMode.Decompress))
{
data = OSDParser.DeserializeLLSDXml(compressed);
}
}
}
catch
{
return;
}
Application.Invoke((sender, e) =>
{
try
{
if (data != null && data is OSDMap)
{
OSDMap map = (OSDMap)data;
OSDArray msgs = (OSDArray)map["Messages"];
foreach (var msgOSD in msgs)
{
var msg = (OSDMap)msgOSD;
var session = Session.FromOSD(msg);
if (session != null)
{
messages.Messages.AppendValues(session);
}
}
}
}
catch { }
});
});
}
protected void OnAboutActionActivated(object sender, EventArgs e)
{
var about = new GridProxyGUI.About();