Skip to content

Commit

Permalink
Save As | Share as txt Files
Browse files Browse the repository at this point in the history
  • Loading branch information
datluong committed Aug 2, 2013
1 parent 4815c94 commit 0d7a4e2
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 13 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 -lbbsystem
LIBS = -lbbdevice -lbb -lbbsystem -lbbcascadespickers

include(config.pri)

Expand Down
16 changes: 16 additions & 0 deletions assets/Editor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 175,22 @@ Page {
onTriggered: {
writerApp.actionShareDocument( titleTextArea.text, editorTextArea.text );
}
},
ActionItem {
title: "Share As File"
ActionBar.placement: ActionBarPlacement.InOverflow
imageSource: "asset:///images/ic_share_file.png"
onTriggered: {
writerApp.actionShareDocumentAsAttachment(titleTextArea.text, editorTextArea.text);
}
},
ActionItem {
title: "Save As"
ActionBar.placement: ActionBarPlacement.InOverflow
imageSource: "asset:///images/ic_save_as.png"
onTriggered: {
writerApp.actionSaveToSharedFolder(titleTextArea.text, editorTextArea.text);
}
}
]

Expand Down
Binary file added assets/images/ic_save_as.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/ic_share_file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
186 changes: 174 additions & 12 deletions src/writely/WriterUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 7,20 @@
#include <bb/cascades/OrientationSupport>
#include <bb/cascades/Sheet>
#include <bb/cascades/ArrayDataModel>
#include <bb/PpsObject>
#include <bb/cascades/pickers/FilePicker>

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

#include <bb/PpsObject>
#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 @@ -29,6 31,7 @@

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

namespace writely {

Expand All @@ -38,6 41,7 @@ WriterUI::WriterUI(bb::cascades::Application *app)
// private vars initialization
mEmbeddedData = QVariantMap();
_queryResults = NULL;
mToast = NULL;

// filesystem initialization
initializeDocumentFolder();
Expand Down Expand Up @@ -435,24 439,132 @@ QString WriterUI::correctFileName(const QString& fileName) {
// SHARING SUPPORT
///////////////////////////////////////////////////////////////////////////////

void WriterUI::actionShareDocument(QString title, QString body) {
void WriterUI::actionShareDocumentAsAttachment(QString title, QString body) {
InvokeManager invokeManager;
InvokeQueryTargetsRequest request;

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

request.setAction( "bb.action.SHARE" );
request.setMimeType( "*" );
request.setUri( QUrl("file://" QDir::currentPath() "/shared/documents/sample.txt") );
InvokeQueryTargetsReply* results = invokeManager.queryTargets(request);

if (!results) {
qWarning() << "Can't query MIME type";
}
else {
results->setParent(this);
connect(results, SIGNAL(finished()), this, SLOT(onWildcardQueryResponse()));
_queryResults = results;
}
}

/**
// Working code for Invoking Remember with title and content
void WriterUI::onWildcardQueryResponse() {
qDebug() << "WriterUI::onWildcardQueryResponse()";
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;
qDebug() << "Label" << action.label();
// 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(onWildcardShareTargetPicked(QVariant)) );
sheet->open();

delete _queryResults;
_queryResults = NULL;
}

QString WriterUI::exportToTempDir() {
// export the file to .slicktasks
QString fileName = correctFileName(mEmbeddedData["shareTitle"].toString().trimmed());
if (fileName.isEmpty()) fileName = "Untitled";
fileName = ".txt";

// create folder .slicktasks folder
QDir tmpDir(QDir::currentPath() "/shared/documents/.writer_tmp");

if (!tmpDir.exists()) {
qDebug() << "Temporary Directory does not exist. Creating.." << tmpDir.absolutePath();
if (!tmpDir.mkdir( tmpDir.absolutePath() )) {
qDebug() << "Can't create " << tmpDir.absolutePath();
return "";
}
}

QString filePath = tmpDir.absolutePath() "/" fileName;

QFile file(filePath);
if (file.open(QIODevice::WriteOnly)) {
file.write( mEmbeddedData["shareBody"].toString().toUtf8() );
file.close();
}
else {
return "";
}

return filePath;
}

void WriterUI::cleanTemporarySharedFolder() {
QDir tmpDir( QDir::currentPath() "/shared/documents/.writer_tmp" );
if (!tmpDir.exists()) return;

QStringList fileList = tmpDir.entryList( QStringList() << "*" );
foreach (QString fileName, fileList) {
if (fileName == "." || fileName == "..") continue;
QFile f(tmpDir.absolutePath() "/" fileName);
f.remove();
}
tmpDir.rmdir(tmpDir.absolutePath());
}


void WriterUI::onWildcardShareTargetPicked(QVariant target) {
qDebug() << "WriterUI::onWildcardShareTargetPicked:" << target;
if (!target.canConvert(QVariant::Map)) return;

QString filePath = exportToTempDir();
if (filePath.isEmpty())
return;
// invoke to share
QVariantMap map = target.toMap();
InvokeManager invokeManager;
InvokeRequest request;

request.setAction( "bb.action.ADD" );
request.setTarget( "sys.pim.remember.composer" );
request.setAction( map["action"].toString() );
request.setTarget( map["target"].toString() );
request.setMimeType( "*" );
request.setFileTransferMode( FileTransferMode::Preserve );
request.setUri( QUrl( "file://" filePath ) );

invokeManager.invoke( request );
}

QUrl uri( "remember://notebookentry?title=TheTitle&description=thedescsdderewrww" );
request.setUri( QString(uri.toEncoded()) );


invokeManager.invoke( request );
return
**/;

void WriterUI::actionShareDocument(QString title, QString body) {
qDebug() << "WriterUI::actionShareDocument";
InvokeManager invokeManager;
InvokeQueryTargetsRequest request;
Expand Down Expand Up @@ -563,6 675,46 @@ void WriterUI::onTextShareTargetPicked(QVariant target) {
invokeManager.invoke( request );
}


void WriterUI::actionSaveToSharedFolder(QString title, QString body) {
QString fileName = title.trimmed();
if (fileName.isEmpty()) fileName = "Untitled";
fileName = ".txt";

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

FilePicker* picker = new FilePicker();
picker->setMode( FilePickerMode::Saver );
picker->setViewMode( FilePickerViewMode::ListView );
picker->setDefaultSaveFileNames( QStringList() << fileName );
connect( picker, SIGNAL(fileSelected(const QStringList&)),
this, SLOT(onFileSelectedForSaveAsTxt(const QStringList&)) );

picker->open();

}

void WriterUI::onFileSelectedForSaveAsTxt(const QStringList& selectedFiles) {
if (selectedFiles.size() == 0 ) return;
if (!mEmbeddedData.contains("shareBody")) return;

qDebug() << "WriterUI::onFileSelectedForSaveAsTxt" << selectedFiles;

QString fileName = selectedFiles.first();

QFile file(fileName);

if (file.open(QIODevice::WriteOnly)) {
file.write( mEmbeddedData["shareBody"].toString().toUtf8() );
file.close();
showToasts("Saved.");
}
else {
showToasts("An error has occurred");
}
}

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

void dumpMetaObject(const QMetaObject* metaObject) {
Expand Down Expand Up @@ -591,6 743,13 @@ Page* WriterUI::currentEditorPage() {
return NULL;
}

void WriterUI::showToasts(const QString& message) {
if (mToast == NULL)
mToast = new bb::system::SystemToast( this );
mToast->setBody( message );
mToast->show();
}

bool WriterUI::isPhysicalKeyboardDevice2() {
bb::device::HardwareInfo hwInfo;
return hwInfo.isPhysicalKeyboardDevice();
Expand Down Expand Up @@ -628,8 787,11 @@ bool WriterUI::determineVirtualKeyboardShown(int screenWidth, int screenHeight)
void WriterUI::onAppAboutToQuit() {
qDebug() << "WriterUI::onAppAboutToQuit()";
Page* page = currentEditorPage();
if (page)
if (page) {
QMetaObject::invokeMethod(page, "handleAppExitEvent" );
}

cleanTemporarySharedFolder();
}

//connect( app, SIGNAL(thumbnail()), this, SLOT(onAppThumbnailed()) );
Expand Down
11 changes: 11 additions & 0 deletions src/writely/WriterUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 10,7 @@
#include <bb/cascades/Page>

#include <bb/system/InvokeQueryTargetsReply>
#include <bb/system/SystemToast>

using namespace bb::cascades;
using namespace bb::system;
Expand Down Expand Up @@ -49,11 50,14 @@ class WriterUI : public QObject
Q_INVOKABLE bool determineVirtualKeyboardShown(int screenWidth, int screenHeight);

Q_INVOKABLE void actionShareDocument(QString title, QString body);
Q_INVOKABLE void actionShareDocumentAsAttachment(QString title, QString body);
Q_INVOKABLE void actionSaveToSharedFolder(QString title, QString body);

private:
NavigationPane* mRootNavigationPane;
QVariantMap mEmbeddedData;
InvokeQueryTargetsReply * _queryResults;
bb::system::SystemToast* mToast;

QString untitledFilePath(const QString& path, int counter);
QString genFolderPath(QString path, int counter, QString defaultName = QString("Untitled Folder") );
Expand All @@ -66,6 70,9 @@ class WriterUI : public QObject
void initializeAutosave();

Page* currentEditorPage();
void showToasts( const QString& message );
QString exportToTempDir();
void cleanTemporarySharedFolder();

private slots:
void onAppAboutToQuit();
Expand All @@ -76,6 83,10 @@ private slots:
// sharing slots
void onTextQueryResponse();
void onTextShareTargetPicked(QVariant target);
void onFileSelectedForSaveAsTxt(const QStringList& selectedFiles);
void onWildcardQueryResponse();
void onWildcardShareTargetPicked(QVariant target);

};

}
Expand Down

0 comments on commit 0d7a4e2

Please sign in to comment.