~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to konqueror/keditbookmarks/testlink.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-05-27 12:09:48 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080527120948-dottsyd5rcwhzd36
Tags: 4:4.0.80-1ubuntu1
* Merge with Debian
 - remove 97_fix_target_link_libraries.diff
 - Add replaces/conflicts on -kde4 packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- indent-tabs-mode:nil -*-
2
 
// vim: set ts=4 sts=4 sw=4 et:
3
 
/* This file is part of the KDE project
4
 
   Copyright (C) 2000 David Faure <faure@kde.org>
5
 
   Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
6
 
 
7
 
   This program is free software; you can redistribute it and/or
8
 
   modify it under the terms of the GNU General Public
9
 
   License version 2 as published by the Free Software Foundation.
10
 
 
11
 
   This program is distributed in the hope that it will be useful,
12
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
   General Public License for more details.
15
 
 
16
 
   You should have received a copy of the GNU General Public License
17
 
   along with this program; see the file COPYING.  If not, write to
18
 
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
 
   Boston, MA 02110-1301, USA.
20
 
*/
21
 
 
22
 
#include "toplevel.h"
23
 
#include "listview.h"
24
 
#include "testlink.h"
25
 
#include "commands.h"
26
 
#include "bookmarkiterator.h"
27
 
 
28
 
#include <qtimer.h>
29
 
#include <qpainter.h>
30
 
 
31
 
#include <kdebug.h>
32
 
 
33
 
#include <krfcdate.h>
34
 
#include <kcharsets.h>
35
 
#include <kbookmarkmanager.h>
36
 
 
37
 
#include <kaction.h>
38
 
 
39
 
TestLinkItrHolder *TestLinkItrHolder::s_self = 0;
40
 
 
41
 
TestLinkItrHolder::TestLinkItrHolder() 
42
 
    : BookmarkIteratorHolder() {
43
 
    // do stuff
44
 
}
45
 
 
46
 
void TestLinkItrHolder::doItrListChanged() {
47
 
    KEBApp::self()->setCancelTestsEnabled(count() > 0);
48
 
    if(count() == 0)
49
 
    {
50
 
        kdDebug()<<"Notifing managers "<<m_affectedBookmark<<endl;
51
 
        CurrentMgr::self()->notifyManagers(CurrentMgr::bookmarkAt(m_affectedBookmark).toGroup());
52
 
        m_affectedBookmark = QString::null;
53
 
    }
54
 
}
55
 
 
56
 
void TestLinkItrHolder::addAffectedBookmark( const QString & address )
57
 
{
58
 
    kdDebug()<<"addAffectedBookmark "<<address<<endl;
59
 
    if(m_affectedBookmark.isNull())
60
 
        m_affectedBookmark = address;
61
 
    else
62
 
        m_affectedBookmark = KBookmark::commonParent(m_affectedBookmark, address);
63
 
    kdDebug()<<" m_affectedBookmark is now "<<m_affectedBookmark<<endl;
64
 
}
65
 
 
66
 
/* -------------------------- */
67
 
 
68
 
TestLinkItr::TestLinkItr(QValueList<KBookmark> bks)
69
 
    : BookmarkIterator(bks) {
70
 
    m_job = 0;
71
 
}
72
 
 
73
 
TestLinkItr::~TestLinkItr() {
74
 
    if (m_job) {
75
 
        // kdDebug() << "JOB kill\n";
76
 
        curItem()->restoreStatus();
77
 
        m_job->disconnect();
78
 
        m_job->kill(false);
79
 
    }
80
 
}
81
 
 
82
 
bool TestLinkItr::isApplicable(const KBookmark &bk) const {
83
 
    return (!bk.isGroup() && !bk.isSeparator());
84
 
}
85
 
 
86
 
void TestLinkItr::doAction() {
87
 
    m_errSet = false;
88
 
 
89
 
    m_job = KIO::get(curBk().url(), true, false);
90
 
    m_job->addMetaData("errorPage", "true");
91
 
    m_job->addMetaData( QString("cookies"), QString("none") );
92
 
 
93
 
    connect(m_job, SIGNAL( result( KIO::Job *)),
94
 
            this, SLOT( slotJobResult(KIO::Job *)));
95
 
    connect(m_job, SIGNAL( data( KIO::Job *,  const QByteArray &)),
96
 
            this, SLOT( slotJobData(KIO::Job *, const QByteArray &)));
97
 
 
98
 
    curItem()->setTmpStatus(i18n("Checking..."));
99
 
    QString oldModDate = TestLinkItrHolder::self()->getMod(curBk().url().url());
100
 
    curItem()->setOldStatus(oldModDate);
101
 
    TestLinkItrHolder::self()->setMod(curBk().url().url(), i18n("Checking..."));
102
 
}
103
 
 
104
 
void TestLinkItr::slotJobData(KIO::Job *job, const QByteArray &data) {
105
 
    KIO::TransferJob *transfer = (KIO::TransferJob *)job;
106
 
 
107
 
    if (transfer->isErrorPage()) {
108
 
        QStringList lines = QStringList::split('\n', data);
109
 
        for (QStringList::Iterator it = lines.begin(); it != lines.end(); ++it) {
110
 
            int open_pos = (*it).find("<title>", 0, false);
111
 
            if (open_pos >= 0) {
112
 
                QString leftover = (*it).mid(open_pos + 7);
113
 
                int close_pos = leftover.findRev("</title>", -1, false);
114
 
                if (close_pos >= 0) {
115
 
                    // if no end tag found then just 
116
 
                    // print the first line of the <title>
117
 
                    leftover = leftover.left(close_pos);
118
 
                }
119
 
                curItem()->nsPut(KCharsets::resolveEntities(leftover));
120
 
                m_errSet = true;
121
 
                break;
122
 
            }
123
 
        }
124
 
 
125
 
    } else {
126
 
        QString modDate = transfer->queryMetaData("modified");
127
 
        if (!modDate.isEmpty()) {
128
 
            curItem()->nsPut(QString::number(KRFCDate::parseDate(modDate)));
129
 
        }
130
 
    }
131
 
 
132
 
    transfer->kill(false);
133
 
}
134
 
 
135
 
void TestLinkItr::slotJobResult(KIO::Job *job) {
136
 
    m_job = 0;
137
 
    if ( !curItem() ) return;
138
 
 
139
 
    KIO::TransferJob *transfer = (KIO::TransferJob *)job;
140
 
    QString modDate = transfer->queryMetaData("modified");
141
 
 
142
 
    bool chkErr = true;
143
 
    if (transfer->error()) {
144
 
        // can we assume that errorString will contain no entities?
145
 
        QString jerr = job->errorString();
146
 
        if (!jerr.isEmpty()) {
147
 
            jerr.replace("\n", " ");
148
 
            curItem()->nsPut(jerr);
149
 
            chkErr = false;
150
 
        }
151
 
    }
152
 
 
153
 
    if (chkErr) {
154
 
        if (!modDate.isEmpty()) {
155
 
            curItem()->nsPut(QString::number(KRFCDate::parseDate(modDate)));
156
 
        } else if (!m_errSet) {
157
 
            curItem()->nsPut(QString::number(KRFCDate::parseDate("0")));
158
 
        }
159
 
    }
160
 
 
161
 
    curItem()->modUpdate();
162
 
    holder()->addAffectedBookmark(KBookmark::parentAddress(curBk().address()));
163
 
    delayedEmitNextOne();
164
 
}
165
 
 
166
 
/* -------------------------- */
167
 
 
168
 
const QString TestLinkItrHolder::getMod(const QString &url) const {
169
 
    return m_modify.contains(url) 
170
 
        ? m_modify[url] 
171
 
        : QString::null;
172
 
}
173
 
 
174
 
const QString TestLinkItrHolder::getOldVisit(const QString &url) const {
175
 
    return self()->m_oldModify.contains(url) 
176
 
        ? self()->m_oldModify[url] 
177
 
        : QString::null;
178
 
}
179
 
 
180
 
void TestLinkItrHolder::setMod(const QString &url, const QString &val) {
181
 
    m_modify[url] = val;
182
 
}
183
 
 
184
 
void TestLinkItrHolder::setOldVisit(const QString &url, const QString &val) {
185
 
    m_oldModify[url] = val;
186
 
}
187
 
 
188
 
void TestLinkItrHolder::resetToValue(const QString &url, const QString &oldValue) {
189
 
    if (!oldValue.isEmpty()) {
190
 
        m_modify[url] = oldValue;
191
 
    } else {
192
 
        m_modify.remove(url);
193
 
    }
194
 
}
195
 
 
196
 
/* -------------------------- */
197
 
 
198
 
QString TestLinkItrHolder::calcPaintStyle(const QString &url, KEBListViewItem::PaintStyle &_style, 
199
 
                                          const QString &nVisit, const QString &Modify) {
200
 
    bool newModValid = false;
201
 
    int newMod = 0;
202
 
    QString newModStr;
203
 
    bool initial = false;
204
 
    bool oldError = false;
205
 
 
206
 
    if (!Modify.isNull() && Modify == "1") {
207
 
        oldError = true;
208
 
    }
209
 
 
210
 
    // get new mod time if there is one
211
 
    newModStr = self()->getMod(url);
212
 
 
213
 
    // if no new mod time use previous one
214
 
    if (newModStr.isNull()) {
215
 
        newModStr = Modify;
216
 
        initial = true;
217
 
    }    
218
 
 
219
 
    if (!newModStr.isNull()) {
220
 
        newMod = newModStr.toInt(&newModValid);
221
 
    }
222
 
 
223
 
 
224
 
//    kdDebug() << "TestLink " << url << " " << "booktime=" << nVisit << " urltime=" << newModStr << 
225
 
//               " Modify=" << Modify << " init=" << initial << " newMod=" << newMod << "\n";
226
 
 
227
 
    QString visitStr;
228
 
 
229
 
    if (self()->getOldVisit(url).isNull()) {
230
 
        // first time
231
 
        visitStr = nVisit;
232
 
        if (!nVisit.isEmpty())
233
 
            self()->setOldVisit(url, visitStr);
234
 
    } else {
235
 
        // may be reading a second bookmark with same url
236
 
        QString oom = nVisit;
237
 
        visitStr = self()->getOldVisit(url);
238
 
        if (oom.toInt() > visitStr.toInt()) {
239
 
            self()->setOldVisit(url, oom);
240
 
            visitStr = oom;
241
 
        }
242
 
    }
243
 
 
244
 
    int visit = 0;
245
 
    if (!visitStr.isNull())
246
 
        visit = visitStr.toInt(); // TODO - check validity?
247
 
 
248
 
    QString statusStr;
249
 
    KEBListViewItem::PaintStyle style = KEBListViewItem::DefaultStyle;
250
 
 
251
 
//    kdDebug() << "TestLink " << "isNull=" << newModStr.isNull() << "newModValid=" 
252
 
//              << newModValid << "newMod > visit " << newMod << ">" << visit << "\n";
253
 
 
254
 
    if (!newModStr.isNull() && !newModValid) { 
255
 
        // Current check has error
256
 
        statusStr = newModStr;
257
 
        if (oldError) {
258
 
            style = KEBListViewItem::BoldStyle;
259
 
        } else {
260
 
            style =  KEBListViewItem::DefaultStyle;
261
 
        }
262
 
 
263
 
    } else if (initial && oldError) { 
264
 
        // Previous check has error
265
 
        style = KEBListViewItem::GreyStyle;
266
 
        statusStr = i18n("Error ");
267
 
 
268
 
    } else if (!initial && !newModStr.isNull() && (newMod == 0)) { 
269
 
        // Current check has no modify time
270
 
        statusStr = i18n("Ok");
271
 
 
272
 
    } else if (initial && !newModStr.isNull() && (newMod == 0)) { 
273
 
        // previous check has no modify time recorded
274
 
        statusStr = QString::null;
275
 
 
276
 
    } else if (!newModStr.isNull() && (newMod > visit)) { 
277
 
        // if modify time greater than last visit, show bold modify time
278
 
        statusStr = CurrentMgr::makeTimeStr(newMod);
279
 
        if (initial) {
280
 
            style = KEBListViewItem::GreyBoldStyle;
281
 
        } else {
282
 
            style = KEBListViewItem::BoldStyle;
283
 
        }
284
 
 
285
 
    } else if (visit != 0) { 
286
 
        // modify time not greater than last visit, show last visit time
287
 
        statusStr = CurrentMgr::makeTimeStr(visit);
288
 
        if (initial) {
289
 
                style = KEBListViewItem::GreyStyle;
290
 
        } else {
291
 
                style = KEBListViewItem::DefaultStyle;
292
 
        }
293
 
 
294
 
    } else {
295
 
        statusStr = QString::null;
296
 
    }
297
 
 
298
 
    _style = style;
299
 
    return statusStr;
300
 
}
301
 
 
302
 
static void parseInfo (KBookmark &bk, QString &nVisited) {
303
 
    nVisited = 
304
 
        NodeEditCommand::getNodeText(bk, QStringList() << "info" << "metadata"
305
 
                                     << "time_visited" );
306
 
 
307
 
//    kdDebug() << " Visited=" << nVisited << "\n";
308
 
}
309
 
 
310
 
static void parseNsInfo(const QString &nsinfo, QString &nCreate, QString &nAccess, QString &nModify) {
311
 
    QStringList sl = QStringList::split(' ', nsinfo);
312
 
 
313
 
    for (QStringList::Iterator it = sl.begin(); it != sl.end(); ++it) {
314
 
        QStringList spl = QStringList::split('"', (*it));
315
 
 
316
 
        if (spl[0] == "LAST_MODIFIED=") {
317
 
            nModify = spl[1];
318
 
        } else if (spl[0] == "ADD_DATE=") {
319
 
            nCreate = spl[1];
320
 
        } else if (spl[0] == "LAST_VISIT=") {
321
 
            nAccess = spl[1];
322
 
        }
323
 
    }
324
 
}
325
 
 
326
 
// Still use nsinfo for storing old modify time
327
 
static const QString updateNsInfoMod(const QString &_nsinfo, const QString &nm) {
328
 
    QString nCreate, nAccess, nModify;
329
 
    parseNsInfo(_nsinfo, nCreate, nAccess, nModify);
330
 
 
331
 
    bool numValid = false;
332
 
    nm.toInt(&numValid);
333
 
 
334
 
    QString tmp;
335
 
    tmp  =  "ADD_DATE=\"" + ((nCreate.isEmpty()) ? QString::number(time(0)) : nCreate) + "\"";
336
 
    tmp += " LAST_VISIT=\"" + ((nAccess.isEmpty()) ? QString("0") : nAccess) + "\"";
337
 
    tmp += " LAST_MODIFIED=\"" + ((numValid) ? nm : QString("1")) + "\"";
338
 
 
339
 
//  if (!numValid) kdDebug() << tmp << "\n";
340
 
    return tmp;
341
 
}
342
 
 
343
 
// KEBListViewItem !!!!!!!!!!!
344
 
void KEBListViewItem::nsPut(const QString &newModDate) {
345
 
    static const QString NetscapeInfoAttribute = "netscapeinfo";
346
 
    const QString info = m_bookmark.internalElement().attribute(NetscapeInfoAttribute);
347
 
    QString blah = updateNsInfoMod(info, newModDate);
348
 
    m_bookmark.internalElement().setAttribute(NetscapeInfoAttribute, blah);
349
 
    TestLinkItrHolder::self()->setMod(m_bookmark.url().url(), newModDate);
350
 
    setText(KEBListView::StatusColumn, newModDate);
351
 
}
352
 
 
353
 
// KEBListViewItem !!!!!!!!!!!
354
 
void KEBListViewItem::modUpdate() {
355
 
    QString nCreate, nAccess, oldModify;
356
 
    QString iVisit;
357
 
 
358
 
    QString nsinfo = m_bookmark.internalElement().attribute("netscapeinfo");
359
 
    if (!nsinfo.isEmpty()) {
360
 
        parseNsInfo(nsinfo, nCreate, nAccess, oldModify);
361
 
    }
362
 
 
363
 
    parseInfo(m_bookmark, iVisit);
364
 
 
365
 
    QString statusLine;
366
 
    statusLine = TestLinkItrHolder::calcPaintStyle(m_bookmark.url().url(), m_paintStyle, iVisit, oldModify);
367
 
    if (statusLine != "Error")
368
 
        setText(KEBListView::StatusColumn, statusLine);
369
 
}
370
 
 
371
 
/* -------------------------- */
372
 
 
373
 
// KEBListViewItem !!!!!!!!!!!
374
 
void KEBListViewItem::setOldStatus(const QString &oldStatus) {
375
 
    // kdDebug() << "KEBListViewItem::setOldStatus" << endl;
376
 
    m_oldStatus = oldStatus;
377
 
}
378
 
 
379
 
// KEBListViewItem !!!!!!!!!!!
380
 
void KEBListViewItem::setTmpStatus(const QString &status) {
381
 
    // kdDebug() << "KEBListViewItem::setTmpStatus" << endl;
382
 
    m_paintStyle = KEBListViewItem::BoldStyle;
383
 
    setText(KEBListView::StatusColumn, status);
384
 
}
385
 
 
386
 
// KEBListViewItem !!!!!!!!!!!
387
 
void KEBListViewItem::restoreStatus() {
388
 
    if (!m_oldStatus.isNull()) {
389
 
        // kdDebug() << "KEBListViewItem::restoreStatus" << endl;
390
 
        TestLinkItrHolder::self()->resetToValue(m_bookmark.url().url(), m_oldStatus);
391
 
        modUpdate();
392
 
    }
393
 
}
394
 
 
395
 
#include "testlink.moc"