Merge "Do not clear keyboard focus on touch when alwaysShowFocus is true." into devel...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-url.cpp
1 /*
2  * Copyright (c) 2017 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 // CLASS HEADER
18 #include <dali-toolkit/internal/visuals/visual-url.h>
19
20 // EXTERNAL HEADERS
21 #include <cstring> // for toupper()
22
23 namespace Dali
24 {
25 namespace Toolkit
26 {
27 namespace Internal
28 {
29
30 namespace
31 {
32
33 VisualUrl::ProtocolType ResolveLocation( const std::string& url )
34 {
35   const char* urlCStr = url.c_str();
36   const uint32_t length = url.size();
37   if( ( length > 7 ) && urlCStr[5] == ':' && urlCStr[6] == '/' && urlCStr[7] == '/' )
38   {
39     // https://
40     if( ( 'h' == tolower( urlCStr[0] ) )&&
41         ( 't' == tolower( urlCStr[1] ) )&&
42         ( 't' == tolower( urlCStr[2] ) )&&
43         ( 'p' == tolower( urlCStr[3] ) )&&
44         ( 's' == tolower( urlCStr[4] ) ) )
45     {
46       return VisualUrl::REMOTE;
47     }
48   }
49   else if( ( length > 6 ) && urlCStr[4] == ':' && urlCStr[5] == '/' && urlCStr[6] == '/' )
50   {
51     // http:// or dali://
52     const char hOrd = tolower( urlCStr[0] );
53     const char tOra = tolower( urlCStr[1] );
54     const char tOrl = tolower( urlCStr[2] );
55     const char pOri = tolower( urlCStr[3] );
56     if( ( 'h' == hOrd )&&
57         ( 't' == tOra )&&
58         ( 't' == tOrl )&&
59         ( 'p' == pOri ) )
60     {
61       return VisualUrl::REMOTE;
62     }
63     if( ( 'd' == hOrd )&&
64         ( 'a' == tOra )&&
65         ( 'l' == tOrl )&&
66         ( 'i' == pOri ) )
67     {
68       return VisualUrl::TEXTURE;
69     }
70   }
71   else if( ( length > 5 ) && urlCStr[3] == ':' && urlCStr[4] == '/' && urlCStr[5] == '/' )
72   {
73     // ftp:// or ssh://
74     const char fOrS = tolower( urlCStr[0] );
75     if( ( 'f' == fOrS )||( 's' == fOrS ) )
76     {
77       const char tOrs = tolower( urlCStr[1] );
78       if( ( 't' == tOrs )||( 's' == tOrs ) )
79       {
80         const char pOrh = tolower( urlCStr[2] );
81         if( ( 'p' == pOrh )||( 'h' == pOrh ) )
82         {
83           return VisualUrl::REMOTE;
84         }
85       }
86     }
87   }
88   return VisualUrl::LOCAL;
89 }
90
91
92 VisualUrl::Type ResolveType( const std::string& url )
93 {
94   // if only one char in string, can only be regular image
95   const std::size_t count = url.size();
96   if( count > 0 )
97   {
98     // parsing from the end for better chance of early outs
99     enum { SUFFIX, HASH, HASH_DOT } state = SUFFIX;
100     char SVG[ 4 ] = { 'g', 'v', 's', '.' };
101     char GIF[ 4 ] = { 'f', 'i', 'g', '.' };
102     unsigned int svgScore = 0;
103     unsigned int gifScore = 0;
104     int index = count;
105     while( --index >= 0 )
106     {
107       const char currentChar = tolower( url[ index ] );
108       const std::size_t offsetFromEnd = count - index - 1u;
109       if( ( offsetFromEnd < sizeof(SVG) )&&( currentChar == SVG[ offsetFromEnd ] ) )
110       {
111         // early out if SVG as can't be used in N patch for now
112         if( ++svgScore == sizeof(SVG) )
113         {
114           return VisualUrl::SVG;
115         }
116       }
117       if( ( offsetFromEnd < sizeof(GIF) )&&( currentChar == GIF[ offsetFromEnd ] ) )
118       {
119         // early out if GIF as can't be used in N patch for now
120         if( ++gifScore == sizeof(GIF) )
121         {
122           return VisualUrl::GIF;
123         }
124       }
125       switch( state )
126       {
127         case SUFFIX:
128         {
129           if( '.' == currentChar )
130           {
131             state = HASH;
132           }
133           break;
134         }
135         case HASH:
136         {
137           if( ( '#' == currentChar ) || ( '9' == currentChar ) )
138           {
139             state = HASH_DOT;
140           }
141           else
142           {
143             // early out, not a valid N/9-patch URL
144             return VisualUrl::REGULAR_IMAGE;
145           }
146           break;
147         }
148         case HASH_DOT:
149         {
150           if( '.' == currentChar )
151           {
152             return VisualUrl::N_PATCH;
153           }
154           else
155           {
156             // early out, not a valid N/9-patch URL
157             return VisualUrl::REGULAR_IMAGE;
158           }
159           break;
160         }
161       }
162     }
163   }
164   // if we got here it is a regular image
165   return VisualUrl::REGULAR_IMAGE;
166 }
167
168 }
169
170
171 VisualUrl::VisualUrl()
172 : mUrl(),
173   mType( VisualUrl::REGULAR_IMAGE ),
174   mLocation( VisualUrl::LOCAL )
175 {
176 }
177
178 VisualUrl::VisualUrl( const std::string& url )
179 : mUrl( url ),
180   mType( VisualUrl::REGULAR_IMAGE ),
181   mLocation( VisualUrl::LOCAL )
182 {
183   if( ! url.empty() )
184   {
185     mLocation = ResolveLocation( url );
186     if( VisualUrl::TEXTURE != mLocation )
187     {
188       // TEXTURE location url doesn't need type resolving, REGULAR_IMAGE is fine
189       mType = ResolveType( url );
190     }
191   }
192 }
193
194 VisualUrl::VisualUrl( const VisualUrl& url )
195 : mUrl( url.mUrl ),
196   mType( url.mType ),
197   mLocation( url.mLocation )
198 {
199 }
200
201 VisualUrl& VisualUrl::operator=( const VisualUrl& url )
202 {
203   if( &url != this )
204   {
205     mUrl = url.mUrl;
206     mType = url.mType;
207     mLocation = url.mLocation;
208   }
209   return *this;
210 }
211
212 const std::string& VisualUrl::GetUrl() const
213 {
214   return mUrl;
215 }
216
217 VisualUrl::Type VisualUrl::GetType() const
218 {
219   return mType;
220 }
221
222 VisualUrl::ProtocolType VisualUrl::GetProtocolType() const
223 {
224   return mLocation;
225 }
226
227 bool VisualUrl::IsValid() const
228 {
229   return mUrl.size() > 0u;
230 }
231
232 bool VisualUrl::IsLocalResource() const
233 {
234   return mLocation == VisualUrl::LOCAL;
235 }
236
237 std::string VisualUrl::GetLocation()
238 {
239   const auto location = mUrl.find( "://" );
240   if( std::string::npos != location )
241   {
242     return mUrl.substr( location + 3u ); // 3 characters forwards from the start of ://
243   }
244   return mUrl;
245 }
246
247 std::string VisualUrl::CreateTextureUrl( const std::string& location )
248 {
249   return "dali://" + location;
250 }
251
252 } // Internal
253
254 } // Toolkit
255
256 } // Dali