bool ModList::saveListFile() { if (m_list_file.isNull() || m_list_file.isEmpty()) return false; QFile textFile(m_list_file); if (!textFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) return false; QTextStream textStream; textStream.setGenerateByteOrderMark(true); textStream.setCodec("UTF-8"); textStream.setDevice(&textFile); for (auto mod : mods) { textStream << mod.mmc_id(); if (!mod.enabled()) textStream << ".disabled"; textStream << endl; } textFile.close(); return false; }
bool TextBuffer::save (const QString &filename) { // codec must be set! Q_ASSERT (m_textCodec); /** * construct correct filter device and try to open */ QIODevice *file = KFilterDev::deviceForFile (filename, m_mimeTypeForFilterDev, false); if (!file->open (QIODevice::WriteOnly)) { delete file; return false; } /** * construct stream + disable Unicode headers */ QTextStream stream (file); stream.setCodec (QTextCodec::codecForName("UTF-16")); // set the correct codec stream.setCodec (m_textCodec); // generate byte order mark? stream.setGenerateByteOrderMark (generateByteOrderMark()); // our loved eol string ;) QString eol = "\n"; //m_doc->config()->eolString (); if (endOfLineMode() == eolDos) eol = QString ("\r\n"); else if (endOfLineMode() == eolMac) eol = QString ("\r"); // just dump the lines out ;) for (int i = 0; i < m_lines; ++i) { // get line to save Kate::TextLine textline = line (i); // strip trailing spaces if (m_removeTrailingSpaces) { int lastChar = textline->lastChar(); if (lastChar > -1) { stream << textline->text().left (lastChar+1); } } else // simple, dump the line stream << textline->text(); // append correct end of line string if ((i+1) < m_lines) stream << eol; } // flush stream stream.flush (); // close and delete file file->close (); delete file; #ifndef Q_OS_WIN // ensure that the file is written to disk // we crete new qfile, as the above might be wrapper around compression QFile syncFile (filename); syncFile.open (QIODevice::ReadOnly); #ifdef HAVE_FDATASYNC fdatasync (syncFile.handle()); #else fsync (syncFile.handle()); #endif #endif // did save work? bool ok = stream.status() == QTextStream::Ok; // remember this revision as last saved if we had success! if (ok) m_history.setLastSavedRevision (); // report CODEC + ERRORS kDebug (13020) << "Saved file " << filename << "with codec" << m_textCodec->name() << (ok ? "without" : "with") << "errors"; // emit signal on success if (ok) emit saved (filename); // return success or not return ok; }