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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/* This file is part of the KDE project
Copyright (C) 2001 Malte Starostik <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <time.h>
#include <tqbuffer.h>
#include <tqfile.h>
#include <tqcache.h>
#include <tqimage.h>
#include <tqtimer.h>
#include <kdatastream.h> // DO NOT REMOVE, otherwise bool marshalling breaks
#include <kicontheme.h>
#include <kimageio.h>
#include <ksimpleconfig.h>
#include <kstandarddirs.h>
#include <tdeio/job.h>
#include "favicons.moc"
struct FaviconsModulePrivate
{
virtual ~FaviconsModulePrivate() { delete config; }
struct DownloadInfo
{
TQString hostOrURL;
bool isHost;
TQByteArray iconData;
};
TQMap<TDEIO::Job *, DownloadInfo> downloads;
TQStringList failedDownloads;
KSimpleConfig *config;
TQPtrList<TDEIO::Job> killJobs;
TDEIO::MetaData metaData;
TQString faviconsDir;
TQCache<TQString> faviconsCache;
};
FaviconsModule::FaviconsModule(const TQCString &obj)
: KDEDModule(obj)
{
// create our favicons folder so that TDEIconLoader knows about it
d = new FaviconsModulePrivate;
d->faviconsDir = TDEGlobal::dirs()->saveLocation( "cache", "favicons/" );
d->faviconsDir.truncate(d->faviconsDir.length()-9); // Strip off "favicons/"
d->metaData.insert("ssl_no_client_cert", "TRUE");
d->metaData.insert("ssl_militant", "TRUE");
d->metaData.insert("UseCache", "false");
d->metaData.insert("cookies", "none");
d->metaData.insert("no-auth", "true");
d->config = new KSimpleConfig(locateLocal("data", "konqueror/faviconrc"));
d->killJobs.setAutoDelete(true);
d->faviconsCache.setAutoDelete(true);
}
FaviconsModule::~FaviconsModule()
{
delete d;
}
TQString removeSlash(TQString result)
{
for (unsigned int i = result.length() - 1; i > 0; --i)
if (result[i] != '/')
{
result.truncate(i + 1);
break;
}
return result;
}
TQString FaviconsModule::iconForURL(const KURL &url)
{
if (url.host().isEmpty())
return TQString::null;
TQString icon;
TQString simplifiedURL = simplifyURL(url);
TQString *iconURL = d->faviconsCache.find( removeSlash(simplifiedURL) );
if (iconURL)
icon = *iconURL;
else
icon = d->config->readEntry( removeSlash(simplifiedURL) );
if (!icon.isEmpty())
icon = iconNameFromURL(KURL( icon ));
else
icon = url.host();
icon = "favicons/" + icon;
if (TQFile::exists(d->faviconsDir+icon+".png"))
return icon;
return TQString::null;
}
TQString FaviconsModule::simplifyURL(const KURL &url)
{
// splat any = in the URL so it can be safely used as a config key
TQString result = url.host() + url.path();
for (unsigned int i = 0; i < result.length(); ++i)
if (result[i] == '=')
result[i] = '_';
return result;
}
TQString FaviconsModule::iconNameFromURL(const KURL &iconURL)
{
if (iconURL.path() == "/favicon.ico")
return iconURL.host();
TQString result = simplifyURL(iconURL);
// splat / so it can be safely used as a file name
for (unsigned int i = 0; i < result.length(); ++i)
if (result[i] == '/')
result[i] = '_';
TQString ext = result.right(4);
if (ext == ".ico" || ext == ".png" || ext == ".xpm")
result.remove(result.length() - 4, 4);
return result;
}
bool FaviconsModule::isIconOld(const TQString &icon)
{
struct stat st;
if (stat(TQFile::encodeName(icon), &st) != 0)
return true; // Trigger a new download on error
return (time(0) - st.st_mtime) > 604800; // arbitrary value (one week)
}
void FaviconsModule::setIconForURL(const KURL &url, const KURL &iconURL)
{
TQString simplifiedURL = simplifyURL(url);
d->faviconsCache.insert(removeSlash(simplifiedURL), new TQString(iconURL.url()) );
TQString iconName = "favicons/" + iconNameFromURL(iconURL);
TQString iconFile = d->faviconsDir + iconName + ".png";
if (!isIconOld(iconFile)) {
emit iconChanged(false, simplifiedURL, iconName);
return;
}
startDownload(simplifiedURL, false, iconURL);
}
void FaviconsModule::downloadHostIcon(const KURL &url)
{
TQString iconFile = d->faviconsDir + "favicons/" + url.host() + ".png";
if (!isIconOld(iconFile))
return;
startDownload(url.host(), true, KURL(url, "/favicon.ico"));
}
void FaviconsModule::startDownload(const TQString &hostOrURL, bool isHost, const KURL &iconURL)
{
if (d->failedDownloads.contains(iconURL.url()))
return;
TDEIO::Job *job = TDEIO::get(iconURL, false, false);
job->addMetaData(d->metaData);
connect(job, TQ_SIGNAL(data(TDEIO::Job *, const TQByteArray &)), TQ_SLOT(slotData(TDEIO::Job *, const TQByteArray &)));
connect(job, TQ_SIGNAL(result(TDEIO::Job *)), TQ_SLOT(slotResult(TDEIO::Job *)));
connect(job, TQ_SIGNAL(infoMessage(TDEIO::Job *, const TQString &)), TQ_SLOT(slotInfoMessage(TDEIO::Job *, const TQString &)));
FaviconsModulePrivate::DownloadInfo download;
download.hostOrURL = hostOrURL;
download.isHost = isHost;
d->downloads.insert(job, download);
}
void FaviconsModule::slotData(TDEIO::Job *job, const TQByteArray &data)
{
FaviconsModulePrivate::DownloadInfo &download = d->downloads[job];
unsigned int oldSize = download.iconData.size();
if (oldSize > 0x10000)
{
d->killJobs.append(job);
TQTimer::singleShot(0, this, TQ_SLOT(slotKill()));
}
download.iconData.resize(oldSize + data.size());
memcpy(download.iconData.data() + oldSize, data.data(), data.size());
}
void FaviconsModule::slotResult(TDEIO::Job *job)
{
FaviconsModulePrivate::DownloadInfo download = d->downloads[job];
d->downloads.remove(job);
KURL iconURL = static_cast<TDEIO::TransferJob *>(job)->url();
TQString iconName;
if (!job->error())
{
TQBuffer buffer(download.iconData);
buffer.open(IO_ReadOnly);
TQImageIO io;
io.setIODevice(&buffer);
io.setParameters("size=16");
// Check here too, the job might have had no error, but the downloaded
// file contains just a 404 message sent with a 200 status.
// microsoft.com does that... (malte)
if (io.read())
{
// Some sites have nasty 32x32 icons, according to the MS docs
// IE ignores them, well, we scale them, otherwise the location
// combo / menu will look quite ugly
if (io.image().width() != TDEIcon::SizeSmall || io.image().height() != TDEIcon::SizeSmall)
io.setImage(io.image().smoothScale(TDEIcon::SizeSmall, TDEIcon::SizeSmall));
if (download.isHost)
iconName = download.hostOrURL;
else
iconName = iconNameFromURL(iconURL);
iconName = "favicons/" + iconName;
io.setIODevice(0);
io.setFileName(d->faviconsDir + iconName + ".png");
io.setFormat("PNG");
if (!io.write())
iconName = TQString::null;
else if (!download.isHost)
d->config->writeEntry( removeSlash(download.hostOrURL), iconURL.url());
}
}
if (iconName.isEmpty())
d->failedDownloads.append(iconURL.url());
emit iconChanged(download.isHost, download.hostOrURL, iconName);
}
void FaviconsModule::slotInfoMessage(TDEIO::Job *job, const TQString &msg)
{
emit infoMessage(static_cast<TDEIO::TransferJob *>( job )->url(), msg);
}
void FaviconsModule::slotKill()
{
d->killJobs.clear();
}
extern "C" {
TDE_EXPORT KDEDModule *create_favicons(const TQCString &obj)
{
KImageIO::registerFormats();
return new FaviconsModule(obj);
}
}
|