]> git.sesse.net Git - kdenlive/blob - src/wizard.cpp
*Add a new page to startup wizard to check presence of important programs (ffplay...
[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 *page2 = new QWizardPage;
48     page2->setTitle(i18n("Video Standard"));
49     m_standard.setupUi(page2);
50
51     // build profiles lists
52     m_profilesInfo = ProfilesDialog::getProfilesInfo();
53     QMap<QString, QString>::const_iterator i = m_profilesInfo.constBegin();
54     while (i != m_profilesInfo.constEnd()) {
55         QMap< QString, QString > profileData = ProfilesDialog::getSettingsFromFile(i.value());
56         if (profileData.value("width") == "720") m_dvProfiles.append(i.key());
57         else if (profileData.value("width").toInt() >= 1080) m_hdvProfiles.append(i.key());
58         else m_otherProfiles.append(i.key());
59         ++i;
60     }
61
62     connect(m_standard.button_all, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
63     connect(m_standard.button_hdv, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
64     connect(m_standard.button_dv, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
65     m_standard.button_all->setChecked(true);
66     connect(m_standard.profiles_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckSelectedItem()));
67
68     // select default profile
69     QList<QListWidgetItem *> profiles = m_standard.profiles_list->findItems(ProfilesDialog::getProfileDescription(KdenliveSettings::default_profile()), Qt::MatchExactly);
70     if (profiles.count() > 0) m_standard.profiles_list->setCurrentItem(profiles.at(0));
71     addPage(page2);
72
73     QWizardPage *page3 = new QWizardPage;
74     page3->setTitle(i18n("Additional Settings"));
75     m_extra.setupUi(page3);
76     m_extra.projectfolder->setMode(KFile::Directory);
77     m_extra.projectfolder->setPath(QDir::homePath() + "/kdenlive");
78     m_extra.videothumbs->setChecked(KdenliveSettings::videothumbnails());
79     m_extra.audiothumbs->setChecked(KdenliveSettings::audiothumbnails());
80     m_extra.autosave->setChecked(KdenliveSettings::crashrecovery());
81     connect(m_extra.videothumbs, SIGNAL(stateChanged(int)), this, SLOT(slotCheckThumbs()));
82     connect(m_extra.audiothumbs, SIGNAL(stateChanged(int)), this, SLOT(slotCheckThumbs()));
83     slotCheckThumbs();
84     addPage(page3);
85
86
87     QWizardPage *page4 = new QWizardPage;
88     page4->setTitle(i18n("Checking system"));
89     m_check.setupUi(page4);
90     slotCheckPrograms();
91     addPage(page4);
92
93     WizardDelegate *listViewDelegate = new WizardDelegate(m_check.programList);
94     m_check.programList->setItemDelegate(listViewDelegate);
95
96     QTimer::singleShot(500, this, SLOT(slotCheckMlt()));
97 }
98
99 void Wizard::slotCheckPrograms() {
100     m_check.programList->setColumnCount(2);
101     m_check.programList->setRootIsDecorated(false);
102     m_check.programList->setHeaderHidden(true);
103     QSize itemSize(20, this->fontMetrics().height() * 2.5);
104     KIcon okIcon("dialog-ok");
105     KIcon missingIcon("dialog-close");
106     m_check.programList->setColumnWidth(0, 30);
107     m_check.programList->setIconSize(QSize(24, 24));
108     QTreeWidgetItem *item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << "FFmpeg & ffplay");
109     item->setData(1, Qt::UserRole, QString("Required for webcam capture"));
110     item->setSizeHint(0, itemSize);
111     QString exepath = KStandardDirs::findExe("ffmpeg");
112     if (exepath.isEmpty()) item->setIcon(0, missingIcon);
113     else if (KStandardDirs::findExe("ffplay").isEmpty()) item->setIcon(0, missingIcon);
114     else item->setIcon(0, okIcon);
115
116     item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << "Recordmydesktop");
117     item->setData(1, Qt::UserRole, QString("Required for screen capture"));
118     item->setSizeHint(0, itemSize);
119     if (KStandardDirs::findExe("recordmydesktop").isEmpty()) item->setIcon(0, missingIcon);
120     else item->setIcon(0, okIcon);
121
122     item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << "Dvgrab");
123     item->setData(1, Qt::UserRole, QString("Required for firewire capture"));
124     item->setSizeHint(0, itemSize);
125     if (KStandardDirs::findExe("dvgrab").isEmpty()) item->setIcon(0, missingIcon);
126     else item->setIcon(0, okIcon);
127
128     item = new QTreeWidgetItem(m_check.programList, QStringList() << QString() << "Inigo");
129     item->setData(1, Qt::UserRole, QString("Required for rendering (part of MLT package)"));
130     item->setSizeHint(0, itemSize);
131     if (KStandardDirs::findExe("inigo").isEmpty()) item->setIcon(0, missingIcon);
132     else item->setIcon(0, okIcon);
133 }
134
135 void Wizard::installExtraMimes(QString baseName, QStringList globs) {
136     QString mimefile = baseName;
137     mimefile.replace('/', '-');
138     KMimeType::Ptr mime = KMimeType::mimeType(baseName);
139     if (!mime) {
140         kDebug() << "KMimeTypeTrader: mimeType " << baseName << " not found";
141     } else {
142         QStringList extensions = mime->patterns();
143         QString comment = mime->comment();
144         foreach(const QString &glob, globs) {
145             if (!extensions.contains(glob)) extensions << glob;
146         }
147         kDebug() << "EXTS: " << extensions;
148         QString packageFileName = KStandardDirs::locateLocal("xdgdata-mime", "packages/" + mimefile + ".xml");
149         kDebug() << "INSTALLING NEW MIME TO: " << packageFileName;
150         QFile packageFile(packageFileName);
151         if (!packageFile.open(QIODevice::WriteOnly)) {
152             kError() << "Couldn't open" << packageFileName << "for writing";
153             return;
154         }
155         QXmlStreamWriter writer(&packageFile);
156         writer.setAutoFormatting(true);
157         writer.writeStartDocument();
158
159         const QString nsUri = "http://www.freedesktop.org/standards/shared-mime-info";
160         writer.writeDefaultNamespace(nsUri);
161         writer.writeStartElement("mime-info");
162         writer.writeStartElement(nsUri, "mime-type");
163         writer.writeAttribute("type", baseName);
164
165         if (!comment.isEmpty()) {
166             writer.writeStartElement(nsUri, "comment");
167             writer.writeCharacters(comment);
168             writer.writeEndElement(); // comment
169         }
170
171         foreach(const QString& pattern, extensions) {
172             writer.writeStartElement(nsUri, "glob");
173             writer.writeAttribute("pattern", pattern);
174             writer.writeEndElement(); // glob
175         }
176
177         writer.writeEndElement(); // mime-info
178         writer.writeEndElement(); // mime-type
179         writer.writeEndDocument();
180     }
181 }
182
183 void Wizard::runUpdateMimeDatabase() {
184     const QString localPackageDir = KStandardDirs::locateLocal("xdgdata-mime", QString());
185     //Q_ASSERT(!localPackageDir.isEmpty());
186     KProcess proc;
187     proc << "update-mime-database";
188     proc << localPackageDir;
189     const int exitCode = proc.execute();
190     if (exitCode) {
191         kWarning() << proc.program() << "exited with error code" << exitCode;
192     }
193 }
194
195 void Wizard::slotCheckThumbs() {
196     QString pixname = "timeline_vthumbs.png";
197     if (!m_extra.audiothumbs->isChecked() && !m_extra.videothumbs->isChecked()) {
198         pixname = "timeline_nothumbs.png";
199     } else if (m_extra.audiothumbs->isChecked()) {
200         if (m_extra.videothumbs->isChecked())
201             pixname = "timeline_avthumbs.png";
202         else pixname = "timeline_athumbs.png";
203     }
204
205     m_extra.timeline_preview->setPixmap(QPixmap(KStandardDirs::locate("appdata", pixname)));
206 }
207
208 void Wizard::slotCheckStandard() {
209     m_standard.profiles_list->clear();
210     QStringList profiles;
211     if (m_standard.button_dv->isChecked()) {
212         // DV standard
213         m_standard.profiles_list->addItems(m_dvProfiles);
214     } else if (m_standard.button_hdv->isChecked()) {
215         // HDV standard
216         m_standard.profiles_list->addItems(m_hdvProfiles);
217     } else {
218         m_standard.profiles_list->addItems(m_dvProfiles);
219         m_standard.profiles_list->addItems(m_hdvProfiles);
220         m_standard.profiles_list->addItems(m_otherProfiles);
221         //m_standard.profiles_list->sortItems();
222     }
223
224     for (int i = 0; i < m_standard.profiles_list->count(); i++) {
225         QListWidgetItem *item = m_standard.profiles_list->item(i);
226         MltVideoProfile prof = ProfilesDialog::getVideoProfile(m_profilesInfo.value(item->text()));
227         const QString infoString = i18n("<b>Frame size:</b>%1x%2<br><b>Frame rate:</b>%3/%4<br><b>Aspect ratio:</b>%5/%6<br><b>Display 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));
228         item->setToolTip(infoString);
229     }
230
231     m_standard.profiles_list->setSortingEnabled(true);
232     m_standard.profiles_list->setCurrentRow(0);
233 }
234
235 void Wizard::slotCheckSelectedItem() {
236     // Make sure we always have an item highlighted
237     m_standard.profiles_list->setCurrentRow(m_standard.profiles_list->currentRow());
238 }
239
240
241 void Wizard::adjustSettings() {
242     if (m_extra.installmimes->isChecked()) {
243         QStringList globs;
244         globs << "*.mts" << "*.m2t" << "*.mod" << "*.ts";
245         installExtraMimes("video/mpeg", globs);
246         globs.clear();
247         globs << "*.dv";
248         installExtraMimes("video/dv", globs);
249         runUpdateMimeDatabase();
250     }
251     KdenliveSettings::setAudiothumbnails(m_extra.audiothumbs->isChecked());
252     KdenliveSettings::setVideothumbnails(m_extra.videothumbs->isChecked());
253     KdenliveSettings::setCrashrecovery(m_extra.autosave->isChecked());
254     if (m_standard.profiles_list->currentItem()) {
255         QString selectedProfile = m_profilesInfo.value(m_standard.profiles_list->currentItem()->text());
256         if (selectedProfile.isEmpty()) selectedProfile = "dv_pal";
257         KdenliveSettings::setDefault_profile(selectedProfile);
258     }
259     QString path = m_extra.projectfolder->url().path();
260     if (KStandardDirs::makeDir(path) == false) kDebug() << "/// ERROR CREATING PROJECT FOLDER: " << path;
261     KdenliveSettings::setDefaultprojectfolder(path);
262
263 }
264
265 void Wizard::slotCheckMlt() {
266     QString errorMessage;
267     if (KdenliveSettings::rendererpath().isEmpty()) {
268         errorMessage.append(i18n("your MLT installation cannot be found. Install MLT and restart Kdenlive.\n"));
269     }
270     QProcess checkProcess;
271     checkProcess.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "producer");
272     if (!checkProcess.waitForStarted())
273         errorMessage.append(i18n("Error starting MLT's command line player (inigo)") + ".\n");
274
275     checkProcess.waitForFinished();
276
277     QByteArray result = checkProcess.readAllStandardError();
278     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");
279
280     QProcess checkProcess2;
281     checkProcess2.start(KdenliveSettings::rendererpath(), QStringList() << "-query" << "consumer");
282     if (!checkProcess2.waitForStarted())
283         errorMessage.append(i18n("Error starting MLT's command line player (inigo).") + "\n");
284
285     checkProcess2.waitForFinished();
286
287     result = checkProcess2.readAllStandardError();
288     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");
289
290     if (!errorMessage.isEmpty()) {
291         errorMessage.prepend(QString("<b>%1</b><br>").arg(i18n("Fatal Error")));
292         QLabel *pix = new QLabel();
293         pix->setPixmap(KIcon("dialog-error").pixmap(30));
294         QLabel *label = new QLabel(errorMessage);
295         label->setWordWrap(true);
296         m_startLayout->addSpacing(40);
297         m_startLayout->addWidget(pix);
298         m_startLayout->addWidget(label);
299         m_systemCheckIsOk = false;
300         button(QWizard::NextButton)->setEnabled(false);
301     } else m_systemCheckIsOk = true;
302 }
303
304 bool Wizard::isOk() const {
305     return m_systemCheckIsOk;
306 }
307
308 #include "wizard.moc"