2009-11-08 Chris Toshok <toshok@ximian.com>
[moon.git] / tools / munxap / munxap.cs
blobbc87a8b4037d96f7550d3e9b22d990bf35e2cdf8
1 /*
2 * munxap.cs.
4 * Contact:
5 * Moonlight List (moonlight-list@lists.ximian.com)
7 * Copyright 2009 Novell, Inc. (http://www.novell.com)
9 * See the LICENSE file included with the distribution for details.
13 using NDesk.Options;
14 using GLib;
15 using Gtk;
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Diagnostics;
21 using System.IO;
22 using System.Resources;
23 using System.Text;
25 public class Munxap
27 Window window;
28 VBox main;
29 VBox left;
30 HBox xap;
31 Button close_button;
32 Label file_label;
33 TreeView xap_file_view;
34 ListStore xap_file_store;
35 ScrolledWindow xap_file_scrollable;
36 Zip zip;
38 public bool Help { get; private set; }
39 public string File { get; private set; }
41 static void ShowHelp (OptionSet o)
43 Console.WriteLine ("Usage is: munxap [args] file.xap\n" +
44 "Arguments are:");
45 o.WriteOptionDescriptions (Console.Out);
48 static int Main (string [] args)
50 try {
51 Munxap unxap = new Munxap ();
53 var p = new OptionSet () {
54 { "h|?|help", v => unxap.Help = v != null },
57 List<string> rest = new List<string> ();
58 try {
59 rest = p.Parse (args);
60 } catch (OptionException){
61 Console.Error.WriteLine ("Try `munxap --help' for more information.");
62 return 1;
65 if (unxap.Help) {
66 ShowHelp (p);
67 return 0;
70 if (rest.Count != 1) {
71 ShowHelp (p);
72 return 1;
75 unxap.File = rest [0];
77 unxap.Show ();
79 return 0;
80 } catch (Exception ex) {
81 Console.WriteLine (ex);
82 return 1;
86 void Show ()
88 zip = new Zip (File);
90 Application.Init ();
92 // Main window
93 window = new Window ("munxap: " + File);
94 window.Resize (600, 600);
95 window.DeleteEvent += delegate (object obj, DeleteEventArgs args)
97 Close ();
100 // Main vbox
101 main = new VBox (false, 10);
102 window.Add (main);
104 // label with the filename of the xap file on top
105 file_label = new Label (File);
106 main.PackStart (file_label, false, true, 0);
108 // the middle consists of a hbox, leftmost column a list of files in the zip file
109 xap = new HBox (false, 10);
110 main.PackStart (xap, true, true, 0);
112 left = new VBox (false, 10);
113 xap.PackStart (left, true, true, 0);
115 // a list of files in the zip file
116 xap_file_store = new ListStore (typeof (String), typeof (String), typeof (ZipContent));
117 xap_file_view = new TreeView ();
118 xap_file_view.Model = xap_file_store;
119 xap_file_view.HeadersVisible = true;
120 xap_file_view.AppendColumn ("Name", new CellRendererText (), "text", 0);
121 xap_file_view.AppendColumn ("Type", new CellRendererText (), "text", 1);
122 xap_file_view.CursorChanged += HandleCursorChanged;
123 xap_file_scrollable = new ScrolledWindow ();
124 xap_file_scrollable.Add (xap_file_view);
125 left.PackStart (xap_file_scrollable, true, true, 0);
127 // close button at the bottom
128 close_button = new Button ("Close");
129 close_button.Clicked += delegate (object obj, EventArgs args)
131 Close ();
133 main.PackEnd (close_button, false, true, 0);
135 // Load zip contents
136 foreach (ZipContent f in zip.Files) {
137 xap_file_store.AppendValues (f.Filename, f.Type, f);
141 window.ShowAll ();
143 Application.Run ();
146 void Close ()
148 zip.Dispose ();
149 Application.Quit ();
152 void HandleCursorChanged (object sender, EventArgs e)
154 TreeSelection selection = xap_file_view.Selection;
155 TreeModel model;
156 TreeIter iter;
157 Value v;
158 ZipContent content;
159 View view = null;
161 // Find the selected column, and create a view for it
163 if (!selection.GetSelected (out model, out iter))
164 return;
166 v = new Value ();
167 xap_file_store.GetValue (iter, 2, ref v);
168 content = v.Val as ZipContent;
170 if (content == null) {
171 Console.WriteLine ("No content here.");
172 return;
175 switch (content.Type) {
176 case ".xaml":
177 case ".xml":
178 view = new ViewXaml (content);
179 break;
180 case ".dll":
181 view = new ViewAssembly (content);
182 break;
183 case ".mdb":
184 case ".pdb":
185 break; // Ignore
186 default:
187 Console.WriteLine ("Unknown type: {0}");
188 break;
191 // Remove any previous views
192 while (left.Children.Length > 1)
193 left.Remove (left.Children [1]);
195 // Add the current one, if any
196 if (view != null) {
197 left.PackStart (view.GetView (), true, true, 0);
198 left.ShowAll ();