summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/io/local/CookieTransport.js
blob: b837eb7f6e87ea5cf6e17581734b08613ea5dc2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org

   License:
     LGPL 2.1: http://www.gnu.org/licenses/lgpl.html

   Authors:
     * Sebastian Werner (wpbasti)
     * Andreas Ecker (ecker)

************************************************************************ */

/* ************************************************************************


************************************************************************ */

qx.OO.defineClass("qx.io.local.CookieTransport",
{
  BASENAME : "qx",
  ITEMSEPARATOR : "&",
  KEYVALUESEPARATOR : "=",
  MAXCOOKIES : 20,
  MAXSIZE : 4096
});





/*
---------------------------------------------------------------------------
  USER APPLICATION METHODS
---------------------------------------------------------------------------
*/

qx.Class.set = function(vName, vValue)
{
  if (!qx.util.Validation.isValid(vValue)) {
    return qx.io.local.CookieTransport.del(vName);
  }

  var vAll = qx.io.local.CookieTransport._getAll();
  vAll[vName] = vValue;
  this._setAll(vAll);
}

qx.Class.get = function(vName)
{
  var vAll = qx.io.local.CookieTransport._getAll();

  var vValue = qx.io.local.CookieTransport._getAll()[vName];
  if (qx.util.Validation.isValidString(vValue)) {
    return vValue;
  }

  return "";
}

qx.Class.del = function(vName)
{
  var vAll = qx.io.local.CookieTransport._getAll();
  delete vAll[vName];
  this._setAll(vAll);
}

qx.Class.setAll = function(vHash)
{
  var vAll = qx.io.local.CookieTransport._getAll();
  vAll = qx.lang.Object.mergeWith(vAll, vHash);
  qx.io.local.CookieTransport._setAll(vAll);
}

qx.Class.getAll = function() {
  return qx.io.local.CookieTransport._getAll();
}

qx.Class.replaceAll = function(vHash) {
  qx.io.local.CookieTransport._setAll(vHash);
}

qx.Class.delAll = function() {
  qx.io.local.CookieTransport.replaceAll({});
}





/*
---------------------------------------------------------------------------
  LOW LEVEL INTERNAL METHODS
---------------------------------------------------------------------------
*/

qx.Class._getAll = function()
{
  var vHash = {};
  var vCookie, vItems, vItem;

  for (var i=0; i<qx.io.local.CookieTransport.MAXCOOKIES; i++)
  {
    vCookie = qx.io.local.CookieApi.get(qx.io.local.CookieTransport.BASENAME + i);
    if (vCookie)
    {
      vItems = vCookie.split(qx.io.local.CookieTransport.ITEMSEPARATOR);
      for (var j=0, l=vItems.length; j<l; j++)
      {
        vItem = vItems[j].split(qx.io.local.CookieTransport.KEYVALUESEPARATOR);
        vHash[vItem[0]] = vItem[1];
      }
    }
  }

  return vHash;
}

qx.Class._setAll = function(vHash)
{
  var vString = "";
  var vTemp;
  var vIndex = 0;

  for (var vName in vHash)
  {
    vTemp = vName + qx.io.local.CookieTransport.KEYVALUESEPARATOR + vHash[vName];

    if (vTemp.length > qx.io.local.CookieTransport.MAXSIZE)
    {
      qx.dev.log.Logger.getClassLogger(qx.io.local.CookieTransport).debug("Could not store value of name '" + vName + "': Maximum size of " + qx.io.local.CookieTransport.MAXSIZE + "reached!");
      continue;
    }

    if ((qx.io.local.CookieTransport.ITEMSEPARATOR.length + vString.length + vTemp.length) > qx.io.local.CookieTransport.MAXSIZE)
    {
      qx.io.local.CookieTransport._setCookie(vIndex++, vString);

      if (vIndex == qx.io.local.CookieTransport.MAXCOOKIES)
      {
        qx.dev.log.Logger.getClassLogger(qx.io.local.CookieTransport).debug("Failed to store cookie. Max cookie amount reached!", "error");
        return false;
      }

      vString = vTemp;
    }
    else
    {
      if (vString != "") {
        vString += qx.io.local.CookieTransport.ITEMSEPARATOR;
      }

      vString += vTemp;
    }
  }

  if (vString != "") {
    qx.io.local.CookieTransport._setCookie(vIndex++, vString);
  }

  while (vIndex < qx.io.local.CookieTransport.MAXCOOKIES) {
    qx.io.local.CookieTransport._delCookie(vIndex++);
  }
}

qx.Class._setCookie = function(vIndex, vString)
{
  // qx.dev.log.Logger.getClassLogger(qx.io.local.CookieTransport).debug("Store: " + vIndex + " = " + vString);
  qx.io.local.CookieApi.set(qx.io.local.CookieTransport.BASENAME + vIndex, vString);
}

qx.Class._delCookie = function(vIndex)
{
  // qx.dev.log.Logger.getClassLogger(qx.io.local.CookieTransport).debug("Delete: " + vIndex);
  qx.io.local.CookieApi.del(qx.io.local.CookieTransport.BASENAME + vIndex);
}