/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is 
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "nsISupports.idl"

interface nsIInputStream;
interface nsIOutputStream;
interface nsIURI;
interface nsIFile;

/**
 * nsIStreamIO is an abstract interface that gives access to input and output
 * streams on an object. Perhaps the most interesting subclass of this is
 * nsIFileIO which allows access to input and output streams to files.
 */
[scriptable, uuid(d6c01ab2-0d04-11d4-986e-00c04fa0cf4a)]
interface nsIStreamIO : nsISupports
{
    /**
     * Logically opens a stream I/O object.  This method may block the
     * calling thread pending i/o or other delays.
     */
    void open();

    /**
     * Logically closes a stream I/O object. A status value is passed in
     * to indicate a successful close (NS_OK) or failure.
     */
    void close(in nsresult status);
    
    /**
     * Gets an input stream from a stream I/O object. 
     */
    readonly attribute nsIInputStream inputStream;

    /**
     * Gets an output stream from a stream I/O object. 
     */
    readonly attribute nsIOutputStream outputStream;

    /**
     * The 'name' of a stream I/O object. This name is often
     * used for display purposes.
     */
    readonly attribute AUTF8String name;

    /**
     * Associated content type, if any.
     */
    readonly attribute ACString contentType; 

    /**
     * Associated content charset, if any.
     */
    readonly attribute ACString contentCharset;

    /**
     * Associated content length; -1 if unknown.
     */
    readonly attribute long contentLength;
};

////////////////////////////////////////////////////////////////////////////////
// nsIFileIO

/**
 * nsIFileIO specializes nsIStreamIO to allow initialization from an nsIFile
 * object. For this implementation, the name attribute will correspond to the
 * path to the file.
 */
[scriptable, uuid(2a45fb42-0d06-11d4-986e-00c04fa0cf4a)]
interface nsIFileIO : nsIStreamIO 
{
    void init(in nsIFile file,
              in long ioFlags,
              in long perm);

    readonly attribute nsIFile file;
};

%{C++

#include "nsCOMPtr.h"
#include "nsIComponentManager.h"

#define NS_FILEIO_CLASSNAME     "File I/O"
#define NS_FILEIO_CONTRACTID        "@mozilla.org/network/file-io;1"

#define NS_FILEIO_CID                                \
{ /* 0965ce3e-0d06-11d4-986e-00c04fa0cf4a */         \
    0x0965ce3e,                                      \
    0x0d06,                                          \
    0x11d4,                                          \
    {0x98, 0x6e, 0x00, 0xc0, 0x4f, 0xa0, 0xcf, 0x4a} \
}

inline nsresult
NS_NewFileIO(nsIFileIO **result,
             nsIFile* file,
             PRInt32 ioFlags = -1,
             PRInt32 perm = -1)
{
    nsresult rv;
    nsCOMPtr<nsIFileIO> fileIO;
    static NS_DEFINE_CID(kFileIOCID, NS_FILEIO_CID);
    rv = nsComponentManager::CreateInstance(kFileIOCID,
                                            nsnull, 
                                            NS_GET_IID(nsIFileIO),
                                            getter_AddRefs(fileIO));
    if (NS_FAILED(rv)) return rv;
    rv = fileIO->Init(file, ioFlags, perm);
    if (NS_FAILED(rv)) return rv;

    *result = fileIO;
    NS_ADDREF(*result);
    return NS_OK;
}

%}

////////////////////////////////////////////////////////////////////////////////
// nsIInputStreamIO

/**
 * nsIInputStreamIO specializes nsIStreamIO to allow initialization from an
 * input stream, name, content type and length. Note that attempts to access
 * the output stream of an nsIInputStreamIO will fail. This implementation
 * is provided as a convenience, to avoid the need to implement the complete
 * nsIStreamIO interface, when all you need is the input stream part.
 */
[scriptable, uuid(2d64af08-0d06-11d4-986e-00c04fa0cf4a)]
interface nsIInputStreamIO : nsIStreamIO 
{
    void init(in AUTF8String name,
              in nsIInputStream input,
              in ACString contentType, 
              in ACString contentCharset,
              in long contentLength);
};

%{C++

#define NS_INPUTSTREAMIO_CLASSNAME "Input Stream I/O"
#define NS_INPUTSTREAMIO_CONTRACTID    "@mozilla.org/network/input-stream-io;1"

#define NS_INPUTSTREAMIO_CID                         \
{ /* 0f5e1198-0d06-11d4-986e-00c04fa0cf4a */         \
    0x0f5e1198,                                      \
    0x0d06,                                          \
    0x11d4,                                          \
    {0x98, 0x6e, 0x00, 0xc0, 0x4f, 0xa0, 0xcf, 0x4a} \
}

inline nsresult
NS_NewInputStreamIO(nsIInputStreamIO* *result,
                    const nsACString &name,
                    nsIInputStream* inStr,
                    const nsACString &contentType,
                    const nsACString &contentCharset,
                    PRInt32 contentLength)
{
    nsresult rv;
    nsCOMPtr<nsIInputStreamIO> io;
    static NS_DEFINE_CID(kInputStreamIOCID, NS_INPUTSTREAMIO_CID);
    rv = nsComponentManager::CreateInstance(kInputStreamIOCID,
                                            nsnull, 
                                            NS_GET_IID(nsIInputStreamIO),
                                            getter_AddRefs(io));
    if (NS_FAILED(rv)) return rv;
    rv = io->Init(name, inStr, contentType, contentCharset, contentLength);
    if (NS_FAILED(rv)) return rv;

    *result = io;
    NS_ADDREF(*result);
    return NS_OK;
}

%}

////////////////////////////////////////////////////////////////////////////////
