]> git.sesse.net Git - kdenlive/blob - src/projectsettings.cpp
Display a list of all used files in project settings dialog (in progress)
[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
25 #include <KStandardDirs>
26 #include <KMessageBox>
27 #include <KDebug>
28 #include <kio/directorysizejob.h>
29 #include <KIO/NetAccess>
30
31 #include <QDir>
32 #include <kmessagebox.h>
33
34 ProjectSettings::ProjectSettings(ProjectList *projectlist, int videotracks, int audiotracks, const QString projectPath, bool readOnlyTracks, bool savedProject, QWidget * parent) :
35         QDialog(parent), m_savedProject(savedProject), m_projectList(projectlist)
36 {
37     setupUi(this);
38
39     list_search->setListWidget(files_list);
40
41     QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
42     QMapIterator<QString, QString> i(profilesInfo);
43     while (i.hasNext()) {
44         i.next();
45         profiles_list->addItem(i.key(), i.value());
46     }
47     project_folder->setMode(KFile::Directory);
48     project_folder->setUrl(KUrl(projectPath));
49     QString currentProf = KdenliveSettings::current_profile();
50
51     for (int i = 0; i < profiles_list->count(); i++) {
52         if (profiles_list->itemData(i).toString() == currentProf) {
53             profiles_list->setCurrentIndex(i);
54             break;
55         }
56     }
57
58     m_buttonOk = buttonBox->button(QDialogButtonBox::Ok);
59     //buttonOk->setEnabled(false);
60     audio_thumbs->setChecked(KdenliveSettings::audiothumbnails());
61     video_thumbs->setChecked(KdenliveSettings::videothumbnails());
62     audio_tracks->setValue(audiotracks);
63     video_tracks->setValue(videotracks);
64     if (readOnlyTracks) {
65         video_tracks->setEnabled(false);
66         audio_tracks->setEnabled(false);
67     }
68     slotUpdateDisplay();
69     if (m_projectList != NULL) {
70         slotUpdateFiles();
71         connect(clear_cache, SIGNAL(clicked()), this, SLOT(slotClearCache()));
72         connect(delete_unused, SIGNAL(clicked()), this, SLOT(slotDeleteUnused()));
73     } else tabWidget->widget(1)->setEnabled(false);
74     connect(profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
75     connect(project_folder, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateButton(const QString &)));
76 }
77
78 void ProjectSettings::slotDeleteUnused()
79 {
80     QStringList toDelete;
81     QList <DocClipBase*> list = m_projectList->documentClipList();
82     for (int i = 0; i < list.count(); i++) {
83         DocClipBase *clip = list.at(i);
84         if (clip->numReferences() == 0 && clip->clipType() != SLIDESHOW) {
85             KUrl url = clip->fileURL();
86             if (!url.isEmpty()) toDelete << url.path();
87         }
88     }
89     toDelete.removeDuplicates();
90
91     // make sure our urls are not used in another clip
92     for (int i = 0; i < list.count(); i++) {
93         DocClipBase *clip = list.at(i);
94         if (clip->numReferences() > 0) {
95             KUrl url = clip->fileURL();
96             if (!url.isEmpty() && toDelete.contains(url.path())) toDelete.removeAll(url.path());
97         }
98     }
99
100     if (toDelete.count() == 0) {
101         // No physical url to delete, we only remove unused clips from project (color clips for example have no physical url)
102         if (KMessageBox::warningContinueCancel(this, i18n("This will remove all unused clips from your project."), i18n("Clean up project")) == KMessageBox::Cancel) return;
103         m_projectList->cleanup();
104         slotUpdateFiles();
105         return;
106     }
107     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;
108     m_projectList->trashUnusedClips();
109     slotUpdateFiles();
110 }
111
112 void ProjectSettings::slotClearCache()
113 {
114     buttonBox->setEnabled(false);
115     KIO::NetAccess::del(KUrl(project_folder->url().path(KUrl::AddTrailingSlash) + "thumbs/"), this);
116     KStandardDirs::makeDir(project_folder->url().path(KUrl::AddTrailingSlash) + "thumbs/");
117     buttonBox->setEnabled(true);
118     slotUpdateFiles(true);
119 }
120
121 void ProjectSettings::slotUpdateFiles(bool cacheOnly)
122 {
123     KIO::DirectorySizeJob * job = KIO::directorySize(project_folder->url().path(KUrl::AddTrailingSlash) + "thumbs/");
124     job->exec();
125     thumbs_count->setText(QString::number(job->totalFiles()));
126     thumbs_size->setText(KIO::convertSize(job->totalSize()));
127     delete job;
128     if (cacheOnly) return;
129     int unused = 0;
130     int used = 0;
131     KIO::filesize_t usedSize = 0;
132     KIO::filesize_t unUsedSize = 0;
133     QList <DocClipBase*> list = m_projectList->documentClipList();
134     files_list->clear();
135
136     // List all files that are used in the project. That also means:
137     // images included in slideshow
138     // TODO: images included in titles, images used in luma transitions, files used in playlist clips, files used for LADSPA effects
139
140     QStringList allFiles;
141     for (int i = 0; i < list.count(); i++) {
142         DocClipBase *clip = list.at(i);
143         if (clip->clipType() == SLIDESHOW) {
144             // special case, list all images
145             QString path = clip->fileURL().directory();
146             QString ext = clip->fileURL().path().section('.', -1);
147             QDir dir(path);
148             QStringList filters;
149             filters << "*." + ext;
150             dir.setNameFilters(filters);
151             QStringList result = dir.entryList(QDir::Files);
152             for (int j = 0; j < result.count(); j++) {
153                 allFiles.append(path + result.at(j));
154             }
155         } else if (!clip->fileURL().isEmpty()) allFiles.append(clip->fileURL().path());
156
157         if (clip->numReferences() == 0) {
158             unused++;
159             unUsedSize += clip->fileSize();
160         } else {
161             used++;
162             usedSize += clip->fileSize();
163         }
164     }
165     allFiles.removeDuplicates();
166     files_count->setText(QString::number(allFiles.count()));
167     files_list->addItems(allFiles);
168     used_count->setText(QString::number(used));
169     used_size->setText(KIO::convertSize(usedSize));
170     unused_count->setText(QString::number(unused));
171     unused_size->setText(KIO::convertSize(unUsedSize));
172     delete_unused->setEnabled(unused > 0);
173 }
174
175 void ProjectSettings::accept()
176 {
177     if (!m_savedProject && selectedProfile() != KdenliveSettings::current_profile())
178         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;
179     QDialog::accept();
180 }
181
182 void ProjectSettings::slotUpdateDisplay()
183 {
184     QString currentProfile = profiles_list->itemData(profiles_list->currentIndex()).toString();
185     QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
186     p_size->setText(values.value("width") + 'x' + values.value("height"));
187     p_fps->setText(values.value("frame_rate_num") + '/' + values.value("frame_rate_den"));
188     p_aspect->setText(values.value("sample_aspect_num") + '/' + values.value("sample_aspect_den"));
189     p_display->setText(values.value("display_aspect_num") + '/' + values.value("display_aspect_den"));
190     if (values.value("progressive").toInt() == 0) p_progressive->setText(i18n("Interlaced"));
191     else p_progressive->setText(i18n("Progressive"));
192 }
193
194 void ProjectSettings::slotUpdateButton(const QString &path)
195 {
196     if (path.isEmpty()) m_buttonOk->setEnabled(false);
197     else {
198         m_buttonOk->setEnabled(true);
199         slotUpdateFiles(true);
200     }
201 }
202
203 QString ProjectSettings::selectedProfile() const
204 {
205     return profiles_list->itemData(profiles_list->currentIndex()).toString();
206 }
207
208 KUrl ProjectSettings::selectedFolder() const
209 {
210     return project_folder->url();
211 }
212
213 QPoint ProjectSettings::tracks()
214 {
215     QPoint p;
216     p.setX(video_tracks->value());
217     p.setY(audio_tracks->value());
218     return p;
219 }
220
221 bool ProjectSettings::enableVideoThumbs() const
222 {
223     return video_thumbs->isChecked();
224 }
225
226 bool ProjectSettings::enableAudioThumbs() const
227 {
228     return audio_thumbs->isChecked();
229 }
230
231 #include "projectsettings.moc"
232
233