Merge branch 'devel/master (1.1.16)' into tizen
[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 <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 <adaptor-impl.h>
29 #include <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 BaseHandle Create()
48 {
49   BaseHandle handle( StyleMonitor::Get() );
50
51   if ( !handle && Adaptor::IsAvailable() )
52   {
53     Dali::SingletonService service( SingletonService::Get() );
54     if ( service )
55     {
56       Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
57       Dali::StyleMonitor styleMonitor = Dali::StyleMonitor( new StyleMonitor( adaptorImpl.GetPlatformAbstraction() ) );
58       service.Register( typeid( styleMonitor ), styleMonitor );
59       handle = styleMonitor;
60     }
61   }
62
63   return handle;
64 }
65 TypeRegistration STYLE_MONITOR_TYPE( typeid(Dali::StyleMonitor), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
66
67 /**
68  * Use font client to get the system default font family
69  * @param[in] fontClient handle to font client
70  * @param[out] fontFamily string representing font family
71  */
72 void GetSystemDefaultFontFamily( TextAbstraction::FontClient& fontClient, std::string& fontFamily )
73 {
74   TextAbstraction::FontDescription defaultFontDescription;
75   if ( fontClient )
76   {
77     fontClient.GetDefaultPlatformFontDescription( defaultFontDescription );
78     fontFamily = defaultFontDescription.family;
79   }
80 }
81
82 } // unnamed namespace
83
84 Dali::StyleMonitor StyleMonitor::Get()
85 {
86   Dali::StyleMonitor styleMonitor;
87
88   Dali::SingletonService service( SingletonService::Get() );
89   if ( service )
90   {
91     // Check whether the singleton is already created
92     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::StyleMonitor ) );
93     if(handle)
94     {
95       // If so, downcast the handle
96       styleMonitor = Dali::StyleMonitor( dynamic_cast< StyleMonitor* >( handle.GetObjectPtr() ) );
97     }
98   }
99
100   return styleMonitor;
101 }
102
103 StyleMonitor::StyleMonitor(Integration::PlatformAbstraction& platformAbstraction)
104 : mPlatformAbstraction(platformAbstraction),
105   mDefaultFontSize(-1)
106 {
107   mFontClient = TextAbstraction::FontClient::Get();
108   GetSystemDefaultFontFamily( mFontClient, mDefaultFontFamily );
109   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "StyleMonitor::StyleMonitor::DefaultFontFamily(%s)\n", mDefaultFontFamily.c_str() );
110   mDefaultFontSize = mPlatformAbstraction.GetDefaultFontSize();
111 }
112
113 StyleMonitor::~StyleMonitor()
114 {
115 }
116
117 void StyleMonitor::StyleChanged( StyleChange::Type styleChange )
118 {
119   switch ( styleChange )
120   {
121     case StyleChange::DEFAULT_FONT_CHANGE:
122     {
123       if ( mFontClient )
124       {
125         mFontClient.ResetSystemDefaults();
126         GetSystemDefaultFontFamily( mFontClient, mDefaultFontFamily );
127       }
128       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "StyleMonitor::StyleChanged::DefaultFontFamily(%s)\n", mDefaultFontFamily.c_str() );
129       break;
130     }
131
132     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
133     {
134       mDefaultFontSize = mPlatformAbstraction.GetDefaultFontSize();
135       break;
136     }
137
138     case StyleChange::THEME_CHANGE:
139     {
140       break;
141     }
142   }
143
144   EmitStyleChangeSignal(styleChange);
145 }
146
147 std::string StyleMonitor::GetDefaultFontFamily() const
148 {
149   return mDefaultFontFamily;
150 }
151
152 std::string StyleMonitor::GetDefaultFontStyle() const
153 {
154   return mDefaultFontStyle;
155 }
156
157 int StyleMonitor::GetDefaultFontSize() const
158 {
159   return mDefaultFontSize;
160 }
161
162 const std::string& StyleMonitor::GetTheme() const
163 {
164   return mUserDefinedThemeFilePath;
165 }
166
167 void StyleMonitor::SetTheme(const std::string& path)
168 {
169   mUserDefinedThemeFilePath = path;
170   EmitStyleChangeSignal( StyleChange::THEME_CHANGE );
171 }
172
173 bool StyleMonitor::LoadThemeFile( const std::string& filename, std::string& output )
174 {
175   bool retval( false );
176   std::ifstream in( filename.c_str(), std::ios::in );
177   if( in )
178   {
179     std::stringstream buffer;
180     buffer << in.rdbuf();
181
182     output = buffer.str();
183
184     in.close();
185     retval = true;
186   }
187   return retval;
188 }
189
190 Dali::StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
191 {
192   return mStyleChangeSignal;
193 }
194
195 void StyleMonitor::EmitStyleChangeSignal( StyleChange::Type styleChange )
196 {
197   if( !mStyleChangeSignal.Empty() )
198   {
199     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "StyleMonitor::EmitStyleChangeSignal\n" );
200     Dali::StyleMonitor handle( this );
201     mStyleChangeSignal.Emit( handle, styleChange );
202   }
203 }
204
205 } // namespace Adaptor
206
207 } // namespace Internal
208
209 } // namespace Dali