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