]> git.sesse.net Git - kdenlive/blob - src/databackup/backupwidget.cpp
Use const'ref, change if(foo) delete foo => delete foo not necessary to check it...
[kdenlive] / src / databackup / backupwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "backupwidget.h"
22 #include "kdenlivesettings.h"
23
24 #include <KUrl>
25
26
27 BackupWidget::BackupWidget(const KUrl &projectUrl, const KUrl &projectFolder, const QString &projectId, QWidget * parent) :
28         QDialog(parent)
29 {
30     setupUi(this);
31     setWindowTitle(i18n("Restore Backup File"));
32
33     if (projectUrl.isEmpty()) {
34         // No url, means we opened the backup dialog from an empty project
35         info_label->setText(i18n("Showing all backup files in folder"));
36         m_projectWildcard = '*';
37     }
38     else {
39         info_label->setText(i18n("Showing backup files for %1", projectUrl.fileName()));
40         m_projectWildcard = projectUrl.fileName().section('.', 0, -2);
41         if (!projectId.isEmpty()) m_projectWildcard.append('-' + projectId);
42         else {
43             // No project id, it was lost, add wildcard
44             m_projectWildcard.append('*');
45         }
46     }
47     project_url->setUrl(projectFolder);
48     m_projectWildcard.append("-??");
49     m_projectWildcard.append("??");
50     m_projectWildcard.append("-??");
51     m_projectWildcard.append("-??");
52     m_projectWildcard.append("-??");
53     m_projectWildcard.append("-??.kdenlive");
54
55     slotParseBackupFiles();
56     connect(backup_list, SIGNAL(currentRowChanged(int)), this, SLOT(slotDisplayBackupPreview()));
57     connect(project_url, SIGNAL(textChanged(QString)), this, SLOT(slotParseBackupFiles()));
58     backup_list->setCurrentRow(0);
59     backup_list->setMinimumHeight(QFontMetrics(font()).lineSpacing() * 12);
60     
61 }
62
63
64
65 BackupWidget::~BackupWidget()
66 {
67
68 }
69
70 void BackupWidget::slotParseBackupFiles()
71 {
72     QStringList filter;
73     KUrl backupFile = project_url->url();
74     backupFile.addPath(".backup/");
75     QDir dir(backupFile.path());
76
77     filter << m_projectWildcard;
78     dir.setNameFilters(filter);
79     QFileInfoList resultList = dir.entryInfoList(QDir::Files, QDir::Time);
80     QStringList results;
81     backup_list->clear();
82     QListWidgetItem *item;
83     QString label;
84     for (int i = 0; i < resultList.count(); i++) {
85         label = resultList.at(i).lastModified().toString(Qt::SystemLocaleLongDate);
86         if (m_projectWildcard.startsWith('*')) {
87             // Displaying all backup files, so add project name in the entries
88             label.prepend(resultList.at(i).fileName().section('-', 0, -7) + ".kdenlive - ");
89         }
90         item = new QListWidgetItem(label, backup_list);
91         item->setData(Qt::UserRole, resultList.at(i).absoluteFilePath());
92     }
93     buttonBox->button(QDialogButtonBox::Open)->setEnabled(backup_list->count() > 0);
94 }
95
96 void BackupWidget::slotDisplayBackupPreview()
97 {
98     if (!backup_list->currentItem()) {
99         backup_preview->setPixmap(QPixmap());
100         return;
101     }
102     const QString path = backup_list->currentItem()->data(Qt::UserRole).toString();
103     QPixmap pix(path + ".png");
104     backup_preview->setPixmap(pix);
105 }
106
107 QString BackupWidget::selectedFile() const
108 {
109     if (!backup_list->currentItem()) return QString();
110     return backup_list->currentItem()->data(Qt::UserRole).toString();
111 }
112