]> git.sesse.net Git - kdenlive/blob - src/recmonitor.cpp
reindent + nice (working) progress bar for document loading
[kdenlive] / src / recmonitor.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "recmonitor.h"
22 #include "gentime.h"
23 #include "kdenlivesettings.h"
24 #include "managecapturesdialog.h"
25
26 #include <KDebug>
27 #include <KLocale>
28 #include <QPainter>
29 #include <KStandardDirs>
30 #include <KComboBox>
31 #include <KIO/NetAccess>
32 #include <KFileItem>
33 #include <KMessageBox>
34
35 #if KDE_IS_VERSION(4,2,0)
36 #include <KDiskFreeSpaceInfo>
37 #endif
38
39 #include <QMouseEvent>
40 #include <QMenu>
41 #include <QToolButton>
42 #include <QFile>
43 #include <QDir>
44
45
46 RecMonitor::RecMonitor(QString name, QWidget *parent) :
47         QWidget(parent),
48         m_name(name),
49         m_isActive(false),
50         m_isCapturing(false),
51         m_didCapture(false),
52         m_isPlaying(false)
53 {
54     setupUi(this);
55
56     video_frame->setAttribute(Qt::WA_PaintOnScreen);
57     device_selector->setCurrentIndex(KdenliveSettings::defaultcapture());
58     connect(device_selector, SIGNAL(currentIndexChanged(int)), this, SLOT(slotVideoDeviceChanged(int)));
59
60
61
62     QToolBar *toolbar = new QToolBar(name, this);
63     QHBoxLayout *layout = new QHBoxLayout;
64     layout->setContentsMargins(0, 0, 0, 0);
65     m_playIcon = KIcon("media-playback-start");
66     m_pauseIcon = KIcon("media-playback-pause");
67
68     m_discAction = toolbar->addAction(KIcon("network-connect"), i18n("Connect"));
69     connect(m_discAction, SIGNAL(triggered()), this, SLOT(slotDisconnect()));
70
71     m_rewAction = toolbar->addAction(KIcon("media-seek-backward"), i18n("Rewind"));
72     connect(m_rewAction, SIGNAL(triggered()), this, SLOT(slotRewind()));
73
74     m_playAction = toolbar->addAction(m_playIcon, i18n("Play"));
75     connect(m_playAction, SIGNAL(triggered()), this, SLOT(slotStartCapture()));
76
77     m_stopAction = toolbar->addAction(KIcon("media-playback-stop"), i18n("Stop"));
78     connect(m_stopAction, SIGNAL(triggered()), this, SLOT(slotStopCapture()));
79     m_stopAction->setEnabled(false);
80     m_fwdAction = toolbar->addAction(KIcon("media-seek-forward"), i18n("Forward"));
81     connect(m_fwdAction, SIGNAL(triggered()), this, SLOT(slotForward()));
82
83     m_recAction = toolbar->addAction(KIcon("media-record"), i18n("Record"));
84     connect(m_recAction, SIGNAL(triggered()), this, SLOT(slotRecord()));
85     m_recAction->setCheckable(true);
86
87     toolbar->addSeparator();
88
89     QAction *configAction = toolbar->addAction(KIcon("configure"), i18n("Configure"));
90     connect(configAction, SIGNAL(triggered()), this, SLOT(slotConfigure()));
91     configAction->setCheckable(false);
92
93     layout->addWidget(toolbar);
94
95     layout->addWidget(&m_dvinfo);
96
97 #if KDE_IS_VERSION(4,2,0)
98     m_freeSpace = new KCapacityBar(KCapacityBar::DrawTextInline, this);
99     m_freeSpace->setMaximumWidth(150);
100     QFontMetricsF fontMetrics(font());
101     m_freeSpace->setMaximumHeight(fontMetrics.height() * 1.2);
102     slotUpdateFreeSpace();
103     layout->addWidget(m_freeSpace);
104     connect(&m_spaceTimer, SIGNAL(timeout()), this, SLOT(slotUpdateFreeSpace()));
105     m_spaceTimer.setInterval(30000);
106     m_spaceTimer.setSingleShot(false);
107 #endif
108
109     control_frame_firewire->setLayout(layout);
110
111     slotVideoDeviceChanged(device_selector->currentIndex());
112     m_displayProcess = new QProcess;
113     m_captureProcess = new QProcess;
114
115     connect(m_captureProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotProcessStatus(QProcess::ProcessState)));
116     connect(m_captureProcess, SIGNAL(readyReadStandardError()), this, SLOT(slotReadDvgrabInfo()));
117
118     QStringList env = QProcess::systemEnvironment();
119     env << "SDL_WINDOWID=" + QString::number(video_frame->winId());
120
121     QString videoDriver = KdenliveSettings::videodrivername();
122     if (!videoDriver.isEmpty()) {
123         if (videoDriver == "x11_noaccel") {
124             env << "SDL_VIDEO_YUV_HWACCEL=0";
125             env << "SDL_VIDEODRIVER=x11";
126         } else env << "SDL_VIDEODRIVER=" + videoDriver;
127     }
128     setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
129
130     m_displayProcess->setEnvironment(env);
131
132     if (KdenliveSettings::video4capture().isEmpty()) {
133         QString captureCommand;
134         if (!KdenliveSettings::video4adevice().isEmpty()) captureCommand = "-f " + KdenliveSettings::video4aformat() + " -i " + KdenliveSettings::video4adevice();
135
136         captureCommand +=  " -f " + KdenliveSettings::video4vformat() + " -s " + KdenliveSettings::video4size() + " -r " + QString::number(KdenliveSettings::video4rate()) + " -i " + KdenliveSettings::video4vdevice();
137         KdenliveSettings::setVideo4capture(captureCommand);
138     }
139
140     kDebug() << "/////// BUILDING MONITOR, ID: " << video_frame->winId();
141 }
142
143 RecMonitor::~RecMonitor()
144 {
145     m_spaceTimer.stop();
146     delete m_captureProcess;
147     delete m_displayProcess;
148 }
149
150 QString RecMonitor::name() const
151 {
152     return m_name;
153 }
154
155 void RecMonitor::slotConfigure()
156 {
157     emit showConfigDialog(4, device_selector->currentIndex());
158 }
159
160 void RecMonitor::slotUpdateCaptureFolder()
161 {
162     if (m_captureProcess) m_captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
163     if (m_captureProcess->state() != QProcess::NotRunning) {
164         if (device_selector->currentIndex() == FIREWIRE)
165             KMessageBox::information(this, i18n("You need to disconnect and reconnect in the capture monitor to apply your changes"), i18n("Capturing"));
166         else KMessageBox::information(this, i18n("You need to stop capture before your changes can be applied"), i18n("Capturing"));
167     } else slotVideoDeviceChanged(device_selector->currentIndex());
168     kDebug() << "// UPDATE CAPT FOLD: " << KdenliveSettings::capturefolder();
169
170 #if KDE_IS_VERSION(4,2,0)
171     // update free space info
172     slotUpdateFreeSpace();
173 #endif
174 }
175
176 void RecMonitor::slotVideoDeviceChanged(int ix)
177 {
178     switch (ix) {
179     case SCREENGRAB:
180         m_discAction->setEnabled(false);
181         m_rewAction->setEnabled(false);
182         m_fwdAction->setEnabled(false);
183         m_recAction->setEnabled(true);
184         m_stopAction->setEnabled(false);
185         m_playAction->setEnabled(false);
186         if (KdenliveSettings::rmd_path().isEmpty()) {
187             QString rmdpath = KStandardDirs::findExe("recordmydesktop");
188             if (rmdpath.isEmpty()) video_frame->setPixmap(mergeSideBySide(KIcon("dialog-warning").pixmap(QSize(50, 50)), i18n("Recordmydesktop utility not found,\n please install it for screen grabs")));
189             else KdenliveSettings::setRmd_path(rmdpath);
190         }
191         if (!KdenliveSettings::rmd_path().isEmpty()) video_frame->setPixmap(mergeSideBySide(KIcon("video-display").pixmap(QSize(50, 50)), i18n("Press record button\nto start screen capture\nFiles will be saved in:\n%1", KdenliveSettings::capturefolder())));
192         //video_frame->setText(i18n("Press record button\nto start screen capture"));
193         break;
194     case VIDEO4LINUX:
195         m_discAction->setEnabled(false);
196         m_rewAction->setEnabled(false);
197         m_fwdAction->setEnabled(false);
198         m_recAction->setEnabled(true);
199         m_stopAction->setEnabled(false);
200         m_playAction->setEnabled(true);
201         checkDeviceAvailability();
202         break;
203     default: // FIREWIRE
204         m_discAction->setEnabled(true);
205         m_recAction->setEnabled(false);
206         m_stopAction->setEnabled(false);
207         m_playAction->setEnabled(false);
208         m_rewAction->setEnabled(false);
209         m_fwdAction->setEnabled(false);
210
211         // Check that dvgab is available
212         if (KdenliveSettings::dvgrab_path().isEmpty()) {
213             QString dvgrabpath = KStandardDirs::findExe("dvgrab");
214             if (dvgrabpath.isEmpty()) video_frame->setPixmap(mergeSideBySide(KIcon("dialog-warning").pixmap(QSize(50, 50)), i18n("dvgrab utility not found,\n please install it for firewire capture")));
215             else KdenliveSettings::setDvgrab_path(dvgrabpath);
216         } else {
217             // Show capture info
218             QString capturefile = KdenliveSettings::capturefolder();
219             if (!capturefile.endsWith("/")) capturefile.append("/");
220             QString capturename = KdenliveSettings::dvgrabfilename();
221             if (capturename.isEmpty()) capturename = "capture";
222             QString extension;
223             switch (KdenliveSettings::firewireformat()) {
224             case 0:
225                 extension = ".dv";
226                 break;
227             case 1:
228             case 2:
229                 extension = ".avi";
230                 break;
231             case 3:
232                 extension = ".m2t";
233                 break;
234             }
235             capturename.append("xxx" + extension);
236             capturefile.append(capturename);
237             video_frame->setPixmap(mergeSideBySide(KIcon("network-connect").pixmap(QSize(50, 50)), i18n("Plug your camcorder and\npress connect button\nto initialize connection\nFiles will be saved in:\n%1", capturefile)));
238         }
239         break;
240     }
241 }
242
243 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt)
244 {
245     QPainter p;
246     QRect r = QApplication::fontMetrics().boundingRect(QRect(0, 0, video_frame->width(), video_frame->height()), Qt::AlignLeft, txt);
247     int strWidth = r.width();
248     int strHeight = r.height();
249     int pixWidth = pix.width();
250     int pixHeight = pix.height();
251     QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
252     res.fill(Qt::transparent);
253     p.begin(&res);
254     p.drawPixmap(0, 0, pix);
255     p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
256     p.end();
257     return res;
258 }
259
260
261 void RecMonitor::checkDeviceAvailability()
262 {
263     if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
264         m_playAction->setEnabled(false);
265         m_recAction->setEnabled(false);
266         video_frame->setPixmap(mergeSideBySide(KIcon("camera-web").pixmap(QSize(50, 50)), i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice())));
267         //video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
268     } else //video_frame->setText(i18n("Press play or record button\nto start video capture"));
269         video_frame->setPixmap(mergeSideBySide(KIcon("camera-web").pixmap(QSize(50, 50)), i18n("Press play or record button\nto start video capture\nFiles will be saved in:\n%1", KdenliveSettings::capturefolder())));
270 }
271
272 void RecMonitor::slotDisconnect()
273 {
274     if (m_captureProcess->state() == QProcess::NotRunning) {
275         m_captureTime = KDateTime::currentLocalDateTime();
276         kDebug() << "CURRENT TIME: " << m_captureTime.toString();
277         m_didCapture = false;
278         slotStartCapture(false);
279         m_discAction->setIcon(KIcon("network-disconnect"));
280         m_discAction->setText(i18n("Disonnect"));
281         m_recAction->setEnabled(true);
282         m_stopAction->setEnabled(true);
283         m_playAction->setEnabled(true);
284         m_rewAction->setEnabled(true);
285         m_fwdAction->setEnabled(true);
286     } else {
287         m_captureProcess->write("q", 1);
288         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
289         if (m_didCapture) manageCapturedFiles();
290         m_didCapture = false;
291     }
292 }
293
294 void RecMonitor::slotRewind()
295 {
296     m_captureProcess->write("a", 1);
297 }
298
299 void RecMonitor::slotForward()
300 {
301     m_captureProcess->write("z", 1);
302 }
303
304 void RecMonitor::slotStopCapture()
305 {
306     // stop capture
307     switch (device_selector->currentIndex()) {
308     case FIREWIRE:
309         m_captureProcess->write("\e", 2);
310         m_playAction->setIcon(m_playIcon);
311         m_isPlaying = false;
312         break;
313     case VIDEO4LINUX:
314         m_captureProcess->write("q\n", 3);
315         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
316
317         break;
318     case SCREENGRAB:
319         m_captureProcess->write("q\n", 3);
320         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
321         break;
322     default:
323         break;
324     }
325 }
326
327 void RecMonitor::slotStartCapture(bool play)
328 {
329     if (m_captureProcess->state() != QProcess::NotRunning) {
330         if (device_selector->currentIndex() == FIREWIRE) {
331             if (m_isPlaying) {
332                 m_captureProcess->write("k", 1);
333                 //captureProcess->write("\e", 2);
334                 m_playAction->setIcon(m_playIcon);
335                 m_isPlaying = false;
336             } else {
337                 m_captureProcess->write("p", 1);
338                 m_playAction->setIcon(m_pauseIcon);
339                 m_isPlaying = true;
340             }
341         }
342         return;
343     }
344     m_captureArgs.clear();
345     m_displayArgs.clear();
346     m_isPlaying = false;
347     QString capturename = KdenliveSettings::dvgrabfilename();
348     QStringList dvargs = KdenliveSettings::dvgrabextra().simplified().split(" ", QString::SkipEmptyParts);
349
350     switch (device_selector->currentIndex()) {
351     case FIREWIRE:
352         switch (KdenliveSettings::firewireformat()) {
353         case 0:
354             // RAW DV CAPTURE
355             m_captureArgs << "--format" << "raw";
356             m_displayArgs << "-f" << "dv";
357             break;
358         case 1:
359             // DV type 1
360             m_captureArgs << "--format" << "dv1";
361             m_displayArgs << "-f" << "dv";
362             break;
363         case 2:
364             // DV type 2
365             m_captureArgs << "--format" << "dv2";
366             m_displayArgs << "-f" << "dv";
367             break;
368         case 3:
369             // HDV CAPTURE
370             m_captureArgs << "--format" << "hdv";
371             m_displayArgs << "-f" << "mpegts";
372             break;
373         }
374         if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
375         if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
376         if (!dvargs.isEmpty()) {
377             m_captureArgs << dvargs;
378         }
379         m_captureArgs << "-i";
380         if (capturename.isEmpty()) capturename = "capture";
381         m_captureArgs << capturename << "-";
382
383         m_displayArgs << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
384
385         m_captureProcess->setStandardOutputProcess(m_displayProcess);
386         m_captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
387         kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
388
389         m_captureProcess->start(KdenliveSettings::dvgrab_path(), m_captureArgs);
390         if (play) m_captureProcess->write(" ", 1);
391         m_discAction->setEnabled(true);
392         break;
393     case VIDEO4LINUX:
394         m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-f" << "mpegts" << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-";
395         m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
396         m_captureProcess->setStandardOutputProcess(m_displayProcess);
397         kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
398         m_captureProcess->start("ffmpeg", m_captureArgs);
399         break;
400     default:
401         break;
402     }
403
404     if (device_selector->currentIndex() != SCREENGRAB) {
405         kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
406         m_displayProcess->start("ffplay", m_displayArgs);
407         video_frame->setText(i18n("Initialising..."));
408     } else {
409         // do something when starting screen grab
410     }
411 }
412
413 void RecMonitor::slotRecord()
414 {
415     if (m_captureProcess->state() == QProcess::NotRunning && device_selector->currentIndex() == FIREWIRE) {
416         slotStartCapture();
417     }
418     if (m_isCapturing) {
419         switch (device_selector->currentIndex()) {
420         case FIREWIRE:
421             m_captureProcess->write("\e", 2);
422             m_playAction->setIcon(m_playIcon);
423             m_isCapturing = false;
424             m_isPlaying = false;
425             m_recAction->setChecked(false);
426             break;
427         case VIDEO4LINUX:
428             m_captureProcess->terminate();
429             slotStopCapture();
430             //m_isCapturing = false;
431             QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
432             break;
433         case SCREENGRAB:
434             //captureProcess->write("q\n", 3);
435             m_captureProcess->terminate();
436             video_frame->setText(i18n("Encoding captured video..."));
437             // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
438             //QTimer::singleShot(1000, captureProcess, SLOT(kill()));
439             break;
440         }
441         return;
442     } else if (device_selector->currentIndex() == FIREWIRE) {
443         m_isCapturing = true;
444         m_didCapture = true;
445         m_captureProcess->write("c\n", 3);
446         m_spaceTimer.start();
447         return;
448     }
449     if (m_captureProcess->state() == QProcess::NotRunning) {
450         m_recAction->setChecked(true);
451         QString extension = "mp4";
452         if (device_selector->currentIndex() == SCREENGRAB) extension = "ogv"; //KdenliveSettings::screengrabextension();
453         QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
454         int i = 1;
455         while (QFile::exists(path)) {
456             QString num = QString::number(i).rightJustified(4, '0', false);
457             path = KdenliveSettings::capturefolder() + "/capture" + num + '.' + extension;
458             i++;
459         }
460         m_captureFile = KUrl(path);
461
462         m_captureArgs.clear();
463         m_displayArgs.clear();
464         QString args;
465         QString capturename = KdenliveSettings::dvgrabfilename();
466         if (capturename.isEmpty()) capturename = "capture";
467
468         switch (device_selector->currentIndex()) {
469         case VIDEO4LINUX:
470             m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-y" << m_captureFile.path() << "-f" << "mpegts" << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-";
471             m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
472             m_captureProcess->setStandardOutputProcess(m_displayProcess);
473             kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
474             m_captureProcess->start("ffmpeg", m_captureArgs);
475             break;
476         case SCREENGRAB:
477             switch (KdenliveSettings::rmd_capture_type()) {
478             case 0:
479                 // Full screen capture, nothing special to do
480                 break;
481             default:
482                 // Region capture
483                 m_captureArgs << "-width" << QString::number(KdenliveSettings::rmd_width()) << "-height" << QString::number(KdenliveSettings::rmd_height());
484                 if (!KdenliveSettings::rmd_follow_mouse()) {
485                     m_captureArgs << "-x" << QString::number(KdenliveSettings::rmd_offsetx()) << "-y" << QString::number(KdenliveSettings::rmd_offsety());
486                 } else {
487                     m_captureArgs << "--follow-mouse";
488                     if (KdenliveSettings::rmd_hide_frame()) m_captureArgs << "--no-frame";
489                 }
490                 break;
491             }
492             m_isCapturing = true;
493             if (KdenliveSettings::rmd_capture_audio()) {
494                 m_captureArgs << "-freq" << KdenliveSettings::rmd_freq();
495                 m_captureArgs << "-channels" << QString::number(KdenliveSettings::rmd_audio_channels());
496                 if (KdenliveSettings::rmd_use_jack()) {
497                     m_captureArgs << "-use-jack" << KdenliveSettings::rmd_jackports();
498                     if (KdenliveSettings::rmd_jack_buffer() > 0.0)
499                         m_captureArgs << "-ring-buffer-size" << QString::number(KdenliveSettings::rmd_jack_buffer());
500                 } else {
501                     if (!KdenliveSettings::rmd_alsadevicename().isEmpty())
502                         m_captureArgs << "-device" << KdenliveSettings::rmd_alsadevicename();
503                     if (KdenliveSettings::rmd_alsa_buffer() > 0)
504                         m_captureArgs << "-buffer-size" << QString::number(KdenliveSettings::rmd_alsa_buffer());
505                 }
506             } else m_captureArgs << "--no-sound";
507
508             if (KdenliveSettings::rmd_fullshots()) m_captureArgs << "--full-shots";
509             m_captureArgs << "-workdir" << KdenliveSettings::currenttmpfolder();
510             m_captureArgs << "-fps" << QString::number(KdenliveSettings::rmd_fps()) << "-o" << m_captureFile.path();
511             m_captureProcess->start(KdenliveSettings::rmd_path(), m_captureArgs);
512             kDebug() << "// RecordMyDesktop params: " << m_captureArgs;
513             break;
514         default:
515             break;
516         }
517
518
519         if (device_selector->currentIndex() != SCREENGRAB) {
520             m_isCapturing = true;
521             kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
522             m_displayProcess->start("ffplay", m_displayArgs);
523             video_frame->setText(i18n("Initialising..."));
524         }
525     } else {
526         // stop capture
527         m_displayProcess->kill();
528         //captureProcess->kill();
529         QTimer::singleShot(1000, this, SLOT(slotRecord()));
530     }
531 }
532
533 /*
534 void RecMonitor::slotStartGrab(const QRect &rect) {
535     rgnGrab->deleteLater();
536     QApplication::restoreOverrideCursor();
537     show();
538     if (rect.isNull()) return;
539     int width = rect.width();
540     int height = rect.height();
541     if (width % 2 != 0) width--;
542     if (height % 2 != 0) height--;
543     QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
544     if (KdenliveSettings::screengrabenableaudio()) {
545         // also capture audio
546         if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
547         else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
548     }
549     m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
550     m_isCapturing = true;
551     video_frame->setText(i18n("Capturing..."));
552     if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
553         QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
554         alsaProcess->setStandardOutputProcess(captureProcess);
555         kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
556         alsaProcess->start("arecord", alsaArgs);
557     }
558     kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
559     captureProcess->start("ffmpeg", m_captureArgs);
560 }*/
561
562 void RecMonitor::slotProcessStatus(QProcess::ProcessState status)
563 {
564     if (status == QProcess::NotRunning) {
565         m_displayProcess->kill();
566         if (m_isCapturing && device_selector->currentIndex() != FIREWIRE)
567             if (autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
568         if (device_selector->currentIndex() == FIREWIRE) {
569             m_discAction->setIcon(KIcon("network-connect"));
570             m_discAction->setText(i18n("Connect"));
571             m_playAction->setEnabled(false);
572             m_rewAction->setEnabled(false);
573             m_fwdAction->setEnabled(false);
574             m_recAction->setEnabled(false);
575         }
576         m_isPlaying = false;
577         m_playAction->setIcon(m_playIcon);
578         m_recAction->setChecked(false);
579         m_stopAction->setEnabled(false);
580         device_selector->setEnabled(true);
581         if (m_captureProcess && m_captureProcess->exitStatus() == QProcess::CrashExit) {
582             video_frame->setText(i18n("Capture crashed, please check your parameters"));
583         } else {
584             if (device_selector->currentIndex() != SCREENGRAB) video_frame->setText(i18n("Not connected"));
585             else video_frame->setPixmap(mergeSideBySide(KIcon("video-display").pixmap(QSize(50, 50)), i18n("Press record button\nto start screen capture\nFiles will be saved in:\n%1", KdenliveSettings::capturefolder())));
586         }
587         m_isCapturing = false;
588         m_spaceTimer.stop();
589
590 #if KDE_IS_VERSION(4,2,0)
591         // update free space info
592         slotUpdateFreeSpace();
593 #endif
594
595     } else {
596         if (device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
597         device_selector->setEnabled(false);
598     }
599 }
600
601 void RecMonitor::manageCapturedFiles()
602 {
603     QString extension;
604     switch (KdenliveSettings::firewireformat()) {
605     case 0:
606         extension = ".dv";
607         break;
608     case 1:
609     case 2:
610         extension = ".avi";
611         break;
612     case 3:
613         extension = ".m2t";
614         break;
615     }
616     QDir dir(KdenliveSettings::capturefolder());
617     QStringList filters;
618     QString capturename = KdenliveSettings::dvgrabfilename();
619     if (capturename.isEmpty()) capturename = "capture";
620     filters << capturename + "*" + extension;
621     const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
622     KUrl::List capturedFiles;
623     foreach(const QString &name, result) {
624         KUrl url = KUrl(dir.filePath(name));
625         if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
626             KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
627             if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
628         }
629     }
630     kDebug() << "Found : " << capturedFiles.count() << " new capture files";
631     kDebug() << capturedFiles;
632
633     if (capturedFiles.count() > 0) {
634         ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
635         if (d->exec() == QDialog::Accepted) {
636             capturedFiles = d->importFiles();
637             foreach(const KUrl &url, capturedFiles) {
638                 emit addProjectClip(url);
639             }
640         }
641         delete d;
642     }
643 }
644
645 // virtual
646 void RecMonitor::mousePressEvent(QMouseEvent * /*event*/)
647 {
648 #if KDE_IS_VERSION(4,2,0)
649     if (m_freeSpace->underMouse()) slotUpdateFreeSpace();
650 #endif
651 }
652
653 void RecMonitor::slotUpdateFreeSpace()
654 {
655 #if KDE_IS_VERSION(4,2,0)
656     KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(KdenliveSettings::capturefolder());
657     if (info.isValid()) {
658         m_freeSpace->setValue(100 * info.used() / info.size());
659         m_freeSpace->setText(i18n("Free space: %1", KIO::convertSize(info.available())));
660         m_freeSpace->update();
661     }
662 #endif
663 }
664
665 void RecMonitor::activateRecMonitor()
666 {
667     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
668 }
669
670 void RecMonitor::stop()
671 {
672     m_isActive = false;
673
674 }
675
676 void RecMonitor::start()
677 {
678     m_isActive = true;
679
680 }
681
682 void RecMonitor::refreshRecMonitor(bool visible)
683 {
684     if (visible) {
685         //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
686
687     }
688 }
689
690 void RecMonitor::slotPlay()
691 {
692
693     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
694
695 }
696
697 void RecMonitor::slotReadDvgrabInfo()
698 {
699     QString data = m_captureProcess->readAllStandardError().simplified();
700     data = data.section('"', 2, 2).simplified();
701     m_dvinfo.setText(data.left(11));
702     m_dvinfo.updateGeometry();
703 }
704
705 #include "recmonitor.moc"