Example #1
0
bool loadDroidTemplates(const char *filename)
{
	WzConfig ini(filename, WzConfig::ReadOnlyAndRequired);
	QStringList list = ini.childGroups();
	for (int i = 0; i < list.size(); ++i)
	{
		ini.beginGroup(list[i]);
		DROID_TEMPLATE design = loadTemplateCommon(ini);
		design.id = list[i];
		design.name = ini.value("name").toString();
		design.multiPlayerID = generateNewObjectId();
		design.prefab = true;
		design.stored = false;
		design.enabled = true;
		bool available = ini.value("available", false).toBool();
		char const *droidResourceName = getDroidResourceName(list[i].toUtf8().constData());
		design.name = droidResourceName != NULL ? droidResourceName : GetDefaultTemplateName(&design);
		ini.endGroup();

		for (int i = 0; i < MAX_PLAYERS; ++i)
		{
			// Give those meant for humans to all human players.
			if (NetPlay.players[i].allocated && available)
			{
				design.prefab = false;
				copyTemplate(i, &design);

				// This sets up the UI templates for display purposes ONLY--we still only use droidTemplates for making them.
				// FIXME: Why are we doing this here, and not on demand ?
				// Only add unique designs to the UI list (Note, perhaps better to use std::map instead?)
				std::list<DROID_TEMPLATE>::iterator it;
				for (it = localTemplates.begin(); it != localTemplates.end(); ++it)
				{
					DROID_TEMPLATE *psCurr = &*it;
					if (psCurr->multiPlayerID == design.multiPlayerID)
					{
						debug(LOG_ERROR, "Design id:%d (%s) *NOT* added to UI list (duplicate), player= %d", design.multiPlayerID, getName(&design), i);
						break;
					}
				}
				if (it == localTemplates.end())
				{
					debug(LOG_NEVER, "Design id:%d (%s) added to UI list, player =%d", design.multiPlayerID, getName(&design), i);
					localTemplates.push_back(design);
				}
			}
			else if (!NetPlay.players[i].allocated)	// AI template
			{
				design.prefab = true;  // prefabricated templates referenced from VLOs
				copyTemplate(i, &design);
			}
		}
		debug(LOG_NEVER, "Droid template found, Name: %s, MP ID: %d, ref: %u, ID: %s, prefab: %s, type:%d (loading)",
		      getName(&design), design.multiPlayerID, design.ref, getID(&design), design.prefab ? "yes" : "no", design.droidType);
	}

	return true;
}
Example #2
0
//load the desktop file or the required template
void Dialog::LoadDesktopFile(QString input)
{
  //if we have "-" as 1st char, it means that this is not a desktop file, but a parameter
  desktopFileName = input;
  if (input.startsWith("-")) {
    QMessageBox::critical(this,tr("Error"),tr("The filename cannot start with a \"-\"."));
    exit(1);
  }
  //if proposed file does not exist, than we will create one based on the templates
  QFileInfo info(desktopFileName);
  if ((info.size() == 0) && (desktopFileName.endsWith(".desktop"))) {
    QFile::remove(desktopFileName); //for the copy, we need to remove it
    if (desktopType=="link") {
      copyTemplate("-link");
    } else {
      copyTemplate("-app");
    }
  }
  this->setWindowTitle(desktopFileName.section("/",-1));
  ui->tabWidget->setCurrentIndex(0); //always start on the file info tab
  //Now load the file info and put it into the UI
  QString mime = LXDG::findAppMimeForFile(desktopFileName);
  
  QList<QByteArray> fmt = QImageReader::supportedImageFormats();
  bool foundimage=false;
    for(int i=0; i<fmt.length(); i++){ 
      if(info.suffix().toLower() == QString(fmt[i]).toLower()){foundimage=true; break; }
    }
  if(foundimage){
    ui->label_file_icon->setPixmap( QPixmap(desktopFileName).scaledToHeight(64) );
  }else{
    ui->label_file_icon->setPixmap( LXDG::findMimeIcon(mime).pixmap(QSize(64,64)) );	  
  }
  ui->label_file_mimetype->setText(mime);
  QString type;
  if(desktopFileName.endsWith(".desktop")){ type = tr("XDG Shortcut"); }
  else if(info.isDir()){ type = tr("Directory"); ui->label_file_icon->setPixmap( LXDG::findIcon("folder","").pixmap(QSize(64,64)) ); }
  else if(info.isExecutable()){ type = tr("Binary"); }
  else{ type = info.suffix().toUpper(); }
  if(info.isHidden()){ type = QString(tr("Hidden %1")).arg(type); }
  ui->label_file_type->setText(type);
  double bytes = info.size();
  QStringList lab; lab << "B" << "KB" << "MB" << "GB" << "TB" << "PB";
  int i=0;
  while(i<lab.length() && bytes>1024){
    bytes = bytes/1024;
    i++; //next label
  }
  //convert the size to two decimel places and add the label
  QString sz = QString::number( qRound(bytes*100)/100.0 )+lab[i];
  ui->label_file_size->setText( sz );
  ui->label_file_owner->setText(info.owner());
  ui->label_file_group->setText(info.group());
  QString perms;
  if(info.isReadable() && info.isWritable()){ perms = tr("Read/Write"); }
  else if(info.isReadable()){ perms = tr("Read Only"); }
  else if(info.isWritable()){ perms = tr("Write Only"); }
  else{ perms = tr("No Access"); }
  ui->label_file_perms->setText(perms);
  ui->label_file_created->setText( info.created().toString(Qt::SystemLocaleLongDate) );
  ui->label_file_modified->setText( info.lastModified().toString(Qt::SystemLocaleLongDate) );
  
  //use the standard LXDG object and load the desktop file
  bool ok = false;
  if(desktopFileName.endsWith(".desktop")){
    DF = LXDG::loadDesktopFile(desktopFileName, ok);
  }
  if( ok ) {
    if ((DF.type == XDGDesktop::LINK) && (desktopType!="link" )) {
      //we open a desktop type "link" but it was not mentionned by parameters
        Dialog::Initialise("-link");
    }
    ui->lName->setText(DF.name);
    ui->lComment->setText(DF.comment);
    ui->lCommand->setText(DF.exec);
    //in case of "link" desktop, we populate the correct content in lWorkingDir
    if (desktopType=="link") {
      ui->lWorkingDir->setText(DF.url);
    } else {
      ui->lWorkingDir->setText(DF.path);
    }
    if (DF.startupNotify) ui->cbStartupNotification->setChecked(true); else ui->cbStartupNotification->setChecked(false);
    if (DF.useTerminal) ui->cbRunInTerminal->setChecked(true); else ui->cbRunInTerminal->setChecked(false);
    iconFileName="";
    ui->pbIcon->setIcon(LXDG::findIcon(DF.icon,""));
    if(!info.isWritable()){ ui->tab_deskedit->setEnabled(false); }
  } else {
    ui->tabWidget->removeTab(1);
    return;
  }

  //we load the file in memory and will adapt it before saving it to disk
  QFile file(desktopFileName);
  inMemoryFile="";
  if (file.open(QFile::ReadOnly)) {
    QTextStream fileData(&file);
    inMemoryFile = fileData.readAll();
    file.close();
    //perform some validation checks
    //this will allow checks improvements without compilation of the file
    if ((inMemoryFile.contains(QRegExp(".*\\[Desktop Entry\\].*\n"))) &&
      (inMemoryFile.contains(QRegExp("\n\\s*Type\\s*=.*\n"))) &&
      (inMemoryFile.contains(QRegExp("\n\\s*Name\\s*=.*\n")))) {
      //qDebug() << "sounds a good file"; 
    } else {
      //qDebug() << "wrong file!!!!";
      QMessageBox msgBox;
      msgBox.setIcon(QMessageBox::Question);
      msgBox.setText(tr("There are some issues with this file !!!!"));
      msgBox.setInformativeText(tr("Either you correct this file your self with an editor, or you start from scratch using the link or app template.\nPlease note that this process will update the file called:") + desktopFileName);
      QPushButton *linkButton = msgBox.addButton("Link",QMessageBox::AcceptRole);
      QPushButton *appButton = msgBox.addButton("App",QMessageBox::ResetRole);
      QPushButton *cancelButton = msgBox.addButton("Cancel",QMessageBox::NoRole);
      msgBox.exec();
      if (msgBox.clickedButton() == linkButton) {
        QFile::remove(desktopFileName);
        copyTemplate("-link");
        Initialise("-link");
        LoadDesktopFile(desktopFileName);
      }
      if (msgBox.clickedButton() == appButton) {
        QFile::remove(desktopFileName);
        copyTemplate("-app");
        Initialise("-app");
        LoadDesktopFile(desktopFileName);
      }
      if (msgBox.clickedButton() == cancelButton) {
        //we stop here
        exit(0);
      }
    }
  }
}
Example #3
0
bool initTemplates()
{
	WzConfig ini("userdata/" + QString(rulesettag) + "/templates.json", WzConfig::ReadOnly);
	if (!ini.status())
	{
		debug(LOG_WZ, "Could not open %s", ini.fileName().toUtf8().constData());
		return false;
	}
	int version = ini.value("version", 0).toInt();
	if (version == 0)
	{
		return true; // too old version
	}
	ini.beginArray("templates");
	while (ini.remainingArrayItems())
	{
		DROID_TEMPLATE design = loadTemplateCommon(ini);
		design.multiPlayerID = generateNewObjectId();
		design.prefab = false;		// not AI template
		design.stored = true;
		if (!(asBodyStats + design.asParts[COMP_BODY])->designable
		    || !(asPropulsionStats + design.asParts[COMP_PROPULSION])->designable
		    || (design.asParts[COMP_BRAIN] > 0 && !(asBrainStats + design.asParts[COMP_BRAIN])->designable)
		    || (design.asParts[COMP_REPAIRUNIT] > 0 && !(asRepairStats + design.asParts[COMP_REPAIRUNIT])->designable)
		    || (design.asParts[COMP_ECM] > 0 && !(asECMStats + design.asParts[COMP_ECM])->designable)
		    || (design.asParts[COMP_SENSOR] > 0 && !(asSensorStats + design.asParts[COMP_SENSOR])->designable)
		    || (design.asParts[COMP_CONSTRUCT] > 0 && !(asConstructStats + design.asParts[COMP_CONSTRUCT])->designable)
		    || (design.numWeaps > 0 && !(asWeaponStats + design.asWeaps[0])->designable)
		    || (design.numWeaps > 1 && !(asWeaponStats + design.asWeaps[1])->designable)
		    || (design.numWeaps > 2 && !(asWeaponStats + design.asWeaps[2])->designable))
		{
			debug(LOG_ERROR, "Template %s from stored templates cannot be designed", design.name.toUtf8().constData());
			ini.nextArrayItem();
			continue;
		}
		bool valid = intValidTemplate(&design, ini.value("name").toString().toUtf8().constData(), false, selectedPlayer);
		if (!valid)
		{
			debug(LOG_ERROR, "Invalid template %s from stored templates", design.name.toUtf8().constData());
			ini.nextArrayItem();
			continue;
		}
		DROID_TEMPLATE *psDestTemplate = NULL;
		for (auto &keyvaluepair : droidTemplates[selectedPlayer])
		{
			psDestTemplate = keyvaluepair.second;
			// Check if template is identical to a loaded template
			if (psDestTemplate->droidType == design.droidType
			    && psDestTemplate->name.compare(design.name) == 0
			    && psDestTemplate->numWeaps == design.numWeaps
			    && psDestTemplate->asWeaps[0] == design.asWeaps[0]
			    && psDestTemplate->asWeaps[1] == design.asWeaps[1]
			    && psDestTemplate->asWeaps[2] == design.asWeaps[2]
			    && psDestTemplate->asParts[COMP_BODY] == design.asParts[COMP_BODY]
			    && psDestTemplate->asParts[COMP_PROPULSION] == design.asParts[COMP_PROPULSION]
			    && psDestTemplate->asParts[COMP_REPAIRUNIT] == design.asParts[COMP_REPAIRUNIT]
			    && psDestTemplate->asParts[COMP_ECM] == design.asParts[COMP_ECM]
			    && psDestTemplate->asParts[COMP_SENSOR] == design.asParts[COMP_SENSOR]
			    && psDestTemplate->asParts[COMP_CONSTRUCT] == design.asParts[COMP_CONSTRUCT]
			    && psDestTemplate->asParts[COMP_BRAIN] == design.asParts[COMP_BRAIN])
			{
				break;
			}
			psDestTemplate = NULL;
		}
		if (psDestTemplate)
		{
			psDestTemplate->stored = true; // assimilate it
			ini.nextArrayItem();
			continue; // next!
		}
		design.enabled = allowDesign;
		copyTemplate(selectedPlayer, &design);
		localTemplates.push_back(design);
		ini.nextArrayItem();
	}
	ini.endArray();
	return true;
}