delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kdesu / kdesud / repo.cpp
blob1fb6cd1eb48b057b9c2695b04f5c2833211feffe
1 /* vi: ts=8 sts=4 sw=4
3 * This file is part of the KDE project, module kdesu.
4 * Copyright (C) 1999,2000 Geert Jansen <g.t.jansen@stud.tue.nl>
5 */
7 #include "repo.h"
9 #include <time.h>
10 #include <assert.h>
12 #include <QStack>
13 #include <KDebug>
16 Repository::Repository()
18 head_time = (unsigned) -1;
22 Repository::~Repository()
26 void Repository::add(const QByteArray &key, Data_entry &data)
28 RepoIterator it = repo.find(key);
29 if (it != repo.end())
30 remove(key);
31 if (data.timeout == 0)
32 data.timeout = (unsigned) -1;
33 else
34 data.timeout += time(0L);
35 head_time = qMin(head_time, data.timeout);
36 repo.insert(key, data);
39 int Repository::remove(const QByteArray &key)
41 if( key.isEmpty() )
42 return -1;
44 RepoIterator it = repo.find(key);
45 if (it == repo.end())
46 return -1;
47 it.value().value.fill('x');
48 it.value().group.fill('x');
49 repo.erase(it);
50 return 0;
53 int Repository::removeSpecialKey(const QByteArray &key)
55 int found = -1;
56 if ( !key.isEmpty() )
58 QStack<QByteArray> rm_keys;
59 for (RepoCIterator it=repo.constBegin(); it!=repo.constEnd(); ++it)
61 if ( key.indexOf( it.value().group ) == 0 &&
62 it.key().indexOf( key ) >= 0 )
64 rm_keys.push(it.key());
65 found = 0;
68 while (!rm_keys.isEmpty())
70 kDebug(1205) << "Removed key: " << rm_keys.top();
71 remove(rm_keys.pop());
74 return found;
77 int Repository::removeGroup(const QByteArray &group)
79 int found = -1;
80 if ( !group.isEmpty() )
82 QStack<QByteArray> rm_keys;
83 for (RepoCIterator it=repo.constBegin(); it!=repo.constEnd(); ++it)
85 if (it.value().group == group)
87 rm_keys.push(it.key());
88 found = 0;
91 while (!rm_keys.isEmpty())
93 kDebug(1205) << "Removed key: " << rm_keys.top();
94 remove(rm_keys.pop());
97 return found;
100 int Repository::hasGroup(const QByteArray &group) const
102 if ( !group.isEmpty() )
104 RepoCIterator it;
105 for (it=repo.begin(); it!=repo.end(); ++it)
107 if (it.value().group == group)
108 return 0;
111 return -1;
114 QByteArray Repository::findKeys(const QByteArray &group, const char *sep ) const
116 QByteArray list="";
117 if( !group.isEmpty() )
119 kDebug(1205) << "Looking for matching key with group key: " << group;
120 int pos;
121 QByteArray key;
122 RepoCIterator it;
123 for (it=repo.begin(); it!=repo.end(); ++it)
125 if (it.value().group == group)
127 key = it.key();
128 kDebug(1205) << "Matching key found: " << key;
129 pos = key.lastIndexOf(sep);
130 key.truncate( pos );
131 key.remove(0, 2);
132 if (!list.isEmpty())
134 // Add the same keys only once please :)
135 if( !list.contains(key) )
137 kDebug(1205) << "Key added to list: " << key;
138 list += '\007'; // I do not know
139 list.append(key);
142 else
143 list = key;
147 return list;
150 QByteArray Repository::find(const QByteArray &key) const
152 if( key.isEmpty() )
153 return 0;
155 RepoCIterator it = repo.find(key);
156 if (it == repo.end())
157 return 0;
158 return it.value().value;
162 int Repository::expire()
164 unsigned current = time(0L);
165 if (current < head_time)
166 return 0;
168 unsigned t;
169 QStack<QByteArray> keys;
170 head_time = (unsigned) -1;
171 RepoIterator it;
172 for (it=repo.begin(); it!=repo.end(); ++it)
174 t = it.value().timeout;
175 if (t <= current)
176 keys.push(it.key());
177 else
178 head_time = qMin(head_time, t);
181 int n = keys.count();
182 while (!keys.isEmpty())
183 remove(keys.pop());
184 return n;