]> git.sesse.net Git - kdenlive/blob - src/wizard.cpp
Check the system for dvd authoring programs
[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     item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << i18n("Dvdauthor"));
266     item->setData(1, Qt::UserRole, i18n("Required for creation of dvd"));
267     item->setSizeHint(0, itemSize);
268     if (KStandardDirs::findExe("dvdauthor").isEmpty()) item->setIcon(0, missingIcon);
269     else item->setIcon(0, okIcon);
270
271     item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << i18n("Mkisofs"));
272     item->setData(1, Qt::UserRole, i18n("Required for creation of dvd iso images"));
273     item->setSizeHint(0, itemSize);
274     if (KStandardDirs::findExe("mkisofs").isEmpty()) item->setIcon(0, missingIcon);
275     else item->setIcon(0, okIcon);
276
277 }
278
279 void Wizard::installExtraMimes(QString baseName, QStringList globs) {
280     QString mimefile = baseName;
281     mimefile.replace('/', '-');
282     KMimeType::Ptr mime = KMimeType::mimeType(baseName);
283     if (!mime) {
284         kDebug() << "KMimeTypeTrader: mimeType " << baseName << " not found";
285     } else {
286         QStringList extensions = mime->patterns();
287         QString comment = mime->comment();
288         foreach(const QString &glob, globs) {
289             if (!extensions.contains(glob)) extensions << glob;
290         }
291         kDebug() << "EXTS: " << extensions;
292         QString packageFileName = KStandardDirs::locateLocal("xdgdata-mime", "packages/" + mimefile + ".xml");
293         kDebug() << "INSTALLING NEW MIME TO: " << packageFileName;
294         QFile packageFile(packageFileName);
295         if (!packageFile.open(QIODevice::WriteOnly)) {
296             kError() << "Couldn't open" << packageFileName << "for writing";
297             return;
298         }
299         QXmlStreamWriter writer(&packageFile);
300         writer.setAutoFormatting(true);
301         writer.writeStartDocument();
302
303         const QString nsUri = "http://www.freedesktop.org/standards/shared-mime-info";
304         writer.writeDefaultNamespace(nsUri);
305         writer.writeStartElement("mime-info");
306         writer.writeStartElement(nsUri, "mime-type");
307         writer.writeAttribute("type", baseName);
308
309         if (!comment.isEmpty()) {
310             writer.writeStartElement(nsUri, "comment");
311             writer.writeCharacters(comment);
312             writer.writeEndElement(); // comment
313         }
314
315         foreach(const QString& pattern, extensions) {
316             writer.writeStartElement(nsUri, "glob");
317             writer.writeAttribute("pattern", pattern);
318             writer.writeEndElement(); // glob
319         }
320
321         writer.writeEndElement(); // mime-info
322         writer.writeEndElement(); // mime-type
323         writer.writeEndDocument();
324     }
325 }
326
327 void Wizard::runUpdateMimeDatabase() {
328     const QString localPackageDir = KStandardDirs::locateLocal("xdgdata-mime", QString());
329     //Q_ASSERT(!localPackageDir.isEmpty());
330     KProcess proc;
331     proc << "update-mime-database";
332     proc << localPackageDir;
333     const int exitCode = proc.execute();
334     if (exitCode) {
335         kWarning() << proc.program() << "exited with error code" << exitCode;
336     }
337 }
338
339 void Wizard::slotCheckThumbs() {
340     QString pixname = "timeline_vthumbs.png";
341     if (!m_extra.audiothumbs->isChecked() && !m_extra.videothumbs->isChecked()) {
342         pixname = "timeline_nothumbs.png";
343     } else if (m_extra.audiothumbs->isChecked()) {
344         if (m_extra.videothumbs->isChecked())
345             pixname = "timeline_avthumbs.png";
346         else pixname = "timeline_athumbs.png";
347     }
348
349     m_extra.timeline_preview->setPixmap(QPixmap(KStandardDirs::locate("appdata", pixname)));
350 }
351
352 void Wizard::slotCheckStandard() {
353     m_standard.profiles_list->clear();
354     QStringList profiles;
355     if (m_standard.button_dv->isChecked()) {
356         // DV standard
357         m_standard.profiles_list->addItems(m_dvProfiles);
358     } else if (m_standard.button_hdv->isChecked()) {
359         // HDV standard
360         m_standard.profiles_list->addItems(m_hdvProfiles);
361     } else {
362         m_standard.profiles_list->addItems(m_dvProfiles);
363         m_standard.profiles_list->addItems(m_hdvProfiles);
364         m_standard.profiles_list->addItems(m_otherProfiles);
365         //m_standard.profiles_list->sortItems();
366     }
367
368     for (int i = 0; i < m_standard.profiles_list->count(); i++) {
369         QListWidgetItem *item = m_standard.profiles_list->item(i);
370         MltVideoProfile prof = ProfilesDialog::getVideoProfile(m_profilesInfo.value(item->text()));
371         const QString infoString = ("<strong>" + i18n("Frame size:") + " </strong>%1x%2<br /><strong>" + i18n("Frame rate:") + " </strong>%3/%4<br /><strong>" + i18n("Pixel aspect ratio:") + "</strong>%5/%6<br /><strong>" + i18n("Display aspect ratio:") + " </strong>%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));
372         item->setToolTip(infoString);
373     }
374
375     m_standard.profiles_list->setSortingEnabled(true);
376     m_standard.profiles_list->setCurrentRow(0);
377 }
378
379 void Wizard::slotCheckSelectedItem() {
380     // Make sure we always have an item highlighted
381     m_standard.profiles_list->setCurrentRow(m_standard.profiles_list->currentRow());
382 }
383
384
385 void Wizard::adjustSettings() {
386     if (m_extra.installmimes->isChecked()) {
387         QStringList globs;
388         globs << "*.mts" << "*.m2t" << "*.mod" << "*.ts";
389         installExtraMimes("video/mpeg", globs);
390         globs.clear();
391         globs << "*.dv";
392         installExtraMimes("video/dv", globs);
393         runUpdateMimeDatabase();
394     }
395     KdenliveSettings::setAudiothumbnails(m_extra.audiothumbs->isChecked());
396     KdenliveSettings::setVideothumbnails(m_extra.videothumbs->isChecked());
397     KdenliveSettings::setCrashrecovery(m_extra.autosave->isChecked());
398     if (m_standard.profiles_list->currentItem()) {
399         QString selectedProfile = m_profilesInfo.value(m_standard.profiles_list->currentItem()->text());
400         if (selectedProfile.isEmpty()) selectedProfile = "dv_pal";
401         KdenliveSettings::setDefault_profile(selectedProfile);
402     }
403     QString path = m_extra.projectfolder->url().path();
404     if (KStandardDirs::makeDir(path) == false) kDebug() << "/// ERROR CREATING PROJECT FOLDER: " << path;
405     KdenliveSettings::setDefaultprojectfolder(path);
406
407 }
408
409 void Wizard::slotCheckMlt() {
410     QString errorMessage;
411     if (KdenliveSettings::rendererpath().isEmpty()) {
412         errorMessage.append(i18n("your MLT installation cannot be found. Install MLT and restart Kdenlive.\n"));
413     }
414     /*QProcess checkProcess;
415     checkProcess.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "producer");
416     if (!checkProcess.waitForStarted())
417         errorMessage.append(i18n("Error starting MLT's command line player (inigo)") + ".\n");
418
419     checkProcess.waitForFinished();
420
421     QByteArray result = checkProcess.readAllStandardError();
422     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");*/
423
424     QProcess checkProcess2;
425     checkProcess2.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "consumer");
426     if (!checkProcess2.waitForStarted())
427         errorMessage.append(i18n("Error starting MLT's command line player (inigo).") + "\n");
428
429     checkProcess2.waitForFinished();
430
431     QByteArray result = checkProcess2.readAllStandardError();
432     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");
433
434     if (!errorMessage.isEmpty()) {
435         errorMessage.prepend(QString("<b>%1</b><br>").arg(i18n("Fatal Error")));
436         QLabel *pix = new QLabel();
437         pix->setPixmap(KIcon("dialog-error").pixmap(30));
438         QLabel *label = new QLabel(errorMessage);
439         label->setWordWrap(true);
440         m_startLayout->addSpacing(40);
441         m_startLayout->addWidget(pix);
442         m_startLayout->addWidget(label);
443         m_systemCheckIsOk = false;
444         button(QWizard::NextButton)->setEnabled(false);
445     } else m_systemCheckIsOk = true;
446
447     if (m_systemCheckIsOk) checkMltComponents();
448 }
449
450 bool Wizard::isOk() const {
451     return m_systemCheckIsOk;
452 }
453
454 #include "wizard.moc"