summaryrefslogtreecommitdiff
path: root/plugins/shotwell-data-imports/FSpotImporter.vala
blob: 593f4567f4341f07e47012705fe9e357de572412 (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/* Copyright 2009-2015 Yorba Foundation
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

public class FSpotService : Object, Spit.Pluggable, Spit.DataImports.Service {
    private const string ICON_FILENAME = "f-spot-24.png";

    private static Gdk.Pixbuf[] icon_pixbuf_set = null;

    public FSpotService(GLib.File resource_directory) {
        // initialize the database layer
        DataImports.FSpot.Db.init();
        if (icon_pixbuf_set == null)
            icon_pixbuf_set = Resources.load_icon_set(resource_directory.get_child(ICON_FILENAME));
    }
    
    public int get_pluggable_interface(int min_host_interface, int max_host_interface) {
        return Spit.negotiate_interfaces(min_host_interface, max_host_interface,
            Spit.DataImports.CURRENT_INTERFACE);
    }

    public unowned string get_id() {
        return "org.yorba.shotwell.dataimports.fspot";
    }

    public unowned string get_pluggable_name() {
        return "F-Spot";
    }

    public void get_info(ref Spit.PluggableInfo info) {
        info.authors = "Bruno Girin";
        info.copyright = _("Copyright 2009-2015 Yorba Foundation");
        info.translators = Resources.TRANSLATORS;
        info.version = _VERSION;
        info.website_name = Resources.WEBSITE_NAME;
        info.website_url = Resources.WEBSITE_URL;
        info.is_license_wordwrapped = false;
        info.license = Resources.LICENSE;
        info.icons = icon_pixbuf_set;
    }

    public void activation(bool enabled) {
    }
    
    public Spit.DataImports.DataImporter create_data_importer(Spit.DataImports.PluginHost host) {
        return new DataImports.FSpot.FSpotDataImporter(this, host);
    }
}

namespace DataImports.FSpot {

internal const string SERVICE_NAME = "F-Spot";
internal const string SERVICE_WELCOME_MESSAGE =
    _("Welcome to the F-Spot library import service.\n\nPlease select a library to import, either by selecting one of the existing libraries found by Shotwell or by selecting an alternative F-Spot database file.");
internal const string SERVICE_WELCOME_MESSAGE_FILE_ONLY =
    _("Welcome to the F-Spot library import service.\n\nPlease select an F-Spot database file.");
internal const string FILE_IMPORT_LABEL =
    _("Manually select an F-Spot database file to import:");
internal const string ERROR_CANT_OPEN_DB_FILE =
    _("Cannot open the selected F-Spot database file: the file does not exist or is not an F-Spot database");
internal const string ERROR_UNSUPPORTED_DB_VERSION =
    _("Cannot open the selected F-Spot database file: this version of the F-Spot database is not supported by Shotwell");
internal const string ERROR_CANT_READ_TAGS_TABLE =
    _("Cannot read the selected F-Spot database file: error while reading tags table");
internal const string ERROR_CANT_READ_PHOTOS_TABLE =
    _("Cannot read the selected F-Spot database file: error while reading photos table");
internal const string MESSAGE_FINAL_SCREEN =
    _("Shotwell has found %d photos in the F-Spot library and is currently importing them. Duplicates will be automatically detected and removed.\n\nYou can close this dialog and start using Shotwell while the import is taking place in the background.");

public class FSpotImportableLibrary : Spit.DataImports.ImportableLibrary, GLib.Object {
    private File db_file;
    
    public FSpotImportableLibrary(File db_file) {
        this.db_file = db_file;
    }
    
    public File get_db_file() {
        return db_file;
    }
    
    public string get_display_name() {
        return _("F-Spot library: %s").printf(db_file.get_path());
    }
}

public class FSpotImportableItem : Spit.DataImports.ImportableMediaItem, GLib.Object {
    private DataImports.FSpot.Db.FSpotPhotoRow photo_row;
    private DataImports.FSpot.Db.FSpotPhotoVersionRow? photo_version_row;
    private DataImports.FSpot.Db.FSpotRollRow? roll_row;
    private FSpotImportableTag[] tags;
    private FSpotImportableEvent? event;
    private FSpotImportableRating rating;
    private string folder_path;
    private string filename;
    private time_t? date_time;
    
    public FSpotImportableItem(
        DataImports.FSpot.Db.FSpotPhotoRow photo_row,
        DataImports.FSpot.Db.FSpotPhotoVersionRow? photo_version_row,
        DataImports.FSpot.Db.FSpotRollRow? roll_row,
        FSpotImportableTag[] tags,
        FSpotImportableEvent? event,
        bool is_hidden,
        bool is_favorite
    ) {
        this.photo_row = photo_row;
        this.photo_version_row = photo_version_row;
        this.roll_row = roll_row;
        this.tags = tags;
        this.event = event;
        this.date_time = photo_row.time;
        if (photo_row.rating > 0)
            this.rating = new FSpotImportableRating(photo_row.rating);
        else if (is_hidden)
            this.rating = new FSpotImportableRating(FSpotImportableRating.REJECTED);
        else if (is_favorite)
            this.rating = new FSpotImportableRating(5);
        else
            this.rating = new FSpotImportableRating(FSpotImportableRating.UNRATED);
            
        // store path and filename
        folder_path = (photo_version_row != null) ?
            photo_version_row.base_path.get_path() :
            photo_row.base_path.get_path();
        filename = (photo_version_row != null) ?
            photo_version_row.filename :
            photo_row.filename;
        
        // In theory, neither field should be null at that point but belts
        // and braces don't hurt
        if (folder_path != null && filename != null) {
            // check if file exist and if not decode as URL
            File photo = File.new_for_path(folder_path).get_child(filename);
                
            // If file not found, parse as URI and store back
            if (!photo.query_exists()) {
                folder_path = decode_url(folder_path);
                filename = decode_url(filename);
            }
        }
    }
    
    public Spit.DataImports.ImportableTag[] get_tags() {
        Spit.DataImports.ImportableTag[] importable_tags = new Spit.DataImports.ImportableTag[0];
        foreach (FSpotImportableTag tag in tags)
            importable_tags += tag;
        return importable_tags;
    }
    
    public Spit.DataImports.ImportableEvent? get_event() {
        return event;
    }
    
    public string get_folder_path() {
        return folder_path;
    }
    
    public string get_filename() {
        return filename;
    }
    
    public string? get_title() {
        return (photo_row.description == null || photo_row.description == "") ? null : photo_row.description;
    }
    
    public Spit.DataImports.ImportableRating get_rating() {
        return rating;
    }

    public time_t? get_exposure_time() {
        return date_time;
    }
    
    private string decode_url(string url) {
        StringBuilder builder = new StringBuilder();
        for (int idx = 0; idx < url.length; ) {
            int cidx = url.index_of_char('%', idx);
            if (cidx > idx) {
                builder.append(url.slice(idx, cidx));
            }
            if (cidx >= 0) {
                if (cidx < url.length - 2) {
                    char c1 = url.get(cidx + 1);
                    char c2 = url.get(cidx + 2);
                    if (c1.isxdigit() && c1.isxdigit()) {
                        int ccode = 0x10 * c1.xdigit_value() + c2.xdigit_value();
                        builder.append_c((char)ccode);
                    }
                    idx = cidx + 3;
                } else {
                    idx = cidx + 1;
                }
            } else {
                builder.append(url.substring(idx));
                idx = url.length;
            }
        }
        return builder.str;
    }
}

public class FSpotImportableTag : Spit.DataImports.ImportableTag, GLib.Object {
    private DataImports.FSpot.Db.FSpotTagRow row;
    private FSpotImportableTag? parent;
    
    public FSpotImportableTag(DataImports.FSpot.Db.FSpotTagRow row, FSpotImportableTag? parent) {
        this.row = row;
        this.parent = parent;
    }
    
    public int64 get_id() {
        return row.tag_id;
    }
    
    public string get_name() {
        return row.name;
    }
    
    public Spit.DataImports.ImportableTag? get_parent() {
        return parent;
    }
    
    public FSpotImportableTag? get_fspot_parent() {
        return parent;
    }
    
    public string get_stock_icon() {
        return row.stock_icon;
    }
    
    public bool is_stock() {
        return (row.stock_icon.has_prefix(DataImports.FSpot.Db.FSpotTagsTable.PREFIX_STOCK_ICON));
    }
    
    public FSpotImportableEvent to_event() {
        return new FSpotImportableEvent(this.row);
    }
}

public class FSpotImportableEvent : Spit.DataImports.ImportableEvent, GLib.Object {
    private DataImports.FSpot.Db.FSpotTagRow row;
    
    public FSpotImportableEvent(DataImports.FSpot.Db.FSpotTagRow row) {
        this.row = row;
    }
    
    public string get_name() {
        return row.name;
    }
}

public class FSpotImportableRating : Spit.DataImports.ImportableRating, GLib.Object {
    public static const int REJECTED = -1;
    public static const int UNRATED = 0;
    
    private int rating_value;
    
    public FSpotImportableRating(int rating_value) {
        if (rating_value < -1)
            rating_value = -1;
        else if (rating_value > 5)
            rating_value = 5;
        this.rating_value = rating_value;
    }
    
    public bool is_rejected() {
        return (rating_value == REJECTED);
    }
    
    public bool is_unrated() {
        return (rating_value == UNRATED);
    }
    
    public int get_value() {
        return rating_value;
    }
}

internal class FSpotTagsCache : Object {
    private DataImports.FSpot.Db.FSpotTagsTable tags_table;
    private Gee.HashMap<int64?, FSpotImportableTag> tags_map;
    
    public FSpotTagsCache(DataImports.FSpot.Db.FSpotTagsTable tags_table) throws DatabaseError {
        this.tags_table = tags_table;
        tags_map = new Gee.HashMap<int64?, FSpotImportableTag> ();
    }
    
    public FSpotImportableTag get_tag(DataImports.FSpot.Db.FSpotTagRow tag_row) throws DatabaseError {
        FSpotImportableTag? tag = tags_map.get(tag_row.tag_id);
        if (tag != null) {
            return tag;
        } else {
            FSpotImportableTag? parent_tag = get_tag_from_id(tag_row.category_id);
            FSpotImportableTag new_tag = new FSpotImportableTag(tag_row, parent_tag);
            tags_map[tag_row.tag_id] = new_tag;
            return new_tag;
        }
    }
    
    private FSpotImportableTag? get_tag_from_id(int64 tag_id) throws DatabaseError {
        // check whether the tag ID is valid first, otherwise return null
        if (tag_id < 1)
            return null;
        FSpotImportableTag? tag = tags_map.get(tag_id);
        if (tag != null)
            return tag;
        DataImports.FSpot.Db.FSpotTagRow? tag_row = tags_table.get_by_id(tag_id);
        if (tag_row != null) {
            FSpotImportableTag? parent_tag = get_tag_from_id(tag_row.category_id);
            FSpotImportableTag new_tag = new FSpotImportableTag(tag_row, parent_tag);
            tags_map[tag_id] = new_tag;
            return new_tag;
        }
        return null;
    }
}

public class FSpotDataImporter : Spit.DataImports.DataImporter, GLib.Object {

    private weak Spit.DataImports.PluginHost host = null;
    private weak Spit.DataImports.Service service = null;
    private bool running = false;

    public FSpotDataImporter(Spit.DataImports.Service service,
        Spit.DataImports.PluginHost host) {
        debug("FSpotDataImporter instantiated.");
        this.service = service;
        this.host = host;
    }
    
    private bool is_running() {
        return running;
    }
    
    public Spit.DataImports.Service get_service() {
        return service;
    }

    public void start() {
        if (is_running())
            return;
        
        debug("FSpotDataImporter: starting interaction.");
        
        running = true;
        
        do_discover_importable_libraries();
    }

    public void stop() {
        debug("FSpotDataImporter: stopping interaction.");
        
        running = false;
    }
    
    // Actions and event implementation
    
    /**
     * Action that discovers importable libraries based on standard locations.
     */
    private void do_discover_importable_libraries() {
        Spit.DataImports.ImportableLibrary[] discovered_libraries =
            new Spit.DataImports.ImportableLibrary[0];
        
        File[] db_files = {
            // where the DB is in Ubuntu Lucid
            File.new_for_path(Environment.get_user_config_dir()).
                get_child("f-spot").get_child("photos.db"),
            // where it seems to be in Ubuntu Jaunty
            File.new_for_path(Environment.get_home_dir()).get_child(".gnome2").
                get_child("f-spot").get_child("photos.db"),
            // where it should really be if it followed the XDG spec
            File.new_for_path(Environment.get_user_data_dir()).
                get_child("f-spot").get_child("photos.db")
        };
        
        foreach (File db_file in db_files) {
            if (db_file.query_exists(null)) {
                discovered_libraries += new FSpotImportableLibrary(db_file);
                message("Discovered importable library: %s", db_file.get_path());
            }
        }
        
        host.install_library_selection_pane(
            (discovered_libraries.length > 0 ? SERVICE_WELCOME_MESSAGE : SERVICE_WELCOME_MESSAGE_FILE_ONLY),
            discovered_libraries,
            FILE_IMPORT_LABEL
        );
    }
    
    public void on_library_selected(Spit.DataImports.ImportableLibrary library) {
        on_file_selected(((FSpotImportableLibrary)library).get_db_file());
    }
    
    public void on_file_selected(File file) {
        DataImports.FSpot.Db.FSpotDatabase database;
        FSpotTagsCache tags_cache;
        Gee.ArrayList<DataImports.FSpot.Db.FSpotPhotoRow> all_photos;
        double progress_delta_per_photo = 1.0;
        double progress_plugin_to_host_ratio = 0.5;
        double current_progress = 0.0;
        try {
            database = new DataImports.FSpot.Db.FSpotDatabase(file);
        } catch(DatabaseError e) {
            debug("FSpotDataImporter: Can't open database file: %s".printf(e.message));
            host.post_error_message(ERROR_CANT_OPEN_DB_FILE);
            return;
        } catch(Spit.DataImports.DataImportError e) {
            debug("FSpotDataImporter: Unsupported F-Spot database version: %s".printf(e.message));
            host.post_error_message(ERROR_UNSUPPORTED_DB_VERSION);
            return;
        }
        try {
            tags_cache = new FSpotTagsCache(database.tags_table);
        } catch(DatabaseError e) {
            debug("FSpotDataImporter: Can't read tags table: %s".printf(e.message));
            host.post_error_message(ERROR_CANT_READ_TAGS_TABLE);
            return;
        }
        host.install_import_progress_pane(_("Preparing to import"));
        try {
            all_photos = database.photos_table.get_all();
        } catch(DatabaseError e) {
            debug("FSpotDataImporter: Can't read photos table: %s".printf(e.message));
            host.post_error_message(ERROR_CANT_READ_PHOTOS_TABLE);
            return;
        }
        if (all_photos.size > 0)
            progress_delta_per_photo = 1.0 / all_photos.size;
        foreach (DataImports.FSpot.Db.FSpotPhotoRow photo_row in all_photos) {
            bool hidden = false;
            bool favorite = false;
            FSpotImportableTag[] tags = new FSpotImportableTag[0];
            FSpotImportableEvent? event = null;
            DataImports.FSpot.Db.FSpotRollRow? roll_row = null;
            
            // TODO: We do not convert F-Spot events to Shotwell events because F-Spot's events
            // are essentially tags.  We would need to detect if the tag is an event (use
            // is_tag_event) and then assign the event to the photo ... since a photo can be
            // in multiple F-Spot events, we would need to pick one, and since their tags
            // are hierarchical, we would need to pick a name (probably the leaf)
            try {
                foreach (
                    DataImports.FSpot.Db.FSpotTagRow tag_row in
                    database.tags_table.get_by_photo_id(photo_row.photo_id)
                ) {
                    FSpotImportableTag tag = tags_cache.get_tag(tag_row);
                    if (is_tag_hidden(tag, database.hidden_tag_id))
                        hidden = true;
                    else if (is_tag_favorite(tag))
                        favorite = true;
                    else
                        tags += tag;
                }
            } catch(DatabaseError e) {
                // log the error and leave the tag list empty
                message("Failed to retrieve tags for photo ID %ld: %s", (long) photo_row.photo_id,
                    e.message);
            }
            
            try {
                roll_row = database.rolls_table.get_by_id(photo_row.roll_id);
            } catch (DatabaseError e) {
                // log the error and leave the roll row null
                message("Failed to retrieve roll for photo ID %ld: %s", (long) photo_row.photo_id,
                    e.message);
            }
            
            Spit.DataImports.ImportableMediaItem[] importable_items = new Spit.DataImports.ImportableMediaItem[0];
            try {
                Gee.ArrayList<DataImports.FSpot.Db.FSpotPhotoVersionRow> photo_versions =
                    database.photo_versions_table.get_by_photo_id(photo_row.photo_id);
                bool photo_versions_added = false;   // set to true if at least one version was added
                bool photo_versions_skipped = false; // set to true if at least one version was skipped due to missing file details
                foreach (DataImports.FSpot.Db.FSpotPhotoVersionRow photo_version_row in photo_versions) {
                    if (photo_version_row.base_path != null && photo_version_row.filename != null) {
                        importable_items += new FSpotImportableItem(
                            photo_row, photo_version_row, roll_row, tags, event, hidden, favorite
                        );
                        photo_versions_added = true;
                    } else {
                        photo_versions_skipped = true;
                    }
                }
                
                // Older versions of F-Spot (0.4.3.1 at least, perhaps later) did not maintain photo_versions,
                // this handles that case
                // It also handles the case when we had to skip any photo version due to missing
                // file details
                if (photo_versions_skipped || !photo_versions_added) {
                    if (photo_row.base_path != null && photo_row.filename != null) {
                        importable_items += new FSpotImportableItem(
                            photo_row, null, roll_row, tags, event, hidden, favorite
                        );
                    }
                }
            } catch (DatabaseError e) {
                // if we can't load the different versions, do the best we can
                // and create one photo from the photo row that was found earlier
                message("Failed to retrieve versions for photo ID %ld: %s", (long) photo_row.photo_id,
                    e.message);
                if (photo_row.base_path != null && photo_row.filename != null) {
                    importable_items += new FSpotImportableItem(
                        photo_row, null, roll_row, tags, event, hidden, favorite
                    );
                }
            }
            // If the importer is still running, import the items and loop,
            // otherwise break the loop
            if (running) {
                host.prepare_media_items_for_import(
                    importable_items,
                    current_progress + (progress_delta_per_photo * progress_plugin_to_host_ratio),
                    progress_delta_per_photo * (1 - progress_plugin_to_host_ratio),
                    null
                );
                current_progress += progress_delta_per_photo;
                host.update_import_progress_pane(current_progress);
            } else {
                break;
            }
        }
        host.finalize_import(on_imported_items_count);
    }
    
    public void on_imported_items_count(int imported_items_count) {
        host.install_static_message_pane(
            MESSAGE_FINAL_SCREEN.printf(imported_items_count),
            Spit.DataImports.PluginHost.ButtonMode.CLOSE
        );
    }
    
    private bool is_tag_event(FSpotImportableTag tag) {
        bool result = (DataImports.FSpot.Db.FSpotTagsTable.STOCK_ICON_EVENTS == tag.get_stock_icon());
        if (!result) {
            FSpotImportableTag? parent = tag.get_fspot_parent();
            if (parent == null)
                result = false;
            else
                result = is_tag_event(parent);
        }
        return result;
    }
    
    private bool is_tag_hidden(FSpotImportableTag tag, int64 hidden_tag_id) {
        bool result = (hidden_tag_id == tag.get_id());
        if (!result) {
            FSpotImportableTag? parent = tag.get_fspot_parent();
            if (parent == null)
                result = false;
            else
                result = is_tag_hidden(parent, hidden_tag_id);
        }
        return result;
    }
    
    private bool is_tag_favorite(FSpotImportableTag tag) {
        bool result = (DataImports.FSpot.Db.FSpotTagsTable.STOCK_ICON_FAV == tag.get_stock_icon());
        if (!result) {
            FSpotImportableTag? parent = tag.get_fspot_parent();
            if (parent == null)
                result = false;
            else
                result = is_tag_favorite(parent);
        }
        return result;
    }
}

} // namespace