1
/* Copyright 2009-2011 Yorba Foundation
3
* This software is licensed under the GNU LGPL (version 2.1 or later).
4
* See the COPYING file in this distribution.
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;
14
private Gtk.Builder builder;
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;
26
public void set_builder(Gtk.Builder builder) {
27
this.builder = builder;
30
public void setup(string title, AlienDatabaseDriver driver,
31
BatchImport.ImportReporter reporter) {
33
set_parent_window(AppWindow.get_instance().get_parent_window());
34
set_transient_for(AppWindow.get_instance());
36
this.reporter = reporter;
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;
44
Gtk.Box options_box = builder.get_object("options_box") as Gtk.Box;
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)
55
db_radio = create_radio_button(options_box, db_radio, db, db_radio_label);
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())
62
Gtk.Label custom_file_label = new Gtk.Label(
63
_("Import from a %s database file:").printf(driver.get_display_name())
65
options_box.pack_start(custom_file_label, true, true, 6);
70
public void execute() {
73
bool is_finished = false;
74
while (!is_finished) {
75
if (run() == Gtk.ResponseType.OK)
76
is_finished = execute_import();
85
// The bit that does all the work once the OK button has been clicked.
87
private bool execute_import() {
89
AlienDatabase? alien_db = null;
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"));
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);
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>();
112
Gee.Collection<AlienDatabasePhoto> photos = alien_db.get_photos();
113
int photo_total = photos.size;
115
foreach (AlienDatabasePhoto src_photo in photos) {
116
AlienDatabaseImportSource import_source = new AlienDatabaseImportSource(src_photo);
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));
126
jobs.add(new AlienDatabaseImportJob(import_source));
128
prepare_progress_bar.set_fraction((double)photo_idx / (double)photo_total);
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,
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
144
LibraryWindow.get_app().switch_to_import_queue_page();
146
if (selected_database != null) {
147
selected_database.release_database();
148
selected_database = null;
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);
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();
178
private void set_ok_sensitivity() {
179
set_response_sensitive(Gtk.ResponseType.OK, (selected_database != null || selected_file != null));
182
private Gtk.RadioButton create_radio_button(
183
Gtk.Box box, Gtk.RadioButton? group, DiscoveredAlienDatabase? alien_db, string label
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;
190
button.toggled.connect (() => {
192
this.selected_database = alien_db;
193
set_ok_sensitivity();
196
box.pack_start(button, true, true, 6);
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] == '/')
207
result = "~%s".printf(path.substring(cidx));
212
private int64 import_job_comparator(void *a, void *b) {
213
return ((AlienDatabaseImportJob *) a)->get_exposure_time()
214
- ((AlienDatabaseImportJob *) b)->get_exposure_time();
218
private void alien_import_reporter(ImportManifest manifest, BatchImportRoll import_roll) {
219
ImportUI.report_manifest(manifest, true);