Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-14206] Ti.Database.install() #131

Merged
merged 3 commits into from
Jul 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/tibb/ImageLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2013 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#include "ImageLoader.h"

namespace titanium {

ImageLoader::ImageLoader(bb::cascades::ImageView* img, QUrl url)
{
imageView = img;
networkManager = new QNetworkAccessManager();
imageUrl = url;
if (networkManager)
{
QNetworkRequest networkRequest(url);
networkReply = networkManager->get(networkRequest);
connect(networkReply, SIGNAL(finished()), this, SLOT(onFinish()));
}
}

ImageLoader::~ImageLoader()
{
networkManager->deleteLater();
}

void ImageLoader::onFinish()
{
if (networkReply->error() != QNetworkReply::NoError)
{
qDebug() << "Some error occurred while fetching remote image";
deleteLater();
return;
}
QByteArray data(networkReply->readAll());
ImageLoader::saveToDisk(imageUrl, data);
bb::cascades::Image image = bb::cascades::Image(data);
imageView->setImage(image);
deleteLater();
}

void ImageLoader::loadImage(bb::cascades::ImageView* img, QUrl url)
{
// check if the file exist
if(ImageLoader::loadFromDisk(url, img))
return;
// otherwise get it from the server
new ImageLoader(img, url);
}

// Generates a temporary directory path for the image, using the image url as file name
QString ImageLoader::generateTempUrl(QUrl url)
{
QString base64 = QString("ti_").append(QString(url.toString().toUtf8().toBase64().constData()));

QString path = QDir::tempPath();
path.append("/");
path.append(base64);

return path;
}

// Saves the image to disk for later use
void ImageLoader::saveToDisk(QUrl url, QByteArray data)
{

QFile imageFile(ImageLoader::generateTempUrl(url));
if(!imageFile.open(QIODevice::WriteOnly)) return;

imageFile.write(data);
imageFile.close();
}

// Gets an image file from disk if it exists
bool ImageLoader::loadFromDisk(QUrl url, bb::cascades::ImageView* img)
{
QFile imageFile(ImageLoader::generateTempUrl(url));
if(imageFile.exists() && imageFile.open(QIODevice::ReadOnly))
{
bb::cascades::Image i = bb::cascades::Image(imageFile.readAll());
img->setImage(i);
imageFile.close();
return true;
}
return false;
}

} /* namespace titanium */
41 changes: 41 additions & 0 deletions src/tibb/ImageLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2013 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#ifndef IMAGELOADER_H_
#define IMAGELOADER_H_

#include <bb/cascades/ImageView>
#include <QObject>

namespace titanium {

class ImageLoader : public QObject
{
Q_OBJECT;
public:
static void loadImage(bb::cascades::ImageView *img, QUrl url);

public slots:
void onFinish();

private:
ImageLoader(bb::cascades::ImageView* img, QUrl url);
virtual ~ImageLoader();

QUrl imageUrl;
QNetworkReply* networkReply;
QNetworkAccessManager *networkManager;
bb::cascades::ImageView *imageView;

static void saveToDisk(QUrl url, QByteArray data);
static bool loadFromDisk(QUrl url, bb::cascades::ImageView* img);
static QString generateTempUrl(QUrl url);

};

} /* namespace titanium */
#endif /* IMAGELOADER_H_ */
7 changes: 7 additions & 0 deletions src/tibb/NativeImageViewObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <bb/cascades/ImageView>
#include <bb/cascades/ScalingMethod>

#include "ImageLoader.h"
#include "TiBlobObject.h"
#include "TiEventContainerFactory.h"
#include "TiObject.h"
Expand Down Expand Up @@ -52,8 +53,14 @@ int NativeImageViewObject::initialize()
int NativeImageViewObject::setImage(TiObject* obj)
{
Handle<Value> img = obj->getValue();
imageView_->resetImage();
if (img->IsString()) {
QString imagePath = V8ValueToQString(obj->getValue());
if(imagePath.startsWith("http://") || imagePath.startsWith("https://"))
{
ImageLoader::loadImage(imageView_, QUrl(imagePath));
return NATIVE_ERROR_OK;
}
imagePath = getResourcePath(imagePath);
imageView_->setImage(QUrl(imagePath));
} else {
Expand Down
40 changes: 39 additions & 1 deletion src/tibb/TiDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include "TiDatabase.h"
#include "TiDBObject.h"
#include "TiResultSetObject.h"

#include "V8Utils.h"
#include <QFile>
#include <QDir>
#include "TiGenericFunctionObject.h"

TiDatabase::TiDatabase()
Expand Down Expand Up @@ -40,9 +42,45 @@ void TiDatabase::onCreateStaticMembers()
TiProxy::onCreateStaticMembers();
TiDBObject::addObjectToParent(this, objectFactory_);
TiGenericFunctionObject::addGenericFunctionToParent(this, "open", this, _open);
TiGenericFunctionObject::addGenericFunctionToParent(this, "install", this, _install);
}

Handle<Value> TiDatabase::_install(void* userContext, TiObject* obj, const Arguments& args)
{
if(args.Length() == 0) return Undefined();
if(!args[0]->IsString()) return Undefined();
QString realName = titanium::V8StringToQString(args[0]->ToString());
QString givenName;
if(args.Length() > 0 && args[1]->IsString())
{
givenName = titanium::V8StringToQString(args[1]->ToString());
}
else
{
givenName = titanium::V8StringToQString(args[0]->ToString());
}

QString dataFolder = "data";
QString newFileName = dataFolder + "/" + givenName;
QFile newFile(newFileName);

if (!newFile.exists())
{
QString appFolder(QDir::homePath());
appFolder.chop(4);

QString originalFileName = appFolder + "app/native/assets/" + realName;
QFile originalFile(originalFileName);

if (originalFile.exists()) {
originalFile.copy(newFileName);
} else {
return Undefined();
}
}

return TiDatabase::_open(userContext, obj, args);
}
Handle<Value> TiDatabase::_open(void* userContext, TiObject* /*caller*/, const Arguments& args)
{
HandleScope handleScope;
Expand Down
1 change: 1 addition & 0 deletions src/tibb/TiDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TiDatabase : public TiProxy
virtual ~TiDatabase();
virtual void onCreateStaticMembers();
static Handle<Value> _open(void* userContext, TiObject* caller, const Arguments& args);
static Handle<Value> _install(void* userContext, TiObject* caller, const Arguments& args);

private:
TiDatabase();
Expand Down
32 changes: 32 additions & 0 deletions test/apps/native/tibbtest/Resources/database_install_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var win = Ti.UI.createWindow();

var btn = Ti.UI.createButton({
title: 'Install todo.sqlite'
});

// item text
// done int

btn.addEventListener('click', function(){

var db = Ti.Database.open("todo.sqlite" /*, "otherName" */);

var sSQL = "select * from todo";
var rows = db.execute(sSQL);

Ti.API.info('Row count: ' + rows.rowCount);

while(rows.isValidRow())
{
Ti.API.info(rows.fieldByName('item'));
rows.next();
}

alert('completed!');
rows.close();
db.close();
});

win.add(btn);

win.open();