]> git.sesse.net Git - kdenlive/blob - src/kdenlivesettingsdialog.cpp
Complete rewrite of the video4linux capture to use MLT, in progress.
[kdenlive] / src / kdenlivesettingsdialog.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 "kdenlivesettingsdialog.h"
21 #include "profilesdialog.h"
22 #include "v4l/v4lcapture.h"
23 #include "blackmagic/devices.h"
24 #include "kdenlivesettings.h"
25
26 #include <KStandardDirs>
27 #include <KDebug>
28 #include <kopenwithdialog.h>
29 #include <KConfigDialogManager>
30 #include <kde_file.h>
31 #include <KIO/NetAccess>
32 #include <kdeversion.h>
33
34 #include <QDir>
35 #include <QTimer>
36 #include <QTreeWidgetItem>
37 #include <QThread>
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #ifndef NO_JOGSHUTTLE
44 #include "jogaction.h"
45 #include "jogshuttleconfig.h"
46 #include <linux/input.h>
47 #endif /* NO_JOGSHUTTLE */
48
49
50 KdenliveSettingsDialog::KdenliveSettingsDialog(const QMap<QString, QString>& mappable_actions, QWidget * parent) :
51     KConfigDialog(parent, "settings", KdenliveSettings::self()),
52     m_modified(false),
53     m_shuttleModified(false),
54     m_mappable_actions(mappable_actions)
55 {
56     KdenliveSettings::setV4l_format(0);
57     QWidget *p1 = new QWidget;
58     m_configMisc.setupUi(p1);
59     m_page1 = addPage(p1, i18n("Misc"), "configure");
60
61     // Hide multi tab option until Kdenlive really supports it
62     m_configMisc.kcfg_activatetabs->setVisible(false);
63
64     QWidget *p8 = new QWidget;
65     m_configProject.setupUi(p8);
66     m_page8 = addPage(p8, i18n("Project Defaults"), "document-new");
67     connect(m_configProject.kcfg_generateproxy, SIGNAL(toggled(bool)), m_configProject.kcfg_proxyminsize, SLOT(setEnabled(bool)));
68     m_configProject.kcfg_proxyminsize->setEnabled(KdenliveSettings::generateproxy());
69     connect(m_configProject.kcfg_generateimageproxy, SIGNAL(toggled(bool)), m_configProject.kcfg_proxyimageminsize, SLOT(setEnabled(bool)));
70     m_configProject.kcfg_proxyimageminsize->setEnabled(KdenliveSettings::generateimageproxy());
71     m_configProject.kcfg_proxyparams->setMaximumHeight(QFontMetrics(font()).lineSpacing() * 5);
72
73     QWidget *p3 = new QWidget;
74     m_configTimeline.setupUi(p3);
75     m_page3 = addPage(p3, i18n("Timeline"), "video-display");
76
77     QWidget *p2 = new QWidget;
78     m_configEnv.setupUi(p2);
79     m_configEnv.mltpathurl->setMode(KFile::Directory);
80     m_configEnv.mltpathurl->lineEdit()->setObjectName("kcfg_mltpath");
81     m_configEnv.rendererpathurl->lineEdit()->setObjectName("kcfg_rendererpath");
82     m_configEnv.kcfg_mltthreads->setMaximum(QThread::idealThreadCount());
83     m_configEnv.tmppathurl->setMode(KFile::Directory);
84     m_configEnv.tmppathurl->lineEdit()->setObjectName("kcfg_currenttmpfolder");
85     m_configEnv.projecturl->setMode(KFile::Directory);
86     m_configEnv.projecturl->lineEdit()->setObjectName("kcfg_defaultprojectfolder");
87     m_configEnv.capturefolderurl->setMode(KFile::Directory);
88     m_configEnv.capturefolderurl->lineEdit()->setObjectName("kcfg_capturefolder");
89     m_configEnv.capturefolderurl->setEnabled(!KdenliveSettings::capturetoprojectfolder());
90     connect(m_configEnv.kcfg_capturetoprojectfolder, SIGNAL(clicked()), this, SLOT(slotEnableCaptureFolder()));
91     m_page2 = addPage(p2, i18n("Environment"), "application-x-executable-script");
92
93     QWidget *p4 = new QWidget;
94     m_configCapture.setupUi(p4);
95
96 #if !defined(Q_WS_MAC) && !defined(Q_OS_FREEBSD)
97     m_configCapture.kcfg_v4l_parameters->setMaximumHeight(QFontMetrics(font()).lineSpacing() * 4);
98
99     // Video 4 Linux device detection
100     for (int i = 0; i < 10; i++) {
101         QString path = "/dev/video" + QString::number(i);
102         if (QFile::exists(path)) {
103             QStringList deviceInfo = V4lCaptureHandler::getDeviceName(path.toUtf8().constData());
104             if (!deviceInfo.isEmpty()) {
105                 m_configCapture.kcfg_detectedv4ldevices->addItem(deviceInfo.at(0), path);
106                 m_configCapture.kcfg_detectedv4ldevices->setItemData(m_configCapture.kcfg_detectedv4ldevices->count() - 1, deviceInfo.at(1), Qt::UserRole + 1);
107             }
108         }
109     }
110     connect(m_configCapture.kcfg_detectedv4ldevices, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdatev4lDevice()));
111     connect(m_configCapture.kcfg_v4l_format, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdatev4lCaptureProfile()));
112     connect(m_configCapture.kcfg_v4l_captureaudio, SIGNAL(toggled(bool)), m_configCapture.kcfg_v4l_alsadevice, SLOT(setEnabled(bool)));
113
114     slotUpdatev4lDevice();
115 #endif
116
117     m_page4 = addPage(p4, i18n("Capture"), "media-record");
118     m_configCapture.tabWidget->setCurrentIndex(KdenliveSettings::defaultcapture());
119 #ifdef Q_WS_MAC
120     m_configCapture.tabWidget->setEnabled(false);
121     m_configCapture.kcfg_defaultcapture->setEnabled(false);
122     m_configCapture.label->setText(i18n("Capture is not yet available on OS X."));
123 #endif
124
125     QWidget *p5 = new QWidget;
126     m_configShuttle.setupUi(p5);
127 #ifdef NO_JOGSHUTTLE
128     m_configShuttle.kcfg_enableshuttle->hide();
129     m_configShuttle.kcfg_enableshuttle->setDisabled(true);
130 #else
131     connect(m_configShuttle.kcfg_enableshuttle, SIGNAL(stateChanged(int)), this, SLOT(slotCheckShuttle(int)));
132     connect(m_configShuttle.shuttledevicelist, SIGNAL(activated(int)), this, SLOT(slotUpdateShuttleDevice(int)));
133     slotCheckShuttle(KdenliveSettings::enableshuttle());
134     m_configShuttle.shuttledisabled->hide();
135
136     // Store the button pointers into an array for easier handling them in the other functions.
137     m_shuttle_buttons.push_back(m_configShuttle.shuttle1);
138     m_shuttle_buttons.push_back(m_configShuttle.shuttle2);
139     m_shuttle_buttons.push_back(m_configShuttle.shuttle3);
140     m_shuttle_buttons.push_back(m_configShuttle.shuttle4);
141     m_shuttle_buttons.push_back(m_configShuttle.shuttle5);
142
143     // populate the buttons with the current configuration. The items are sorted
144     // according to the user-selected language, so they do not appear in random order.
145     QList<QString> action_names = mappable_actions.keys();
146     qSort(action_names);
147
148     // Here we need to compute the action_id -> index-in-action_names. We iterate over the
149     // action_names, as the sorting may depend on the user-language.
150     QStringList actions_map = JogShuttleConfig::actionMap(KdenliveSettings::shuttlebuttons());
151     QMap<QString, int> action_pos;
152     foreach (const QString& action_id, actions_map) {
153       // This loop find out at what index is the string that would map to the action_id.
154       for (int i = 0; i < action_names.size(); i++) {
155           if (mappable_actions[action_names[i]] == action_id) {
156               action_pos[action_id] = i;
157               break;
158           }
159       }
160     }
161
162     int i = 0;
163     foreach (KComboBox* button, m_shuttle_buttons) {
164       button->addItems(action_names);
165       connect(button, SIGNAL(activated(int)), this, SLOT(slotShuttleModified()));
166       ++i;
167       if (i < actions_map.size())
168         button->setCurrentIndex(action_pos[actions_map[i]]);
169     }
170 #endif /* NO_JOGSHUTTLE */
171     m_page5 = addPage(p5, i18n("JogShuttle"), "input-mouse");
172
173     QWidget *p6 = new QWidget;
174     m_configSdl.setupUi(p6);
175
176 #if not defined(Q_WS_MAC) && not defined(USE_OPEN_GL)
177     m_configSdl.kcfg_openglmonitors->setHidden(true);
178 #endif
179
180     m_page6 = addPage(p6, i18n("Playback"), "media-playback-start");
181
182     QWidget *p7 = new QWidget;
183     m_configTranscode.setupUi(p7);
184     m_page7 = addPage(p7, i18n("Transcode"), "edit-copy");
185     connect(m_configTranscode.button_add, SIGNAL(clicked()), this, SLOT(slotAddTranscode()));
186     connect(m_configTranscode.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteTranscode()));
187     connect(m_configTranscode.profiles_list, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(slotDialogModified()));
188     
189     connect(m_configCapture.kcfg_rmd_capture_audio, SIGNAL(clicked(bool)), m_configCapture.audio_group, SLOT(setVisible(bool)));
190
191     m_configCapture.audio_group->setVisible(KdenliveSettings::rmd_capture_audio());
192
193     connect(m_configEnv.kp_image, SIGNAL(clicked()), this, SLOT(slotEditImageApplication()));
194     connect(m_configEnv.kp_audio, SIGNAL(clicked()), this, SLOT(slotEditAudioApplication()));
195     connect(m_configEnv.kp_player, SIGNAL(clicked()), this, SLOT(slotEditVideoApplication()));
196
197     checkProfile();
198
199     slotUpdateDisplay();
200
201     connect(m_configSdl.kcfg_audio_driver, SIGNAL(currentIndexChanged(int)), this, SLOT(slotCheckAlsaDriver()));
202     initDevices();
203     connect(m_configProject.kcfg_profiles_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateDisplay()));
204     connect(m_configCapture.kcfg_rmd_capture_type, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateRmdRegionStatus()));
205
206     slotUpdateRmdRegionStatus();
207     loadTranscodeProfiles();
208
209
210     //HACK: check dvgrab version, because only dvgrab >= 3.3 supports
211     //   --timestamp option without bug
212
213     if (KdenliveSettings::dvgrab_path().isEmpty() || !QFile::exists(KdenliveSettings::dvgrab_path())) {
214         QString dvgrabpath = KStandardDirs::findExe("dvgrab");
215         KdenliveSettings::setDvgrab_path(dvgrabpath);
216     }
217
218
219     BMInterface::getBlackMagicDeviceList(m_configCapture.kcfg_hdmi_capturedevice, m_configCapture.kcfg_hdmi_capturemode);
220     connect(m_configCapture.kcfg_hdmi_capturedevice, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateHDMIModes()));
221
222     if (BMInterface::getBlackMagicOutputDeviceList(m_configSdl.kcfg_blackmagic_output_device)) {
223         // Found blackmagic card
224     } else m_configSdl.kcfg_external_display->setEnabled(false);
225
226     double dvgrabVersion = 0;
227     if (!KdenliveSettings::dvgrab_path().isEmpty()) {
228         QProcess *versionCheck = new QProcess;
229         versionCheck->setProcessChannelMode(QProcess::MergedChannels);
230         versionCheck->start("dvgrab", QStringList() << "--version");
231         if (versionCheck->waitForFinished()) {
232             QString version = QString(versionCheck->readAll()).simplified();
233             if (version.contains(' ')) version = version.section(' ', -1);
234             dvgrabVersion = version.toDouble();
235
236             kDebug() << "// FOUND DVGRAB VERSION: " << dvgrabVersion;
237         }
238         delete versionCheck;
239         if (dvgrabVersion < 3.3) {
240             KdenliveSettings::setFirewiretimestamp(false);
241             m_configCapture.kcfg_firewiretimestamp->setEnabled(false);
242         }
243         m_configCapture.dvgrab_info->setText(i18n("dvgrab version %1 at %2", dvgrabVersion, KdenliveSettings::dvgrab_path()));
244     } else m_configCapture.dvgrab_info->setText(i18n("<strong><em>dvgrab</em> utility not found, please install it for firewire capture</strong>"));
245
246     if (KdenliveSettings::rmd_path().isEmpty() || !QFile::exists(KdenliveSettings::rmd_path())) {
247         QString rmdpath = KStandardDirs::findExe("recordmydesktop");
248         KdenliveSettings::setRmd_path(rmdpath);
249     }
250     if (KdenliveSettings::rmd_path().isEmpty())
251         m_configCapture.rmd_info->setText(i18n("<strong><em>Recordmydesktop</em> utility not found, please install it for screen grabs</strong>"));
252     else
253         m_configCapture.rmd_info->setText(i18n("Recordmydesktop found at: %1", KdenliveSettings::rmd_path()));
254 }
255
256 KdenliveSettingsDialog::~KdenliveSettingsDialog() {}
257
258 void KdenliveSettingsDialog::slotUpdateHDMIModes()
259 {
260     QStringList modes = m_configCapture.kcfg_hdmi_capturedevice->itemData(m_configCapture.kcfg_hdmi_capturedevice->currentIndex()).toStringList();
261     m_configCapture.kcfg_hdmi_capturemode->clear();
262     m_configCapture.kcfg_hdmi_capturemode->insertItems(0, modes);
263 }
264
265 void KdenliveSettingsDialog::slotUpdateRmdRegionStatus()
266 {
267     m_configCapture.region_group->setHidden(m_configCapture.kcfg_rmd_capture_type->currentIndex() != 1);
268 }
269
270 void KdenliveSettingsDialog::slotEnableCaptureFolder()
271 {
272     m_configEnv.capturefolderurl->setEnabled(!m_configEnv.kcfg_capturetoprojectfolder->isChecked());
273 }
274
275 void KdenliveSettingsDialog::checkProfile()
276 {
277     m_configProject.kcfg_profiles_list->clear();
278     QMap <QString, QString> profilesInfo = ProfilesDialog::getProfilesInfo();
279     QMapIterator<QString, QString> i(profilesInfo);
280     while (i.hasNext()) {
281         i.next();
282         m_configProject.kcfg_profiles_list->addItem(i.key(), i.value());
283     }
284
285     if (!KdenliveSettings::default_profile().isEmpty()) {
286         for (int i = 0; i < m_configProject.kcfg_profiles_list->count(); i++) {
287             if (m_configProject.kcfg_profiles_list->itemData(i).toString() == KdenliveSettings::default_profile()) {
288                 m_configProject.kcfg_profiles_list->setCurrentIndex(i);
289                 KdenliveSettings::setProfiles_list(i);
290                 break;
291             }
292         }
293     }
294 }
295
296 void KdenliveSettingsDialog::initDevices()
297 {
298     // Fill audio drivers
299     m_configSdl.kcfg_audio_driver->addItem(i18n("Automatic"), QString());
300 #ifndef Q_WS_MAC
301     m_configSdl.kcfg_audio_driver->addItem(i18n("OSS"), "dsp");
302     m_configSdl.kcfg_audio_driver->addItem(i18n("ALSA"), "alsa");
303     m_configSdl.kcfg_audio_driver->addItem(i18n("PulseAudio"), "pulse");
304     m_configSdl.kcfg_audio_driver->addItem(i18n("OSS with DMA access"), "dma");
305     m_configSdl.kcfg_audio_driver->addItem(i18n("Esound daemon"), "esd");
306     m_configSdl.kcfg_audio_driver->addItem(i18n("ARTS daemon"), "artsc");
307 #endif
308
309     if (!KdenliveSettings::audiodrivername().isEmpty())
310         for (int i = 1; i < m_configSdl.kcfg_audio_driver->count(); i++) {
311             if (m_configSdl.kcfg_audio_driver->itemData(i).toString() == KdenliveSettings::audiodrivername()) {
312                 m_configSdl.kcfg_audio_driver->setCurrentIndex(i);
313                 KdenliveSettings::setAudio_driver((uint) i);
314             }
315         }
316
317     // Fill video drivers
318     m_configSdl.kcfg_video_driver->addItem(i18n("Automatic"), QString());
319 #ifndef Q_WS_MAC
320     m_configSdl.kcfg_video_driver->addItem(i18n("XVideo"), "x11");
321     m_configSdl.kcfg_video_driver->addItem(i18n("X11"), "x11_noaccel");
322     m_configSdl.kcfg_video_driver->addItem(i18n("XFree86 DGA 2.0"), "dga");
323     m_configSdl.kcfg_video_driver->addItem(i18n("Nano X"), "nanox");
324     m_configSdl.kcfg_video_driver->addItem(i18n("Framebuffer console"), "fbcon");
325     m_configSdl.kcfg_video_driver->addItem(i18n("Direct FB"), "directfb");
326     m_configSdl.kcfg_video_driver->addItem(i18n("SVGAlib"), "svgalib");
327     m_configSdl.kcfg_video_driver->addItem(i18n("General graphics interface"), "ggi");
328     m_configSdl.kcfg_video_driver->addItem(i18n("Ascii art library"), "aalib");
329 #endif
330
331     // Fill the list of audio playback devices
332     m_configSdl.kcfg_audio_device->addItem(i18n("Default"), QString());
333     m_configCapture.kcfg_rmd_alsa_device->addItem(i18n("Default"), QString());
334     if (!KStandardDirs::findExe("aplay").isEmpty()) {
335         m_readProcess.setOutputChannelMode(KProcess::OnlyStdoutChannel);
336         m_readProcess.setProgram("aplay", QStringList() << "-l");
337         connect(&m_readProcess, SIGNAL(readyReadStandardOutput()) , this, SLOT(slotReadAudioDevices()));
338         m_readProcess.execute(5000);
339     } else {
340         // If aplay is not installed on the system, parse the /proc/asound/pcm file
341         QFile file("/proc/asound/pcm");
342         if (file.open(QIODevice::ReadOnly)) {
343             QTextStream stream(&file);
344             QString line;
345             QString deviceId;
346             while (!stream.atEnd()) {
347                 line = stream.readLine();
348                 if (line.contains("playback")) {
349                     deviceId = line.section(':', 0, 0);
350                     m_configSdl.kcfg_audio_device->addItem(line.section(':', 1, 1), "plughw:" + QString::number(deviceId.section('-', 0, 0).toInt()) + ',' + QString::number(deviceId.section('-', 1, 1).toInt()));
351                 }
352                 if (line.contains("capture")) {
353                     deviceId = line.section(':', 0, 0);
354                     m_configCapture.kcfg_rmd_alsa_device->addItem(line.section(':', 1, 1), "plughw:" + QString::number(deviceId.section('-', 0, 0).toInt()) + ',' + QString::number(deviceId.section('-', 1, 1).toInt()));
355                     m_configCapture.kcfg_v4l_alsadevice->addItem(line.section(':', 1, 1), "hw:" + QString::number(deviceId.section('-', 0, 0).toInt()) + ',' + QString::number(deviceId.section('-', 1, 1).toInt()));
356                 }
357             }
358             file.close();
359         }
360     }
361     if (!KdenliveSettings::audiodevicename().isEmpty()) {
362         // Select correct alsa device
363         int ix = m_configSdl.kcfg_audio_device->findData(KdenliveSettings::audiodevicename());
364         m_configSdl.kcfg_audio_device->setCurrentIndex(ix);
365         KdenliveSettings::setAudio_device(ix);
366     }
367     
368     if (!KdenliveSettings::rmd_alsadevicename().isEmpty()) {
369         // Select correct alsa device
370         int ix = m_configCapture.kcfg_rmd_alsa_device->findData(KdenliveSettings::rmd_alsadevicename());
371         m_configCapture.kcfg_rmd_alsa_device->setCurrentIndex(ix);
372         KdenliveSettings::setRmd_alsa_device(ix);
373     }
374
375     if (!KdenliveSettings::v4l_alsadevicename().isEmpty()) {
376         // Select correct alsa device
377         int ix = m_configCapture.kcfg_v4l_alsadevice->findData(KdenliveSettings::v4l_alsadevicename());
378         m_configCapture.kcfg_v4l_alsadevice->setCurrentIndex(ix);
379         KdenliveSettings::setV4l_alsadevice(ix);
380     }
381
382     loadCurrentV4lProfileInfo();
383 }
384
385
386 void KdenliveSettingsDialog::slotReadAudioDevices()
387 {
388     QString result = QString(m_readProcess.readAllStandardOutput());
389     kDebug() << "// / / / / / READING APLAY: ";
390     kDebug() << result;
391     QStringList lines = result.split('\n');
392     foreach(const QString & data, lines) {
393         kDebug() << "// READING LINE: " << data;
394         if (data.simplified().startsWith("card")) {
395             QString card = data.section(':', 0, 0).section(' ', -1);
396             QString device = data.section(':', 1, 1).section(' ', -1);
397             m_configSdl.kcfg_audio_device->addItem(data.section(':', -1), "plughw:" + card + ',' + device);
398             m_configCapture.kcfg_rmd_alsa_device->addItem(data.section(':', -1), "plughw:" + card + ',' + device);
399             m_configCapture.kcfg_v4l_alsadevice->addItem(data.section(':', -1), "hw:" + card + ',' + device);
400         }
401     }
402 }
403
404 void KdenliveSettingsDialog::showPage(int page, int option)
405 {
406     switch (page) {
407     case 1:
408         setCurrentPage(m_page1);
409         break;
410     case 2:
411         setCurrentPage(m_page2);
412         break;
413     case 3:
414         setCurrentPage(m_page3);
415         break;
416     case 4:
417         setCurrentPage(m_page4);
418         m_configCapture.tabWidget->setCurrentIndex(option);
419         break;
420     case 5:
421         setCurrentPage(m_page5);
422         break;
423     case 6:
424         setCurrentPage(m_page6);
425         break;
426     case 7:
427         setCurrentPage(m_page7);
428         break;
429     default:
430         setCurrentPage(m_page1);
431     }
432 }
433
434 void KdenliveSettingsDialog::slotEditVideoApplication()
435 {
436     KService::Ptr service;
437     KOpenWithDialog dlg(KUrl::List(), i18n("Select default video player"), m_configEnv.kcfg_defaultplayerapp->text(), this);
438     if (dlg.exec() != QDialog::Accepted)
439         return;
440
441     service = dlg.service();
442     m_configEnv.kcfg_defaultplayerapp->setText(service->exec());
443 }
444
445 void KdenliveSettingsDialog::slotEditAudioApplication()
446 {
447     KService::Ptr service;
448     KOpenWithDialog dlg(KUrl::List(), i18n("Select default audio editor"), m_configEnv.kcfg_defaultaudioapp->text(), this);
449     if (dlg.exec() != QDialog::Accepted)
450         return;
451
452     service = dlg.service();
453     m_configEnv.kcfg_defaultaudioapp->setText(service->exec());
454 }
455
456 void KdenliveSettingsDialog::slotEditImageApplication()
457 {
458     KService::Ptr service;
459     KOpenWithDialog dlg(KUrl::List(), i18n("Select default image editor"), m_configEnv.kcfg_defaultimageapp->text(), this);
460     if (dlg.exec() != QDialog::Accepted)
461         return;
462
463     service = dlg.service();
464     m_configEnv.kcfg_defaultimageapp->setText(service->exec());
465 }
466
467 #ifndef NO_JOGSHUTTLE
468 void KdenliveSettingsDialog::slotCheckShuttle(int state)
469 {
470     m_configShuttle.config_group->setEnabled(state);
471     if (m_configShuttle.shuttledevicelist->count() == 0) {
472         // parse devices
473         QString baseName = "/dev/input/event";
474         int fd;
475         for (int i = 0; i < 30; i++) {
476             QString filename = baseName + QString::number(i);
477             kDebug() << "/// CHECKING OFR: " << filename;
478
479             char name[256] = "unknown";
480             fd = KDE_open((char *) filename.toUtf8().data(), O_RDONLY);
481             if (fd >= 0 && ioctl(fd, EVIOCGNAME(sizeof(name)), name) >= 0) {
482                 m_configShuttle.shuttledevicelist->addItem(name, filename);
483             }
484             ::close(fd);
485         }
486         if (KdenliveSettings::shuttledevice().isEmpty()) QTimer::singleShot(1500, this, SLOT(slotUpdateShuttleDevice()));
487     }
488 }
489
490 void KdenliveSettingsDialog::slotUpdateShuttleDevice(int ix)
491 {
492     QString device = m_configShuttle.shuttledevicelist->itemData(ix).toString();
493     //KdenliveSettings::setShuttledevice(device);
494     m_configShuttle.kcfg_shuttledevice->setText(device);
495 }
496
497 #endif /* NO_JOGSHUTTLE */
498
499 void KdenliveSettingsDialog::updateWidgets()
500 {
501     // Revert widgets to last saved state (for example when user pressed "Cancel")
502     // kDebug() << "// // // KCONFIG Revert called";
503 #ifndef NO_JOGSHUTTLE
504     // revert jog shuttle device
505     if (m_configShuttle.shuttledevicelist->count() > 0) {
506         for (int i = 0; i < m_configShuttle.shuttledevicelist->count(); i++) {
507             if (m_configShuttle.shuttledevicelist->itemData(i) == KdenliveSettings::shuttledevice()) {
508                 m_configShuttle.shuttledevicelist->setCurrentIndex(i);
509                 break;
510             }
511         }
512     }
513
514     // Revert jog shuttle buttons
515     QList<QString> action_names = m_mappable_actions.keys();
516     qSort(action_names);
517     QStringList actions_map = JogShuttleConfig::actionMap(KdenliveSettings::shuttlebuttons());
518     QMap<QString, int> action_pos;
519     foreach (const QString& action_id, actions_map) {
520       // This loop find out at what index is the string that would map to the action_id.
521       for (int i = 0; i < action_names.size(); i++) {
522           if (m_mappable_actions[action_names[i]] == action_id) {
523               action_pos[action_id] = i;
524               break;
525           }
526       }
527     }
528     int i = 0;
529     foreach (KComboBox* button, m_shuttle_buttons) {
530       ++i;
531       if (i < actions_map.size())
532         button->setCurrentIndex(action_pos[actions_map[i]]);
533     }
534 #endif
535 }
536
537 void KdenliveSettingsDialog::updateSettings()
538 {
539     // Save changes to settings (for example when user pressed "Apply" or "Ok")
540     // kDebug() << "// // // KCONFIG UPDATE called";
541     m_defaultProfile = m_configProject.kcfg_profiles_list->currentText();
542     KdenliveSettings::setDefault_profile(m_defaultPath);
543
544     bool resetProfile = false;
545     bool updateCapturePath = false;
546     
547     /*if (m_configShuttle.shuttledevicelist->count() > 0) {
548         QString device = m_configShuttle.shuttledevicelist->itemData(m_configShuttle.shuttledevicelist->currentIndex()).toString();
549         if (device != KdenliveSettings::shuttledevice()) KdenliveSettings::setShuttledevice(device);
550     }*/
551
552     if (m_configEnv.kcfg_capturetoprojectfolder->isChecked() != KdenliveSettings::capturetoprojectfolder()) {
553         KdenliveSettings::setCapturetoprojectfolder(m_configEnv.kcfg_capturetoprojectfolder->isChecked());
554         updateCapturePath = true;
555     }
556
557     if (m_configEnv.capturefolderurl->url().path() != KdenliveSettings::capturefolder()) {
558         KdenliveSettings::setCapturefolder(m_configEnv.capturefolderurl->url().path());
559         updateCapturePath = true;
560     }
561
562     if (m_configCapture.kcfg_dvgrabfilename->text() != KdenliveSettings::dvgrabfilename()) {
563         KdenliveSettings::setDvgrabfilename(m_configCapture.kcfg_dvgrabfilename->text());
564         updateCapturePath = true;
565     }
566
567     if ((uint) m_configCapture.kcfg_firewireformat->currentIndex() != KdenliveSettings::firewireformat()) {
568         KdenliveSettings::setFirewireformat(m_configCapture.kcfg_firewireformat->currentIndex());
569         updateCapturePath = true;
570     }
571
572     if ((uint) m_configCapture.kcfg_v4l_format->currentIndex() != KdenliveSettings::v4l_format()) {
573         saveCurrentV4lProfile();
574         KdenliveSettings::setV4l_format(0);
575     }
576     
577
578     if (updateCapturePath) emit updateCaptureFolder();
579
580     QString value = m_configCapture.kcfg_rmd_alsa_device->itemData(m_configCapture.kcfg_rmd_alsa_device->currentIndex()).toString();
581     if (value != KdenliveSettings::rmd_alsadevicename()) {
582         KdenliveSettings::setRmd_alsadevicename(value);
583     }
584
585     value = m_configCapture.kcfg_v4l_alsadevice->itemData(m_configCapture.kcfg_v4l_alsadevice->currentIndex()).toString();
586     if (value != KdenliveSettings::v4l_alsadevicename()) {
587         KdenliveSettings::setV4l_alsadevicename(value);
588     }
589
590     value = m_configCapture.kcfg_rmd_audio_freq->itemText(m_configCapture.kcfg_rmd_audio_freq->currentIndex());
591     kDebug() << "// AUDIO FREQ VALUE: " << value << ", CURRENT: " << KdenliveSettings::rmd_freq() << ", IX: " << m_configCapture.kcfg_rmd_audio_freq->currentIndex();
592     if (value != KdenliveSettings::rmd_freq()) {
593         kDebug() << "// SETTING AUDIO FREQ TO: " << value;
594         KdenliveSettings::setRmd_freq(value);
595     }
596
597     if (m_configSdl.kcfg_external_display->isChecked() != KdenliveSettings::external_display()) {
598         KdenliveSettings::setExternal_display(m_configSdl.kcfg_external_display->isChecked());
599         resetProfile = true;
600     }
601
602     value = m_configSdl.kcfg_audio_driver->itemData(m_configSdl.kcfg_audio_driver->currentIndex()).toString();
603     if (value != KdenliveSettings::audiodrivername()) {
604         KdenliveSettings::setAudiodrivername(value);
605         resetProfile = true;
606     }
607
608     if (value == "alsa") {
609         // Audio device setting is only valid for alsa driver
610         value = m_configSdl.kcfg_audio_device->itemData(m_configSdl.kcfg_audio_device->currentIndex()).toString();
611         if (value != KdenliveSettings::audiodevicename()) {
612             KdenliveSettings::setAudiodevicename(value);
613             resetProfile = true;
614         }
615     } else if (KdenliveSettings::audiodevicename().isEmpty() == false) {
616         KdenliveSettings::setAudiodevicename(QString::null);
617         resetProfile = true;
618     }
619
620     value = m_configSdl.kcfg_video_driver->itemData(m_configSdl.kcfg_video_driver->currentIndex()).toString();
621     if (value != KdenliveSettings::videodrivername()) {
622         KdenliveSettings::setVideodrivername(value);
623         resetProfile = true;
624     }
625
626     if (m_configSdl.kcfg_window_background->color() != KdenliveSettings::window_background()) {
627         KdenliveSettings::setWindow_background(m_configSdl.kcfg_window_background->color());
628         resetProfile = true;
629     }
630
631     if (m_configSdl.kcfg_volume->value() != KdenliveSettings::volume()) {
632         KdenliveSettings::setVolume(m_configSdl.kcfg_volume->value());
633         resetProfile = true;
634     }
635     
636     if (m_configProject.kcfg_enableproxy->isChecked() != KdenliveSettings::enableproxy()) {
637         KdenliveSettings::setEnableproxy(m_configProject.kcfg_enableproxy->isChecked());
638     }
639     
640     if (m_configProject.kcfg_generateproxy->isChecked() != KdenliveSettings::generateproxy()) {
641         KdenliveSettings::setGenerateproxy(m_configProject.kcfg_generateproxy->isChecked());
642     }
643     
644     if (m_configProject.kcfg_proxyminsize->value() != KdenliveSettings::proxyminsize()) {
645         KdenliveSettings::setProxyminsize(m_configProject.kcfg_proxyminsize->value());
646     }
647
648     if (m_modified) {
649         // The transcoding profiles were modified, save.
650         m_modified = false;
651         saveTranscodeProfiles();
652     }
653
654 #ifndef NO_JOGSHUTTLE
655     m_shuttleModified = false;
656
657     QStringList actions;
658     actions << "monitor_pause";  // the Job rest position action.
659     foreach (KComboBox* button, m_shuttle_buttons) {
660         actions << m_mappable_actions[button->currentText()];
661     }
662     QString maps = JogShuttleConfig::actionMap(actions);
663     //fprintf(stderr, "Shuttle config: %s\n", JogShuttleConfig::actionMap(actions).toAscii().constData());
664     if (KdenliveSettings::shuttlebuttons() != maps)
665         KdenliveSettings::setShuttlebuttons(maps);
666 #endif
667
668 #if KDE_IS_VERSION(4,3,0)
669     KConfigDialog::settingsChangedSlot();
670 #endif
671
672     //KConfigDialog::updateSettings();
673     if (resetProfile) emit doResetProfile();
674 }
675
676 void KdenliveSettingsDialog::slotUpdateDisplay()
677 {
678     QString currentProfile = m_configProject.kcfg_profiles_list->itemData(m_configProject.kcfg_profiles_list->currentIndex()).toString();
679     QMap< QString, QString > values = ProfilesDialog::getSettingsFromFile(currentProfile);
680     m_configProject.p_size->setText(values.value("width") + 'x' + values.value("height"));
681     m_configProject.p_fps->setText(values.value("frame_rate_num") + '/' + values.value("frame_rate_den"));
682     m_configProject.p_aspect->setText(values.value("sample_aspect_num") + '/' + values.value("sample_aspect_den"));
683     m_configProject.p_display->setText(values.value("display_aspect_num") + '/' + values.value("display_aspect_den"));
684     if (values.value("progressive").toInt() == 0)
685         m_configProject.p_progressive->setText(i18n("Interlaced"));
686     else
687         m_configProject.p_progressive->setText(i18n("Progressive"));
688     m_defaultProfile = m_configProject.kcfg_profiles_list->itemText(m_configProject.kcfg_profiles_list->currentIndex());
689     m_defaultPath = currentProfile;
690 }
691
692 void KdenliveSettingsDialog::slotCheckAlsaDriver()
693 {
694     QString value = m_configSdl.kcfg_audio_driver->itemData(m_configSdl.kcfg_audio_driver->currentIndex()).toString();
695     m_configSdl.kcfg_audio_device->setEnabled(value == "alsa");
696 }
697
698 void KdenliveSettingsDialog::loadTranscodeProfiles()
699 {
700     KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
701     KConfigGroup transConfig(config, "Transcoding");
702     // read the entries
703     m_configTranscode.profiles_list->blockSignals(true);
704     QMap< QString, QString > profiles = transConfig.entryMap();
705     QMapIterator<QString, QString> i(profiles);
706     while (i.hasNext()) {
707         i.next();
708         QTreeWidgetItem *item = new QTreeWidgetItem(m_configTranscode.profiles_list, QStringList() << i.key() << i.value());
709         item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
710     }
711     m_configTranscode.profiles_list->blockSignals(false);
712 }
713
714 void KdenliveSettingsDialog::saveTranscodeProfiles()
715 {
716     KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
717     //KSharedConfigPtr config = KGlobal::config();
718     KConfigGroup transConfig(config, "Transcoding");
719     // read the entries
720     transConfig.deleteGroup();
721     int max = m_configTranscode.profiles_list->topLevelItemCount();
722     for (int i = 0; i < max; i++) {
723         QTreeWidgetItem *item = m_configTranscode.profiles_list->topLevelItem(i);
724         transConfig.writeEntry(item->text(0), item->text(1));
725     }
726     config->sync();
727 }
728
729 void KdenliveSettingsDialog::slotAddTranscode()
730 {
731     QTreeWidgetItem *item = new QTreeWidgetItem(m_configTranscode.profiles_list, QStringList() << i18n("Name") << i18n("Parameters"));
732     item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
733     m_configTranscode.profiles_list->setCurrentItem(item);
734     m_configTranscode.profiles_list->editItem(item);
735     slotDialogModified();
736 }
737
738 void KdenliveSettingsDialog::slotDeleteTranscode()
739 {
740     QTreeWidgetItem *item = m_configTranscode.profiles_list->currentItem();
741     if (item == NULL) return;
742     delete item;
743     slotDialogModified();
744 }
745
746 void KdenliveSettingsDialog::slotShuttleModified()
747 {
748 #ifndef NO_JOGSHUTTLE
749     QStringList actions;
750     actions << "monitor_pause";  // the Job rest position action.
751     foreach (KComboBox* button, m_shuttle_buttons) {
752         actions << m_mappable_actions[button->currentText()];
753     }
754     QString maps = JogShuttleConfig::actionMap(actions);
755     m_shuttleModified = KdenliveSettings::shuttlebuttons() != maps;
756 #endif
757 #if KDE_IS_VERSION(4,3,0)
758     KConfigDialog::updateButtons();
759 #endif
760 }
761
762 void KdenliveSettingsDialog::slotDialogModified()
763 {
764     m_modified = true;
765 #if KDE_IS_VERSION(4,3,0)
766     KConfigDialog::updateButtons();
767 #endif
768 }
769
770 //virtual
771 bool KdenliveSettingsDialog::hasChanged()
772 {
773     if (m_modified || m_shuttleModified) return true;
774     return KConfigDialog::hasChanged();
775 }
776
777 void KdenliveSettingsDialog::slotUpdatev4lDevice()
778 {
779     QString device = m_configCapture.kcfg_detectedv4ldevices->itemData(m_configCapture.kcfg_detectedv4ldevices->currentIndex()).toString();
780     if (!device.isEmpty()) m_configCapture.kcfg_video4vdevice->setText(device);
781     QString info = m_configCapture.kcfg_detectedv4ldevices->itemData(m_configCapture.kcfg_detectedv4ldevices->currentIndex(), Qt::UserRole + 1).toString();
782
783     m_configCapture.kcfg_v4l_format->blockSignals(true);
784     m_configCapture.kcfg_v4l_format->clear();
785
786     QString vl4ProfilePath = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
787     if (QFile::exists(vl4ProfilePath)) m_configCapture.kcfg_v4l_format->addItem(i18n("Current settings"));
788     
789     QStringList pixelformats = info.split(">", QString::SkipEmptyParts);
790     QString itemSize;
791     QString pixelFormat;
792     QStringList itemRates;
793     for (int i = 0; i < pixelformats.count(); i++) {
794         QString format = pixelformats.at(i).section(':', 0, 0);
795         QStringList sizes = pixelformats.at(i).split(":", QString::SkipEmptyParts);
796         pixelFormat = sizes.takeFirst();
797         for (int j = 0; j < sizes.count(); j++) {
798             itemSize = sizes.at(j).section("=", 0, 0);
799             itemRates = sizes.at(j).section("=", 1, 1).split(",", QString::SkipEmptyParts);
800             for (int k = 0; k < itemRates.count(); k++) {
801                 m_configCapture.kcfg_v4l_format->addItem("[" + format + "] " + itemSize + " (" + itemRates.at(k) + ")", QStringList() << format << itemSize.section('x', 0, 0) << itemSize.section('x', 1, 1) << itemRates.at(k).section('/', 0, 0) << itemRates.at(k).section('/', 1, 1));
802             }
803         }
804     }
805     m_configCapture.kcfg_v4l_format->blockSignals(false);
806     slotUpdatev4lCaptureProfile();
807 }
808
809 void KdenliveSettingsDialog::slotUpdatev4lCaptureProfile()
810 {
811     QStringList info = m_configCapture.kcfg_v4l_format->itemData(m_configCapture.kcfg_v4l_format->currentIndex(), Qt::UserRole).toStringList();
812     if (info.isEmpty()) {
813         // No auto info, display the current ones
814         loadCurrentV4lProfileInfo();
815         return;
816     }
817     m_configCapture.p_size->setText(info.at(1) + 'x' + info.at(2));
818     m_configCapture.p_fps->setText(info.at(3) + '/' + info.at(4));
819     m_configCapture.p_aspect->setText("1/1");
820     m_configCapture.p_display->setText(info.at(1) + '/' + info.at(2));
821     m_configCapture.p_colorspace->setText(ProfilesDialog::getColorspaceDescription(601));
822     m_configCapture.p_progressive->setText(i18n("Progressive"));
823
824     QString vl4ProfilePath = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
825     if (!QFile::exists(vl4ProfilePath)) saveCurrentV4lProfile();
826 }
827
828 void KdenliveSettingsDialog::loadCurrentV4lProfileInfo()
829 {
830     QString vl4ProfilePath = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
831     MltVideoProfile prof;
832     if (!QFile::exists(vl4ProfilePath)) {
833         // No default formats found, build one
834         prof.width = 320;
835         prof.height = 200;
836         prof.frame_rate_num = 15;
837         prof.frame_rate_den = 1;
838         prof.display_aspect_num = 4;
839         prof.display_aspect_den = 3;
840         prof.sample_aspect_num = 1;
841         prof.sample_aspect_den = 1;
842         prof.progressive = 1;
843         prof.colorspace = 601;
844         ProfilesDialog::saveProfile(prof, vl4ProfilePath);
845     }
846     else prof = ProfilesDialog::getVideoProfile(vl4ProfilePath);
847     m_configCapture.p_size->setText(QString::number(prof.width) + 'x' + QString::number(prof.height));
848     m_configCapture.p_fps->setText(QString::number(prof.frame_rate_num) + '/' + QString::number(prof.frame_rate_den));
849     m_configCapture.p_aspect->setText(QString::number(prof.sample_aspect_num) + '/' + QString::number(prof.sample_aspect_den));
850     m_configCapture.p_display->setText(QString::number(prof.display_aspect_num) + '/' + QString::number(prof.display_aspect_den));
851     m_configCapture.p_colorspace->setText(ProfilesDialog::getColorspaceDescription(prof.colorspace));
852     if (prof.progressive) m_configCapture.p_progressive->setText(i18n("Progressive"));
853 }
854
855 void KdenliveSettingsDialog::saveCurrentV4lProfile()
856 {
857     MltVideoProfile profile;
858     profile.description = "Video4Linux capture";
859     profile.colorspace = ProfilesDialog::getColorspaceFromDescription(m_configCapture.p_colorspace->text());
860     profile.width = m_configCapture.p_size->text().section('x', 0, 0).toInt();
861     profile.height = m_configCapture.p_size->text().section('x', 1, 1).toInt();
862     profile.sample_aspect_num = m_configCapture.p_aspect->text().section('/', 0, 0).toInt();
863     profile.sample_aspect_den = m_configCapture.p_aspect->text().section('/', 1, 1).toInt();
864     profile.display_aspect_num = m_configCapture.p_display->text().section('/', 0, 0).toInt();
865     profile.display_aspect_den = m_configCapture.p_display->text().section('/', 1, 1).toInt();
866     profile.frame_rate_num = m_configCapture.p_fps->text().section('/', 0, 0).toInt();
867     profile.frame_rate_den = m_configCapture.p_fps->text().section('/', 1, 1).toInt();
868     profile.progressive = m_configCapture.p_progressive->text() == i18n("Progressive");
869     QString vl4ProfilePath = KStandardDirs::locateLocal("appdata", "profiles/video4linux");
870     ProfilesDialog::saveProfile(profile, vl4ProfilePath);
871 }
872
873 #include "kdenlivesettingsdialog.moc"
874
875