Fix installer script
[SnapShooter.git] / SnapShooter / MainWindow.xaml.cs
1 using System;
2 using System.IO;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Data;
9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 using System.Windows.Threading;
16 using Microsoft.WindowsAPICodePack.Taskbar;
17 using Microsoft.WindowsAPICodePack.Shell;
18 using DominionOfAwesome;
19
20 namespace SnapShooter {
21     /// <summary>
22     /// Interaction logic for MainWindow.xaml
23     /// </summary>
24     public partial class MainWindow : Window {
25         DispatcherTimer dt;
26         StockIcon foundstuff;
27
28         public MainWindow() {
29             InitializeComponent();
30             dt = new DispatcherTimer();
31             foundstuff = new StockIcon(StockIconIdentifier.ImageFiles, StockIconSizes.ShellSize, false, false);
32         }
33
34         private void Window_Loaded(object sender, RoutedEventArgs e) {
35             dt.Interval = new TimeSpan(0, 0, 1);
36             dt.Tick += new EventHandler(dt_Tick);
37             dt.Start();
38         }
39
40         void dt_Tick(object sender, EventArgs e) {
41             if (Clipboard.ContainsImage()) {
42                 TaskbarManager.Instance.SetOverlayIcon(foundstuff.Icon, "Found Image");
43
44                 string filename = System.IO.Path.Combine(
45                     Environment.GetEnvironmentVariable("TEMP"),
46                     "SnapShoot.jpg"
47                 );
48
49                 BitmapSource b = Clipboard.GetImage();
50                 System.Console.WriteLine(b.GetHashCode());
51                 FileStream s = new FileStream(filename, FileMode.Create);
52                 JpegBitmapEncoder j = new JpegBitmapEncoder();
53                 j.QualityLevel = 85;
54                 j.Frames.Add(BitmapFrame.Create(b));
55                 j.Save(s);
56                 s.Close();
57
58                 Transit t = new Transit();
59                 t.Create();
60                 t.TransferProgress += delegate(object sender2, TransferProgressArgs tpe) {
61                     TaskbarManager.Instance.SetProgressValue((int)tpe.PercentComplete, 100);
62                 };
63                 t.TransferComplete += delegate {
64                     TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
65                     Dispatcher.Invoke(new Action(delegate {
66                         File.Delete(filename);
67
68                         Clipboard.SetText(t.Url);
69
70                         SnapShotItem i = new SnapShotItem();
71                         i.TransitURL = t.Url;
72                         SnapShots.Items.Add(i);
73
74                         TaskbarManager.Instance.SetOverlayIcon(null, "");
75                     }), null);
76                 };
77                 t.UploadAsync(filename);
78
79                 Clipboard.Clear();
80             }
81         }
82
83         private void Enabled_Checked(object sender, RoutedEventArgs e) {
84             if (dt == null) return;
85
86             System.Console.WriteLine("Enabled");
87             dt.Start();
88         }
89
90         private void Enabled_Unchecked(object sender, RoutedEventArgs e) {
91             if (dt == null) return;
92
93             System.Console.WriteLine("Disabled");
94             dt.Stop();
95         }
96     }
97 }