]> git.sesse.net Git - kdenlive/blobdiff - src/documentvalidator.cpp
Const'ref
[kdenlive] / src / documentvalidator.cpp
index a31c18a841cf29688b6d1b06a38267fdf5c709fb..899bad46f87ac7290d7720297df24b439fd4ba0c 100644 (file)
@@ -27,7 +27,6 @@
 #include <KMessageBox>
 #include <KApplication>
 #include <KLocale>
-#include <KUrl>
 #include <KStandardDirs>
 
 #include <QFile>
 
 #include <mlt++/Mlt.h>
 
-#include "locale.h"
+#include <locale>
 
 
-DocumentValidator::DocumentValidator(QDomDocument doc):
+DocumentValidator::DocumentValidator(const QDomDocument &doc, const KUrl &documentUrl):
         m_doc(doc),
+        m_url(documentUrl),
         m_modified(false)
 {}
 
@@ -57,29 +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.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");
+        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));
     }
 
+    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;
 
     /*
@@ -128,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") {
@@ -246,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());
@@ -258,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();
@@ -319,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");
@@ -349,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();
@@ -396,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");
@@ -425,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");
@@ -439,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
@@ -565,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";
@@ -654,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());
         }
@@ -703,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")
@@ -714,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");
@@ -731,7 +774,7 @@ bool DocumentValidator::upgrade(double version, const double currentVersion)
     if (version <= 0.82) {
         // Convert <westley />s in <mlt />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");
         }
@@ -876,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");
@@ -901,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");
@@ -932,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
@@ -1131,7 +1174,7 @@ void DocumentValidator::updateEffects()
     }
 }
 
-bool DocumentValidator::updateEffectParameters(QDomNodeList parameters, const QScriptValue* updateRules, const double serviceVersion, const double effectVersion)
+bool DocumentValidator::updateEffectParameters(const QDomNodeList &parameters, const QScriptValue* updateRules, const double serviceVersion, const double effectVersion)
 {
     bool updated = false;
     bool isDowngrade = serviceVersion < effectVersion;