<commit_before>// Copyright (c) 2011-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "bantablemodel.h"

#include "clientmodel.h"
#include "guiconstants.h"
#include "guiutil.h"

#include "net.h"
#include "sync.h"
#include "utiltime.h"

#include <QDebug>
#include <QList>
#include <QTimer>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/c_local_time_adjustor.hpp>

// private implementation
class BanTablePriv
{
public:
    /** Local cache of peer information */
    QList<CCombinedBan> cachedBanlist;
    /** Column to sort nodes by */
    int sortColumn;
    /** Order (ascending or descending) to sort nodes by */
    Qt::SortOrder sortOrder;

    /** Pull a full list of banned nodes from CNode into our cache */
    void refreshBanlist()
    {
        std::map<CSubNet, int64_t> banMap;
        CNode::GetBanned(banMap);

        cachedBanlist.clear();
#if QT_VERSION >= 0x040700
        cachedBanlist.reserve(banMap.size());
#endif
        std::map<CSubNet, int64_t>::iterator iter;
        for (iter = banMap.begin(); iter != banMap.end(); ++iter) {
            CCombinedBan banEntry;
            banEntry.subnet = iter->first;
            banEntry.bantil = iter->second;
            cachedBanlist.append(banEntry);
        }
    }

    int size()
    {
        return cachedBanlist.size();
    }

    CCombinedBan *index(int idx)
    {
        if(idx >= 0 && idx < cachedBanlist.size()) {
            return &cachedBanlist[idx];
        } else {
            return 0;
        }
    }
};

BanTableModel::BanTableModel(ClientModel *parent) :
    QAbstractTableModel(parent),
    clientModel(parent),
    timer(0)
{
    columns << tr("IP/Netmask") << tr("Banned Until");
    priv = new BanTablePriv();
    // default to unsorted
    priv->sortColumn = -1;

    // set up timer for auto refresh
    timer = new QTimer();
    connect(timer, SIGNAL(timeout()), SLOT(refresh()));
    timer->setInterval(MODEL_UPDATE_DELAY);

    // load initial data
    refresh();
}

void BanTableModel::startAutoRefresh()
{
    timer->start();
}

void BanTableModel::stopAutoRefresh()
{
    timer->stop();
}

int BanTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return priv->size();
}

int BanTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return columns.length();;
}

QVariant BanTableModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();

    CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());

    if (role == Qt::DisplayRole) {
        switch(index.column())
        {
        case Address:
            return QString::fromStdString(rec->subnet.ToString());
        case Bantime:
            //show time in users local timezone, not 64bit compatible!
            //TODO find a way to support 64bit timestamps
            boost::posix_time::ptime pt1 = boost::posix_time::from_time_t(rec->bantil);
            boost::posix_time::ptime pt2 = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(pt1);
            std::stringstream ss;
            ss << pt2;
            return QString::fromStdString(ss.str());
        }
    } else if (role == Qt::TextAlignmentRole) {
        if (index.column() == Bantime)
            return (int)(Qt::AlignRight | Qt::AlignVCenter);
    }

    return QVariant();
}

QVariant BanTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal)
    {
        if(role == Qt::DisplayRole && section < columns.size())
        {
            return columns[section];
        }
    }
    return QVariant();
}

Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const
{
    if(!index.isValid())
        return 0;

    Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    return retval;<commit_msg>[Qt] bantable fix timestamp 64bit issue<commit_after>// Copyright (c) 2011-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "bantablemodel.h"

#include "clientmodel.h"
#include "guiconstants.h"
#include "guiutil.h"

#include "net.h"
#include "sync.h"
#include "utiltime.h"

#include <QDebug>
#include <QList>
#include <QTimer>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/c_local_time_adjustor.hpp>

// private implementation
class BanTablePriv
{
public:
    /** Local cache of peer information */
    QList<CCombinedBan> cachedBanlist;
    /** Column to sort nodes by */
    int sortColumn;
    /** Order (ascending or descending) to sort nodes by */
    Qt::SortOrder sortOrder;

    /** Pull a full list of banned nodes from CNode into our cache */
    void refreshBanlist()
    {
        std::map<CSubNet, int64_t> banMap;
        CNode::GetBanned(banMap);

        cachedBanlist.clear();
#if QT_VERSION >= 0x040700
        cachedBanlist.reserve(banMap.size());
#endif
        std::map<CSubNet, int64_t>::iterator iter;
        for (iter = banMap.begin(); iter != banMap.end(); ++iter) {
            CCombinedBan banEntry;
            banEntry.subnet = iter->first;
            banEntry.bantil = iter->second;
            cachedBanlist.append(banEntry);
        }
    }

    int size()
    {
        return cachedBanlist.size();
    }

    CCombinedBan *index(int idx)
    {
        if(idx >= 0 && idx < cachedBanlist.size()) {
            return &cachedBanlist[idx];
        } else {
            return 0;
        }
    }
};

BanTableModel::BanTableModel(ClientModel *parent) :
    QAbstractTableModel(parent),
    clientModel(parent),
    timer(0)
{
    columns << tr("IP/Netmask") << tr("Banned Until");
    priv = new BanTablePriv();
    // default to unsorted
    priv->sortColumn = -1;

    // set up timer for auto refresh
    timer = new QTimer();
    connect(timer, SIGNAL(timeout()), SLOT(refresh()));
    timer->setInterval(MODEL_UPDATE_DELAY);

    // load initial data
    refresh();
}

void BanTableModel::startAutoRefresh()
{
    timer->start();
}

void BanTableModel::stopAutoRefresh()
{
    timer->stop();
}

int BanTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return priv->size();
}

int BanTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return columns.length();;
}

QVariant BanTableModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();

    CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());

    if (role == Qt::DisplayRole) {
        switch(index.column())
        {
        case Address:
            return QString::fromStdString(rec->subnet.ToString());
        case Bantime:
            QDateTime date = QDateTime::fromMSecsSinceEpoch(0);
            date = date.addSecs(rec->bantil);
            return date.toString(Qt::SystemLocaleShortDate);
        }
    } else if (role == Qt::TextAlignmentRole) {
        if (index.column() == Bantime)
            return (int)(Qt::AlignRight | Qt::AlignVCenter);
    }

    return QVariant();
}

QVariant BanTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal)
    {
        if(role == Qt::DisplayRole && section < columns.size())
        {
            return columns[section];
        }
    }
    return QVariant();
}

Qt::ItemFlags BanTableModel::flags(const QModelIndex &index) const
{
    if(!index.isValid())
        return 0;

    Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    return retval;
{'fraction_non_alphanumeric': 0.1320440573770492, 'fraction_numerical': 0.007556352459016393, 'mean_word_length': 2.9359879032258065, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2939871', 'n_tokens_mistral': 2505, 'n_tokens_neox': 2410, 'n_words': 546}