/*!
    Creates and returns a source with the specified \a parent that reads
    from the system's default source of satellite update information.

    Returns 0 if the system has no default source.
*/
QGeoSatelliteInfoSource *QGeoSatelliteInfoSource::createDefaultSource(QObject *parent)
{
#if defined(Q_OS_SYMBIAN)
    CQGeoSatelliteInfoSourceS60 *ret = NULL;
    TRAPD(error, ret = CQGeoSatelliteInfoSourceS60::NewL(parent));
    if (error != KErrNone)
        return 0;
    return ret;
#elif defined(Q_OS_WINCE)
    return new QGeoSatelliteInfoSourceWinCE(parent);
#elif (defined(Q_WS_MAEMO_6)) || (defined(Q_WS_MAEMO_5))
    QGeoSatelliteInfoSourceMaemo *source = new QGeoSatelliteInfoSourceMaemo(parent);
    int status = source->init();

    if (status == -1) {
        delete source;
        return 0;
    }

    return source;
#else
    Q_UNUSED(parent);
    return 0;
#endif
}
/*!
    Creates and returns a source with the specified \a parent that reads
    from the system's default source of satellite update information, or the
    highest priority available plugin.

    Returns 0 if the system has no default source and no valid plugins
    could be found.

    Note: Symbian applications will need to have the Location capability
    otherwise this will return 0.
    \since 1.0
*/
QGeoSatelliteInfoSource *QGeoSatelliteInfoSource::createDefaultSource(QObject *parent)
{
    QSettings pluginSettings(QSettings::SystemScope, QLatin1String("Nokia"), QLatin1String("QtLocationPosAndSat"));
    QVariant value = pluginSettings.value("position.plugin.operator.whitelist");
    if (value.isValid()) {
        QStringList parts = value.toString().split(",");
        if (parts.size() == 4) {
            QGeoSatelliteInfoSource *source = createSource(parts.at(0), parent);
            if (source)
                return source;
        }
    }

#if defined(Q_OS_SYMBIAN)
    CQGeoSatelliteInfoSourceS60 *ret = NULL;
    TRAPD(error, QT_TRYCATCH_LEAVING(ret = CQGeoSatelliteInfoSourceS60::NewL(parent)));
    if (error == KErrNone)
        return ret;
#elif defined(Q_OS_WINCE)
    return new QGeoSatelliteInfoSourceWinCE(parent);
#elif (defined(Q_WS_MAEMO_6)) || (defined(Q_WS_MAEMO_5))
    QGeoSatelliteInfoSourceMaemo *source = new QGeoSatelliteInfoSourceMaemo(parent);
    int status = source->init();

    if (status != -1)
        return source;
    else
        delete source;
#elif defined(QT_SIMULATOR)
    return new QGeoSatelliteInfoSourceSimulator(parent);
#elif defined(Q_WS_MEEGO)
    // Use Maemo6 backend if available, otherwise use Gypsy backend
    QSettings maemo6Settings(QSettings::UserScope, QLatin1String("Nokia"), QLatin1String("QtLocationPosAndSatMaemo6"));
    if (!maemo6Settings.value("maemo6satelliteavailable").isValid()) {
        QGeoSatelliteInfoSourceMaemo *maemoSource = new QGeoSatelliteInfoSourceMaemo(parent);
        int status = maemoSource->init();
        if (status == -1) {
            delete maemoSource;
            maemoSource = 0;
            maemo6Settings.setValue("maemo6satelliteavailable", false);
        } else {
            return maemoSource;
        }
    }
#ifdef GYPSY_AVAILABLE
    QGeoSatelliteInfoSourceGypsy* gypsySource = new QGeoSatelliteInfoSourceGypsy(parent);
    int status = gypsySource->init();
    if (status >= 0)
        return gypsySource;
    delete gypsySource;
#endif // GYPSY_AVAILABLE
#endif
    foreach (QGeoPositionInfoSourceFactory *f, QGeoSatelliteInfoSourcePrivate::pluginsSorted()) {
        QGeoSatelliteInfoSource *src = f->satelliteInfoSource(parent);
        if (src)
            return src;
    }