Merge "(Orientation) Move to devel-api" 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
26 // INTERNAL INCLUDES
27 #include <adaptor-impl.h>
28 #include <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 BaseHandle Create()
43 {
44   BaseHandle handle( StyleMonitor::Get() );
45
46   if ( !handle && Adaptor::IsAvailable() )
47   {
48     Dali::SingletonService service( SingletonService::Get() );
49     if ( service )
50     {
51       Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
52       Dali::StyleMonitor styleMonitor = Dali::StyleMonitor( new StyleMonitor( adaptorImpl.GetPlatformAbstraction() ) );
53       service.Register( typeid( styleMonitor ), styleMonitor );
54       handle = styleMonitor;
55     }
56   }
57
58   return handle;
59 }
60 TypeRegistration STYLE_MONITOR_TYPE( typeid(Dali::StyleMonitor), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
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   }
79
80   return styleMonitor;
81 }
82
83 StyleMonitor::StyleMonitor(Integration::PlatformAbstraction& platformAbstraction)
84 : mPlatformAbstraction(platformAbstraction),
85   mDefaultFontSize(-1)
86 {
87   mPlatformAbstraction.GetDefaultFontDescription( mDefaultFontFamily, mDefaultFontStyle );
88   mDefaultFontSize = mPlatformAbstraction.GetDefaultFontSize();
89 }
90
91 StyleMonitor::~StyleMonitor()
92 {
93 }
94
95 void StyleMonitor::StyleChanged( StyleChange::Type styleChange )
96 {
97   switch ( styleChange )
98   {
99     case StyleChange::DEFAULT_FONT_CHANGE:
100     {
101       mPlatformAbstraction.GetDefaultFontDescription( mDefaultFontFamily, mDefaultFontStyle );
102       break;
103     }
104
105     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
106     {
107       mDefaultFontSize = mPlatformAbstraction.GetDefaultFontSize();
108       break;
109     }
110
111     case StyleChange::THEME_CHANGE:
112     {
113       break;
114     }
115   }
116
117   EmitStyleChangeSignal(styleChange);
118 }
119
120 std::string StyleMonitor::GetDefaultFontFamily() const
121 {
122   return mDefaultFontFamily;
123 }
124
125 std::string StyleMonitor::GetDefaultFontStyle() const
126 {
127   return mDefaultFontStyle;
128 }
129
130 int StyleMonitor::GetDefaultFontSize() const
131 {
132   return mDefaultFontSize;
133 }
134
135 const std::string& StyleMonitor::GetTheme() const
136 {
137   return mUserDefinedThemeFilePath;
138 }
139
140 void StyleMonitor::SetTheme(const std::string& path)
141 {
142   mUserDefinedThemeFilePath = path;
143   EmitStyleChangeSignal( StyleChange::THEME_CHANGE );
144 }
145
146 bool StyleMonitor::LoadThemeFile( const std::string& filename, std::string& output )
147 {
148   bool retval( false );
149   std::ifstream in( filename.c_str(), std::ios::in );
150   if( in )
151   {
152     std::stringstream buffer;
153     buffer << in.rdbuf();
154
155     output = buffer.str();
156
157     in.close();
158     retval = true;
159   }
160   return retval;
161 }
162
163 Dali::StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
164 {
165   return mStyleChangeSignal;
166 }
167
168 void StyleMonitor::EmitStyleChangeSignal( StyleChange::Type styleChange )
169 {
170   if( !mStyleChangeSignal.Empty() )
171   {
172     Dali::StyleMonitor handle( this );
173     mStyleChangeSignal.Emit( handle, styleChange );
174   }
175 }
176
177 } // namespace Adaptor
178
179 } // namespace Internal
180
181 } // namespace Dali