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