Properties.vala 23.9 KB
Newer Older
1
/* Copyright 2016 Software Freedom Conservancy Inc.
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 8 9
private abstract class Properties : Gtk.Box {
    protected Gtk.Grid grid = new Gtk.Grid();
    protected uint line_count = 0;
10 11

    public Properties() {
12 13 14 15 16
        grid.row_spacing = 6;
        grid.column_spacing = 12;
        set_homogeneous(false);
        set_orientation(Gtk.Orientation.VERTICAL);
        pack_start(grid, false, false, 0);
17 18
    }

19
    protected void add_line(string label_text, string info_text, bool multi_line = false, string? href = null) {
20
        Gtk.Label label = new Gtk.Label("");
21 22
        Gtk.Widget info;

23
        label.set_justify(Gtk.Justification.RIGHT);
24
        label.get_style_context().add_class("dim-label");
25 26
        
        label.set_markup(GLib.Markup.printf_escaped("<span font_weight=\"bold\">%s</span>", label_text));
27 28 29

        if (multi_line) {
            Gtk.ScrolledWindow info_scroll = new Gtk.ScrolledWindow(null, null);
30
            info_scroll.shadow_type = Gtk.ShadowType.NONE;
31 32 33 34 35
            Gtk.TextView view = new Gtk.TextView();
            // by default TextView widgets have a white background, which
            // makes sense during editing. In this instance we only *show*
            // the content and thus want that the parent's background color
            // is inherited to the TextView
36
            view.get_style_context().add_class("shotwell-static");
37 38 39 40
            view.set_wrap_mode(Gtk.WrapMode.WORD);
            view.set_cursor_visible(false);
            view.set_editable(false);
            view.buffer.text = is_string_empty(info_text) ? "" : info_text;
41
            view.hexpand = true;
42
            info_scroll.add(view);
43 44
            label.halign = Gtk.Align.END;
            label.valign = Gtk.Align.START;
45 46 47
            info = (Gtk.Widget) info_scroll;
        } else {
            Gtk.Label info_label = new Gtk.Label("");
48 49 50
            if (!is_string_empty(info_text)) {
                info_label.set_tooltip_markup(info_text);
            }
51 52 53 54 55 56

            if (href == null) {
                info_label.set_markup(is_string_empty(info_text) ? "" : info_text);
            } else {
                info_label.set_markup("<a href=\"%s\">%s</a>".printf(href, info_text));
            }
57
            info_label.set_ellipsize(Pango.EllipsizeMode.END);
58 59 60 61 62
            info_label.halign = Gtk.Align.START;
            info_label.valign = Gtk.Align.FILL;
            info_label.hexpand = false;
            info_label.vexpand = false;
            info_label.set_justify(Gtk.Justification.LEFT);
63
            info_label.set_selectable(true);
64 65
            label.halign = Gtk.Align.END;
            label.valign = Gtk.Align.FILL;
66 67 68
            info = (Gtk.Widget) info_label;
        }

69
        grid.attach(label, 0, (int) line_count, 1, 1);
70 71

        if (multi_line) {
72
            grid.attach(info, 1, (int) line_count, 1, 3);
73
        } else {
74
            grid.attach(info, 1, (int) line_count, 1, 1);
75
        }
76

77
        line_count++;
78
    }
79
    
80
    protected string get_prettyprint_time(Time time) {
81
        string timestring = time.format(Resources.get_hh_mm_format_string());
82 83 84 85 86
        
        if (timestring[0] == '0')
            timestring = timestring.substring(1, -1);
        
        return timestring;
87
    }
88 89 90 91 92 93 94 95 96 97
    
    protected string get_prettyprint_time_with_seconds(Time time) {
        string timestring = time.format(Resources.get_hh_mm_ss_format_string());
        
        if (timestring[0] == '0')
            timestring = timestring.substring(1, -1);
        
        return timestring;
    }
    
98
    protected string get_prettyprint_date(Time date) {
99
        string date_string = null;
100 101
        Time today = Time.local(time_t());
        if (date.day_of_year == today.day_of_year && date.year == today.year) {
102
            date_string = _("Today");
103
        } else if (date.day_of_year == (today.day_of_year - 1) && date.year == today.year) {
104
            date_string = _("Yesterday");
105
        } else {
106
            date_string = format_local_date(date);
107 108
        }

109
        return date_string;
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
    protected virtual void get_single_properties(DataView view) {
    }

    protected virtual void get_multiple_properties(Gee.Iterable<DataView>? iter) {
    }

    protected virtual void get_properties(Page current_page) {
        ViewCollection view = current_page.get_view();
        if (view == null)
            return;

        // summarize selected items, if none selected, summarize all
        int count = view.get_selected_count();
        Gee.Iterable<DataView> iter = null;
        if (count != 0) {
            iter = view.get_selected();
        } else {
            count = view.get_count();
            iter = (Gee.Iterable<DataView>) view.get_all();
        }
        
        if (iter == null || count == 0)
            return;

        if (count == 1) {
            foreach (DataView item in iter) {
138
                get_single_properties(item);
139 140 141 142 143 144 145 146
                break;
            }
        } else {
            get_multiple_properties(iter);
        }
    }

    protected virtual void clear_properties() {
147 148 149
        foreach (Gtk.Widget child in grid.get_children())
            grid.remove(child);

150
        line_count = 0;
151 152
    }

153
    public void update_properties(Page page) {
154
        clear_properties();
155 156
        internal_update_properties(page);
        show_all();
157 158
    }

159 160
    public virtual void internal_update_properties(Page page) {
        get_properties(page);
161
    }
162 163 164 165 166 167 168
}

private class BasicProperties : Properties {
    private string title;
    private time_t start_time = time_t();
    private time_t end_time = time_t();
    private Dimensions dimensions;
169
    private int photo_count;
170
    private int event_count;
171
    private int video_count;
172 173 174
    private string exposure;
    private string aperture;
    private string iso;
175
    private double clip_duration;
176
    private string raw_developer;
177
    private string raw_assoc;
178
    private MapWidget map_widget;
179

180
    public BasicProperties() {
181 182
        map_widget = MapWidget.get_instance();
        pack_start(map_widget, true, true, 0);
183 184
    }

185
    protected override void clear_properties() {
186
        base.clear_properties();
187
        title = "";
188 189
        start_time = 0;
        end_time = 0;
190 191
        dimensions = Dimensions(0,0);
        photo_count = -1;
192
        event_count = -1;
193
        video_count = -1;
194 195 196
        exposure = "";
        aperture = "";
        iso = "";
197
        clip_duration = 0.0;
198
        raw_developer = "";
199
        raw_assoc = "";
200
        map_widget.clear();
201 202
    }

203
    protected override void get_single_properties(DataView view) {
204 205
        base.get_single_properties(view);

206 207 208 209
        DataSource source = view.get_source();

        title = source.get_name();
        
210 211 212
        if (source is PhotoSource || source is PhotoImportSource) {           
            start_time = (source is PhotoSource) ? ((PhotoSource) source).get_exposure_time() :
                ((PhotoImportSource) source).get_exposure_time();
213
            end_time = start_time;
214 215 216 217
                        
            PhotoMetadata? metadata = (source is PhotoSource) ? ((PhotoSource) source).get_metadata() :
                ((PhotoImportSource) source).get_metadata();

218 219 220 221 222
            if (metadata != null) {
                exposure = metadata.get_exposure_string();
                if (exposure == null)
                    exposure = "";
                
223
                aperture = metadata.get_aperture_string(true);
224 225 226 227 228 229
                if (aperture == null)
                    aperture = "";
                
                iso = metadata.get_iso_string();
                if (iso == null)
                    iso = "";
230 231 232 233

                dimensions = (metadata.get_pixel_dimensions() != null) ?
                    metadata.get_orientation().rotate_dimensions(metadata.get_pixel_dimensions()) :
                    Dimensions(0, 0);
234
            }
235 236 237
            
            if (source is PhotoSource)
                dimensions = ((PhotoSource) source).get_dimensions();
238 239
            
            if (source is Photo && ((Photo) source).get_master_file_format() == PhotoFileFormat.RAW) {
240 241
                Photo photo = source as Photo;
                raw_developer = photo.get_raw_developer().get_label();
242
                raw_assoc = photo.is_raw_developer_available(RawDeveloper.CAMERA) ? _("RAW+JPEG") : "";
243
            }
244 245
        } else if (source is EventSource) {
            EventSource event_source = (EventSource) source;
246 247 248 249

            start_time = event_source.get_start_time();
            end_time = event_source.get_end_time();

250 251 252 253 254 255 256
            int event_photo_count;
            int event_video_count;
            MediaSourceCollection.count_media(event_source.get_media(), out event_photo_count,
                out event_video_count);
            
            photo_count = event_photo_count;
            video_count = event_video_count;
257 258 259 260
        } else if (source is VideoSource || source is VideoImportSource) {
            if (source is VideoSource) {
                Video video = (Video) source;
                clip_duration = video.get_clip_duration();
261

262 263 264 265 266 267 268
                if (video.get_is_interpretable())
                    dimensions = video.get_frame_dimensions();

                start_time = video.get_exposure_time();
            } else {
                start_time = ((VideoImportSource) source).get_exposure_time();
            }
269
            end_time = start_time;
270
        }
271
        map_widget.add_data_view(view);
272

273 274
    }

275
    protected override void get_multiple_properties(Gee.Iterable<DataView>? iter) {
276 277
        base.get_multiple_properties(iter);

278
        photo_count = 0;
279
        video_count = 0;
280 281
        foreach (DataView view in iter) {
            DataSource source = view.get_source();
282 283

            if (source is PhotoSource || source is PhotoImportSource) {
284 285 286
                time_t exposure_time = (source is PhotoSource) ?
                    ((PhotoSource) source).get_exposure_time() :
                    ((PhotoImportSource) source).get_exposure_time();
287 288 289 290 291 292 293 294

                if (exposure_time != 0) {
                    if (start_time == 0 || exposure_time < start_time)
                        start_time = exposure_time;

                    if (end_time == 0 || exposure_time > end_time)
                        end_time = exposure_time;
                }
295

296 297 298
                photo_count++;
            } else if (source is EventSource) {
                EventSource event_source = (EventSource) source;
299 300 301 302 303 304 305 306
          
                if (event_count == -1)
                    event_count = 0;

                if ((start_time == 0 || event_source.get_start_time() < start_time) &&
                    event_source.get_start_time() != 0 ) {
                    start_time = event_source.get_start_time();
                }
307
                if ((end_time == 0 || event_source.get_end_time() > end_time) &&
308 309 310 311 312 313
                    event_source.get_end_time() != 0 ) {
                    end_time = event_source.get_end_time();
                } else if (end_time == 0 || event_source.get_start_time() > end_time) {
                    end_time = event_source.get_start_time();
                }

314 315 316 317 318 319 320
                int event_photo_count;
                int event_video_count;
                MediaSourceCollection.count_media(event_source.get_media(), out event_photo_count,
                    out event_video_count);

                photo_count += event_photo_count;
                video_count += event_video_count;
321
                event_count++;
322 323 324 325 326 327 328 329 330 331 332 333 334
            } else if (source is VideoSource || source is VideoImportSource) {
                time_t exposure_time = (source is VideoSource) ?
                    ((VideoSource) source).get_exposure_time() :
                    ((VideoImportSource) source).get_exposure_time();

                if (exposure_time != 0) {
                    if (start_time == 0 || exposure_time < start_time)
                        start_time = exposure_time;

                    if (end_time == 0 || exposure_time > end_time)
                        end_time = exposure_time;
                }

335
                video_count++;
336
            }
337
            map_widget.add_data_view(view);
338 339
        }
    }
340

341
    protected override void get_properties(Page current_page) {
342
        base.get_properties(current_page);
343

344
        map_widget.set_page(current_page);
345 346 347 348
        if (end_time == 0)
            end_time = start_time;
        if (start_time == 0)
            start_time = end_time;
349 350
    }

351
    protected override void internal_update_properties(Page page) {
352
        base.internal_update_properties(page);
353

354 355
        // display the title if a Tag page
        if (title == "" && page is TagPage)
356
            title = ((TagPage) page).get_tag().get_user_visible_name();
357
            
358
        if (title != "")
359
            add_line(_("Title:"), guarded_markup_escape_text(title));
360

361
        if (photo_count >= 0 || video_count >= 0) {
362
            string label = _("Items:");
363 364
  
            if (event_count >= 0) {
365 366
                string event_num_string = (ngettext("%d Event", "%d Events", event_count)).printf(
                    event_count);
367

368
                add_line(label, event_num_string);
369 370 371
                label = "";
            }

372 373
            string photo_num_string = (ngettext("%d Photo", "%d Photos", photo_count)).printf(
                photo_count);
374 375
            string video_num_string = (ngettext("%d Video", "%d Videos", video_count)).printf(
                video_count);
376

377 378 379 380 381
            if (photo_count == 0 && video_count > 0) {
                add_line(label, video_num_string);
                return;
            }
            
382
            add_line(label, photo_num_string);
383 384 385
            
            if (video_count > 0)
                add_line("", video_num_string);
386 387
        }

388
        if (start_time != 0) {
389 390 391 392
            string start_date = get_prettyprint_date(Time.local(start_time));
            string start_time = get_prettyprint_time(Time.local(start_time));
            string end_date = get_prettyprint_date(Time.local(end_time));
            string end_time = get_prettyprint_time(Time.local(end_time));
393

394
            if (start_date == end_date) {
395
                // display only one date if start and end are the same
396
                add_line(_("Date:"), start_date);
397 398 399

                if (start_time == end_time) {
                    // display only one time if start and end are the same
400
                    add_line(_("Time:"), start_time);
401
                } else {
402
                    // display time range
403 404
                    add_line(_("From:"), start_time);
                    add_line(_("To:"), end_time);
405 406
                }
            } else {
407
                // display date range
408 409
                add_line(_("From:"), start_date);
                add_line(_("To:"), end_date);
410
            }
411 412
        }

413
        if (dimensions.has_area()) {
414
            string label = _("Size:");
415

416
            if (dimensions.has_area()) {
417
                add_line(label, "%d &#215; %d".printf(dimensions.width, dimensions.height));
418
                label = "";
419
            }
420
        }
421 422 423 424
        
        if (clip_duration > 0.0) {
            add_line(_("Duration:"), _("%.1f seconds").printf(clip_duration));
        }
425 426 427 428
        
        if (raw_developer != "") {
            add_line(_("Developer:"), raw_developer);
        }
429 430 431 432
        
        // RAW+JPEG flag.
        if (raw_assoc != "")
            add_line("", raw_assoc);
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
        if (exposure != "" || aperture != "" || iso != "") {
            string line = null;
            
            // attempt to put exposure and aperture on the same line
            if (exposure != "")
                line = exposure;
            
            if (aperture != "") {
                if (line != null)
                    line += ", " + aperture;
                else
                    line = aperture;
            }
            
            // if not both available but ISO is, add it to the first line
            if ((exposure == "" || aperture == "") && iso != "") {
                if (line != null)
                    line += ", " + "ISO " + iso;
                else
                    line = "ISO " + iso;
                
                add_line(_("Exposure:"), line);
            } else {
                // fit both on the top line, emit and move on
                if (line != null)
                    add_line(_("Exposure:"), line);
                
                // emit ISO on a second unadorned line
                if (iso != "") {
                    if (line != null)
                        add_line("","ISO " + iso);
                    else
                        add_line(_("Exposure:"), "ISO " + iso);
                }
            }
469
        }
470 471

        map_widget.show_position_markers();
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
private class ExtendedProperties : Properties {
    private const string NO_VALUE = "";
    // Photo stuff
    private string file_path;
    private uint64 filesize;
    private Dimensions? original_dim;
    private string camera_make;
    private string camera_model;
    private string flash;
    private string focal_length;
    private double gps_lat;
    private string gps_lat_ref;
    private double gps_long;
    private string gps_long_ref;
    private double gps_alt;
    private string artist;
    private string copyright;
    private string software;
    private string exposure_bias;
    private string exposure_date;
    private string exposure_time;
    private bool is_raw;
    private string? development_path;
498
    private const string OSM_LINK_TEMPLATE = "https://www.openstreetmap.org/?mlat=%1$f&amp;mlon=%2$f#map=16/%1$f/%2$f";
499 500 501

    public ExtendedProperties() {
        base();
502
        grid.row_spacing = 6;
503 504 505 506 507 508 509 510
    }

    // Event stuff
    // nothing here which is not already shown in the BasicProperties but
    // comments, which are common, see below

    // common stuff
    private string comment;
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
    protected override void clear_properties() {
        base.clear_properties();

        file_path = "";
        development_path = "";
        is_raw = false;
        filesize = 0;
        original_dim = Dimensions(0, 0);
        camera_make = "";
        camera_model = "";
        flash = "";
        focal_length = "";
        gps_lat = -1;
        gps_lat_ref = "";
        gps_long = -1;
        gps_long_ref = "";
        artist = "";
        copyright = "";
        software = "";
        exposure_bias = "";
        exposure_date = "";
        exposure_time = "";
        comment = "";
    }
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
    protected override void get_single_properties(DataView view) {
        base.get_single_properties(view);

        DataSource source = view.get_source();
        if (source == null)
            return;

        if (source is MediaSource) {
            MediaSource media = (MediaSource) source;
            file_path = media.get_master_file().get_path();
            development_path = media.get_file().get_path();
            filesize = media.get_master_filesize();

            // as of right now, all extended properties other than filesize, filepath & comment aren't
            // applicable to non-photo media types, so if the current media source isn't a photo,
            // just do a short-circuit return
            Photo photo = media as Photo;
            if (photo == null)
                return;

            PhotoMetadata? metadata;

            try {
                // For some raw files, the developments may not contain metadata (please
                // see the comment about cameras generating 'crazy' exif segments in
                // Photo.develop_photo() for why), and so we'll want to display what was
                // in the original raw file instead.
                metadata = photo.get_master_metadata();
            } catch (Error e) {
                metadata = photo.get_metadata();
            }
568
            
569
            if (metadata == null)
570 571
                return;
            
572 573 574 575 576 577 578
            // Fix up any timestamp weirdness.
            //
            // If the exposure date wasn't properly set (the most likely cause of this
            // is a raw with a metadataless development), use the one from the photo
            // row.
            if (metadata.get_exposure_date_time() == null)
                metadata.set_exposure_date_time(new MetadataDateTime(photo.get_timestamp()));
579
            
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
            is_raw = (photo.get_master_file_format() == PhotoFileFormat.RAW);
            original_dim = metadata.get_pixel_dimensions();
            camera_make = metadata.get_camera_make();
            camera_model = metadata.get_camera_model();
            flash = metadata.get_flash_string();
            focal_length = metadata.get_focal_length_string();
            metadata.get_gps(out gps_long, out gps_long_ref, out gps_lat, out gps_lat_ref, out gps_alt);
            artist = metadata.get_artist();
            copyright = metadata.get_copyright();
            software = metadata.get_software();
            exposure_bias = metadata.get_exposure_bias();
            time_t exposure_time_obj = metadata.get_exposure_date_time().get_timestamp();
            exposure_date = get_prettyprint_date(Time.local(exposure_time_obj));
            exposure_time = get_prettyprint_time_with_seconds(Time.local(exposure_time_obj));
            comment = media.get_comment();
        } else if (source is EventSource) {
            Event event = (Event) source;
            comment = event.get_comment();
598
        }
599
    }
600

601 602
    public override void internal_update_properties(Page page) {
        base.internal_update_properties(page);
603

604 605 606 607 608
        if (page is EventsDirectoryPage) {
            // nothing special to be done for now for Events
        } else {
            add_line(_("Location:"), (file_path != "" && file_path != null) ?
                file_path.replace("&", "&amp;") : NO_VALUE);
609

610 611
            add_line(_("File size:"), (filesize > 0) ?
                format_size((int64) filesize) : NO_VALUE);
612

613 614
            if (is_raw)
                add_line(_("Current Development:"), development_path);
615

616 617
            add_line(_("Original dimensions:"), (original_dim != null && original_dim.has_area()) ?
                "%d &#215; %d".printf(original_dim.width, original_dim.height) : NO_VALUE);
618

619 620
            add_line(_("Camera make:"), (camera_make != "" && camera_make != null) ?
                camera_make : NO_VALUE);
621

622 623
            add_line(_("Camera model:"), (camera_model != "" && camera_model != null) ?
                camera_model : NO_VALUE);
624

625 626 627 628
            add_line(_("Flash:"), (flash != "" && flash != null) ? flash : NO_VALUE);

            add_line(_("Focal length:"), (focal_length != "" && focal_length != null) ?
                focal_length : NO_VALUE);
629
            
630 631
            add_line(_("Exposure date:"), (exposure_date != "" && exposure_date != null) ?
                exposure_date : NO_VALUE);
632
            
633 634
            add_line(_("Exposure time:"), (exposure_time != "" && exposure_time != null) ?
                exposure_time : NO_VALUE);
635

636
            add_line(_("Exposure bias:"), (exposure_bias != "" && exposure_bias != null) ? exposure_bias : NO_VALUE);
637

638 639 640
            string? osm_link = null;
            if (gps_lat != -1 && gps_lat_ref != "" && gps_long != -1 && gps_long_ref != "") {
                var old_locale = Intl.setlocale(LocaleCategory.NUMERIC, "C");
641
                osm_link = OSM_LINK_TEMPLATE.printf(gps_lat, gps_long);
642
                Intl.setlocale(LocaleCategory.NUMERIC, old_locale);
643 644
            }

645 646
            add_line(_("GPS latitude:"), (gps_lat != -1 && gps_lat_ref != "" &&
                gps_lat_ref != null) ? "%f °%s".printf(gps_lat, gps_lat_ref) : NO_VALUE, false, osm_link);
647

648 649
            add_line(_("GPS longitude:"), (gps_long != -1 && gps_long_ref != "" &&
                gps_long_ref != null) ? "%f °%s".printf(gps_long, gps_long_ref) : NO_VALUE, false, osm_link);
650

651
            add_line(_("Artist:"), (artist != "" && artist != null) ? artist : NO_VALUE);
652

653
            add_line(_("Copyright:"), (copyright != "" && copyright != null) ? copyright : NO_VALUE);
654

655
            add_line(_("Software:"), (software != "" && software != null) ? software : NO_VALUE);
656
        }
657

658 659
        bool has_comment = (comment != "" && comment != null);
        add_line(_("Comment:"), has_comment ? comment : NO_VALUE, has_comment);
660
    }
661

662
}