summaryrefslogtreecommitdiffstats
path: root/tdeio/bookmarks/kbookmarkimporter_crash.cc
blob: b2871eecd829b8a16493645fbaa0621d8292ab3c (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
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
//  -*- c-basic-offset:4; indent-tabs-mode:nil -*-
// vim: set ts=4 sts=4 sw=4 et:
/* This file is part of the KDE libraries
   Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   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 "kbookmarkimporter_crash.h"

#include <tdefiledialog.h>
#include <kstringhandler.h>
#include <klocale.h>
#include <kdebug.h>
#include <kapplication.h>
#include <kstandarddirs.h>
#include <tqfile.h>
#include <tqdir.h>
#include <tqstring.h>
#include <tqtextcodec.h>
#include <dcopclient.h>

#include <sys/types.h>
#include <stddef.h>
#include <dirent.h>
#include <sys/stat.h>

typedef TQMap<TQString, TQString> ViewMap;

// KDE 4.0: remove this BC keeping stub
void KCrashBookmarkImporter::parseCrashLog( TQString /*filename*/, bool /*del*/ ) 
{
    ;
}

ViewMap KCrashBookmarkImporterImpl::parseCrashLog_noemit( const TQString & filename, bool del ) 
{
    static const int g_lineLimit = 16*1024;

    TQFile f( filename );
    ViewMap views;

    if ( !f.open( IO_ReadOnly ) )
        return views;

    TQCString s( g_lineLimit );

    TQTextCodec * codec = TQTextCodec::codecForName( "UTF-8" );
    Q_ASSERT( codec );
    if ( !codec ) 
        return views;

    while ( f.readLine( s.data(), g_lineLimit ) >=0 ) 
    {
        if ( s[s.length()-1] != '\n' )
        {
            kdWarning() << "Crash bookmarks contain a line longer than " << g_lineLimit << ". Skipping." << endl;
            continue;
        }
        TQString t = codec->toUnicode( s.stripWhiteSpace() );
        TQRegExp rx( "(.*)\\((.*)\\):(.*)$" );
        rx.setMinimal( true );
        if ( !rx.exactMatch( t ) ) 
            continue;
        if ( rx.cap(1) == "opened" )
            views[rx.cap(2)] = rx.cap(3);
        else if ( rx.cap(1) == "close" )
            views.remove( rx.cap(2) );
    }

    f.close();

    if ( del ) 
        f.remove();

    return views;
}

TQStringList KCrashBookmarkImporter::getCrashLogs() 
{
    return KCrashBookmarkImporterImpl::getCrashLogs();
}

TQStringList KCrashBookmarkImporterImpl::getCrashLogs() 
{
    TQMap<TQString, bool> activeLogs;

    DCOPClient* dcop = kapp->dcopClient();

    QCStringList apps = dcop->registeredApplications();
    for ( QCStringList::Iterator it = apps.begin(); it != apps.end(); ++it ) 
    {
        TQCString &clientId = *it;

        if ( tqstrncmp(clientId, "konqueror", 9) != 0 ) 
            continue;

        TQByteArray data, replyData;
        TQCString replyType;
        TQDataStream arg( data, IO_WriteOnly );

        if ( !dcop->call( clientId.data(), "KonquerorIface", 
                          "crashLogFile()", data, replyType, replyData) ) 
        {
            kdWarning() << "can't find dcop function KonquerorIface::crashLogFile()" << endl;
            continue;
        }

        if ( replyType != "TQString" ) 
            continue;

        TQDataStream reply( replyData, IO_ReadOnly );
        TQString ret;
        reply >> ret;
        activeLogs[ret] = true;
    }

    TQDir d( KCrashBookmarkImporterImpl().findDefaultLocation() );
    d.setSorting( TQDir::Time );
    d.setFilter( TQDir::Files );
    d.setNameFilter( "konqueror-crash-*.log" );

    const TQFileInfoList *list = d.entryInfoList();
    TQFileInfoListIterator it( *list );

    TQFileInfo *fi;
    TQStringList crashFiles;

    int count = 0;
    for ( ; (( fi = it.current() ) != 0) && (count < 20); ++it, ++count ) 
    {
        bool stillAlive = activeLogs.contains( fi->absFilePath() );
        if ( !stillAlive )
            crashFiles << fi->absFilePath();
    }
    // Delete remaining ones
    for ( ; ( fi = it.current() ) != 0; ++it ) 
    {
        TQFile::remove( fi->absFilePath() );
    }

    return crashFiles;
}

void KCrashBookmarkImporterImpl::parse() 
{
    TQDict<bool> signatureMap;
    TQStringList crashFiles = KCrashBookmarkImporterImpl::getCrashLogs();
    int count = 1;
    for ( TQStringList::Iterator it = crashFiles.begin(); it != crashFiles.end(); ++it ) 
    {
        ViewMap views;
        views = parseCrashLog_noemit( *it, m_shouldDelete );
        TQString signature;
        for ( ViewMap::Iterator vit = views.begin(); vit != views.end(); ++vit ) 
            signature += "|"+vit.data();
        if (signatureMap[signature])
        {
            // Duplicate... throw away and skip
            TQFile::remove(*it);
            continue;
        }
            
        signatureMap.insert(signature, (bool *) true); // hack

        int outerFolder = ( crashFiles.count() > 1 ) && (views.count() > 0);
        if ( outerFolder )
            emit newFolder( TQString("Konqueror Window %1").arg(count++), false, "" );
        for ( ViewMap::Iterator vit = views.begin(); vit != views.end(); ++vit ) 
            emit newBookmark( vit.data(), vit.data().latin1(), TQString("") );
        if ( outerFolder )
            emit endFolder();
    }
}

TQString KCrashBookmarkImporter::crashBookmarksDir() 
{
    static KCrashBookmarkImporterImpl *p = 0;
    if (!p)
        p = new KCrashBookmarkImporterImpl;
    return p->findDefaultLocation();
}

void KCrashBookmarkImporterImpl::setShouldDelete( bool shouldDelete ) 
{
    m_shouldDelete = shouldDelete;
}

void KCrashBookmarkImporter::parseCrashBookmarks( bool del ) 
{
    KCrashBookmarkImporterImpl importer;
    importer.setFilename( m_fileName );
    importer.setShouldDelete( del );
    importer.setupSignalForwards( &importer, this );
    importer.parse();
}

TQString KCrashBookmarkImporterImpl::findDefaultLocation( bool ) const 
{
    return locateLocal( "tmp", "" );
}

#include "kbookmarkimporter_crash.moc"