transferdbrecord.cpp 7.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (c) 2013 - 2019 Jolla Ltd.
 *
 * All rights reserved.
 *
 * This file is part of Sailfish Transfer Engine package.
 *
 * You may use this file under the terms of the GNU Lesser General
 * Public License version 2.1 as published by the Free Software Foundation
 * and appearing in the file license.lgpl included in the packaging
 * of this file.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * and appearing in the file license.lgpl included in the packaging
 * of this file.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

#include "transferdbrecord.h"
#include "metatypedeclarations.h"

#include <QtDBus>

/*!
    \class TransferDBRecord
    \brief The TransferDBRecord class is a wrapper class for TransferEngine DBus message.

    \ingroup transfer-engine-lib

    This class wraps transfer method related information and is used to pass that information
    over the DBus to the any client who is interested in about it.

    A single instance of TransferDBRecord contains information of a one transfer record in TransferEngine
    database. Usually clients don't need to fill any data to this class, instead they can request a list
    of TransferDBRecords from the TransferEngine and access its data via value() method.
 */

/*!
    \enum TransferDBRecord::TransferDBRecordField

    An enum for accessing TrasnferDBRecord values using value() method

    \value TransferID Id of the transfer
    \value TransferType Type of the transfer (Upload, Download, Sync
    \value Progress Progress of the transfer
    \value URL Url of the media related to the transfer
    \value Status The status of the transfer
    \value PluginID The id of the plugin which is handling the transfer
    \value Timestamp The timestamp for the transfer
    \value DisplayName The display name for the transfer
    \value ResourceName The name of the resource
    \value MimeType MimeType information of the media being transfered
    \value FileSize File size of the media being transfered
    \value ServiceIcon Icon url for a service related to the transfer
    \value ApplicationIcon Application icon url which has created e.g. sync or download transfer
    \value ThumbnailIcon Thumbnail url
    \value CancelSupported Boolean to indicate if cancel is supported
    \value RestartSupported Boolean to indicate if cancel is supported
*/

TransferDBRecord::TransferDBRecord()
{
}

/*!
    Assigns \a other to this transfer db record and returns a reference to this transfer db record.
 */
TransferDBRecord &TransferDBRecord::operator=(const TransferDBRecord &other)
{
    transfer_id     = other.transfer_id;
    transfer_type   = other.transfer_type;
    status          = other.status;
    size            = other.size;
    progress        = other.progress;
    plugin_id       = other.plugin_id;
    url             = other.url;
    timestamp       = other.timestamp;
    display_name    = other.display_name;
    resource_name   = other.resource_name;
    mime_type       = other.mime_type;
    service_icon    = other.service_icon;
    application_icon= other.application_icon;
    thumbnail_icon  = other.thumbnail_icon;
    cancel_supported = other.cancel_supported;
    restart_supported = other.restart_supported;
    return *this;
}

/*!
    Constructs a copy of \a other.
 */
TransferDBRecord::TransferDBRecord(const TransferDBRecord &other):
    transfer_id(other.transfer_id),
    transfer_type(other.transfer_type),
    status(other.status),
    size(other.size),
    progress(other.progress),
    plugin_id(other.plugin_id),
    url(other.url),
    timestamp(other.timestamp),
    display_name(other.display_name),
    resource_name(other.resource_name),
    mime_type(other.mime_type),
    service_icon(other.service_icon),
    application_icon(other.application_icon),
    thumbnail_icon(other.thumbnail_icon),
    cancel_supported(other.cancel_supported),
    restart_supported(other.restart_supported)
{
}

TransferDBRecord::~TransferDBRecord()
{
}

/*!
    Writes the given \a record to specified \a argument.
*/
QDBusArgument &operator<<(QDBusArgument &argument, const TransferDBRecord &record)
{
    argument.beginStructure();

    argument << record.transfer_id
             << record.transfer_type
             << record.status
             << record.size
             << record.progress
             << record.plugin_id
             << record.url
             << record.timestamp
             << record.display_name
             << record.resource_name
             << record.mime_type
             << record.service_icon
             << record.application_icon
             << record.thumbnail_icon
             << record.cancel_supported
             << record.restart_supported;
    argument.endStructure();
    return argument;
}


/*!
    Reads the given \a argument and stores it to the specified \a record.
*/
const QDBusArgument &operator>>(const QDBusArgument &argument, TransferDBRecord &record)
{
    argument.beginStructure();
    argument >> record.transfer_id
             >> record.transfer_type
             >> record.status
             >> record.size
             >> record.progress
             >> record.plugin_id
             >> record.url
             >> record.timestamp
             >> record.display_name
             >> record.resource_name
             >> record.mime_type
             >> record.service_icon
             >> record.application_icon
             >> record.thumbnail_icon
             >> record.cancel_supported
             >> record.restart_supported;
    argument.endStructure();
    return argument;
}

/*!
    Registers TransferDBRecord and QList<TransferDBRecord> as DBus types.
 */
void TransferDBRecord::registerType()
{
    qDBusRegisterMetaType<TransferDBRecord>();
    qDBusRegisterMetaType<QList<TransferDBRecord> >();
}

/*!
    Returns any of the TransferDBRecord values based on the index. As an \a index it's recommended to use
    TransferDBRecord::TransferDBRecordField enum.
*/
QVariant TransferDBRecord::value(int index) const
{
    switch(index) {
    case TransferID:
        return transfer_id;

    case TransferType:
        return transfer_type;

    case Progress:
        return progress;

    case URL:
        return url;

    case Status:
        return status;

    case PluginID:
        return plugin_id;

    case Timestamp:
        return timestamp;

    case DisplayName:
        return display_name;

    case ResourceName:
        return resource_name;

    case MimeType:
        return mime_type;

    case FileSize:
        return size;

    case ServiceIcon:
        return service_icon;

    case ApplicationIcon:
        return application_icon;

    case ThumbnailIcon:
        return thumbnail_icon;

    case CancelSupported:
        return cancel_supported;

    case  RestartSupported:
        return restart_supported;

    default:
241
        qWarning() << Q_FUNC_INFO << "Unknown index: " << index;
242 243 244
        return QVariant();
    }
}
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

bool TransferDBRecord::isValid() const
{
    return transfer_id > 0 && transfer_type > 0;
}

bool operator ==(const TransferDBRecord &left, const TransferDBRecord &right)
{
    return left.transfer_id == right.transfer_id &&
           left.transfer_type == right.transfer_type &&
           left.status == right.status &&
           left.progress == right.progress;
}

bool operator !=(const TransferDBRecord &left, const TransferDBRecord &right)
{
    return left.transfer_id != right.transfer_id ||
            left.transfer_type != right.transfer_type ||
            left.status != right.status ||
            left.progress != right.progress;
}