Kill CameraButtonRow and use the QtQuick Row component
[harmattan/cameraplus] / lib / qtcammetadata.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 "qtcammetadata.h"
22 #include <gst/gsttaglist.h>
23 #include "qtcamdevice.h"
24 #include "qtcamdevice_p.h"
25 #include <QDebug>
26 #include <QDate>
27 #include <QTime>
28 #include <QDateTime>
29 #include <QPointer>
30
31 const char *orientations[] = {
32   "rotate-0",
33   "rotate-90",
34   "rotate-180",
35   "rotate-270"
36 };
37
38 class QtCamMetaDataPrivate {
39 public:
40   void addTag(const char *tag, const QString& value) {
41     GstTagSetter *s = setter();
42     if (!s) {
43       return;
44     }
45
46     gst_tag_setter_add_tags(s, GST_TAG_MERGE_REPLACE, tag, value.toUtf8().data(), NULL);
47
48     gst_object_unref(s);
49   }
50
51   void addTag(const char *tag, double value) {
52     GstTagSetter *s = setter();
53     if (!s) {
54       return;
55     }
56
57     gst_tag_setter_add_tags(s, GST_TAG_MERGE_REPLACE, tag, value, NULL);
58
59     gst_object_unref(s);
60   }
61
62   void addTag(const char *tag, GstDateTime *value) {
63     GstTagSetter *s = setter();
64     if (!s) {
65       return;
66     }
67
68     gst_tag_setter_add_tags(s, GST_TAG_MERGE_REPLACE, tag, value, NULL);
69
70     gst_object_unref(s);
71   }
72
73   GstTagSetter *setter() {
74     if (!device || !device->d_ptr->cameraBin) {
75       return 0;
76     }
77
78     if (!GST_IS_TAG_SETTER(device->d_ptr->cameraBin)) {
79       return 0;
80     }
81
82     return GST_TAG_SETTER(gst_object_ref(device->d_ptr->cameraBin));
83   }
84
85   QPointer<QtCamDevice> device;
86 };
87
88 QtCamMetaData::QtCamMetaData(QObject *parent) :
89   QObject(parent), d_ptr(new QtCamMetaDataPrivate) {
90 }
91
92 QtCamMetaData::~QtCamMetaData() {
93   setDevice(0);
94   delete d_ptr; d_ptr = 0;
95 }
96
97 void QtCamMetaData::setDevice(QtCamDevice *device) {
98   if (device != d_ptr->device) {
99     d_ptr->device = device;
100   }
101 }
102
103 void QtCamMetaData::setManufacturer(const QString& manufacturer) {
104   d_ptr->addTag(GST_TAG_DEVICE_MANUFACTURER, manufacturer);
105 }
106
107 void QtCamMetaData::setModel(const QString& model) {
108   d_ptr->addTag(GST_TAG_DEVICE_MODEL, model);
109 }
110
111 void QtCamMetaData::setCountry(const QString& country) {
112   d_ptr->addTag(GST_TAG_GEO_LOCATION_COUNTRY, country);
113 }
114
115 void QtCamMetaData::setCity(const QString& city) {
116   d_ptr->addTag(GST_TAG_GEO_LOCATION_CITY, city);
117 }
118
119 void QtCamMetaData::setSuburb(const QString& suburb) {
120   d_ptr->addTag(GST_TAG_GEO_LOCATION_SUBLOCATION, suburb);
121 }
122
123 void QtCamMetaData::setLongitude(double longitude) {
124   d_ptr->addTag(GST_TAG_GEO_LOCATION_LONGITUDE, longitude);
125 }
126
127 void QtCamMetaData::setLatitude(double latitude) {
128   d_ptr->addTag(GST_TAG_GEO_LOCATION_LATITUDE, latitude);
129 }
130
131 void QtCamMetaData::setElevation(double elevation) {
132   d_ptr->addTag(GST_TAG_GEO_LOCATION_ELEVATION, elevation);
133 }
134
135 void QtCamMetaData::setOrientation(Orientation orientation) {
136   int len = sizeof(orientations) / sizeof(orientations[0]);
137
138   if (orientation <= 0 || orientation >= len) {
139     orientation = Landscape;
140   }
141
142   d_ptr->addTag(GST_TAG_IMAGE_ORIENTATION, orientations[orientation]);
143 }
144
145 void QtCamMetaData::setArtist(const QString& artist) {
146   d_ptr->addTag(GST_TAG_ARTIST, artist);
147 }
148
149 void QtCamMetaData::setDateTime(const QDateTime& dateTime) {
150   QDate d = dateTime.date();
151   QTime t = dateTime.time();
152
153   int day = d.day();
154   int month = d.month();
155   int year = d.year();
156   int hour = t.hour();
157   int minute = t.minute();
158
159   // GstDateTime seconds expects microseconds to be there too :|
160   gdouble seconds = t.second();
161   seconds += t.msec()/(1000.0);
162
163   // Current UTC time. Created through string so that the link between
164   // current and utc is lost and secsTo returns non-zero values.
165   QDateTime utcTime = QDateTime::fromString(dateTime.toUTC().toString());
166   gfloat tzoffset = (utcTime.secsTo(dateTime)/3600.0);
167
168   GstDateTime *dt = gst_date_time_new(tzoffset, year, month, day,
169                                       hour, minute, seconds);
170
171   d_ptr->addTag(GST_TAG_DATE_TIME, dt);
172
173   gst_date_time_unref(dt);
174 }
175
176 void QtCamMetaData::setCaptureDirection(double direction) {
177   d_ptr->addTag(GST_TAG_GEO_LOCATION_CAPTURE_DIRECTION, direction);
178 }
179
180 void QtCamMetaData::setHorizontalError(double error) {
181   d_ptr->addTag(GST_TAG_GEO_LOCATION_HORIZONTAL_ERROR, error);
182 }
183
184 void QtCamMetaData::reset() {
185   GstTagSetter *s = d_ptr->setter();
186   if (!s) {
187     return;
188   }
189
190   gst_tag_setter_reset_tags(s);
191
192   gst_object_unref(s);
193 }