bool Document::rename() { if (m_filename.isEmpty()) { return false; } QString selected; QString filename = QFileDialog::getSaveFileName(window(), tr("Rename File"), m_filename, fileFilter(m_filename), &selected); if (!filename.isEmpty()) { filename = fileNameWithExtension(filename, selected); if (QFile::exists(filename) && !QFile::remove(filename)) { QMessageBox::critical(window(), tr("Sorry"), tr("Unable to overwrite '%1'.").arg(filename)); return false; } if (!QFile::rename(m_filename, filename)) { QMessageBox::critical(window(), tr("Sorry"), tr("Unable to rename '%1'.").arg(m_filename)); return false; } m_filename = filename; updateSaveLocation(); m_text->document()->setModified(false); emit changedName(); return true; } else { return false; } }
bool Document::saveAs() { // Request new filename QString filename = getSaveFileName(tr("Save File As")); if (filename.isEmpty()) { return false; } if (m_filename == filename) { return save(); } // Save file as new name if (QFile::exists(filename) && (DocumentWatcher::instance()->isWatching(filename) || !QFile::remove(filename))) { QMessageBox::critical(window(), tr("Sorry"), tr("Unable to overwrite '%1'.").arg(QDir::toNativeSeparators(filename))); return false; } QByteArray encoding; std::swap(m_filename, filename); std::swap(m_encoding, encoding); if (!save()) { std::swap(m_filename, filename); std::swap(m_encoding, encoding); return false; } clearIndex(); updateSaveLocation(); m_text->setReadOnly(false); m_text->document()->setModified(false); emit changedName(); return true; }
bool Document::rename() { // Request new filename if (m_filename.isEmpty()) { return false; } QString filename = getSaveFileName(tr("Rename File")); if (filename.isEmpty()) { return false; } // Rename file if (QFile::exists(filename) && (DocumentWatcher::instance()->isWatching(filename) || !QFile::remove(filename))) { QMessageBox::critical(window(), tr("Sorry"), tr("Unable to overwrite '%1'.").arg(QDir::toNativeSeparators(filename))); return false; } DocumentWatcher::instance()->pauseWatch(this); if (!QFile::rename(m_filename, filename)) { DocumentWatcher::instance()->resumeWatch(this); QMessageBox::critical(window(), tr("Sorry"), tr("Unable to rename '%1'.").arg(QDir::toNativeSeparators(m_filename))); return false; } DocumentWatcher::instance()->resumeWatch(this); m_filename = filename; m_encoding.clear(); save(); updateSaveLocation(); m_text->document()->setModified(false); emit changedName(); return true; }
bool Document::saveAs() { QString selected; QString filename = QFileDialog::getSaveFileName(window(), tr("Save File As"), m_filename, fileFilter(m_filename), &selected); if (!filename.isEmpty()) { filename = fileNameWithExtension(filename, selected); if (QFile::exists(filename) && !QFile::remove(filename)) { QMessageBox::critical(window(), tr("Sorry"), tr("Unable to overwrite '%1'.").arg(QDir::toNativeSeparators(filename))); return false; } qSwap(m_filename, filename); if (!save()) { m_filename = filename; return false; } clearIndex(); updateSaveLocation(); m_text->setReadOnly(false); m_text->document()->setModified(false); emit changedName(); return true; } else { return false; } }