File: UploadFileChooserUI.cs

package info (click to toggle)
dfo 0.8%2Bsvn52-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 916 kB
  • sloc: cs: 6,025; makefile: 16; sh: 15
file content (215 lines) | stat: -rw-r--r-- 7,597 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

using System;
using System.Threading;
using System.IO;
using Gtk;
using Glade;

	public class UploadFileChooserUI
	{
    [Glade.Widget]
    FileChooserDialog filechooserdialog1;
    
    [Glade.Widget]
    Image image6;
    
    [Glade.Widget]
    Button button10;
    
    [Glade.Widget]
    Button button11;
    
    [Glade.Widget]
    EventBox eventbox7;
    
    [Glade.Widget]
    EventBox eventbox8;
    
    [Glade.Widget]
    EventBox eventbox9;
    
    [Glade.Widget]
    Label label16;
    
    [Glade.Widget]
    ProgressBar progressbar3;
    
    // private static UploadFileChooserUI uploader;
    private ThreadStart _job;
    private Thread _previewthread;
    private Gdk.Pixbuf _buf;
    private Thread _processfilesthread;
    
    //private static Gdk.Color bgcolor = new Gdk.Color(0xA8, 0xA8, 0x94);
    private static Gdk.Color bgcolor = new Gdk.Color(0xFF, 0xFF, 0xFF);
    
		private UploadFileChooserUI()
		{
		  Glade.XML gxml = new Glade.XML (null, "organizer.glade", "filechooserdialog1", null);
		  gxml.Autoconnect (this);
		  _job = new ThreadStart(ProcessThumbnail);
		  _previewthread = new Thread(_job);
		  
		  label16.WidthRequest = eventbox7.WidthRequest;
		  
		  eventbox7.ModifyBg(Gtk.StateType.Normal, bgcolor);
		  eventbox8.ModifyBg(Gtk.StateType.Normal, bgcolor);
		  eventbox9.ModifyBg(Gtk.StateType.Normal, bgcolor);
		  
		  filechooserdialog1.Title = "Select files to upload";
		  filechooserdialog1.SetIconFromFile(DeskFlickrUI.ICON_PATH);
		  filechooserdialog1.SetFilename(PersistentInformation.GetInstance().UploadFilename);
      filechooserdialog1.SelectMultiple = true;
      
      FileFilter imagefilter = new FileFilter();
      imagefilter.AddMimeType("image/jpeg");
      imagefilter.AddMimeType("image/png");
      filechooserdialog1.Filter = imagefilter;
      
      filechooserdialog1.SelectionChanged += new EventHandler(OnFileSelectedChanged);
      filechooserdialog1.FileActivated += new EventHandler(OnOpenButtonClicked);
		  button10.Clicked += new EventHandler(OnOpenButtonClicked);
		  button11.Clicked += new EventHandler(OnCancelButtonClicked);
		  DeskFlickrUI.GetInstance().SetUploadWindow(false);
      filechooserdialog1.ShowAll();
		}
		
		public static void FireUp() {
      new UploadFileChooserUI();
		}
		
		private void OnFileSelectedChanged(object sender, EventArgs args) {
		  if (_processfilesthread != null && _processfilesthread.IsAlive) return;
		  _previewthread.Abort();
		  _previewthread = new Thread(_job);
		  _previewthread.Start();
		}
		
		private string GetInfo(string filename) {
		  if (System.IO.Directory.Exists(filename)) {
		    return "<span font_desc='Times Bold 10'>Directory</span>";
		  }
		  FileInfo finfo = new FileInfo(filename);
		  if (!finfo.Exists) {
		    return "<span font_desc='Times Bold 10'>Unable to process file " + finfo.Name + "</span>";
		  }
		  System.Text.StringBuilder strb = new System.Text.StringBuilder(); 
		  strb.AppendLine("<span font_desc='Times Bold 10'>" + finfo.Name + "</span>");
		  string sizestr;
		  long size = finfo.Length;
		  if (size < 1024) {
		    sizestr = size + " bytes";
		  } else if (size < 1024*1024) {
		    sizestr = String.Format("{0:###.##} KB", ((float) size)/1024);
		  } else {
		    sizestr = String.Format("{0:###.##} MB", ((float) size)/(1024*1024));
		  }
		  strb.AppendLine("<span font_desc='Times 10'>" + sizestr + "</span>");
		  return strb.ToString();
		}
		
		// This method is run through a thread.
		private void ProcessThumbnail() {
		  Gtk.Application.Invoke(delegate{
		    progressbar3.Text = "Processing...";
		    //progressbar3.PulseStep = 0.3;
		    //progressbar3.Pulse();
		  });
		  Thread.Sleep(700); // wait for 0.7 seconds. This way, if the user gets
		  // jumpy and selects a lot of files quickly, the thread would be
		  // interrupted before it reaches the processing of file stage, hence
		  // saving processing power and RAM consumption.
		  string[] filenames = filechooserdialog1.Filenames;
		  if (filenames == null || filenames.Length == 0) return;
		  string filename = filenames[filenames.Length - 1];
		  
		  if (System.IO.Directory.Exists(filename)) {
		    _buf = DeskFlickrUI.GetInstance().GetDFOThumbnail();
		  }
		  else {
  		  try {
  		    // Scalesimple creates a new pixbuf buffer, with the scaled
  		    // version of the image. If we do, _buf = _buf.ScaleSimple, the
  		    // originally loaded buffer remains referenced, and hence,
  		    // causes memory leak. Instead, we use a different buffer to load
  		    // file, and then dispose it once the image is scaled.
  		    Gdk.Pixbuf original = new Gdk.Pixbuf(filename);
    		  _buf = original.ScaleSimple(150, 150, Gdk.InterpType.Bilinear);
    		  original.Dispose();
    		} catch (GLib.GException) {
    		  _buf = DeskFlickrUI.GetInstance().GetDFOThumbnail();
    		}
  		}
		  Gtk.Application.Invoke (delegate {
  		  image6.Pixbuf = _buf;
  		  label16.Markup = GetInfo(filename);
  		  progressbar3.Text = "";
		  });
		}
		
		// To be run in a thread by OnOpenButtonClicked method.
		private void ProcessFiles() {
		  string[] filenames = null;
		  filenames = filechooserdialog1.Filenames;
		  if (filenames == null) return;
      PersistentInformation.GetInstance().UploadFilename = filenames[0];
      Gdk.Pixbuf thumbnail;
      Gdk.Pixbuf smallimage;
      Gdk.Pixbuf sqimage;
      Gtk.Application.Invoke(delegate{
        progressbar3.Adjustment.Lower = 0;
        progressbar3.Adjustment.Upper = filenames.Length;
        progressbar3.Adjustment.Value = 0;
        progressbar3.Text = "Processing files...";
        button10.Sensitive = false;
      });
      foreach (string filename in filenames) {
        Gtk.Application.Invoke(delegate{
          progressbar3.Adjustment.Value += 1;
        });
        try {
          Gdk.Pixbuf buf = new Gdk.Pixbuf(filename);
          thumbnail = buf.ScaleSimple(75, 75, Gdk.InterpType.Bilinear);
          smallimage = buf.ScaleSimple(240, 180, Gdk.InterpType.Bilinear);
          sqimage = buf.ScaleSimple(150, 150, Gdk.InterpType.Bilinear);
          buf.Dispose();
        } catch (GLib.GException) {
          continue;
          // Couldn't process the file.
        }
        Gtk.Application.Invoke(delegate{
          label16.Markup = GetInfo(filename);
          image6.Pixbuf = sqimage;
        });
        PersistentInformation.GetInstance().SetThumbnail(filename, thumbnail);
        thumbnail.Dispose();
        PersistentInformation.GetInstance().SetSmallImage(filename, smallimage);
        smallimage.Dispose();
        PersistentInformation.GetInstance().InsertEntryToUpload(filename);
      }
      Gtk.Application.Invoke(delegate{
        DeskFlickrUI.GetInstance().UpdateToolBarButtons();
        DeskFlickrUI.GetInstance().RefreshUploadPhotos();
        filechooserdialog1.Destroy();
        DeskFlickrUI.GetInstance().SetUploadWindow(true);
      });
		}
		
		private void OnOpenButtonClicked(object sender, EventArgs args) {
		  if (_processfilesthread != null && _processfilesthread.IsAlive) return; 
		  _previewthread.Abort();
      ThreadStart job = new ThreadStart(ProcessFiles);
      _processfilesthread = new Thread(job);
      _processfilesthread.Start();
		}
		
		private void OnCancelButtonClicked(object sender, EventArgs args) {
		  if (_processfilesthread != null) {
		    _processfilesthread.Abort();
		    DeskFlickrUI.GetInstance().UpdateToolBarButtons();
		    DeskFlickrUI.GetInstance().RefreshUploadPhotos();
		  }
		  filechooserdialog1.Destroy();
		  DeskFlickrUI.GetInstance().SetUploadWindow(true);
		}
	}