File indexing completed on 2024-12-29 05:06:03

0001 // SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "foliodelegate.h"
0005 #include "homescreenstate.h"
0006 
0007 FolioDelegate::FolioDelegate(QObject *parent)
0008     : QObject{parent}
0009     , m_type{FolioDelegate::None}
0010     , m_application{nullptr}
0011     , m_folder{nullptr}
0012     , m_widget{nullptr}
0013 {
0014 }
0015 
0016 FolioDelegate::FolioDelegate(FolioApplication *application, QObject *parent)
0017     : QObject{parent}
0018     , m_type{FolioDelegate::Application}
0019     , m_application{application}
0020     , m_folder{nullptr}
0021     , m_widget{nullptr}
0022 {
0023 }
0024 
0025 FolioDelegate::FolioDelegate(FolioApplicationFolder *folder, QObject *parent)
0026     : QObject{parent}
0027     , m_type{FolioDelegate::Folder}
0028     , m_application{nullptr}
0029     , m_folder{folder}
0030     , m_widget{nullptr}
0031 {
0032 }
0033 
0034 FolioDelegate::FolioDelegate(FolioWidget *widget, QObject *parent)
0035     : QObject{parent}
0036     , m_type{FolioDelegate::Widget}
0037     , m_application{nullptr}
0038     , m_folder{nullptr}
0039     , m_widget{widget}
0040 {
0041 }
0042 
0043 FolioDelegate *FolioDelegate::fromJson(QJsonObject &obj, QObject *parent)
0044 {
0045     const QString type = obj[QStringLiteral("type")].toString();
0046     if (type == "application") {
0047         // read application
0048         FolioApplication *app = FolioApplication::fromJson(obj, parent);
0049 
0050         if (app) {
0051             return new FolioDelegate{app, parent};
0052         }
0053 
0054     } else if (type == "folder") {
0055         // read folder
0056         FolioApplicationFolder *folder = FolioApplicationFolder::fromJson(obj, parent);
0057 
0058         if (folder) {
0059             return new FolioDelegate{folder, parent};
0060         }
0061 
0062     } else if (type == "widget") {
0063         // read widget
0064         FolioWidget *widget = FolioWidget::fromJson(obj, parent);
0065 
0066         if (widget) {
0067             return new FolioDelegate{widget, parent};
0068         }
0069     } else if (type == "none") {
0070         return new FolioDelegate{parent};
0071     }
0072 
0073     return nullptr;
0074 }
0075 
0076 QJsonObject FolioDelegate::toJson() const
0077 {
0078     switch (m_type) {
0079     case FolioDelegate::Application:
0080         return m_application->toJson();
0081     case FolioDelegate::Folder:
0082         return m_folder->toJson();
0083     case FolioDelegate::Widget:
0084         return m_widget->toJson();
0085     case FolioDelegate::None: {
0086         QJsonObject obj;
0087         obj[QStringLiteral("type")] = "none";
0088         return obj;
0089     }
0090     default:
0091         break;
0092     }
0093     return QJsonObject{};
0094 }
0095 
0096 FolioDelegate::Type FolioDelegate::type()
0097 {
0098     return m_type;
0099 }
0100 
0101 FolioApplication *FolioDelegate::application()
0102 {
0103     return m_application;
0104 }
0105 
0106 FolioApplicationFolder *FolioDelegate::folder()
0107 {
0108     return m_folder;
0109 }
0110 
0111 FolioWidget *FolioDelegate::widget()
0112 {
0113     return m_widget;
0114 }