<commit_before>/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (<EMAIL>)
**
** This file is part of meegotouch-controlpanelsoundsettingsapplet.
**
** If you have questions regarding the use of this file, please contact
** Nokia at <EMAIL>.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include <QTimer>
#include <QFile>

#include "trackerconnection.h"
#include "alerttonedefaultsmodel.h"
#include "alerttone.h"
#include "static.h"

/*
 * Define this macro to add items asynchronously.
 */
#define USE_ASYNC_LOADING

//#define DEBUG
#define WARNING
#include "debug.h"

AlertToneDefaultsModel::AlertToneDefaultsModel() : QStandardItemModel(),
	m_isFinished(false)
{
	insertColumn(NiceNameColumn);
	insertColumn(FullPathColumn);
	insertColumn(ForcedColumn);

	m_dirStack.push(QDir(DEFAULT_RINGTONE_PATH1));
	m_dirIdx.push(0);
	m_dirStack.push(QDir(DEFAULT_RINGTONE_PATH2));
	m_dirIdx.push(0);

    /*
     * Creatig a file system watcher.
     */
    m_FileWatcher = new QFileSystemWatcher (this);
    connect (m_FileWatcher, SIGNAL(fileChanged(const QString &)),
            this, SLOT(fileChanged(const QString &)));

    /*
     * TrackerConnection might send a signal about the tracker answer.
     */
    connect (TrackerConnection::instance(), 
            SIGNAL(dataReady(QString,QString,QString)),
            this, 
            SLOT(dataReceived(QString,QString,QString)));

    #ifdef USE_ASYNC_LOADING
    /*
     * We need to give the UI a chance to process the tap events, but we also
     * want to be as fast as possible... this is not a perfect solution though.
     */
	QTimer::singleShot(20, this, SLOT(addSingleItem()));
    #else 
	while (!m_isFinished)
        addSingleItem();
    #endif
}

void
AlertToneDefaultsModel::addSingleItem()
{
    SYS_DEBUG ("*** m_isFinished = %s", SYS_BOOL(m_isFinished));
	if (m_isFinished) 
        return;

    while (m_dirStack.count() > 0) {
        if (m_dirIdx.top() >= m_dirStack.top().count()) {
            m_dirIdx.pop();
            m_dirStack.pop();
        } else {
            break;
        }
    }

    if (m_dirStack.count() <= 0) {
        m_isFinished = true;
        emit finished();
        return;
	} 

    #if 1
    SYS_DEBUG ("*** path        = %s", 
            SYS_STR(m_dirStack.top().absolutePath()));
    SYS_DEBUG ("***             = %s", 
            SYS_STR(m_dirStack.top()[m_dirIdx.top()]));
    #endif
    int currentLevel = m_dirIdx.count() - 1;
    if (m_dirStack.top()[m_dirIdx.top()] != "." &&
            m_dirStack.top()[m_dirIdx.top()] != "..") {
        QString fullPath = m_dirStack.top().absolutePath() + 
                QDir::separator() + m_dirStack.top()[m_dirIdx.top()];

		QDir subdir(fullPath);

		if (subdir.count() > 0) {
			m_dirStack.push(subdir);
			m_dirIdx.push(0);
		} else if (m_dirStack.top()[m_dirIdx.top()].right(4) == ".aac" &&
					m_dirStack.top()[m_dirIdx.top()].left(1) == m_dirStack.top()[m_dirIdx.top()].left(1).toUpper()) {
            QString niceName = 
                TrackerConnection::instance()->niceNameFromFileName (fullPath);
            SYS_DEBUG ("calling niceNameFromFileName()");
            addSingleItem (niceName, fullPath);
        }
    }

    m_dirIdx[currentLevel]++;

    #ifdef USE_ASYNC_LOADING
    QTimer::singleShot(20, this, SLOT(addSingleItem()));
    #endif
}

/*!
 * \param niceName The human readable name for the new item
 * \param fileName The full path file name for the new entry
 * \param forced Should be true when the item is not found in the filesystem but
 *   inserted for some other reasons (maybe coming from the content picker).
 *
 * This is the method that should be used to add a new item into the model once
 * the file name and the human readable name is known. No other function should<commit_msg>Loading of the soundsettingsapplet sound list is faster now.<commit_after>/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (<EMAIL>)
**
** This file is part of meegotouch-controlpanelsoundsettingsapplet.
**
** If you have questions regarding the use of this file, please contact
** Nokia at <EMAIL>.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include <QTimer>
#include <QFile>

#include "trackerconnection.h"
#include "alerttonedefaultsmodel.h"
#include "alerttone.h"
#include "static.h"

/*
 * Define this macro to add items asynchronously.
 */
#define USE_ASYNC_LOADING

//#define DEBUG
#define WARNING
#include "debug.h"

AlertToneDefaultsModel::AlertToneDefaultsModel() : QStandardItemModel(),
	m_isFinished(false)
{
	insertColumn(NiceNameColumn);
	insertColumn(FullPathColumn);
	insertColumn(ForcedColumn);

	m_dirStack.push(QDir(DEFAULT_RINGTONE_PATH1));
	m_dirIdx.push(0);
	m_dirStack.push(QDir(DEFAULT_RINGTONE_PATH2));
	m_dirIdx.push(0);

    /*
     * Creatig a file system watcher.
     */
    m_FileWatcher = new QFileSystemWatcher (this);
    connect (m_FileWatcher, SIGNAL(fileChanged(const QString &)),
            this, SLOT(fileChanged(const QString &)));

    /*
     * TrackerConnection might send a signal about the tracker answer.
     */
    connect (TrackerConnection::instance(), 
            SIGNAL(dataReady(QString,QString,QString)),
            this, 
            SLOT(dataReceived(QString,QString,QString)));

    #ifdef USE_ASYNC_LOADING
    /*
     * We need to give the UI a chance to process the tap events, but we also
     * want to be as fast as possible... this is not a perfect solution though.
     */
	QTimer::singleShot(0, this, SLOT(addSingleItem()));
    #else 
	while (!m_isFinished)
        addSingleItem();
    #endif
}

void
AlertToneDefaultsModel::addSingleItem()
{
    SYS_DEBUG ("*** m_isFinished = %s", SYS_BOOL(m_isFinished));
	if (m_isFinished) 
        return;

    while (m_dirStack.count() > 0) {
        if (m_dirIdx.top() >= m_dirStack.top().count()) {
            m_dirIdx.pop();
            m_dirStack.pop();
        } else {
            break;
        }
    }

    if (m_dirStack.count() <= 0) {
        m_isFinished = true;
        emit finished();
        return;
	} 

    #if 1
    SYS_DEBUG ("*** path        = %s", 
            SYS_STR(m_dirStack.top().absolutePath()));
    SYS_DEBUG ("***             = %s", 
            SYS_STR(m_dirStack.top()[m_dirIdx.top()]));
    #endif
    int currentLevel = m_dirIdx.count() - 1;
    if (m_dirStack.top()[m_dirIdx.top()] != "." &&
            m_dirStack.top()[m_dirIdx.top()] != "..") {
        QString fullPath = m_dirStack.top().absolutePath() + 
                QDir::separator() + m_dirStack.top()[m_dirIdx.top()];

		QDir subdir(fullPath);

		if (subdir.count() > 0) {
			m_dirStack.push(subdir);
			m_dirIdx.push(0);
		} else if (m_dirStack.top()[m_dirIdx.top()].right(4) == ".aac" &&
					m_dirStack.top()[m_dirIdx.top()].left(1) == m_dirStack.top()[m_dirIdx.top()].left(1).toUpper()) {
            QString niceName = 
                TrackerConnection::instance()->niceNameFromFileName (fullPath);
            SYS_DEBUG ("calling niceNameFromFileName()");
            addSingleItem (niceName, fullPath);
        }
    }

    m_dirIdx[currentLevel]++;

    #ifdef USE_ASYNC_LOADING
    /*
     * We have to keep this delay at 0, otherwise the loading of the list is
     * going to be sooooo slow!
     */
    QTimer::singleShot(0, this, SLOT(addSingleItem()));
    #endif
}

/*!
 * \param niceName The human readable name for the new item
 * \param fileName The full path file name for the new entry
 * \param forced Should be true when the item is not found in the filesystem but
 *   inserted for some other reasons (maybe coming from the content picker).
 *
 * This is the method that should be used to add a new item into the model once
 * the file name and the human readable name is known. No other function should
{'fraction_non_alphanumeric': 0.15667936727860524, 'fraction_numerical': 0.005195704883962591, 'mean_word_length': 3.2233057045343734, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '5520824', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2632, 'n_words': 717}