<commit_before>//
// This file is part of the Marble Desktop Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2010      <NAME> <<EMAIL>>
//

#include "BookmarkManager.h"
#include "GeoDataParser.h"
#include "GeoDataDocument.h"
#include "GeoDataFolder.h"
#include "GeoDataPlacemark.h"
#include "GeoWriter.h"
#include "KmlElementDictionary.h"
#include "MarbleDebug.h"
#include "MarbleDirs.h"
#include <QtCore/QFile>
using namespace Marble;

BookmarkManager::BookmarkManager()
     : d( new BookmarkManagerPrivate() )
{
}

BookmarkManager::~BookmarkManager()
{
    
    delete d->m_bookmarkDocument;   
    delete d;
}

QString BookmarkManager::bookmarkFile() const
{
    return MarbleDirs::path( d->m_bookmarkFileRelativePath );
}

bool BookmarkManager::loadFile( const QString &relativeFilePath)
{

    d->m_bookmarkFileRelativePath = relativeFilePath;
    QString absoluteFilePath = MarbleDirs::path( d->m_bookmarkFileRelativePath ); 

    mDebug() << "Loading Bookmark File : " << absoluteFilePath;
    if ( ! relativeFilePath.isNull() ) {
        GeoDataParser parser( GeoData_KML );

        QFile file( absoluteFilePath );
        
        if ( !file.exists() ) {
            mDebug() <<  " File does not exist in local and system path! ";
            return false;
        }

        // Open file in read mode
        file.open( QIODevice::ReadOnly );

        if ( !parser.read( &file ) ) {
            qWarning( "Could not parse file!" );
            
        }
        
        d->m_bookmarkDocument = dynamic_cast<GeoDataDocument*>(parser.releaseDocument() );
        Q_ASSERT( d->m_bookmarkDocument );

        file.close();
        return true;
    }

    return false;
}

void BookmarkManager::addBookmark( const GeoDataPlacemark &bookmark, const QString &folderName ) 
{
    QVector<GeoDataFolder*> bookmarkFolders = folders();
    QVector<GeoDataFolder*>::const_iterator i = bookmarkFolders.constBegin();
    QVector<GeoDataFolder*>::const_iterator end = bookmarkFolders.constEnd();

    for (; i != end; ++i ) {
        //Folder found where bookmark should be inserted
        if( !folderName.compare( (*i)->name() ) ){
            (*i)->append( new GeoDataPlacemark( bookmark ) );
            break;            
        }
    }

    updateBookmarkFile();
}

QVector<GeoDataFolder*> BookmarkManager::folders() const
{
        return d->m_bookmarkDocument->folderList();
}

void BookmarkManager::addNewBookmarkFolder( const QString &folder)
{<commit_msg>do not crash when there are no bookmarks<commit_after>//
// This file is part of the Marble Desktop Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2010      <NAME> <<EMAIL>>
//

#include "BookmarkManager.h"
#include "GeoDataParser.h"
#include "GeoDataDocument.h"
#include "GeoDataFolder.h"
#include "GeoDataPlacemark.h"
#include "GeoWriter.h"
#include "KmlElementDictionary.h"
#include "MarbleDebug.h"
#include "MarbleDirs.h"
#include <QtCore/QFile>
using namespace Marble;

BookmarkManager::BookmarkManager()
     : d( new BookmarkManagerPrivate() )
{
}

BookmarkManager::~BookmarkManager()
{
    
    delete d->m_bookmarkDocument;   
    delete d;
}

QString BookmarkManager::bookmarkFile() const
{
    return MarbleDirs::path( d->m_bookmarkFileRelativePath );
}

bool BookmarkManager::loadFile( const QString &relativeFilePath)
{

    d->m_bookmarkFileRelativePath = relativeFilePath;
    QString absoluteFilePath = MarbleDirs::path( d->m_bookmarkFileRelativePath ); 

    mDebug() << "Loading Bookmark File : " << absoluteFilePath;
    if ( ! relativeFilePath.isNull() ) {
        GeoDataParser parser( GeoData_KML );

        QFile file( absoluteFilePath );
        
        if ( !file.exists() ) {
            mDebug() <<  " File does not exist in local and system path! ";
            return false;
        }

        // Open file in read mode
        file.open( QIODevice::ReadOnly );

        if ( !parser.read( &file ) ) {
            qWarning( "Could not parse file!" );
            
        }
        
        d->m_bookmarkDocument = dynamic_cast<GeoDataDocument*>(parser.releaseDocument() );
        Q_ASSERT( d->m_bookmarkDocument );

        file.close();
        return true;
    }

    return false;
}

void BookmarkManager::addBookmark( const GeoDataPlacemark &bookmark, const QString &folderName ) 
{
    QVector<GeoDataFolder*> bookmarkFolders = folders();
    QVector<GeoDataFolder*>::const_iterator i = bookmarkFolders.constBegin();
    QVector<GeoDataFolder*>::const_iterator end = bookmarkFolders.constEnd();

    for (; i != end; ++i ) {
        //Folder found where bookmark should be inserted
        if( !folderName.compare( (*i)->name() ) ){
            (*i)->append( new GeoDataPlacemark( bookmark ) );
            break;            
        }
    }

    updateBookmarkFile();
}

QVector<GeoDataFolder*> BookmarkManager::folders() const
{
    return d->m_bookmarkDocument ? d->m_bookmarkDocument->folderList() : QVector<GeoDataFolder*>();
}

void BookmarkManager::addNewBookmarkFolder( const QString &folder)
{
{'fraction_non_alphanumeric': 0.1198182850653038, 'fraction_numerical': 0.0015142911224682945, 'mean_word_length': 3.0866202629543698, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3683670', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1610, 'n_words': 387}