~ubuntu-branches/ubuntu/quantal/shotwell/quantal

« back to all changes in this revision

Viewing changes to src/alien_db/AlienDatabaseImportDialog.vala

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2012-02-21 13:52:58 UTC
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: package-import@ubuntu.com-20120221135258-ao9jiib5qicomq7q
Tags: upstream-0.11.92
ImportĀ upstreamĀ versionĀ 0.11.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright 2009-2011 Yorba Foundation
2
 
 *
3
 
 * This software is licensed under the GNU LGPL (version 2.1 or later).
4
 
 * See the COPYING file in this distribution. 
5
 
 */
6
 
 
7
 
namespace AlienDb {
8
 
 
9
 
public class AlienDatabaseImportDialog : Gtk.Dialog {
10
 
    private static const int MSG_NOTEBOOK_PAGE_EMPTY = 0;
11
 
    private static const int MSG_NOTEBOOK_PAGE_PROGRESS = 1;
12
 
    private static const int MSG_NOTEBOOK_PAGE_ERROR = 2;
13
 
    
14
 
    private Gtk.Builder builder;
15
 
    
16
 
    private AlienDatabaseDriver driver;
17
 
    private DiscoveredAlienDatabase? selected_database = null;
18
 
    private File? selected_file = null;
19
 
    private Gtk.FileChooserButton file_chooser;
20
 
    private Gtk.RadioButton? file_chooser_radio;
21
 
    private Gtk.Notebook message_notebook;
22
 
    private Gtk.ProgressBar prepare_progress_bar;
23
 
    private Gtk.Label error_message_label;
24
 
    private unowned BatchImport.ImportReporter reporter;
25
 
    
26
 
    public void set_builder(Gtk.Builder builder) {
27
 
        this.builder = builder;
28
 
    }
29
 
    
30
 
    public void setup(string title, AlienDatabaseDriver driver,
31
 
        BatchImport.ImportReporter reporter) {
32
 
        set_title(title);
33
 
        set_parent_window(AppWindow.get_instance().get_parent_window());
34
 
        set_transient_for(AppWindow.get_instance());
35
 
        this.driver = driver;
36
 
        this.reporter = reporter;
37
 
        
38
 
        file_chooser = builder.get_object("db_filechooserbutton") as Gtk.FileChooserButton;
39
 
        message_notebook = builder.get_object("message_notebook") as Gtk.Notebook;
40
 
        message_notebook.set_current_page(MSG_NOTEBOOK_PAGE_EMPTY);
41
 
        prepare_progress_bar = builder.get_object("prepare_progress_bar") as Gtk.ProgressBar;
42
 
        error_message_label = builder.get_object("import_error_label") as Gtk.Label;
43
 
        
44
 
        Gtk.Box options_box = builder.get_object("options_box") as Gtk.Box;
45
 
        
46
 
        Gee.Collection<DiscoveredAlienDatabase> discovered_databases = driver.get_discovered_databases();
47
 
        if (discovered_databases.size > 0) {
48
 
            Gtk.RadioButton db_radio = null;
49
 
            foreach (DiscoveredAlienDatabase db in discovered_databases) {
50
 
                string db_radio_label =
51
 
                    _("Import from default %1$s library (%2$s)").printf(
52
 
                        driver.get_display_name(),
53
 
                        collapse_user_path(db.get_id().driver_specific_uri)
54
 
                    );
55
 
                db_radio = create_radio_button(options_box, db_radio, db, db_radio_label);
56
 
            }
57
 
            file_chooser_radio = create_radio_button(
58
 
                options_box, db_radio, null,
59
 
                _("Import from another %s database file:").printf(driver.get_display_name())
60
 
            );
61
 
        } else {
62
 
            Gtk.Label custom_file_label = new Gtk.Label(
63
 
                _("Import from a %s database file:").printf(driver.get_display_name())
64
 
            );
65
 
            options_box.pack_start(custom_file_label, true, true, 6);
66
 
        }
67
 
        set_ok_sensitivity();
68
 
    }
69
 
 
70
 
    public void execute() {
71
 
        show_all();
72
 
        
73
 
        bool is_finished = false;
74
 
        while (!is_finished) {
75
 
            if (run() == Gtk.ResponseType.OK)
76
 
                is_finished = execute_import();
77
 
            else
78
 
                is_finished = true;
79
 
        }
80
 
        
81
 
        destroy();
82
 
    }
83
 
    
84
 
    //
85
 
    // The bit that does all the work once the OK button has been clicked.
86
 
    //
87
 
    private bool execute_import() {
88
 
        bool result = false;
89
 
        AlienDatabase? alien_db = null;
90
 
        try {
91
 
            if (selected_database != null)
92
 
                alien_db = selected_database.get_database();
93
 
            else if (selected_file != null)
94
 
                alien_db = driver.open_database_from_file(selected_file);
95
 
            if (alien_db == null) {
96
 
                message_notebook.set_current_page(MSG_NOTEBOOK_PAGE_ERROR);
97
 
                error_message_label.set_label(_("No database selected"));
98
 
            } else {
99
 
                message_notebook.set_current_page(MSG_NOTEBOOK_PAGE_PROGRESS);
100
 
                prepare_progress_bar.set_fraction(0.0);
101
 
                set_response_sensitive(Gtk.ResponseType.OK, false);
102
 
                set_response_sensitive(Gtk.ResponseType.CANCEL, false);
103
 
                spin_event_loop();
104
 
                
105
 
                SortedList<AlienDatabaseImportJob> jobs =
106
 
                    new SortedList<AlienDatabaseImportJob>(import_job_comparator);
107
 
                Gee.ArrayList<AlienDatabaseImportJob> already_imported =
108
 
                    new Gee.ArrayList<AlienDatabaseImportJob>();
109
 
                Gee.ArrayList<AlienDatabaseImportJob> failed =
110
 
                    new Gee.ArrayList<AlienDatabaseImportJob>();
111
 
                
112
 
                Gee.Collection<AlienDatabasePhoto> photos = alien_db.get_photos();
113
 
                int photo_total = photos.size;
114
 
                int photo_idx = 0;
115
 
                foreach (AlienDatabasePhoto src_photo in photos) {
116
 
                    AlienDatabaseImportSource import_source = new AlienDatabaseImportSource(src_photo);
117
 
                    
118
 
                    if (import_source.is_already_imported()) {
119
 
                        message("Skipping import of %s: checksum detected in library", 
120
 
                            import_source.get_filename());
121
 
                        already_imported.add(new AlienDatabaseImportJob(import_source));
122
 
                        
123
 
                        continue;
124
 
                    }
125
 
                    
126
 
                    jobs.add(new AlienDatabaseImportJob(import_source));
127
 
                    photo_idx++;
128
 
                    prepare_progress_bar.set_fraction((double)photo_idx / (double)photo_total);
129
 
                    spin_event_loop();
130
 
                }
131
 
                
132
 
                // Go through the motions of importing even if the job size is
133
 
                // zero so that the reported function can display a message dialog
134
 
                // notifying the user that nothing was imported
135
 
                string db_name = _("%s Database").printf(alien_db.get_display_name());
136
 
                BatchImport batch_import = new BatchImport(jobs, db_name, reporter, failed,
137
 
                    already_imported);
138
 
                
139
 
                LibraryWindow.get_app().enqueue_batch_import(batch_import, true);
140
 
                // However, if there is really nothing to import, don't switch
141
 
                // to the import queue page so that the user is not faced with
142
 
                // an empty page
143
 
                if (jobs.size > 0)
144
 
                    LibraryWindow.get_app().switch_to_import_queue_page();
145
 
                // clean up
146
 
                if (selected_database != null) {
147
 
                    selected_database.release_database();
148
 
                    selected_database = null;
149
 
                }
150
 
                
151
 
                result = true;
152
 
            }
153
 
        } catch (Error e) {
154
 
            message_notebook.set_current_page(MSG_NOTEBOOK_PAGE_ERROR);
155
 
            error_message_label.set_label(_("Shotwell failed to load the database file"));
156
 
            // most failures should happen before the two buttons have been set
157
 
            // to the insensitive state but you never know so set them back to the
158
 
            // normal state so that the user can interact with them
159
 
            set_response_sensitive(Gtk.ResponseType.OK, true);
160
 
            set_response_sensitive(Gtk.ResponseType.CANCEL, true);
161
 
        }
162
 
        return result;
163
 
    }
164
 
    
165
 
    //
166
 
    // Signals
167
 
    //
168
 
    public void on_file_chooser_file_set() {
169
 
        selected_file = file_chooser.get_file();
170
 
        if (file_chooser_radio != null)
171
 
            file_chooser_radio.active = true;
172
 
        set_ok_sensitivity();
173
 
    }
174
 
    
175
 
    //
176
 
    // Private methods
177
 
    //
178
 
    private void set_ok_sensitivity() {
179
 
        set_response_sensitive(Gtk.ResponseType.OK, (selected_database != null || selected_file != null));
180
 
    }
181
 
    
182
 
    private Gtk.RadioButton create_radio_button(
183
 
        Gtk.Box box, Gtk.RadioButton? group, DiscoveredAlienDatabase? alien_db, string label
184
 
    ) {
185
 
        var button = new Gtk.RadioButton.with_label_from_widget (group, label);
186
 
        if (group == null) { // first radio button is active
187
 
            button.active = true;
188
 
            selected_database = alien_db;
189
 
        }
190
 
        button.toggled.connect (() => {
191
 
            if (button.active) {
192
 
                this.selected_database = alien_db;
193
 
                set_ok_sensitivity();
194
 
            }
195
 
        });
196
 
        box.pack_start(button, true, true, 6);
197
 
        return button;
198
 
    }
199
 
    
200
 
    private string collapse_user_path(string path) {
201
 
        string result = path;
202
 
        string home_dir = Environment.get_home_dir();
203
 
        if (path.has_prefix(home_dir)) {
204
 
            long cidx = home_dir.length;
205
 
            if (home_dir[home_dir.length - 1] == '/')
206
 
                cidx--;
207
 
            result = "~%s".printf(path.substring(cidx));
208
 
        }
209
 
        return result;
210
 
    }
211
 
    
212
 
    private int64 import_job_comparator(void *a, void *b) {
213
 
        return ((AlienDatabaseImportJob *) a)->get_exposure_time()
214
 
            - ((AlienDatabaseImportJob *) b)->get_exposure_time();
215
 
    }
216
 
}
217
 
 
218
 
private void alien_import_reporter(ImportManifest manifest, BatchImportRoll import_roll) {
219
 
    ImportUI.report_manifest(manifest, true);
220
 
}
221
 
 
222
 
}
223