NfcShareHandler.cpp Example File
nfcshare/src/NfcShareHandler.cpp
#include "NfcShareHandler.hpp"
NfcShareHandler::NfcShareHandler(QObject *parent)
: QObject(parent)
, m_shareMode(DataShare)
, m_fileListModel(new FileListModel(this))
, m_systemToast(new bb::system::SystemToast(this))
, m_nfcShareManager(new bb::system::NfcShareManager(this))
{
bool ok = connect(m_fileListModel, SIGNAL(changed()), this, SLOT(prepareFileShareContent()));
Q_ASSERT(ok);
ok = connect(m_nfcShareManager, SIGNAL(finished(bb::system::NfcShareSuccess::Type)),
this, SLOT(sharingFinished(bb::system::NfcShareSuccess::Type)));
Q_ASSERT(ok);
ok = connect(m_nfcShareManager, SIGNAL(error(bb::system::NfcShareError::Type)),
this, SLOT(sharingError(bb::system::NfcShareError::Type)));
Q_ASSERT(ok);
}
void NfcShareHandler::prepareDataShareContent()
{
if (m_data.isEmpty())
return;
bb::system::NfcShareDataContent dataShareContent;
const QByteArray data(m_data.toUtf8());
dataShareContent.setMimeType(QLatin1String("text/plain"));
dataShareContent.setData(data);
m_nfcShareManager->setShareContent(dataShareContent);
}
void NfcShareHandler::prepareFileShareContent()
{
bb::system::NfcShareFilesContent fileShareContent;
const QList<QUrl> files = m_fileListModel->files();
if (files.isEmpty())
return;
fileShareContent.setFileUrls(files);
m_nfcShareManager->setShareContent(fileShareContent);
}
void NfcShareHandler::sharingFinished(bb::system::NfcShareSuccess::Type result)
{
switch (result) {
case bb::system::NfcShareSuccess::File:
displayNotification(tr("NFC file transfer started successfully"));
break;
case bb::system::NfcShareSuccess::Snep:
displayNotification(tr("NFC data was transfered successfully"));
break;
case bb::system::NfcShareSuccess::TagWrite:
displayNotification(tr("NFC data was written successfully"));
break;
default:
displayNotification(tr("Unknown share success"));
break;
}
}
void NfcShareHandler::sharingError(bb::system::NfcShareError::Type error)
{
switch (error) {
case bb::system::NfcShareError::NoContentToShare:
case bb::system::NfcShareError::Unknown:
displayNotification(tr("Failed to share data over NFC"));
break;
case bb::system::NfcShareError::RegisterFileSharing:
displayNotification(tr("Failed to prepare for file sharing"));
break;
case bb::system::NfcShareError::RegisterDataSharing:
displayNotification(tr("Failed to prepare for data sharing"));
break;
case bb::system::NfcShareError::MessageSize:
displayNotification(tr("NFC Tag is too small for a shared message"));
break;
case bb::system::NfcShareError::TagLocked:
displayNotification(tr("Failed to write to a locked NFC Tag"));
break;
case bb::system::NfcShareError::UnsupportedTagType:
displayNotification(tr("Failed to write to a unsupported NFC Tag"));
break;
case bb::system::NfcShareError::DataTransferFailed:
case bb::system::NfcShareError::HandoverFailed:
case bb::system::NfcShareError::BluetoothFileTransferFailed:
displayNotification(tr("Failed to share data over NFC - tap again"));
break;
default:
displayNotification(tr("Unknown nfc share error"));
break;
}
}
void NfcShareHandler::displayNotification(const QString& notification)
{
m_systemToast->setBody(notification);
m_systemToast->setPosition(bb::system::SystemUiPosition::MiddleCenter);
m_systemToast->setModality(bb::system::SystemUiModality::Application);
bb::system::SystemUiButton* toastButton = m_systemToast->button();
toastButton->setLabel(tr("Ok"));
toastButton->setEnabled(true);
m_systemToast->show();
}
void NfcShareHandler::setShareMode(ShareMode mode)
{
if (m_shareMode == mode)
return;
m_shareMode = mode;
m_nfcShareManager->setShareMode(bb::system::NfcShareMode::Disabled);
switch (m_shareMode) {
case DataShare:
m_nfcShareManager->setShareMode(bb::system::NfcShareMode::Data);
prepareDataShareContent();
break;
case FileShare:
m_nfcShareManager->setShareMode(bb::system::NfcShareMode::File);
prepareFileShareContent();
break;
}
emit shareModeChanged();
}
NfcShareHandler::ShareMode NfcShareHandler::shareMode() const
{
return m_shareMode;
}
FileListModel* NfcShareHandler::fileModel() const
{
return m_fileListModel;
}
void NfcShareHandler::setData(const QString &data)
{
if (m_data == data)
return;
m_data = data;
prepareDataShareContent();
emit dataChanged();
}
QString NfcShareHandler::data() const
{
return m_data;
}