Example #1
0
void saveProperty( QListView *lv, KTNEFPropertySet *pSet, QWidget *parent )
{
	QListViewItem *item = lv->selectedItem();
	if ( !item )
		KMessageBox::error( parent, i18n( "Select an item." ) );
	else if ( item->text( 2 ).isEmpty() )
		KMessageBox::error( parent, i18n( "The selected item cannot be saved." ) );
	else
	{
		QString tag = item->text( 2 );
		int key = tag.mid( 5 ).toInt();
		QVariant prop = ( tag.startsWith( "attr_" ) ? pSet->attribute( key ) : pSet->property( key ) );
		QString filename = KFileDialog::getSaveFileName( tag, QString::null, parent );
		if ( !filename.isEmpty() )
		{
			QFile f( filename );
			if ( f.open( IO_WriteOnly ) )
			{
				switch ( prop.type() )
				{
					case QVariant::ByteArray:
						f.writeBlock( prop.asByteArray().data(), prop.asByteArray().size() );
						break;
					default:
						{
							QTextStream t( &f );
							t << prop.toString();
							break;
						}
				}
				f.close();
			}
			else
				KMessageBox::error( parent, i18n( "Unable to open file for writing, check file permissions." ) );
		}
	}
}
Example #2
0
uint64_t XMLPreferences::getPrefUInt64(const QString& inName, 
				       const QString& inSection, 
				       uint64_t def, 
				       Persistence pers)
{
  // try to retrieve the preference
  QVariant* preference = getPref(inName, inSection, pers);

  // if preference was retrieved, return it as a string
  if (preference != NULL)
  {
    uint64_t value = def;

    switch(preference->type())
    {
    case QVariant::String:
      // convert it to a uint64_t (in base 16)
      value = strtoull(preference->toString(), 0, 16);
      break;
    case QVariant::Int:
    case QVariant::UInt:
      value = preference->toInt();
      break;
    case QVariant::Double:
      value = uint64_t(preference->toDouble());
      break;
    case QVariant::ByteArray:
      {
	QByteArray& ba = preference->asByteArray();
	if (ba.size() == sizeof(uint64_t))
	  value = *(uint64_t*)ba.data();
	break;
      }
    default:
      qWarning("XMLPreferences::getPrefUInt64(%s, %s, %llu): preference found,\n"
	       "\tbut type %s is not convertable to type uint64_t!",
	       (const char*)inName, (const char*)inSection, 
	       (unsigned long long)def,
	       preference->typeName());
    }

    // return the key
    return value;
  }

  // return the default value
  return def;
}