static bool onTransition(HangulInputContext* /* hic */, ucschar /* c */, const ucschar *str, void* /* data */) { QTextCodec *codec = QTextCodec::codecForLocale(); QString s = ucsToQString(str); return codec->canEncode(s); }
void MainWindow::aboutToShowSaveAsMenu() { QString currentText = textEdit->toPlainText(); foreach (QAction *action, saveAsActs) { QByteArray codecName = action->data().toByteArray(); QTextCodec *codec = QTextCodec::codecForName(codecName); action->setVisible(codec && codec->canEncode(currentText)); }
bool ParserM3u::writeM3UFile(const QString &file_str, QList<QString> &items, bool useRelativePath, bool useUtf8) { // Important note: // On Windows \n will produce a <CR><CL> (=\r\n) // On Linux and OS X \n is <CR> (which remains \n) QTextCodec* codec; if (useUtf8) { codec = QTextCodec::codecForName("UTF-8"); } else { // according to http://en.wikipedia.org/wiki/M3U the default encoding of m3u is Windows-1252 // see also http://tools.ietf.org/html/draft-pantos-http-live-streaming-07 // check if the all items can be properly encoded to Latin1. codec = QTextCodec::codecForName("windows-1252"); for (int i = 0; i < items.size(); ++i) { if (!codec->canEncode(items.at(i))) { // filepath contains incompatible character QMessageBox::warning(NULL,tr("Playlist Export Failed"), tr("File path contains characters, not allowed in m3u playlists.\n") + tr("Export a m3u8 playlist instead!\n") + items.at(i)); return false; } } } QFile file(file_str); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { QMessageBox::warning(NULL,tr("Playlist Export Failed"), tr("Could not create file") + " " + file_str); return false; } // Base folder of file QString base = file_str.section('/', 0, -2); QDir base_dir(base); qDebug() << "Basepath: " << base; QTextStream out(&file); out.setCodec(codec); out << "#EXTM3U\n"; for (int i = 0; i < items.size(); ++i) { out << "#EXTINF\n"; // Write relative path if possible if (useRelativePath) { //QDir::relativePath() will return the absolutePath if it cannot compute the //relative Path out << base_dir.relativeFilePath(items.at(i)) << "\n"; } else { out << items.at(i) << "\n"; } } return true; }
void PluginList::writePlugins(const QString &fileName, bool writeUnchecked) const { SafeWriteFile file(fileName); QTextCodec *textCodec = writeUnchecked ? m_Utf8Codec : m_LocalCodec; file->resize(0); file->write(textCodec->fromUnicode("# This file was automatically generated by Mod Organizer.\r\n")); QStringList saveList; bool invalidFileNames = false; int writtenCount = 0; for (size_t i = 0; i < m_ESPs.size(); ++i) { int priority = m_ESPsByPriority[i]; if (m_ESPs[priority].m_Enabled || writeUnchecked) { //file.write(m_ESPs[priority].m_Name.toUtf8()); if (!textCodec->canEncode(m_ESPs[priority].m_Name)) { invalidFileNames = true; qCritical("invalid plugin name %s", m_ESPs[priority].m_Name.toUtf8().constData()); } else { saveList << m_ESPs[priority].m_Name; file->write(textCodec->fromUnicode(m_ESPs[priority].m_Name)); } file->write("\r\n"); ++writtenCount; } } if (invalidFileNames) { reportError(tr("Some of your plugins have invalid names! These plugins can not be loaded by the game. " "Please see mo_interface.log for a list of affected plugins and rename them.")); } if (writtenCount == 0) { qWarning("plugin list would be empty, this is almost certainly wrong. Not saving."); } else { if (file.commitIfDifferent(m_LastSaveHash[fileName])) { qDebug("%s saved", QDir::toNativeSeparators(fileName).toUtf8().constData()); } } }
/** @brief Import feeds from OPML-file * * Calls open file system dialog with filter *.opml. * Adds all feeds to DB include hierarchy, ignore duplicate feeds *---------------------------------------------------------------------------*/ void UpdateObject::slotImportFeeds(QByteArray xmlData) { int elementCount = 0; int outlineCount = 0; QSqlQuery q(db_); QList<int> idsList; QList<QString> urlsList; QXmlStreamReader xml; QString convertData; bool codecOk = false; QzRegExp rx("&(?!([a-z0-9#]+;))", Qt::CaseInsensitive); int pos = 0; while ((pos = rx.indexIn(QString::fromLatin1(xmlData), pos)) != -1) { xmlData.replace(pos, 1, "&"); pos += 1; } rx.setPattern("encoding=\"([^\"]+)"); pos = rx.indexIn(xmlData); if (pos == -1) { rx.setPattern("encoding='([^']+)"); pos = rx.indexIn(xmlData); } if (pos == -1) { QStringList codecNameList; codecNameList << "UTF-8" << "Windows-1251" << "KOI8-R" << "KOI8-U" << "ISO 8859-5" << "IBM 866"; foreach (QString codecNameT, codecNameList) { QTextCodec *codec = QTextCodec::codecForName(codecNameT.toUtf8()); if (codec && codec->canEncode(xmlData)) { convertData = codec->toUnicode(xmlData); codecOk = true; break; } }
/** @brief Parse xml-data *----------------------------------------------------------------------------*/ void ParseObject::slotParse(const QByteArray &xmlData, const int &feedId, const QDateTime &dtReply, const QString &codecName) { if (mainApp->isSaveDataLastFeed()) { QFile file(mainApp->dataDir() + "/lastfeed.dat"); file.open(QIODevice::WriteOnly); file.write(xmlData); file.close(); } qDebug() << "=================== parseXml:start ============================"; db_.transaction(); // extract feed id and duplicate news mode from feed table parseFeedId_ = feedId; QString feedUrl; duplicateNewsMode_ = false; QSqlQuery q(db_); q.setForwardOnly(true); q.exec(QString("SELECT duplicateNewsMode, xmlUrl FROM feeds WHERE id=='%1'").arg(parseFeedId_)); if (q.first()) { duplicateNewsMode_ = q.value(0).toBool(); feedUrl = q.value(1).toString(); } // id not found (ex. feed deleted while updating) if (feedUrl.isEmpty()) { qWarning() << QString("Feed with id = '%1' not found").arg(parseFeedId_); emit signalFinishUpdate(parseFeedId_, false, 0, "0"); db_.commit(); return; } qDebug() << QString("Feed '%1' found with id = %2").arg(feedUrl).arg(parseFeedId_); // actually parsing feedChanged_ = false; bool codecOk = false; QString convertData(xmlData); QString feedType; QDomDocument doc; QString errorStr; int errorLine; int errorColumn; QRegExp rx("encoding=\"([^\"]+)", Qt::CaseInsensitive, QRegExp::RegExp2); int pos = rx.indexIn(xmlData); if (pos == -1) { rx.setPattern("encoding='([^']+)"); pos = rx.indexIn(xmlData); } if (pos > -1) { QString codecNameT = rx.cap(1); qDebug() << "Codec name (1):" << codecNameT; QTextCodec *codec = QTextCodec::codecForName(codecNameT.toUtf8()); if (codec) { convertData = codec->toUnicode(xmlData); } else { qWarning() << "Codec not found (1): " << codecNameT << feedUrl; if (codecNameT.contains("us-ascii", Qt::CaseInsensitive)) { QString str(xmlData); convertData = str.remove(rx.cap(0)+"\""); } } } else { if (!codecName.isEmpty()) { qDebug() << "Codec name (2):" << codecName; QTextCodec *codec = QTextCodec::codecForName(codecName.toUtf8()); if (codec) { convertData = codec->toUnicode(xmlData); codecOk = true; } else { qWarning() << "Codec not found (2): " << codecName << feedUrl; } } if (!codecOk) { codecOk = false; QStringList codecNameList; codecNameList << "UTF-8" << "Windows-1251" << "KOI8-R" << "KOI8-U" << "ISO 8859-5" << "IBM 866"; foreach (QString codecNameT, codecNameList) { QTextCodec *codec = QTextCodec::codecForName(codecNameT.toUtf8()); if (codec && codec->canEncode(xmlData)) { qDebug() << "Codec name (3):" << codecNameT; convertData = codec->toUnicode(xmlData); codecOk = true; break; } } if (!codecOk) { convertData = QString::fromLocal8Bit(xmlData); } }