bool AIMLParser::saveVars(const QString &filename) { QDomDocument doc; QDomElement root = doc.createElement("vars"); doc.appendChild(root); QMap<QString, QString>::ConstIterator it; for ( it = _parameterValue.begin(); it != _parameterValue.end(); ++it ) { QDomElement setElem = doc.createElement("set"); setElem.setAttribute("name", it.key()); QDomText text = doc.createTextNode(it.data()); setElem.appendChild(text); root.appendChild(setElem); } //Backup the file first QFile fileBackup( filename + ".bak" ); if ( !fileBackup.open( IO_WriteOnly ) ) return false; QTextStream tsBackup(&fileBackup); QFile file( filename ); if ( !file.open( IO_ReadOnly ) ) return false; tsBackup << QString(file.readAll()); fileBackup.close(); file.close(); //now, save it! if ( !file.open( IO_WriteOnly ) ) return false; QTextStream ts(&file); ts << doc.toString(); file.close(); return true; }
bool MasterScore::saveFile() { QString suffix = info.suffix(); if (info.exists() && !info.isWritable()) { MScore::lastError = tr("The following file is locked: \n%1 \n\nTry saving to a different location.").arg(info.filePath()); return false; } // // step 1 // save into temporary file to prevent partially overwriting // the original file in case of "disc full" // QString tempName = info.filePath() + QString(".temp"); QFile temp(tempName); if (!temp.open(QIODevice::WriteOnly)) { MScore::lastError = tr("Open Temp File\n%1\nfailed: %2").arg(tempName, strerror(errno)); return false; } bool rv = suffix == "mscx" ? Score::saveFile(&temp, false) : Score::saveCompressedFile(&temp, info, false); if (!rv) { return false; } if (temp.error() != QFile::NoError) { MScore::lastError = tr("Save File failed: %1").arg(temp.errorString()); return false; } temp.close(); QString name(info.filePath()); QString basename(info.fileName()); QDir dir(info.path()); if (!saved()) { // if file was already saved in this session // save but don't overwrite backup again // // step 2 // remove old backup file if exists // QString backupName = QString(".") + info.fileName() + QString(","); if (dir.exists(backupName)) { if (!dir.remove(backupName)) { // if (!MScore::noGui) // QMessageBox::critical(0, QObject::tr("Save File"), // tr("Removing old backup file %1 failed").arg(backupName)); } } // // step 3 // rename old file into backup // if (dir.exists(basename)) { if (!dir.rename(basename, backupName)) { // if (!MScore::noGui) // QMessageBox::critical(0, tr("Save File"), // tr("Renaming old file <%1> to backup <%2> failed").arg(name, backupname); } } #ifdef Q_OS_WIN QFileInfo fileBackup(dir, backupName); QString backupNativePath = QDir::toNativeSeparators(fileBackup.absoluteFilePath()); #if (defined (_MSCVER) || defined (_MSC_VER)) #if (defined (UNICODE)) SetFileAttributes((LPCTSTR)backupNativePath.unicode(), FILE_ATTRIBUTE_HIDDEN); #else // Use byte-based Windows function SetFileAttributes((LPCTSTR)backupNativePath.toLocal8Bit(), FILE_ATTRIBUTE_HIDDEN); #endif #else SetFileAttributes((LPCTSTR)backupNativePath.toLocal8Bit(), FILE_ATTRIBUTE_HIDDEN); #endif #endif } else { // file has previously been saved - remove the old file if (dir.exists(basename)) { if (!dir.remove(basename)) { // if (!MScore::noGui) // QMessageBox::critical(0, tr("Save File"), // tr("Removing old file %1 failed").arg(name)); } } } // // step 4 // rename temp name into file name // if (!QFile::rename(tempName, name)) { MScore::lastError = tr("Renaming temp. file <%1> to <%2> failed:\n%3").arg(tempName, name, strerror(errno)); return false; } // make file readable by all QFile::setPermissions(name, QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther); undoStack()->setClean(); setSaved(true); info.refresh(); update(); return true; }
bool Score::saveFile() { QString suffix = info.suffix(); if (info.exists() && !info.isWritable()) { MScore::lastError = tr("The following file is locked: \n%1 \n\nTry saving to a different location.").arg(info.filePath()); return false; } // if file was already saved in this session // save but don't overwrite backup again if (saved()) { try { if (suffix == "mscx") saveFile(info); else saveCompressedFile(info, false); } catch (QString s) { MScore::lastError = s; return false; } undo()->setClean(); info.refresh(); update(); return true; } // // step 1 // save into temporary file to prevent partially overwriting // the original file in case of "disc full" // QString tempName = info.filePath() + QString(".temp"); QFile temp(tempName); if (!temp.open(QIODevice::WriteOnly)) { MScore::lastError = tr("Open Temp File\n%1\nfailed: %2").arg(tempName).arg(QString(strerror(errno))); return false; } try { if (suffix == "mscx") saveFile(&temp, false); else saveCompressedFile(&temp, info, false); } catch (QString s) { MScore::lastError = s; return false; } if (temp.error() != QFile::NoError) { MScore::lastError = tr("MuseScore: Save File failed: %1").arg(temp.errorString()); temp.close(); return false; } temp.close(); // // step 2 // remove old backup file if exists // QDir dir(info.path()); QString backupName = QString(".") + info.fileName() + QString(","); if (dir.exists(backupName)) { if (!dir.remove(backupName)) { // if (!MScore::noGui) // QMessageBox::critical(0, tr("MuseScore: Save File"), // tr("Removing old backup file ") + backupName + tr(" failed")); } } // // step 3 // rename old file into backup // QString name(info.filePath()); if (dir.exists(name)) { if (!dir.rename(name, backupName)) { // if (!MScore::noGui) // QMessageBox::critical(0, tr("MuseScore: Save File"), // tr("Renaming old file <") // + name + tr("> to backup <") + backupName + tr("> failed")); } } #ifdef Q_OS_WIN QFileInfo fileBackup(dir, backupName); QString backupNativePath = QDir::toNativeSeparators(fileBackup.absoluteFilePath()); SetFileAttributes((LPCTSTR)backupNativePath.toLocal8Bit(), FILE_ATTRIBUTE_HIDDEN); #endif // // step 4 // rename temp name into file name // if (!QFile::rename(tempName, name)) { MScore::lastError = tr("Renaming temp. file <%1> to <%2> failed:\n%3").arg(tempName).arg(name).arg(QString(strerror(errno))); return false; } // make file readable by all QFile::setPermissions(name, QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther); undo()->setClean(); setSaved(true); info.refresh(); update(); return true; }