void XmlWriter::write( const Account *acc, const QString &filename ) { Q_ASSERT( acc ); if( filename.isEmpty() ) { qDebug() << Q_FUNC_INFO << ':' << __LINE__ << " - Filename is empty."; throw StorageFileException( QT_TR_NOOP( "The file given could not be written; check whether " "it exists or is writeable for the current user." ) ); } QFile file( filename ); if( !file.open( QFile::WriteOnly ) ) { qDebug() << Q_FUNC_INFO << ':' << __LINE__ << " - File" << filename << "could not be opened."; throw StorageFileException( QT_TR_NOOP( "The file given could not be written; check whether " "it exists or is writeable for the current user." ) ); } delete d; d = new XmlWriter::Private; QByteArray byteArray; QBuffer buffer( &byteArray ); buffer.open( QIODevice::WriteOnly ); QXmlStreamWriter stream( &buffer ); stream.setAutoFormatting( true ); stream.setAutoFormattingIndent( 4 ); stream.writeStartDocument( "1.0" ); stream.writeStartElement( "knipptasch" ); stream.writeAttribute( "version", "1.2" ); stream.writeAttribute( "href", "http://projects.kde.org/knipptasch" ); writeAccount( stream, acc ); stream.writeEndElement(); // knipptasch stream.writeEndDocument(); buffer.close(); //TODO write to a temporary file first or make a backup or... qint64 size = file.write( byteArray ); if( size <= 0 || size != byteArray.size() ) { qDebug() << Q_FUNC_INFO << ':' << __LINE__ << " - File" << filename << "could not be written (" << size << "out of" << byteArray.size() << ")."; throw StorageFileException( QT_TR_NOOP( "The file given could not be written; check whether " "it exists or is writeable for the current user." ) ); } file.close(); }
void MyMoneyStorageXML::writeAccounts(QDomElement& accounts) { QList<MyMoneyAccount> list; m_storage->accountList(list); QList<MyMoneyAccount>::ConstIterator it; accounts.setAttribute("count", list.count() + 5); writeAccount(accounts, m_storage->asset()); writeAccount(accounts, m_storage->liability()); writeAccount(accounts, m_storage->expense()); writeAccount(accounts, m_storage->income()); writeAccount(accounts, m_storage->equity()); signalProgress(0, list.count(), i18n("Saving accounts...")); int i = 0; for (it = list.constBegin(); it != list.constEnd(); ++it) { writeAccount(accounts, *it); signalProgress(++i, 0); } }