[Archive] add InvalidStateError exception
authorWitold Choinkowski <w.choinkowsk@samsung.com>
Fri, 2 Jan 2015 13:21:05 +0000 (14:21 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 2 Feb 2015 09:28:29 +0000 (18:28 +0900)
Change-Id: I869fd3ab236d966e9da35dad7cd776e23102c215
Signed-off-by: Witold Choinkowski <w.choinkowsk@samsung.com>
src/archive/archive_api.js

index 80931e64d72d4fc7449d9fe707436bc2eeea67fb..d57b27d36086904cbdee15f95ed73ebbf6cb6f27 100644 (file)
@@ -149,7 +149,7 @@ var ArchiveCompressionLevel = {
  * This constructor is for internal use only.
  * It should be prohibited to call this constructor by user.
  */
-function ArchiveFileEntry(data) {
+function ArchiveFileEntry(data, priv) {
     if (!(this instanceof ArchiveFileEntry)) {
         return new ArchiveFileEntry(data);
     }
@@ -162,64 +162,70 @@ function ArchiveFileEntry(data) {
     propertyFactory_(this, 'size',           data.size                      || 0,     Property.E);
     propertyFactory_(this, 'compressedSize', data.compressedSize            || 0,     Property.E);
     propertyFactory_(this, 'modified',       new Date(data.modified * 1000) || null , Property.E);
-    propertyFactory_(this, '_handle',        data.handle                    || -1 );
-}
 
-/**
- * Extracts ArchiveFileEntry to the given location.
- */
-ArchiveFileEntry.prototype.extract = function () {
-    var args = validator_.validateArgs(arguments, [
-        { name: "destinationDirectory", type: types_.STRING }, //TODO: add FileReferece validation
-        { name: "onsuccess", type: types_.FUNCTION, optional: true, nullable: true },
-        { name: "onerror", type: types_.FUNCTION, optional: true, nullable: true },
-        { name: "onprogress", type: types_.FUNCTION, optional: true, nullable: true },
-        { name: "stripName", type: types_.STRING, optional: true, nullable: true },
-        { name: "overwrite", type: types_.BOOLEAN, optional: true, nullable: true }
-    ]),
-    opId = getNextOpId();
+    function getHandle() {
+        if(priv.handle)
+            return priv.handle;
+        else throw new tizen.WebAPIException(tizen.WebAPIException.UNKNOWN_ERR, 'Archive is not opened');
+    }
 
-    if (!CommonFS.isVirtualPath(args.destinationDirectory)) //TODO: add FileReferece validation
-        throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR,
-                "Destination directory should be virtual path or file.");
-    bridge.async({
-        cmd: 'ArchiveFileEntry_extract',
-        args: {
-            destinationDirectory: CommonFS.toRealPath(args.destinationDirectory),
-            stripName: args.stripName || null,
-            overwrite: args.overwrite || null,
-            opId: opId,
-            handle: this._handle,
-            name: this.name
-        }
-    }).then({
-        success: function () {
-            if (args.onsuccess) {
-                args.onsuccess.call(null);
-            }
-        },
-        error: function (e) {
-            if (args.onerror) {
-                args.onerror.call(
-                    null,
-                    new tizen.WebAPIException(e.code, e.message, e.name)
-                );
+    /**
+     * Extracts ArchiveFileEntry to the given location.
+     */
+    this.extract = function () {
+        var args = validator_.validateArgs(arguments, [
+            { name: "destinationDirectory", type: types_.STRING }, //TODO: add FileReferece validation
+            { name: "onsuccess", type: types_.FUNCTION, optional: true, nullable: true },
+            { name: "onerror", type: types_.FUNCTION, optional: true, nullable: true },
+            { name: "onprogress", type: types_.FUNCTION, optional: true, nullable: true },
+            { name: "stripName", type: types_.STRING, optional: true, nullable: true },
+            { name: "overwrite", type: types_.BOOLEAN, optional: true, nullable: true }
+        ]),
+        opId = getNextOpId();
+
+        if (!CommonFS.isVirtualPath(args.destinationDirectory)) //TODO: add FileReferece validation
+            throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR,
+                    "Destination directory should be virtual path or file.");
+        bridge.async({
+            cmd: 'ArchiveFileEntry_extract',
+            args: {
+                destinationDirectory: CommonFS.toRealPath(args.destinationDirectory),
+                stripName: args.stripName || null,
+                overwrite: args.overwrite || null,
+                opId: opId,
+                handle: getHandle(),
+                name: this.name
             }
-        },
-        progress: function (data) {
-            if (args.onprogress) {
-                args.onprogress.call(
-                    null,
-                    opId,
-                    data.value,
-                    data.filename
-                );
+        }).then({
+            success: function () {
+                if (args.onsuccess) {
+                    args.onsuccess.call(null);
+                }
+            },
+            error: function (e) {
+                if (args.onerror) {
+                    args.onerror.call(
+                        null,
+                        new tizen.WebAPIException(e.code, e.message, e.name)
+                    );
+                }
+            },
+            progress: function (data) {
+                if (args.onprogress) {
+                    args.onprogress.call(
+                        null,
+                        opId,
+                        data.value,
+                        data.filename
+                    );
+                }
             }
-        }
-    });
+        });
+
+        return opId;
+    };
+}
 
-    return opId;
-};
 
 /**
  * The ArchiveManager interface provides methods for global operations related to ArchiveFile.
@@ -241,209 +247,221 @@ function ArchiveFile(data) {
 
     propertyFactory_(this, 'mode'            , data.mode             || "r", Property.E);
     propertyFactory_(this, 'decompressedSize', data.decompressedSize || 0,   Property.E);
-    propertyFactory_(this, '_handle',          data.handle           || -1 );
-}
-
-/**
- * Adds a new member file to ArchiveFile.
- */
-ArchiveFile.prototype.add = function () {
-    var args = validator_.validateArgs(arguments, [
-        { name: "sourceFile", type: types_.STRING }, //TODO: add FileReferece validation
-        { name: "onsuccess", type: types_.FUNCTION, optional: true, nullable: true },
-        { name: "onerror", type: types_.FUNCTION, optional: true, nullable: true },
-        { name: "onprogress", type: types_.FUNCTION, optional: true, nullable: true },
-        { name: "options", type: types_.DICTIONARY, optional: true, nullable: true }
-    ]),
-    opId = getNextOpId();
 
-    if (!CommonFS.isVirtualPath(args.sourceFile)) //TODO: add FileReferece validation
-        throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR,
-                "sourceFile should be virtual path or file.");
-
-    var optionsAttributes = ["destination", "stripSourceDirectory", "compressionLevel"],
-        options = args.options || {};
+    var priv ={ handle: data.handle };
 
-    for(var i in optionsAttributes) {
-        if (!options[optionsAttributes[i]]) {
-            options[optionsAttributes[i]] = null;
-        }
+    function getHandle() {
+        if(priv.handle)
+            return priv.handle;
+        else throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_STATE_ERR, 'ArchiveFile closed - operation not permitted');
     }
 
-    bridge.async({
-        cmd: 'ArchiveFile_add',
-        args: {
-            sourceFile: CommonFS.toRealPath(args.sourceFile),
-            options: options,
-            opId: opId,
-            handle: this._handle
-        }
-    }).then({
-        success: function () {
-            if (args.onsuccess) {
-                args.onsuccess.call(null);
-            }
-        },
-        error: function (e) {
-            if (args.onerror) {
-                args.onerror.call(
-                    null,
-                    new tizen.WebAPIException(e.code, e.message, e.name)
-                );
-            }
-        },
-        progress: function (data) {
-            if (args.onprogress) {
-                args.onprogress.call(
-                    null,
-                    opId,
-                    data.value,
-                    data.filename
-                );
+    /**
+     * Adds a new member file to ArchiveFile.
+     */
+    this.add = function () {
+        var args = validator_.validateArgs(arguments, [
+            { name: "sourceFile", type: types_.STRING }, //TODO: add FileReferece validation
+            { name: "onsuccess", type: types_.FUNCTION, optional: true, nullable: true },
+            { name: "onerror", type: types_.FUNCTION, optional: true, nullable: true },
+            { name: "onprogress", type: types_.FUNCTION, optional: true, nullable: true },
+            { name: "options", type: types_.DICTIONARY, optional: true, nullable: true }
+        ]),
+        opId = getNextOpId();
+
+        if (!CommonFS.isVirtualPath(args.sourceFile)) //TODO: add FileReferece validation
+            throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR,
+                    "sourceFile should be virtual path or file.");
+
+        var optionsAttributes = ["destination", "stripSourceDirectory", "compressionLevel"],
+            options = args.options || {};
+
+        for(var i in optionsAttributes) {
+            if (!options[optionsAttributes[i]]) {
+                options[optionsAttributes[i]] = null;
             }
         }
-    });
 
-    return opId;
-};
-
-/**
- * Extracts every file from this ArchiveFile to a given directory.
- */
-ArchiveFile.prototype.extractAll = function () {
-    var args = validator_.validateArgs(arguments, [
-        { name: "destinationDirectory", type: types_.STRING }, //TODO: add FileReferece validation
-        { name: "onsuccess", type: types_.FUNCTION, optional: true, nullable: true },
-        { name: "onerror", type: types_.FUNCTION, optional: true, nullable: true },
-        { name: "onprogress", type: types_.FUNCTION, optional: true, nullable: true },
-        { name: "overwrite", type: types_.BOOLEAN, optional: true, nullable: true }
-    ]),
-    opId = getNextOpId();
+        bridge.async({
+            cmd: 'ArchiveFile_add',
+            args: {
+                sourceFile: CommonFS.toRealPath(args.sourceFile),
+                options: options,
+                opId: opId,
+                handle: getHandle()
+            }
+        }).then({
+            success: function () {
+                if (args.onsuccess) {
+                    args.onsuccess.call(null);
+                }
+            },
+            error: function (e) {
+                if (args.onerror) {
+                    args.onerror.call(
+                        null,
+                        new tizen.WebAPIException(e.code, e.message, e.name)
+                    );
+                }
+            },
+            progress: function (data) {
+                if (args.onprogress) {
+                    args.onprogress.call(
+                        null,
+                        opId,
+                        data.value,
+                        data.filename
+                    );
+                }
+            }
+        });
 
-    if (!CommonFS.isVirtualPath(args.destinationDirectory)) //TODO: add FileReferece validation
-        throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR,
-                "destinationDirectory should be virtual path or file.");
+        return opId;
+    };
 
-    bridge.async({
-        cmd: 'ArchiveFile_extractAll',
-        args: {
-            destinationDirectory: CommonFS.toRealPath(args.destinationDirectory),
-            overwrite: args.overwrite || null,
-            opId: opId,
-            handle: this._handle
-        }
-    }).then({
-        success: function () {
-            if (args.onsuccess) {
-                args.onsuccess.call(null);
-            }
-        },
-        error: function (e) {
-            if (args.onerror) {
-                args.onerror.call(
-                    null,
-                    new tizen.WebAPIException(e.code, e.message, e.name)
-                );
+    /**
+     * Extracts every file from this ArchiveFile to a given directory.
+     */
+    this.extractAll = function () {
+        var args = validator_.validateArgs(arguments, [
+            { name: "destinationDirectory", type: types_.STRING }, //TODO: add FileReferece validation
+            { name: "onsuccess", type: types_.FUNCTION, optional: true, nullable: true },
+            { name: "onerror", type: types_.FUNCTION, optional: true, nullable: true },
+            { name: "onprogress", type: types_.FUNCTION, optional: true, nullable: true },
+            { name: "overwrite", type: types_.BOOLEAN, optional: true, nullable: true }
+        ]),
+        opId = getNextOpId();
+
+        if (!CommonFS.isVirtualPath(args.destinationDirectory)) //TODO: add FileReferece validation
+            throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR,
+                    "destinationDirectory should be virtual path or file.");
+
+        bridge.async({
+            cmd: 'ArchiveFile_extractAll',
+            args: {
+                destinationDirectory: CommonFS.toRealPath(args.destinationDirectory),
+                overwrite: args.overwrite || null,
+                opId: opId,
+                handle: getHandle()
             }
-        },
-        progress: function (data) {
-            if (args.onprogress) {
-                args.onprogress.call(
-                    null,
-                    opId,
-                    data.value,
-                    data.filename
-                );
+        }).then({
+            success: function () {
+                if (args.onsuccess) {
+                    args.onsuccess.call(null);
+                }
+            },
+            error: function (e) {
+                if (args.onerror) {
+                    args.onerror.call(
+                        null,
+                        new tizen.WebAPIException(e.code, e.message, e.name)
+                    );
+                }
+            },
+            progress: function (data) {
+                if (args.onprogress) {
+                    args.onprogress.call(
+                        null,
+                        opId,
+                        data.value,
+                        data.filename
+                    );
+                }
             }
-        }
-    });
+        });
 
-    return opId;
-};
-
-/**
- * Retrieves information about the member files in ArchiveFile.
- */
-ArchiveFile.prototype.getEntries = function () {
-    var args = validator_.validateArgs(arguments, [
-        { name: "onsuccess", type: types_.FUNCTION },
-        { name: "onerror", type: types_.FUNCTION, optional: true, nullable: true }
-    ]),
-    opId = getNextOpId();
+        return opId;
+    };
 
-    bridge.async({
-        cmd: 'ArchiveFile_getEntries',
-        args: {
-            opId: opId,
-            handle: this._handle
-        }
-    }).then({
-        success: function (data) {
-            var entries = [];
-            data.forEach(function (e) {
-                entries.push(new ArchiveFileEntry(e));
-            });
-            args.onsuccess.call(null, entries);
-        },
-        error: function (e) {
-            if (args.onerror) {
-                args.onerror.call(
-                    null,
-                    new tizen.WebAPIException(e.code, e.message, e.name)
-                );
+    /**
+     * Retrieves information about the member files in ArchiveFile.
+     */
+    this.getEntries = function () {
+        var args = validator_.validateArgs(arguments, [
+            { name: "onsuccess", type: types_.FUNCTION },
+            { name: "onerror", type: types_.FUNCTION, optional: true, nullable: true }
+        ]),
+        opId = getNextOpId();
+
+        bridge.async({
+            cmd: 'ArchiveFile_getEntries',
+            args: {
+                opId: opId,
+                handle: getHandle()
             }
-        }
-    });
-
-    return opId;
-};
+        }).then({
+            success: function (data) {
+                var entries = [];
+                data.forEach(function (e) {
+                    entries.push(new ArchiveFileEntry(e, priv));
+                });
+                args.onsuccess.call(null, entries);
+            },
+            error: function (e) {
+                if (args.onerror) {
+                    args.onerror.call(
+                        null,
+                        new tizen.WebAPIException(e.code, e.message, e.name)
+                    );
+                }
+            }
+        });
 
-/**
- * Retrieves information about ArchiveFileEntry with the specified name in ArchiveFile.
- */
-ArchiveFile.prototype.getEntryByName = function () {
-    var args = validator_.validateArgs(arguments, [
-        { name: "name", type: types_.STRING },
-        { name: "onsuccess", type: types_.FUNCTION },
-        { name: "onerror", type: types_.FUNCTION, optional: true, nullable: true }
-    ]),
-    opId = getNextOpId();
+        return opId;
+    };
 
-    bridge.async({
-        cmd: 'ArchiveFile_getEntryByName',
-        args: {
-            name: args.name,
-            opId: opId,
-            handle: this._handle
-        }
-    }).then({
-        success: function (data) {
-            args.onsuccess.call(null, new ArchiveFileEntry(data));
-        },
-        error: function (e) {
-            if (args.onerror) {
-                args.onerror.call(
-                    null,
-                    new tizen.WebAPIException(e.code, e.message, e.name)
-                );
+    /**
+     * Retrieves information about ArchiveFileEntry with the specified name in ArchiveFile.
+     */
+    this.getEntryByName = function () {
+        var args = validator_.validateArgs(arguments, [
+            { name: "name", type: types_.STRING },
+            { name: "onsuccess", type: types_.FUNCTION },
+            { name: "onerror", type: types_.FUNCTION, optional: true, nullable: true }
+        ]),
+        opId = getNextOpId();
+
+        bridge.async({
+            cmd: 'ArchiveFile_getEntryByName',
+            args: {
+                name: args.name,
+                opId: opId,
+                handle: getHandle()
             }
-        }
-    });
+        }).then({
+            success: function (data) {
+                args.onsuccess.call(null, new ArchiveFileEntry(data, priv));
+            },
+            error: function (e) {
+                if (args.onerror) {
+                    args.onerror.call(
+                        null,
+                        new tizen.WebAPIException(e.code, e.message, e.name)
+                    );
+                }
+            }
+        });
 
-    return opId;
-};
+        return opId;
+    };
 
-/**
- * Closes the ArchiveFile.
- */
-ArchiveFile.prototype.close = function () {
-    bridge.sync({
-        cmd: 'ArchiveFile_close',
-        args: {
-            handle: this._handle
+    /**
+     * Closes the ArchiveFile.
+     */
+    this.close = function () {
+        var handle = priv.handle;
+        if(priv.handle) {
+            delete priv.handle;
+            bridge.sync({
+                cmd: 'ArchiveFile_close',
+                args: {
+                    handle: handle
+                }
+            });
         }
-    });
-};
+    };
+}
+
 
 function ArchiveManager() {
 }