Refactor Lifecycles
[platform/core/csapi/tizenfx.git] / Tizen.Applications / Tizen.Applications / AppControlFilter.cs
1 /// Copyright 2016 by Samsung Electronics, Inc.,
2 ///
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
8
9
10 using System;
11 using System.Text.RegularExpressions;
12
13 namespace Tizen.Applications
14 {
15     /// <summary>
16     /// 
17     /// </summary>
18     [AttributeUsage(AttributeTargets.Class)]
19     public class AppControlFilter : Attribute
20     {
21         private readonly string _operation;
22         private readonly string _mime;
23         private readonly string _uri;
24
25         /// <summary>
26         /// 
27         /// </summary>
28         /// <param name="operation"></param>
29         /// <param name="mime"></param>
30         /// <param name="uri"></param>
31         public AppControlFilter(string operation, string mime = null, string uri = null)
32         {
33             _operation = operation;
34             _mime = mime;
35             _uri = uri;
36         }
37
38         /// <summary>
39         /// 
40         /// </summary>
41         public string Operation { get { return _operation; } }
42
43         /// <summary>
44         /// 
45         /// </summary>
46         public string Mime { get { return _mime; } }
47
48         /// <summary>
49         /// 
50         /// </summary>
51         public string Uri { get { return _uri; } }
52
53         /// <summary>
54         /// 
55         /// </summary>
56         /// <param name="obj"></param>
57         /// <returns></returns>
58         public override bool Equals(object obj)
59         {
60             AppControlFilter f = obj as AppControlFilter;
61             if (f == null) return false;
62             
63             return (_operation == f._operation) & (_mime == f._mime) & (_uri == f._uri);
64         }
65
66         /// <summary>
67         /// 
68         /// </summary>
69         /// <returns></returns>
70         public override int GetHashCode()
71         {
72             int hash = 0;
73             if (_operation != null)
74             {
75                 hash ^= _operation.GetHashCode();
76             }
77             if (_mime != null)
78             {
79                 hash ^= _mime.GetHashCode();
80             }
81             if (_uri != null)
82             {
83                 hash ^= _uri.GetHashCode();
84             }
85             return hash;
86         }
87
88         internal bool IsMatch(AppControl e)
89         {
90             string mime = e.Mime;
91             if (String.IsNullOrEmpty(mime) && !String.IsNullOrEmpty(e.Uri))
92             {
93                 mime = Interop.Aul.GetMimeFromUri(e.Uri);
94             }
95             return _operation == e.Operation && IsMimeMatched(mime) && IsUriMatched(e.Uri);
96         }
97
98         private bool IsMimeMatched(string mime)
99         {
100             if (_mime == "*" || _mime == "*/*")
101             {
102                 return true;
103             }
104             if (String.IsNullOrEmpty(_mime))
105             {
106                 return String.IsNullOrEmpty(mime);
107             }
108             Regex pat = new Regex(_mime.Replace("*", ".*"));
109             return pat.IsMatch(mime);
110         }
111
112         private bool IsUriMatched(string uri)
113         {
114             if (_uri == "*")
115             {
116                 return true;
117             }
118             if (String.IsNullOrEmpty(_uri))
119             {
120                 return String.IsNullOrEmpty(uri);
121             }
122             string schema = _uri.Split(':')[0];
123             if (schema == _uri || _uri.EndsWith("://") || _uri.EndsWith("://*"))
124             {
125                 return schema == uri.Split(':')[0];
126             }
127             if (_uri.EndsWith("*"))
128             {
129                 return uri.StartsWith(_uri.Substring(0, _uri.Length - 1));
130             }
131             return _uri == uri;
132         }
133     }
134 }