<commit_before>#include "share.h"

#include "object.h"
#include "directory.h"
#include "file.h"
#include "objectiterator.h"
#include "objectcache.h"
#include "ipfs/ipfs.h"

#include <QDateTime>
#include <QDebug>
#include <QStringBuilder>

/*
 * PlanUML
 * @startuml
 *
 * [*] -> CREATING
 * CREATING -> CREATING : add file/dir
 * CREATING -> DL_METADATA : download
 * DL_METADATA -> READY : dl metadata done
 * DL_METADATA -> WAITING_FOR_DL : start
 * READY -> DOWNLOAD : start
 * PAUSED -> DOWNLOAD : start
 * DOWNLOAD -> SHARING : ended
 * DOWNLOAD -> PAUSED : pause
 *
 * [*] -> SHARING : share
 *
 * @enduml
 */

Share::Share(QObject *parent):
    QObject(parent),
    id_(-1),
    creation_date_(QDateTime::currentDateTime()),
    starred_(false),
    state_(CREATING)
{
}

int Share::id() const
{
    return id_;
}

const QString& Share::title() const
{
    return title_;
}

const QString Share::description() const
{
    if(!description_.isEmpty() || objects_.count() == 0)
        return description_;

    // If no description is available, we build a list of the first files
    QString desc;
    for(QList<Object*>::const_iterator i = objects_.constBegin(); i != objects_.constEnd(); i++)
    {
        for(ObjectIterator it = ObjectIterator(*i); it != ObjectIterator(); it++)
        {
            if(!(*it).name().isEmpty())
            {
                desc += (*it).name();
                desc += ", ";
            }
            if(desc.count() > 100)
            {
                desc += "...";
                return desc;
            }
        }
    }

    return desc;
}

const QDir &Share::path() const
{
    return path_;
}

const QDateTime &Share::date_creation() const
{
    return creation_date_;
}

bool Share::starred() const
{
    return starred_;
}

void Share::set_starred(const bool &starred)
{
    starred_ = starred;
    emit shareChanged();
}

QString Share::textual_arborescence() const
{
    if(this->objects_.count() == 0)
        return "";

    QString result;
    for(ObjectIterator it = ObjectIterator(this->objects_[0]); it != ObjectIterator(); it++)
    {
        result += QString("--").repeated(it.depth());
        result += (*it).name();
        result += "\n";
    }

    return result;
}

Share::ShareState Share::state() const
{
    return state_;
}

float Share::progress() const
{
    if(state_ == CREATING)
        return 0.0;

    uint total = block_total();

    if(total == 0)
        return 0.0;

    return (float)block_local() / (float)total;
}

uint Share::size_total() const
{
    uint size_total = 0;
    for(QList<Object*>::const_iterator i = objects_.constBegin();
        i != objects_.constEnd();
        i++)
    {
        size_total += (*i)->size_total();
    }
    return size_total;
}

uint Share::size_local() const<commit_msg>Share: use the now working size to compute the progress<commit_after>#include "share.h"

#include "object.h"
#include "directory.h"
#include "file.h"
#include "objectiterator.h"
#include "objectcache.h"
#include "ipfs/ipfs.h"

#include <QDateTime>
#include <QDebug>
#include <QStringBuilder>

/*
 * PlanUML
 * @startuml
 *
 * [*] -> CREATING
 * CREATING -> CREATING : add file/dir
 * CREATING -> DL_METADATA : download
 * DL_METADATA -> READY : dl metadata done
 * DL_METADATA -> WAITING_FOR_DL : start
 * READY -> DOWNLOAD : start
 * PAUSED -> DOWNLOAD : start
 * DOWNLOAD -> SHARING : ended
 * DOWNLOAD -> PAUSED : pause
 *
 * [*] -> SHARING : share
 *
 * @enduml
 */

Share::Share(QObject *parent):
    QObject(parent),
    id_(-1),
    creation_date_(QDateTime::currentDateTime()),
    starred_(false),
    state_(CREATING)
{
}

int Share::id() const
{
    return id_;
}

const QString& Share::title() const
{
    return title_;
}

const QString Share::description() const
{
    if(!description_.isEmpty() || objects_.count() == 0)
        return description_;

    // If no description is available, we build a list of the first files
    QString desc;
    for(QList<Object*>::const_iterator i = objects_.constBegin(); i != objects_.constEnd(); i++)
    {
        for(ObjectIterator it = ObjectIterator(*i); it != ObjectIterator(); it++)
        {
            if(!(*it).name().isEmpty())
            {
                desc += (*it).name();
                desc += ", ";
            }
            if(desc.count() > 100)
            {
                desc += "...";
                return desc;
            }
        }
    }

    return desc;
}

const QDir &Share::path() const
{
    return path_;
}

const QDateTime &Share::date_creation() const
{
    return creation_date_;
}

bool Share::starred() const
{
    return starred_;
}

void Share::set_starred(const bool &starred)
{
    starred_ = starred;
    emit shareChanged();
}

QString Share::textual_arborescence() const
{
    if(this->objects_.count() == 0)
        return "";

    QString result;
    for(ObjectIterator it = ObjectIterator(this->objects_[0]); it != ObjectIterator(); it++)
    {
        result += QString("--").repeated(it.depth());
        result += (*it).name();
        result += "\n";
    }

    return result;
}

Share::ShareState Share::state() const
{
    return state_;
}

float Share::progress() const
{
    if(state_ == CREATING)
        return 0.0;

    uint total = size_total();

    if(total == 0)
        return 0.0;

    return (float)size_local() / (float)total;
}

uint Share::size_total() const
{
    uint size_total = 0;
    for(QList<Object*>::const_iterator i = objects_.constBegin();
        i != objects_.constEnd();
        i++)
    {
        size_total += (*i)->size_total();
    }
    return size_total;
}

uint Share::size_local() const
{'fraction_non_alphanumeric': 0.1554413328606877, 'fraction_numerical': 0.004608294930875576, 'mean_word_length': 2.594267515923567, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4066192', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1786, 'n_words': 420}