File indexing completed on 2024-04-21 04:59:27

0001 // SPDX-FileCopyrightText: 2022 Bharadwaj Raju <bharadwaj.raju777@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-or-later OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #pragma once
0005 
0006 #include <QObject>
0007 #include <QQmlEngine>
0008 #include <QUrl>
0009 
0010 namespace Quotient
0011 {
0012 class RoomMessageEvent;
0013 }
0014 
0015 class NeoChatRoom;
0016 
0017 /**
0018  * @class LinkPreviewer
0019  *
0020  * A class to download the link preview info for a URL and provide a QML interface for it.
0021  *
0022  * To use set the URL property and then access the other parameters which will be
0023  * populated once loaded is true.
0024  */
0025 class LinkPreviewer : public QObject
0026 {
0027     Q_OBJECT
0028     QML_ELEMENT
0029 
0030     /**
0031      * @brief The URL to get the preview for.
0032      */
0033     Q_PROPERTY(QUrl url READ url NOTIFY urlChanged)
0034 
0035     /**
0036      * @brief Whether the preview information has been loaded.
0037      */
0038     Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged)
0039 
0040     /**
0041      * @brief The title of the preview.
0042      */
0043     Q_PROPERTY(QString title READ title NOTIFY titleChanged)
0044 
0045     /**
0046      * @brief The description of the preview.
0047      */
0048     Q_PROPERTY(QString description READ description NOTIFY descriptionChanged)
0049 
0050     /**
0051      * @brief The image source for the preview.
0052      */
0053     Q_PROPERTY(QUrl imageSource READ imageSource NOTIFY imageSourceChanged)
0054 
0055     /**
0056      * @brief Whether the there is a link to preview.
0057      *
0058      * A linkPreviwer is empty if the URL is empty.
0059      */
0060     Q_PROPERTY(bool empty READ empty NOTIFY emptyChanged)
0061 
0062 public:
0063     explicit LinkPreviewer(const NeoChatRoom *room = nullptr, const Quotient::RoomMessageEvent *event = nullptr);
0064 
0065     [[nodiscard]] QUrl url() const;
0066     [[nodiscard]] bool loaded() const;
0067     [[nodiscard]] QString title() const;
0068     [[nodiscard]] QString description() const;
0069     [[nodiscard]] QUrl imageSource() const;
0070     [[nodiscard]] bool empty() const;
0071 
0072     /**
0073      * @brief Whether the given event has at least 1 pre-viewable link.
0074      *
0075      * A link is only pre-viewable if it is http, https or something starting with www.
0076      */
0077     static bool hasPreviewableLinks(const Quotient::RoomMessageEvent *event);
0078 
0079 private:
0080     const NeoChatRoom *m_currentRoom;
0081     const Quotient::RoomMessageEvent *m_event;
0082 
0083     bool m_loaded;
0084     QString m_title = QString();
0085     QString m_description = QString();
0086     QUrl m_imageSource = QUrl();
0087     QUrl m_url;
0088 
0089     void loadUrlPreview();
0090 
0091     /**
0092      * @brief Return the link to be previewed from the given event.
0093      *
0094      * This function is designed to give only links that should be previewed so
0095      * http, https or something starting with www. The first valid link is returned.
0096      */
0097     static QUrl linkPreview(const Quotient::RoomMessageEvent *event);
0098 
0099 Q_SIGNALS:
0100     void loadedChanged();
0101     void titleChanged();
0102     void descriptionChanged();
0103     void imageSourceChanged();
0104     void urlChanged();
0105     void emptyChanged();
0106 };
0107 Q_DECLARE_METATYPE(LinkPreviewer *)