/** @file MOMPackageManager.cpp
 *
 * Copyright (C) Espial Limited 2014 - Company confidential - All rights reserved.
 *
 */

#include "config.h"

#if ENABLE(RMAI)

#include "MOMPackageManager.h"
#include "MOMPackageList.h"
#include "MOMPackageDescriptor.h"

#include "UtilsMOM.h"
#include "MOMException.h"
#include "EventNames.h"

extern "C" {
#include "layers.h"
#include "cdf/mom_package_manager.h"
#include "cdf/mom_apps_manager.h"
#include "cdf/mom_content_list.h"
#include "cdf/mom_channels_local.h"
}

namespace WebCore {

PassRefPtr<MOMPackageManager> MOMPackageManager::create(ScriptExecutionContext* context)
{
    return adoptRef(new MOMPackageManager(context));
}

MOMPackageManager::MOMPackageManager(ScriptExecutionContext* context)
    : ActiveDOMObject(context)
{
}

MOMPackageManager::~MOMPackageManager()
{
}

EventTargetInterface MOMPackageManager::eventTargetInterface() const
{
    return MOMPackageManagerEventTargetInterfaceType;
}

PassRefPtr<MOMPackageList> MOMPackageManager::all() const
{
    return MOMPackageList::create(scriptExecutionContext(), mom_package_manager_get_all());
}

PassRefPtr<MOMPackageList> MOMPackageManager::createFilteredList( int type, int signalled, int stored, int builtin, int displayInMenu )
{
    return MOMPackageList::createAdopt(scriptExecutionContext(), mom_package_manager_create_filtered_list(type, signalled, stored, builtin, displayInMenu));
}

void MOMPackageManager::store( const String & pkg )
{
    layers_string lstr = UtilsMOM::LayersStringFromString(pkg);
    mom_package_manager_store_uri(lstr);
    layers_string_dispose(lstr);
}

void MOMPackageManager::store( MOMPackageDescriptor* pkg )
{
    mom_package_manager_store(pkg->itemAsPackage());
}

void MOMPackageManager::upgrade( MOMPackageDescriptor* pkg )
{
    mom_package_manager_upgrade(pkg->itemAsPackage());
}

void MOMPackageManager::remove( MOMPackageDescriptor* pkg )
{
    mom_package_manager_remove(pkg->itemAsPackage());
}

void MOMPackageManager::setPackageRoot( const String & root, int period )
{
    layers_string lroot = UtilsMOM::LayersStringFromString(root);
    mom_appsman_set_package_root(lroot, period);
    layers_string_dispose(lroot);
}

void MOMPackageManager::setManifestUri( const String & uri, const String & base_uri )
{
    layers_string luri = UtilsMOM::LayersStringFromString(uri);
    layers_string lbaseUri = UtilsMOM::LayersStringFromString(base_uri);
    mom_appsman_set_manifest(luri, lbaseUri);
    layers_string_dispose(luri);
    layers_string_dispose(lbaseUri);
}

void MOMPackageManager::scanDirectory( const String & directory )
{
    layers_string lroot = UtilsMOM::LayersStringFromString(directory);
    mom_appsman_scan_directory(lroot);
    layers_string_dispose(lroot);
}

void MOMPackageManager::init()
{
    mom_channels_local_init();
}

} // namespace WebCore

#endif // ENABLE(RMAI)
