File indexing completed on 2024-04-28 15:39:42

0001 /* SPDX-FileCopyrightText: 2014 Jesper K. Pedersen <blackie@kde.org>
0002 
0003    SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "RemoteCommand.h"
0007 
0008 #include "Serializer.h"
0009 
0010 #include <QDebug>
0011 #include <QElapsedTimer>
0012 #include <QMap>
0013 #include <QPainter>
0014 #include <functional>
0015 #include <memory>
0016 
0017 using namespace RemoteControl;
0018 
0019 #define ENUMSTREAM(TYPE)                                     \
0020     QDataStream &operator<<(QDataStream &stream, TYPE type)  \
0021     {                                                        \
0022         stream << (qint32)type;                              \
0023         return stream;                                       \
0024     }                                                        \
0025                                                              \
0026     QDataStream &operator>>(QDataStream &stream, TYPE &type) \
0027     {                                                        \
0028         stream >> (qint32 &)type;                            \
0029         return stream;                                       \
0030     }
0031 
0032 ENUMSTREAM(ViewType)
0033 ENUMSTREAM(SearchType)
0034 ENUMSTREAM(ToggleTokenRequest::State)
0035 
0036 RemoteCommand::RemoteCommand(CommandType type)
0037     : m_type(type)
0038 {
0039 }
0040 
0041 RemoteCommand::~RemoteCommand()
0042 {
0043     qDeleteAll(m_serializers);
0044 }
0045 
0046 void RemoteCommand::encode(QDataStream &stream) const
0047 {
0048     for (SerializerInterface *serializer : m_serializers)
0049         serializer->encode(stream);
0050 }
0051 
0052 void RemoteCommand::decode(QDataStream &stream)
0053 {
0054     for (SerializerInterface *serializer : m_serializers)
0055         serializer->decode(stream);
0056 }
0057 
0058 CommandType RemoteCommand::commandType() const
0059 {
0060     return m_type;
0061 }
0062 
0063 void RemoteCommand::addSerializer(SerializerInterface *serializer)
0064 {
0065     m_serializers.append(serializer);
0066 }
0067 
0068 using CommandFacory = std::function<std::unique_ptr<RemoteCommand>()>;
0069 #define ADDFACTORY(COMMAND)                \
0070     factories.insert(CommandType::COMMAND, \
0071                      []() { return std::unique_ptr<RemoteCommand>(new COMMAND); })
0072 
0073 std::unique_ptr<RemoteCommand> RemoteCommand::create(CommandType id)
0074 {
0075     static QMap<CommandType, CommandFacory> factories;
0076     if (factories.isEmpty()) {
0077         ADDFACTORY(ThumbnailResult);
0078         ADDFACTORY(CategoryListResult);
0079         ADDFACTORY(SearchRequest);
0080         ADDFACTORY(SearchResult);
0081         ADDFACTORY(ThumbnailRequest);
0082         ADDFACTORY(ThumbnailCancelRequest);
0083         ADDFACTORY(TimeCommand);
0084         ADDFACTORY(ImageDetailsRequest);
0085         ADDFACTORY(ImageDetailsResult);
0086         ADDFACTORY(CategoryItemsResult);
0087         ADDFACTORY(StaticImageRequest);
0088         ADDFACTORY(StaticImageResult);
0089         ADDFACTORY(ToggleTokenRequest);
0090     }
0091     Q_ASSERT(factories.contains(id));
0092     return factories[id]();
0093 }
0094 
0095 ThumbnailResult::ThumbnailResult(ImageId _imageId, const QString &_label, const QImage &_image, ViewType _type)
0096     : RemoteCommand(CommandType::ThumbnailResult)
0097     , imageId(_imageId)
0098     , label(_label)
0099     , image(_image)
0100     , type(_type)
0101 {
0102     addSerializer(new Serializer<ImageId>(imageId));
0103     addSerializer(new Serializer<QString>(label));
0104     addSerializer(new Serializer<QImage>(image));
0105     addSerializer(new Serializer<ViewType>(type));
0106 }
0107 
0108 QDataStream &operator<<(QDataStream &stream, const Category &category)
0109 {
0110     stream << category.name << category.enabled << (int)category.viewType;
0111     fastStreamImage(stream, category.icon, BackgroundType::Transparent);
0112     return stream;
0113 }
0114 
0115 QDataStream &operator>>(QDataStream &stream, Category &category)
0116 {
0117     int tmp;
0118     stream >> category.name >> category.enabled >> tmp;
0119     category.viewType = static_cast<RemoteControl::CategoryViewType>(tmp);
0120     category.icon.load(stream.device(), "JPEG");
0121     return stream;
0122 }
0123 
0124 CategoryListResult::CategoryListResult()
0125     : RemoteCommand(CommandType::CategoryListResult)
0126 {
0127     addSerializer(new Serializer<QList<Category>>(categories));
0128 }
0129 
0130 SearchRequest::SearchRequest(SearchType _type, const SearchInfo &_searchInfo, int _size)
0131     : RemoteCommand(CommandType::SearchRequest)
0132     , type(_type)
0133     , searchInfo(_searchInfo)
0134     , size(_size)
0135 {
0136     addSerializer(new Serializer<SearchType>(type));
0137     addSerializer(new Serializer<SearchInfo>(searchInfo));
0138     addSerializer(new Serializer<int>(size));
0139 }
0140 
0141 SearchResult::SearchResult(SearchType _type, const QList<int> &_result)
0142     : RemoteCommand(CommandType::SearchResult)
0143     , type(_type)
0144     , result(_result)
0145 {
0146     addSerializer(new Serializer<SearchType>(type));
0147     addSerializer(new Serializer<QList<int>>(result));
0148 }
0149 
0150 ThumbnailRequest::ThumbnailRequest(ImageId _imageId, const QSize &_size, ViewType _type)
0151     : RemoteCommand(CommandType::ThumbnailRequest)
0152     , imageId(_imageId)
0153     , size(_size)
0154     , type(_type)
0155 {
0156     addSerializer(new Serializer<ImageId>(imageId));
0157     addSerializer(new Serializer<QSize>(size));
0158     addSerializer(new Serializer<ViewType>(type));
0159 }
0160 
0161 RemoteControl::ThumbnailCancelRequest::ThumbnailCancelRequest(ImageId _imageId, ViewType _type)
0162     : RemoteCommand(CommandType::ThumbnailCancelRequest)
0163     , imageId(_imageId)
0164     , type(_type)
0165 {
0166     addSerializer(new Serializer<ImageId>(imageId));
0167     addSerializer(new Serializer<ViewType>(type));
0168 }
0169 
0170 TimeCommand::TimeCommand()
0171     : RemoteCommand(CommandType::TimeCommand)
0172 {
0173 }
0174 
0175 static void printElapsed()
0176 {
0177     static QElapsedTimer timer;
0178     qDebug() << "Time since last dump: " << timer.elapsed();
0179     timer.restart();
0180 }
0181 
0182 void TimeCommand::encode(QDataStream &) const
0183 {
0184     printElapsed();
0185 }
0186 
0187 void TimeCommand::decode(QDataStream &)
0188 {
0189     printElapsed();
0190 }
0191 
0192 ImageDetailsRequest::ImageDetailsRequest(ImageId _imageId)
0193     : RemoteCommand(CommandType::ImageDetailsRequest)
0194     , imageId(_imageId)
0195 {
0196     addSerializer(new Serializer<ImageId>(imageId));
0197 }
0198 
0199 ImageDetailsResult::ImageDetailsResult()
0200     : RemoteCommand(CommandType::ImageDetailsResult)
0201 {
0202     addSerializer(new Serializer<QString>(fileName));
0203     addSerializer(new Serializer<QString>(date));
0204     addSerializer(new Serializer<QString>(description));
0205     addSerializer(new Serializer<QMap<QString, CategoryItemDetailsList>>(categories));
0206 }
0207 
0208 //// WHAT WHAT WHAT
0209 QDataStream &operator<<(QDataStream &stream, const CategoryItemDetails &item)
0210 {
0211     stream << item.name << item.age;
0212     return stream;
0213 }
0214 
0215 QDataStream &operator>>(QDataStream &stream, CategoryItemDetails &item)
0216 {
0217     stream >> item.name >> item.age;
0218     return stream;
0219 }
0220 
0221 CategoryItemsResult::CategoryItemsResult(const QStringList &_items)
0222     : RemoteCommand(CommandType::CategoryItemsResult)
0223     , items(_items)
0224 {
0225     addSerializer(new Serializer<QStringList>(items));
0226 }
0227 
0228 StaticImageRequest::StaticImageRequest(int _size)
0229     : RemoteCommand(CommandType::StaticImageRequest)
0230     , size(_size)
0231 {
0232     addSerializer(new Serializer<int>(size));
0233 }
0234 
0235 StaticImageResult::StaticImageResult(const QImage &_homeIcon, const QImage &_kphotoalbumIcon, const QImage &_discoverIcon)
0236     : RemoteCommand(CommandType::StaticImageResult)
0237     , homeIcon(_homeIcon)
0238     , kphotoalbumIcon(_kphotoalbumIcon)
0239     , discoverIcon(_discoverIcon)
0240 {
0241     addSerializer(new Serializer<QImage>(homeIcon, BackgroundType::Transparent));
0242     addSerializer(new Serializer<QImage>(kphotoalbumIcon, BackgroundType::Transparent));
0243     addSerializer(new Serializer<QImage>(discoverIcon, BackgroundType::Transparent));
0244 }
0245 
0246 ToggleTokenRequest::ToggleTokenRequest(ImageId _imageId, const QString &_token, State _state)
0247     : RemoteCommand(CommandType::ToggleTokenRequest)
0248     , imageId(_imageId)
0249     , token(_token)
0250     , state(_state)
0251 {
0252     addSerializer(new Serializer<ImageId>(imageId));
0253     addSerializer(new Serializer<QString>(token));
0254     addSerializer(new Serializer<State>(state));
0255 }