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