[4.0] Fix memory leak in InputMethodOptions
[platform/core/uifw/dali-adaptor.git] / adaptors / devel-api / adaptor-framework / input-method-options.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <input-method-options.h>
20
21 // INTERNAL INCLUDES
22 #include <input-method-devel.h>
23
24 using namespace Dali::InputMethod;
25 using namespace Dali::InputMethod::Category;
26
27 namespace Dali
28 {
29
30 #define TOKEN_STRING(x) #x
31
32 struct InputMethodOptions::Impl
33 {
34   Impl()
35   {
36     mPanelLayout = PanelLayout::NORMAL;
37     mAutoCapital = AutoCapital::SENTENCE;
38     mActionButton = ActionButtonTitle::DEFAULT;
39     mVariation = NormalLayout::NORMAL;
40   }
41
42   PanelLayout::Type mPanelLayout;
43   AutoCapital::Type mAutoCapital;
44   ActionButtonTitle::Type mActionButton;
45   int mVariation:4;
46 };
47
48 InputMethodOptions::InputMethodOptions()
49 {
50   mImpl.reset(new Impl());
51 }
52
53 InputMethodOptions::~InputMethodOptions()
54 {
55   // destructor cannot be inlined and must be in a unit
56   // for unique_ptr to work with forward declaration
57 }
58
59 bool InputMethodOptions::IsPassword()
60 {
61   return (mImpl->mPanelLayout == Dali::InputMethod::PanelLayout::PASSWORD);
62 }
63
64 void InputMethodOptions::ApplyProperty( const Property::Map& settings )
65 {
66   for ( unsigned int i = 0, count = settings.Count(); i < count; ++i )
67   {
68     Property::Key key = settings.GetKeyAt( i );
69     if( key.type == Property::Key::INDEX )
70     {
71       continue;
72     }
73
74     Property::Value item = settings.GetValue(i);
75
76     if( key == TOKEN_STRING( PANEL_LAYOUT ) )
77     {
78       if( item.GetType() == Property::INTEGER )
79       {
80         int value = item.Get< int >();
81         mImpl->mPanelLayout = static_cast<InputMethod::PanelLayout::Type>(value);
82       }
83     }
84     else if ( key == TOKEN_STRING( AUTO_CAPITALISE ) )
85     {
86       if ( item.GetType() == Property::INTEGER )
87       {
88         int value = item.Get< int >();
89         mImpl->mAutoCapital = static_cast<InputMethod::AutoCapital::Type>(value);
90       }
91     }
92     else if ( key == TOKEN_STRING( ACTION_BUTTON ) )
93     {
94       if ( item.GetType() == Property::INTEGER )
95       {
96         int value = item.Get< int >();
97         mImpl->mActionButton = static_cast<InputMethod::ActionButtonTitle::Type>(value);
98       }
99     }
100     else if( key == TOKEN_STRING( VARIATION ) )
101     {
102       if( item.GetType() == Property::INTEGER )
103       {
104         int value = item.Get< int >();
105         mImpl->mVariation = value;
106       }
107     }
108     else
109     {
110     }
111   }
112 }
113
114 void InputMethodOptions::RetrieveProperty( Property::Map& settings )
115 {
116   settings[TOKEN_STRING( PANEL_LAYOUT )] = mImpl->mPanelLayout;
117   settings[TOKEN_STRING( AUTO_CAPITALISE )] = mImpl->mAutoCapital;
118   settings[TOKEN_STRING( ACTION_BUTTON )] = mImpl->mActionButton;
119   settings[TOKEN_STRING( VARIATION )] = mImpl->mVariation;
120 }
121
122 bool InputMethodOptions::CompareAndSet( InputMethod::Category::Type type, const InputMethodOptions& options, int& index)
123 {
124   bool updated = false;
125
126   switch (type)
127   {
128     case PANEL_LAYOUT:
129     {
130       if ( options.mImpl->mPanelLayout != mImpl->mPanelLayout )
131       {
132         mImpl->mPanelLayout = options.mImpl->mPanelLayout;
133         index = static_cast<int>(mImpl->mPanelLayout);
134         updated = true;
135       }
136       break;
137     }
138     case AUTO_CAPITALISE:
139     {
140       if ( options.mImpl->mAutoCapital != mImpl->mAutoCapital )
141       {
142         mImpl->mAutoCapital = options.mImpl->mAutoCapital;
143         index = static_cast<int>(mImpl->mAutoCapital);
144         updated = true;
145       }
146       break;
147     }
148     case ACTION_BUTTON_TITLE:
149     {
150       if ( options.mImpl->mActionButton != mImpl->mActionButton )
151       {
152         mImpl->mActionButton = options.mImpl->mActionButton;
153         index = static_cast<int>(mImpl->mActionButton);
154         updated = true;
155       }
156       break;
157     }
158     case VARIATION:
159     {
160       if ( options.mImpl->mVariation != mImpl->mVariation )
161       {
162         mImpl->mVariation = options.mImpl->mVariation;
163         index = static_cast<int>(mImpl->mVariation);
164         updated = true;
165       }
166       break;
167     }
168   }
169   return updated;
170 }
171
172 } // namespace Dali