X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fdocumentvalidator.cpp;h=899bad46f87ac7290d7720297df24b439fd4ba0c;hb=c3302003093710ee247ad84c0fe2ef3c579d417f;hp=3722a0d929c822efec099ff51b03410c100ebd5b;hpb=beaafc4344c4b7e63921d711bf6f66a7f1634ca8;p=kdenlive diff --git a/src/documentvalidator.cpp b/src/documentvalidator.cpp index 3722a0d9..899bad46 100644 --- a/src/documentvalidator.cpp +++ b/src/documentvalidator.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #include #include @@ -38,11 +37,12 @@ #include -#include "locale.h" +#include -DocumentValidator::DocumentValidator(QDomDocument doc): +DocumentValidator::DocumentValidator(const QDomDocument &doc, const KUrl &documentUrl): m_doc(doc), + m_url(documentUrl), m_modified(false) {} @@ -57,30 +57,72 @@ bool DocumentValidator::validate(const double currentVersion) // Check if we're validating a Kdenlive project if (kdenliveDoc.isNull()) return false; + + QString rootDir = mlt.attribute("root"); + if (rootDir == "$CURRENTPATH") { + // The document was extracted from a Kdenlive archived project, fix root directory + QString playlist = m_doc.toString(); + playlist.replace("$CURRENTPATH", m_url.directory(KUrl::IgnoreTrailingSlash)); + m_doc.setContent(playlist); + mlt = m_doc.firstChildElement("mlt"); + kdenliveDoc = mlt.firstChildElement("kdenlivedoc"); + } // Previous MLT / Kdenlive versions used C locale by default QLocale documentLocale = QLocale::c(); if (mlt.hasAttribute("LC_NUMERIC")) { - // Set locale for the document - // WARNING: what should be done in case the locale does not exist on the system? - setlocale(LC_NUMERIC, mlt.attribute("LC_NUMERIC").toUtf8().constData()); + // Set locale for the document + const QString newLocale = setlocale(LC_NUMERIC, mlt.attribute("LC_NUMERIC").toUtf8().constData()); documentLocale = QLocale(mlt.attribute("LC_NUMERIC")); + + // Make sure Qt locale and C++ locale have the same numeric separator, might not be the case + // With some locales since C++ and Qt use a different database for locales + char *separator = localeconv()->decimal_point; + if (newLocale.isEmpty()) { + // Requested locale not available, ask for install + KMessageBox::sorry(kapp->activeWindow(), i18n("The document was created in \"%1\" locale, which is not installed on your system. Please install that language pack. Until then, Kdenlive might not be able to correctly open the document.", mlt.attribute("LC_NUMERIC"))); + } + + if (separator != documentLocale.decimalPoint()) { + KMessageBox::sorry(kapp->activeWindow(), i18n("There is a locale conflict on your system. The document uses locale %1 which uses a \"%2\" as numeric separator (in system libraries) but Qt expects \"%3\". You might not be able to correctly open the project.", mlt.attribute("LC_NUMERIC"), separator, documentLocale.decimalPoint())); + kDebug()<<"------\n!!! system locale is not similar to Qt's locale... be prepared for bugs!!!\n------"; + // HACK: There is a locale conflict, so set locale to at least have correct decimal point + if (strncmp(separator, ".", 1) == 0) documentLocale = QLocale::c(); + else if (strncmp(separator, ",", 1) == 0) documentLocale = QLocale("fr_FR.UTF-8"); + } } documentLocale.setNumberOptions(QLocale::OmitGroupSeparator); - if (documentLocale != QLocale()) { + if (documentLocale.decimalPoint() != QLocale().decimalPoint()) { + // If loading an older MLT file without LC_NUMERIC, set locale to C which was previously the default + if (!mlt.hasAttribute("LC_NUMERIC")) { + setlocale(LC_NUMERIC, "C"); + } + QLocale::setDefault(documentLocale); // locale conversion might need to be redone - initEffects::parseEffectFiles(); + initEffects::parseEffectFiles(setlocale(LC_NUMERIC, NULL)); } - // TODO: remove after string freeze - if (0) - KMessageBox::sorry(kapp->activeWindow(), i18n("The document you are opening uses a different locale (%1) than your system. You can only open and render it, no editing is supported unless you change your system's locale.", mlt.attribute("LC_NUMERIC")), i18n("Read only project")); - + bool ok; + double version = documentLocale.toDouble(kdenliveDoc.attribute("version"), &ok); + if (!ok) { + // Could not parse version number, there is probably a conflict in decimal separator + QLocale tempLocale = QLocale(mlt.attribute("LC_NUMERIC")); + version = tempLocale.toDouble(kdenliveDoc.attribute("version"), &ok); + if (!ok) version = kdenliveDoc.attribute("version").toDouble(&ok); + if (!ok) { + // Last try: replace comma with a dot + QString versionString = kdenliveDoc.attribute("version"); + if (versionString.contains(',')) versionString.replace(',', '.'); + version = versionString.toDouble(&ok); + if (!ok) kDebug()<<"// CANNOT PARSE VERSION NUMBER, ERROR!"; + } + } + // Upgrade the document to the latest version - if (!upgrade(documentLocale.toDouble(kdenliveDoc.attribute("version")), currentVersion)) + if (!upgrade(version, currentVersion)) return false; /* @@ -129,7 +171,7 @@ bool DocumentValidator::validate(const double currentVersion) tnode = tinfo.firstChild(); } - for (int i = 1; i < tracks.count(); i++) { + for (int i = 1; i < tracks.count(); ++i) { QString hide = tracks.at(i).toElement().attribute("hide"); QDomElement newTrack = m_doc.createElement("trackinfo"); if (hide == "video") { @@ -247,7 +289,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) blank_tractor.appendChild(blank_track); QDomNodeList kdenlivetracks = m_doc.elementsByTagName("kdenlivetrack"); - for (int i = 0; i < kdenlivetracks.count(); i++) { + for (int i = 0; i < kdenlivetracks.count(); ++i) { blank_playlist = m_doc.createElement("playlist"); blank_playlist.setAttribute("id", "playlist" + QString::number(i)); westley.insertBefore(blank_playlist, QDomNode()); @@ -259,7 +301,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) blank_track.setAttribute("hide", "video"); } } - } else for (int i = 0; i < max; i++) { + } else for (int i = 0; i < max; ++i) { QDomNode n = playlists.at(i); westley.insertBefore(n, QDomNode()); QDomElement pl = n.toElement(); @@ -320,7 +362,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) // audio track mixing transitions should not be added to track view, so add required attribute QDomNodeList transitions = m_doc.elementsByTagName("transition"); max = transitions.count(); - for (int i = 0; i < max; i++) { + for (int i = 0; i < max; ++i) { QDomElement tr = transitions.at(i).toElement(); if (tr.attribute("combine") == "1" && tr.attribute("mlt_service") == "mix") { QDomElement property = m_doc.createElement("property"); @@ -350,14 +392,14 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) } // move transitions after tracks - for (int i = 0; i < max; i++) { + for (int i = 0; i < max; ++i) { tractor.insertAfter(transitions.at(0), QDomNode()); } // Fix filters format QDomNodeList entries = m_doc.elementsByTagName("entry"); max = entries.count(); - for (int i = 0; i < max; i++) { + for (int i = 0; i < max; ++i) { QString last_id; int effectix = 0; QDomNode m = entries.at(i).firstChild(); @@ -397,7 +439,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) max = filters.count(); QString last_id; int effectix = 0; - for (int i = 0; i < max; i++) { + for (int i = 0; i < max; ++i) { QDomElement filt = filters.at(i).toElement(); QDomNamedNodeMap attrs = filt.attributes(); QString current_id = filt.attribute("kdenlive_id"); @@ -426,7 +468,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) // fix slowmotion QDomNodeList producers = westley.toElement().elementsByTagName("producer"); max = producers.count(); - for (int i = 0; i < max; i++) { + for (int i = 0; i < max; ++i) { QDomElement prod = producers.at(i).toElement(); if (prod.attribute("mlt_service") == "framebuffer") { QString slowmotionprod = prod.attribute("resource"); @@ -440,7 +482,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) // This will get the xml producers: producers = m_doc.elementsByTagName("producer"); max = producers.count(); - for (int i = 0; i < max; i++) { + for (int i = 0; i < max; ++i) { QDomElement prod = producers.at(0).toElement(); // add resource also as a property (to allow path correction in setNewResource()) // TODO: will it work with slowmotion? needs testing @@ -566,7 +608,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) } else { QDomNodeList wproducers = westley_element.elementsByTagName("producer"); int kmax = wproducers.count(); - for (int i = 0; i < kmax; i++) { + for (int i = 0; i < kmax; ++i) { QDomElement wproducer = wproducers.at(i).toElement(); if (wproducer.isNull()) { kWarning() << "Found producer in westley0, that was not a QDomElement"; @@ -655,7 +697,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) #endif QDomNodeList elements = westley.childNodes(); max = elements.count(); - for (int i = 0; i < max; i++) { + for (int i = 0; i < max; ++i) { QDomElement prod = elements.at(0).toElement(); westley0.insertAfter(prod, QDomNode()); } @@ -704,7 +746,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) QString tracksOrder = infoXml.attribute("tracks"); if (tracksOrder.isEmpty()) { QDomNodeList tracks = m_doc.elementsByTagName("track"); - for (int i = 0; i < tracks.count(); i++) { + for (int i = 0; i < tracks.count(); ++i) { QDomElement track = tracks.at(i).toElement(); if (track.attribute("producer") != "black_track") { if (track.attribute("hide") == "video") @@ -715,7 +757,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) } } QDomElement tracksinfo = m_doc.createElement("tracksinfo"); - for (int i = 0; i < tracksOrder.size(); i++) { + for (int i = 0; i < tracksOrder.size(); ++i) { QDomElement trackinfo = m_doc.createElement("trackinfo"); if (tracksOrder.data()[i] == 'a') { trackinfo.setAttribute("type", "audio"); @@ -732,7 +774,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) if (version <= 0.82) { // Convert s in s (MLT extreme makeover) QDomNodeList westleyNodes = m_doc.elementsByTagName("westley"); - for (int i = 0; i < westleyNodes.count(); i++) { + for (int i = 0; i < westleyNodes.count(); ++i) { QDomElement westley = westleyNodes.at(i).toElement(); westley.setTagName("mlt"); } @@ -877,7 +919,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) // Make sure we don't have avformat-novalidate producers, since it caused crashes QDomNodeList producers = m_doc.elementsByTagName("producer"); int max = producers.count(); - for (int i = 0; i < max; i++) { + for (int i = 0; i < max; ++i) { QDomElement prod = producers.at(i).toElement(); if (EffectsList::property(prod, "mlt_service") == "avformat-novalidate") EffectsList::setProperty(prod, "mlt_service", "avformat"); @@ -902,7 +944,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) QDomNodeList transitions = m_doc.elementsByTagName("transition"); max = transitions.count(); int out; - for (int i = 0; i < max; i++) { + for (int i = 0; i < max; ++i) { QDomElement trans = transitions.at(i).toElement(); out = trans.attribute("out").toInt() - trans.attribute("in").toInt(); QString geom = EffectsList::property(trans, "geometry"); @@ -933,7 +975,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion) return true; } -QStringList DocumentValidator::getInfoFromEffectName(const QString oldName) +QStringList DocumentValidator::getInfoFromEffectName(const QString &oldName) { QStringList info; // Returns a list to convert old Kdenlive ladspa effects @@ -1051,10 +1093,16 @@ void DocumentValidator::updateEffects() } QDomNodeList effects = m_doc.elementsByTagName("filter"); - - for(int i = 0; i < effects.count(); ++i) { + int max = effects.count(); + QStringList safeEffects; + for(int i = 0; i < max; ++i) { QDomElement effect = effects.at(i).toElement(); QString effectId = EffectsList::property(effect, "kdenlive_id"); + if (safeEffects.contains(effectId)) { + // Do not check the same effect twice if it is at the correct version + // (assume we don't have different versions of the same effect in a project file) + continue; + } QString effectTag = EffectsList::property(effect, "tag"); QString effectVersionStr = EffectsList::property(effect, "version"); double effectVersion = effectVersionStr.isNull() ? -1 : effectVersionStr.toDouble(); @@ -1100,7 +1148,7 @@ void DocumentValidator::updateEffects() QString effectString = updateRules.call(QScriptValue(), QScriptValueList() << serviceVersion << effectVersion << scriptDoc.toString()).toString(); - if (!effectString.isEmpty()) { + if (!effectString.isEmpty() && !scriptEngine.hasUncaughtException()) { scriptDoc.setContent(effectString); QDomNode updatedEffect = effect.ownerDocument().importNode(scriptDoc.documentElement(), true); effect.parentNode().replaceChild(updatedEffect, effect); @@ -1109,12 +1157,24 @@ void DocumentValidator::updateEffects() } else { m_modified = updateEffectParameters(effect.childNodes(), &updateRules, serviceVersion, effectVersion); } + + // set version number since MLT won't change it (only initially set it) + QDomElement versionElem = effect.firstChildElement("version"); + if (EffectsList::property(effect, "version").isNull()) { + versionElem = effect.ownerDocument().createTextNode(QLocale().toString(serviceVersion)).toElement(); + versionElem.setTagName("property"); + versionElem.setAttribute("name", "version"); + effect.appendChild(versionElem); + } else { + EffectsList::setProperty(effect, "version", QLocale().toString(serviceVersion)); + } } + else safeEffects.append(effectId); } } } -bool DocumentValidator::updateEffectParameters(QDomNodeList parameters, const QScriptValue* updateRules, const double serviceVersion, const double effectVersion) +bool DocumentValidator::updateEffectParameters(const QDomNodeList ¶meters, const QScriptValue* updateRules, const double serviceVersion, const double effectVersion) { bool updated = false; bool isDowngrade = serviceVersion < effectVersion;