aboutsummaryrefslogtreecommitdiffstats
path: root/src/activitypub/apattachment.cpp
blob: 83d0bea53a85b42966a6a47d1c92a0616a1da333 (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
#include "apattachment.h"
#include <QFileInfo>
#include <QDebug>
#include <memory>

APAttachment::APAttachment() {}

APAttachment::APAttachment(APAttachmentFields fields) {
    path_url = fields.path;
    type = fields.media_type;
    description = fields.name;
}

QString APAttachment::get_html_render(HtmlRenderDetails info) {
    QString html(get_html_template(QStringLiteral("apattachment")));

    if (not path_url.isEmpty()) {
        html.replace("{{path}}", path_url);
        html.replace("{{filename}}", QFileInfo(path_url).fileName()); // FIXME: this is maybe ugly?
    }

    if (type.startsWith("image/"))
        // dynamically resize image based on the display widget size to avoid horizontal scrolling
        html.replace("{{img-width}}", QString::number((float)info.text_zone_width - (float)info.text_zone_width*0.15)); // finally after passing this for so long i finally use it
    else
        html.replace("{{img-width}}", "0");

    html.replace("{{alt-text}}", description);

    return html;
}

std::shared_ptr<QPixmap> APAttachment::get_pixmap(int width, int height) {
    // Recreating the pixmap if it doesn't have the requested dimensions is quite inefficent. So it's better for the callee to scale elsewhere the pixmap instead of relying on this function, if possible.
    if (pixmap and pixmap_dimens.is_equal(width, height)) return pixmap;
    else if (pixmap)
        pixmap.reset();

    // pixmap_dimens are correctly based on the requested/function argument width and height, and not the actual dimentions of the pixmap!
    pixmap_dimens.height = height;
    pixmap_dimens.width = width;

    if (width > 0 or height > 0) {
        QPixmap image(path_url);

        /* proportionality rule:
         * width     image width
         * -----  =  -----------
         * height    image height
         */
        if (height == 0)
            height = (width * image.height()) / image.width();
        else if (width == 0)
            width = (height * image.width()) / image.height();

        pixmap = std::make_shared<QPixmap>(image.scaled(QSize(width, height)));
    } else {
        pixmap = std::make_shared<QPixmap>();
        if (not pixmap->load(path_url))
            qDebug() << "failed to load" << path_url;
    }
    return pixmap;
}

QString APAttachmentList::get_html_render(HtmlRenderDetails render_info) {
    QString html;

    int i = 1;
    for (APAttachment attachment : *this) {
        QString item_html(get_html_template(QStringLiteral("apattachmentlist_item")));
        item_html.replace("{{id}}", QString::number(i));
        item_html.replace("{{attachment}}", attachment.get_html_render(render_info));

        html.append(item_html);
        ++i;
    }

    return html;
}