view FrmExportXml.cs @ 167:9808adf2d566

Re #419: Remove assumptions of a file-based install * Update GTK# GUI to use Streams version of XSLT export
author IBBoard <dev@ibboard.co.uk>
date Wed, 28 Nov 2012 20:25:53 +0000
parents 0d8004d6a4e5
children
line source
1 // This file (FrmExportXml.cs) is a part of the IBBoard.WarFoundry.GUI.GTK project and is copyright 2011 IBBoard
2 //
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
4 using System;
5 using System.IO;
6 using IBBoard.GtkSharp;
7 using System.Collections.Generic;
8 using Gtk;
9 using IBBoard.WarFoundry.API.Objects;
10 using System.Xml.Xsl;
11 using IBBoard.Lang;
12 using IBBoard.WarFoundry.API.Exporters;
14 namespace IBBoard.WarFoundry.GUI.GTK
15 {
16 public partial class FrmExportXml : FileChooserDialog
17 {
18 private Army army;
19 private TransformXmlWidget transformWidget;
20 private Button buttonOk;
22 public FrmExportXml(Army army) : base("", null, FileChooserAction.Save)
23 {
24 this.army = army;
25 AddButton(Gtk.Stock.Cancel, ResponseType.Cancel);
26 buttonOk = (Button)AddButton(Gtk.Stock.Save, ResponseType.Ok);
27 buttonOk.Clicked+= OnButtonOkClicked;
28 transformWidget = new TransformXmlWidget();
29 transformWidget.TransformChanged += HandleTransformWidgetTransformChanged;
30 ExtraWidget = transformWidget;
31 SetFilename(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "export.xml"));
32 SelectionChanged += OnSaveAsPathSelectionChanged;
33 }
35 private void HandleTransformWidgetTransformChanged (object sender, EventArgs e)
36 {
37 SetOkayButtonSensitive();
38 }
40 private bool IsValid
41 {
42 get
43 {
44 return CheckFileCanExist() && transformWidget.IsValid;
45 }
46 }
48 private bool CheckFileCanExist()
49 {
50 bool canExist = false;
52 if (!String.IsNullOrEmpty(Filename))
53 {
54 FileInfo file = new FileInfo(Filename);
55 canExist = file.Directory.Exists;
56 }
58 return canExist;
59 }
61 private void SetOkayButtonSensitive()
62 {
63 //TODO: It would be nice to disable save when appropriate options aren't set,
64 //but we don't seem to get change notifications early enough
65 //buttonOk.Sensitive = IsValid();
66 }
68 protected void OnSaveAsPathSelectionChanged(object sender, System.EventArgs e)
69 {
70 SetOkayButtonSensitive();
71 }
73 protected void OnButtonOkClicked (object sender, System.EventArgs e)
74 {
75 if (IsValid)
76 {
77 DoExport();
78 }
79 }
81 void DoExport()
82 {
83 string errorMessage = "";
84 // Catch potential errors with the file export or XSL compiliation
85 try
86 {
87 string fileName = Filename;
89 if (transformWidget.TransformEnabled)
90 {
91 using (Stream stream = transformWidget.GetXsltStream())
92 {
93 WarFoundryXmlWithXslExporter.GetDefault().ExportArmyWithTransform(army, fileName, stream);
94 }
95 }
96 else
97 {
98 WarFoundryXmlWithXslExporter.GetDefault().ExportArmy(army, fileName);
99 }
100 }
101 catch (XsltCompileException ex)
102 {
103 errorMessage = Translation.GetTranslation("mbErrorCompileFailed", "") + ":\n" + ex.Message;
104 }
105 catch (XsltException ex)
106 {
108 errorMessage = Translation.GetTranslation("mbErrorXSLTFailed", "") + ":\n" + ex.Message;
109 }
110 catch (FileNotFoundException ex)
111 {
112 errorMessage = Translation.GetTranslation("mbErrorFileNotFoundFailed", "") + ":\n" + ex.Message;
113 }
114 catch (IOException ex)
115 {
116 errorMessage = Translation.GetTranslation("mbErrorIOFailed", "") + ":\n" + ex.Message;
117 }
118 catch (Exception ex)
119 {
120 errorMessage = Translation.GetTranslation("mbErrorFailed", "") + ":\n" + ex.Message;
121 }
123 if (errorMessage != "")
124 {
125 MessageDialog dialog = new MessageDialog(this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, errorMessage);
126 dialog.Run();
127 }
129 Respond(ResponseType.Ok);
130 }
131 }
132 }