Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/uibase/nxmurl.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,31 @@ class QDLLEXPORT NXMUrl : public QObject
*/
int userId() const { return m_UserId; }

/**
* @return if collection link
*/
bool isCollection() const { return m_Collection; }

/**
* @return collection ID
*/
QString collectionId() const { return m_CollectionId; }

/**
* @return collection revision
*/
int collectionRevision() const { return m_CollectionRevision; }

private:
QString m_Game;
QString m_Key;
int m_ModId;
int m_FileId;
int m_Expires;
int m_UserId;
bool m_Collection;
QString m_CollectionId;
int m_CollectionRevision;
};

#endif // NXMURL_H
32 changes: 22 additions & 10 deletions src/nxmurl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,28 @@ NXMUrl::NXMUrl(const QString& url)
{
QUrl nxm(url);
QUrlQuery query(nxm);
QRegularExpression exp("nxm://[a-z0-9]+/mods/(\\d+)/files/(\\d+)",
QRegularExpression::CaseInsensitiveOption);
auto match = exp.match(url);
if (!match.hasMatch()) {

static const QRegularExpression modRegex("nxm://[a-z0-9]+/mods/(\\d+)/files/(\\d+)",
QRegularExpression::CaseInsensitiveOption);
static const QRegularExpression collectionRegex(
"nxm://[a-z0-9]+/collections/([a-z0-9]+)/revisions/(\\d+)",
QRegularExpression::CaseInsensitiveOption);

m_Game = nxm.host();

if (const auto modMatch = modRegex.match(url); modMatch.hasMatch()) {
m_Collection = false;
m_ModId = modMatch.captured(1).toInt();
m_FileId = modMatch.captured(2).toInt();
m_Key = query.queryItemValue("key");
m_Expires = query.queryItemValue("expires").toInt();
m_UserId = query.queryItemValue("user_id").toInt();
} else if (const auto collectionMatch = collectionRegex.match(url);
collectionMatch.hasMatch()) {
m_Collection = true;
m_CollectionId = collectionMatch.captured(1);
m_CollectionRevision = collectionMatch.captured(2).toInt();
} else {
throw MOBase::InvalidNXMLinkException(url);
}
Comment on lines 30 to 55

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is a bit confusing here, I would suggest something like:

QUrl nxm(url);
QUrlQuery query(nxm);

static const QRegularExpression modRegex(
    "nxm://[a-z0-9]+/mods/(\\d+)/files/(\\d+)",
    QRegularExpression::CaseInsensitiveOption);
static const QRegularExpression collectionRegex(
    "nxm://[a-z0-9]+/collections/([a-z0-9]+)/revisions/(\\d+)",
    QRegularExpression::CaseInsensitiveOption);

m_Game = nxm.host();

if (const auto modMatch = modRegex.match(url); modMatch.hasMatch()) {
  m_Collection = false;
  m_ModId      = modMatch.captured(1).toInt();
  m_FileId     = modMatch.captured(2).toInt();
  m_Key        = query.queryItemValue("key");
  m_Expires    = query.queryItemValue("expires").toInt();
  m_UserId     = query.queryItemValue("user_id").toInt();
} else if (const auto collectionMatch = collectionRegex.match(url);
           collectionMatch.hasMatch()) {
  m_Collection         = true;
  m_CollectionId       = collectionMatch.captured(1);
  m_CollectionRevision = collectionMatch.captured(2).toInt();
} else {
  throw MOBase::InvalidNXMLinkException(url);
}

m_Game = nxm.host();
m_ModId = match.captured(1).toInt();
m_FileId = match.captured(2).toInt();
m_Key = query.queryItemValue("key");
m_Expires = query.queryItemValue("expires").toInt();
m_UserId = query.queryItemValue("user_id").toInt();
}
Loading