bool Score::saveCompressedFile(QFileInfo& info, bool onlySelection) { QFile fp(info.filePath()); if (!fp.open(QIODevice::WriteOnly)) { MScore::lastError = tr("Open File\n%1\nfailed: %2").arg(info.filePath(), strerror(errno)); return false; } return saveCompressedFile(&fp, info, onlySelection); }
void Score::saveCompressedFile(QFileInfo& info, bool onlySelection) { QFile fp(info.filePath()); if (!fp.open(QIODevice::WriteOnly)) { QString s = tr("Open File\n%1\nfailed: ") + QString(strerror(errno)); throw(s.arg(info.filePath())); } saveCompressedFile(&fp, info, onlySelection); fp.close(); }
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; }