]> git.sesse.net Git - kdenlive/blob - src/recmonitor.cpp
reindent + create a backup copy of the document if we upgraded the document version
[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 #if KDE_IS_VERSION(4,2,0)
146     m_spaceTimer.stop();
147 #endif
148     delete m_captureProcess;
149     delete m_displayProcess;
150 }
151
152 QString RecMonitor::name() const
153 {
154     return m_name;
155 }
156
157 void RecMonitor::slotConfigure()
158 {
159     emit showConfigDialog(4, device_selector->currentIndex());
160 }
161
162 void RecMonitor::slotUpdateCaptureFolder()
163 {
164     if (m_captureProcess) m_captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
165     if (m_captureProcess->state() != QProcess::NotRunning) {
166         if (device_selector->currentIndex() == FIREWIRE)
167             KMessageBox::information(this, i18n("You need to disconnect and reconnect in the capture monitor to apply your changes"), i18n("Capturing"));
168         else KMessageBox::information(this, i18n("You need to stop capture before your changes can be applied"), i18n("Capturing"));
169     } else slotVideoDeviceChanged(device_selector->currentIndex());
170     kDebug() << "// UPDATE CAPT FOLD: " << KdenliveSettings::capturefolder();
171
172 #if KDE_IS_VERSION(4,2,0)
173     // update free space info
174     slotUpdateFreeSpace();
175 #endif
176 }
177
178 void RecMonitor::slotVideoDeviceChanged(int ix)
179 {
180     switch (ix) {
181     case SCREENGRAB:
182         m_discAction->setEnabled(false);
183         m_rewAction->setEnabled(false);
184         m_fwdAction->setEnabled(false);
185         m_recAction->setEnabled(true);
186         m_stopAction->setEnabled(false);
187         m_playAction->setEnabled(false);
188         if (KdenliveSettings::rmd_path().isEmpty()) {
189             QString rmdpath = KStandardDirs::findExe("recordmydesktop");
190             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")));
191             else KdenliveSettings::setRmd_path(rmdpath);
192         }
193         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())));
194         //video_frame->setText(i18n("Press record button\nto start screen capture"));
195         break;
196     case VIDEO4LINUX:
197         m_discAction->setEnabled(false);
198         m_rewAction->setEnabled(false);
199         m_fwdAction->setEnabled(false);
200         m_recAction->setEnabled(true);
201         m_stopAction->setEnabled(false);
202         m_playAction->setEnabled(true);
203         checkDeviceAvailability();
204         break;
205     default: // FIREWIRE
206         m_discAction->setEnabled(true);
207         m_recAction->setEnabled(false);
208         m_stopAction->setEnabled(false);
209         m_playAction->setEnabled(false);
210         m_rewAction->setEnabled(false);
211         m_fwdAction->setEnabled(false);
212
213         // Check that dvgab is available
214         if (KdenliveSettings::dvgrab_path().isEmpty()) {
215             QString dvgrabpath = KStandardDirs::findExe("dvgrab");
216             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")));
217             else KdenliveSettings::setDvgrab_path(dvgrabpath);
218         } else {
219             // Show capture info
220             QString capturefile = KdenliveSettings::capturefolder();
221             if (!capturefile.endsWith("/")) capturefile.append("/");
222             QString capturename = KdenliveSettings::dvgrabfilename();
223             if (capturename.isEmpty()) capturename = "capture";
224             QString extension;
225             switch (KdenliveSettings::firewireformat()) {
226             case 0:
227                 extension = ".dv";
228                 break;
229             case 1:
230             case 2:
231                 extension = ".avi";
232                 break;
233             case 3:
234                 extension = ".m2t";
235                 break;
236             }
237             capturename.append("xxx" + extension);
238             capturefile.append(capturename);
239             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)));
240         }
241         break;
242     }
243 }
244
245 QPixmap RecMonitor::mergeSideBySide(const QPixmap& pix, const QString txt)
246 {
247     QPainter p;
248     QRect r = QApplication::fontMetrics().boundingRect(QRect(0, 0, video_frame->width(), video_frame->height()), Qt::AlignLeft, txt);
249     int strWidth = r.width();
250     int strHeight = r.height();
251     int pixWidth = pix.width();
252     int pixHeight = pix.height();
253     QPixmap res(strWidth + 8 + pixWidth, qMax(strHeight, pixHeight));
254     res.fill(Qt::transparent);
255     p.begin(&res);
256     p.drawPixmap(0, 0, pix);
257     p.drawText(QRect(pixWidth + 8, 0, strWidth, strHeight), 0, txt);
258     p.end();
259     return res;
260 }
261
262
263 void RecMonitor::checkDeviceAvailability()
264 {
265     if (!KIO::NetAccess::exists(KUrl(KdenliveSettings::video4vdevice()), KIO::NetAccess::SourceSide , this)) {
266         m_playAction->setEnabled(false);
267         m_recAction->setEnabled(false);
268         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())));
269         //video_frame->setText(i18n("Cannot read from device %1\nPlease check drivers and access rights.", KdenliveSettings::video4vdevice()));
270     } else //video_frame->setText(i18n("Press play or record button\nto start video capture"));
271         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())));
272 }
273
274 void RecMonitor::slotDisconnect()
275 {
276     if (m_captureProcess->state() == QProcess::NotRunning) {
277         m_captureTime = KDateTime::currentLocalDateTime();
278         kDebug() << "CURRENT TIME: " << m_captureTime.toString();
279         m_didCapture = false;
280         slotStartCapture(false);
281         m_discAction->setIcon(KIcon("network-disconnect"));
282         m_discAction->setText(i18n("Disonnect"));
283         m_recAction->setEnabled(true);
284         m_stopAction->setEnabled(true);
285         m_playAction->setEnabled(true);
286         m_rewAction->setEnabled(true);
287         m_fwdAction->setEnabled(true);
288     } else {
289         m_captureProcess->write("q", 1);
290         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
291         if (m_didCapture) manageCapturedFiles();
292         m_didCapture = false;
293     }
294 }
295
296 void RecMonitor::slotRewind()
297 {
298     m_captureProcess->write("a", 1);
299 }
300
301 void RecMonitor::slotForward()
302 {
303     m_captureProcess->write("z", 1);
304 }
305
306 void RecMonitor::slotStopCapture()
307 {
308     // stop capture
309     switch (device_selector->currentIndex()) {
310     case FIREWIRE:
311         m_captureProcess->write("\e", 2);
312         m_playAction->setIcon(m_playIcon);
313         m_isPlaying = false;
314         break;
315     case VIDEO4LINUX:
316         m_captureProcess->write("q\n", 3);
317         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
318
319         break;
320     case SCREENGRAB:
321         m_captureProcess->write("q\n", 3);
322         QTimer::singleShot(1000, m_captureProcess, SLOT(kill()));
323         break;
324     default:
325         break;
326     }
327 }
328
329 void RecMonitor::slotStartCapture(bool play)
330 {
331     if (m_captureProcess->state() != QProcess::NotRunning) {
332         if (device_selector->currentIndex() == FIREWIRE) {
333             if (m_isPlaying) {
334                 m_captureProcess->write("k", 1);
335                 //captureProcess->write("\e", 2);
336                 m_playAction->setIcon(m_playIcon);
337                 m_isPlaying = false;
338             } else {
339                 m_captureProcess->write("p", 1);
340                 m_playAction->setIcon(m_pauseIcon);
341                 m_isPlaying = true;
342             }
343         }
344         return;
345     }
346     m_captureArgs.clear();
347     m_displayArgs.clear();
348     m_isPlaying = false;
349     QString capturename = KdenliveSettings::dvgrabfilename();
350     QStringList dvargs = KdenliveSettings::dvgrabextra().simplified().split(" ", QString::SkipEmptyParts);
351
352     switch (device_selector->currentIndex()) {
353     case FIREWIRE:
354         switch (KdenliveSettings::firewireformat()) {
355         case 0:
356             // RAW DV CAPTURE
357             m_captureArgs << "--format" << "raw";
358             m_displayArgs << "-f" << "dv";
359             break;
360         case 1:
361             // DV type 1
362             m_captureArgs << "--format" << "dv1";
363             m_displayArgs << "-f" << "dv";
364             break;
365         case 2:
366             // DV type 2
367             m_captureArgs << "--format" << "dv2";
368             m_displayArgs << "-f" << "dv";
369             break;
370         case 3:
371             // HDV CAPTURE
372             m_captureArgs << "--format" << "hdv";
373             m_displayArgs << "-f" << "mpegts";
374             break;
375         }
376         if (KdenliveSettings::firewireautosplit()) m_captureArgs << "--autosplit";
377         if (KdenliveSettings::firewiretimestamp()) m_captureArgs << "--timestamp";
378         if (!dvargs.isEmpty()) {
379             m_captureArgs << dvargs;
380         }
381         m_captureArgs << "-i";
382         if (capturename.isEmpty()) capturename = "capture";
383         m_captureArgs << capturename << "-";
384
385         m_displayArgs << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
386
387         m_captureProcess->setStandardOutputProcess(m_displayProcess);
388         m_captureProcess->setWorkingDirectory(KdenliveSettings::capturefolder());
389         kDebug() << "Capture: Running dvgrab " << m_captureArgs.join(" ");
390
391         m_captureProcess->start(KdenliveSettings::dvgrab_path(), m_captureArgs);
392         if (play) m_captureProcess->write(" ", 1);
393         m_discAction->setEnabled(true);
394         break;
395     case VIDEO4LINUX:
396         m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-f" << "mpegts" << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-";
397         m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
398         m_captureProcess->setStandardOutputProcess(m_displayProcess);
399         kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
400         m_captureProcess->start("ffmpeg", m_captureArgs);
401         break;
402     default:
403         break;
404     }
405
406     if (device_selector->currentIndex() != SCREENGRAB) {
407         kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
408         m_displayProcess->start("ffplay", m_displayArgs);
409         video_frame->setText(i18n("Initialising..."));
410     } else {
411         // do something when starting screen grab
412     }
413 }
414
415 void RecMonitor::slotRecord()
416 {
417     if (m_captureProcess->state() == QProcess::NotRunning && device_selector->currentIndex() == FIREWIRE) {
418         slotStartCapture();
419     }
420     if (m_isCapturing) {
421         switch (device_selector->currentIndex()) {
422         case FIREWIRE:
423             m_captureProcess->write("\e", 2);
424             m_playAction->setIcon(m_playIcon);
425             m_isCapturing = false;
426             m_isPlaying = false;
427             m_recAction->setChecked(false);
428             break;
429         case VIDEO4LINUX:
430             m_captureProcess->terminate();
431             slotStopCapture();
432             //m_isCapturing = false;
433             QTimer::singleShot(1000, this, SLOT(slotStartCapture()));
434             break;
435         case SCREENGRAB:
436             //captureProcess->write("q\n", 3);
437             m_captureProcess->terminate();
438             video_frame->setText(i18n("Encoding captured video..."));
439             // in case ffmpeg doesn't exit with the 'q' command, kill it one second later
440             //QTimer::singleShot(1000, captureProcess, SLOT(kill()));
441             break;
442         }
443         return;
444     } else if (device_selector->currentIndex() == FIREWIRE) {
445         m_isCapturing = true;
446         m_didCapture = true;
447         m_captureProcess->write("c\n", 3);
448 #if KDE_IS_VERSION(4,2,0)
449         m_spaceTimer.start();
450 #endif
451         return;
452     }
453     if (m_captureProcess->state() == QProcess::NotRunning) {
454         m_recAction->setChecked(true);
455         QString extension = "mp4";
456         if (device_selector->currentIndex() == SCREENGRAB) extension = "ogv"; //KdenliveSettings::screengrabextension();
457         QString path = KdenliveSettings::capturefolder() + "/capture0000." + extension;
458         int i = 1;
459         while (QFile::exists(path)) {
460             QString num = QString::number(i).rightJustified(4, '0', false);
461             path = KdenliveSettings::capturefolder() + "/capture" + num + '.' + extension;
462             i++;
463         }
464         m_captureFile = KUrl(path);
465
466         m_captureArgs.clear();
467         m_displayArgs.clear();
468         QString args;
469         QString capturename = KdenliveSettings::dvgrabfilename();
470         if (capturename.isEmpty()) capturename = "capture";
471
472         switch (device_selector->currentIndex()) {
473         case VIDEO4LINUX:
474             m_captureArgs << KdenliveSettings::video4capture().simplified().split(' ') << KdenliveSettings::video4encoding().simplified().split(' ') << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-y" << m_captureFile.path() << "-f" << "mpegts" << "-vcodec" << "mpeg4" << "-acodec" << "mp2" << "-";
475             m_displayArgs << "-f" << "mpegts" << "-x" << QString::number(video_frame->width()) << "-y" << QString::number(video_frame->height()) << "-";
476             m_captureProcess->setStandardOutputProcess(m_displayProcess);
477             kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
478             m_captureProcess->start("ffmpeg", m_captureArgs);
479             break;
480         case SCREENGRAB:
481             switch (KdenliveSettings::rmd_capture_type()) {
482             case 0:
483                 // Full screen capture, nothing special to do
484                 break;
485             default:
486                 // Region capture
487                 m_captureArgs << "-width" << QString::number(KdenliveSettings::rmd_width()) << "-height" << QString::number(KdenliveSettings::rmd_height());
488                 if (!KdenliveSettings::rmd_follow_mouse()) {
489                     m_captureArgs << "-x" << QString::number(KdenliveSettings::rmd_offsetx()) << "-y" << QString::number(KdenliveSettings::rmd_offsety());
490                 } else {
491                     m_captureArgs << "--follow-mouse";
492                     if (KdenliveSettings::rmd_hide_frame()) m_captureArgs << "--no-frame";
493                 }
494                 break;
495             }
496             m_isCapturing = true;
497             if (KdenliveSettings::rmd_capture_audio()) {
498                 m_captureArgs << "-freq" << KdenliveSettings::rmd_freq();
499                 m_captureArgs << "-channels" << QString::number(KdenliveSettings::rmd_audio_channels());
500                 if (KdenliveSettings::rmd_use_jack()) {
501                     m_captureArgs << "-use-jack" << KdenliveSettings::rmd_jackports();
502                     if (KdenliveSettings::rmd_jack_buffer() > 0.0)
503                         m_captureArgs << "-ring-buffer-size" << QString::number(KdenliveSettings::rmd_jack_buffer());
504                 } else {
505                     if (!KdenliveSettings::rmd_alsadevicename().isEmpty())
506                         m_captureArgs << "-device" << KdenliveSettings::rmd_alsadevicename();
507                     if (KdenliveSettings::rmd_alsa_buffer() > 0)
508                         m_captureArgs << "-buffer-size" << QString::number(KdenliveSettings::rmd_alsa_buffer());
509                 }
510             } else m_captureArgs << "--no-sound";
511
512             if (KdenliveSettings::rmd_fullshots()) m_captureArgs << "--full-shots";
513             m_captureArgs << "-workdir" << KdenliveSettings::currenttmpfolder();
514             m_captureArgs << "-fps" << QString::number(KdenliveSettings::rmd_fps()) << "-o" << m_captureFile.path();
515             m_captureProcess->start(KdenliveSettings::rmd_path(), m_captureArgs);
516             kDebug() << "// RecordMyDesktop params: " << m_captureArgs;
517             break;
518         default:
519             break;
520         }
521
522
523         if (device_selector->currentIndex() != SCREENGRAB) {
524             m_isCapturing = true;
525             kDebug() << "Capture: Running ffplay " << m_displayArgs.join(" ");
526             m_displayProcess->start("ffplay", m_displayArgs);
527             video_frame->setText(i18n("Initialising..."));
528         }
529     } else {
530         // stop capture
531         m_displayProcess->kill();
532         //captureProcess->kill();
533         QTimer::singleShot(1000, this, SLOT(slotRecord()));
534     }
535 }
536
537 /*
538 void RecMonitor::slotStartGrab(const QRect &rect) {
539     rgnGrab->deleteLater();
540     QApplication::restoreOverrideCursor();
541     show();
542     if (rect.isNull()) return;
543     int width = rect.width();
544     int height = rect.height();
545     if (width % 2 != 0) width--;
546     if (height % 2 != 0) height--;
547     QString args = KdenliveSettings::screengrabcapture().replace("%size", QString::number(width) + "x" + QString::number(height)).replace("%offset", "+" + QString::number(rect.x()) + "," + QString::number(rect.y()));
548     if (KdenliveSettings::screengrabenableaudio()) {
549         // also capture audio
550         if (KdenliveSettings::useosscapture()) m_captureArgs << KdenliveSettings::screengrabosscapture().simplified().split(' ');
551         else m_captureArgs << KdenliveSettings::screengrabalsacapture2().simplified().split(' ');
552     }
553     m_captureArgs << args.simplified().split(' ') << KdenliveSettings::screengrabencoding().simplified().split(' ') << m_captureFile.path();
554     m_isCapturing = true;
555     video_frame->setText(i18n("Capturing..."));
556     if (KdenliveSettings::screengrabenableaudio() && !KdenliveSettings::useosscapture()) {
557         QStringList alsaArgs = KdenliveSettings::screengrabalsacapture().simplified().split(' ');
558         alsaProcess->setStandardOutputProcess(captureProcess);
559         kDebug() << "Capture: Running arecord " << alsaArgs.join(" ");
560         alsaProcess->start("arecord", alsaArgs);
561     }
562     kDebug() << "Capture: Running ffmpeg " << m_captureArgs.join(" ");
563     captureProcess->start("ffmpeg", m_captureArgs);
564 }*/
565
566 void RecMonitor::slotProcessStatus(QProcess::ProcessState status)
567 {
568     if (status == QProcess::NotRunning) {
569         m_displayProcess->kill();
570         if (m_isCapturing && device_selector->currentIndex() != FIREWIRE)
571             if (autoaddbox->isChecked() && QFile::exists(m_captureFile.path())) emit addProjectClip(m_captureFile);
572         if (device_selector->currentIndex() == FIREWIRE) {
573             m_discAction->setIcon(KIcon("network-connect"));
574             m_discAction->setText(i18n("Connect"));
575             m_playAction->setEnabled(false);
576             m_rewAction->setEnabled(false);
577             m_fwdAction->setEnabled(false);
578             m_recAction->setEnabled(false);
579         }
580         m_isPlaying = false;
581         m_playAction->setIcon(m_playIcon);
582         m_recAction->setChecked(false);
583         m_stopAction->setEnabled(false);
584         device_selector->setEnabled(true);
585         if (m_captureProcess && m_captureProcess->exitStatus() == QProcess::CrashExit) {
586             video_frame->setText(i18n("Capture crashed, please check your parameters"));
587         } else {
588             if (device_selector->currentIndex() != SCREENGRAB) video_frame->setText(i18n("Not connected"));
589             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())));
590         }
591         m_isCapturing = false;
592
593 #if KDE_IS_VERSION(4,2,0)
594         m_spaceTimer.stop();
595         // update free space info
596         slotUpdateFreeSpace();
597 #endif
598
599     } else {
600         if (device_selector->currentIndex() != SCREENGRAB) m_stopAction->setEnabled(true);
601         device_selector->setEnabled(false);
602     }
603 }
604
605 void RecMonitor::manageCapturedFiles()
606 {
607     QString extension;
608     switch (KdenliveSettings::firewireformat()) {
609     case 0:
610         extension = ".dv";
611         break;
612     case 1:
613     case 2:
614         extension = ".avi";
615         break;
616     case 3:
617         extension = ".m2t";
618         break;
619     }
620     QDir dir(KdenliveSettings::capturefolder());
621     QStringList filters;
622     QString capturename = KdenliveSettings::dvgrabfilename();
623     if (capturename.isEmpty()) capturename = "capture";
624     filters << capturename + "*" + extension;
625     const QStringList result = dir.entryList(filters, QDir::Files, QDir::Time);
626     KUrl::List capturedFiles;
627     foreach(const QString &name, result) {
628         KUrl url = KUrl(dir.filePath(name));
629         if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) {
630             KFileItem file(KFileItem::Unknown, KFileItem::Unknown, url, true);
631             if (file.time(KFileItem::ModificationTime) > m_captureTime) capturedFiles.append(url);
632         }
633     }
634     kDebug() << "Found : " << capturedFiles.count() << " new capture files";
635     kDebug() << capturedFiles;
636
637     if (capturedFiles.count() > 0) {
638         ManageCapturesDialog *d = new ManageCapturesDialog(capturedFiles, this);
639         if (d->exec() == QDialog::Accepted) {
640             capturedFiles = d->importFiles();
641             foreach(const KUrl &url, capturedFiles) {
642                 emit addProjectClip(url);
643             }
644         }
645         delete d;
646     }
647 }
648
649 // virtual
650 void RecMonitor::mousePressEvent(QMouseEvent * /*event*/)
651 {
652 #if KDE_IS_VERSION(4,2,0)
653     if (m_freeSpace->underMouse()) slotUpdateFreeSpace();
654 #endif
655 }
656
657 void RecMonitor::slotUpdateFreeSpace()
658 {
659 #if KDE_IS_VERSION(4,2,0)
660     KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(KdenliveSettings::capturefolder());
661     if (info.isValid()) {
662         m_freeSpace->setValue(100 * info.used() / info.size());
663         m_freeSpace->setText(i18n("Free space: %1", KIO::convertSize(info.available())));
664         m_freeSpace->update();
665     }
666 #endif
667 }
668
669 void RecMonitor::activateRecMonitor()
670 {
671     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
672 }
673
674 void RecMonitor::stop()
675 {
676     m_isActive = false;
677
678 }
679
680 void RecMonitor::start()
681 {
682     m_isActive = true;
683
684 }
685
686 void RecMonitor::refreshRecMonitor(bool visible)
687 {
688     if (visible) {
689         //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
690
691     }
692 }
693
694 void RecMonitor::slotPlay()
695 {
696
697     //if (!m_isActive) m_monitorManager->activateRecMonitor(m_name);
698
699 }
700
701 void RecMonitor::slotReadDvgrabInfo()
702 {
703     QString data = m_captureProcess->readAllStandardError().simplified();
704     data = data.section('"', 2, 2).simplified();
705     m_dvinfo.setText(data.left(11));
706     m_dvinfo.updateGeometry();
707 }
708
709 #include "recmonitor.moc"
710