GH-1227: World import using drag and drop - zip files and folders

This commit is contained in:
Petr Mrázek 2015-09-09 23:53:33 +02:00
parent 51070a13f7
commit a1fd50e920
15 changed files with 345 additions and 121 deletions

View File

@ -51,14 +51,13 @@ void GuiUtil::setClipboardText(const QString &text)
QApplication::clipboard()->setText(text); QApplication::clipboard()->setText(text);
} }
QStringList GuiUtil::BrowseForMods(QString context, QString caption, QString filter,
QWidget *parentWidget) QStringList GuiUtil::BrowseForFiles(QString context, QString caption, QString filter, QString defaultPath, QWidget *parentWidget)
{ {
static QMap<QString, QString> savedPaths; static QMap<QString, QString> savedPaths;
QFileDialog w(parentWidget, caption); QFileDialog w(parentWidget, caption);
QSet<QString> locations; QSet<QString> locations;
QString modsFolder = MMC->settings()->get("CentralModsDir").toString();
auto f = [&](QStandardPaths::StandardLocation l) auto f = [&](QStandardPaths::StandardLocation l)
{ {
QString location = QStandardPaths::writableLocation(l); QString location = QStandardPaths::writableLocation(l);
@ -76,19 +75,30 @@ QStringList GuiUtil::BrowseForMods(QString context, QString caption, QString fil
{ {
urls.append(QUrl::fromLocalFile(location)); urls.append(QUrl::fromLocalFile(location));
} }
urls.append(QUrl::fromLocalFile(modsFolder)); urls.append(QUrl::fromLocalFile(defaultPath));
w.setFileMode(QFileDialog::ExistingFiles); w.setFileMode(QFileDialog::ExistingFiles);
w.setAcceptMode(QFileDialog::AcceptOpen); w.setAcceptMode(QFileDialog::AcceptOpen);
w.setNameFilter(filter); w.setNameFilter(filter);
QString pathToOpen;
if(savedPaths.contains(context)) if(savedPaths.contains(context))
{ {
w.setDirectory(savedPaths[context]); pathToOpen = savedPaths[context];
} }
else else
{ {
w.setDirectory(modsFolder); pathToOpen = defaultPath;
} }
if(!pathToOpen.isEmpty())
{
QFileInfo finfo(pathToOpen);
if(finfo.exists() && finfo.isDir())
{
w.setDirectory(finfo.absoluteFilePath());
}
}
w.setSidebarUrls(urls); w.setSidebarUrls(urls);
if (w.exec()) if (w.exec())

View File

@ -6,5 +6,5 @@ namespace GuiUtil
{ {
void uploadPaste(const QString &text, QWidget *parentWidget); void uploadPaste(const QString &text, QWidget *parentWidget);
void setClipboardText(const QString &text); void setClipboardText(const QString &text);
QStringList BrowseForMods(QString context, QString caption, QString filter, QWidget *parentWidget); QStringList BrowseForFiles(QString context, QString caption, QString filter, QString defaultPath, QWidget *parentWidget);
} }

View File

@ -100,7 +100,7 @@ bool LegacyJarModPage::eventFilter(QObject *obj, QEvent *ev)
void LegacyJarModPage::on_addJarBtn_clicked() void LegacyJarModPage::on_addJarBtn_clicked()
{ {
auto list = GuiUtil::BrowseForMods("jarmod", tr("Select jar mods"), tr("Minecraft.jar mods (*.zip *.jar)"), this->parentWidget()); auto list = GuiUtil::BrowseForFiles("jarmod", tr("Select jar mods"), tr("Minecraft.jar mods (*.zip *.jar)"), MMC->settings()->get("CentralModsDir").toString(), this->parentWidget());
if(!list.empty()) if(!list.empty())
{ {
m_jarmods->stopWatching(); m_jarmods->stopWatching();

View File

@ -131,12 +131,13 @@ bool ModFolderPage::eventFilter(QObject *obj, QEvent *ev)
void ModFolderPage::on_addModBtn_clicked() void ModFolderPage::on_addModBtn_clicked()
{ {
auto list = GuiUtil::BrowseForMods( auto list = GuiUtil::BrowseForFiles(
m_helpName, m_helpName,
tr("Select %1", tr("Select %1",
"Select whatever type of files the page contains. Example: 'Loader Mods'") "Select whatever type of files the page contains. Example: 'Loader Mods'")
.arg(m_displayName), .arg(m_displayName),
m_filter.arg(m_displayName), this->parentWidget()); m_filter.arg(m_displayName), MMC->settings()->get("CentralModsDir").toString(),
this->parentWidget());
if (!list.empty()) if (!list.empty())
{ {
m_mods->stopWatching(); m_mods->stopWatching();
@ -146,8 +147,8 @@ void ModFolderPage::on_addModBtn_clicked()
} }
m_mods->startWatching(); m_mods->startWatching();
} }
} }
void ModFolderPage::on_rmModBtn_clicked() void ModFolderPage::on_rmModBtn_clicked()
{ {
int first, last; int first, last;

View File

@ -180,7 +180,7 @@ void VersionPage::on_jarmodBtn_clicked()
nagShown = true; nagShown = true;
} }
} }
auto list = GuiUtil::BrowseForMods("jarmod", tr("Select jar mods"), tr("Minecraft.jar mods (*.zip *.jar)"), this->parentWidget()); auto list = GuiUtil::BrowseForFiles("jarmod", tr("Select jar mods"), tr("Minecraft.jar mods (*.zip *.jar)"), MMC->settings()->get("CentralModsDir").toString(), this->parentWidget());
if(!list.empty()) if(!list.empty())
{ {
m_version->installJarMods(list); m_version->installJarMods(list);

View File

@ -25,6 +25,7 @@
#include "MultiMC.h" #include "MultiMC.h"
#include <GuiUtil.h>
WorldListPage::WorldListPage(BaseInstance *inst, std::shared_ptr<WorldList> worlds, QString id, WorldListPage::WorldListPage(BaseInstance *inst, std::shared_ptr<WorldList> worlds, QString id,
QString iconName, QString displayName, QString helpPage, QString iconName, QString displayName, QString helpPage,
@ -67,8 +68,6 @@ WorldListPage::~WorldListPage()
bool WorldListPage::shouldDisplay() const bool WorldListPage::shouldDisplay() const
{ {
if (m_inst)
return !m_inst->isRunning();
return true; return true;
} }
@ -200,3 +199,20 @@ void WorldListPage::worldChanged(const QModelIndex &current, const QModelIndex &
ui->mcEditBtn->setEnabled(enable); ui->mcEditBtn->setEnabled(enable);
ui->rmWorldBtn->setEnabled(enable); ui->rmWorldBtn->setEnabled(enable);
} }
void WorldListPage::on_addBtn_clicked()
{
auto list = GuiUtil::BrowseForFiles(
m_helpName,
tr("Select a Minecraft world zip"),
tr("Minecraft World Zip File (*.zip)"), QString(), this->parentWidget());
if (!list.empty())
{
m_worlds->stopWatching();
for (auto filename : list)
{
m_worlds->installWorld(QFileInfo(filename));
}
m_worlds->startWatching();
}
}

View File

@ -81,6 +81,7 @@ private slots:
void on_copySeedBtn_clicked(); void on_copySeedBtn_clicked();
void on_mcEditBtn_clicked(); void on_mcEditBtn_clicked();
void on_rmWorldBtn_clicked(); void on_rmWorldBtn_clicked();
void on_addBtn_clicked();
void on_viewFolderBtn_clicked(); void on_viewFolderBtn_clicked();
void worldChanged(const QModelIndex &current, const QModelIndex &previous); void worldChanged(const QModelIndex &current, const QModelIndex &previous);
}; };

View File

@ -48,7 +48,7 @@
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="dragDropMode"> <property name="dragDropMode">
<enum>QAbstractItemView::DropOnly</enum> <enum>QAbstractItemView::DragDrop</enum>
</property> </property>
<property name="sortingEnabled"> <property name="sortingEnabled">
<bool>true</bool> <bool>true</bool>
@ -63,6 +63,23 @@
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
<layout class="QVBoxLayout" name="verticalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPushButton" name="addBtn">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="rmWorldBtn">
<property name="text">
<string>&amp;Remove</string>
</property>
</widget>
</item>
<item>
<widget class="LineSeparator" name="separator" native="true"/>
</item>
<item> <item>
<widget class="QPushButton" name="mcEditBtn"> <widget class="QPushButton" name="mcEditBtn">
<property name="text"> <property name="text">
@ -77,13 +94,6 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="rmWorldBtn">
<property name="text">
<string>&amp;Remove</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">
@ -112,12 +122,21 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>LineSeparator</class>
<extends>QWidget</extends>
<header>widgets/LineSeparator.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops> <tabstops>
<tabstop>tabWidget</tabstop> <tabstop>tabWidget</tabstop>
<tabstop>worldTreeView</tabstop> <tabstop>worldTreeView</tabstop>
<tabstop>addBtn</tabstop>
<tabstop>rmWorldBtn</tabstop>
<tabstop>mcEditBtn</tabstop> <tabstop>mcEditBtn</tabstop>
<tabstop>copySeedBtn</tabstop> <tabstop>copySeedBtn</tabstop>
<tabstop>rmWorldBtn</tabstop>
<tabstop>viewFolderBtn</tabstop> <tabstop>viewFolderBtn</tabstop>
</tabstops> </tabstops>
<resources/> <resources/>

View File

@ -54,7 +54,7 @@ LIBUTIL_EXPORT bool ensureFolderPathExists(QString filenamepath);
/** /**
* Copy a folder recursively * Copy a folder recursively
*/ */
LIBUTIL_EXPORT bool copyPath(QString src, QString dst, bool follow_symlinks = true); LIBUTIL_EXPORT bool copyPath(const QString &src, const QString &dst, bool follow_symlinks = true);
/** /**
* Delete a folder recursively * Delete a folder recursively

View File

@ -81,16 +81,24 @@ QString RemoveInvalidFilenameChars(QString string, QChar replaceWith)
QString DirNameFromString(QString string, QString inDir) QString DirNameFromString(QString string, QString inDir)
{ {
int num = 0; int num = 0;
QString dirName = RemoveInvalidFilenameChars(string, '-'); QString baseName = RemoveInvalidFilenameChars(string, '-');
while (QFileInfo(PathCombine(inDir, dirName)).exists()) QString dirName;
do
{ {
num++; if(num == 0)
dirName = RemoveInvalidFilenameChars(dirName, '-') + QString::number(num); {
dirName = baseName;
}
else
{
dirName = baseName + QString::number(num);;
}
// If it's over 9000 // If it's over 9000
if (num > 9000) if (num > 9000)
return ""; return "";
} num++;
} while (QFileInfo(PathCombine(inDir, dirName)).exists());
return dirName; return dirName;
} }
@ -112,7 +120,7 @@ bool ensureFolderPathExists(QString foldernamepath)
return success; return success;
} }
bool copyPath(QString src, QString dst, bool follow_symlinks) bool copyPath(const QString &src, const QString &dst, bool follow_symlinks)
{ {
//NOTE always deep copy on windows. the alternatives are too messy. //NOTE always deep copy on windows. the alternatives are too messy.
#if defined Q_OS_WIN32 #if defined Q_OS_WIN32
@ -127,21 +135,26 @@ bool copyPath(QString src, QString dst, bool follow_symlinks)
bool OK = true; bool OK = true;
qDebug() << "Looking at " << dir.absolutePath();
foreach(QString f, dir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System)) foreach(QString f, dir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System))
{ {
QString inner_src = src + QDir::separator() + f; QString inner_src = src + QDir::separator() + f;
QString inner_dst = dst + QDir::separator() + f; QString inner_dst = dst + QDir::separator() + f;
qDebug() << f << "translates to"<< inner_src << "to" << inner_dst;
QFileInfo fileInfo(inner_src); QFileInfo fileInfo(inner_src);
if(!follow_symlinks && fileInfo.isSymLink()) if(!follow_symlinks && fileInfo.isSymLink())
{ {
qDebug() << "creating symlink" << inner_src << " - " << inner_dst;
OK &= QFile::link(fileInfo.symLinkTarget(),inner_dst); OK &= QFile::link(fileInfo.symLinkTarget(),inner_dst);
} }
else if (fileInfo.isDir()) else if (fileInfo.isDir())
{ {
qDebug() << "recursing" << inner_src << " - " << inner_dst;
OK &= copyPath(inner_src, inner_dst, follow_symlinks); OK &= copyPath(inner_src, inner_dst, follow_symlinks);
} }
else if (fileInfo.isFile()) else if (fileInfo.isFile())
{ {
qDebug() << "copying file" << inner_src << " - " << inner_dst;
OK &= QFile::copy(inner_src, inner_dst); OK &= QFile::copy(inner_src, inner_dst);
} }
else else

View File

@ -539,6 +539,7 @@ QMimeData *ModList::mimeData(const QModelIndexList &indexes) const
data->setText(params.join('|')); data->setText(params.join('|'));
return data; return data;
} }
bool ModList::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, bool ModList::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
const QModelIndex &parent) const QModelIndex &parent)
{ {

View File

@ -20,10 +20,13 @@
#include <pathutils.h> #include <pathutils.h>
#include "GZip.h" #include "GZip.h"
#include <MMCZip.h>
#include <sstream> #include <sstream>
#include <io/stream_reader.h> #include <io/stream_reader.h>
#include <tag_string.h> #include <tag_string.h>
#include <tag_primitive.h> #include <tag_primitive.h>
#include <quazip.h>
#include <quazipfile.h>
World::World(const QFileInfo &file) World::World(const QFileInfo &file)
{ {
@ -32,8 +35,20 @@ World::World(const QFileInfo &file)
void World::repath(const QFileInfo &file) void World::repath(const QFileInfo &file)
{ {
m_file = file; m_containerFile = file;
m_folderName = file.fileName(); m_folderName = file.fileName();
if(file.isFile() && file.suffix() == "zip")
{
readFromZip(file);
}
else if(file.isDir())
{
readFromFS(file);
}
}
void World::readFromFS(const QFileInfo &file)
{
QDir worldDir(file.filePath()); QDir worldDir(file.filePath());
is_valid = file.isDir() && worldDir.exists("level.dat"); is_valid = file.isDir() && worldDir.exists("level.dat");
if(!is_valid) if(!is_valid)
@ -48,66 +63,123 @@ void World::repath(const QFileInfo &file)
{ {
return; return;
} }
QFileInfo finfo(fullFilePath);
levelDatTime = finfo.lastModified();
parseLevelDat(f.readAll());
}
void World::readFromZip(const QFileInfo &file)
{
QuaZip zip(file.absoluteFilePath());
is_valid = zip.open(QuaZip::mdUnzip);
if (!is_valid)
{
return;
}
QuaZipFile zippedFile(&zip);
// read the install profile
is_valid = zip.setCurrentFile("level.dat");
if (!is_valid)
{
return;
}
is_valid = zippedFile.open(QIODevice::ReadOnly);
QuaZipFileInfo64 levelDatInfo;
zippedFile.getFileInfo(&levelDatInfo);
auto modTime = levelDatInfo.getNTFSmTime();
if(!modTime.isValid())
{
modTime = levelDatInfo.dateTime;
}
levelDatTime = modTime;
if (!is_valid)
{
return;
}
parseLevelDat(zippedFile.readAll());
zippedFile.close();
}
bool World::install(QString to)
{
auto finalPath = PathCombine(to, DirNameFromString(m_actualName, to));
if(!ensureFolderPathExists(finalPath))
{
return false;
}
if(m_containerFile.isFile())
{
// FIXME: check if this is OK.
return !MMCZip::extractDir(m_containerFile.absoluteFilePath(), finalPath).isEmpty();
}
else if(m_containerFile.isDir())
{
QString from = m_containerFile.filePath();
return copyPath(from, finalPath);
}
return false;
}
static QString read_string (nbt::value& parent, const char * name, const QString & fallback = QString())
{
try
{
auto &namedValue = parent.at(name);
if(namedValue.get_type() != nbt::tag_type::String)
{
return fallback;
}
auto & tag_str = namedValue.as<nbt::tag_string>();
return QString::fromStdString(tag_str.get());
}
catch(std::out_of_range e)
{
// fallback for old world formats
qWarning() << "String NBT tag" << name << "could not be found. Defaulting to" << fallback;
return fallback;
}
catch(std::bad_cast e)
{
// type mismatch
qWarning() << "NBT tag" << name << "could not be converted to string. Defaulting to" << fallback;
return fallback;
}
};
static int64_t read_long (nbt::value& parent, const char * name, const int64_t & fallback = 0)
{
try
{
auto &namedValue = parent.at(name);
if(namedValue.get_type() != nbt::tag_type::Long)
{
return fallback;
}
auto & tag_str = namedValue.as<nbt::tag_long>();
return tag_str.get();
}
catch(std::out_of_range e)
{
// fallback for old world formats
qWarning() << "Long NBT tag" << name << "could not be found. Defaulting to" << fallback;
return fallback;
}
catch(std::bad_cast e)
{
// type mismatch
qWarning() << "NBT tag" << name << "could not be converted to long. Defaulting to" << fallback;
return fallback;
}
};
void World::parseLevelDat(QByteArray data)
{
QByteArray output; QByteArray output;
is_valid = GZip::inflate(f.readAll(), output); is_valid = GZip::inflate(data, output);
if(!is_valid) if(!is_valid)
{ {
return; return;
} }
f.close();
auto read_string = [](nbt::value& parent, const char * name, const QString & fallback = QString()) -> QString
{
try
{
auto &namedValue = parent.at(name);
if(namedValue.get_type() != nbt::tag_type::String)
{
return fallback;
}
auto & tag_str = namedValue.as<nbt::tag_string>();
return QString::fromStdString(tag_str.get());
}
catch(std::out_of_range e)
{
// fallback for old world formats
qWarning() << "String NBT tag" << name << "could not be found. Defaulting to" << fallback;
return fallback;
}
catch(std::bad_cast e)
{
// type mismatch
qWarning() << "NBT tag" << name << "could not be converted to string. Defaulting to" << fallback;
return fallback;
}
};
auto read_long = [](nbt::value& parent, const char * name, const int64_t & fallback = 0) -> int64_t
{
try
{
auto &namedValue = parent.at(name);
if(namedValue.get_type() != nbt::tag_type::Long)
{
return fallback;
}
auto & tag_str = namedValue.as<nbt::tag_long>();
return tag_str.get();
}
catch(std::out_of_range e)
{
// fallback for old world formats
qWarning() << "Long NBT tag" << name << "could not be found. Defaulting to" << fallback;
return fallback;
}
catch(std::bad_cast e)
{
// type mismatch
qWarning() << "NBT tag" << name << "could not be converted to long. Defaulting to" << fallback;
return fallback;
}
};
try try
{ {
@ -138,8 +210,7 @@ void World::repath(const QFileInfo &file)
int64_t temp = read_long(val, "LastPlayed", 0); int64_t temp = read_long(val, "LastPlayed", 0);
if(temp == 0) if(temp == 0)
{ {
QFileInfo finfo(fullFilePath); m_lastPlayed = levelDatTime;
m_lastPlayed = finfo.lastModified();
} }
else else
{ {
@ -164,11 +235,11 @@ bool World::replace(World &with)
{ {
if (!destroy()) if (!destroy())
return false; return false;
bool success = copyPath(with.m_file.filePath(), m_file.path()); bool success = copyPath(with.m_containerFile.filePath(), m_containerFile.path());
if (success) if (success)
{ {
m_folderName = with.m_folderName; m_folderName = with.m_folderName;
m_file.refresh(); m_containerFile.refresh();
} }
return success; return success;
} }
@ -176,18 +247,15 @@ bool World::replace(World &with)
bool World::destroy() bool World::destroy()
{ {
if(!is_valid) return false; if(!is_valid) return false;
if (m_file.isDir()) if (m_containerFile.isDir())
{ {
QDir d(m_file.filePath()); QDir d(m_containerFile.filePath());
if (d.removeRecursively()) return d.removeRecursively();
{
return true;
}
return false;
} }
else else if(m_containerFile.isFile())
{ {
return false; QFile file(m_containerFile.absoluteFilePath());
return file.remove();
} }
return true; return true;
} }

View File

@ -48,15 +48,23 @@ public:
// change the world's filesystem path (used by world lists for *MAGIC* purposes) // change the world's filesystem path (used by world lists for *MAGIC* purposes)
void repath(const QFileInfo &file); void repath(const QFileInfo &file);
bool install(QString to);
// WEAK compare operator - used for replacing worlds // WEAK compare operator - used for replacing worlds
bool operator==(const World &other) const; bool operator==(const World &other) const;
bool strongCompare(const World &other) const; bool strongCompare(const World &other) const;
private:
void readFromZip(const QFileInfo &file);
void readFromFS(const QFileInfo &file);
void parseLevelDat(QByteArray data);
protected: protected:
QFileInfo m_file; QFileInfo m_containerFile;
QString m_folderName; QString m_folderName;
QString m_actualName; QString m_actualName;
QDateTime levelDatTime;
QDateTime m_lastPlayed; QDateTime m_lastPlayed;
int64_t m_randomSeed = 0; int64_t m_randomSeed = 0;
bool is_valid = false; bool is_valid = false;

View File

@ -62,40 +62,28 @@ void WorldList::stopWatching()
} }
} }
void WorldList::internalSort(QList<World> &what)
{
auto predicate = [](const World &left, const World &right)
{
return left.folderName().localeAwareCompare(right.folderName()) < 0;
};
std::sort(what.begin(), what.end(), predicate);
}
bool WorldList::update() bool WorldList::update()
{ {
if (!isValid()) if (!isValid())
return false; return false;
QList<World> orderedWorlds;
QList<World> newWorlds; QList<World> newWorlds;
m_dir.refresh(); m_dir.refresh();
auto folderContents = m_dir.entryInfoList(); auto folderContents = m_dir.entryInfoList();
// if there are any untracked files... // if there are any untracked files...
if (folderContents.size()) for (QFileInfo entry : folderContents)
{ {
// the order surely changed! if(!entry.isDir())
for (auto entry : folderContents) continue;
World w(entry);
if(w.isValid())
{ {
World w(entry); newWorlds.append(w);
if(w.isValid()) {
newWorlds.append(w);
}
} }
internalSort(newWorlds);
orderedWorlds.append(newWorlds);
} }
beginResetModel(); beginResetModel();
worlds.swap(orderedWorlds); worlds.swap(newWorlds);
endResetModel(); endResetModel();
return true; return true;
} }
@ -232,6 +220,7 @@ QStringList WorldList::mimeTypes() const
{ {
QStringList types; QStringList types;
types << "text/plain"; types << "text/plain";
types << "text/uri-list";
return types; return types;
} }
@ -250,3 +239,90 @@ QMimeData *WorldList::mimeData(const QModelIndexList &indexes) const
data->setText(QString::number(row)); data->setText(QString::number(row));
return data; return data;
} }
Qt::ItemFlags WorldList::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
if (index.isValid())
return Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled |
defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
Qt::DropActions WorldList::supportedDragActions() const
{
// move to other mod lists or VOID
return Qt::MoveAction;
}
Qt::DropActions WorldList::supportedDropActions() const
{
// copy from outside, move from within and other mod lists
return Qt::CopyAction | Qt::MoveAction;
}
void WorldList::installWorld(QFileInfo filename)
{
qDebug() << "installing: " << filename.absoluteFilePath();
World w(filename);
if(!w.isValid())
{
return;
}
w.install(m_dir.absolutePath());
}
bool WorldList::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
const QModelIndex &parent)
{
if (action == Qt::IgnoreAction)
return true;
// check if the action is supported
if (!data || !(action & supportedDropActions()))
return false;
// files dropped from outside?
if (data->hasUrls())
{
bool was_watching = is_watching;
if (was_watching)
stopWatching();
auto urls = data->urls();
for (auto url : urls)
{
// only local files may be dropped...
if (!url.isLocalFile())
continue;
QString filename = url.toLocalFile();
QFileInfo worldInfo(filename);
installWorld(worldInfo);
}
if (was_watching)
startWatching();
return true;
}
/*
else if (data->hasText())
{
QString sourcestr = data->text();
auto list = sourcestr.split('|');
if (list.size() != 2)
return false;
QString remoteId = list[0];
int remoteIndex = list[1].toInt();
qDebug() << "move: " << sourcestr;
// no moving of things between two lists
if (remoteId != m_list_id)
return false;
// no point moving to the same place...
if (row == remoteIndex)
return false;
// otherwise, move the mod :D
moveModTo(remoteIndex, row);
return true;
}
*/
return false;
}

View File

@ -19,6 +19,7 @@
#include <QString> #include <QString>
#include <QDir> #include <QDir>
#include <QAbstractListModel> #include <QAbstractListModel>
#include <QMimeData>
#include "minecraft/World.h" #include "minecraft/World.h"
#include "multimc_logic_export.h" #include "multimc_logic_export.h"
@ -71,16 +72,28 @@ public:
/// Reloads the mod list and returns true if the list changed. /// Reloads the mod list and returns true if the list changed.
virtual bool update(); virtual bool update();
/// Install a world from location
void installWorld(QFileInfo filename);
/// Deletes the mod at the given index. /// Deletes the mod at the given index.
virtual bool deleteWorld(int index); virtual bool deleteWorld(int index);
/// Deletes all the selected mods /// Deletes all the selected mods
virtual bool deleteWorlds(int first, int last); virtual bool deleteWorlds(int first, int last);
/// flags, mostly to support drag&drop
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
/// get data for drag action /// get data for drag action
virtual QMimeData *mimeData(const QModelIndexList &indexes) const; virtual QMimeData *mimeData(const QModelIndexList &indexes) const;
/// get the supported mime types /// get the supported mime types
virtual QStringList mimeTypes() const; virtual QStringList mimeTypes() const;
/// process data from drop action
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);
/// what drag actions do we support?
virtual Qt::DropActions supportedDragActions() const;
/// what drop actions do we support?
virtual Qt::DropActions supportedDropActions() const;
void startWatching(); void startWatching();
void stopWatching(); void stopWatching();
@ -97,8 +110,6 @@ public:
return worlds; return worlds;
} }
private:
void internalSort(QList<World> &what);
private slots: private slots:
void directoryChanged(QString path); void directoryChanged(QString path);