API  0.9.6
 All Classes Files Functions Variables 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 
30 @implementation CPCookie : CPObject
31 {
32  CPString _cookieName;
33  CPString _cookieValue;
34 
35  CPString _expires;
36 }
37 
42 - (id)initWithName:(CPString)aName
43 {
44  self = [super init];
45 
46  _cookieName = aName;
47  _cookieValue = [self _readCookieValue];
48 
49  return self;
50 }
51 
55 - (CPString)value
56 {
57  return _cookieValue;
58 }
59 
63 - (CPString)name
64 {
65  return _cookieName;
66 }
67 
71 - (CPString)expires
72 {
73  return _expires;
74 }
75 
82 - (void)setValue:(CPString)value expires:(CPDate)date domain:(CPString)domain
83 {
84  if (date)
85  var expires = "; expires=" + date.toGMTString();
86  else
87  var expires = "";
88 
89  if (domain)
90  domain = "; domain=" + domain;
91  else
92  domain = "";
93 
94 #if PLATFORM(DOM)
95  document.cookie = _cookieName + "=" + value + expires + "; path=/" + domain;
96 #else
97  _cookieValue = value;
98  _expires = expires;
99 #endif
100 }
101 
102 /* @ignore */
103 - (CPString)_readCookieValue
104 {
105 #if PLATFORM(DOM)
106  var nameEQ = _cookieName + "=",
107  ca = document.cookie.split(';');
108 
109  for (var i = 0; i < ca.length; i++)
110  {
111  var c = ca[i];
112 
113  while (c.charAt(0) == ' ')
114  c = c.substring(1, c.length);
115 
116  if (c.indexOf(nameEQ) == 0)
117  return c.substring(nameEQ.length, c.length);
118  }
119 #endif
120  return "";
121 }
122 
123 @end
124 
125 //http://www.quirksmode.org/js/cookies.html