Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[buteo-sync-plugins-social] Fetch backup configuration from sync prof…
…ile. Contributes to JB#47080

Do this instead of fetching config values from dconf.
  • Loading branch information
Bea Lam committed Oct 8, 2019
1 parent ef91cc8 commit 74ce619
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 163 deletions.
3 changes: 3 additions & 0 deletions src/common.pri
Expand Up @@ -6,6 +6,7 @@ PKGCONFIG += \
accounts-qt5 \
buteosyncfw5 \
socialcache
LIBS += -lssu

QT += \
network \
Expand All @@ -30,12 +31,14 @@ DEFINES += 'SOCIALD_SYNC_DATABASE_NAME=\'\"sociald.db\"\''
INCLUDEPATH += . $$PWD/common/

HEADERS += \
$$PWD/common/backuprestoreoptions_p.h \
$$PWD/common/buteosyncfw_p.h \
$$PWD/common/socialdbuteoplugin.h \
$$PWD/common/socialnetworksyncadaptor.h \
$$PWD/common/trace.h

SOURCES += \
$$PWD/common/backuprestoreoptions.cpp \
$$PWD/common/socialdbuteoplugin.cpp \
$$PWD/common/socialnetworksyncadaptor.cpp

Expand Down
132 changes: 132 additions & 0 deletions src/common/backuprestoreoptions.cpp
@@ -0,0 +1,132 @@
/****************************************************************************
**
** Copyright (C) 2019 Open Mobile Platform LLC
**
** This program/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.
**
** This program/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.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program/library; if not, write to the Free
** Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
** 02110-1301 USA
**
****************************************************************************/

#include "backuprestoreoptions_p.h"
#include "trace.h"

#include <QtCore/QCryptographicHash>

// nemo
#include <ssudeviceinfo.h>

// buteo-syncfw
#include <SyncProfile.h>

bool BackupRestoreOptions::copyToProfile(Buteo::SyncProfile *syncProfile)
{
if (!syncProfile) {
qWarning() << "Invalid profile!";
return false;
}

Buteo::Profile *clientProfile = syncProfile->clientProfile();
if (!clientProfile) {
qWarning() << "Cannot find client profile in sync profile:" << syncProfile->name();
return false;
}

QString operationValue;
switch (operation) {
case DirectoryListing:
operationValue = "dir-listing";
break;
case Upload:
operationValue = "upload";
break;
case Download:
operationValue = "download";
break;
};

clientProfile->setKey("sfos-operation", operationValue);
clientProfile->setKey("sfos-dir-local", localDirPath);
clientProfile->setKey("sfos-dir-remote", remoteDirPath);
clientProfile->setKey("sfos-filename", fileName);

return true;
}

BackupRestoreOptions BackupRestoreOptions::fromProfile(Buteo::SyncProfile *syncProfile, bool *ok)
{
if (!syncProfile) {
qWarning() << "Invalid sync profile!";
return BackupRestoreOptions();
}

Buteo::Profile *clientProfile = syncProfile->clientProfile();
if (!clientProfile) {
qWarning() << "Cannot find client profile in sync profile:" << syncProfile->name();
return BackupRestoreOptions();
}

BackupRestoreOptions options;
QString operation = clientProfile->key("sfos-operation");
options.localDirPath = clientProfile->key("sfos-dir-local");
options.remoteDirPath = clientProfile->key("sfos-dir-remote");
options.fileName = clientProfile->key("sfos-filename");

if (operation == "dir-listing") {
options.operation = BackupRestoreOptions::DirectoryListing;
} else if (operation == "upload") {
options.operation = BackupRestoreOptions::Upload;
} else if (operation == "download") {
options.operation = BackupRestoreOptions::Download;
} else {
qWarning() << "Backup/restore options for sync profile" << syncProfile->name()
<< "has invalid operation type:" << operation;
return BackupRestoreOptions();
}

if (options.localDirPath.isEmpty()) {
qWarning() << "Backup/restore options for sync profile" << syncProfile->name()
<< "do not specify a local directory!";
return BackupRestoreOptions();
}

if (options.operation == BackupRestoreOptions::DirectoryListing && options.fileName.isEmpty()) {
qWarning() << "Backup/restore options for sync profile" << syncProfile->name()
<< "do not specify a file name for directory listing!";
return BackupRestoreOptions();
}

if (ok) {
*ok = true;
}
return options;
}

QString BackupRestoreOptions::backupDeviceName()
{
SsuDeviceInfo deviceInfo;
const QString deviceId = deviceInfo.deviceUid();
const QByteArray hashedDeviceId = QCryptographicHash::hash(deviceId.toUtf8(), QCryptographicHash::Sha256);
const QString encodedDeviceId = QString::fromUtf8(hashedDeviceId.toBase64(QByteArray::Base64UrlEncoding)).mid(0,12);
if (deviceId.isEmpty()) {
qWarning() << "Could not determine device identifier for backup directory name!";
return QString();
}

QString deviceDisplayNamePrefix = deviceInfo.displayName(Ssu::DeviceModel);
if (!deviceDisplayNamePrefix.isEmpty()) {
deviceDisplayNamePrefix = deviceDisplayNamePrefix.replace(' ', '-') + '_';
}

return deviceDisplayNamePrefix + encodedDeviceId;
}
52 changes: 52 additions & 0 deletions src/common/backuprestoreoptions_p.h
@@ -0,0 +1,52 @@
/****************************************************************************
**
** Copyright (C) 2019 Open Mobile Platform LLC
**
** This program/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.
**
** This program/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.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this program/library; if not, write to the Free
** Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
** 02110-1301 USA
**
****************************************************************************/

#ifndef BACKUPRESTOREOPTIONS_H
#define BACKUPRESTOREOPTIONS_H

#include <QtCore/QString>

namespace Buteo {
class SyncProfile;
}

// Basically a mirror of AccountSyncManager::BackupRestoreOptions.
class BackupRestoreOptions
{
public:
enum Operation {
DirectoryListing,
Upload,
Download
};

Operation operation = DirectoryListing;
QString localDirPath;
QString remoteDirPath;
QString fileName;

bool copyToProfile(Buteo::SyncProfile *syncProfile);

static BackupRestoreOptions fromProfile(Buteo::SyncProfile *syncProfile, bool *ok);

static QString backupDeviceName();
};

#endif // BACKUPRESTOREOPTIONS_H
4 changes: 0 additions & 4 deletions src/dropbox/dropbox-backup/dropbox-backup.pri
@@ -1,7 +1,3 @@
CONFIG += link_pkgconfig
PKGCONFIG += mlite5
LIBS += -lssu

SOURCES += $$PWD/dropboxbackupsyncadaptor.cpp
HEADERS += $$PWD/dropboxbackupsyncadaptor.h
INCLUDEPATH += $$PWD
Expand Down
5 changes: 3 additions & 2 deletions src/dropbox/dropbox-backup/dropboxbackupplugin.cpp
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2015 Jolla Ltd.
** Copyright (C) 2015-2019 Jolla Ltd.
** Copyright (C) 2019 Open Mobile Platform LLC
** Contact: Chris Adams <chris.adams@jolla.com>
**
** This program/library is free software; you can redistribute it and/or
Expand Down Expand Up @@ -50,5 +51,5 @@ DropboxBackupPlugin::~DropboxBackupPlugin()

SocialNetworkSyncAdaptor *DropboxBackupPlugin::createSocialNetworkSyncAdaptor()
{
return new DropboxBackupSyncAdaptor(this);
return new DropboxBackupSyncAdaptor(profile().name(), this);
}

0 comments on commit 74ce619

Please sign in to comment.