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.
10
* A specialized import job implementation for alien databases.
12
public class AlienDatabaseImportJob : BatchImportJob {
13
private AlienDatabaseImportSource import_source;
14
private File? src_file;
15
private uint64 filesize;
16
private time_t exposure_time;
17
private AlienDatabaseImportJob? associated = null;
18
private HierarchicalTagIndex? detected_htags = null;
20
public AlienDatabaseImportJob(AlienDatabaseImportSource import_source) {
21
this.import_source = import_source;
23
// stash everything called in prepare(), as it may/will be called from a separate thread
24
src_file = import_source.get_file();
25
filesize = import_source.get_filesize();
26
exposure_time = import_source.get_exposure_time();
29
private HierarchicalTagIndex? build_exclusion_index(Gee.Collection<AlienDatabaseTag> src_tags) {
30
Gee.Set<string> detected_htags = new Gee.HashSet<string>();
32
foreach (AlienDatabaseTag src_tag in src_tags) {
33
string? prepped = HierarchicalTagUtilities.join_path_components(
35
build_path_components(src_tag)
39
if (prepped != null && prepped.has_prefix(Tag.PATH_SEPARATOR_STRING)) {
40
detected_htags.add(prepped);
42
Gee.List<string> parents = HierarchicalTagUtilities.enumerate_parent_paths(prepped);
43
foreach (string parent in parents)
44
detected_htags.add(parent);
48
return (detected_htags.size > 0) ? HierarchicalTagIndex.from_paths(detected_htags) : null;
51
public time_t get_exposure_time() {
55
public override string get_dest_identifier() {
56
return import_source.get_filename();
59
public override string get_source_identifier() {
60
return import_source.get_filename();
63
public override bool is_directory() {
67
public override string get_basename() {
68
return src_file.get_basename();
71
public override string get_path() {
72
return src_file.get_parent().get_path();
75
public override void set_associated(BatchImportJob associated) {
76
this.associated = associated as AlienDatabaseImportJob;
79
public override bool determine_file_size(out uint64 filesize, out File file) {
81
filesize = this.filesize;
86
public override bool prepare(out File file_to_import, out bool copy_to_library) throws Error {
87
file_to_import = src_file;
88
copy_to_library = false;
90
detected_htags = build_exclusion_index(import_source.get_photo().get_tags());
95
public override bool complete(MediaSource source, BatchImportRoll import_roll) throws Error {
96
LibraryPhoto? photo = source as LibraryPhoto;
100
AlienDatabasePhoto src_photo = import_source.get_photo();
103
if (detected_htags != null) {
104
Gee.Collection<string> paths = detected_htags.get_all_paths();
106
foreach (string path in paths)
110
Gee.Collection<AlienDatabaseTag> src_tags = src_photo.get_tags();
111
foreach (AlienDatabaseTag src_tag in src_tags) {
112
string? prepped = HierarchicalTagUtilities.join_path_components(
114
build_path_components(src_tag)
117
if (prepped != null) {
118
if (HierarchicalTagUtilities.enumerate_path_components(prepped).size == 1) {
119
if (prepped.has_prefix(Tag.PATH_SEPARATOR_STRING))
120
prepped = HierarchicalTagUtilities.hierarchical_to_flat(prepped);
122
Gee.List<string> parents =
123
HierarchicalTagUtilities.enumerate_parent_paths(prepped);
125
assert(parents.size > 0);
127
string top_level_parent = parents.get(0);
128
string flat_top_level_parent =
129
HierarchicalTagUtilities.hierarchical_to_flat(top_level_parent);
131
if (Tag.global.exists(flat_top_level_parent))
132
Tag.for_path(flat_top_level_parent).promote();
135
Tag.for_path(prepped).attach(photo);
139
AlienDatabaseEvent? src_event = src_photo.get_event();
140
if (src_event != null) {
141
string? prepped = prepare_input_text(src_event.get_name(),
142
PrepareInputTextOptions.DEFAULT, -1);
144
Event.generate_single_event(photo, import_roll.generated_events, prepped);
147
photo.set_rating(src_photo.get_rating());
149
string? title = src_photo.get_title();
151
photo.set_title(title);
153
photo.set_import_id(import_roll.import_id);
158
private string[] build_path_components(AlienDatabaseTag tag) {
159
// use a linked list as we are always inserting in head position
160
Gee.List<string> components = new Gee.LinkedList<string>();
161
for (AlienDatabaseTag current_tag = tag; current_tag != null; current_tag = current_tag.get_parent()) {
162
components.insert(0, HierarchicalTagUtilities.make_flat_tag_safe(current_tag.get_name()));
164
return components.to_array();