documentchecker.cpp 58.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/***************************************************************************
 *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

Vincent Pinon's avatar
Vincent Pinon committed
20
#include "documentchecker.h"
21
#include "bin/binplaylist.hpp"
22
#include "effects/effectsrepository.hpp"
23
#include "kdenlivesettings.h"
Nicolas Carion's avatar
Nicolas Carion committed
24
#include "kthumb.h"
Nicolas Carion's avatar
Nicolas Carion committed
25
#include "titler/titlewidget.h"
26

27
#include <KMessageBox>
28
#include <KRecentDirs>
Nicolas Carion's avatar
Nicolas Carion committed
29 30
#include <KUrlRequesterDialog>
#include <klocalizedstring.h>
31

Laurent Montel's avatar
Laurent Montel committed
32
#include "kdenlive_debug.h"
Nicolas Carion's avatar
Nicolas Carion committed
33
#include <QCryptographicHash>
34
#include <QFile>
35
#include <QFileDialog>
Nicolas Carion's avatar
Nicolas Carion committed
36
#include <QFontDatabase>
37
#include <QStandardPaths>
Nicolas Carion's avatar
Nicolas Carion committed
38
#include <QTreeWidgetItem>
Nicolas Carion's avatar
Nicolas Carion committed
39
#include <utility>
40 41 42 43
const int hashRole = Qt::UserRole;
const int sizeRole = Qt::UserRole + 1;
const int idRole = Qt::UserRole + 2;
const int statusRole = Qt::UserRole + 3;
44 45
const int typeRole = Qt::UserRole + 4;
const int typeOriginalResource = Qt::UserRole + 5;
46
const int clipTypeRole = Qt::UserRole + 6;
47

48 49 50
const int CLIPMISSING = 0;
const int CLIPOK = 1;
const int CLIPPLACEHOLDER = 2;
51
const int PROXYMISSING = 4;
52
const int SOURCEMISSING = 5;
53

54 55 56
const int LUMAMISSING = 10;
const int LUMAOK = 11;
const int LUMAPLACEHOLDER = 12;
57

58
enum MISSINGTYPE { TITLE_IMAGE_ELEMENT = 20, TITLE_FONT_ELEMENT = 21 };
59

Nicolas Carion's avatar
Nicolas Carion committed
60 61
DocumentChecker::DocumentChecker(QUrl url, const QDomDocument &doc)
    : m_url(std::move(url))
62 63
    , m_doc(doc)
    , m_dialog(nullptr)
64
{
65 66
}

67
QMap<QString, QString> DocumentChecker::getLumaPairs() const
68
{
69
    QMap<QString, QString> lumaSearchPairs;
70 71 72 73 74 75 76
    lumaSearchPairs.insert(QStringLiteral("luma"), QStringLiteral("resource"));
    lumaSearchPairs.insert(QStringLiteral("movit.luma_mix"), QStringLiteral("resource"));
    lumaSearchPairs.insert(QStringLiteral("composite"), QStringLiteral("luma"));
    lumaSearchPairs.insert(QStringLiteral("region"), QStringLiteral("composite.luma"));
    return lumaSearchPairs;
}

77
bool DocumentChecker::hasErrorInClips()
78
{
79
    int max;
80 81 82 83 84 85
    QDomElement baseElement = m_doc.documentElement();
    QString root = baseElement.attribute(QStringLiteral("root"));
    if (!root.isEmpty()) {
        QDir dir(root);
        if (!dir.exists()) {
            // Looks like project was moved, try recovering root from current project url
86
            m_rootReplacement.first = dir.absolutePath() + QDir::separator();
87
            root = m_url.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash).toLocalFile();
88
            baseElement.setAttribute(QStringLiteral("root"), root);
89
            root = QDir::cleanPath(root) + QDir::separator();
90
            m_rootReplacement.second = root;
Nicolas Carion's avatar
Nicolas Carion committed
91
        } else {
92 93
            root = QDir::cleanPath(root) + QDir::separator();
        }
94
    }
95 96
    // Check if strorage folder for temp files exists
    QString storageFolder;
97
    QDir projectDir(m_url.adjusted(QUrl::RemoveFilename).toLocalFile());
98
    QString documentid;
99 100
    QDomNodeList playlists = m_doc.elementsByTagName(QStringLiteral("playlist"));
    for (int i = 0; i < playlists.count(); ++i) {
101
        if (playlists.at(i).toElement().attribute(QStringLiteral("id")) == BinPlaylist::binPlaylistId) {
102
            documentid = Xml::getXmlProperty(playlists.at(i).toElement(), QStringLiteral("kdenlive:docproperties.documentid"));
103 104 105
            if (documentid.isEmpty()) {
                // invalid document id, recreate one
                documentid = QString::number(QDateTime::currentMSecsSinceEpoch());
Nicolas Carion's avatar
Nicolas Carion committed
106
                // TODO: Warn on invalid doc id
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
107
                Xml::setXmlProperty(playlists.at(i).toElement(), QStringLiteral("kdenlive:docproperties.documentid"), documentid);
108
            }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
109
            storageFolder = Xml::getXmlProperty(playlists.at(i).toElement(), QStringLiteral("kdenlive:docproperties.storagefolder"));
110
            if (!storageFolder.isEmpty() && QFileInfo(storageFolder).isRelative()) {
111 112
                storageFolder.prepend(root);
            }
Nicolas Carion's avatar
Nicolas Carion committed
113
            if (!storageFolder.isEmpty() && !QFile::exists(storageFolder) && projectDir.exists(documentid)) {
114
                storageFolder = projectDir.absolutePath();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
115
                Xml::setXmlProperty(playlists.at(i).toElement(), QStringLiteral("kdenlive:docproperties.storagefolder"),
116
                                    projectDir.absoluteFilePath(documentid));
117
                m_doc.documentElement().setAttribute(QStringLiteral("modified"), 1);
118 119 120 121 122
            }
            break;
        }
    }

123
    QDomNodeList documentProducers = m_doc.elementsByTagName(QStringLiteral("producer"));
124
    QDomElement profile = baseElement.firstChildElement(QStringLiteral("profile"));
125 126 127 128 129 130
    bool hdProfile = true;
    if (!profile.isNull()) {
        if (profile.attribute(QStringLiteral("width")).toInt() < 1000) {
            hdProfile = false;
        }
    }
131
    // List clips whose proxy is missing
Laurent Montel's avatar
Laurent Montel committed
132
    QList<QDomElement> missingProxies;
133
    // List clips who have a working proxy but no source clip
Laurent Montel's avatar
Laurent Montel committed
134
    QList<QDomElement> missingSources;
135 136
    m_safeImages.clear();
    m_safeFonts.clear();
137
    m_missingFonts.clear();
138
    max = documentProducers.count();
139
    QStringList verifiedPaths;
140
    QStringList missingPaths;
141
    QStringList serviceToCheck;
Nicolas Carion's avatar
Nicolas Carion committed
142
    serviceToCheck << QStringLiteral("kdenlivetitle") << QStringLiteral("qimage") << QStringLiteral("pixbuf") << QStringLiteral("timewarp")
143
                   << QStringLiteral("framebuffer") << QStringLiteral("xml") << QStringLiteral("qtext");
144
    for (int i = 0; i < max; ++i) {
145
        QDomElement e = documentProducers.item(i).toElement();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
146
        QString service = Xml::getXmlProperty(e, QStringLiteral("mlt_service"));
Laurent Montel's avatar
Laurent Montel committed
147 148 149
        if (!service.startsWith(QLatin1String("avformat")) && !serviceToCheck.contains(service)) {
            continue;
        }
Vincent Pinon's avatar
Vincent Pinon committed
150
        if (service == QLatin1String("qtext")) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
            QString text = Xml::getXmlProperty(e, QStringLiteral("text"));
            if (text == QLatin1String("INVALID")) {
                // Warning, this is an invalid clip (project saved with missing source)
                // Check if source clip is now available
                QString resource = Xml::getXmlProperty(e, QStringLiteral("warp_resource"));
                if (resource.isEmpty()) {
                    resource = Xml::getXmlProperty(e, QStringLiteral("resource"));
                }
                // Make sure to have absolute paths
                if (QFileInfo(resource).isRelative()) {
                    resource.prepend(root);
                }
                if (QFile::exists(resource)) {
                    // Reset to original service
                    Xml::removeXmlProperty(e, QStringLiteral("text"));
                    QString original_service = Xml::getXmlProperty(e, QStringLiteral("kdenlive:orig_service"));
                    if (!original_service.isEmpty()) {
                        Xml::setXmlProperty(e, QStringLiteral("mlt_service"), original_service);
                    } else {
                        // Try to guess service
                        if (Xml::hasXmlProperty(e, QStringLiteral("ttl"))) {
                            Xml::setXmlProperty(e, QStringLiteral("mlt_service"), QStringLiteral("qimage"));
                        }
                        else if (resource.endsWith(QLatin1String(".kdenlivetitle"))) {
                            Xml::setXmlProperty(e, QStringLiteral("mlt_service"), QStringLiteral("kdenlivetitle"));
                        } else if (resource.endsWith(QLatin1String(".kdenlive")) || resource.endsWith(QLatin1String(".mlt"))) {
                            Xml::setXmlProperty(e, QStringLiteral("mlt_service"), QStringLiteral("xml"));
                        } else {
                            Xml::setXmlProperty(e, QStringLiteral("mlt_service"), QStringLiteral("avformat"));
                        }
                    }
                }
                continue;
            }
185

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
186
            checkMissingImagesAndFonts(QStringList(), QStringList(Xml::getXmlProperty(e, QStringLiteral("family"))), e.attribute(QStringLiteral("id")),
Nicolas Carion's avatar
Nicolas Carion committed
187
                                       e.attribute(QStringLiteral("name")));
Vincent Pinon's avatar
Vincent Pinon committed
188 189
            continue;
        }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
190
        if (service == QLatin1String("kdenlivetitle")) {
Nicolas Carion's avatar
Nicolas Carion committed
191
            // TODO: Check is clip template is missing (xmltemplate) or hash changed
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
192
            QString xml = Xml::getXmlProperty(e, QStringLiteral("xmldata"));
193 194
            QStringList images = TitleWidget::extractImageList(xml);
            QStringList fonts = TitleWidget::extractFontList(xml);
195
            checkMissingImagesAndFonts(images, fonts, e.attribute(QStringLiteral("id")), e.attribute(QStringLiteral("name")));
196 197
            continue;
        }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
198
        QString resource = Xml::getXmlProperty(e, QStringLiteral("resource"));
Laurent Montel's avatar
Laurent Montel committed
199 200 201
        if (resource.isEmpty()) {
            continue;
        }
202
        if (service == QLatin1String("timewarp")) {
Nicolas Carion's avatar
Nicolas Carion committed
203
            // slowmotion clip, trim speed info
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
204
            resource = Xml::getXmlProperty(e, QStringLiteral("warp_resource"));
Laurent Montel's avatar
Laurent Montel committed
205
        } else if (service == QLatin1String("framebuffer")) {
Nicolas Carion's avatar
Nicolas Carion committed
206
            // slowmotion clip, trim speed info
Laurent Montel's avatar
Laurent Montel committed
207
            resource = resource.section(QLatin1Char('?'), 0, 0);
208
        }
209 210 211 212 213

        // Make sure to have absolute paths
        if (QFileInfo(resource).isRelative()) {
            resource.prepend(root);
        }
214 215
        if (verifiedPaths.contains(resource)) {
            // Don't check same url twice (for example track producers)
216 217 218
            if (missingPaths.contains(resource)) {
                m_missingClips.append(e);
            }
219 220
            continue;
        }
221

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
222
        QString proxy = Xml::getXmlProperty(e, QStringLiteral("kdenlive:proxy"));
223
        if (proxy.length() > 1) {
224
            if (QFileInfo(proxy).isRelative()) {
225 226
                proxy.prepend(root);
            }
227 228
            if (!QFile::exists(proxy)) {
                // Missing clip found
229 230 231 232 233 234
                // Check if proxy exists in current storage folder
                bool fixed = false;
                if (!storageFolder.isEmpty()) {
                    QDir dir(storageFolder + QStringLiteral("/proxy/"));
                    if (dir.exists(QFileInfo(proxy).fileName())) {
                        QString updatedPath = dir.absoluteFilePath(QFileInfo(proxy).fileName());
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
235
                        fixProxyClip(e.attribute(QStringLiteral("id")), Xml::getXmlProperty(e, QStringLiteral("kdenlive:proxy")), updatedPath,
Nicolas Carion's avatar
Nicolas Carion committed
236
                                     documentProducers);
237 238 239
                        fixed = true;
                    }
                }
Laurent Montel's avatar
Laurent Montel committed
240
                if (!fixed) {
241
                    missingProxies.append(e);
Laurent Montel's avatar
Laurent Montel committed
242
                }
243
            }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
244
            QString original = Xml::getXmlProperty(e, QStringLiteral("kdenlive:originalurl"));
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
245
            if (QFileInfo(original).isRelative()) {
246 247
                original.prepend(root);
            }
248
            // Check for slideshows
Laurent Montel's avatar
Laurent Montel committed
249
            bool slideshow = original.contains(QStringLiteral("/.all.")) || original.contains(QLatin1Char('?')) || original.contains(QLatin1Char('%'));
250
            if (slideshow && Xml::hasXmlProperty(e, QStringLiteral("ttl"))) {
251 252
                original = QFileInfo(original).absolutePath();
            }
253 254 255
            if (!QFile::exists(original)) {
                // clip has proxy but original clip is missing
                missingSources.append(e);
256
                missingPaths.append(original);
257 258 259
            }
            verifiedPaths.append(resource);
            continue;
260
        }
261
        // Check for slideshows
Laurent Montel's avatar
Laurent Montel committed
262
        bool slideshow = resource.contains(QStringLiteral("/.all.")) || resource.contains(QLatin1Char('?')) || resource.contains(QLatin1Char('%'));
263 264 265 266 267 268 269 270 271 272
        if (slideshow) {
            if (service == QLatin1String("qimage") || service == QLatin1String("pixbuf")) {
                resource = QFileInfo(resource).absolutePath();
            } else if (service.startsWith(QLatin1String("avformat")) && Xml::hasXmlProperty(e, QStringLiteral("ttl"))) {
                // Fix MLT 6.20 avformat slideshows
                if (service.startsWith(QLatin1String("avformat"))) {
                    Xml::setXmlProperty(e, QStringLiteral("mlt_service"), QStringLiteral("qimage"));
                }
                resource = QFileInfo(resource).absolutePath();
            }
273 274
        }
        if (!QFile::exists(resource)) {
275 276 277 278 279 280 281
            // Missing clip found, make sure to omit timeline preview
            if (QFileInfo(resource).absolutePath().endsWith(QString("/%1/preview").arg(documentid))) {
                // This is a timeline preview missing chunk, ignore
            } else {
                m_missingClips.append(e);
                missingPaths.append(resource);
            }
282
        }
283 284
        // Make sure we don't query same path twice
        verifiedPaths.append(resource);
285 286
    }

287
    // Get list of used Luma files
288
    QStringList missingLumas;
289 290
    QStringList filesToCheck;
    QString filePath;
291
    QMap<QString, QString> lumaSearchPairs = getLumaPairs();
292

293
    QDomNodeList trans = m_doc.elementsByTagName(QStringLiteral("transition"));
294
    max = trans.count();
295
    for (int i = 0; i < max; ++i) {
296
        QDomElement transition = trans.at(i).toElement();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
297
        QString service = getProperty(transition, QStringLiteral("mlt_service"));
298
        QString luma;
299 300
        if (lumaSearchPairs.contains(service)) {
            luma = getProperty(transition, lumaSearchPairs.value(service));
301 302
        }
        if (!luma.isEmpty() && !filesToCheck.contains(luma)) {
303
            filesToCheck.append(luma);
304
        }
305
    }
306

Laurent Montel's avatar
Laurent Montel committed
307
    QMap<QString, QString> autoFixLuma;
308
    QString lumaPath;
309
    QString lumaMltPath;
310
    // Check existence of luma files
Nicolas Carion's avatar
Nicolas Carion committed
311
    for (const QString &lumafile : filesToCheck) {
312
        filePath = lumafile;
313
        if (QFileInfo(filePath).isRelative()) {
Laurent Montel's avatar
Laurent Montel committed
314 315
            filePath.prepend(root);
        }
316
        if (!QFile::exists(filePath)) {
317
            QString lumaName = filePath.section(QLatin1Char('/'), -1);
318
            // check if this was an old format luma, not in correct folder
319 320 321
            QString fixedLuma = filePath.section(QLatin1Char('/'), 0, -2);
            lumaName.prepend(hdProfile ? QStringLiteral("/HD/") : QStringLiteral("/PAL/"));
            fixedLuma.append(lumaName);
322 323 324 325 326
            if (QFile::exists(fixedLuma)) {
                // Auto replace pgm with png for lumas
                autoFixLuma.insert(filePath, fixedLuma);
                continue;
            }
327
            // Check Kdenlive folder
328 329 330 331
            if (lumaPath.isEmpty()) {
                QDir dir(QCoreApplication::applicationDirPath());
                dir.cdUp();
                dir.cd(QStringLiteral("share/kdenlive/lumas/"));
332
                lumaPath = dir.absolutePath() + QStringLiteral("/");
333
            }
334
            lumaName = filePath.section(QLatin1Char('/'), -2);
335 336 337 338 339
            lumaName.prepend(lumaPath);
            if (QFile::exists(lumaName)) {
                autoFixLuma.insert(filePath, lumaName);
                continue;
            }
340 341 342 343 344 345 346 347 348 349 350 351
            // Check MLT folder
            if (lumaMltPath.isEmpty()) {
                QDir dir(KdenliveSettings::mltpath());
                dir.cd(QStringLiteral("../lumas/"));
                lumaMltPath = dir.absolutePath() + QStringLiteral("/");
            }
            lumaName = filePath.section(QLatin1Char('/'), -2);
            lumaName.prepend(lumaMltPath);
            if (QFile::exists(lumaName)) {
                autoFixLuma.insert(filePath, lumaName);
                continue;
            }
352

Laurent Montel's avatar
Laurent Montel committed
353
            if (filePath.endsWith(QLatin1String(".pgm"))) {
Nicolas Carion's avatar
Nicolas Carion committed
354
                fixedLuma = filePath.section(QLatin1Char('.'), 0, -2) + QStringLiteral(".png");
Laurent Montel's avatar
Laurent Montel committed
355
            } else if (filePath.endsWith(QLatin1String(".png"))) {
Laurent Montel's avatar
Laurent Montel committed
356
                fixedLuma = filePath.section(QLatin1Char('.'), 0, -2) + QStringLiteral(".pgm");
357 358 359 360
            }
            if (!fixedLuma.isEmpty() && QFile::exists(fixedLuma)) {
                // Auto replace pgm with png for lumas
                autoFixLuma.insert(filePath, fixedLuma);
Laurent Montel's avatar
Laurent Montel committed
361 362
            } else {
                missingLumas.append(lumafile);
363 364 365 366 367 368
            }
        }
    }
    if (!autoFixLuma.isEmpty()) {
        for (int i = 0; i < max; ++i) {
            QDomElement transition = trans.at(i).toElement();
Laurent Montel's avatar
Laurent Montel committed
369
            QString service = getProperty(transition, QStringLiteral("mlt_service"));
370
            QString luma;
371 372
            if (lumaSearchPairs.contains(service)) {
                luma = getProperty(transition, lumaSearchPairs.value(service));
373 374
            }
            if (!luma.isEmpty() && autoFixLuma.contains(luma)) {
375
                updateProperty(transition, lumaSearchPairs.value(service), autoFixLuma.value(luma));
376
            }
377
        }
378
    }
379 380 381 382 383 384 385
    // Check for missing effects
    QDomNodeList effs = m_doc.elementsByTagName(QStringLiteral("filter"));
    max = effs.count();
    QStringList filters;
    for (int i = 0; i < max; ++i) {
        QDomElement transition = effs.at(i).toElement();
        QString service = getProperty(transition, QStringLiteral("kdenlive_id"));
386 387 388
        if (service.isEmpty()) {
            service = getProperty(transition, QStringLiteral("mlt_service"));
        }
389 390
        filters << service;
    }
391
    QStringList processed;
392
    for (const QString &id : filters) {
393
        if (!processed.contains(id) && !EffectsRepository::get()->exists(id)) {
394 395
            m_missingFilters << id;
        }
396
        processed << id;
397
    }
398

399 400 401 402 403 404 405 406 407 408 409
    if (!m_missingFilters.isEmpty()) {
        // Delete missing effects
        for (int i = 0; i < effs.count(); ++i) {
            QDomElement e = effs.item(i).toElement();
            if (m_missingFilters.contains(getProperty(e, QStringLiteral("kdenlive_id")))) {
                // Remove clip
                e.parentNode().removeChild(e);
                --i;
            }
        }
    }
Nicolas Carion's avatar
Nicolas Carion committed
410 411
    if (m_missingClips.isEmpty() && missingLumas.isEmpty() && missingProxies.isEmpty() && missingSources.isEmpty() && m_missingFonts.isEmpty() &&
        m_missingFilters.isEmpty()) {
412
        return false;
Laurent Montel's avatar
Laurent Montel committed
413
    }
414

415
    m_dialog = new QDialog();
416
    m_dialog->setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
417 418
    m_ui.setupUi(m_dialog);

Nicolas Carion's avatar
Nicolas Carion committed
419
    for (const QString &l : missingLumas) {
420
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << i18n("Luma file") << l);
421
        item->setIcon(0, QIcon::fromTheme(QStringLiteral("dialog-close")));
422 423
        item->setData(0, idRole, l);
        item->setData(0, statusRole, LUMAMISSING);
424
    }
425
    m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(m_missingClips.isEmpty() && missingProxies.isEmpty() && missingSources.isEmpty());
426
    max = m_missingClips.count();
427
    m_missingProxyIds.clear();
428
    QStringList processedIds;
429
    for (int i = 0; i < max; ++i) {
430
        QDomElement e = m_missingClips.at(i).toElement();
431
        QString clipType;
432
        ClipType::ProducerType type;
433
        int status = CLIPMISSING;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
434
        const QString service = Xml::getXmlProperty(e, QStringLiteral("mlt_service"));
435 436
        QString resource =
            service == QLatin1String("timewarp") ? Xml::getXmlProperty(e, QStringLiteral("warp_resource")) : Xml::getXmlProperty(e, QStringLiteral("resource"));
Laurent Montel's avatar
Laurent Montel committed
437
        bool slideshow = resource.contains(QStringLiteral("/.all.")) || resource.contains(QLatin1Char('?')) || resource.contains(QLatin1Char('%'));
438
        if (service.startsWith(QLatin1String("avformat")) || service == QLatin1String("framebuffer") ||
Nicolas Carion's avatar
Nicolas Carion committed
439
            service == QLatin1String("timewarp")) {
440
            clipType = i18n("Video clip");
441
            type = ClipType::AV;
Laurent Montel's avatar
Laurent Montel committed
442
        } else if (service == QLatin1String("qimage") || service == QLatin1String("pixbuf")) {
443 444
            if (slideshow) {
                clipType = i18n("Slideshow clip");
445
                type = ClipType::SlideShow;
446 447
            } else {
                clipType = i18n("Image clip");
448
                type = ClipType::Image;
449
            }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
450
        } else if (service == QLatin1String("mlt") || service == QLatin1String("xml")) {
Laurent Montel's avatar
Laurent Montel committed
451
            clipType = i18n("Playlist clip");
452
            type = ClipType::Playlist;
Laurent Montel's avatar
Laurent Montel committed
453
        } else if (e.tagName() == QLatin1String("missingtitle")) {
454
            clipType = i18n("Title Image");
455
            status = TITLE_IMAGE_ELEMENT;
456
            type = ClipType::Text;
Laurent Montel's avatar
Laurent Montel committed
457 458
        } else {
            clipType = i18n("Unknown");
459
            type = ClipType::Unknown;
Laurent Montel's avatar
Laurent Montel committed
460
        }
461 462 463 464 465 466 467 468 469 470 471 472 473 474
        // Newer project format
        QString clipId = Xml::getXmlProperty(e, QStringLiteral("kdenlive:id"));
        if (!clipId.isEmpty()) {
            if (processedIds.contains(clipId)) {
                continue;
            }
            processedIds << clipId;
        } else {
            // Older project file format
            clipId = e.attribute(QStringLiteral("id")).section(QLatin1Char('_'), 0, 0);
            if (processedIds.contains(clipId)) {
                continue;
            }
            processedIds << clipId;
475
        }
476

477
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << clipType);
478
        item->setData(0, statusRole, CLIPMISSING);
479
        item->setData(0, clipTypeRole, (int)type);
480 481 482
        item->setData(0, idRole, e.attribute(QStringLiteral("id")));
        item->setToolTip(0, i18n("Missing item"));

483
        if (status == TITLE_IMAGE_ELEMENT) {
484
            item->setIcon(0, QIcon::fromTheme(QStringLiteral("dialog-warning")));
Laurent Montel's avatar
Laurent Montel committed
485 486
            item->setToolTip(1, e.attribute(QStringLiteral("name")));
            QString imageResource = e.attribute(QStringLiteral("resource"));
487
            item->setData(0, typeRole, status);
Laurent Montel's avatar
Laurent Montel committed
488
            item->setData(0, typeOriginalResource, e.attribute(QStringLiteral("resource")));
489 490 491 492 493 494 495 496 497 498
            if (!m_rootReplacement.first.isEmpty()) {
                if (imageResource.startsWith(m_rootReplacement.first)) {
                    imageResource.replace(m_rootReplacement.first, m_rootReplacement.second);
                    if (QFile::exists(imageResource)) {
                        item->setData(0, statusRole, CLIPOK);
                        item->setToolTip(0, i18n("Relocated item"));
                    }
                }
            }
            item->setText(1, imageResource);
499
        } else {
500
            item->setIcon(0, QIcon::fromTheme(QStringLiteral("dialog-close")));
501
            if (QFileInfo(resource).isRelative()) {
502 503
                resource.prepend(root);
            }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
504 505
            item->setData(0, hashRole, Xml::getXmlProperty(e, QStringLiteral("kdenlive:file_hash")));
            item->setData(0, sizeRole, Xml::getXmlProperty(e, QStringLiteral("kdenlive:file_size")));
506 507 508 509 510 511 512 513 514 515
            if (!m_rootReplacement.first.isEmpty()) {
                if (resource.startsWith(m_rootReplacement.first)) {
                    resource.replace(m_rootReplacement.first, m_rootReplacement.second);
                    if (QFile::exists(resource)) {
                        item->setData(0, statusRole, CLIPOK);
                        item->setToolTip(0, i18n("Relocated item"));
                    }
                }
            }
            item->setText(1, resource);
516
        }
517
    }
518

Nicolas Carion's avatar
Nicolas Carion committed
519
    for (const QString &font : m_missingFonts) {
520 521
        QString clipType = i18n("Title Font");
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << clipType);
522
        item->setData(0, statusRole, CLIPPLACEHOLDER);
523
        item->setIcon(0, QIcon::fromTheme(QStringLiteral("dialog-warning")));
524 525
        QString newft = QFontInfo(QFont(font)).family();
        item->setText(1, i18n("%1 will be replaced by %2", font, newft));
526
        item->setData(0, typeRole, TITLE_FONT_ELEMENT);
527
    }
Nicolas Carion's avatar
Nicolas Carion committed
528

529
    QString infoLabel;
Laurent Montel's avatar
Laurent Montel committed
530
    if (!m_missingClips.isEmpty()) {
531 532 533 534 535 536
        infoLabel = i18n("The project file contains missing clips or files.");
    }
    if (!m_missingFilters.isEmpty()) {
        if (!infoLabel.isEmpty()) {
            infoLabel.append(QStringLiteral("\n"));
        }
Nicolas Carion's avatar
Nicolas Carion committed
537 538
        infoLabel.append(i18np("Missing effect: %2 will be removed from project.", "Missing effects: %2 will be removed from project.",
                               m_missingFilters.count(), m_missingFilters.join(",")));
539
    }
Laurent Montel's avatar
Laurent Montel committed
540
    if (!missingProxies.isEmpty()) {
541 542
        if (!infoLabel.isEmpty()) {
            infoLabel.append(QStringLiteral("\n"));
Laurent Montel's avatar
Laurent Montel committed
543
        }
544
        infoLabel.append(i18n("Missing proxies will be recreated after opening."));
545
    }
Laurent Montel's avatar
Laurent Montel committed
546
    if (!missingSources.isEmpty()) {
547 548
        if (!infoLabel.isEmpty()) {
            infoLabel.append(QStringLiteral("\n"));
Laurent Montel's avatar
Laurent Montel committed
549
        }
550
        infoLabel.append(i18np("The project file contains a missing clip, you can still work with its proxy.",
Nicolas Carion's avatar
Nicolas Carion committed
551
                               "The project file contains %1 missing clips, you can still work with their proxies.", missingSources.count()));
552
    }
553 554
    if (!infoLabel.isEmpty()) {
        m_ui.infoLabel->setText(infoLabel);
555 556
    } else {
        m_ui.infoLabel->setVisible(false);
557
    }
558

559
    m_ui.removeSelected->setEnabled(!m_missingClips.isEmpty());
560
    m_ui.recursiveSearch->setEnabled(!m_missingClips.isEmpty() || !missingLumas.isEmpty() || !missingSources.isEmpty());
561
    m_ui.usePlaceholders->setEnabled(!m_missingClips.isEmpty());
562

563 564 565
    // Check missing proxies
    max = missingProxies.count();
    if (max > 0) {
566
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << i18n("Proxy clip"));
567
        item->setIcon(0, QIcon::fromTheme(QStringLiteral("dialog-warning")));
Nicolas Carion's avatar
Nicolas Carion committed
568 569 570
        item->setText(
            1, i18np("%1 missing proxy clip, will be recreated on project opening", "%1 missing proxy clips, will be recreated on project opening", max));
        // item->setData(0, hashRole, e.attribute("file_hash"));
571 572 573
        item->setData(0, statusRole, PROXYMISSING);
        item->setToolTip(0, i18n("Missing proxy"));
    }
574

575
    for (int i = 0; i < max; ++i) {
576
        QDomElement e = missingProxies.at(i).toElement();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
577
        QString realPath = Xml::getXmlProperty(e, QStringLiteral("kdenlive:originalurl"));
578
        QString id = Xml::getXmlProperty(e, QStringLiteral("kdenlive:id"));
579
        m_missingProxyIds << id;
580
        // Tell Kdenlive to recreate proxy
581
        e.setAttribute(QStringLiteral("_replaceproxy"), QStringLiteral("1"));
582 583
        // Replace proxy url with real clip in MLT producers
        QDomElement mltProd;
584
        int prodsCount = documentProducers.count();
585
        for (int j = 0; j < prodsCount; ++j) {
586
            mltProd = documentProducers.at(j).toElement();
587
            QString prodId = mltProd.attribute(QStringLiteral("id"));
588
            QString parentId = Xml::getXmlProperty(mltProd, QStringLiteral("kdenlive:id"));
589
            bool slowmotion = false;
590
            if (parentId.startsWith(QLatin1String("slowmotion"))) {
591
                slowmotion = true;
Laurent Montel's avatar
Laurent Montel committed
592
                parentId = parentId.section(QLatin1Char(':'), 1, 1);
593
            }
594 595
            if (parentId.contains(QLatin1Char('_'))) {
                parentId = parentId.section(QLatin1Char('_'), 0, 0);
Laurent Montel's avatar
Laurent Montel committed
596
            }
597
            if (parentId == id) {
598
                // Hit, we must replace url
599
                QString suffix;
Laurent Montel's avatar
Laurent Montel committed
600
                if (slowmotion) {
601
                    QString resource = Xml::getXmlProperty(mltProd, QStringLiteral("resource"));
Laurent Montel's avatar
Laurent Montel committed
602
                    suffix = QLatin1Char('?') + resource.section(QLatin1Char('?'), -1);
Laurent Montel's avatar
Laurent Montel committed
603
                }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
604
                Xml::setXmlProperty(mltProd, QStringLiteral("resource"), realPath + suffix);
605 606 607 608
                Xml::setXmlProperty(mltProd, QStringLiteral("kdenlive:proxy"), QStringLiteral("-"));
                if (missingPaths.contains(realPath)) {
                    // Proxy AND source missing
                    setProperty(mltProd, QStringLiteral("_placeholder"), QStringLiteral("1"));
609
                    setProperty(mltProd, QStringLiteral("kdenlive:orig_service"), Xml::getXmlProperty(mltProd, "mlt_service"));
610
                }
611 612 613
            }
        }
    }
614

615 616
    if (max > 0) {
        // original doc was modified
617
        m_doc.documentElement().setAttribute(QStringLiteral("modified"), 1);
618
    }
619

620 621 622 623
    // Check clips with available proxies but missing original source clips
    max = missingSources.count();
    if (max > 0) {
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui.treeWidget, QStringList() << i18n("Source clip"));
624
        item->setIcon(0, QIcon::fromTheme(QStringLiteral("dialog-warning")));
625
        item->setText(1, i18n("%1 missing source clips, you can only use the proxies", max));
Nicolas Carion's avatar
Nicolas Carion committed
626
        // item->setData(0, hashRole, e.attribute("file_hash"));
627 628
        item->setData(0, statusRole, SOURCEMISSING);
        item->setToolTip(0, i18n("Missing source clip"));
629
        for (int i = 0; i < max; ++i) {
630
            QDomElement e = missingSources.at(i).toElement();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
631
            QString realPath = Xml::getXmlProperty(e, QStringLiteral("kdenlive:originalurl"));
632
            QString id = e.attribute(QStringLiteral("id"));
633
            // Tell Kdenlive the source is missing
634
            e.setAttribute(QStringLiteral("_missingsource"), QStringLiteral("1"));
635
            QTreeWidgetItem *subitem = new QTreeWidgetItem(item, QStringList() << i18n("Source clip"));
Nicolas Carion's avatar
Nicolas Carion committed
636
            // qCDebug(KDENLIVE_LOG)<<"// Adding missing source clip: "<<realPath;
637
            subitem->setIcon(0, QIcon::fromTheme(QStringLiteral("dialog-close")));
638
            subitem->setText(1, realPath);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
639 640
            subitem->setData(0, hashRole, Xml::getXmlProperty(e, QStringLiteral("kdenlive:file_hash")));
            subitem->setData(0, sizeRole, Xml::getXmlProperty(e, QStringLiteral("kdenlive:file_size")));
641
            subitem->setData(0, statusRole, CLIPMISSING);
Nicolas Carion's avatar
Nicolas Carion committed
642
            // int t = e.attribute("type").toInt();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
643
            subitem->setData(0, typeRole, Xml::getXmlProperty(e, QStringLiteral("mlt_service")));
644 645
            subitem->setData(0, idRole, id);
        }
646 647
    }
    if (max > 0) {
648
        // original doc was modified
649
        m_doc.documentElement().setAttribute(QStringLiteral("modified"), 1);
650
    }
651
    m_ui.treeWidget->resizeColumnToContents(0);
Laurent Montel's avatar
Laurent Montel committed
652 653 654 655 656
    connect(m_ui.recursiveSearch, &QAbstractButton::pressed, this, &DocumentChecker::slotSearchClips);
    connect(m_ui.usePlaceholders, &QAbstractButton::pressed, this, &DocumentChecker::slotPlaceholders);
    connect(m_ui.removeSelected, &QAbstractButton::pressed, this, &DocumentChecker::slotDeleteSelected);
    connect(m_ui.treeWidget, &QTreeWidget::itemDoubleClicked, this, &DocumentChecker::slotEditItem);
    connect(m_ui.treeWidget, &QTreeWidget::itemSelectionChanged, this, &DocumentChecker::slotCheckButtons);
Nicolas Carion's avatar
Nicolas Carion committed
657
    // adjustSize();
Laurent Montel's avatar
Laurent Montel committed
658 659 660
    if (m_ui.treeWidget->topLevelItem(0)) {
        m_ui.treeWidget->setCurrentItem(m_ui.treeWidget->topLevelItem(0));
    }
661 662
    checkStatus();
    int acceptMissing = m_dialog->exec();
Laurent Montel's avatar
Laurent Montel committed
663 664 665
    if (acceptMissing == QDialog::Accepted) {
        acceptDialog();
    }
666
    return (acceptMissing != QDialog::Accepted);
667 668
}

669 670
DocumentChecker::~DocumentChecker()
{
671
    delete m_dialog;
672
}
673

Laurent Montel's avatar
Laurent Montel committed
674
QString DocumentChecker::getProperty(const QDomElement &effect, const QString &name)
675
{
676
    QDomNodeList params = effect.elementsByTagName(QStringLiteral("property"));
677
    for (int i = 0; i < params.count(); ++i) {
678
        QDomElement e = params.item(i).toElement();
679
        if (e.attribute(QStringLiteral("name")) == name) {
680 681 682 683 684 685
            return e.firstChild().nodeValue();
        }
    }
    return QString();
}

686
void DocumentChecker::updateProperty(const QDomElement &effect, const QString