void DefinitionManager::installZip(QString path, bool overwrite,
                                   bool install) {
  QString destdir = QStandardPaths::writableLocation(
      QStandardPaths::DataLocation);
  ZipReader zip(path);
  if (!zip.open()) {
    QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
                         tr("Corrupt zip"),
                         QMessageBox::Cancel);
    return;
  }
  // fetch the pack info
  JSONData *info;
  try {
    info = JSON::parse(zip.get("pack_info.json"));
  } catch (JSONParseException e) {
    QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
                         tr("pack_info.json : %1").arg(e.reason),
                         QMessageBox::Cancel);
    zip.close();
    return;
  }
  // let's verify all the jsons in the pack
  for (int i = 0; i < info->at("data")->length(); i++) {
    JSONData *def;
    try {
      def = JSON::parse(zip.get(info->at("data")->at(i)->asString()));
      delete def;
    } catch (JSONParseException e) {
      QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
                           tr("%1: %2")
                           .arg(info->at("data")->at(i)->asString(),
                                e.reason), QMessageBox::Cancel);
      delete info;
      zip.close();
      return;
    }
  }

  QString key = info->at("name")->asString() + info->at("type")->asString();
  delete info;
  QString dest = destdir + "/" + QString("%1").arg(qHash(key,42)) + ".zip";
  if (!QFile::exists(dest) || overwrite) {
    if (QFile::exists(dest) && install)
      removeDefinition(dest);
    if (!QFile::copy(path, dest)) {
      QMessageBox::warning(this,
                           tr("Couldn't install %1").arg(path),
                           tr("Copy error"),
                           QMessageBox::Cancel);
      return;
    }
    sorted.prepend(dest);
    if (install)
      loadDefinition(dest);
  }
}
void DefinitionManager::removePack() {
  // find selected pack
  if (definitions.contains(selected)) {
    int ret = QMessageBox::question(this, tr("Delete Pack"),
                                    tr("Are you sure you want to delete %1?")
                                    .arg(definitions[selected].name),
                                    QMessageBox::Yes | QMessageBox::No,
                                    QMessageBox::No);
    if (ret == QMessageBox::Yes)
      removeDefinition(selected);
  }
}
void DefinitionManager::installJson(QString path,bool overwrite,bool install)
{
	QString destdir=QStandardPaths::writableLocation(QStandardPaths::DataLocation);

	JSONData *def;
	QFile f(path);
	f.open(QIODevice::ReadOnly);
	try {
		def=JSON::parse(f.readAll());
		f.close();
	} catch (JSONParseException e) {
		f.close();
		QMessageBox::warning(this,
							 tr("Couldn't install %1").arg(path),
							 e.reason,
							 QMessageBox::Cancel);
		return;
	}

	QString key=def->at("name")->asString()+def->at("type")->asString();
	delete def;
	QString dest=destdir+"/"+QString("%1").arg(qHash(key))+".json";
	if (!QFile::exists(dest) || overwrite)
	{
		if (QFile::exists(dest) && install)
			removeDefinition(dest);
		if (!QFile::copy(path,dest))
		{
			QMessageBox::warning(this,
								 tr("Couldn't install %1").arg(path),
								 tr("Copy error"),
								 QMessageBox::Cancel);
			return;
		}
		// fix permissions since we might be copying a readonly resource.
		QFile::setPermissions(dest, QFile::ReadOwner|QFile::WriteOwner);
		sorted.prepend(dest);
		if (install)
			loadDefinition(dest);
	}
}
void DefinitionManager::installJson(QString path, bool overwrite,
                                    bool install) {
  QString destdir = QStandardPaths::writableLocation(
      QStandardPaths::DataLocation);

  JSONData *def;
  QFile f(path);
  f.open(QIODevice::ReadOnly);
  try {
    def = JSON::parse(f.readAll());
    f.close();
  } catch (JSONParseException e) {
    f.close();
    QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
                         e.reason,
                         QMessageBox::Cancel);
    return;
  }

  QString key = def->at("name")->asString() + def->at("type")->asString();
  QString exeversion = def->at("version")->asString();
  delete def;
  QString dest = destdir + "/" + QString("%1").arg(qHash(key,42)) + ".json";

  // check if build in version is newer than version on disk
  if (QFile::exists(dest)) {
    QFile f(dest);
    f.open(QIODevice::ReadOnly);
    try {
      def = JSON::parse(f.readAll());
      f.close();
    } catch (JSONParseException e) {
      f.close();
      return;
    }
    QString fileversion = def->at("version")->asString();
    delete def;
    if (exeversion.compare(fileversion, Qt::CaseInsensitive) > 0) {
      // force overwriting outdated local copy
      QFile::remove(dest);
      QFile::copy(path, dest);
      QFile::setPermissions(dest, QFile::ReadOwner|QFile::WriteOwner);
    }
  }

  // import new definition (if file is not present)
  if (!QFile::exists(dest) || overwrite) {
    if (QFile::exists(dest) && install)
      removeDefinition(dest);
    if (!QFile::copy(path, dest)) {
      QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
                           tr("Copy error"),
                           QMessageBox::Cancel);
      return;
    }
    // fix permissions since we might be copying a readonly resource.
    QFile::setPermissions(dest, QFile::ReadOwner|QFile::WriteOwner);
    sorted.prepend(dest);
    if (install)
      loadDefinition(dest);
  }
}