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