<commit_before>/*
    Copyright (C) 2009    <NAME> <<EMAIL>>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "feeditemmodel.h"
#include "rssitem.h"
#include "person.h"

#include <KDateTime>
#include <KGlobal>
#include <KIcon>
#include <KLocale>

using namespace Akonadi;
using namespace KRss;

class KRss::FeedItemModelPrivate {
    FeedItemModel* const q;
public:
    explicit FeedItemModelPrivate( FeedItemModel* qq ) : q( qq ), importantIcon( KIcon( QLatin1String("mail-mark-important") ) ) {

    }

    KIcon importantIcon;
};


FeedItemModel::FeedItemModel( Session* session, ChangeRecorder* monitor, QObject* parent ) : EntityTreeModel( session, monitor, parent ), d( new FeedItemModelPrivate( this ) ) {
    setItemPopulationStrategy( EntityTreeModel::LazyPopulation );
}

FeedItemModel::~FeedItemModel() {
    delete d;
}

QVariant FeedItemModel::getData( const Akonadi::Item &akonadiItem, int column, int role ) const {
    if ( !akonadiItem.hasPayload<RssItem>() )
        return QVariant();

    const RssItem item = akonadiItem.payload<RssItem>();
    if ( role == SortRole && column == DateColumn )
        return item.dateUpdated().toTime_t();

    if ( role == IsDeletedRole )
        return false;

    if ( role == Qt::DisplayRole || role == SortRole ) {
        switch ( column ) {
            case ItemTitleColumn:
                return item.titleAsPlainText();
            case AuthorsColumn:
            {
                QString authors;
                Q_FOREACH( const KRss::Person &person, item.authors() ) {
                    if ( !authors.isEmpty() )
                        authors.append( QLatin1Char(';') );
                    authors += person.name();
                }
                return authors;
            }
            case DateColumn:
                return KGlobal::locale()->formatDateTime( item.dateUpdated(),
                                                          KLocale::FancyShortDate );
            case FeedTitleForItemColumn:
#ifdef TEMPORARILY_REMOVED
               return d->m_feed->title();
#endif<commit_msg>use the sort role for dates, disable the author column for now <commit_after>/*
    Copyright (C) 2009    <NAME> <<EMAIL>>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "feeditemmodel.h"
#include "rssitem.h"
#include "person.h"

#include <KDateTime>
#include <KGlobal>
#include <KIcon>
#include <KLocale>

using namespace Akonadi;
using namespace KRss;

class KRss::FeedItemModelPrivate {
    FeedItemModel* const q;
public:
    explicit FeedItemModelPrivate( FeedItemModel* qq ) : q( qq ), importantIcon( KIcon( QLatin1String("mail-mark-important") ) ) {

    }

    KIcon importantIcon;
};


FeedItemModel::FeedItemModel( Session* session, ChangeRecorder* monitor, QObject* parent ) : EntityTreeModel( session, monitor, parent ), d( new FeedItemModelPrivate( this ) ) {
    setItemPopulationStrategy( EntityTreeModel::LazyPopulation );
}

FeedItemModel::~FeedItemModel() {
    delete d;
}

QVariant FeedItemModel::getData( const Akonadi::Item &akonadiItem, int column, int role ) const {
    if ( !akonadiItem.hasPayload<RssItem>() )
        return QVariant();

    const RssItem item = akonadiItem.payload<RssItem>();
    if ( role == SortRole && column == DateColumn )
        return item.dateUpdated().toTime_t();

    if ( role == IsDeletedRole )
        return false;


    if ( role == Qt::DisplayRole || role == SortRole ) {
        switch ( column ) {
            case ItemTitleColumn:
                return item.titleAsPlainText();
            case AuthorsColumn:
            {
#if 0 //TODO: we probably want to cache this
                QString authors;
                Q_FOREACH( const KRss::Person &person, item.authors() ) {
                    if ( !authors.isEmpty() )
                        authors.append( QLatin1Char(';') );
                    authors += person.name();
                }
                return authors;
#endif
                return QVariant();
            }
            case DateColumn:
                if ( role == SortRole )
                    return item.dateUpdated().toTime_t();
                else
                    return KGlobal::locale()->formatDateTime( item.dateUpdated(),
                                                              KLocale::FancyShortDate );
            case FeedTitleForItemColumn:
#ifdef TEMPORARILY_REMOVED
               return d->m_feed->title();
#endif
{'fraction_non_alphanumeric': 0.09420931468036126, 'fraction_numerical': 0.002656277669559058, 'mean_word_length': 2.4418037781840343, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '895564', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1474, 'n_words': 495}