]> git.sesse.net Git - kdenlive/blob - src/projectsettings.cpp
bed29b1e9fe4817ffffe9f3593a4144f71ddb9ee
[kdenlive] / src / projectsettings.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 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 #include "projectsettings.h"
21 #include "kdenlivesettings.h"
22 #include "profilesdialog.h"
23 #include "docclipbase.h"
24 #include "titlewidget.h"
25 #include "effectslist.h"
26
27 #include <KStandardDirs>
28 #include <KMessageBox>
29 #include <KDebug>
30 #include <kio/directorysizejob.h>
31 #include <KIO/NetAccess>
32
33 #include <QDir>
34 #include <kmessagebox.h>
35
36 ProjectSettings::ProjectSettings(ProjectList *projectlist, QStringList lumas, int videotracks, int audiotracks, const QString projectPath, bool readOnlyTracks, bool savedProject, QWidget * parent) :
37     QDialog(parent), m_savedProject(savedProject), m_projectList(projectlist), m_lumas(lumas)
38 {
39     setupUi(this);
40
41     list_search->setListWidget(files_list);
42
43     QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
44     QMapIterator<QString, QString> i(profilesInfo);
45     while (i.hasNext()) {
46         i.next();
47         profiles_list->addItem(i.key(), i.value());
48     }
49     project_folder->setMode(KFile::Directory);
50     project_folder->setUrl(KUrl(projectPath));
51     QString currentProf = KdenliveSettings::current_profile();
52
53     for (int i = 0; i < profiles_list->count(); i++) {
54         if (profiles_list->itemData(i).toString() == currentProf) {
55             profiles_list->setCurrentIndex(i);
56             break;
57         }
58     }
59
60     m_buttonOk = buttonBox->button(QDialogButtonBox::Ok);
61     //buttonOk->setEnabled(false);
62     audio_thumbs->setChecked(KdenliveSettings::audiothumbnails());
63     video_thumbs->setChecked(KdenliveSettings::videothumbnails());
64     audio_tracks->setValue(audiotracks);
65     video_tracks->setValue(videotracks);
66
67     if (readOnlyTracks) {
68         video_tracks->setEnabled(false);
69         audio_tracks->setEnabled(false);
70     }
71     slotUpdateDisplay();
72     if (m_projectList != NULL) {
73         slotUpdateFiles();
74         connect(clear_cache, SIGNAL(clicked()), this, SLOT(slotClearCache()));
75         connect(delete_unused, SIGNAL(clicked()), this, SLOT(slotDeleteUnused()));
76     } else tabWidget->widget(1)->setEnabled(false);
77     connect(profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
78     connect(project_folder, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButton(const QString &)));
79 }
80
81 void ProjectSettings::slotDeleteUnused()
82 {
83     QStringList toDelete;
84     QList <DocClipBase*> list = m_projectList->documentClipList();
85     for (int i = 0; i < list.count(); i++) {
86         DocClipBase *clip = list.at(i);
87         if (clip->numReferences() == 0 && clip->clipType() != SLIDESHOW) {
88             KUrl url = clip->fileURL();
89             if (!url.isEmpty() && !toDelete.contains(url.path())) toDelete << url.path();
90         }
91     }
92
93     // make sure our urls are not used in another clip
94     for (int i = 0; i < list.count(); i++) {
95         DocClipBase *clip = list.at(i);
96         if (clip->numReferences() > 0) {
97             KUrl url = clip->fileURL();
98             if (!url.isEmpty() && toDelete.contains(url.path())) toDelete.removeAll(url.path());
99         }
100     }
101
102     if (toDelete.count() == 0) {
103         // No physical url to delete, we only remove unused clips from project (color clips for example have no physical url)
104         if (KMessageBox::warningContinueCancel(this, i18n("This will remove all unused clips from your project."), i18n("Clean up project")) == KMessageBox::Cancel) return;
105         m_projectList->cleanup();
106         slotUpdateFiles();
107         return;
108     }
109     if (KMessageBox::warningYesNoList(this, i18n("This will remove the following files from your hard drive.\nThis action cannot be undone, only use if you know what you are doing.\nAre you sure you want to continue?"), toDelete, i18n("Delete unused clips")) != KMessageBox::Yes) return;
110     m_projectList->trashUnusedClips();
111     slotUpdateFiles();
112 }
113
114 void ProjectSettings::slotClearCache()
115 {
116     buttonBox->setEnabled(false);
117     KIO::NetAccess::del(KUrl(project_folder->url().path(KUrl::AddTrailingSlash) + "thumbs/"), this);
118     KStandardDirs::makeDir(project_folder->url().path(KUrl::AddTrailingSlash) + "thumbs/");
119     buttonBox->setEnabled(true);
120     slotUpdateFiles(true);
121 }
122
123 void ProjectSettings::slotUpdateFiles(bool cacheOnly)
124 {
125     KIO::DirectorySizeJob * job = KIO::directorySize(project_folder->url().path(KUrl::AddTrailingSlash) + "thumbs/");
126     job->exec();
127     thumbs_count->setText(QString::number(job->totalFiles()));
128     thumbs_size->setText(KIO::convertSize(job->totalSize()));
129     delete job;
130     if (cacheOnly) return;
131     int unused = 0;
132     int used = 0;
133     KIO::filesize_t usedSize = 0;
134     KIO::filesize_t unUsedSize = 0;
135     QList <DocClipBase*> list = m_projectList->documentClipList();
136     files_list->clear();
137
138     // List all files that are used in the project. That also means:
139     // images included in slideshow and titles, files in playlist clips
140     // TODO: images used in luma transitions, files used for LADSPA effects?
141
142     QStringList allFiles;
143     QStringList allFonts;
144     allFiles << m_lumas;
145     for (int i = 0; i < list.count(); i++) {
146         DocClipBase *clip = list.at(i);
147         if (clip->clipType() == SLIDESHOW) {
148             // special case, list all images
149             QStringList files = extractSlideshowUrls(clip->fileURL());
150             allFiles << files;
151         } else if (!clip->fileURL().isEmpty()) allFiles.append(clip->fileURL().path());
152         if (clip->clipType() == TEXT) {
153             QStringList images = TitleWidget::extractImageList(clip->getProperty("xmldata"));
154             QStringList fonts = TitleWidget::extractFontList(clip->getProperty("xmldata"));
155             allFiles << images;
156             allFonts << fonts;
157         } else if (clip->clipType() == PLAYLIST) {
158             QStringList files = extractPlaylistUrls(clip->fileURL().path());
159             allFiles << files;
160         }
161
162         if (clip->numReferences() == 0) {
163             unused++;
164             unUsedSize += clip->fileSize();
165         } else {
166             used++;
167             usedSize += clip->fileSize();
168         }
169     }
170 #if QT_VERSION >= 0x040500
171     allFiles.removeDuplicates();
172     allFonts.removeDuplicates();
173 #endif
174     files_count->setText(QString::number(allFiles.count()));
175     files_list->addItems(allFiles);
176     fonts_list->addItems(allFonts);
177     used_count->setText(QString::number(used));
178     used_size->setText(KIO::convertSize(usedSize));
179     unused_count->setText(QString::number(unused));
180     unused_size->setText(KIO::convertSize(unUsedSize));
181     delete_unused->setEnabled(unused > 0);
182 }
183
184 void ProjectSettings::accept()
185 {
186     if (!m_savedProject && selectedProfile() != KdenliveSettings::current_profile())
187         if (KMessageBox::warningContinueCancel(this, i18n("Changing the profile of your project cannot be undone.\nIt is recommended to save your project before attempting this operation that might cause some corruption in transitions.\n Are you sure you want to proceed?"), i18n("Confirm profile change")) == KMessageBox::Cancel) return;
188     QDialog::accept();
189 }
190
191 void ProjectSettings::slotUpdateDisplay()
192 {
193     QString currentProfile = profiles_list->itemData(profiles_list->currentIndex()).toString();
194     QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
195     p_size->setText(values.value("width") + 'x' + values.value("height"));
196     p_fps->setText(values.value("frame_rate_num") + '/' + values.value("frame_rate_den"));
197     p_aspect->setText(values.value("sample_aspect_num") + '/' + values.value("sample_aspect_den"));
198     p_display->setText(values.value("display_aspect_num") + '/' + values.value("display_aspect_den"));
199     if (values.value("progressive").toInt() == 0) {
200         p_progressive->setText(i18n("Interlaced (%1 fields per second)",
201                                     QString::number((double)2 * values.value("frame_rate_num").toInt() / values.value("frame_rate_den").toInt(), 'f', 2)));
202     } else {
203         p_progressive->setText(i18n("Progressive"));
204     }
205     p_colorspace->setText(ProfilesDialog::getColorspaceDescription(values.value("colorspace").toInt()));
206 }
207
208 void ProjectSettings::slotUpdateButton(const QString &path)
209 {
210     if (path.isEmpty()) m_buttonOk->setEnabled(false);
211     else {
212         m_buttonOk->setEnabled(true);
213         slotUpdateFiles(true);
214     }
215 }
216
217 QString ProjectSettings::selectedProfile() const
218 {
219     return profiles_list->itemData(profiles_list->currentIndex()).toString();
220 }
221
222 KUrl ProjectSettings::selectedFolder() const
223 {
224     return project_folder->url();
225 }
226
227 QPoint ProjectSettings::tracks()
228 {
229     QPoint p;
230     p.setX(video_tracks->value());
231     p.setY(audio_tracks->value());
232     return p;
233 }
234
235 bool ProjectSettings::enableVideoThumbs() const
236 {
237     return video_thumbs->isChecked();
238 }
239
240 bool ProjectSettings::enableAudioThumbs() const
241 {
242     return audio_thumbs->isChecked();
243 }
244
245
246 //static
247 QStringList ProjectSettings::extractPlaylistUrls(QString path)
248 {
249     QStringList urls;
250     QDomDocument doc;
251     QFile file(path);
252     if (!file.open(QIODevice::ReadOnly))
253         return urls;
254     if (!doc.setContent(&file)) {
255         file.close();
256         return urls;
257     }
258     file.close();
259     QString root = doc.documentElement().attribute("root");
260     if (!root.isEmpty() && !root.endsWith('/')) root.append('/');
261     QDomNodeList files = doc.elementsByTagName("producer");
262     for (int i = 0; i < files.count(); i++) {
263         QDomElement e = files.at(i).toElement();
264         QString type = EffectsList::property(e, "mlt_service");
265         if (type != "colour") {
266             QString url = EffectsList::property(e, "resource");
267             if (!url.isEmpty()) {
268                 if (!url.startsWith('/')) url.prepend(root);
269                 if (url.section('.', 0, -2).endsWith("/.all")) {
270                     // slideshow clip, extract image urls
271                     urls << extractSlideshowUrls(KUrl(url));
272                 } else urls << url;
273                 if (url.endsWith(".mlt") || url.endsWith(".kdenlive")) {
274                     //TODO: Do something to avoid infinite loops if 2 files reference themselves...
275                     urls << extractPlaylistUrls(url);
276                 }
277             }
278         }
279     }
280
281     // luma files for transitions
282     files = doc.elementsByTagName("transition");
283     for (int i = 0; i < files.count(); i++) {
284         QDomElement e = files.at(i).toElement();
285         QString url = EffectsList::property(e, "luma");
286         if (!url.isEmpty()) {
287             if (!url.startsWith('/')) url.prepend(root);
288             urls << url;
289         }
290     }
291
292     return urls;
293 }
294
295
296 //static
297 QStringList ProjectSettings::extractSlideshowUrls(KUrl url)
298 {
299     QStringList urls;
300     QString path = url.directory(KUrl::AppendTrailingSlash);
301     QString ext = url.path().section('.', -1);
302     QDir dir(path);
303     QStringList filters;
304     filters << "*." + ext;
305     dir.setNameFilters(filters);
306     QStringList result = dir.entryList(QDir::Files);
307     for (int j = 0; j < result.count(); j++) {
308         urls.append(path + result.at(j));
309     }
310     return urls;
311 }
312
313 #include "projectsettings.moc"
314
315