<commit_before>#include "models/favorite.h"
#include <QJsonArray>
#include "functions.h"


Favorite::Favorite(QString name)
	: Favorite(std::move(name), 50, QDateTime::currentDateTime(), QString())
{}
Favorite::Favorite(QString name, int note, QDateTime lastViewed, QString imagePath)
	: Favorite(std::move(name), note, std::move(lastViewed), QList<Monitor>(), std::move(imagePath))
{}
Favorite::Favorite(QString name, int note, QDateTime lastViewed, QList<Monitor> monitors, QString imagePath)
	: m_name(std::move(name)), m_note(note), m_lastViewed(std::move(lastViewed)), m_monitors(std::move(monitors)), m_imagePath(std::move(imagePath))
{}

void Favorite::setImagePath(const QString &imagePath)
{ m_imagePath = imagePath; }
void Favorite::setLastViewed(const QDateTime &lastViewed)
{ m_lastViewed = lastViewed; }
void Favorite::setNote(int note)
{ m_note = note; }

QString Favorite::getName(bool clean) const
{
	if (clean) {
		return QString(m_name).remove('\\').remove('/').remove(':').remove('*').remove('?').remove('"').remove('<').remove('>').remove('|');
	}
	return m_name;
}
int Favorite::getNote() const
{ return m_note; }
QDateTime Favorite::getLastViewed() const
{ return m_lastViewed; }
QString Favorite::getImagePath() const
{ return m_imagePath; }
QList<Monitor> &Favorite::getMonitors()
{ return m_monitors; }

bool Favorite::setImage(const QPixmap &img)
{
	if (!QDir(savePath("thumbs")).exists()) {
		QDir(savePath()).mkdir("thumbs");
	}

	m_imagePath = savePath("thumbs/" + getName(true) + ".png");
	return img
		.scaled(QSize(150, 150), Qt::KeepAspectRatio, Qt::SmoothTransformation)
		.save(m_imagePath, "PNG");
}
QPixmap Favorite::getImage() const
{
	QPixmap img(m_imagePath);
	if (img.width() > 150 || img.height() > 150) {
		img = img.scaled(QSize(150, 150), Qt::KeepAspectRatio, Qt::SmoothTransformation);
		img.save(savePath("thumbs/" + getName(true) + ".png"), "PNG");
	}
	return img;
}

QString Favorite::toString() const
{
	return getName() + "|" + QString::number(getNote()) + "|" + getLastViewed().toString(Qt::ISODate);
}
Favorite Favorite::fromString(const QString &path, const QString &text)
{
	QStringList xp = text.split("|");

	const QString tag = xp.takeFirst();
	const int note = xp.isEmpty() ? 50 : xp.takeFirst().toInt();
	const QDateTime lastViewed = xp.isEmpty()
		? QDateTime(QDate(2000, 1, 1), QTime(0, 0, 0, 0))
		: QDateTime::fromString(xp.takeFirst(), Qt::ISODate);

	QString thumbPath = path + "/thumbs/" + (QString(tag).remove('\\').remove('/').remove(':').remove('*').remove('?').remove('"').remove('<').remove('>').remove('|')) + ".png";
	if (!QFile::exists(thumbPath)) {
		thumbPath = ":/images/noimage.png";
	}

	return Favorite(tag, note, lastViewed, thumbPath);
}

void Favorite::toJson(QJsonObject &json) const
{
	json["tag"] = getName();
	json["note"] = getNote();
	json["lastViewed"] = getLastViewed().toString(Qt::ISODate);

	QJsonArray monitorsJson;
	for (const Monitor &monitor : m_monitors) {
		QJsonObject obj;
		monitor.toJson(obj);
		monitorsJson.append(obj);
	}
	json["monitors"] = monitorsJson;
}
Favorite Favorite::fromJson(const QString &path, const QJsonObject &json, const QMap<QString, Site *> &sites)
{
	const QString tag = json["tag"].toString();
	const int note = json["note"].toInt();
	const QDateTime lastViewed = QDateTime::fromString(json["lastViewed"].toString(), Qt::ISODate);

	QString thumbPath = path + "/thumbs/" + (QString(tag).remove('\\').remove('/').remove(':').remove('*').remove('?').remove('"').remove('<').remove('>').remove('|')) + ".png";
	if (!QFile::exists(thumbPath)) {
		thumbPath = ":/images/noimage.png";
	}

	QList<Monitor> monitors;
	QJsonArray monitorsJson = json["monitors"].toArray();
	for (auto monitorJson : monitorsJson) {
		monitors.append(Monitor::fromJson(monitorJson.toObject(), sites));
	}

	return Favorite(tag, note, lastViewed, monitors, thumbPath);
}

bool Favorite::sortByNote(const Favorite &s1, const Favorite &s2)
{ return s1.getNote() < s2.getNote(); }
bool Favorite::sortByName(const Favorite &s1, const Favorite &s2)
{ return s1.getName().toLower() < s2.getName().toLower(); }
bool Favorite::sortByLastViewed(const Favorite &s1, const Favorite &s2)
{ return s1.getLastViewed() < s2.getLastViewed(); }


bool operator==(const Favorite &lhs, const Favorite &rhs)
{ return lhs.getName().toLower() == rhs.getName().toLower(); }
bool operator!=(const Favorite &lhs, const Favorite &rhs)
{ return !(lhs == rhs); }<commit_msg>Don't serialize empty monitor lists<commit_after>#include "models/favorite.h"
#include <QJsonArray>
#include "functions.h"


Favorite::Favorite(QString name)
	: Favorite(std::move(name), 50, QDateTime::currentDateTime(), QString())
{}
Favorite::Favorite(QString name, int note, QDateTime lastViewed, QString imagePath)
	: Favorite(std::move(name), note, std::move(lastViewed), QList<Monitor>(), std::move(imagePath))
{}
Favorite::Favorite(QString name, int note, QDateTime lastViewed, QList<Monitor> monitors, QString imagePath)
	: m_name(std::move(name)), m_note(note), m_lastViewed(std::move(lastViewed)), m_monitors(std::move(monitors)), m_imagePath(std::move(imagePath))
{}

void Favorite::setImagePath(const QString &imagePath)
{ m_imagePath = imagePath; }
void Favorite::setLastViewed(const QDateTime &lastViewed)
{ m_lastViewed = lastViewed; }
void Favorite::setNote(int note)
{ m_note = note; }

QString Favorite::getName(bool clean) const
{
	if (clean) {
		return QString(m_name).remove('\\').remove('/').remove(':').remove('*').remove('?').remove('"').remove('<').remove('>').remove('|');
	}
	return m_name;
}
int Favorite::getNote() const
{ return m_note; }
QDateTime Favorite::getLastViewed() const
{ return m_lastViewed; }
QString Favorite::getImagePath() const
{ return m_imagePath; }
QList<Monitor> &Favorite::getMonitors()
{ return m_monitors; }

bool Favorite::setImage(const QPixmap &img)
{
	if (!QDir(savePath("thumbs")).exists()) {
		QDir(savePath()).mkdir("thumbs");
	}

	m_imagePath = savePath("thumbs/" + getName(true) + ".png");
	return img
		.scaled(QSize(150, 150), Qt::KeepAspectRatio, Qt::SmoothTransformation)
		.save(m_imagePath, "PNG");
}
QPixmap Favorite::getImage() const
{
	QPixmap img(m_imagePath);
	if (img.width() > 150 || img.height() > 150) {
		img = img.scaled(QSize(150, 150), Qt::KeepAspectRatio, Qt::SmoothTransformation);
		img.save(savePath("thumbs/" + getName(true) + ".png"), "PNG");
	}
	return img;
}

QString Favorite::toString() const
{
	return getName() + "|" + QString::number(getNote()) + "|" + getLastViewed().toString(Qt::ISODate);
}
Favorite Favorite::fromString(const QString &path, const QString &text)
{
	QStringList xp = text.split("|");

	const QString tag = xp.takeFirst();
	const int note = xp.isEmpty() ? 50 : xp.takeFirst().toInt();
	const QDateTime lastViewed = xp.isEmpty()
		? QDateTime(QDate(2000, 1, 1), QTime(0, 0, 0, 0))
		: QDateTime::fromString(xp.takeFirst(), Qt::ISODate);

	QString thumbPath = path + "/thumbs/" + (QString(tag).remove('\\').remove('/').remove(':').remove('*').remove('?').remove('"').remove('<').remove('>').remove('|')) + ".png";
	if (!QFile::exists(thumbPath)) {
		thumbPath = ":/images/noimage.png";
	}

	return Favorite(tag, note, lastViewed, thumbPath);
}

void Favorite::toJson(QJsonObject &json) const
{
	json["tag"] = getName();
	json["note"] = getNote();
	json["lastViewed"] = getLastViewed().toString(Qt::ISODate);

	if (!m_monitors.isEmpty()) {
		QJsonArray monitorsJson;
		for (const Monitor &monitor : m_monitors) {
			QJsonObject obj;
			monitor.toJson(obj);
			monitorsJson.append(obj);
		}
		json["monitors"] = monitorsJson;
	}
}
Favorite Favorite::fromJson(const QString &path, const QJsonObject &json, const QMap<QString, Site *> &sites)
{
	const QString tag = json["tag"].toString();
	const int note = json["note"].toInt();
	const QDateTime lastViewed = QDateTime::fromString(json["lastViewed"].toString(), Qt::ISODate);

	QString thumbPath = path + "/thumbs/" + (QString(tag).remove('\\').remove('/').remove(':').remove('*').remove('?').remove('"').remove('<').remove('>').remove('|')) + ".png";
	if (!QFile::exists(thumbPath)) {
		thumbPath = ":/images/noimage.png";
	}

	QList<Monitor> monitors;
	if (json.contains("monitors")) {
		QJsonArray monitorsJson = json["monitors"].toArray();
		for (auto monitorJson : monitorsJson) {
			monitors.append(Monitor::fromJson(monitorJson.toObject(), sites));
		}
	}

	return Favorite(tag, note, lastViewed, monitors, thumbPath);
}

bool Favorite::sortByNote(const Favorite &s1, const Favorite &s2)
{ return s1.getNote() < s2.getNote(); }
bool Favorite::sortByName(const Favorite &s1, const Favorite &s2)
{ return s1.getName().toLower() < s2.getName().toLower(); }
bool Favorite::sortByLastViewed(const Favorite &s1, const Favorite &s2)
{ return s1.getLastViewed() < s2.getLastViewed(); }


bool operator==(const Favorite &lhs, const Favorite &rhs)
{ return lhs.getName().toLower() == rhs.getName().toLower(); }
bool operator!=(const Favorite &lhs, const Favorite &rhs)
{ return !(lhs == rhs); }
{'fraction_non_alphanumeric': 0.19677598665925514, 'fraction_numerical': 0.009783212896053363, 'mean_word_length': 6.3980263157894735, 'pattern_counts': {'":': 4, '<': 27, '<?xml version=': 0, '>': 25, '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': '330739', 'n_tokens_mistral': 3295, 'n_tokens_neox': 3158, 'n_words': 618}