File indexing completed on 2024-04-28 04:52:13

0001 /*
0002     SPDX-FileCopyrightText: 2011 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003 
0004 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "backupwidget.h"
0008 #include "kdenlivesettings.h"
0009 
0010 #include <QDir>
0011 #include <QPushButton>
0012 
0013 BackupWidget::BackupWidget(const QUrl &projectUrl, QUrl projectFolder, const QString &projectId, QWidget *parent)
0014     : QDialog(parent)
0015     , m_projectFolder(std::move(projectFolder))
0016 {
0017     setupUi(this);
0018     setWindowTitle(i18nc("@title:window", "Restore Backup File"));
0019 
0020     if (!projectUrl.isValid()) {
0021         // No url, means we opened the backup dialog from an empty project
0022         info_label->setText(i18n("Showing all backup files in folder"));
0023         m_projectWildcard = QLatin1Char('*');
0024     } else {
0025         info_label->setText(i18n("Showing backup files for %1", projectUrl.fileName()));
0026         m_projectWildcard = projectUrl.fileName().section(QLatin1Char('.'), 0, -2);
0027         if (!projectId.isEmpty()) {
0028             m_projectWildcard.append(QLatin1Char('-') + projectId);
0029         } else {
0030             // No project id, it was lost, add wildcard
0031             m_projectWildcard.append(QLatin1Char('*'));
0032         }
0033     }
0034     m_projectWildcard.append(QStringLiteral("-??"));
0035     m_projectWildcard.append(QStringLiteral("??"));
0036     m_projectWildcard.append(QStringLiteral("-??"));
0037     m_projectWildcard.append(QStringLiteral("-??"));
0038     m_projectWildcard.append(QStringLiteral("-??"));
0039     m_projectWildcard.append(QStringLiteral("-??.kdenlive"));
0040 
0041     slotParseBackupFiles();
0042     connect(backup_list, &QListWidget::currentRowChanged, this, &BackupWidget::slotDisplayBackupPreview);
0043     backup_list->setCurrentRow(0);
0044     backup_list->setMinimumHeight(QFontMetrics(font()).lineSpacing() * 12);
0045     slotParseBackupFiles();
0046 }
0047 
0048 BackupWidget::~BackupWidget() = default;
0049 
0050 void BackupWidget::slotParseBackupFiles()
0051 {
0052     QLocale locale;
0053     QStringList filter;
0054     filter << m_projectWildcard;
0055     backup_list->clear();
0056 
0057     // Parse new XDG backup folder $HOME/.local/share/kdenlive/.backup
0058     QDir backupFolder(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/.backup"));
0059     backupFolder.setNameFilters(filter);
0060     QFileInfoList resultList = backupFolder.entryInfoList(QDir::Files, QDir::Time);
0061     for (int i = 0; i < resultList.count(); ++i) {
0062         QString label = locale.toString(resultList.at(i).lastModified(), QLocale::ShortFormat);
0063         if (m_projectWildcard.startsWith(QLatin1Char('*'))) {
0064             // Displaying all backup files, so add project name in the entries
0065             label.prepend(resultList.at(i).fileName().section(QLatin1Char('-'), 0, -7) + QStringLiteral(".kdenlive - "));
0066         }
0067         auto *item = new QListWidgetItem(label, backup_list);
0068         item->setData(Qt::UserRole, resultList.at(i).absoluteFilePath());
0069         item->setToolTip(resultList.at(i).absoluteFilePath());
0070     }
0071 
0072     // Parse old $HOME/kdenlive/.backup folder
0073     QDir dir(m_projectFolder.toLocalFile() + QStringLiteral("/.backup"));
0074     if (dir.exists()) {
0075         dir.setNameFilters(filter);
0076         QFileInfoList resultList2 = dir.entryInfoList(QDir::Files, QDir::Time);
0077         for (int i = 0; i < resultList2.count(); ++i) {
0078             QString label = locale.toString(resultList2.at(i).lastModified(), QLocale::ShortFormat);
0079             if (m_projectWildcard.startsWith(QLatin1Char('*'))) {
0080                 // Displaying all backup files, so add project name in the entries
0081                 label.prepend(resultList2.at(i).fileName().section(QLatin1Char('-'), 0, -7) + QStringLiteral(".kdenlive - "));
0082             }
0083             auto *item = new QListWidgetItem(label, backup_list);
0084             item->setData(Qt::UserRole, resultList2.at(i).absoluteFilePath());
0085             item->setToolTip(resultList2.at(i).absoluteFilePath());
0086         }
0087     }
0088 
0089     buttonBox->button(QDialogButtonBox::Open)->setEnabled(backup_list->count() > 0);
0090 }
0091 
0092 void BackupWidget::slotDisplayBackupPreview()
0093 {
0094     if (!backup_list->currentItem()) {
0095         backup_preview->setPixmap(QPixmap());
0096         return;
0097     }
0098     const QString path = backup_list->currentItem()->data(Qt::UserRole).toString();
0099     QPixmap pix(path + QStringLiteral(".png"));
0100     backup_preview->setPixmap(pix);
0101 }
0102 
0103 QString BackupWidget::selectedFile() const
0104 {
0105     if (!backup_list->currentItem()) {
0106         return QString();
0107     }
0108     return backup_list->currentItem()->data(Qt::UserRole).toString();
0109 }