Move qt_cam_copy_register to QtCamera
[harmattan/cameraplus] / lib / qtcamera.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "qtcamera.h"
22 #include "qtcamscanner.h"
23 #include "qtcamconfig.h"
24 #include "qtcamdevice.h"
25 #include "gst/gstcopy.h"
26 #include <gst/gst.h>
27
28 class QtCameraPrivate {
29 public:
30   QtCameraPrivate() {
31     qt_cam_copy_register();
32   }
33
34   QtCamConfig *conf;
35   QtCamScanner *scanner;
36 };
37
38 QtCamera::QtCamera(QObject *parent) :
39   QObject(parent), d_ptr(new QtCameraPrivate) {
40
41   gst_init(0, 0);
42
43   d_ptr->conf = new QtCamConfig(this);
44   d_ptr->scanner = new QtCamScanner(d_ptr->conf, this);
45
46   refreshDevices();
47 }
48
49 QtCamera::QtCamera(const QString& configPath, QObject *parent) :
50   QObject(parent), d_ptr(new QtCameraPrivate) {
51
52   gst_init(0, 0);
53
54   d_ptr->conf = new QtCamConfig(configPath, this);
55   d_ptr->scanner = new QtCamScanner(d_ptr->conf, this);
56
57   refreshDevices();
58 }
59
60 QtCamera::QtCamera(QtCamConfig *config, QObject *parent) :
61   QObject(parent), d_ptr(new QtCameraPrivate) {
62
63   gst_init(0, 0);
64
65   d_ptr->conf = config;
66   d_ptr->scanner = new QtCamScanner(d_ptr->conf, this);
67
68   refreshDevices();
69 }
70
71 QtCamera::~QtCamera() {
72   delete d_ptr; d_ptr = 0;
73
74   gst_deinit();
75 }
76
77 void QtCamera::refreshDevices() {
78   d_ptr->scanner->refresh();
79 }
80
81 QList<QPair<QString, QVariant> > QtCamera::devices() const {
82   return d_ptr->scanner->devices();
83 }
84
85 QtCamDevice *QtCamera::device(const QVariant& id, QObject *parent) {
86   QList<QPair<QString, QVariant> > devs = devices();
87
88   // G++ barfs with foreach and class templates.
89   typedef QPair<QString, QVariant> Dev;
90   foreach (const Dev& dev, devs) {
91     if (dev.second == id) {
92       return new QtCamDevice(d_ptr->conf, dev.first, dev.second, parent ? parent : this);
93     }
94   }
95
96   return 0;
97 }
98
99 QtCamConfig *QtCamera::config() const {
100   return d_ptr->conf;
101 }