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