Cappuccino  1.0.0
 All Classes Files Functions Variables Typedefs Macros Groups Pages
CPCookie.j
Go to the documentation of this file.
1 /*
2  * CPCookie.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
29 @implementation CPCookie : CPObject
30 {
31  CPString _cookieName;
32  CPString _cookieValue;
33 
34  CPString _expires;
35 }
36 
41 - (id)initWithName:(CPString)aName
42 {
43  self = [super init];
44 
45  _cookieName = aName;
46  _cookieValue = [self _readCookieValue];
47 
48  return self;
49 }
50 
54 - (CPString)value
55 {
56  return _cookieValue;
57 }
58 
63 {
64  return _cookieName;
65 }
66 
70 - (CPString)expires
71 {
72  return _expires;
73 }
74 
81 - (void)setValue:(CPString)value expires:(CPDate)date domain:(CPString)domain
82 {
83  if (date)
84  var expires = "; expires=" + date.toGMTString();
85  else
86  var expires = "";
87 
88  if (domain)
89  domain = "; domain=" + domain;
90  else
91  domain = "";
92 
93 #if PLATFORM(DOM)
94  document.cookie = _cookieName + "=" + value + expires + "; path=/" + domain;
95 #else
96  _cookieValue = value;
97  _expires = expires;
98 #endif
99 }
100 
101 /* @ignore */
102 - (CPString)_readCookieValue
103 {
104 #if PLATFORM(DOM)
105  var nameEQ = _cookieName + "=",
106  ca = document.cookie.split(';');
107 
108  for (var i = 0; i < ca.length; i++)
109  {
110  var c = ca[i];
111 
112  while (c.charAt(0) == ' ')
113  c = c.substring(1, c.length);
114 
115  if (c.indexOf(nameEQ) == 0)
116  return c.substring(nameEQ.length, c.length);
117  }
118 #endif
119  return "";
120 }
121 
122 @end
123 
124 //http://www.quirksmode.org/js/cookies.html