Skip to content

Commit

Permalink
Add nexus category ID to mod info
Browse files Browse the repository at this point in the history
- We will check the mod info first then fall back to the download file
- Add category field in the info dialog nexus tab
  • Loading branch information
Silarn committed Sep 19, 2023
1 parent f9be24f commit dd0896d
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 171 deletions.
2 changes: 2 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3316,6 +3316,8 @@ void MainWindow::nxmModInfoAvailable(QString gameName, int modID, QVariant userD

mod->setNexusDescription(result["description"].toString());

mod->setNexusCategory(result["category_id"].toInt());

if ((mod->endorsedState() != EndorsedState::ENDORSED_NEVER) &&
(result.contains("endorsement"))) {
QVariantMap endorsement = result["endorsement"].toMap();
Expand Down
10 changes: 10 additions & 0 deletions src/modinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,16 @@ class ModInfo : public QObject, public MOBase::IModInterface
* @brief Set the last time the mod was updated on Nexus.
*/
virtual void setNexusLastModified(QDateTime time) = 0;

/**
* @return the assigned nexus category ID
*/
virtual int getNexusCategory() const = 0;

/**
* @brief Assigns the given Nexus category ID
*/
virtual void setNexusCategory(int category) = 0;

public: // Conflicts
// retrieve the list of mods (as mod index) that are overwritten by this one.
Expand Down
2 changes: 2 additions & 0 deletions src/modinfobackup.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ModInfoBackup : public ModInfoRegular
virtual QDateTime getNexusLastModified() const override { return QDateTime(); }
virtual void setNexusLastModified(QDateTime) override {}
virtual QString getNexusDescription() const override { return QString(); }
virtual void setNexusCategory(int) override {}
virtual int getNexusCategory() const override { return 0; }
virtual bool isBackup() const override { return true; }

virtual void addInstalledFile(int, int) override {}
Expand Down
25 changes: 24 additions & 1 deletion src/modinfodialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ text-align: left;</string>
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6" stretch="0,0,0,0,0,0,1">
<layout class="QHBoxLayout" name="horizontalLayout_6" stretch="0,0,0,0,0,0,0,0,1">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
Expand Down Expand Up @@ -1026,6 +1026,29 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="categoryLabel">
<property name="text">
<string>Category</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="category">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
Expand Down
16 changes: 16 additions & 0 deletions src/modinfodialognexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ NexusTab::NexusTab(ModInfoDialogTabContext cx)
connect(ui->version, &QLineEdit::editingFinished, [&] {
onVersionChanged();
});
connect(ui->category, &QLineEdit::editingFinished, [&] {
onCategoryChanged();
});

connect(ui->refresh, &QPushButton::clicked, [&] {
onRefreshBrowser();
Expand Down Expand Up @@ -75,6 +78,7 @@ void NexusTab::clear()
ui->modID->clear();
ui->sourceGame->clear();
ui->version->clear();
ui->category->clear();
ui->browser->setPage(new NexusTabWebpage(ui->browser));
ui->hasCustomURL->setChecked(false);
ui->customURL->clear();
Expand Down Expand Up @@ -108,6 +112,8 @@ void NexusTab::update()

ui->sourceGame->setCurrentIndex(ui->sourceGame->findData(gameName));

ui->category->setText(QString("%1").arg(mod().getNexusCategory()));

auto* page = new NexusTabWebpage(ui->browser);
ui->browser->setPage(page);

Expand Down Expand Up @@ -360,6 +366,16 @@ void NexusTab::onVersionChanged()
updateVersionColor();
}

void NexusTab::onCategoryChanged()
{
if (m_loading) {
return;
}

int category = ui->category->text().toInt();
mod().setNexusCategory(category);
}

void NexusTab::onRefreshBrowser()
{
const auto modID = mod().nexusId();
Expand Down
1 change: 1 addition & 0 deletions src/modinfodialognexus.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class NexusTab : public ModInfoDialogTab
void onModIDChanged();
void onSourceGameChanged();
void onVersionChanged();
void onCategoryChanged();

void onRefreshBrowser();
void onVisitNexus();
Expand Down
2 changes: 2 additions & 0 deletions src/modinfoforeign.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class ModInfoForeign : public ModInfoWithConflictInfo
virtual void setNexusFileStatus(int) override {}
virtual QDateTime getLastNexusUpdate() const override { return QDateTime(); }
virtual void setLastNexusUpdate(QDateTime) override {}
virtual int getNexusCategory() const override { return 0; }
virtual void setNexusCategory(int) override {}
virtual QDateTime getLastNexusQuery() const override { return QDateTime(); }
virtual void setLastNexusQuery(QDateTime) override {}
virtual QDateTime getNexusLastModified() const override { return QDateTime(); }
Expand Down
2 changes: 2 additions & 0 deletions src/modinfooverwrite.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class ModInfoOverwrite : public ModInfoWithConflictInfo
virtual QDateTime getNexusLastModified() const override { return QDateTime(); }
virtual void setNexusLastModified(QDateTime) override {}
virtual QString getNexusDescription() const override { return QString(); }
virtual void setNexusCategory(int) override {}
virtual int getNexusCategory() const override { return 0; }
virtual QStringList archives(bool checkOnDisk = false) override;
virtual void addInstalledFile(int, int) override {}
virtual std::set<std::pair<int, int>> installedFiles() const override { return {}; }
Expand Down
23 changes: 19 additions & 4 deletions src/modinforegular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ void ModInfoRegular::readMeta()
m_InstallationFile = metaFile.value("installationFile", "").toString();
m_NexusDescription = metaFile.value("nexusDescription", "").toString();
m_NexusFileStatus = metaFile.value("nexusFileStatus", "1").toInt();
m_NexusCategory = metaFile.value("nexusCategory", 0).toInt();
m_Repository = metaFile.value("repository", "Nexus").toString();
m_Converted = metaFile.value("converted", false).toBool();
m_Validated = metaFile.value("validated", false).toBool();
Expand Down Expand Up @@ -171,10 +172,11 @@ void ModInfoRegular::readMeta()
m_NexusLastModified = QDateTime::fromString(
metaFile.value("nexusLastModified", QDateTime::currentDateTimeUtc()).toString(),
Qt::ISODate);
m_Color = metaFile.value("color", QColor()).value<QColor>();
m_TrackedState = metaFile.value("tracked", false).toBool()
? TrackedState::TRACKED_TRUE
: TrackedState::TRACKED_FALSE;
m_NexusCategory = metaFile.value("nexusCategory", 0).toInt();
m_Color = metaFile.value("color", QColor()).value<QColor>();
m_TrackedState = metaFile.value("tracked", false).toBool()
? TrackedState::TRACKED_TRUE
: TrackedState::TRACKED_FALSE;
if (metaFile.contains("endorsed")) {
if (metaFile.value("endorsed").canConvert<int>()) {
using ut = std::underlying_type_t<EndorsedState>;
Expand Down Expand Up @@ -267,6 +269,7 @@ void ModInfoRegular::saveMeta()
metaFile.setValue("lastNexusQuery", m_LastNexusQuery.toString(Qt::ISODate));
metaFile.setValue("lastNexusUpdate", m_LastNexusUpdate.toString(Qt::ISODate));
metaFile.setValue("nexusLastModified", m_NexusLastModified.toString(Qt::ISODate));
metaFile.setValue("nexusCategory", m_NexusCategory);
metaFile.setValue("converted", m_Converted);
metaFile.setValue("validated", m_Validated);
metaFile.setValue("color", m_Color);
Expand Down Expand Up @@ -834,6 +837,18 @@ void ModInfoRegular::setNexusLastModified(QDateTime time)
emit modDetailsUpdated(true);
}

int ModInfoRegular::getNexusCategory() const
{
return m_NexusCategory;
}

void ModInfoRegular::setNexusCategory(int category)
{
m_NexusCategory = category;
m_MetaInfoChanged = true;
saveMeta();
}

void ModInfoRegular::setCustomURL(QString const& url)
{
m_CustomURL = url;
Expand Down
11 changes: 11 additions & 0 deletions src/modinforegular.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ class ModInfoRegular : public ModInfoWithConflictInfo
*/
virtual void setNexusLastModified(QDateTime time) override;

/**
* @return the assigned nexus category ID
*/
virtual int getNexusCategory() const override;

/**
* @brief Assigns the given Nexus category ID
*/
virtual void setNexusCategory(int category) override;

virtual QStringList archives(bool checkOnDisk = false) override;

virtual void setColor(QColor color) override;
Expand Down Expand Up @@ -457,6 +467,7 @@ private slots:
QDateTime m_LastNexusQuery;
QDateTime m_LastNexusUpdate;
QDateTime m_NexusLastModified;
int m_NexusCategory;

QColor m_Color;

Expand Down
2 changes: 2 additions & 0 deletions src/modinfoseparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class ModInfoSeparator : public ModInfoRegular
virtual void setLastNexusQuery(QDateTime) override {}
virtual QDateTime getNexusLastModified() const override { return QDateTime(); }
virtual void setNexusLastModified(QDateTime) override {}
virtual int getNexusCategory() const override { return 0; }
virtual void setNexusCategory(int) override {}
virtual QDateTime creationTime() const override { return QDateTime(); }
virtual QString getNexusDescription() const override { return QString(); }
virtual void addInstalledFile(int /*modId*/, int /*fileId*/) override {}
Expand Down
44 changes: 25 additions & 19 deletions src/modlistviewactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,20 @@ void ModListViewActions::assignCategories() const
for (auto mod : m_core.modList()->allMods()) {
ModInfo::Ptr modInfo = ModInfo::getByName(mod);
QString file = modInfo->installationFile();
auto download = m_core.downloadManager()->getDownloadIndex(file);
if (download >= 0) {
int nexusCategory = m_core.downloadManager()->getCategoryID(download);
int newCategory = CategoryFactory::instance()->resolveNexusID(nexusCategory);
if (newCategory != 0) {
for (auto category : modInfo->categories()) {
modInfo->removeCategory(category);
}
int nexusCategory = modInfo->getNexusCategory();
if (!nexusCategory) {
auto download = m_core.downloadManager()->getDownloadIndex(file);
if (download >= 0) {
int nexusCategory = m_core.downloadManager()->getCategoryID(download);
}
}
int newCategory = CategoryFactory::instance()->resolveNexusID(nexusCategory);
if (newCategory != 0) {
for (auto category : modInfo->categories()) {
modInfo->removeCategory(category);
}
modInfo->setCategory(CategoryFactory::instance()->getCategoryID(newCategory),
true);
}
modInfo->setCategory(CategoryFactory::instance()->getCategoryID(newCategory), true);
}
}

Expand Down Expand Up @@ -1108,16 +1110,20 @@ void ModListViewActions::remapCategory(const QModelIndexList& indices) const
for (auto& idx : indices) {
ModInfo::Ptr modInfo = ModInfo::getByIndex(idx.data(ModList::IndexRole).toInt());

int downloadIndex =
m_core.downloadManager()->getDownloadIndex(modInfo->installationFile());
if (downloadIndex >= 0) {
auto downloadInfo = m_core.downloadManager()->getFileInfo(downloadIndex);
unsigned int categoryIndex =
CategoryFactory::instance()->resolveNexusID(downloadInfo->categoryID);
if (categoryIndex != 0)
modInfo->setPrimaryCategory(
CategoryFactory::instance()->getCategoryID(categoryIndex));
int categoryID = modInfo->getNexusCategory();
if (!categoryID) {
int downloadIndex =
m_core.downloadManager()->getDownloadIndex(modInfo->installationFile());
if (downloadIndex >= 0) {
auto downloadInfo = m_core.downloadManager()->getFileInfo(downloadIndex);
categoryID = downloadInfo->categoryID;
}
}
unsigned int categoryIndex =
CategoryFactory::instance()->resolveNexusID(categoryID);
if (categoryIndex != 0)
modInfo->setPrimaryCategory(
CategoryFactory::instance()->getCategoryID(categoryIndex));
}
}

Expand Down
Loading

0 comments on commit dd0896d

Please sign in to comment.