]> git.sesse.net Git - kdenlive/blob - src/profilesdialog.cpp
Fix typo in management of custom profiles. Should fix:
[kdenlive] / src / profilesdialog.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 <QDir>
21
22 #include <KStandardDirs>
23 #include <KDebug>
24 #include <KMessageBox>
25 #include <KIO/NetAccess>
26
27 #include "kdenlivesettings.h"
28 #include "profilesdialog.h"
29
30 ProfilesDialog::ProfilesDialog(QWidget * parent): QDialog(parent), m_isCustomProfile(false) {
31     m_view.setupUi(this);
32
33     QStringList profilesFilter;
34     profilesFilter << "*";
35
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"));
42
43     fillList();
44     slotUpdateDisplay();
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()));
49 }
50
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);
56     while (i.hasNext()) {
57         i.next();
58         m_view.profiles_list->addItem(i.key(), i.value());
59     }
60
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);
65                 break;
66             }
67         }
68     }
69     int ix = m_view.profiles_list->findText(selectedProfile);
70     if (ix != -1) m_view.profiles_list->setCurrentIndex(ix);
71 }
72
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);
78 }
79
80 void ProfilesDialog::slotSaveProfile() {
81     const QString profileDesc = m_view.description->text();
82     int ix = m_view.profiles_list->findText(profileDesc);
83     if (ix != -1) {
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."));
88             return;
89         }
90         saveProfile(path);
91     } else {
92         int i = 0;
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)) {
97             i++;
98             profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
99         }
100         saveProfile(profilePath);
101     }
102     fillList(profileDesc);
103 }
104
105 void ProfilesDialog::saveProfile(const QString path) {
106     QFile file(path);
107     if (!file.open(QIODevice::WriteOnly)) {
108         KMessageBox::sorry(this, i18n("Cannot write to file %1", path));
109         return;
110     }
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";
113     file.close();
114 }
115
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);
120         fillList();
121     } else kDebug() << "//// Cannot delete profile " << path << ", does not seem to be custom one";
122 }
123
124 // static
125 MltVideoProfile ProfilesDialog::getVideoProfile(QString name) {
126     MltVideoProfile result;
127     QStringList profilesNames;
128     QStringList profilesFiles;
129     QStringList profilesFilter;
130     profilesFilter << "*";
131     QString path;
132     bool isCustom = false;
133     if (name.contains('/')) isCustom = true;
134
135     if (!isCustom) {
136         // List the Mlt profiles
137         profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
138         if (profilesFiles.contains(name)) path = KdenliveSettings::mltpath() + "/" + name;
139     }
140     if (isCustom  || path.isEmpty()) {
141         // List custom profiles
142         path = name;
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;
148                         break;
149                     }
150                 }*/
151     }
152
153     if (path.isEmpty()) return result;
154     KConfig confFile(path, KConfig::SimpleConfig);
155     result.path = name;
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();
166     return result;
167 }
168
169 // static
170 QString ProfilesDialog::getProfileDescription(QString name) {
171     QStringList profilesNames;
172     QStringList profilesFiles;
173     QStringList profilesFilter;
174     profilesFilter << "*";
175
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");
181     }
182
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");
190         }
191     }
192
193     return QString();
194 }
195
196 // static
197 QMap <QString, QString> ProfilesDialog::getProfilesInfo() {
198     QMap <QString, QString> result;
199     QStringList profilesFilter;
200     profilesFilter << "*";
201
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));
208     }
209
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 j = 0; j < profilesFiles.size(); ++j) {
215             KConfig confFile(customProfiles.at(i) + "/" + profilesFiles.at(j), KConfig::SimpleConfig);
216             QString desc = confFile.entryMap().value("description");
217             if (!desc.isEmpty()) result.insert(desc, customProfiles.at(i) + "/" + profilesFiles.at(j));
218         }
219     }
220     return result;
221 }
222
223 // static
224 QMap< QString, QString > ProfilesDialog::getSettingsFromFile(const QString path) {
225     QStringList profilesNames;
226     QStringList profilesFiles;
227     QStringList profilesFilter;
228     profilesFilter << "*";
229
230     if (!path.contains("/")) {
231         // This is an MLT profile
232         KConfig confFile(KdenliveSettings::mltpath() + "/" + path, KConfig::SimpleConfig);
233         return confFile.entryMap();
234     } else {
235         // This is a custom profile
236         KConfig confFile(path, KConfig::SimpleConfig);
237         return confFile.entryMap();
238     }
239 }
240
241 // static
242 QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString profileName) {
243     QStringList profilesNames;
244     QStringList profilesFiles;
245     QStringList profilesFilter;
246     profilesFilter << "*";
247
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));
255             return values;
256         }
257     }
258
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));
268                 return values;
269             }
270         }
271     }
272     return QMap< QString, QString >();
273 }
274
275 // static
276 QString ProfilesDialog::getPathFromDescription(const QString profileDesc) {
277     QStringList profilesNames;
278     QStringList profilesFiles;
279     QStringList profilesFilter;
280     profilesFilter << "*";
281
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);
288     }
289
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);
298         }
299     }
300     return QString();
301 }
302
303
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());
321 }
322
323
324 #include "profilesdialog.moc"
325
326