Initial commit
[fillmore] / src / marina / MultiFileProgress.vala
1 /* Copyright 2009 Yorba Foundation
2  *
3  * This software is licensed under the GNU Lesser General Public License
4  * (version 2.1 or later).  See the COPYING file in this distribution. 
5  */
6
7 using Logging;
8
9 public interface MultiFileProgressInterface : Object {
10     public signal void fraction_updated(double d);
11     public signal void file_updated(string filename, int index);
12     public signal void done();
13     
14     public abstract void cancel();
15     public abstract void complete();
16 }
17
18 public class MultiFileProgress : Gtk.Window {
19     Gtk.ProgressBar progress_bar;
20     Gtk.Label file_label;
21     Gtk.Label number_label;
22     Gtk.Button cancel_button;
23     int num_files;
24     bool needs_show;
25     
26     MultiFileProgressInterface provider_interface;
27   
28     string dialog_title;
29
30     public MultiFileProgress(Gtk.Window parent, int num_files, 
31         string dialog_title, MultiFileProgressInterface provider) {
32         this.num_files = num_files;
33         needs_show = true;
34         file_label = new Gtk.Label("");
35         number_label = new Gtk.Label("");
36         progress_bar = new Gtk.ProgressBar();
37         
38         Gtk.VBox vbox = new Gtk.VBox(true, 0);
39         
40         vbox.pack_start(number_label, false, false, 0);
41         vbox.pack_start(file_label, false, false, 0);
42         vbox.pack_start(progress_bar, false, false, 0);
43         
44         Gtk.HButtonBox button_area = new Gtk.HButtonBox();
45         button_area.set("layout-style", Gtk.ButtonBoxStyle.CENTER); 
46         
47         cancel_button = new Gtk.Button.from_stock(Gtk.STOCK_CANCEL);
48         cancel_button.clicked.connect(on_cancel_clicked);
49         
50         button_area.add(cancel_button);
51         
52         vbox.pack_start(button_area, false, false, 0);
53         add(vbox);
54
55         set_transient_for(parent);
56         set_title(dialog_title);
57         this.dialog_title = dialog_title;
58         
59         provider_interface = provider;
60         
61         provider_interface.fraction_updated.connect(on_fraction_updated);
62         provider_interface.file_updated.connect(on_file_updated);
63         provider_interface.done.connect(on_done);
64     }
65
66     void on_done() {
67         emit(this, Facility.SIGNAL_HANDLERS, Level.INFO, "on_done");
68         destroy();
69     }
70     
71     void on_cancel_clicked() {
72         emit(this, Facility.SIGNAL_HANDLERS, Level.INFO, "on_cancel_clicked");
73         destroy();
74     }
75     
76     void on_destroy() {
77         emit(this, Facility.SIGNAL_HANDLERS, Level.INFO, "on_destroy");
78         if (progress_bar.get_fraction() < 1.0)
79             provider_interface.cancel();
80         else
81             provider_interface.complete();
82     }
83     
84     void on_fraction_updated(double d) {
85         emit(this, Facility.SIGNAL_HANDLERS, Level.INFO, "on_fraction_updated");
86         if (needs_show) {
87             needs_show = false;
88             set_border_width(8);
89             set_resizable(false);
90             set_modal(true);
91             
92             destroy.connect(on_destroy);
93             
94             show_all();
95         }
96
97         progress_bar.set_fraction(d);
98         
99         if (progress_bar.get_fraction() == 1.0)
100             destroy();
101         else {
102             show_all();
103             queue_draw();
104         }
105     }
106
107     void on_file_updated(string filename, int index) {
108         emit(this, Facility.SIGNAL_HANDLERS, Level.INFO, "on_file_updated");
109         number_label.set_text("%sing %d of %d".printf(dialog_title, index + 1, num_files));
110         file_label.set_text("%s".printf(filename));
111     }
112 }