removed many deprecated interfaces. made variant type an abstractproperty
[profile/ivi/automotive-message-broker.git] / plugins / common / varianttype.cpp
1 /*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19
20 #include "varianttype.h"
21 #include "abstractdbusinterface.h"
22 #include <listplusplus.h>
23
24 VariantType::VariantType(AbstractRoutingEngine* re, VehicleProperty::Property ambPropertyName, std::string propertyName,  Access access)
25         :mPropertyName(propertyName), mAccess(access), mValue(nullptr), mZoneFilter(Zone::None), mUpdateFrequency(0), mInitialized(false)
26 {
27         mAmbPropertyName = ambPropertyName;
28         routingEngine = re;
29         //set default value:
30         setValue(VehicleProperty::getPropertyTypeForPropertyNameValue(mAmbPropertyName));
31 }
32
33 void VariantType::initialize()
34 {
35         if(mInitialized) return;
36         AsyncPropertyRequest request;
37         request.property = mAmbPropertyName;
38         request.sourceUuidFilter = mSourceFilter;
39         request.zoneFilter = mZoneFilter;
40
41         using namespace std::placeholders;
42         request.completed = [this](AsyncPropertyReply* r)
43         {
44                 auto reply = amb::make_unique(r);
45                 if(reply->success)
46                         setValue(reply->value->copy());
47                 else
48                         DebugOut(DebugOut::Error)<<"get request unsuccessful for "<<reply->property<<" : "<<reply->error<<endl;
49
50                 mInitialized = true;
51         };
52
53         /// do not request if not supported:
54         PropertyList proplist = routingEngine->supported();
55
56         if(contains(proplist, mAmbPropertyName))
57                 routingEngine->getPropertyAsync(request);
58 }
59
60 VariantType::~VariantType()
61 {
62         if(mValue){
63                 delete mValue;
64                 mValue = nullptr;
65         }
66 }
67
68 GVariant *VariantType::toVariant()
69 {
70         if(!value())
71         {
72                 setValue(VehicleProperty::getPropertyTypeForPropertyNameValue(name()));
73         }
74
75         auto v = value();
76
77         return v->toVariant();
78 }
79
80 void VariantType::fromVariant(GVariant *val)
81 {
82         AbstractPropertyType *v = VehicleProperty::getPropertyTypeForPropertyNameValue(name());
83         v->fromVariant(val);
84
85         AsyncSetPropertyRequest request;
86         request.property = name();
87         request.value = v;
88         request.zoneFilter = zoneFilter();
89         request.completed = [&](AsyncPropertyReply* r)
90         {
91                 auto reply = amb::make_unique(r);
92                 /// TODO: throw dbus exception
93                 if(!reply->success)
94                 {
95                         DebugOut(DebugOut::Error)<<"SetProperty fail: "<<reply->error<<endl;
96                 }
97         };
98
99         routingEngine->setProperty(request);
100 }