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