1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
22 #include <KStandardDirs>
24 #include <KMessageBox>
25 #include <KIO/NetAccess>
27 #include "kdenlivesettings.h"
28 #include "profilesdialog.h"
30 ProfilesDialog::ProfilesDialog(QWidget * parent): QDialog(parent), m_isCustomProfile(false) {
33 QStringList profilesFilter;
34 profilesFilter << "*";
36 m_view.button_delete->setIcon(KIcon("trash-empty"));
37 m_view.button_delete->setToolTip(i18n("Delete profile"));
38 m_view.button_save->setIcon(KIcon("document-save"));
39 m_view.button_save->setToolTip(i18n("Save profile"));
40 m_view.button_create->setIcon(KIcon("document-new"));
41 m_view.button_create->setToolTip(i18n("Create new profile"));
45 connect(m_view.profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
46 connect(m_view.button_create, SIGNAL(clicked()), this, SLOT(slotCreateProfile()));
47 connect(m_view.button_save, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
48 connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
51 void ProfilesDialog::fillList(const QString selectedProfile) {
52 // List the Mlt profiles
53 m_view.profiles_list->clear();
54 QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
55 QMapIterator<QString, QString> i(profilesInfo);
58 m_view.profiles_list->addItem(i.key(), i.value());
61 if (!KdenliveSettings::default_profile().isEmpty()) {
62 for (int i = 0; i < m_view.profiles_list->count(); i++) {
63 if (m_view.profiles_list->itemData(i).toString() == KdenliveSettings::default_profile()) {
64 m_view.profiles_list->setCurrentIndex(i);
69 int ix = m_view.profiles_list->findText(selectedProfile);
70 if (ix != -1) m_view.profiles_list->setCurrentIndex(ix);
73 void ProfilesDialog::slotCreateProfile() {
74 m_view.button_delete->setEnabled(false);
75 m_view.button_create->setEnabled(false);
76 m_view.button_save->setEnabled(true);
77 m_view.properties->setEnabled(true);
80 void ProfilesDialog::slotSaveProfile() {
81 const QString profileDesc = m_view.description->text();
82 int ix = m_view.profiles_list->findText(profileDesc);
84 // this profile name already exists
85 const QString path = m_view.profiles_list->itemData(ix).toString();
86 if (!path.contains("/")) {
87 KMessageBox::sorry(this, i18n("A profile with same name already exists in MLT's default profiles, please choose another description for your custom profile."));
93 QString customName = "profiles/customprofile";
94 QString profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
95 kDebug() << " TYING PROFILE FILE: " << profilePath;
96 while (KIO::NetAccess::exists(KUrl(profilePath), KIO::NetAccess::SourceSide, this)) {
98 profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
100 saveProfile(profilePath);
102 fillList(profileDesc);
105 void ProfilesDialog::saveProfile(const QString path) {
107 if (!file.open(QIODevice::WriteOnly)) {
108 KMessageBox::sorry(this, i18n("Cannot write to file %1", path));
111 QTextStream out(&file);
112 out << "description=" << m_view.description->text() << "\n" << "frame_rate_num=" << m_view.frame_num->value() << "\n" << "frame_rate_den=" << m_view.frame_den->value() << "\n" << "width=" << m_view.size_w->value() << "\n" << "height=" << m_view.size_h->value() << "\n" << "progressive=" << m_view.progressive->isChecked() << "\n" << "sample_aspect_num=" << m_view.aspect_num->value() << "\n" << "sample_aspect_den=" << m_view.aspect_den->value() << "\n" << "display_aspect_num=" << m_view.display_num->value() << "\n" << "display_aspect_den=" << m_view.display_den->value() << "\n";
116 void ProfilesDialog::slotDeleteProfile() {
117 const QString path = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
118 if (path.contains("/")) {
119 KIO::NetAccess::del(KUrl(path), this);
121 } else kDebug() << "//// Cannot delete profile " << path << ", does not seem to be custom one";
125 MltVideoProfile ProfilesDialog::getVideoProfile(QString name) {
126 MltVideoProfile result;
127 QStringList profilesNames;
128 QStringList profilesFiles;
129 QStringList profilesFilter;
130 profilesFilter << "*";
132 bool isCustom = false;
133 if (name.contains('/')) isCustom = true;
136 // List the Mlt profiles
137 profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
138 if (profilesFiles.contains(name)) path = KdenliveSettings::mltpath() + "/" + name;
140 if (isCustom || path.isEmpty()) {
141 // List custom profiles
143 /* QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
144 for (int i = 0; i < customProfiles.size(); ++i) {
145 profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
146 if (profilesFiles.contains(name)) {
147 path = customProfiles.at(i) + "/" + name;
153 if (path.isEmpty()) return result;
154 KConfig confFile(path, KConfig::SimpleConfig);
156 result.description = confFile.entryMap().value("description");
157 result.frame_rate_num = confFile.entryMap().value("frame_rate_num").toInt();
158 result.frame_rate_den = confFile.entryMap().value("frame_rate_den").toInt();
159 result.width = confFile.entryMap().value("width").toInt();
160 result.height = confFile.entryMap().value("height").toInt();
161 result.progressive = confFile.entryMap().value("progressive").toInt();
162 result.sample_aspect_num = confFile.entryMap().value("sample_aspect_num").toInt();
163 result.sample_aspect_den = confFile.entryMap().value("sample_aspect_den").toInt();
164 result.display_aspect_num = confFile.entryMap().value("display_aspect_num").toInt();
165 result.display_aspect_den = confFile.entryMap().value("display_aspect_den").toInt();
170 QString ProfilesDialog::getProfileDescription(QString name) {
171 QStringList profilesNames;
172 QStringList profilesFiles;
173 QStringList profilesFilter;
174 profilesFilter << "*";
176 // List the Mlt profiles
177 profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
178 if (profilesFiles.contains(name)) {
179 KConfig confFile(KdenliveSettings::mltpath() + "/" + name, KConfig::SimpleConfig);
180 return confFile.entryMap().value("description");
183 // List custom profiles
184 QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
185 for (int i = 0; i < customProfiles.size(); ++i) {
186 profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
187 if (profilesFiles.contains(name)) {
188 KConfig confFile(customProfiles.at(i) + "/" + name, KConfig::SimpleConfig);
189 return confFile.entryMap().value("description");
197 QMap <QString, QString> ProfilesDialog::getProfilesInfo() {
198 QMap <QString, QString> result;
199 QStringList profilesFilter;
200 profilesFilter << "*";
202 // List the Mlt profiles
203 QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
204 for (int i = 0; i < profilesFiles.size(); ++i) {
205 KConfig confFile(KdenliveSettings::mltpath() + "/" + profilesFiles.at(i), KConfig::SimpleConfig);
206 QString desc = confFile.entryMap().value("description");
207 if (!desc.isEmpty()) result.insert(desc, profilesFiles.at(i));
210 // List custom profiles
211 QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
212 for (int i = 0; i < customProfiles.size(); ++i) {
213 profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
214 for (int i = 0; i < profilesFiles.size(); ++i) {
215 KConfig confFile(customProfiles.at(i) + "/" + profilesFiles.at(i), KConfig::SimpleConfig);
216 QString desc = confFile.entryMap().value("description");
217 if (!desc.isEmpty()) result.insert(desc, customProfiles.at(i) + "/" + profilesFiles.at(i));
224 QMap< QString, QString > ProfilesDialog::getSettingsFromFile(const QString path) {
225 QStringList profilesNames;
226 QStringList profilesFiles;
227 QStringList profilesFilter;
228 profilesFilter << "*";
230 if (!path.contains("/")) {
231 // This is an MLT profile
232 KConfig confFile(KdenliveSettings::mltpath() + "/" + path, KConfig::SimpleConfig);
233 return confFile.entryMap();
235 // This is a custom profile
236 KConfig confFile(path, KConfig::SimpleConfig);
237 return confFile.entryMap();
242 QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString profileName) {
243 QStringList profilesNames;
244 QStringList profilesFiles;
245 QStringList profilesFilter;
246 profilesFilter << "*";
248 // List the Mlt profiles
249 profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
250 for (int i = 0; i < profilesFiles.size(); ++i) {
251 KConfig confFile(KdenliveSettings::mltpath() + "/" + profilesFiles.at(i), KConfig::SimpleConfig);
252 QMap< QString, QString > values = confFile.entryMap();
253 if (values.value("description") == profileName) {
254 values.insert("path", profilesFiles.at(i));
259 // List custom profiles
260 QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
261 for (int i = 0; i < customProfiles.size(); ++i) {
262 QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
263 for (int i = 0; i < profiles.size(); ++i) {
264 KConfig confFile(customProfiles.at(i) + "/" + profiles.at(i), KConfig::SimpleConfig);
265 QMap< QString, QString > values = confFile.entryMap();
266 if (values.value("description") == profileName) {
267 values.insert("path", customProfiles.at(i) + "/" + profiles.at(i));
272 return QMap< QString, QString >();
276 QString ProfilesDialog::getPathFromDescription(const QString profileDesc) {
277 QStringList profilesNames;
278 QStringList profilesFiles;
279 QStringList profilesFilter;
280 profilesFilter << "*";
282 // List the Mlt profiles
283 profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
284 for (int i = 0; i < profilesFiles.size(); ++i) {
285 KConfig confFile(KdenliveSettings::mltpath() + "/" + profilesFiles.at(i), KConfig::SimpleConfig);
286 QMap< QString, QString > values = confFile.entryMap();
287 if (values.value("description") == profileDesc) return profilesFiles.at(i);
290 // List custom profiles
291 QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
292 for (int i = 0; i < customProfiles.size(); ++i) {
293 QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
294 for (int i = 0; i < profiles.size(); ++i) {
295 KConfig confFile(customProfiles.at(i) + "/" + profiles.at(i), KConfig::SimpleConfig);
296 QMap< QString, QString > values = confFile.entryMap();
297 if (values.value("description") == profileDesc) return customProfiles.at(i) + "/" + profiles.at(i);
304 void ProfilesDialog::slotUpdateDisplay() {
305 QString currentProfile = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
306 m_isCustomProfile = currentProfile.contains("/");
307 m_view.button_delete->setEnabled(m_isCustomProfile);
308 m_view.properties->setEnabled(m_isCustomProfile);
309 m_view.button_save->setEnabled(m_isCustomProfile);
310 QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
311 m_view.description->setText(values.value("description"));
312 m_view.size_w->setValue(values.value("width").toInt());
313 m_view.size_h->setValue(values.value("height").toInt());
314 m_view.aspect_num->setValue(values.value("sample_aspect_num").toInt());
315 m_view.aspect_den->setValue(values.value("sample_aspect_den").toInt());
316 m_view.display_num->setValue(values.value("display_aspect_num").toInt());
317 m_view.display_den->setValue(values.value("display_aspect_den").toInt());
318 m_view.frame_num->setValue(values.value("frame_rate_num").toInt());
319 m_view.frame_den->setValue(values.value("frame_rate_den").toInt());
320 m_view.progressive->setChecked(values.value("progressive").toInt());
324 #include "profilesdialog.moc"