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