summaryrefslogtreecommitdiff
path: root/src/data_imports/DataImportsPluginHost.vala
blob: 6939ea51f90baafbdb5f325bf7d3d549462960e1 (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

namespace Spit.DataImports {

private class CoreImporter {
    private weak Spit.DataImports.PluginHost host;
    public int imported_items_count = 0;
    public BatchImportRoll? current_import_roll = null;
    
    public CoreImporter(Spit.DataImports.PluginHost host) {
        this.host = host;
    }
    
    public void prepare_media_items_for_import(
        ImportableMediaItem[] items,
        double progress,
        double host_progress_delta = 0.0,
        string? progress_message = null
    ) {
        host.update_import_progress_pane(progress, progress_message);
        //
        SortedList<DataImportJob> jobs =
            new SortedList<DataImportJob>(import_job_comparator);
        Gee.ArrayList<DataImportJob> already_imported =
            new Gee.ArrayList<DataImportJob>();
        Gee.ArrayList<DataImportJob> failed =
            new Gee.ArrayList<DataImportJob>();
        
        int item_idx = 0;
        double item_progress_delta = host_progress_delta / items.length;
        foreach (ImportableMediaItem src_item in items) {
            DataImportSource import_source = new DataImportSource(src_item);
            
            if (!import_source.was_backing_file_found()) {
                message("Skipping import of %s: backing file not found", 
                    import_source.get_filename());
                failed.add(new DataImportJob(import_source));
                
                continue;
            }
            
            if (import_source.is_already_imported()) {
                message("Skipping import of %s: checksum detected in library", 
                    import_source.get_filename());
                already_imported.add(new DataImportJob(import_source));
                
                continue;
            }
            
            jobs.add(new DataImportJob(import_source));
            item_idx++;
            host.update_import_progress_pane(progress + item_idx * item_progress_delta);
        }
        
        if (jobs.size > 0) {
            // If there it no current import roll, create one to ensure that all
            // imported items end up in the same roll even if this method is called
            // several times
            if (current_import_roll == null)
                current_import_roll = new BatchImportRoll();
            string db_name = _("%s Database").printf(host.get_data_importer().get_service().get_pluggable_name());
            BatchImport batch_import = new BatchImport(jobs, db_name, data_import_reporter,
                failed, already_imported, null, current_import_roll);
            
            LibraryWindow.get_app().enqueue_batch_import(batch_import, true);
            imported_items_count += jobs.size;
        }
        
        host.update_import_progress_pane(progress + host_progress_delta);
    }
    
    public void finalize_import() {
        // Send an empty job to the queue to mark the end of the import
        string db_name = _("%s Database").printf(host.get_data_importer().get_service().get_pluggable_name());
        BatchImport batch_import = new BatchImport(
            new Gee.ArrayList<BatchImportJob>(), db_name, data_import_reporter, null, null, null, current_import_roll
        );
        LibraryWindow.get_app().enqueue_batch_import(batch_import, true);
        current_import_roll = null;
    }
}

public class ConcreteDataImportsHost : Plugins.StandardHostInterface,
    Spit.DataImports.PluginHost {
    
    private Spit.DataImports.DataImporter active_importer = null;
    private weak DataImportsUI.DataImportsDialog dialog = null;
    private DataImportsUI.ProgressPane? progress_pane = null;
    private bool importing_halted = false;
    private CoreImporter core_importer;
    
    public ConcreteDataImportsHost(Service service, DataImportsUI.DataImportsDialog dialog) {
        base(service, "data_imports");
        this.dialog = dialog;
        
        this.active_importer = service.create_data_importer(this);
        this.core_importer = new CoreImporter(this);
    }
    
    public DataImporter get_data_importer() {
        return active_importer;
    }
    
    public void start_importing() {
        if (get_data_importer().is_running())
            return;

        debug("ConcreteDataImportsHost.start_importing( ): invoked.");
        
        get_data_importer().start();
    }

    public void stop_importing() {
        debug("ConcreteDataImportsHost.stop_importing( ): invoked.");
        
        if (get_data_importer().is_running())
            get_data_importer().stop();

        clean_up();

        importing_halted = true;
    }
    
    private void clean_up() {
        progress_pane = null;
    }
    
    public void set_button_mode(Spit.DataImports.PluginHost.ButtonMode mode) {
        if (mode == Spit.DataImports.PluginHost.ButtonMode.CLOSE)
            dialog.set_close_button_mode();
        else if (mode == Spit.DataImports.PluginHost.ButtonMode.CANCEL)
            dialog.set_cancel_button_mode();
        else
            error("unrecognized button mode enumeration value");
    }
    
    // Pane handling methods
    
    public void post_error(Error err) {
        post_error_message(err.message);
    }
    
    public void post_error_message(string message) {
        string msg = _("Importing from %s can't continue because an error occurred:").printf(
            active_importer.get_service().get_pluggable_name());
        msg += GLib.Markup.printf_escaped("\n\n<i>%s</i>\n\n", message);
        msg += _("To try importing from another service, select one from the above menu.");
        
        dialog.install_pane(new DataImportsUI.StaticMessagePane.with_pango(msg));
        dialog.set_close_button_mode();
        dialog.unlock_service();

        get_data_importer().stop();
        
        // post_error_message( ) tells the active_importer to stop importing and displays a
        // non-removable error pane that effectively ends the publishing interaction,
        // so no problem calling clean_up( ) here.
        clean_up();
    }
    
    public void install_dialog_pane(Spit.DataImports.DialogPane pane,
        Spit.DataImports.PluginHost.ButtonMode button_mode = Spit.DataImports.PluginHost.ButtonMode.CANCEL) {
        debug("DataImports.PluginHost: install_dialog_pane( ): invoked.");

        if (get_data_importer() == null || (!get_data_importer().is_running()))
            return;

        dialog.install_pane(pane);
        
        set_button_mode(button_mode);
    }

    public void install_static_message_pane(string message,
        Spit.DataImports.PluginHost.ButtonMode button_mode = Spit.DataImports.PluginHost.ButtonMode.CANCEL) {
        
        set_button_mode(button_mode);

        dialog.install_pane(new DataImportsUI.StaticMessagePane.with_pango(message));
    }
        
    public void install_library_selection_pane(
        string welcome_message,
        ImportableLibrary[] discovered_libraries,
        string? file_select_label
    ) {
        if (discovered_libraries.length == 0 && file_select_label == null)
            post_error_message("Libraries or file option needed");
        else
            dialog.install_pane(new DataImportsUI.LibrarySelectionPane(
                this,
                welcome_message,
                discovered_libraries,
                file_select_label
            ));
        set_button_mode(Spit.DataImports.PluginHost.ButtonMode.CLOSE);
    }
    
    public void install_import_progress_pane(
        string message
    ) {
        progress_pane = new DataImportsUI.ProgressPane(message);
        dialog.install_pane(progress_pane);
        set_button_mode(Spit.DataImports.PluginHost.ButtonMode.CANCEL);
        // initialize the import
        core_importer.imported_items_count = 0;
        core_importer.current_import_roll = null;
    }
    
    public void update_import_progress_pane(
        double progress,
        string? progress_message = null
    ) {
        if (progress_pane != null) {
            progress_pane.update_progress(progress, progress_message);
        }
    }
    
    public void prepare_media_items_for_import(
        ImportableMediaItem[] items,
        double progress,
        double host_progress_delta = 0.0,
        string? progress_message = null
    ) {
        core_importer.prepare_media_items_for_import(items, progress, host_progress_delta, progress_message);
    }
    
    public void finalize_import(
        ImportedItemsCountCallback report_imported_items_count,
        string? finalize_message = null
    ) {
        update_import_progress_pane(1.0, finalize_message);
        set_button_mode(Spit.DataImports.PluginHost.ButtonMode.CLOSE);
        core_importer.finalize_import();
        report_imported_items_count(core_importer.imported_items_count);
        if (core_importer.imported_items_count > 0)
            LibraryWindow.get_app().switch_to_import_queue_page();
    }
}

public class WelcomeDataImportsHost : Plugins.StandardHostInterface,
    Spit.DataImports.PluginHost {
    
    private weak WelcomeImportMetaHost meta_host;
    private Spit.DataImports.DataImporter active_importer = null;
    private bool importing_halted = false;
    private CoreImporter core_importer;
    
    public WelcomeDataImportsHost(Service service, WelcomeImportMetaHost meta_host) {
        base(service, "data_imports");
        
        this.active_importer = service.create_data_importer(this);
        this.core_importer = new CoreImporter(this);
        this.meta_host = meta_host;
    }
    
    public DataImporter get_data_importer() {
        return active_importer;
    }
    
    public void start_importing() {
        if (get_data_importer().is_running())
            return;

        debug("WelcomeDataImportsHost.start_importing( ): invoked.");
        
        get_data_importer().start();
    }

    public void stop_importing() {
        debug("WelcomeDataImportsHost.stop_importing( ): invoked.");
        
        if (get_data_importer().is_running())
            get_data_importer().stop();

        clean_up();

        importing_halted = true;
    }
    
    private void clean_up() {
    }
    
    // Pane handling methods
    
    public void post_error(Error err) {
        post_error_message(err.message);
    }
    
    public void post_error_message(string message) {
        string msg = _("Importing from %s can't continue because an error occurred:").printf(
            active_importer.get_service().get_pluggable_name());
        
        debug(msg);

        get_data_importer().stop();
        
        // post_error_message( ) tells the active_importer to stop importing and displays a
        // non-removable error pane that effectively ends the publishing interaction,
        // so no problem calling clean_up( ) here.
        clean_up();
    }
    
    public void install_dialog_pane(Spit.DataImports.DialogPane pane,
        Spit.DataImports.PluginHost.ButtonMode button_mode = Spit.DataImports.PluginHost.ButtonMode.CANCEL) {
        // do nothing
    }
        
    public void install_static_message_pane(string message,
        Spit.DataImports.PluginHost.ButtonMode button_mode = Spit.DataImports.PluginHost.ButtonMode.CANCEL) {
        // do nothing
    }
        
    public void install_library_selection_pane(
        string welcome_message,
        ImportableLibrary[] discovered_libraries,
        string? file_select_label
    ) {
        debug("WelcomeDataImportsHost: Installing library selection pane for %s".printf(get_data_importer().get_service().get_pluggable_name()));
        if (discovered_libraries.length > 0) {
            meta_host.install_service_entry(new WelcomeImportServiceEntry(
                this,
                get_data_importer().get_service().get_pluggable_name(),
                discovered_libraries
            ));
        }
    }
    
    public void install_import_progress_pane(
        string message
    ) {
        // empty implementation
    }
    
    public void update_import_progress_pane(
        double progress,
        string? progress_message = null
    ) {
        // empty implementation
    }
    
    public void prepare_media_items_for_import(
        ImportableMediaItem[] items,
        double progress,
        double host_progress_delta = 0.0,
        string? progress_message = null
    ) {
        core_importer.prepare_media_items_for_import(items, progress, host_progress_delta, progress_message);
    }
    
    public void finalize_import(
        ImportedItemsCountCallback report_imported_items_count,
        string? finalize_message = null
    ) {
        core_importer.finalize_import();
        report_imported_items_count(core_importer.imported_items_count);
        meta_host.finalize_import(this);
    }
}


//public delegate void WelcomeImporterCallback();

public class WelcomeImportServiceEntry : GLib.Object, WelcomeServiceEntry {
    private string pluggable_name;
    private ImportableLibrary[] discovered_libraries;
    private Spit.DataImports.PluginHost host;
    
    public WelcomeImportServiceEntry(
        Spit.DataImports.PluginHost host,
        string pluggable_name, ImportableLibrary[] discovered_libraries) {
        
        this.host = host;
        this.pluggable_name = pluggable_name;
        this.discovered_libraries = discovered_libraries;
    }
    
    public string get_service_name() {
        return pluggable_name;
    }
    
    public void execute() {
        foreach (ImportableLibrary library in discovered_libraries) {
            host.get_data_importer().on_library_selected(library);
        }
    }
}

public class WelcomeImportMetaHost : GLib.Object {
    private WelcomeDialog dialog;
    
    public WelcomeImportMetaHost(WelcomeDialog dialog) {
        this.dialog = dialog;
    }
    
    public void start() {
        Service[] services = load_all_services();
        foreach (Service service in services) {
            WelcomeDataImportsHost host = new WelcomeDataImportsHost(service, this);
            host.start_importing();
        }
    }
    
    public void finalize_import(WelcomeDataImportsHost host) {
        host.stop_importing();
    }
    
    public void install_service_entry(WelcomeServiceEntry entry) {
        debug("WelcomeImportMetaHost: Installing service entry for %s".printf(entry.get_service_name()));
        dialog.install_service_entry(entry);
    }
}

public static Spit.DataImports.Service[] load_all_services() {
    return load_services(true);
}

public static Spit.DataImports.Service[] load_services(bool load_all = false) {
    Spit.DataImports.Service[] loaded_services = new Spit.DataImports.Service[0];
    
    // load publishing services from plug-ins
    Gee.Collection<Spit.Pluggable> pluggables = Plugins.get_pluggables_for_type(
        typeof(Spit.DataImports.Service), null, load_all);
        // TODO: include sorting function to ensure consistent order
        
    debug("DataImportsDialog: discovered %d pluggable data import services.", pluggables.size);

    foreach (Spit.Pluggable pluggable in pluggables) {
        int pluggable_interface = pluggable.get_pluggable_interface(
            Spit.DataImports.CURRENT_INTERFACE, Spit.DataImports.CURRENT_INTERFACE);
        if (pluggable_interface != Spit.DataImports.CURRENT_INTERFACE) {
            warning("Unable to load data import plugin %s: reported interface %d.",
                Plugins.get_pluggable_module_id(pluggable), pluggable_interface);
            
            continue;
        }
        
        Spit.DataImports.Service service =
            (Spit.DataImports.Service) pluggable;

        debug("DataImportsDialog: discovered pluggable data import service '%s'.",
            service.get_pluggable_name());
        
        loaded_services += service;
    }
    
    // Sort import services by name.
    // TODO: extract to a function to sort it on initial request
    Posix.qsort(loaded_services, loaded_services.length, sizeof(Spit.DataImports.Service), 
        (a, b) => {return utf8_cs_compare((*((Spit.DataImports.Service**) a))->get_pluggable_name(), 
            (*((Spit.DataImports.Service**) b))->get_pluggable_name());
    });
    
    return loaded_services;
}

private ImportManifest? meta_manifest = null;

private void data_import_reporter(ImportManifest manifest, BatchImportRoll import_roll) {
    if (manifest.all.size > 0) {
        if (meta_manifest == null)
            meta_manifest = new ImportManifest();
        foreach (BatchImportResult result in manifest.all) {
            meta_manifest.add_result(result);
        }
    } else {
        DataImportsUI.DataImportsDialog.terminate_instance();
        ImportUI.report_manifest(meta_manifest, true);
        meta_manifest = null;
    }
}

private int64 import_job_comparator(void *a, void *b) {
    return ((DataImportJob *) a)->get_exposure_time()
        - ((DataImportJob *) b)->get_exposure_time();
}

}