Implemented version list sorting.
Resolves MMC-8: https://jira.forkk.net/browse/MMC-8
This commit is contained in:
		@@ -104,6 +104,30 @@ public:
 | 
			
		||||
	 */
 | 
			
		||||
	virtual InstVersion *copyVersion(InstVersionList *newParent) const = 0;
 | 
			
		||||
	
 | 
			
		||||
	/*!
 | 
			
		||||
	 * Checks if this version is less (older) than the given version.
 | 
			
		||||
	 * \param other The version to compare this one to.
 | 
			
		||||
	 * \return True if this version is older than the given version.
 | 
			
		||||
	 */
 | 
			
		||||
	virtual bool isLessThan(const InstVersion &other) const;
 | 
			
		||||
	
 | 
			
		||||
	/*!
 | 
			
		||||
	 * Checks if this version is greater (newer) than the given version.
 | 
			
		||||
	 * \param other The version to compare this one to.
 | 
			
		||||
	 * \return True if this version is newer than the given version.
 | 
			
		||||
	 */
 | 
			
		||||
	virtual bool isGreaterThan(const InstVersion &other) const;
 | 
			
		||||
	
 | 
			
		||||
	/*!
 | 
			
		||||
	 * \sa shouldSortBefore()
 | 
			
		||||
	 */
 | 
			
		||||
	virtual bool operator<(const InstVersion &rhs) { return isLessThan(rhs); }
 | 
			
		||||
	
 | 
			
		||||
	/*!
 | 
			
		||||
	 * \sa shouldSortAfter()
 | 
			
		||||
	 */
 | 
			
		||||
	virtual bool operator>(const InstVersion &rhs) { return isGreaterThan(rhs); }
 | 
			
		||||
	
 | 
			
		||||
protected:
 | 
			
		||||
	QString m_descriptor;
 | 
			
		||||
	QString m_name;
 | 
			
		||||
 
 | 
			
		||||
@@ -88,6 +88,11 @@ public:
 | 
			
		||||
	 */
 | 
			
		||||
	virtual const InstVersion *getLatestStable();
 | 
			
		||||
	
 | 
			
		||||
	/*!
 | 
			
		||||
	 * Sorts the version list.
 | 
			
		||||
	 */
 | 
			
		||||
	virtual void sort() = 0;
 | 
			
		||||
	
 | 
			
		||||
protected slots:
 | 
			
		||||
	/*!
 | 
			
		||||
	 * Updates this list with the given list of versions.
 | 
			
		||||
 
 | 
			
		||||
@@ -45,12 +45,14 @@ public:
 | 
			
		||||
	virtual const InstVersion *at(int i) const;
 | 
			
		||||
	virtual int count() const;
 | 
			
		||||
	virtual void printToStdOut() const;
 | 
			
		||||
	virtual void sort();
 | 
			
		||||
	
 | 
			
		||||
	/*!
 | 
			
		||||
	 * Gets the main version list instance.
 | 
			
		||||
	 */
 | 
			
		||||
	static MinecraftVersionList &getMainList();
 | 
			
		||||
	
 | 
			
		||||
	
 | 
			
		||||
protected:
 | 
			
		||||
	QList<InstVersion *>m_vlist;
 | 
			
		||||
	
 | 
			
		||||
 
 | 
			
		||||
@@ -41,6 +41,16 @@ InstVersionList *InstVersion::versionList() const
 | 
			
		||||
		return (InstVersionList *)parent();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool InstVersion::isLessThan(const InstVersion &other) const
 | 
			
		||||
{
 | 
			
		||||
	return timestamp() < other.timestamp();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool InstVersion::isGreaterThan(const InstVersion &other) const
 | 
			
		||||
{
 | 
			
		||||
	return timestamp() > other.timestamp();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool InstVersion::isMeta() const
 | 
			
		||||
{
 | 
			
		||||
	return false;
 | 
			
		||||
 
 | 
			
		||||
@@ -48,6 +48,9 @@ enum VListColumns
 | 
			
		||||
	// Second column - Type
 | 
			
		||||
	TypeColumn,
 | 
			
		||||
	
 | 
			
		||||
	// Third column - Timestamp
 | 
			
		||||
	TimeColumn,
 | 
			
		||||
	
 | 
			
		||||
	// Column count
 | 
			
		||||
	ColCount
 | 
			
		||||
};
 | 
			
		||||
@@ -74,6 +77,9 @@ QVariant InstVersionList::data(const QModelIndex &index, int role) const
 | 
			
		||||
		case TypeColumn:
 | 
			
		||||
			return version->typeName();
 | 
			
		||||
			
 | 
			
		||||
		case TimeColumn:
 | 
			
		||||
			return version->timestamp();
 | 
			
		||||
			
 | 
			
		||||
		default:
 | 
			
		||||
			return QVariant();
 | 
			
		||||
		}
 | 
			
		||||
@@ -101,6 +107,9 @@ QVariant InstVersionList::headerData(int section, Qt::Orientation orientation, i
 | 
			
		||||
			
 | 
			
		||||
		case TypeColumn:
 | 
			
		||||
			return "Type";
 | 
			
		||||
			
 | 
			
		||||
		case TimeColumn:
 | 
			
		||||
			return "Time";
 | 
			
		||||
		
 | 
			
		||||
		default:
 | 
			
		||||
			return QVariant();
 | 
			
		||||
 
 | 
			
		||||
@@ -25,6 +25,8 @@
 | 
			
		||||
#include <QJsonValue>
 | 
			
		||||
#include <QJsonParseError>
 | 
			
		||||
 | 
			
		||||
#include <QtAlgorithms>
 | 
			
		||||
 | 
			
		||||
#include <QtNetwork>
 | 
			
		||||
 | 
			
		||||
#define MCVLIST_URLBASE "http://s3.amazonaws.com/Minecraft.Download/versions/"
 | 
			
		||||
@@ -78,6 +80,18 @@ void MinecraftVersionList::printToStdOut() const
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool cmpVersions(const InstVersion *first, const InstVersion *second)
 | 
			
		||||
{
 | 
			
		||||
	return !first->isLessThan(*second);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MinecraftVersionList::sort()
 | 
			
		||||
{
 | 
			
		||||
	beginResetModel();
 | 
			
		||||
	qSort(m_vlist.begin(), m_vlist.end(), cmpVersions);
 | 
			
		||||
	endResetModel();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MinecraftVersionList &MinecraftVersionList::getMainList()
 | 
			
		||||
{
 | 
			
		||||
	return mcVList;
 | 
			
		||||
@@ -108,6 +122,9 @@ void MinecraftVersionList::updateListData(QList<InstVersion *> versions)
 | 
			
		||||
	// tempList (and vice-versa). Now we just free the memory.
 | 
			
		||||
	while (!tempList.isEmpty())
 | 
			
		||||
		delete tempList.takeFirst();
 | 
			
		||||
	
 | 
			
		||||
	// NOW SORT!!
 | 
			
		||||
	sort();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname)
 | 
			
		||||
@@ -125,6 +142,22 @@ inline QDateTime timeFromS3Time(QString str)
 | 
			
		||||
	return QDateTime::fromString(str, fmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline QDateTime timeFromMCVListTime(QString str)
 | 
			
		||||
{
 | 
			
		||||
	int operatorPos = str.indexOf("+", str.indexOf("T"));
 | 
			
		||||
	if (operatorPos == -1)
 | 
			
		||||
		operatorPos = str.indexOf("-", str.indexOf("T"));
 | 
			
		||||
	if (operatorPos)
 | 
			
		||||
		operatorPos = str.length();
 | 
			
		||||
	
 | 
			
		||||
	const QString fmt("yyyy-MM-dd'T'HH:mm:ss'+02:00'");
 | 
			
		||||
	
 | 
			
		||||
	// It's a dark templar!
 | 
			
		||||
	QDateTime dt = QDateTime::fromString(str.left(operatorPos), fmt);
 | 
			
		||||
	return dt;
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline void waitForNetRequest(QNetworkReply *netReply)
 | 
			
		||||
{
 | 
			
		||||
	QEventLoop loop;
 | 
			
		||||
@@ -234,7 +267,11 @@ bool MCVListLoadTask::loadFromVList()
 | 
			
		||||
				// Now, process that info and add the version to the list.
 | 
			
		||||
				
 | 
			
		||||
				// Parse the timestamp.
 | 
			
		||||
				QDateTime versionTime = timeFromS3Time(versionTimeStr);
 | 
			
		||||
				QDateTime versionTime = timeFromMCVListTime(versionTimeStr);
 | 
			
		||||
				
 | 
			
		||||
				Q_ASSERT_X(versionTime.isValid(), "loadFromVList",
 | 
			
		||||
						   QString("in versions array, index %1's timestamp failed to parse").
 | 
			
		||||
						   arg(i).toUtf8());
 | 
			
		||||
				
 | 
			
		||||
				// Parse the type.
 | 
			
		||||
				MinecraftVersion::VersionType versionType;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user