<commit_before>#include "noteeverywhere.h"
#include <QGuiApplication>
#include <QScreen>
#include <QDebug>

NoteEverywhere::NoteEverywhere(QObject *parent) : QObject(parent), m_model(0), m_currentNote(0), m_previousNote(0) //TODO check if crash persists when not initializing m_currentNote
{
    m_isMobile = false;
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_BLACKBERRY)
    m_isMobile = true;
#endif

    QRect rect = qApp->primaryScreen()->geometry();
    m_ratio = m_isMobile ? qMin(qMax(rect.width(), rect.height())/1136. , qMin(rect.width(), rect.height())/640.) : 1;
    m_currentCategory = NoteValues::NONE;       // TODO category that has been viewed before closing the app

    m_sqlInterface = new SqlNoteInterface(this);

    m_colors = new QQmlPropertyMap(this);
    m_colors->insert(QLatin1String("separator"), QVariant("#133a0f"));
    m_colors->insert(QLatin1String("currentItem"), QVariant("#66bb6a"));
    m_colors->insert(QLatin1String("categoryIndicator"), QVariant("#aed581"));
}

void NoteEverywhere::setCurrentCategory(const NoteValues::Category &category)
{
    if (m_currentCategory != category){
        m_currentCategory = category;
        emit currentCategoryChanged(m_currentCategory);
    }
}

void NoteEverywhere::setCurrentNote(Note *currentNote)
{
    if (m_currentNote != currentNote){
        m_previousNote = m_currentNote;
        m_currentNote = currentNote;
        emit currentNoteChanged();
    }
}

void NoteEverywhere::setSearchQuery(const QString &searchQuery)
{
    if (m_searchQuery != searchQuery){
        m_searchQuery = searchQuery;
        searchNotes();
    }
}

void NoteEverywhere::populateModel()
{
    m_model = m_sqlInterface->populateModel();
    emit modelChanged();
}

void NoteEverywhere::searchNotes()
{
    m_model = m_sqlInterface->notesFor(m_searchQuery, m_currentCategory);
    emit modelChanged();
}

QString NoteEverywhere::categoryAsText(int category) const
{
    switch (category){
    case NoteValues::WORK: return QString("Work"); break;
    case NoteValues::ENTERTAINMENT: return QString("Entertainment"); break;
    case NoteValues::HOBBY: return QString("Hobby"); break;
    case NoteValues::HOME: return QString("Home"); break;
    case NoteValues::NONE: return QString("None"); break;
    default: return QString();
    }
}

void NoteEverywhere::changeNoteCategory(Note *note, int category)
{
    if (note->category() == category) {
        return;
    }

    /*
     * If 'm_currentCategory' has "NoteValues::NONE" value 'note' will not be deleted, because no matter what category it is assigned to, it will still be visible in 'NONE' category.
     * 'note' is deleted from the model only if category is different.
     * Deleting notes has consequences, if deleted note was assigned to 'm_currentNote' or 'm_previousNote' those fields should be updated.
     * Both model and database are updated not matter what category is the current one.
     *
     * */
    if (m_currentCategory != NoteValues::NONE) {
        if (m_previousNote == note) {
            m_previousNote = NULL;
        }

        if (m_currentNote == note) {
            m_currentNote = NULL;
        }

        m_model->removeNote(m_model->indexOf(note));
    }
    m_model->setCategoryAt(m_model->indexOf(note), category);
    m_sqlInterface->updateNoteCategory(note->id(), category);
}<commit_msg>Fixed dangling pointer issue when changing category.<commit_after>#include "noteeverywhere.h"
#include <QGuiApplication>
#include <QScreen>
#include <QDebug>

NoteEverywhere::NoteEverywhere(QObject *parent) : QObject(parent), m_model(0), m_currentNote(0), m_previousNote(0) //TODO check if crash persists when not initializing m_currentNote
{
    m_isMobile = false;
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_BLACKBERRY)
    m_isMobile = true;
#endif

    QRect rect = qApp->primaryScreen()->geometry();
    m_ratio = m_isMobile ? qMin(qMax(rect.width(), rect.height())/1136. , qMin(rect.width(), rect.height())/640.) : 1;
    m_currentCategory = NoteValues::NONE;       // TODO category that has been viewed before closing the app

    m_sqlInterface = new SqlNoteInterface(this);

    m_colors = new QQmlPropertyMap(this);
    m_colors->insert(QLatin1String("separator"), QVariant("#133a0f"));
    m_colors->insert(QLatin1String("currentItem"), QVariant("#66bb6a"));
    m_colors->insert(QLatin1String("categoryIndicator"), QVariant("#aed581"));
}

void NoteEverywhere::setCurrentCategory(const NoteValues::Category &category)
{
    if (m_currentCategory != category){
        m_currentCategory = category;
        emit currentCategoryChanged(m_currentCategory);
    }
}

void NoteEverywhere::setCurrentNote(Note *currentNote)
{
    if (m_currentNote != currentNote){
        m_previousNote = m_currentNote;
        m_currentNote = currentNote;
        emit currentNoteChanged();
    }
}

void NoteEverywhere::setSearchQuery(const QString &searchQuery)
{
    if (m_searchQuery != searchQuery){
        m_searchQuery = searchQuery;
        searchNotes();
    }
}

void NoteEverywhere::populateModel()
{
    m_model = m_sqlInterface->populateModel();
    emit modelChanged();
}

void NoteEverywhere::searchNotes()
{
    m_model = m_sqlInterface->notesFor(m_searchQuery, m_currentCategory);
    emit modelChanged();
}

QString NoteEverywhere::categoryAsText(int category) const
{
    switch (category){
    case NoteValues::WORK: return QString("Work"); break;
    case NoteValues::ENTERTAINMENT: return QString("Entertainment"); break;
    case NoteValues::HOBBY: return QString("Hobby"); break;
    case NoteValues::HOME: return QString("Home"); break;
    case NoteValues::NONE: return QString("None"); break;
    default: return QString();
    }
}

void NoteEverywhere::changeNoteCategory(Note *note, int category)
{
    if (note->category() == category) {
        return;
    }

    /*
     * If 'm_currentCategory' has "NoteValues::NONE" value 'note' will not be deleted, because no matter what category it is assigned to, it will still be visible in 'NONE' category.
     * 'note' is deleted from the model only if category is different.
     * Deleting notes has consequences, if deleted note was assigned to 'm_currentNote' or 'm_previousNote' those fields should be updated.
     * Both model and database are updated not matter what category is the current one.
     *
     * */
    int id = note->id();

    if (m_currentCategory != NoteValues::NONE) {
        if (m_previousNote == note) {
            m_previousNote = NULL;
        }

        if (m_currentNote == note) {
            m_currentNote = NULL;
        }

        m_model->removeNote(m_model->indexOf(note));
    }
    else {
        m_model->setCategoryAt(m_model->indexOf(note), category);
    }
    m_sqlInterface->updateNoteCategory(id, category);
}
{'fraction_non_alphanumeric': 0.11924317981812849, 'fraction_numerical': 0.007040187738339689, 'mean_word_length': 4.058605341246291, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, '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': '2330541', 'n_tokens_mistral': 2064, 'n_tokens_neox': 2024, 'n_words': 496}