transferengine.cpp 53 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 24
/*
 * Copyright (c) 2013 - 2019 Jolla Ltd.
 * Copyright (c) 2019 Open Mobile Platform LLC.
 *
 * 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.
 */
25 26 27 28 29 30

#include "transferengine.h"
#include "transferengine_p.h"
#include "transferplugininterface.h"
#include "mediaitem.h"
#include "dbmanager.h"
31
#include "logging.h"
32
#include "transferengine_adaptor.h"
33 34 35 36 37 38 39 40 41
#include "transfertypes.h"
#include "transferplugininfo.h"

#include <QDir>
#include <QtDebug>
#include <QPluginLoader>
#include <QDBusMessage>
#include <QFileSystemWatcher>
#include <QTimer>
Pekka Vuorela's avatar
Pekka Vuorela committed
42
#include <QSettings>
43

44
#include <notification.h>
45 46 47

#include <signal.h>

48 49
#include <Accounts/Manager>

50
#define SHARE_PLUGINS_PATH "/usr/lib/nemo-transferengine/plugins"
51
#define CONFIG_PATH "/usr/share/nemo-transferengine/nemo-transfer-engine.conf"
52
#define FILE_WATCHER_TIMEOUT 5000
53
#define ACTIVITY_MONITOR_TIMEOUT 1*60*1000 // 1 minute in ms
54
#define TRANSFER_EXPIRATION_THRESHOLD 3*60 // 3 minutes in seconds
55

56 57 58 59 60
#define TRANSFER_EVENT_CATEGORY "transfer"
#define TRANSFER_COMPLETE_EVENT_CATEGORY "transfer.complete"
#define TRANSFER_ERROR_EVENT_CATEGORY "transfer.error"

#define TRANSFER_PROGRESS_HINT "x-nemo-progress"
61

62 63 64 65 66 67 68 69
TransferEngineSignalHandler * TransferEngineSignalHandler::instance()
{
    static TransferEngineSignalHandler instance;
    return &instance;
}

void TransferEngineSignalHandler::signalHandler(int signal)
{
Pekka Vuorela's avatar
Pekka Vuorela committed
70
    if (signal == SIGUSR1) {
71 72 73 74 75 76 77 78 79 80 81
        QMetaObject::invokeMethod(TransferEngineSignalHandler::instance(),
                                  "exitSafely",
                                  Qt::AutoConnection);
    }
}

TransferEngineSignalHandler::TransferEngineSignalHandler()
{
    signal(SIGUSR1, TransferEngineSignalHandler::signalHandler);
}

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
// ---------------------------

// ClientActivityMonitor runs periodic checks if there are transfers which are expired.
// A transfer can be expired e.g. when a client has been crashed in the middle of Sync,
// Download or Upload operation or the client API isn't used properly.
//
// NOTE: This class only monitors if there are expired transfers and emit signal to indicate
// that it's cleaning time.  It is up to Transfer Engine to remoce expired ids from the
// ClientActivityMonitor instance.
ClientActivityMonitor::ClientActivityMonitor(QObject *parent)
    : QObject(parent)
    , m_timer(new QTimer(this))
{
    connect(m_timer, SIGNAL(timeout()), this, SLOT(checkActivity()));
    m_timer->start(ACTIVITY_MONITOR_TIMEOUT);
}

ClientActivityMonitor::~ClientActivityMonitor()
{
    m_activityMap.clear();
}

void ClientActivityMonitor::newActivity(int transferId)
{
    // Update or add a new timestamp
    m_activityMap.insert(transferId, QDateTime::currentDateTimeUtc().toTime_t());
}

void ClientActivityMonitor::activityFinished(int transferId)
{
    if (!m_activityMap.contains(transferId)) {
113
        qCWarning(lcTransferLog) << Q_FUNC_INFO << "Could not find matching TransferId. This is probably an error!";
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        return;
    }

    m_activityMap.remove(transferId);
}

bool ClientActivityMonitor::activeTransfers() const
{
    return !m_activityMap.isEmpty();
}

void ClientActivityMonitor::checkActivity()
{
    // Check if there are existing transfers which are not yet finished and
    // they've been around too long. Notify TransferEngine about these transfers.
    QList<int> ids;
130
    quint32 currTime = QDateTime::currentDateTimeUtc().toTime_t();
131 132 133 134 135 136 137 138 139 140 141 142 143
    QMap<int, quint32>::const_iterator i = m_activityMap.constBegin();
    while (i != m_activityMap.constEnd()) {
        if ((currTime - i.value()) >= TRANSFER_EXPIRATION_THRESHOLD) {
            ids << i.key();
        }
        i++;
    }

    if (!ids.isEmpty()) {
        emit transfersExpired(ids);
    }
}

144 145 146 147 148 149 150 151 152 153 154

// ----------------------------

TransferEnginePrivate::TransferEnginePrivate(TransferEngine *parent):
    m_notificationsEnabled(true),
    q_ptr(parent)
{
    m_fileWatcherTimer = new QTimer(this);
    m_fileWatcherTimer->setSingleShot(true);
    connect(m_fileWatcherTimer, SIGNAL(timeout()), this, SLOT(enabledPluginsCheck()));

155 156
    m_delayedExitTimer = new QTimer(this);
    m_delayedExitTimer->setSingleShot(true);
157 158
    m_delayedExitTimer->setInterval(60000);
    m_delayedExitTimer->start(); // Exit if nothing happens within 60 sec
159 160
    connect(m_delayedExitTimer, SIGNAL(timeout()), this, SLOT(delayedExitSafely()));

161 162 163 164 165 166 167 168 169
    m_fileWatcher = new QFileSystemWatcher(this);
    m_fileWatcher->addPath(SHARE_PLUGINS_PATH);
    connect(m_fileWatcher, SIGNAL(directoryChanged(QString)), this, SLOT(pluginDirChanged()));

    m_accountManager = new Accounts::Manager("sharing", this);
    connect(m_accountManager, SIGNAL(accountCreated(Accounts::AccountId)), this, SLOT(enabledPluginsCheck()));
    connect(m_accountManager, SIGNAL(accountRemoved(Accounts::AccountId)), this, SLOT(enabledPluginsCheck()));
    connect(m_accountManager, SIGNAL(accountUpdated(Accounts::AccountId)), this, SLOT(enabledPluginsCheck()));

170
    // Exit safely stuff if we recieve certain signal or there are no active transfers
171
    Q_Q(TransferEngine);
172 173 174 175 176 177
    connect(TransferEngineSignalHandler::instance(), SIGNAL(exitSafely()), this, SLOT(exitSafely()));
    connect(q, SIGNAL(statusChanged(int,int)), this, SLOT(exitSafely()));

    // Monitor expired transfers and cleanup them if required
    m_activityMonitor = new ClientActivityMonitor(this);
    connect(m_activityMonitor, SIGNAL(transfersExpired(QList<int>)), this, SLOT(cleanupExpiredTransfers(QList<int>)));
Pekka Vuorela's avatar
Pekka Vuorela committed
178 179 180 181

    QSettings settings(CONFIG_PATH, QSettings::IniFormat);

    if (settings.status() != QSettings::NoError) {
182
        qCWarning(lcTransferLog) << Q_FUNC_INFO << "Failed to read settings!" << settings.status();
Pekka Vuorela's avatar
Pekka Vuorela committed
183 184 185 186 187 188 189 190 191 192 193 194 195
    } else {
        settings.beginGroup("transfers");
        const QString service = settings.value("service").toString();
        const QString path = settings.value("path").toString();
        const QString iface = settings.value("interface").toString();
        const QString method = settings.value("method").toString();
        settings.endGroup();

        if (!service.isEmpty() && !path.isEmpty() && !iface.isEmpty() && !method.isEmpty()) {
            m_defaultActions << Notification::remoteAction("default", "", service, path, iface, method)
                             << Notification::remoteAction("app", "", service, path, iface, method);
        }
    }
196 197 198 199 200 201 202 203 204 205 206 207
}

void TransferEnginePrivate::pluginDirChanged()
{
    // We need to check our plugins, but only after a short period of time
    // because some operations may cause calling this slot over 10 times.
    // E.g. reinstallation of plugins from the RPM package
    m_fileWatcherTimer->start(FILE_WATCHER_TIMEOUT);
}

void TransferEnginePrivate::exitSafely()
{
208
    if (!m_activityMonitor->activeTransfers()) {
209
        qCDebug(lcTransferLog) << "Scheduling exit in" << m_delayedExitTimer->interval() << "ms";
210 211 212
        m_delayedExitTimer->start();
    } else {
        m_delayedExitTimer->stop();
213 214 215 216 217
    }
}

void TransferEnginePrivate::delayedExitSafely()
{
218
    if (getenv("TRANSFER_ENGINE_KEEP_RUNNING")) {
219
        qCDebug(lcTransferLog) << "Keeping transfer engine running";
220
    } else {
221
        qCDebug(lcTransferLog) << "Stopping transfer engine";
222 223
        qApp->exit();
    }
224 225 226 227 228 229 230 231 232
}

void TransferEnginePrivate::enabledPluginsCheck()
{
    Q_Q(TransferEngine);
    if (m_fileWatcherTimer->isActive()) {
        m_fileWatcherTimer->stop();
    }

233
    if (m_infoObjects.count() > 0) {
234
        qCWarning(lcTransferLog) << Q_FUNC_INFO << "Already quering account info" << m_infoObjects.count();
235 236 237
        return;
    }

238 239
    // First clear old data
    m_enabledPlugins.clear();
240
    m_pluginMetaData.clear();
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    qDeleteAll(m_infoObjects);
    m_infoObjects.clear();

    QPluginLoader loader;
    loader.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint);

    // Handle the case if all the plugins have been removed.
    QStringList plugins = pluginList();
    if (plugins.isEmpty()) {
        emit q->transferMethodListChanged();
        return;
    }

    // We have plugins
    Q_FOREACH(QString plugin, plugins) {
        loader.setFileName(plugin);
        TransferPluginInterface *interface =
                qobject_cast<TransferPluginInterface*>(loader.instance());

        if (interface && interface->enabled()) {

            TransferPluginInfo *info = interface->infoObject();
            if (!info) {
264
                qCWarning(lcTransferLog) << Q_FUNC_INFO << "NULL Info object!";
265 266 267
                continue;
            }

268 269 270 271
            if (info->ready()) {
                if (info->info().count() > 0) {
                    m_enabledPlugins << info->info();
                }
272
                m_pluginMetaData << info->metaData();
273
                delete info;
274 275 276 277 278 279 280
            } else {
                // These object will be cleaned in pluginInfoReady() slot.
                m_infoObjects << info;
                connect(info, SIGNAL(infoReady()), this, SLOT(pluginInfoReady()));
                connect(info, SIGNAL(infoError(QString)), this, SLOT(pluginInfoError(QString)));
                info->query();
            }
281 282 283
        }

        if (!interface) {
284
            qCWarning(lcTransferLog) << Q_FUNC_INFO << loader.errorString();
285 286
        }
    }
287
}
288

289 290 291 292 293 294 295 296 297 298 299 300
void TransferEnginePrivate::cleanupExpiredTransfers(const QList<int> &expiredIds)
{
    // Clean up expired items from the database by changing the status to TransferInterrupted. This way
    // database doesn't maintain objects with unifinished state and failed items can be cleaned by the
    // user manually from the UI.
    Q_Q(TransferEngine);
    Q_FOREACH(int id, expiredIds) {
        if (DbManager::instance()->updateTransferStatus(id, TransferEngineData::TransferInterrupted)) {
            m_activityMonitor->activityFinished(id);
            emit q->statusChanged(id, TransferEngineData::TransferInterrupted);
        }
    }
301 302 303 304 305 306 307
}

void TransferEnginePrivate::recoveryCheck()
{
    QList<TransferDBRecord> records = DbManager::instance()->transfers();
    // Check all transfer which are not properly finished and mark those as
    //  interrupted
308
    Q_Q(TransferEngine);
309 310 311
    Q_FOREACH(TransferDBRecord record, records) {
        if (record.status == TransferEngineData::TransferStarted ||
            record.status == TransferEngineData::NotStarted) {
312 313
            if (DbManager::instance()->updateTransferStatus(record.transfer_id, TransferEngineData::TransferInterrupted)) {
                emit q->statusChanged(record.transfer_id, TransferEngineData::TransferInterrupted);
314 315 316
                if (record.status == TransferEngineData::TransferStarted) {
                    emit q_ptr->activeTransfersChanged(); // It's not active anymore
                }
317
            }
318 319 320 321 322 323
        }
    }
}

void TransferEnginePrivate::sendNotification(TransferEngineData::TransferType type,
                                             TransferEngineData::TransferStatus status,
324 325 326
                                             qreal progress,
                                             const QString &fileName,
                                             int transferId)
327
{
328
    if (!m_notificationsEnabled || fileName.isEmpty()) {
329 330 331
        return;
    }

332 333 334 335 336
    QString category;
    QString body;
    QString summary;
    QString previewBody;
    QString previewSummary;
337 338 339
    bool useProgress = false;
    Notification::Urgency urgency = Notification::Normal;
    QString appIcon = QStringLiteral("icon-lock-information");
340
    QString icon = QStringLiteral("icon-lock-transfer");
341

342 343 344
    // TODO: explicit grouping of transfer notifications is now removed, as grouping
    // will now be performed by lipstick.  We may need to reinstate group summary
    // notifications at some later point...
345

346 347 348 349 350 351 352 353 354 355 356
    // Notification & Banner rules:
    //
    // Show Banner:
    // - For succesfull uploads and for downloads
    // - For failed Uploads, Downloads, Syncs
    //
    // Show an event in the EventView:
    // - For downloads
    // - For failed uploads, downloads and syncs


357
    int notificationId = DbManager::instance()->notificationId(transferId);
358

359 360 361 362 363
    if (status == TransferEngineData::TransferFinished) {
        switch(type) {
        case TransferEngineData::Upload:
            //: Notification for successful file upload
            //% "File uploaded"
364 365 366
            previewBody = qtTrId("transferengine-no-file-upload-success");
            previewSummary = fileName;
            category = TRANSFER_EVENT_CATEGORY; // Use "generic" transfer event for uploads
367 368
            break;
        case TransferEngineData::Download:
369
            category = TRANSFER_COMPLETE_EVENT_CATEGORY;
370 371
            //: Notification for successful file download
            //% "File downloaded"
372 373
            body = qtTrId("transferengine-no-file-download-success");
            summary = fileName;
374 375
            previewBody = body;
            previewSummary = summary;
376 377 378
            break;
        case TransferEngineData::Sync:
            // Ok exit
379
            break;
380
        default:
381
            qCWarning(lcTransferLog) << "TransferEnginePrivate::sendNotification: unknown state";
382
            break;
383
        }
384

385
    } else if (status == TransferEngineData::TransferInterrupted) {
386 387
        urgency = Notification::Critical;
        appIcon = QStringLiteral("icon-lock-information");
388
        category = TRANSFER_ERROR_EVENT_CATEGORY;
389
        icon.clear();
390

391 392 393
        switch (type) {
        case TransferEngineData::Upload:
            //: Notification for failed file upload
394 395
            //% "Upload failed"
            body = qtTrId("transferengine-la-file_upload_failed");
396 397
            break;
        case TransferEngineData::Download:
398
            //: Notification for failed file download
399 400
            //% "Download failed"
            body = qtTrId("transferengine-la-file_download_failed");
401 402
            break;
        case TransferEngineData::Sync:
403 404
            // no notification required
            category.clear();
405 406
            break;
        default:
407
            qCWarning(lcTransferLog) << "TransferEnginePrivate::sendNotification: unknown state";
408 409
            category.clear();
            break;
410 411
        }

412 413 414
        summary = fileName;
        previewSummary = summary;
        previewBody = body;
415

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    } else if (status == TransferEngineData::TransferStarted) {
        if (type == TransferEngineData::Upload || type == TransferEngineData::Download) {
            category = TRANSFER_EVENT_CATEGORY;

            if (type == TransferEngineData::Upload) {
                //: Notification for ongoing upload
                //% "File uploading"
                body = qtTrId("transferengine-no-file-uploading");
            } else {
                //: Notification for ongoing file download
                //% "File downloading"
                body = qtTrId("transferengine-no-file-downloading");
            }

            summary = fileName;

            if (progress > 0)
                useProgress = true;
434
        }
435 436 437 438 439 440 441 442 443

    } else if (status == TransferEngineData::TransferCanceled && notificationId > 0) {
        // Exit, no banners or events when user has canceled a transfer
        // Remove any existing notification
        Notification notification;
        notification.setReplacesId(notificationId);
        notification.close();
        DbManager::instance()->setNotificationId(transferId, 0);
        notificationId = 0;
444
    }
445

446 447
    if (!category.isEmpty()) {
        Notification notification;
448

449 450 451
        if (notificationId > 0) {
            notification.setReplacesId(notificationId);
        }
452

Pekka Vuorela's avatar
Pekka Vuorela committed
453 454
        if (!m_defaultActions.isEmpty()) {
            notification.setRemoteActions(m_defaultActions);
455 456
        }

457 458 459 460 461 462 463
        //: Group name for notifications of successful transfers
        //% "Transfers"
        const QString transfersGroup(qtTrId("transferengine-notification_group"));
        //: Group name for notifications of failed transfers
        //% "Warnings"
        const QString errorsGroup(qtTrId("transferengine-notification_errors_group"));

464 465 466 467 468 469 470
        notification.setCategory(category);
        notification.setAppName(category == TRANSFER_ERROR_EVENT_CATEGORY ? errorsGroup : transfersGroup);
        notification.setSummary(summary);
        notification.setBody(body);
        notification.setPreviewSummary(previewSummary);
        notification.setPreviewBody(previewBody);
        notification.setUrgency(urgency);
471

472 473 474 475
        if (!icon.isEmpty()) {
            notification.setIcon(icon);
        }

476 477 478 479 480 481 482 483 484 485
        if (useProgress) {
            notification.setHintValue(TRANSFER_PROGRESS_HINT, static_cast<double>(progress));
        }
        notification.publish();
        int newId = notification.replacesId();

        if (newId != notificationId) {
            DbManager::instance()->setNotificationId(transferId, newId);
        }
    }
486 487 488 489 490 491 492 493
}

int TransferEnginePrivate::uploadMediaItem(MediaItem *mediaItem,
                                           MediaTransferInterface *muif,
                                           const QVariantMap &userData)
{
    Q_Q(TransferEngine);

494
    if (mediaItem == 0) {
495
        qCWarning(lcTransferLog) << "TransferEngine::uploadMediaItem invalid MediaItem";
496 497 498
        return -1;
    }
    if (muif == 0) {
499
        qCWarning(lcTransferLog) << "TransferEngine::uploadMediaItem Failed to get MediaTransferInterface";
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
        return -1;
    }

    mediaItem->setValue(MediaItem::TransferType,        TransferEngineData::Upload);
    mediaItem->setValue(MediaItem::DisplayName,         muif->displayName());
    mediaItem->setValue(MediaItem::ServiceIcon,         muif->serviceIcon());
    mediaItem->setValue(MediaItem::CancelSupported,     muif->cancelEnabled());
    mediaItem->setValue(MediaItem::RestartSupported,    muif->restartEnabled());

    // Get and set data from user data if that's set. The following user data values
    // are stored to the database so that's why they are set to the media item too.
    // If the user data is fully custom for plugin it won't be stored to the database and
    // it's up to the plugin to handle or ignore it.
    QString title = userData.value("title").toString();
    QString desc  = userData.value("description").toString();
    qint64  accId = userData.value("accountId").toInt();
    qreal scale   = userData.value("scalePercent").toReal();

    mediaItem->setValue(MediaItem::Title,               title);
    mediaItem->setValue(MediaItem::Description,         desc);
    mediaItem->setValue(MediaItem::AccountId,           accId);
    mediaItem->setValue(MediaItem::ScalePercent,        scale);
    muif->setMediaItem(mediaItem);

    connect(muif, SIGNAL(statusChanged(MediaTransferInterface::TransferStatus)),
            this, SLOT(uploadItemStatusChanged(MediaTransferInterface::TransferStatus)));
    connect(muif, SIGNAL(progressUpdated(qreal)),
            this, SLOT(updateProgress(qreal)));

    // Let's create an entry into Transfer DB
    const int key = DbManager::instance()->createTransferEntry(mediaItem);
    m_keyTypeCache.insert(key, TransferEngineData::Upload);

    if (key < 0) {
534
        qCWarning(lcTransferLog) << "TransferEngine::uploadMediaItem: Failed to create an entry to transfer database!";
535 536 537 538
        delete muif;
        return key;
    }

539
    m_activityMonitor->newActivity(key);
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    emit q->transfersChanged();
    emit q->statusChanged(key, TransferEngineData::NotStarted);

    // For now, we just store our uploader to a map. It'll be removed from it when
    // the upload has finished.
    m_plugins.insert(muif, key);
    muif->start();
    return key;
}

QStringList TransferEnginePrivate::pluginList() const
{
    QDir dir(SHARE_PLUGINS_PATH);
    QStringList plugins = dir.entryList(QStringList() << "*.so",
                                        QDir::Files,
                                        QDir::NoSort);
    QStringList filePaths;
    Q_FOREACH(QString plugin, plugins) {
        filePaths << dir.absolutePath() + QDir::separator() + plugin;
    }

    return filePaths;
}

QList <TransferMethodInfo> TransferEnginePrivate::enabledPlugins() const
{
    return m_enabledPlugins;
}

569 570 571 572 573
QList<QVariantMap> TransferEnginePrivate::pluginMetaData() const
{
    return m_pluginMetaData;
}

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
MediaTransferInterface *TransferEnginePrivate::loadPlugin(const QString &pluginId) const
{
    QPluginLoader loader;
    loader.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint);
    Q_FOREACH(QString plugin, pluginList()) {
        loader.setFileName(plugin);
        TransferPluginInterface *interface =
                qobject_cast<TransferPluginInterface*>(loader.instance());


        if (interface && interface->enabled() && interface->pluginId() == pluginId) {
            return interface->transferObject();
        }

        if (!interface) {
589
            qCWarning(lcTransferLog) << "TransferEngine::loadPlugin: " + loader.errorString();
590 591 592 593 594 595 596 597 598 599
        }

        if (loader.isLoaded()) {
            loader.unload();
        }
    }

    return 0;
}

600 601 602 603 604 605 606 607 608 609 610 611 612
QString TransferEnginePrivate::mediaFileOrResourceName(MediaItem *mediaItem) const
{
    if (!mediaItem) {
        return QString();
    }
    QUrl url = mediaItem->value(MediaItem::Url).toUrl();
    if (!url.isEmpty()) {
        QStringList split = url.toString().split(QDir::separator());
        return split.at(split.length()-1);
    }
    return mediaItem->value(MediaItem::ResourceName).toString();
}

613 614 615 616 617 618 619 620 621 622 623 624 625
void TransferEnginePrivate::uploadItemStatusChanged(MediaTransferInterface::TransferStatus status)
{
    MediaTransferInterface *muif = qobject_cast<MediaTransferInterface*>(sender());
    const int key = m_plugins.value(muif);
    const TransferEngineData::TransferType type =
            static_cast<TransferEngineData::TransferType>(muif->mediaItem()->value(MediaItem::TransferType).toInt());

    TransferEngineData::TransferStatus tStatus = static_cast<TransferEngineData::TransferStatus>(status);

    bool ok = false;
    switch(tStatus) {
    case TransferEngineData::TransferStarted:
        ok = DbManager::instance()->updateTransferStatus(key, tStatus);
626
        m_activityMonitor->newActivity(key);
627 628 629 630 631 632 633 634 635
        break;

    case TransferEngineData::TransferInterrupted:
    case TransferEngineData::TransferCanceled:
    case TransferEngineData::TransferFinished:
    {
        // If the flow ends up here, we are not interested in any signals the same object
        // might emit. Let's just disconnect them.
        muif->disconnect();
636
        sendNotification(type, tStatus, muif->progress(), mediaFileOrResourceName(muif->mediaItem()), key);
637 638
        ok = DbManager::instance()->updateTransferStatus(key, tStatus);
        if (m_plugins.remove(muif) == 0) {
639
            qCWarning(lcTransferLog) << "TransferEnginePrivate::uploadItemStatusChanged: Failed to remove media upload object from the map!";
640 641 642 643
            // What to do here.. Let's just delete it..
        }
        muif->deleteLater();
        muif = 0;
644
        m_activityMonitor->activityFinished(key);
645 646 647
    } break;

    default:
648
        qCWarning(lcTransferLog) << "TransferEnginePrivate::uploadItemStatusChanged: unhandled status: "  << tStatus;
649 650 651 652
        break;
    }

    if (!ok) {
653
        qCWarning(lcTransferLog) << "TransferEnginePrivate::uploadItemStatusChanged: Failed update share status for the item: " + key;
654 655 656 657 658 659 660 661 662 663 664 665 666
        return;
    }

    Q_Q(TransferEngine);
    emit q->statusChanged(key, tStatus);
}

void TransferEnginePrivate::updateProgress(qreal progress)
{
    MediaTransferInterface *muif = qobject_cast<MediaTransferInterface*>(sender());
    const int key = m_plugins.value(muif);

    if (!DbManager::instance()->updateProgress(key, progress)) {
667
        qCWarning(lcTransferLog) << "TransferEnginePrivate::updateProgress: Failed to update progress";
668 669 670
        return;
    }

671
    m_activityMonitor->newActivity(key);
672
    Q_Q(TransferEngine);
673
    q->updateTransferProgress(key, progress);
674 675 676 677 678 679
}

void TransferEnginePrivate::pluginInfoReady()
{
    TransferPluginInfo *infoObj = qobject_cast<TransferPluginInfo*>(sender());

680 681 682 683
    QList<TransferMethodInfo> infoList = infoObj->info();
    if (!infoList.isEmpty()) {
        m_enabledPlugins << infoList;
    }
684
    m_pluginMetaData << infoObj->metaData();
685 686 687 688

    if (m_infoObjects.removeOne(infoObj)) {
        delete infoObj;
    } else {
689
        qCWarning(lcTransferLog) << Q_FUNC_INFO << "Failed to remove info object!";
690 691
        delete infoObj;
    }
692 693 694 695 696 697 698 699 700

    if (m_infoObjects.isEmpty()) {
        Q_Q(TransferEngine);
        emit q->transferMethodListChanged();
    }
}

void TransferEnginePrivate::pluginInfoError(const QString &msg)
{
701
    qCWarning(lcTransferLog) << "TransferEnginePrivate::pluginInfoError:" << msg;
702 703
    TransferPluginInfo *infoObj = qobject_cast<TransferPluginInfo*>(sender());
    m_infoObjects.removeOne(infoObj);
704
    infoObj->deleteLater();
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731

    if (m_infoObjects.isEmpty()) {
        Q_Q(TransferEngine);
        emit q->transferMethodListChanged();
    }
}


TransferEngineData::TransferType TransferEnginePrivate::transferType(int transferId)
{
    if (!m_keyTypeCache.contains(transferId)) {
        TransferEngineData::TransferType type = DbManager::instance()->transferType(transferId);
        m_keyTypeCache.insert(transferId, type);
        return type;
    } else {
        return m_keyTypeCache.value(transferId);
    }
}

void TransferEnginePrivate::callbackCall(int transferId, CallbackMethodType method)
{
    // Get DBus callback information. Callback list must contain at least
    // service, path, interface and one callback method name. Note that even
    // if the cancel or restart method is missing, it'll be indicated as an
    // empty string. So the list length is always 5.
    QStringList callback = DbManager::instance()->callback(transferId);
    if (callback.length() != 5) {
732
        qCWarning(lcTransferLog) << "TransferEnginePrivate:callbackCall: Invalid callback interface";
733 734 735 736 737 738 739 740
        return;
    }

    QDBusInterface remoteInterface(callback.at(Service),
                                   callback.at(Path),
                                   callback.at(Interface));

    if (!remoteInterface.isValid()) {
741
        qCWarning(lcTransferLog) << "TransferEnginePrivate::callbackCall: DBus interface is not valid!";
742 743 744 745
        return;
    }

    if (method >= callback.size()) {
746
        qCWarning(lcTransferLog) << "TransferEnginePrivate::callbackCall: method index out of range!";
747 748 749 750 751
        return;
    }

    const QString methodName = callback.at(method);
    if (methodName.isEmpty()) {
752
        qCWarning(lcTransferLog) << "TransferEnginePrivate::callbackCall: Failed to get callback method name!";
753
        return;
754 755
    }    
    remoteInterface.call(methodName, transferId);    
756 757 758 759 760 761 762 763 764 765 766
}


/*!
    \class TransferEngine
    \brief The TransferEngine class implements the functionality for different transfer types.

    \ingroup transfer-engine

    TransferEngine is the central place for:
    \list
Pekka Vuorela's avatar
Pekka Vuorela committed
767 768 769
        \li Sharing - Provides requires plugin interfaces for share plugins
        \li Downloads - Provides an API to create Download entries
        \li Syncs - Provides an API to create Sync entries
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    \endlist

    For Downloads and Syncs, the Transfer Engine acts only a place to keep track of these operations.
    The actual Download and Sync is executed by a client using TransferEngine API. For sharing
    the TransferEngine provides an API containing a few interaces, which a share plugin must implement.
    TransferEngine also takes care of loading and executing the sharing, based on the API it defines.

    The most essential thing to remember is that Transfer Engine provides share plugin API, DBus API e.g.
    for creating Transfer UI or Share UIs, it stores data to the local sqlite database using DbManager
    and that's it.

    How to implement a share plugin see: TransferPluginInterface, MediaTransferInterface, MediaItem,
    TransferPluginInfo

    TransferEngine provides DBus API, but instead of using it directly, it's recommended to use
    TransferEngineClient. If there is a need to create UI to display e.g. transfer statuses, then
    the DBus API is the recommend way to implement it.

 */

/*!
    \fn void TransferEngine::progressChanged(int transferId, double progress)

    The signal is emitted when \a progress for a transfer with a \a transferId has changed.
*/

/*!
    \fn void TransferEngine::statusChanged(int transferId, int status)

    The signal is emitted when \a status for a transfer with a \a transferId has changed.
*/

/*!
    \fn void TransferEngine::transferMethodListChanged()

    The signal is emitted when transfer methods have changed. Usually tranfer methods change
    when a new plugin is installed to the system or an account has been enabled or disabled.
*/

/*!
    \fn void TransferEngine::transfersChanged()

    The signal is emitted when there is a new transfer or a transfer has been removed from the
    database.
*/

/*!
    Constructor with optional \a parent arguement.
 */
TransferEngine::TransferEngine(QObject *parent) :
    QObject(parent),
    d_ptr(new TransferEnginePrivate(this))
{
823
    TransferMethodInfoDeprecated::registerType();
824 825
    TransferMethodInfo::registerType();
    TransferDBRecord::registerType();
826
    TransferPluginInfo::registerType();
827 828 829 830 831 832

    QDBusConnection connection = QDBusConnection::sessionBus();
    if (!connection.registerObject("/org/nemo/transferengine", this)) {
        qFatal("Could not register object \'/org/nemo/transferengine\'");
    }

833 834 835 836
    if (!connection.registerService("org.nemo.transferengine")) {
        qFatal("DBUS service already taken. Kill the other instance first.");
    }

837
    new TransferEngineAdaptor(this);
838 839 840 841 842 843

    // Let's make sure that db is open by creating
    // DbManager singleton instance.
    DbManager::instance();
    Q_D(TransferEngine);
    d->recoveryCheck();
844
    d->enabledPluginsCheck();    
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
}

/*!
    Destroys the TransferEngine object.
 */
TransferEngine::~TransferEngine()
{
    Q_D(TransferEngine);
    d->recoveryCheck();
    delete d_ptr;
    d_ptr = 0;

    QDBusConnection connection = QDBusConnection::sessionBus();
    connection.unregisterObject("/org/nemo/transferengine");

    if (!connection.unregisterService("org.nemo.transferengine")) {
861
        qCWarning(lcTransferLog) << "Failed to unregister org.nemo.tranferengine service";
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
    }
}


/*!
    DBus adaptor calls this method to start uploading a media item. The minimum information
    needed to start an upload and to create an entry to the transfer database is:
    \a source the path to the media item to be downloaded. \a serviceId the ID of the share
    plugin. See TransferPluginInterface::pluginId() for more details. \a mimeType is the MimeType
    of the media item e.g. "image/jpeg". \a metadataStripped boolean to indicate if the metadata
    should be kept or removed before uploading. \a userData is various kind of data which share UI
    may provide to the engine. UserData is QVariant map i.e. the data must be provided as key-value
    pairs, where the keys must be QStrings.

    TransferEngine handles the following user defined data automatically and stores them to the database:
    \list
Pekka Vuorela's avatar
Pekka Vuorela committed
878 879 880 881
        \li "title" Title for the media
        \li "description" Description for the media
        \li "accountId" The ID of the account which is used for sharing. See qt-accounts for more details.
        \li "scalePercent" The scale percent e.g. downscale image to 50% from original before uploading.
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
    \endlist

    In practice this method instantiates a share plugin with \a serviceId and passes a MediaItem instance filled
    with required data to it. When the plugin has been loaded, the MediaTransferInterface::start() method is called
    and the actual sharing starts.

    This method returns a transfer ID which can be used later to fetch information of this specific transfer.
 */
int TransferEngine::uploadMediaItem(const QString &source,
                                    const QString &serviceId,
                                    const QString &mimeType,
                                    bool metadataStripped,
                                    const QVariantMap &userData)
{
    Q_D(TransferEngine);
897 898
    d->exitSafely();

899 900
    MediaTransferInterface *muif = d->loadPlugin(serviceId);
    if (muif == 0) {
901
        qCWarning(lcTransferLog) << "TransferEngine::uploadMediaItem Failed to get MediaTransferInterface";
902 903 904 905 906 907
        return -1;
    }

    QUrl filePath(source);
    QFileInfo fileInfo(filePath.toLocalFile());
    if (!fileInfo.exists()) {
908
        qCWarning(lcTransferLog) << "TransferEnginePrivate::uploadMediaItem file " << source << " doesn't exist!";
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
    }

    MediaItem *mediaItem = new MediaItem(muif);
    mediaItem->setValue(MediaItem::Url,                 filePath);
    mediaItem->setValue(MediaItem::MetadataStripped,    metadataStripped);
    mediaItem->setValue(MediaItem::ResourceName,        fileInfo.fileName());
    mediaItem->setValue(MediaItem::MimeType,            mimeType);
    mediaItem->setValue(MediaItem::FileSize,            fileInfo.size());
    mediaItem->setValue(MediaItem::PluginId,            serviceId);
    mediaItem->setValue(MediaItem::UserData,            userData);
    return d->uploadMediaItem(mediaItem, muif, userData);
}

/*!
    DBus adaptor calls this method to start uploading media item content. Sometimes the content
    to be shared is not a file, but data e.g. contact information in vcard format. In order to
    avoid serializing data to a file, pass url to the file, reading the data, deleting the file,
    TransferEngine provides this convenience API.

    \a content is the media item content to be shared. \a serviceId is the id of the share plugin. See
    TransferPluginInterface::pluginId() for more details. \a userData is a QVariantMap containing
    share plugin specific data. See TransferEngine::uploadMediaItem for more details.

    This method returns a transfer ID which can be used later to fetch information of this specific transfer.
*/
int TransferEngine::uploadMediaItemContent(const QVariantMap &content,
                                           const QString &serviceId,
                                           const QVariantMap &userData)
{
    Q_D(TransferEngine);
939 940
    d->exitSafely();

941 942
    MediaTransferInterface *muif = d->loadPlugin(serviceId);
    if (muif == 0) {
943
        qCWarning(lcTransferLog) << "TransferEngine::uploadMediaItemContent Failed to get MediaTransferInterface";
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
        return -1;
    }

    MediaItem *mediaItem = new MediaItem(muif);
    mediaItem->setValue(MediaItem::ContentData,     content.value("data"));
    mediaItem->setValue(MediaItem::ResourceName,    content.value("name"));
    mediaItem->setValue(MediaItem::MimeType,        content.value("type"));
    mediaItem->setValue(MediaItem::ThumbnailIcon,   content.value("icon"));
    mediaItem->setValue(MediaItem::PluginId,        serviceId);
    mediaItem->setValue(MediaItem::UserData,        userData);
    return d->uploadMediaItem(mediaItem, muif, userData);
}

/*!
    DBus adaptor calls this method to create a download entry. Note that this is purely write-only
    method and doesn't involve anything else from TransferEngine side than creating a new DB record
    of type 'Download'.

    \list
Pekka Vuorela's avatar
Pekka Vuorela committed
963 964 965 966 967 968 969 970 971
        \li \a displayName  The name for Download which may be used by the UI displaying the download
        \li \a applicationIcon The application icon of the application created the download
        \li \a serviceIcon The service icon, which provides the file to be downloaded
        \li \a filePath The filePath e.g. url to the file to be downloaded
        \li \a mimeType the MimeType of the file to be downloaded
        \li \a expectedFileSize The file size of the file to be downloaded
        \li \a callback QStringList containing DBus callback information such as: service, path and interface
        \li \a cancelMethod The name of the cancel callback method, which DBus callback provides
        \li \a restartMethod The name of the restart callback method, which DBus callback provides
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    \endlist

    This method returns the transfer id of the created Download transfer. Note that this method only
    creates an entry to the database. To start the actual transfer, the startTransfer() method must
    be called.

    \sa startTransfer(), restartTransfer(), finishTransfer(), updateTransferProgress()
 */
int TransferEngine::createDownload(const QString &displayName,
                                   const QString &applicationIcon,
                                   const QString &serviceIcon,
                                   const QString &filePath,
                                   const QString &mimeType,
                                   qlonglong expectedFileSize,
                                   const QStringList &callback,
                                   const QString &cancelMethod,
                                   const QString &restartMethod)
{
    Q_D(TransferEngine);
    QUrl url(filePath);
    QFileInfo fileInfo(filePath);

    MediaItem *mediaItem = new MediaItem();
    mediaItem->setValue(MediaItem::Url,             url);
    mediaItem->setValue(MediaItem::ResourceName,    fileInfo.fileName());
    mediaItem->setValue(MediaItem::MimeType,        mimeType);
    mediaItem->setValue(MediaItem::TransferType,    TransferEngineData::Download);
    mediaItem->setValue(MediaItem::FileSize,        expectedFileSize);
    mediaItem->setValue(MediaItem::DisplayName,     displayName);
    mediaItem->setValue(MediaItem::ApplicationIcon, applicationIcon);
    mediaItem->setValue(MediaItem::ServiceIcon,     serviceIcon);
    mediaItem->setValue(MediaItem::Callback,        callback);
    mediaItem->setValue(MediaItem::CancelCBMethod,  cancelMethod);
    mediaItem->setValue(MediaItem::RestartCBMethod, restartMethod);
    mediaItem->setValue(MediaItem::CancelSupported, !cancelMethod.isEmpty());
    mediaItem->setValue(MediaItem::RestartSupported,!restartMethod.isEmpty());

    const int key = DbManager::instance()->createTransferEntry(mediaItem);
1010
    d->m_activityMonitor->newActivity(key);
1011 1012
    d->m_keyTypeCache.insert(key, TransferEngineData::Download);
    emit transfersChanged();
1013
    emit statusChanged(key, TransferEngineData::NotStarted);    
1014 1015 1016 1017 1018 1019 1020 1021 1022
    return key;
}

/*!
    DBus adaptor calls this method to create a Sync entry. Note that this is purely write-only
    method and doesn't involve anything else from TransferEngine side than creating a new DB record
    of type 'Download'.

    \list
Pekka Vuorela's avatar
Pekka Vuorela committed
1023 1024 1025 1026 1027 1028
        \li \a displayName  The name for download which may be used by the UI displaying the download
        \li \a applicationIcon The application icon of the application created the download
        \li \a serviceIcon The service icon, which provides the file to be downloaded
        \li \a callback QStringList containing DBus callback information such as: service, path and interface
        \li \a cancelMethod The name of the cancel callback method, which DBus callback provides
        \li \a restartMethod The name of the restart callback method, which DBus callback provides
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
    \endlist

    This method returns the transfer id of the created Download transfer. Note that this method only
    creates an entry to the database. To start the actual transfer, the startTransfer() method must
    be called.

    \sa startTransfer(), restartTransfer(), finishTransfer(), updateTransferProgress()
 */
int TransferEngine::createSync(const QString &displayName,
                               const QString &applicationIcon,
                               const QString &serviceIcon,
                               const QStringList &callback,
                               const QString &cancelMethod,
                               const QString &restartMethod)
{
    MediaItem *mediaItem = new MediaItem();
    mediaItem->setValue(MediaItem::TransferType,    TransferEngineData::Sync);
    mediaItem->setValue(MediaItem::DisplayName,     displayName);
    mediaItem->setValue(MediaItem::ApplicationIcon, applicationIcon);
    mediaItem->setValue(MediaItem::ServiceIcon,     serviceIcon);
    mediaItem->setValue(MediaItem::Callback,        callback);
    mediaItem->setValue(MediaItem::CancelCBMethod,  cancelMethod);
    mediaItem->setValue(MediaItem::RestartCBMethod, restartMethod);
    mediaItem->setValue(MediaItem::CancelSupported, !cancelMethod.isEmpty());
    mediaItem->setValue(MediaItem::RestartSupported,!restartMethod.isEmpty());

    const int key = DbManager::instance()->createTransferEntry(mediaItem);
1056 1057
    delete mediaItem;

1058
    Q_D(TransferEngine);
1059
    d->m_activityMonitor->newActivity(key);
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    d->m_keyTypeCache.insert(key, TransferEngineData::Sync);
    emit transfersChanged();
    emit statusChanged(key, TransferEngineData::NotStarted);
    return key;
}

/*!
    DBus adaptor calls this method to start the actual transfer. This method changes the transfer
    status of the existing  transfer with a \a transferId to TransferEngineData::TransferStarted. This
    method can only be called for Sync and Download transfers.

    Calling this method causes the corresponding statusChanged() signal to be emitted.
*/
void TransferEngine::startTransfer(int transferId)
{
    Q_D(TransferEngine);
1076 1077
    d->exitSafely();

1078 1079
    TransferEngineData::TransferType type = d->transferType(transferId);
    if (type == TransferEngineData::Undefined) {
1080
        qCWarning(lcTransferLog) << "TransferEngine::startTransfer: failed to get transfer type";
1081 1082 1083 1084
        return;
    }

    if (type == TransferEngineData::Upload) {
1085
        qCWarning(lcTransferLog) << "TransferEngine::startTransfer: starting upload isn't supported";
1086 1087 1088 1089 1090 1091 1092 1093
        return;
    }

    TransferEngineData::TransferStatus status = DbManager::instance()->transferStatus(transferId);
    // First check if this is a new transfer
    if (status == TransferEngineData::NotStarted ||
        status == TransferEngineData::TransferCanceled ||
        status == TransferEngineData::TransferInterrupted) {
1094
        d->m_activityMonitor->newActivity(transferId);
1095 1096
        DbManager::instance()->updateTransferStatus(transferId, TransferEngineData::TransferStarted);
        emit statusChanged(transferId, TransferEngineData::TransferStarted);
1097
        emit activeTransfersChanged();
1098
    } else {
1099
        qCWarning(lcTransferLog) << "TransferEngine::startTransfer: could not start transfer";
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
    }
}

/*!
    DBus adaptor calls this method to restart a canceled or failed transfer with a \a transferId. In
    a case of Upload, this method creates MediaItem instance of the existing transfer and instantiates
    the required share plugin. The MediaItem instance is passed to the plugin and sharing is restarted.

    For Sync and Download entries, this method calls their callbacks methods, if a callback interface
    has been defined by the client originally created the Sync or Download entry.
*/
void TransferEngine::restartTransfer(int transferId)
{
    Q_D(TransferEngine);
1114 1115
    d->exitSafely();

1116 1117
    TransferEngineData::TransferType type = d->transferType(transferId);
    if (type == TransferEngineData::Undefined) {
1118
        qCWarning(lcTransferLog) << "TransferEngine::restartTransfer: failed to get transfer type";
1119 1120 1121 1122 1123 1124 1125
        return;
    }

    if (type == TransferEngineData::Upload) {

        MediaItem * item = DbManager::instance()->mediaItem(transferId);
        if (!item) {
1126
            qCWarning(lcTransferLog) << "TransferEngine::restartTransfer: failed to reload media item from db!";
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
            return;
        }

        Q_D(TransferEngine);
        MediaTransferInterface *muif = d->loadPlugin(item->value(MediaItem::PluginId).toString());
        muif->setMediaItem(item);

        connect(muif, SIGNAL(statusChanged(MediaTransferInterface::TransferStatus)),
                d, SLOT(uploadItemStatusChanged(MediaTransferInterface::TransferStatus)));
        connect(muif, SIGNAL(progressUpdated(qreal)),
                d, SLOT(updateProgress(qreal)));

1139
        d->m_activityMonitor->newActivity(transferId);
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
        d->m_keyTypeCache.insert(transferId, TransferEngineData::Upload);
        d->m_plugins.insert(muif, transferId);
        muif->start();
        return;
    }

    TransferEngineData::TransferStatus status = DbManager::instance()->transferStatus(transferId);
    // Check if this is canceled or interrupted transfer
    // and make a callback call to the client. It's client's
    // responsibility to update states properly
    if (status == TransferEngineData::TransferCanceled ||
        status == TransferEngineData::TransferInterrupted) {
        DbManager::instance()->updateProgress(transferId, 0);
1153
        d->m_activityMonitor->newActivity(transferId);
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
        d->callbackCall(transferId, TransferEnginePrivate::RestartCallback);
    }
}

/*!
    Finishes an existing Sync or Download transfer with a \a transferId. Transfer can be finished
    with different \a status e.g for successfully finish status can be set to
    TransferEngineData::TransferFinished, for canceling TransferEngineData::Canceled and for
    failure with TransferEngineData::TransferInterrupted. In a case of failure, the client can
    also provide a \a reason.

    This method causes statusChanged() signal to be emitted. If a sync has been successfully
    finished, then it will also be removed from the database automatically which causes
    transferChanged() signal to be emitted.
 */
void TransferEngine::finishTransfer(int transferId, int status, const QString &reason)
{
    Q_UNUSED(reason);
    Q_D(TransferEngine);
1173 1174
    d->exitSafely();

1175 1176 1177 1178 1179
    TransferEngineData::TransferType type = d->transferType(transferId);
    if (type == TransferEngineData::Undefined || type == TransferEngineData::Upload) {
        return; // We don't handle plugins here
    }

1180
    QString fileName;
1181 1182 1183 1184
    // Read the file path from the database for download
    if (type == TransferEngineData::Download) {
        MediaItem *mediaItem = DbManager::instance()->mediaItem(transferId);
        if (!mediaItem) {
1185
            qCWarning(lcTransferLog) << "TransferEngine::finishTransfer: Failed to fetch MediaItem";
1186 1187
            return;
        }
1188
        fileName = d->mediaFileOrResourceName(mediaItem);
1189 1190 1191 1192 1193 1194 1195
    }

    TransferEngineData::TransferStatus transferStatus = static_cast<TransferEngineData::TransferStatus>(status);
    if (transferStatus == TransferEngineData::TransferFinished ||
        transferStatus == TransferEngineData::TransferCanceled ||
        transferStatus == TransferEngineData::TransferInterrupted) {
        DbManager::instance()->updateTransferStatus(transferId, transferStatus);
1196
        d->sendNotification(type, transferStatus, DbManager::instance()->transferProgress(transferId), fileName, transferId);
1197
        d->m_activityMonitor->activityFinished(transferId);
1198 1199
        emit statusChanged(transferId, status);

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
        bool notify = false;

        // Clean up old failed syncs from the database and leave only the latest one there
        if (type == TransferEngineData::Sync) {
            if (DbManager::instance()->clearFailedTransfers(transferId, type)) {
                notify = true;
            }

            // We don't want to leave successfully finished syncs to populate the database, just remove it.
            if (transferStatus == TransferEngineData::TransferFinished) {
                if (DbManager::instance()->removeTransfer(transferId)) {
                    notify = true;
                }
1213 1214
            }
        }
1215

1216 1217
        emit activeTransfersChanged(); // Assume that the transfer was active

1218 1219 1220
        if (notify) {
            emit transfersChanged();
        }
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
    }
}

/*!
    DBus adaptor calls this method to update transfer progress of the transfer with a \a transferId and
    with a new \a progress.
*/
void TransferEngine::updateTransferProgress(int transferId, double progress)
{
    Q_D(TransferEngine);
1231
    d->exitSafely();
1232

1233
    TransferEngineData::TransferType type = d->transferType(transferId);
1234
    if (type != TransferEngineData::Download && type != TransferEngineData::Upload) {
1235 1236 1237
        return;
    }

1238 1239
    MediaItem *mediaItem = DbManager::instance()->mediaItem(transferId);
    if (!mediaItem) {
1240
        qCWarning(lcTransferLog) << "TransferEngine::finishTransfer: Failed to fetch MediaItem";
1241 1242 1243 1244 1245
        return;
    }
    QString fileName = d->mediaFileOrResourceName(mediaItem);

    int oldProgressPercentage = DbManager::instance()->transferProgress(transferId) * 100;
1246
    if (DbManager::instance()->updateProgress(transferId, progress)) {
1247
        d->m_activityMonitor->newActivity(transferId);
1248
        emit progressChanged(transferId, progress);
1249 1250 1251 1252

        if (oldProgressPercentage != (progress * 100)) {
            d->sendNotification(type, DbManager::instance()->transferStatus(transferId), progress, fileName, transferId);
        }
1253
    } else {
1254
         qCWarning(lcTransferLog) << "TransferEngine::updateTransferProgress: Failed to update progress for " << transferId;
1255 1256 1257 1258 1259 1260 1261 1262
    }
}

/*!
    DBus adaptor calls this method to fetch a list of transfers. This method returns QList<TransferDBRecord>.
 */
QList<TransferDBRecord> TransferEngine::transfers()
{    
1263 1264
    Q_D(TransferEngine);
    d->exitSafely();
1265 1266 1267
    return DbManager::instance()->transfers();
}

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
/*!
    DBus adaptor calls this method to fetch a list of active transfers. This method returns QList<TransferDBRecord>.
 */
QList<TransferDBRecord> TransferEngine::activeTransfers()
{
    Q_D(TransferEngine);
    d->exitSafely();
    return DbManager::instance()->activeTransfers();
}

1278 1279 1280 1281 1282
/*!
    DBus adaptor calls this method to fetch a list of transfer methods. This method returns QList<TransferMethodInfo>.

    Transfer methods are basically a list of share plugins installed to the system.
 */
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
QList <TransferMethodInfoDeprecated> TransferEngine::transferMethods()
{
    Q_D(TransferEngine);
    d->exitSafely();

    QList <TransferMethodInfo> newPlugins = d->enabledPlugins();
    QList <TransferMethodInfoDeprecated> oldPlugins;
    for (auto info : newPlugins) {
        TransferMethodInfoDeprecated oldInfo;
        oldInfo.displayName     = info.displayName;
        oldInfo.userName        = info.userName;
        oldInfo.methodId        = info.methodId;
        oldInfo.shareUIPath     = info.shareUIPath;
        oldInfo.capabilitities  = info.capabilitities;
        oldInfo.accountId       = info.accountId;
        oldPlugins << oldInfo;
    }
    return oldPlugins;
}

QList <TransferMethodInfo> TransferEngine::transferMethods2()
1304 1305
{
    Q_D(TransferEngine);
1306
    d->exitSafely();
1307 1308 1309
    return d->enabledPlugins();
}

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
/*!
    DBus adaptor calls this method to fetch a list of the share plugin metadata.

    This method returns QList<QVariantMap>.
 */
QList<QVariantMap> TransferEngine::pluginMetaData()
{
    Q_D(TransferEngine);
    d->exitSafely();
    return d->pluginMetaData();
}

1322 1323 1324 1325 1326
/*!
    DBus adaptor calls this method to clear all the finished, canceled or interrupted transfers in the database.
 */
void TransferEngine::clearTransfers()
{    
1327 1328
    Q_D(TransferEngine);
    d->exitSafely();
1329 1330 1331 1332 1333 1334 1335 1336 1337
    const int count = DbManager::instance()->transferCount();
    if (count > 0) {
        const int active = DbManager::instance()->activeTransferCount();
        if (DbManager::instance()->clearTransfers()) {
            if (active > 0) {
                emit activeTransfersChanged();
            }
            emit transfersChanged();
        }
1338 1339 1340
    }
}

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
/*!
    DBus adaptor calls this method to remove a finished, canceled or interrupted transfer from the database.
 */
void TransferEngine::clearTransfer(int transferId)
{
    Q_D(TransferEngine);
    d->exitSafely();
    if (DbManager::instance()->clearTransfer(transferId)) {
        emit transfersChanged();
    }
}

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
/*!
    DBus adaptor calls this method to cancel an existing transfer with a \a transferId.

    If the transfer is Upload, then this method calls MediaTransferInterface instance's
    cancel method. In a case of Sync or Download this method calls client's cancel callback
    method, if the one exists.

    Calling this method causes statusChanged() signal to be emitted.
 */
void TransferEngine::cancelTransfer(int transferId)
{
    Q_D(TransferEngine);
1365 1366
    d->exitSafely();

1367 1368 1369 1370 1371
    TransferEngineData::TransferType type = d->transferType(transferId);

    // Handle canceling of Download or Sync
    if (type == TransferEngineData::Download || type == TransferEngineData::Sync) {
        d->callbackCall(transferId, TransferEnginePrivate::CancelCallback);
1372
        d->m_activityMonitor->activityFinished(transferId);
1373 1374 1375 1376 1377 1378 1379
        return;
    }

    // Let plugin handle canceling of up upload
    if (type == TransferEngineData::Upload) {
        MediaTransferInterface *muif = d->m_plugins.key(transferId);
        if (muif == 0) {
1380
            qCWarning(lcTransferLog) << "TransferEngine::cancelTransfer: Failed to get MediaTransferInterface!";
1381 1382
            return;
        }
1383
        d->m_activityMonitor->activityFinished(transferId);
1384 1385 1386 1387
        muif->cancel();
    }
}
/*!
1388
    DBus adaptor calls this method to enable or disable transfer specific notifications
1389 1390 1391 1392 1393
    based on \a enable argument.
*/
void TransferEngine::enableNotifications(bool enable)
{
    Q_D(TransferEngine);
1394
    d->exitSafely();
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
    if (d->m_notificationsEnabled != enable) {
        d->m_notificationsEnabled = enable;
    }
}

/*!
    DBus adaptor calls this method.
    Returns true or false depending if notifications are enabled or disabled.
*/
bool TransferEngine::notificationsEnabled()
{
    Q_D(TransferEngine);
1407
    d->exitSafely();
1408 1409
    return d->m_notificationsEnabled;
}