bool Editor::saveFile(const QUrl &targetUrl) { QUrl url(targetUrl); bool result = false; if (url.isEmpty() && currentUrl().isEmpty()) { result = saveFileAs(); } else { if (url.isEmpty()) url = currentUrl(); QTemporaryFile tmp; // only used for network export tmp.setAutoRemove(false); tmp.open(); QString filename = url.isLocalFile() ? url.toLocalFile() : tmp.fileName(); QSaveFile *savefile = new QSaveFile(filename); if (savefile->open(QIODevice::WriteOnly)) { QTextStream outputStream(savefile); // store commands in their generic @(...) notation format, to be translatable when reopened // this allows sharing of scripts written in different languages Tokenizer tokenizer; tokenizer.initialize(editor->document()->toPlainText()); const QStringList localizedLooks(Translator::instance()->allLocalizedLooks()); QString unstranslated; Token* t; bool pendingEOL = false; // to avoid writing a final EOL token while ((t = tokenizer.getToken())->type() != Token::EndOfInput) { if (pendingEOL) { unstranslated.append('\n'); pendingEOL = false; } if (localizedLooks.contains(t->look())) { QString defaultLook(Translator::instance()->defaultLook(t->look())); unstranslated.append(QString("@(%1)").arg(defaultLook)); } else { if (t->type() == Token::EndOfLine) pendingEOL = true; else unstranslated.append(t->look()); } } outputStream << KTURTLE_MAGIC_1_0 << '\n'; outputStream << unstranslated; outputStream.flush(); savefile->commit(); // check for error here? } delete savefile; if (!url.isLocalFile()) { KIO::StoredTransferJob *job = KIO::storedPut(savefile, url, -1, 0); if(job->exec()){ setCurrentUrl(url); editor->document()->setModified(false); // MainWindow will add us to the recent file list emit fileSaved(url); result = true; // fix GUI for saveAs and saveExamples. TODO: check 5 lines above } } } return result; }
int ZCALLBACK qiodevice_close_file_func ( voidpf /*opaque UNUSED*/, voidpf stream) { QIODevice *device = reinterpret_cast<QIODevice*>(stream); #ifdef QUAZIP_QSAVEFILE_BUG_WORKAROUND // QSaveFile terribly breaks the is-a idiom: // it IS a QIODevice, but it is NOT compatible with it: close() is private QSaveFile *file = qobject_cast<QSaveFile*>(device); if (file != NULL) { // We have to call the ugly commit() instead: return file->commit() ? 0 : -1; } #endif device->close(); return 0; }
void KArchivePrivate::abortWriting() { if ( saveFile ) { saveFile->cancelWriting(); delete saveFile; saveFile = 0; dev = 0; } }
void PListConfigBackend::save(const QString &file, const QVariant &entry) { QDomImplementation domImpl; QDomDocumentType type = domImpl.createDocumentType(QLatin1String("plist"), QLatin1String("-//Apple Computer//DTD PLIST 1.0//EN"), QLatin1String("http://www.apple.com/DTDs/PropertyList-1.0.dtd") ); QDomDocument root(type); QDomElement plist = root.createElement("plist"); plist.setAttribute("version","1.0"); plist.appendChild(generateQDomElement(entry, root)); root.appendChild(plist); QSaveFile output (file); if (!output.open(QIODevice::WriteOnly)) { qWarning() << tr("Cannot write to file %1").arg(file); return; } QTextStream out(&output); static QTextCodec *utf8 = QTextCodec::codecForName("utf-8"); out.setCodec(utf8); out << QLatin1String("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); //hack root.save(out, 2, QDomNode::EncodingFromDocument); output.commit(); }
void TrackRecorder::exportGpx(QString name, QString desc, QString type) { qDebug()<<"Exporting track to gpx"; if(m_points.size() < 1) { qDebug()<<"Nothing to save"; return; // Nothing to save } QString homeDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); QString subDir = "Rena"; QString filename; if(!name.isEmpty()) { filename = m_points.at(0).timestamp().toUTC().toString(Qt::ISODate) + " - " + name + ".gpx"; } else { filename = m_points.at(0).timestamp().toUTC().toString(Qt::ISODate) + ".gpx"; } qDebug()<<"File:"<<homeDir<<"/"<<subDir<<"/"<<filename; QDir home = QDir(homeDir); if(!home.exists(subDir)) { qDebug()<<"Directory does not exist, creating"; if(home.mkdir(subDir)) { qDebug()<<"Directory created"; } else { qDebug()<<"Directory creation failed, aborting"; return; } } QSaveFile file; file.setFileName(homeDir + "/" + subDir + "/" + filename); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug()<<"File opening failed, aborting"; return; } QXmlStreamWriter xml; xml.setDevice(&file); xml.setAutoFormatting(true); // Human readable output xml.writeStartDocument(); xml.writeDefaultNamespace("http://www.topografix.com/GPX/1/1"); xml.writeStartElement("gpx"); xml.writeAttribute("version", "1.1"); xml.writeAttribute("Creator", "Rena for Sailfish"); // Now this is a bit redundant, but to be able to write the totals in the metadata section we do need to loop // through the whole pointset here... qreal duration = 0; qreal distance = 0; qreal avg_speed = 0; qreal max_speed = 0; QDateTime start_time(m_points.at(0).timestamp()); QDateTime stop_time(m_points.at(m_points.size()-1).timestamp()); duration = start_time.secsTo(stop_time); for(int i=1 ; i < m_points.size(); i++) { if(m_points.at(i-1).coordinate().type() != QGeoCoordinate::Coordinate3D) { break; // No position info, skip this point } QGeoCoordinate first(m_points.at(i-1).coordinate().latitude(), m_points.at(i-1).coordinate().longitude()); QGeoCoordinate second(m_points.at(i).coordinate().latitude(), m_points.at(i).coordinate().longitude()); distance += first.distanceTo(second); if(m_points.at(i).attribute(QGeoPositionInfo::GroundSpeed) > max_speed) { max_speed = m_points.at(i).attribute(QGeoPositionInfo::GroundSpeed); } } if(distance == 0 || duration == 0) { avg_speed = 0; } else { avg_speed = distance / duration; } xml.writeStartElement("metadata"); if(!name.isEmpty()) { xml.writeTextElement("name", name); } if(!desc.isEmpty()) { xml.writeTextElement("desc", desc); } if(!type.isEmpty()) { xml.writeTextElement("type", type); } xml.writeTextElement("duration", QString::number(duration)); xml.writeTextElement("distance", QString::number(distance)); xml.writeTextElement("avg_speed", QString::number(avg_speed)); xml.writeTextElement("max_speed", QString::number(max_speed)); xml.writeEndElement(); // metadata xml.writeStartElement("trk"); xml.writeStartElement("trkseg"); for(int i=0 ; i < m_points.size(); i++) { if(m_points.at(i).coordinate().type() == QGeoCoordinate::InvalidCoordinate) { break; // No position info, skip this point } xml.writeStartElement("trkpt"); xml.writeAttribute("lat", QString::number(m_points.at(i).coordinate().latitude(), 'g', 15)); xml.writeAttribute("lon", QString::number(m_points.at(i).coordinate().longitude(), 'g', 15)); xml.writeTextElement("time", m_points.at(i).timestamp().toUTC().toString(Qt::ISODate)); if(m_points.at(i).coordinate().type() == QGeoCoordinate::Coordinate3D) { xml.writeTextElement("ele", QString::number(m_points.at(i).coordinate().altitude(), 'g', 15)); } xml.writeStartElement("extensions"); if(m_points.at(i).hasAttribute(QGeoPositionInfo::Direction)) { xml.writeTextElement("dir", QString::number(m_points.at(i).attribute(QGeoPositionInfo::Direction), 'g', 15)); } if(m_points.at(i).hasAttribute(QGeoPositionInfo::GroundSpeed)) { xml.writeTextElement("g_spd", QString::number(m_points.at(i).attribute(QGeoPositionInfo::GroundSpeed), 'g', 15)); } if(m_points.at(i).hasAttribute(QGeoPositionInfo::VerticalSpeed)) { xml.writeTextElement("v_spd", QString::number(m_points.at(i).attribute(QGeoPositionInfo::VerticalSpeed), 'g', 15)); } if(m_points.at(i).hasAttribute(QGeoPositionInfo::MagneticVariation)) { xml.writeTextElement("m_var", QString::number(m_points.at(i).attribute(QGeoPositionInfo::MagneticVariation), 'g', 15)); } if(m_points.at(i).hasAttribute(QGeoPositionInfo::HorizontalAccuracy)) { xml.writeTextElement("h_acc", QString::number(m_points.at(i).attribute(QGeoPositionInfo::HorizontalAccuracy), 'g', 15)); } if(m_points.at(i).hasAttribute(QGeoPositionInfo::VerticalAccuracy)) { xml.writeTextElement("v_acc", QString::number(m_points.at(i).attribute(QGeoPositionInfo::VerticalAccuracy), 'g', 15)); } xml.writeEndElement(); // extensions xml.writeEndElement(); // trkpt } xml.writeEndElement(); // trkseg xml.writeEndElement(); // trk xml.writeEndElement(); // gpx xml.writeEndDocument(); file.commit(); if(file.error()) { qDebug()<<"Error in writing to a file"; qDebug()<<file.errorString(); } else { QDir renaDir = QDir(homeDir + "/" + subDir); renaDir.remove("Autosave"); } }