Skip to content

Commit

Permalink
Sharing text to other apps
Browse files Browse the repository at this point in the history
  • Loading branch information
datluong committed Aug 2, 2013
1 parent dc8141f commit 4815c94
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Writer.pro
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
APP_NAME = Writer

CONFIG = qt warn_on cascades10
LIBS = -lbbdevice -lbb
LIBS = -lbbdevice -lbb -lbbsystem

include(config.pri)

Expand Down
11 changes: 11 additions & 0 deletions assets/Editor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 167,17 @@ Page {
}
]

actions: [
ActionItem {
title: "Share"
ActionBar.placement: ActionBarPlacement.OnBar
imageSource: "asset:///images/ic_share.png"
onTriggered: {
writerApp.actionShareDocument( titleTextArea.text, editorTextArea.text );
}
}
]

function updateWordCount(count) {
var t = '' count (count < 2 ? ' word' : ' words');
wordCountLabel.setText( t );
Expand Down
52 changes: 52 additions & 0 deletions assets/ShareTargetPickerSheet.qml
Original file line number Diff line number Diff line change
@@ -0,0 1,52 @@
import bb.cascades 1.0
import bb.system 1.0

Sheet {
id: shareTargetPickerSheet
signal shareTargetPicked(variant type);

Page {
titleBar: TitleBar {
title: "Share"
visibility: ChromeVisibility.Visible
dismissAction: ActionItem {
title: "Cancel"
onTriggered: {
shareTargetPickerSheet.close();
}
}
}
Container {
Container {
preferredHeight: 0
horizontalAlignment: HorizontalAlignment.Fill
}
ListView {
dataModel: shareModels
topPadding: 12
listItemComponents: [
ListItemComponent {
StandardListItem {
title: ListItemData.title
imageSource: ListItemData.imageSource
}
}
]
onTriggered: {
console.log('list view triggered:', indexPath);
if (indexPath < 0 || indexPath >= shareModels.size()) return;
shareTargetPicked( shareModels.value(indexPath) );
shareTargetPickerSheet.close();
}
} // end of ListView
} // end of Main Containre
} // end of Page

attachedObjects: [
ArrayDataModel {
id: shareModels
objectName: "shareModels"
}
]

}// end of Sheet
Binary file added assets/images/ic_share.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
150 changes: 149 additions & 1 deletion src/writely/WriterUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 5,20 @@
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/cascades/OrientationSupport>
#include <bb/cascades/Sheet>
#include <bb/cascades/ArrayDataModel>
#include <bb/PpsObject>

#include <bb/device/HardwareInfo>
#include <bb/device/DisplayInfo>

#include <bb/system/InvokeManager>
#include <bb/system/InvokeRequest>
#include <bb/system/InvokeQueryTargetsRequest>
#include <bb/system/InvokeQueryTargetsReply>
#include <bb/system/InvokeReplyError>
#include <bb/system/SystemDialog>

#include <QDir>
#include <QFileInfoList>
#include <QFileInfo>
Expand All @@ -16,14 27,17 @@
#include <QMetaMethod>
#include <QTimer>


using namespace bb::cascades;
using namespace bb::system;

namespace writely {

WriterUI::WriterUI(bb::cascades::Application *app)
: QObject(app)
{
// private vars initialization
mEmbeddedData = QVariantMap();
_queryResults = NULL;

// filesystem initialization
initializeDocumentFolder();
Expand Down Expand Up @@ -417,6 431,140 @@ QString WriterUI::correctFileName(const QString& fileName) {
return name.trimmed();
}

///////////////////////////////////////////////////////////////////////////////
// SHARING SUPPORT
///////////////////////////////////////////////////////////////////////////////

void WriterUI::actionShareDocument(QString title, QString body) {

/**
// Working code for Invoking Remember with title and content
InvokeManager invokeManager;
InvokeRequest request;
request.setAction( "bb.action.ADD" );
request.setTarget( "sys.pim.remember.composer" );
QUrl uri( "remember://notebookentry?title=TheTitle&description=thedescsdderewrww" );
request.setUri( QString(uri.toEncoded()) );
invokeManager.invoke( request );
return
**/;

qDebug() << "WriterUI::actionShareDocument";
InvokeManager invokeManager;
InvokeQueryTargetsRequest request;

request.setAction( "bb.action.SHARE" );
request.setMimeType( "text/plain" );

mEmbeddedData["shareTitle"] = title;
mEmbeddedData["shareBody"] = body;

InvokeQueryTargetsReply* results = invokeManager.queryTargets(request);
if (!results) {
qWarning() << "Can't query MIME type";
}
else {
results->setParent(this);
connect(results, SIGNAL(finished()), this, SLOT(onTextQueryResponse()));
_queryResults = results;
}
}

void WriterUI::onTextQueryResponse() {
qDebug() << "WriterUI::onTextQueryResponse()";

if (_queryResults == NULL) return;
if (_queryResults->error() != InvokeReplyError::None) {
qDebug() << "InvokeReplyError:" << _queryResults->error();
return;
}

if (_queryResults->actions().size() == 0) return;
InvokeAction action = _queryResults->actions().first();
if (action.targets().size() == 0) return;

// load the sheet
QmlDocument* qml = QmlDocument::create("asset:///ShareTargetPickerSheet.qml");
Sheet* sheet = qml->createRootObject<Sheet>();
ArrayDataModel* dataModel = sheet->findChild<ArrayDataModel*>("shareModels");
qDebug() << "dataModel" << (dataModel!=NULL?true:false);
if (dataModel) {
foreach (InvokeTarget target, action.targets()) {
QVariantMap map;
map["title"] = target.label();
map["imageSource"] = QUrl( QString("file://") target.icon().toString() );
map["target"] = target.name();
map["action"] = action.name();
dataModel->append(map);
}
}
connect( sheet, SIGNAL(shareTargetPicked(QVariant)), this, SLOT(onTextShareTargetPicked(QVariant)) );
sheet->open();

delete _queryResults;
_queryResults = NULL;
};


void WriterUI::onTextShareTargetPicked(QVariant target) {
qDebug() << "WriterUI::onTextShareTargetPicked:" << target;
if (!target.canConvert(QVariant::Map)) return;
if (!mEmbeddedData.contains("shareBody")) return;

QVariantMap map = target.toMap();
if (!map.contains("target")) return;
QString targetName = map["target"].toString();

InvokeManager invokeManager;
InvokeRequest request;

if (targetName == "sys.pim.remember.composer") {
// custom code for sharing title to remember
request.setAction( "bb.action.ADD" );
request.setTarget( "sys.pim.remember.composer" );

QUrl uri( "remember://notebookentry" );
uri.addEncodedQueryItem( QString("title").toAscii(), QUrl::toPercentEncoding( mEmbeddedData["shareTitle"].toString() ) );
uri.addEncodedQueryItem( QString("description").toAscii(), QUrl::toPercentEncoding( mEmbeddedData["shareBody"].toString() ) );
request.setUri( uri );

invokeManager.invoke( request );
return;
}

if (targetName == "sys.pim.uib.email.hybridcomposer" ) {
request.setTarget( "sys.pim.uib.email.hybridcomposer" );
request.setAction( "bb.action.COMPOSE" );
request.setMimeType( "message/rfc822" );

// prepare body
QVariantMap map;
map["subject"] = mEmbeddedData["shareTitle"].toString();
map["body"] = mEmbeddedData["shareBody"].toString();

QVariantMap moreData;
moreData["data"] = map;
bool ok=true;
request.setData( bb::PpsObject::encode(moreData, &ok) );

invokeManager.invoke(request);
return;
}

request.setAction( map["action"].toString() );
request.setTarget( map["target"].toString() );
request.setData( mEmbeddedData["shareBody"].toString().toUtf8() );
request.setMimeType( "text/plain" );

invokeManager.invoke( request );
}

// End of Sharing Section /////////////////////////////////////////////////////

void dumpMetaObject(const QMetaObject* metaObject) {
qDebug() << "dumpMetaObject" << metaObject;
if (metaObject == NULL) return;
Expand Down
11 changes: 11 additions & 0 deletions src/writely/WriterUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 9,10 @@
#include <bb/cascades/NavigationPane>
#include <bb/cascades/Page>

#include <bb/system/InvokeQueryTargetsReply>

using namespace bb::cascades;
using namespace bb::system;

namespace bb { namespace cascades { class Application; }}

Expand Down Expand Up @@ -44,8 47,13 @@ class WriterUI : public QObject
Q_INVOKABLE QString correctFileName(const QString& fileName);

Q_INVOKABLE bool determineVirtualKeyboardShown(int screenWidth, int screenHeight);

Q_INVOKABLE void actionShareDocument(QString title, QString body);

private:
NavigationPane* mRootNavigationPane;
QVariantMap mEmbeddedData;
InvokeQueryTargetsReply * _queryResults;

QString untitledFilePath(const QString& path, int counter);
QString genFolderPath(QString path, int counter, QString defaultName = QString("Untitled Folder") );
Expand All @@ -65,6 73,9 @@ private slots:
void onAppThumbnailed();
void onAppFullscreen();

// sharing slots
void onTextQueryResponse();
void onTextShareTargetPicked(QVariant target);
};

}
Expand Down

0 comments on commit 4815c94

Please sign in to comment.