]> git.sesse.net Git - kdenlive/blob - src/wizard.cpp
Only display available codecs in rendering dialog
[kdenlive] / src / wizard.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 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 <QLabel>
21 #include <QFile>
22 #include <QXmlStreamWriter>
23 #include <QApplication>
24 #include <QTimer>
25
26 #include <KStandardDirs>
27 #include <KLocale>
28 #include <KProcess>
29 #include <kmimetype.h>
30
31 #include "kdenlivesettings.h"
32 #include "profilesdialog.h"
33 #include "wizard.h"
34
35 Wizard::Wizard(QWidget *parent): QWizard(parent) {
36     setPixmap(QWizard::WatermarkPixmap, QPixmap(KStandardDirs::locate("appdata", "banner.png")));
37
38     QWizardPage *page1 = new QWizardPage;
39     page1->setTitle(i18n("Welcome"));
40     QLabel *label = new QLabel(i18n("This is the first time you run Kdenlive. This wizard will let you adjust some basic settings, you will be ready to edit your first movie in a few seconds..."));
41     label->setWordWrap(true);
42     m_startLayout = new QVBoxLayout;
43     m_startLayout->addWidget(label);
44     page1->setLayout(m_startLayout);
45     addPage(page1);
46
47     QWizardPage *page4 = new QWizardPage;
48     page4->setTitle(i18n("Checking MLT engine"));
49     m_mltCheck.setupUi(page4);
50     addPage(page4);
51
52     WizardDelegate *listViewDelegate = new WizardDelegate(m_mltCheck.programList);
53     m_mltCheck.programList->setItemDelegate(listViewDelegate);
54
55     QWizardPage *page2 = new QWizardPage;
56     page2->setTitle(i18n("Video Standard"));
57     m_standard.setupUi(page2);
58
59     // build profiles lists
60     m_profilesInfo = ProfilesDialog::getProfilesInfo();
61     QMap<QString, QString>::const_iterator i = m_profilesInfo.constBegin();
62     while (i != m_profilesInfo.constEnd()) {
63         QMap< QString, QString > profileData = ProfilesDialog::getSettingsFromFile(i.value());
64         if (profileData.value("width") == "720") m_dvProfiles.append(i.key());
65         else if (profileData.value("width").toInt() >= 1080) m_hdvProfiles.append(i.key());
66         else m_otherProfiles.append(i.key());
67         ++i;
68     }
69
70     connect(m_standard.button_all, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
71     connect(m_standard.button_hdv, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
72     connect(m_standard.button_dv, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
73     m_standard.button_all->setChecked(true);
74     connect(m_standard.profiles_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckSelectedItem()));
75
76     // select default profile
77     QList<QListWidgetItem *> profiles = m_standard.profiles_list->findItems(ProfilesDialog::getProfileDescription(KdenliveSettings::default_profile()), Qt::MatchExactly);
78     if (profiles.count() > 0) m_standard.profiles_list->setCurrentItem(profiles.at(0));
79     addPage(page2);
80
81     QWizardPage *page3 = new QWizardPage;
82     page3->setTitle(i18n("Additional Settings"));
83     m_extra.setupUi(page3);
84     m_extra.projectfolder->setMode(KFile::Directory);
85     m_extra.projectfolder->setPath(QDir::homePath() + "/kdenlive");
86     m_extra.videothumbs->setChecked(KdenliveSettings::videothumbnails());
87     m_extra.audiothumbs->setChecked(KdenliveSettings::audiothumbnails());
88     m_extra.autosave->setChecked(KdenliveSettings::crashrecovery());
89     connect(m_extra.videothumbs, SIGNAL(stateChanged(int)), this, SLOT(slotCheckThumbs()));
90     connect(m_extra.audiothumbs, SIGNAL(stateChanged(int)), this, SLOT(slotCheckThumbs()));
91     slotCheckThumbs();
92     addPage(page3);
93
94
95     QWizardPage *page5 = new QWizardPage;
96     page5->setTitle(i18n("Checking system"));
97     m_check.setupUi(page5);
98     slotCheckPrograms();
99     addPage(page5);
100
101     listViewDelegate = new WizardDelegate(m_check.programList);
102     m_check.programList->setItemDelegate(listViewDelegate);
103
104     QTimer::singleShot(500, this, SLOT(slotCheckMlt()));
105 }
106
107
108 void Wizard::checkMltComponents() {
109     m_mltCheck.programList->setColumnCount(2);
110     m_mltCheck.programList->setRootIsDecorated(false);
111     m_mltCheck.programList->setHeaderHidden(true);
112     QSize itemSize(20, this->fontMetrics().height() * 2.5);
113     KIcon okIcon("dialog-ok");
114     KIcon missingIcon("dialog-close");
115     m_mltCheck.programList->setColumnWidth(0, 30);
116     m_mltCheck.programList->setIconSize(QSize(24, 24));
117
118
119     QTreeWidgetItem *item = new QTreeWidgetItem(m_mltCheck.programList, QStringList() << QString() << i18n("Inigo") + " (" + KdenliveSettings::rendererpath() + ')');
120     item->setData(1, Qt::UserRole, i18n("Required for rendering (part of MLT package)"));
121     item->setSizeHint(0, itemSize);
122     item->setIcon(0, okIcon);
123
124     // Check MLT's installed producers
125     QProcess checkProcess;
126     checkProcess.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "producer");
127     if (!checkProcess.waitForStarted()) {
128         item->setIcon(0, missingIcon);
129         item->setData(1, Qt::UserRole, i18n("Error starting MLT's command line player (inigo)"));
130         button(QWizard::NextButton)->setEnabled(false);
131     } else {
132         checkProcess.waitForFinished();
133         QByteArray result = checkProcess.readAllStandardError();
134
135         // Check MLT avformat module
136         QTreeWidgetItem *avformatItem = new QTreeWidgetItem(m_mltCheck.programList, QStringList() << QString() << i18n("Avformat module (FFmpeg)"));
137         avformatItem->setData(1, Qt::UserRole, i18n("Required to work with various video formats (hdv, mpeg, flash, ...)"));
138         avformatItem->setSizeHint(0, itemSize);
139         if (!result.contains("- avformat")) {
140             avformatItem->setIcon(0, missingIcon);
141             m_mltCheck.tabWidget->setTabEnabled(1, false);
142         } else {
143             avformatItem->setIcon(0, okIcon);
144             // Check installed audio codecs
145             QProcess checkProcess2;
146             checkProcess2.start(KdenliveSettings::rendererpath(), QStringList() << "noise:" << "-consumer" << "avformat" << "acodec=list");
147             if (!checkProcess2.waitForStarted()) {
148                 m_mltCheck.tabWidget->setTabEnabled(1, false);
149                 kDebug() << "// Error parsing MLT's avformat codecs";
150             } else {
151                 checkProcess2.waitForFinished();
152                 QByteArray codecList = checkProcess2.readAllStandardError();
153                 QString acodecList(codecList);
154                 QStringList result;
155                 QStringList alist = acodecList.split("\n", QString::SkipEmptyParts);
156                 for (int i = 0; i < alist.count(); i++) {
157                     if (alist.at(i).contains("- ")) result.append(alist.at(i).section("- ", 1).simplified().toLower());
158                 }
159                 m_mltCheck.acodecs_list->addItems(result);
160                 KdenliveSettings::setAudiocodecs(result);
161                 //kDebug()<<"// FOUND LIST:\n\n"<<m_audioCodecs<<"\n\n++++++++++++++++++++";
162             }
163             // Check video codecs
164             checkProcess2.start(KdenliveSettings::rendererpath(), QStringList() << "noise:" << "-consumer" << "avformat" << "vcodec=list");
165             if (!checkProcess2.waitForStarted()) {
166                 kDebug() << "// Error parsing MLT's avformat codecs";
167             } else {
168                 checkProcess2.waitForFinished();
169                 QByteArray codecList = checkProcess2.readAllStandardError();
170                 QString vcodecList(codecList);
171                 QStringList result;
172                 QStringList vlist = vcodecList.split("\n", QString::SkipEmptyParts);
173                 for (int i = 0; i < vlist.count(); i++) {
174                     if (vlist.at(i).contains("- ")) result.append(vlist.at(i).section("- ", 1).simplified().toLower());
175                 }
176                 m_mltCheck.vcodecs_list->addItems(result);
177                 KdenliveSettings::setVideocodecs(result);
178                 //kDebug()<<"// FOUND LIST:\n\n"<<m_videoCodecs<<"\n\n++++++++++++++++++++";
179             }
180             // Check formats
181             checkProcess2.start(KdenliveSettings::rendererpath(), QStringList() << "noise:" << "-consumer" << "avformat" << "f=list");
182             if (!checkProcess2.waitForStarted()) {
183                 kDebug() << "// Error parsing MLT's avformat codecs";
184             } else {
185                 checkProcess2.waitForFinished();
186                 QByteArray codecList = checkProcess2.readAllStandardError();
187                 QString vcodecList(codecList);
188                 QStringList result;
189                 QStringList vlist = vcodecList.split("\n", QString::SkipEmptyParts);
190                 for (int i = 0; i < vlist.count(); i++) {
191                     if (vlist.at(i).contains("- ")) {
192                         QString format = vlist.at(i).section("- ", 1).simplified().toLower();
193                         if (format.contains(',')) {
194                             QStringList sub = format.split(',', QString::SkipEmptyParts);
195                             for (int j = 0; j < sub.count(); j++)
196                                 result.append(sub.at(j));
197                         } else result.append(format);
198                     }
199                 }
200                 m_mltCheck.formats_list->addItems(result);
201                 KdenliveSettings::setSupportedformats(result);
202                 //kDebug()<<"// FOUND LIST:\n\n"<<m_videoCodecs<<"\n\n++++++++++++++++++++";
203             }
204
205
206         }
207
208         // Check MLT dv module
209         QTreeWidgetItem *dvItem = new QTreeWidgetItem(m_mltCheck.programList, QStringList() << QString() << i18n("DV module (libdv)"));
210         dvItem->setData(1, Qt::UserRole, i18n("Required to work with dv files if avformat module is not installed"));
211         dvItem->setSizeHint(0, itemSize);
212         if (!result.contains("- libdv")) {
213             dvItem->setIcon(0, missingIcon);
214         } else {
215             dvItem->setIcon(0, okIcon);
216         }
217
218         // Check MLT image format module
219         QTreeWidgetItem *imageItem = new QTreeWidgetItem(m_mltCheck.programList, QStringList() << QString() << i18n("QImage module"));
220         imageItem->setData(1, Qt::UserRole, i18n("Required to work with images"));
221         imageItem->setSizeHint(0, itemSize);
222         if (!result.contains("- qimage")) {
223             imageItem->setIcon(0, missingIcon);
224             imageItem = new QTreeWidgetItem(m_mltCheck.programList, QStringList() << QString() << i18n("Pixbuf module"));
225             imageItem->setData(1, Qt::UserRole, i18n("Required to work with images"));
226             imageItem->setSizeHint(0, itemSize);
227             if (!result.contains("- pixbuf")) imageItem->setIcon(0, missingIcon);
228             else imageItem->setIcon(0, okIcon);
229         } else {
230             imageItem->setIcon(0, okIcon);
231         }
232     }
233 }
234
235 void Wizard::slotCheckPrograms() {
236     m_check.programList->setColumnCount(2);
237     m_check.programList->setRootIsDecorated(false);
238     m_check.programList->setHeaderHidden(true);
239     QSize itemSize(20, this->fontMetrics().height() * 2.5);
240     KIcon okIcon("dialog-ok");
241     KIcon missingIcon("dialog-close");
242     m_check.programList->setColumnWidth(0, 30);
243     m_check.programList->setIconSize(QSize(24, 24));
244
245     QTreeWidgetItem *item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << i18n("FFmpeg & ffplay"));
246     item->setData(1, Qt::UserRole, i18n("Required for webcam capture"));
247     item->setSizeHint(0, itemSize);
248     QString exepath = KStandardDirs::findExe("ffmpeg");
249     if (exepath.isEmpty()) item->setIcon(0, missingIcon);
250     else if (KStandardDirs::findExe("ffplay").isEmpty()) item->setIcon(0, missingIcon);
251     else item->setIcon(0, okIcon);
252
253     item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << i18n("Recordmydesktop"));
254     item->setData(1, Qt::UserRole, i18n("Required for screen capture"));
255     item->setSizeHint(0, itemSize);
256     if (KStandardDirs::findExe("recordmydesktop").isEmpty()) item->setIcon(0, missingIcon);
257     else item->setIcon(0, okIcon);
258
259     item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << i18n("Dvgrab"));
260     item->setData(1, Qt::UserRole, i18n("Required for firewire capture"));
261     item->setSizeHint(0, itemSize);
262     if (KStandardDirs::findExe("dvgrab").isEmpty()) item->setIcon(0, missingIcon);
263     else item->setIcon(0, okIcon);
264
265 }
266
267 void Wizard::installExtraMimes(QString baseName, QStringList globs) {
268     QString mimefile = baseName;
269     mimefile.replace('/', '-');
270     KMimeType::Ptr mime = KMimeType::mimeType(baseName);
271     if (!mime) {
272         kDebug() << "KMimeTypeTrader: mimeType " << baseName << " not found";
273     } else {
274         QStringList extensions = mime->patterns();
275         QString comment = mime->comment();
276         foreach(const QString &glob, globs) {
277             if (!extensions.contains(glob)) extensions << glob;
278         }
279         kDebug() << "EXTS: " << extensions;
280         QString packageFileName = KStandardDirs::locateLocal("xdgdata-mime", "packages/" + mimefile + ".xml");
281         kDebug() << "INSTALLING NEW MIME TO: " << packageFileName;
282         QFile packageFile(packageFileName);
283         if (!packageFile.open(QIODevice::WriteOnly)) {
284             kError() << "Couldn't open" << packageFileName << "for writing";
285             return;
286         }
287         QXmlStreamWriter writer(&packageFile);
288         writer.setAutoFormatting(true);
289         writer.writeStartDocument();
290
291         const QString nsUri = "http://www.freedesktop.org/standards/shared-mime-info";
292         writer.writeDefaultNamespace(nsUri);
293         writer.writeStartElement("mime-info");
294         writer.writeStartElement(nsUri, "mime-type");
295         writer.writeAttribute("type", baseName);
296
297         if (!comment.isEmpty()) {
298             writer.writeStartElement(nsUri, "comment");
299             writer.writeCharacters(comment);
300             writer.writeEndElement(); // comment
301         }
302
303         foreach(const QString& pattern, extensions) {
304             writer.writeStartElement(nsUri, "glob");
305             writer.writeAttribute("pattern", pattern);
306             writer.writeEndElement(); // glob
307         }
308
309         writer.writeEndElement(); // mime-info
310         writer.writeEndElement(); // mime-type
311         writer.writeEndDocument();
312     }
313 }
314
315 void Wizard::runUpdateMimeDatabase() {
316     const QString localPackageDir = KStandardDirs::locateLocal("xdgdata-mime", QString());
317     //Q_ASSERT(!localPackageDir.isEmpty());
318     KProcess proc;
319     proc << "update-mime-database";
320     proc << localPackageDir;
321     const int exitCode = proc.execute();
322     if (exitCode) {
323         kWarning() << proc.program() << "exited with error code" << exitCode;
324     }
325 }
326
327 void Wizard::slotCheckThumbs() {
328     QString pixname = "timeline_vthumbs.png";
329     if (!m_extra.audiothumbs->isChecked() && !m_extra.videothumbs->isChecked()) {
330         pixname = "timeline_nothumbs.png";
331     } else if (m_extra.audiothumbs->isChecked()) {
332         if (m_extra.videothumbs->isChecked())
333             pixname = "timeline_avthumbs.png";
334         else pixname = "timeline_athumbs.png";
335     }
336
337     m_extra.timeline_preview->setPixmap(QPixmap(KStandardDirs::locate("appdata", pixname)));
338 }
339
340 void Wizard::slotCheckStandard() {
341     m_standard.profiles_list->clear();
342     QStringList profiles;
343     if (m_standard.button_dv->isChecked()) {
344         // DV standard
345         m_standard.profiles_list->addItems(m_dvProfiles);
346     } else if (m_standard.button_hdv->isChecked()) {
347         // HDV standard
348         m_standard.profiles_list->addItems(m_hdvProfiles);
349     } else {
350         m_standard.profiles_list->addItems(m_dvProfiles);
351         m_standard.profiles_list->addItems(m_hdvProfiles);
352         m_standard.profiles_list->addItems(m_otherProfiles);
353         //m_standard.profiles_list->sortItems();
354     }
355
356     for (int i = 0; i < m_standard.profiles_list->count(); i++) {
357         QListWidgetItem *item = m_standard.profiles_list->item(i);
358         MltVideoProfile prof = ProfilesDialog::getVideoProfile(m_profilesInfo.value(item->text()));
359         const QString infoString = i18n("<b>Frame size: </b>%1x%2<br><b>Frame rate: </b>%3/%4<br><b>Pixel aspect ratio: </b>%5/%6<br><b>Display aspect ratio: </b>%7/%8").arg(QString::number(prof.width), QString::number(prof.height), QString::number(prof.frame_rate_num), QString::number(prof.frame_rate_den), QString::number(prof.sample_aspect_num), QString::number(prof.sample_aspect_den), QString::number(prof.display_aspect_num), QString::number(prof.display_aspect_den));
360         item->setToolTip(infoString);
361     }
362
363     m_standard.profiles_list->setSortingEnabled(true);
364     m_standard.profiles_list->setCurrentRow(0);
365 }
366
367 void Wizard::slotCheckSelectedItem() {
368     // Make sure we always have an item highlighted
369     m_standard.profiles_list->setCurrentRow(m_standard.profiles_list->currentRow());
370 }
371
372
373 void Wizard::adjustSettings() {
374     if (m_extra.installmimes->isChecked()) {
375         QStringList globs;
376         globs << "*.mts" << "*.m2t" << "*.mod" << "*.ts";
377         installExtraMimes("video/mpeg", globs);
378         globs.clear();
379         globs << "*.dv";
380         installExtraMimes("video/dv", globs);
381         runUpdateMimeDatabase();
382     }
383     KdenliveSettings::setAudiothumbnails(m_extra.audiothumbs->isChecked());
384     KdenliveSettings::setVideothumbnails(m_extra.videothumbs->isChecked());
385     KdenliveSettings::setCrashrecovery(m_extra.autosave->isChecked());
386     if (m_standard.profiles_list->currentItem()) {
387         QString selectedProfile = m_profilesInfo.value(m_standard.profiles_list->currentItem()->text());
388         if (selectedProfile.isEmpty()) selectedProfile = "dv_pal";
389         KdenliveSettings::setDefault_profile(selectedProfile);
390     }
391     QString path = m_extra.projectfolder->url().path();
392     if (KStandardDirs::makeDir(path) == false) kDebug() << "/// ERROR CREATING PROJECT FOLDER: " << path;
393     KdenliveSettings::setDefaultprojectfolder(path);
394
395 }
396
397 void Wizard::slotCheckMlt() {
398     QString errorMessage;
399     if (KdenliveSettings::rendererpath().isEmpty()) {
400         errorMessage.append(i18n("your MLT installation cannot be found. Install MLT and restart Kdenlive.\n"));
401     }
402     /*QProcess checkProcess;
403     checkProcess.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "producer");
404     if (!checkProcess.waitForStarted())
405         errorMessage.append(i18n("Error starting MLT's command line player (inigo)") + ".\n");
406
407     checkProcess.waitForFinished();
408
409     QByteArray result = checkProcess.readAllStandardError();
410     if (!result.contains("- avformat")) errorMessage.append(i18n("MLT's avformat (FFMPEG) module not found. Please check your FFMPEG and MLT install. Kdenlive will not work until this issue is fixed.") + "\n");*/
411
412     QProcess checkProcess2;
413     checkProcess2.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "consumer");
414     if (!checkProcess2.waitForStarted())
415         errorMessage.append(i18n("Error starting MLT's command line player (inigo).") + "\n");
416
417     checkProcess2.waitForFinished();
418
419     QByteArray result = checkProcess2.readAllStandardError();
420     if (!result.contains("sdl") || !result.contains("sdl_preview")) errorMessage.append(i18n("MLT's SDL module not found. Please check your MLT install. Kdenlive will not work until this issue is fixed.") + "\n");
421
422     if (!errorMessage.isEmpty()) {
423         errorMessage.prepend(QString("<b>%1</b><br>").arg(i18n("Fatal Error")));
424         QLabel *pix = new QLabel();
425         pix->setPixmap(KIcon("dialog-error").pixmap(30));
426         QLabel *label = new QLabel(errorMessage);
427         label->setWordWrap(true);
428         m_startLayout->addSpacing(40);
429         m_startLayout->addWidget(pix);
430         m_startLayout->addWidget(label);
431         m_systemCheckIsOk = false;
432         button(QWizard::NextButton)->setEnabled(false);
433     } else m_systemCheckIsOk = true;
434
435     if (m_systemCheckIsOk) checkMltComponents();
436 }
437
438 bool Wizard::isOk() const {
439     return m_systemCheckIsOk;
440 }
441
442 #include "wizard.moc"