]> git.sesse.net Git - kdenlive/blob - src/profilesdialog.cpp
Make sure there is no write error when saving a file:
[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 "profilesdialog.h"
21 #include "kdenlivesettings.h"
22
23 #include <KStandardDirs>
24 #include <KDebug>
25 #include <KMessageBox>
26 #include <KIO/NetAccess>
27
28 #include <QDir>
29 #include <QCloseEvent>
30
31 ProfilesDialog::ProfilesDialog(QWidget * parent): QDialog(parent), m_isCustomProfile(false), m_profileIsModified(false) {
32     m_view.setupUi(this);
33
34     QStringList profilesFilter;
35     profilesFilter << "*";
36
37     m_view.button_delete->setIcon(KIcon("trash-empty"));
38     m_view.button_delete->setToolTip(i18n("Delete profile"));
39     m_view.button_save->setIcon(KIcon("document-save"));
40     m_view.button_save->setToolTip(i18n("Save profile"));
41     m_view.button_create->setIcon(KIcon("document-new"));
42     m_view.button_create->setToolTip(i18n("Create new profile"));
43
44     fillList();
45     slotUpdateDisplay();
46     connect(m_view.profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
47     connect(m_view.button_create, SIGNAL(clicked()), this, SLOT(slotCreateProfile()));
48     connect(m_view.button_save, SIGNAL(clicked()), this, SLOT(slotSaveProfile()));
49     connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteProfile()));
50     connect(m_view.button_default, SIGNAL(clicked()), this, SLOT(slotSetDefaultProfile()));
51
52     connect(m_view.description, SIGNAL(textChanged(const QString &)), this, SLOT(slotProfileEdited()));
53     connect(m_view.frame_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
54     connect(m_view.frame_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
55     connect(m_view.aspect_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
56     connect(m_view.aspect_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
57     connect(m_view.display_num, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
58     connect(m_view.display_den, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
59     connect(m_view.progressive, SIGNAL(stateChanged(int)), this, SLOT(slotProfileEdited()));
60     connect(m_view.size_h, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
61     connect(m_view.size_w, SIGNAL(valueChanged(int)), this, SLOT(slotProfileEdited()));
62 }
63
64 void ProfilesDialog::slotProfileEdited() {
65     m_profileIsModified = true;
66 }
67
68 void ProfilesDialog::fillList(const QString selectedProfile) {
69     // List the Mlt profiles
70     m_view.profiles_list->clear();
71     QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
72     QMapIterator<QString, QString> i(profilesInfo);
73     while (i.hasNext()) {
74         i.next();
75         m_view.profiles_list->addItem(i.key(), i.value());
76     }
77
78     if (!KdenliveSettings::default_profile().isEmpty()) {
79         for (int i = 0; i < m_view.profiles_list->count(); i++) {
80             if (m_view.profiles_list->itemData(i).toString() == KdenliveSettings::default_profile()) {
81                 m_view.profiles_list->setCurrentIndex(i);
82                 break;
83             }
84         }
85     }
86     int ix = m_view.profiles_list->findText(selectedProfile);
87     if (ix != -1) m_view.profiles_list->setCurrentIndex(ix);
88     m_selectedProfileIndex = m_view.profiles_list->currentIndex();
89 }
90
91 void ProfilesDialog::accept() {
92     if (askForSave()) QDialog::accept();
93 }
94
95 void ProfilesDialog::closeEvent(QCloseEvent *event) {
96     if (askForSave()) {
97         event->accept();
98     } else {
99         event->ignore();
100     }
101 }
102
103 bool ProfilesDialog::askForSave() {
104     if (!m_profileIsModified) return true;
105     if (KMessageBox::questionYesNo(this, i18n("The custom profile was modified, do you want to save it?")) != KMessageBox::Yes) return true;
106     return slotSaveProfile();
107 }
108
109 void ProfilesDialog::slotCreateProfile() {
110     m_view.button_delete->setEnabled(false);
111     m_view.button_create->setEnabled(false);
112     m_view.button_save->setEnabled(true);
113     m_view.properties->setEnabled(true);
114 }
115
116 void ProfilesDialog::slotSetDefaultProfile() {
117     int ix = m_view.profiles_list->currentIndex();
118     QString path = m_view.profiles_list->itemData(ix).toString();
119     if (!path.isEmpty()) KdenliveSettings::setDefault_profile(path);
120 }
121
122 bool ProfilesDialog::slotSaveProfile() {
123     const QString profileDesc = m_view.description->text();
124     int ix = m_view.profiles_list->findText(profileDesc);
125     if (ix != -1) {
126         // this profile name already exists
127         const QString path = m_view.profiles_list->itemData(ix).toString();
128         if (!path.contains('/')) {
129             KMessageBox::sorry(this, i18n("A profile with same name already exists in MLT's default profiles, please choose another description for your custom profile."));
130             return false;
131         }
132         saveProfile(path);
133     } else {
134         int i = 0;
135         QString customName = "profiles/customprofile";
136         QString profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
137         kDebug() << " TYING PROFILE FILE: " << profilePath;
138         while (KIO::NetAccess::exists(KUrl(profilePath), KIO::NetAccess::SourceSide, this)) {
139             i++;
140             profilePath = KStandardDirs::locateLocal("appdata", customName + QString::number(i));
141         }
142         saveProfile(profilePath);
143     }
144     m_profileIsModified = false;
145     fillList(profileDesc);
146     m_view.button_create->setEnabled(true);
147     return true;
148 }
149
150 void ProfilesDialog::saveProfile(const QString path) {
151     QFile file(path);
152     if (!file.open(QIODevice::WriteOnly)) {
153         KMessageBox::sorry(this, i18n("Cannot write to file %1", path));
154         return;
155     }
156     QTextStream out(&file);
157     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";
158     if (file.error() != QFile::NoError) {
159         KMessageBox::error(this, i18n("Cannot write to file %1", path));
160     }
161     file.close();
162 }
163
164 void ProfilesDialog::slotDeleteProfile() {
165     const QString path = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
166     if (path.contains('/')) {
167         KIO::NetAccess::del(KUrl(path), this);
168         fillList();
169     } else kDebug() << "//// Cannot delete profile " << path << ", does not seem to be custom one";
170 }
171
172 // static
173 MltVideoProfile ProfilesDialog::getVideoProfile(QString name) {
174     MltVideoProfile result;
175     QStringList profilesNames;
176     QStringList profilesFiles;
177     QStringList profilesFilter;
178     profilesFilter << "*";
179     QString path;
180     bool isCustom = false;
181     if (name.contains('/')) isCustom = true;
182
183     if (!isCustom) {
184         // List the Mlt profiles
185         profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
186         if (profilesFiles.contains(name)) path = KdenliveSettings::mltpath() + '/' + name;
187     }
188     if (isCustom  || path.isEmpty()) {
189         path = name;
190     }
191
192     if (path.isEmpty() || !QFile::exists(path)) {
193         if (name == "dv_pal") {
194             kDebug() << "!!! WARNING, COULD NOT FIND DEFAULT MLT PROFILE";
195             return result;
196         }
197         if (name == KdenliveSettings::default_profile()) KdenliveSettings::setDefault_profile("dv_pal");
198         kDebug() << "// WARNING, COULD NOT FIND PROFILE " << name;
199         return getVideoProfile("dv_pal");
200     }
201     KConfig confFile(path, KConfig::SimpleConfig);
202     result.path = name;
203     result.description = confFile.entryMap().value("description");
204     result.frame_rate_num = confFile.entryMap().value("frame_rate_num").toInt();
205     result.frame_rate_den = confFile.entryMap().value("frame_rate_den").toInt();
206     result.width = confFile.entryMap().value("width").toInt();
207     result.height = confFile.entryMap().value("height").toInt();
208     result.progressive = confFile.entryMap().value("progressive").toInt();
209     result.sample_aspect_num = confFile.entryMap().value("sample_aspect_num").toInt();
210     result.sample_aspect_den = confFile.entryMap().value("sample_aspect_den").toInt();
211     result.display_aspect_num = confFile.entryMap().value("display_aspect_num").toInt();
212     result.display_aspect_den = confFile.entryMap().value("display_aspect_den").toInt();
213     return result;
214 }
215
216 // static
217 QString ProfilesDialog::getProfileDescription(QString name) {
218     QStringList profilesNames;
219     QStringList profilesFiles;
220     QStringList profilesFilter;
221     profilesFilter << "*";
222
223     // List the Mlt profiles
224     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
225     if (profilesFiles.contains(name)) {
226         KConfig confFile(KdenliveSettings::mltpath() + '/' + name, KConfig::SimpleConfig);
227         return confFile.entryMap().value("description");
228     }
229
230     // List custom profiles
231     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
232     for (int i = 0; i < customProfiles.size(); ++i) {
233         profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
234         if (profilesFiles.contains(name)) {
235             KConfig confFile(customProfiles.at(i) + '/' + name, KConfig::SimpleConfig);
236             return confFile.entryMap().value("description");
237         }
238     }
239
240     return QString();
241 }
242
243 // static
244 QMap <QString, QString> ProfilesDialog::getProfilesInfo() {
245     QMap <QString, QString> result;
246     QStringList profilesFilter;
247     profilesFilter << "*";
248
249     // List the Mlt profiles
250     QStringList profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
251     for (int i = 0; i < profilesFiles.size(); ++i) {
252         KConfig confFile(KdenliveSettings::mltpath() + '/' + profilesFiles.at(i), KConfig::SimpleConfig);
253         QString desc = confFile.entryMap().value("description");
254         if (!desc.isEmpty()) result.insert(desc, profilesFiles.at(i));
255     }
256
257     // List custom profiles
258     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
259     for (int i = 0; i < customProfiles.size(); ++i) {
260         profilesFiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
261         for (int j = 0; j < profilesFiles.size(); ++j) {
262             KConfig confFile(customProfiles.at(i) + '/' + profilesFiles.at(j), KConfig::SimpleConfig);
263             QString desc = confFile.entryMap().value("description");
264             if (!desc.isEmpty()) result.insert(desc, customProfiles.at(i) + '/' + profilesFiles.at(j));
265         }
266     }
267     return result;
268 }
269
270 // static
271 QMap< QString, QString > ProfilesDialog::getSettingsFromFile(const QString path) {
272     QStringList profilesNames;
273     QStringList profilesFiles;
274     QStringList profilesFilter;
275     profilesFilter << "*";
276
277     if (!path.contains('/')) {
278         // This is an MLT profile
279         KConfig confFile(KdenliveSettings::mltpath() + '/' + path, KConfig::SimpleConfig);
280         return confFile.entryMap();
281     } else {
282         // This is a custom profile
283         KConfig confFile(path, KConfig::SimpleConfig);
284         return confFile.entryMap();
285     }
286 }
287
288 // static
289 QMap< QString, QString > ProfilesDialog::getSettingsForProfile(const QString profileName) {
290     QStringList profilesNames;
291     QStringList profilesFiles;
292     QStringList profilesFilter;
293     profilesFilter << "*";
294
295     // List the Mlt profiles
296     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
297     for (int i = 0; i < profilesFiles.size(); ++i) {
298         KConfig confFile(KdenliveSettings::mltpath() + '/' + profilesFiles.at(i), KConfig::SimpleConfig);
299         QMap< QString, QString > values = confFile.entryMap();
300         if (values.value("description") == profileName) {
301             values.insert("path", profilesFiles.at(i));
302             return values;
303         }
304     }
305
306     // List custom profiles
307     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
308     for (int i = 0; i < customProfiles.size(); ++i) {
309         QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
310         for (int i = 0; i < profiles.size(); ++i) {
311             KConfig confFile(customProfiles.at(i) + '/' + profiles.at(i), KConfig::SimpleConfig);
312             QMap< QString, QString > values = confFile.entryMap();
313             if (values.value("description") == profileName) {
314                 values.insert("path", customProfiles.at(i) + '/' + profiles.at(i));
315                 return values;
316             }
317         }
318     }
319     return QMap< QString, QString >();
320 }
321
322 // static
323 QString ProfilesDialog::getPathFromDescription(const QString profileDesc) {
324     QStringList profilesNames;
325     QStringList profilesFiles;
326     QStringList profilesFilter;
327     profilesFilter << "*";
328
329     // List the Mlt profiles
330     profilesFiles = QDir(KdenliveSettings::mltpath()).entryList(profilesFilter, QDir::Files);
331     for (int i = 0; i < profilesFiles.size(); ++i) {
332         KConfig confFile(KdenliveSettings::mltpath() + '/' + profilesFiles.at(i), KConfig::SimpleConfig);
333         QMap< QString, QString > values = confFile.entryMap();
334         if (values.value("description") == profileDesc) return profilesFiles.at(i);
335     }
336
337     // List custom profiles
338     QStringList customProfiles = KGlobal::dirs()->findDirs("appdata", "profiles");
339     for (int i = 0; i < customProfiles.size(); ++i) {
340         QStringList profiles = QDir(customProfiles.at(i)).entryList(profilesFilter, QDir::Files);
341         for (int i = 0; i < profiles.size(); ++i) {
342             KConfig confFile(customProfiles.at(i) + '/' + profiles.at(i), KConfig::SimpleConfig);
343             QMap< QString, QString > values = confFile.entryMap();
344             if (values.value("description") == profileDesc) return customProfiles.at(i) + '/' + profiles.at(i);
345         }
346     }
347     return QString();
348 }
349
350
351 void ProfilesDialog::slotUpdateDisplay() {
352     if (askForSave() == false) {
353         m_view.profiles_list->blockSignals(true);
354         m_view.profiles_list->setCurrentIndex(m_selectedProfileIndex);
355         m_view.profiles_list->blockSignals(false);
356         return;
357     }
358
359     m_selectedProfileIndex = m_view.profiles_list->currentIndex();
360     QString currentProfile = m_view.profiles_list->itemData(m_view.profiles_list->currentIndex()).toString();
361     m_isCustomProfile = currentProfile.contains('/');
362     m_view.button_delete->setEnabled(m_isCustomProfile);
363     m_view.properties->setEnabled(m_isCustomProfile);
364     m_view.button_save->setEnabled(m_isCustomProfile);
365     QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
366     m_view.description->setText(values.value("description"));
367     m_view.size_w->setValue(values.value("width").toInt());
368     m_view.size_h->setValue(values.value("height").toInt());
369     m_view.aspect_num->setValue(values.value("sample_aspect_num").toInt());
370     m_view.aspect_den->setValue(values.value("sample_aspect_den").toInt());
371     m_view.display_num->setValue(values.value("display_aspect_num").toInt());
372     m_view.display_den->setValue(values.value("display_aspect_den").toInt());
373     m_view.frame_num->setValue(values.value("frame_rate_num").toInt());
374     m_view.frame_den->setValue(values.value("frame_rate_den").toInt());
375     m_view.progressive->setChecked(values.value("progressive").toInt());
376     m_profileIsModified = false;
377 }
378
379
380 #include "profilesdialog.moc"
381
382