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

« back to all changes in this revision

Viewing changes to src/alien_db/AlienDatabaseImportJob.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
 
/**
10
 
 * A specialized import job implementation for alien databases.
11
 
 */
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;
19
 
    
20
 
    public AlienDatabaseImportJob(AlienDatabaseImportSource import_source) {
21
 
        this.import_source = import_source;
22
 
        
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();
27
 
    }
28
 
    
29
 
    private HierarchicalTagIndex? build_exclusion_index(Gee.Collection<AlienDatabaseTag> src_tags) {
30
 
        Gee.Set<string> detected_htags = new Gee.HashSet<string>();
31
 
        
32
 
        foreach (AlienDatabaseTag src_tag in src_tags) {
33
 
            string? prepped = HierarchicalTagUtilities.join_path_components(
34
 
                Tag.prep_tag_names(
35
 
                    build_path_components(src_tag)
36
 
                )
37
 
            );
38
 
            
39
 
            if (prepped != null && prepped.has_prefix(Tag.PATH_SEPARATOR_STRING)) {
40
 
                detected_htags.add(prepped);
41
 
 
42
 
                Gee.List<string> parents = HierarchicalTagUtilities.enumerate_parent_paths(prepped);
43
 
                foreach (string parent in parents)
44
 
                    detected_htags.add(parent);
45
 
            }
46
 
        }
47
 
        
48
 
        return (detected_htags.size > 0) ? HierarchicalTagIndex.from_paths(detected_htags) : null;
49
 
    }
50
 
    
51
 
    public time_t get_exposure_time() {
52
 
        return exposure_time;
53
 
    }
54
 
    
55
 
    public override string get_dest_identifier() {
56
 
        return import_source.get_filename();
57
 
    }
58
 
    
59
 
    public override string get_source_identifier() {
60
 
        return import_source.get_filename();
61
 
    }
62
 
    
63
 
    public override bool is_directory() {
64
 
        return false;
65
 
    }
66
 
    
67
 
    public override string get_basename() {
68
 
        return src_file.get_basename();
69
 
    }
70
 
    
71
 
    public override string get_path() {
72
 
        return src_file.get_parent().get_path();
73
 
    }
74
 
    
75
 
    public override void set_associated(BatchImportJob associated) {
76
 
        this.associated = associated as AlienDatabaseImportJob;
77
 
    }
78
 
    
79
 
    public override bool determine_file_size(out uint64 filesize, out File file) {
80
 
        file = null;
81
 
        filesize = this.filesize;
82
 
        
83
 
        return true;
84
 
    }
85
 
    
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;
89
 
        
90
 
        detected_htags = build_exclusion_index(import_source.get_photo().get_tags());
91
 
        
92
 
        return true;
93
 
    }
94
 
    
95
 
    public override bool complete(MediaSource source, BatchImportRoll import_roll) throws Error {
96
 
        LibraryPhoto? photo = source as LibraryPhoto;
97
 
        if (photo == null)
98
 
            return false;
99
 
        
100
 
        AlienDatabasePhoto src_photo = import_source.get_photo();
101
 
        
102
 
        // tags
103
 
        if (detected_htags != null) {
104
 
            Gee.Collection<string> paths = detected_htags.get_all_paths();
105
 
 
106
 
            foreach (string path in paths)
107
 
                Tag.for_path(path);
108
 
        }
109
 
        
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(
113
 
                Tag.prep_tag_names(
114
 
                    build_path_components(src_tag)
115
 
                )
116
 
            );
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);
121
 
                } else {
122
 
                    Gee.List<string> parents =
123
 
                        HierarchicalTagUtilities.enumerate_parent_paths(prepped);
124
 
 
125
 
                    assert(parents.size > 0);
126
 
 
127
 
                    string top_level_parent = parents.get(0);
128
 
                    string flat_top_level_parent =
129
 
                        HierarchicalTagUtilities.hierarchical_to_flat(top_level_parent);
130
 
                    
131
 
                    if (Tag.global.exists(flat_top_level_parent))
132
 
                        Tag.for_path(flat_top_level_parent).promote();
133
 
                }
134
 
 
135
 
                Tag.for_path(prepped).attach(photo);
136
 
            }
137
 
        }
138
 
        // event
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);
143
 
            if (prepped != null)
144
 
                Event.generate_single_event(photo, import_roll.generated_events, prepped);
145
 
        }
146
 
        // rating
147
 
        photo.set_rating(src_photo.get_rating());
148
 
        // title
149
 
        string? title = src_photo.get_title();
150
 
        if (title != null)
151
 
            photo.set_title(title);
152
 
        // import ID
153
 
        photo.set_import_id(import_roll.import_id);
154
 
        
155
 
        return true;
156
 
    }
157
 
    
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()));
163
 
        }
164
 
        return components.to_array();
165
 
    }
166
 
}
167
 
 
168
 
}
169