[Tizen] Fix Coverity issues
[platform/core/uifw/dali-adaptor.git] / dali / internal / styling / common / style-monitor-impl.cpp
1 /*
2  * Copyright (c) 2014 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 <dali/internal/styling/common/style-monitor-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <fstream>
24 #include <sstream>
25 #include <dali/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/internal/adaptor/common/adaptor-impl.h>
29 #include <dali/internal/system/common/singleton-service-impl.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 namespace
41 {
42
43 #if defined(DEBUG_ENABLED)
44 Dali::Integration::Log::Filter* gLogFilter = Dali::Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_STYLE_MONITOR");
45 #endif
46
47 /**
48  * Use font client to get the system default font family
49  * @param[in] fontClient handle to font client
50  * @param[out] fontFamily string representing font family
51  */
52 void GetSystemDefaultFontFamily( TextAbstraction::FontClient& fontClient, std::string& fontFamily )
53 {
54   TextAbstraction::FontDescription defaultFontDescription;
55   if ( fontClient )
56   {
57     fontClient.GetDefaultPlatformFontDescription( defaultFontDescription );
58     fontFamily = defaultFontDescription.family;
59   }
60 }
61
62 } // unnamed namespace
63
64 Dali::StyleMonitor StyleMonitor::Get()
65 {
66   Dali::StyleMonitor styleMonitor;
67
68   Dali::SingletonService service( SingletonService::Get() );
69   if( service )
70   {
71     // Check whether the singleton is already created
72     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::StyleMonitor ) );
73     if( handle )
74     {
75       // If so, downcast the handle
76       styleMonitor = Dali::StyleMonitor( dynamic_cast< StyleMonitor* >( handle.GetObjectPtr() ) );
77     }
78     else
79     {
80       styleMonitor = Dali::StyleMonitor( new StyleMonitor() );
81       service.Register( typeid( styleMonitor ), styleMonitor );
82     }
83   }
84
85   return styleMonitor;
86 }
87
88 StyleMonitor::StyleMonitor()
89 : mDefaultFontSize(-1)
90 {
91   mFontClient = TextAbstraction::FontClient::Get();
92   GetSystemDefaultFontFamily( mFontClient, mDefaultFontFamily );
93   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "StyleMonitor::StyleMonitor::DefaultFontFamily(%s)\n", mDefaultFontFamily.c_str() );
94   mDefaultFontSize = mFontClient.GetDefaultFontSize();
95 }
96
97 StyleMonitor::~StyleMonitor()
98 {
99 }
100
101 void StyleMonitor::StyleChanged( StyleChange::Type styleChange )
102 {
103   switch ( styleChange )
104   {
105     case StyleChange::DEFAULT_FONT_CHANGE:
106     {
107       if ( mFontClient )
108       {
109         mFontClient.ResetSystemDefaults();
110         GetSystemDefaultFontFamily( mFontClient, mDefaultFontFamily );
111       }
112       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "StyleMonitor::StyleChanged::DefaultFontFamily(%s)\n", mDefaultFontFamily.c_str() );
113       break;
114     }
115
116     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
117     {
118       mDefaultFontSize = mFontClient.GetDefaultFontSize();
119       break;
120     }
121
122     case StyleChange::THEME_CHANGE:
123     {
124       break;
125     }
126   }
127
128   EmitStyleChangeSignal(styleChange);
129 }
130
131 std::string StyleMonitor::GetDefaultFontFamily() const
132 {
133   return mDefaultFontFamily;
134 }
135
136 std::string StyleMonitor::GetDefaultFontStyle() const
137 {
138   return mDefaultFontStyle;
139 }
140
141 int StyleMonitor::GetDefaultFontSize() const
142 {
143   return mDefaultFontSize;
144 }
145
146 const std::string& StyleMonitor::GetTheme() const
147 {
148   return mUserDefinedThemeFilePath;
149 }
150
151 void StyleMonitor::SetTheme(const std::string& path)
152 {
153   mUserDefinedThemeFilePath = path;
154   EmitStyleChangeSignal( StyleChange::THEME_CHANGE );
155 }
156
157 bool StyleMonitor::LoadThemeFile( const std::string& filename, std::string& output )
158 {
159   bool retval( false );
160   std::ifstream in( filename.c_str(), std::ios::in );
161   if( in )
162   {
163     std::stringstream buffer;
164     buffer << in.rdbuf();
165
166     output = buffer.str();
167
168     in.close();
169     retval = true;
170   }
171   return retval;
172 }
173
174 Dali::StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
175 {
176   return mStyleChangeSignal;
177 }
178
179 void StyleMonitor::EmitStyleChangeSignal( StyleChange::Type styleChange )
180 {
181   if( !mStyleChangeSignal.Empty() )
182   {
183     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "StyleMonitor::EmitStyleChangeSignal\n" );
184     Dali::StyleMonitor handle( this );
185     mStyleChangeSignal.Emit( handle, styleChange );
186   }
187 }
188
189 } // namespace Adaptor
190
191 } // namespace Internal
192
193 } // namespace Dali