API 0.9.5
AppKit/CPCookie.j
Go to the documentation of this file.
00001 /*
00002  * CPCookie.j
00003  * AppKit
00004  *
00005  * Created by Francisco Tolmasky.
00006  * Copyright 2008, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00030 @implementation CPCookie : CPObject
00031 {
00032     CPString    _cookieName;
00033     CPString    _cookieValue;
00034 
00035     CPString    _expires;
00036 }
00037 
00042 - (id)initWithName:(CPString)aName
00043 {
00044     self = [super init];
00045 
00046     _cookieName  = aName;
00047     _cookieValue = [self _readCookieValue];
00048 
00049     return self;
00050 }
00051 
00055 - (CPString)value
00056 {
00057     return _cookieValue;
00058 }
00059 
00063 - (CPString)name
00064 {
00065     return _cookieName;
00066 }
00067 
00071 - (CPString)expires
00072 {
00073     return _expires;
00074 }
00075 
00082 - (void)setValue:(CPString)value expires:(CPDate)date domain:(CPString)domain
00083 {
00084     if (date)
00085         var expires = "; expires=" + date.toGMTString();
00086     else
00087         var expires = "";
00088 
00089     if (domain)
00090         domain = "; domain=" + domain;
00091     else
00092         domain = "";
00093 
00094 #if PLATFORM(DOM)
00095     document.cookie = _cookieName+"="+value+expires+"; path=/"+domain;
00096 #else
00097     _cookieValue = value;
00098     _expires = expires;
00099 #endif
00100 }
00101 
00102 /* @ignore */
00103 - (CPString)_readCookieValue
00104 {
00105 #if PLATFORM(DOM)
00106     var nameEQ = _cookieName + "=",
00107         ca = document.cookie.split(';');
00108 
00109     for (var i = 0; i < ca.length; i++)
00110     {
00111         var c = ca[i];
00112         while (c.charAt(0) == ' ') c = c.substring(1, c.length);
00113         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
00114     }
00115 #endif
00116     return "";
00117 }
00118 
00119 @end
00120 
00121 //http://www.quirksmode.org/js/cookies.html
 All Classes Files Functions Variables Defines