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