You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by lo...@apache.org on 2012/05/18 08:10:44 UTC
qt commit: added move copy for file and directory, //TODO,
have the right callback
Updated Branches:
refs/heads/master a1627a028 -> 98c6ab857
added move copy for file and directory, //TODO, have the right callback
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/commit/98c6ab85
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/tree/98c6ab85
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/diff/98c6ab85
Branch: refs/heads/master
Commit: 98c6ab857571ff66e835fa1b3dfc2bdfa330967a
Parents: a1627a0
Author: Longwei Su <lo...@apache.org>
Authored: Fri May 18 02:09:34 2012 -0400
Committer: Longwei Su <lo...@apache.org>
Committed: Fri May 18 02:09:34 2012 -0400
----------------------------------------------------------------------
src/plugins/fileapi.cpp | 103 ++++++++++++++++++++++++++++++++----------
src/plugins/fileapi.h | 15 +++----
www/js/file.js | 34 +++++++++++---
3 files changed, 113 insertions(+), 39 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/blob/98c6ab85/src/plugins/fileapi.cpp
----------------------------------------------------------------------
diff --git a/src/plugins/fileapi.cpp b/src/plugins/fileapi.cpp
index f6e1e7a..0fbe6d4 100644
--- a/src/plugins/fileapi.cpp
+++ b/src/plugins/fileapi.cpp
@@ -81,8 +81,6 @@ void FileAPI::requestFileSystem( int scId, int ecId, unsigned short p_type, unsi
*/
void FileAPI::resolveLocalFileSystemURL( int scId, int ecId, QString p_url ) {
QUrl url = QUrl::fromUserInput( p_url );
- qDebug() << Q_FUNC_INFO << p_url;
-
// Check if we have a valid URL
if( !url.isValid() ) {
this->callback( ecId, "FileException.cast( FileException.ENCODING_ERR )" );
@@ -116,7 +114,6 @@ void FileAPI::resolveLocalFileSystemURL( int scId, int ecId, QString p_url ) {
* DirectoryEntry.getFile - http://www.w3.org/TR/file-system-api/#widl-DirectoryEntry-getFile
*/
void FileAPI::getFile( int scId, int ecId, QString p_path, QVariantMap p_options ) {
- qDebug() << Q_FUNC_INFO << p_path;
//NOTE: colon is not safe in url, it is not a valid path in Win and Mac, simple disable it here.
if(p_path.contains(":")){
this->callback( ecId, "FileException.cast( FileException.ENCODING_ERR )" );
@@ -181,7 +178,6 @@ void FileAPI::getFile( int scId, int ecId, QString p_path, QVariantMap p_options
* DirectoryEntry.getDirectory - http://www.w3.org/TR/file-system-api/#widl-DirectoryEntry-getDirectory
*/
void FileAPI::getDirectory( int scId, int ecId, QString p_path, QVariantMap p_options ) {
- qDebug() << Q_FUNC_INFO << p_path;
//NOTE: colon is not safe in url, it is not a valid path in Win and Mac, simple disable it here.
if(p_path.contains(":")){
this->callback( ecId, "FileException.cast( FileException.ENCODING_ERR )" );
@@ -249,8 +245,6 @@ void FileAPI::getDirectory( int scId, int ecId, QString p_path, QVariantMap p_op
*/
void FileAPI::removeRecursively( int scId, int ecId, QString p_path ) {
QDir dir( p_path );
- qDebug() << Q_FUNC_INFO << p_path;
-
if( FileAPI::rmDir(dir) ) {
this->callback( scId, "" );
return;
@@ -266,7 +260,6 @@ void FileAPI::removeRecursively( int scId, int ecId, QString p_path ) {
*/
void FileAPI::file( int scId, int ecId, QString p_path ) {
QFileInfo fileInfo(p_path);
- qDebug() << Q_FUNC_INFO << p_path;
if( !fileInfo.exists() ) {
this->callback( ecId, "FileException.cast( FileException.NOT_FOUND_ERR )" );
@@ -283,9 +276,6 @@ void FileAPI::file( int scId, int ecId, QString p_path ) {
*/
void FileAPI::write( int scId, int ecId, QString p_path, unsigned long long p_position, QString p_data ) {
QFile file( p_path );
-
- qDebug() << Q_FUNC_INFO << p_path;
-
// Check if file exists
if( !file.exists() ) {
this->callback( ecId, "FileException.cast( FileException.NOT_FOUND_ERR ), 0, 0" );
@@ -332,8 +322,6 @@ void FileAPI::write( int scId, int ecId, QString p_path, unsigned long long p_po
void FileAPI::truncate( int scId, int ecId, QString p_path, unsigned long long p_size ) {
QFile file(p_path);
- qDebug() << Q_FUNC_INFO << p_path;
-
// Check if file exists at all
if( !file.exists() ) {
this->callback( ecId, "FileException.cast( FileException.NOT_FOUND_ERR ), 0, 0" );
@@ -357,8 +345,6 @@ void FileAPI::truncate( int scId, int ecId, QString p_path, unsigned long long p
void FileAPI::getParent( int scId, int ecId, QString p_path ) {
QDir dir( p_path );
- qDebug() << Q_FUNC_INFO << p_path;
-
//can't cdup more than app's root
QDir root = QDir::current();
QString absPath = root.absolutePath() + "/doc";
@@ -370,8 +356,6 @@ void FileAPI::getParent( int scId, int ecId, QString p_path ) {
}
}
-
-
// Extract names and send back
this->callback( scId, "DirectoryEntry.cast( '" + dir.dirName() + "', '" + dir.absolutePath() + "' )" );
return;
@@ -382,8 +366,6 @@ void FileAPI::getParent( int scId, int ecId, QString p_path ) {
*/
void FileAPI::remove( int scId, int ecId, QString p_path ) {
QFileInfo fileInfo(p_path);
-
- qDebug() << Q_FUNC_INFO << p_path;
QDir root = QDir::current();
QString absPath = root.absolutePath() + "/doc";
// Check if entry exists at all
@@ -421,7 +403,6 @@ void FileAPI::remove( int scId, int ecId, QString p_path ) {
void FileAPI::getMetadata( int scId, int ecId, QString p_path ) {
QFileInfo fileInfo( p_path );
- qDebug() << Q_FUNC_INFO << p_path;
// Check if file exists
if( !fileInfo.exists() ) {
@@ -440,8 +421,6 @@ void FileAPI::getMetadata( int scId, int ecId, QString p_path ) {
void FileAPI::readEntries( int scId, int ecId, QString p_path ) {
QDir dir( p_path );
- qDebug() << Q_FUNC_INFO << p_path;
-
QString entriesList = "";
if( dir.exists() ) {
@@ -473,7 +452,6 @@ void FileAPI::readEntries( int scId, int ecId, QString p_path ) {
*/
void FileAPI::readAsText( int scId, int ecId, QString p_path ) {
QFile file( p_path );
- qDebug() << Q_FUNC_INFO << p_path;
// Check if file exists at all
if( !file.exists() ) {
@@ -497,7 +475,6 @@ void FileAPI::readAsText( int scId, int ecId, QString p_path ) {
void FileAPI::readAsDataURL( int scId, int ecId, QString p_path ) {
QFile file( p_path );
QFileInfo fileInfo( p_path );
- qDebug() << Q_FUNC_INFO << p_path;
if(p_path.startsWith("content:")){
this->callback( ecId, "FileException.cast( FileException.NOT_READABLE_ERR )" );
@@ -527,7 +504,6 @@ void FileAPI::readAsDataURL( int scId, int ecId, QString p_path ) {
* Helper function for recursively removing a directory
*/
bool FileAPI::rmDir( QDir p_dir ) {
- qDebug() << Q_FUNC_INFO << p_dir;
QDir dir = QDir::current();
QString absPath = dir.absolutePath() + "/doc";
if ( p_dir == absPath){//can't remove root dir
@@ -555,3 +531,82 @@ bool FileAPI::rmDir( QDir p_dir ) {
}
return result;
}
+
+void FileAPI::copyFile(int scId, int ecId,const QString& sourceFile, const QString& destinationParentDir, const QString& newName)
+{
+ qDebug()<< Q_FUNC_INFO;
+ QString destinationFile;
+ if (newName.isEmpty()){
+ QFileInfo fileInfo(sourceFile);
+ destinationFile = destinationParentDir + "/" + fileInfo.fileName();
+ destinationFile = destinationParentDir + "/" + newName;
+ } else {
+ }
+ if(QFile::copy(sourceFile, destinationFile)){
+ qDebug()<< Q_FUNC_INFO << "success";
+ };
+ this->callback( scId, "" );
+}
+
+void FileAPI::moveFile(int scId, int ecId,const QString& sourceFile, const QString& destinationParentDir, const QString& newName){
+ qDebug()<< Q_FUNC_INFO;
+ copyFile(scId,ecId,sourceFile, destinationParentDir, newName);
+ remove(scId,ecId, sourceFile);
+ this->callback( scId, "" );
+}
+
+void FileAPI::copyDir(int scId, int ecId,const QString& sourceFolder, const QString& destinationParentDir, const QString& newName)
+{
+ qDebug()<< Q_FUNC_INFO;
+ QDir sourceDir(sourceFolder);
+ QString destFolder(destinationParentDir + "/" + newName);
+ if(newName.isEmpty()){
+ destFolder = (destinationParentDir + "/" + sourceDir.dirName());
+ }
+ qDebug() << "destFolder: "<<destFolder;
+ QDir destDir(destFolder);
+ if(!destDir.exists())
+ {
+ qDebug() << "mkdir" << destDir.mkdir(destFolder);;
+ }
+ copyFolder(sourceFolder, destFolder);
+ this->callback( scId, "" );
+}
+
+void FileAPI::moveDir(int scId, int ecId,const QString& sourceDir, const QString& destinationParentDir, const QString& newName){
+ qDebug()<< Q_FUNC_INFO;
+ copyDir(scId, ecId, sourceDir, destinationParentDir,newName);
+ removeRecursively(scId, ecId,sourceDir );
+ this->callback( scId, "" );
+}
+
+//helper function to copy foler to new destination
+bool FileAPI::copyFolder(const QString& sourceFolder, const QString& destFolder)
+{
+ qDebug()<< Q_FUNC_INFO;
+ QDir sourceDir(sourceFolder);
+ if(!sourceDir.exists())
+ return false;
+ QDir destDir(destFolder);
+ if(!destDir.exists())
+ {
+ destDir.mkdir(destFolder);
+ }
+ QStringList files = sourceDir.entryList(QDir::Files);
+ for(int i = 0; i< files.count(); i++)
+ {
+ QString srcName = sourceFolder + "/" + files[i];
+ QString destName = destFolder + "/" + files[i];
+ QFile::copy(srcName, destName);
+ }
+ files.clear();
+ files = sourceDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
+ for(int i = 0; i< files.count(); i++)
+ {
+ QString srcName = sourceFolder + "/" + files[i];
+ QString destName = destFolder + "/" + files[i];
+ copyFolder(srcName, destName);
+ }
+ return true;
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/blob/98c6ab85/src/plugins/fileapi.h
----------------------------------------------------------------------
diff --git a/src/plugins/fileapi.h b/src/plugins/fileapi.h
index 6870886..3dc5c83 100644
--- a/src/plugins/fileapi.h
+++ b/src/plugins/fileapi.h
@@ -46,17 +46,14 @@ public slots:
void readEntries( int scId, int ecId, QString p_path );
void readAsDataURL( int scId, int ecId, QString p_path );
void readAsText( int scId, int ecId, QString p_path );
-// void copyTo();
-// void moveTo();
-// void canCopyMoveSrc();
-// void doCopyMove();
-// void getFileMetadata();
-// void readEntries();
-// void readFile();
-// void readAsText();
-// void readAsDataUrl();
+ void copyFile(int scId, int ecId,const QString& sourceFile, const QString& destinationParentDir, const QString& newName);
+ void moveFile(int scId, int ecId,const QString& sourceFile, const QString& destinationParentDir, const QString& newName);
+ void copyDir(int scId, int ecId,const QString& sourceFolder, const QString& destFolder, const QString& newName);
+ void moveDir(int scId, int ecId,const QString& sourceFolder, const QString& destFolder, const QString& newName);
+
protected:
static bool rmDir( QDir p_dir );
+ static bool copyFolder(const QString& sourceFolder, const QString& destFolder);
private:
static FileAPI* m_fileAPI;
http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/blob/98c6ab85/www/js/file.js
----------------------------------------------------------------------
diff --git a/www/js/file.js b/www/js/file.js
index c5a2a5a..c2d508b 100644
--- a/www/js/file.js
+++ b/www/js/file.js
@@ -119,12 +119,16 @@ Entry.prototype.getMetadata = function( successCallback, errorCallback ) {
Entry.prototype.setMetadata = function( successCallback, errorCallback ) {
//Cordova.exec(successCallback, errorCallback, "com.cordova.File", "setMetadata", [this.fullPath]);
}
-Entry.prototype.moveTo = function( parent, newName, successCallback, errorCallback ) {
- }
-Entry.prototype.moveTo = function( parent, newName, successCallback, errorCallback ) {
- }
-Entry.prototype.copyTo = function( parent, newName, successCallback, errorCallback ) {
- }
+/**
+ * Copies or Move a file to a new location
+ *
+ * @param {Entry} parent the directory to which to copy the entry
+ * @param {DOMString} newName the new name of the entry, defaults to the current name
+ */
+//Entry.prototype.moveTo = function( parent, newName, successCallback, errorCallback ) {
+// }
+//Entry.prototype.copyTo = function( parent, newName, successCallback, errorCallback ) {
+// }
Entry.prototype.toURL = function( mimeType ) {
return "file://" + this.fullPath;
}
@@ -352,6 +356,7 @@ FileReader.prototype.readAsArrayBuffer = function( file ) {
}
FileReader.prototype.readAsBinaryString = function( file ) {
}
+
FileReader.prototype.readAsText = function( file ) {
this.readyState = FileReader.EMPTY;
this.result = null;
@@ -442,6 +447,13 @@ FileEntry.prototype.file = function( successCallback, errorCallback ) {
// Lets get the fileinfo
Cordova.exec(successCallback, errorCallback, "com.cordova.File", "file", [this.fullPath]);
}
+FileEntry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) {
+ Cordova.exec(successCallback, errorCallback, "com.cordova.File", "copyFile", [this.fullPath, parent, newName]);
+};
+
+FileEntry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) {
+ Cordova.exec(successCallback, errorCallback, "com.cordova.File", "moveFile", [this.fullPath, parent, newName]);
+};
/**
* DirectoryReader interface
@@ -507,6 +519,16 @@ DirectoryEntry.prototype.removeRecursively = function( successCallback, errorCal
Cordova.exec(successCallback, errorCallback, "com.cordova.File", "removeRecursively", [this.fullPath]);
}
+DirectoryEntry.prototype.copyTo = function(parent, newName, successCallback, errorCallback) {
+ Cordova.exec(successCallback, errorCallback, "com.cordova.File", "copyDir", [this.fullPath, parent, newName]);
+};
+
+DirectoryEntry.prototype.moveTo = function(parent, newName, successCallback, errorCallback) {
+ Cordova.exec(successCallback, errorCallback, "com.cordova.File", "moveDir", [this.fullPath, parent, newName]);
+};
+
+
+
/**
* FileSystem interface
* http://www.w3.org/TR/file-system-api/#the-filesystem-interface