libkonq Library API Documentation

kbookmarkimporter_crash.cc

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2002 Alexander Kellett <lypanov@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "kbookmarkimporter_crash.h"
00020 #include <kfiledialog.h>
00021 #include <kstringhandler.h>
00022 #include <klocale.h>
00023 #include <kdebug.h>
00024 #include <kapplication.h>
00025 #include <kstandarddirs.h>
00026 #include <qfile.h>
00027 #include <qdir.h>
00028 #include <qstring.h>
00029 #include <qtextcodec.h>
00030 #include <dcopclient.h>
00031 
00032 #include <sys/types.h>
00033 #include <stddef.h>
00034 #include <dirent.h>
00035 #include <sys/stat.h>
00036 
00037 #define LINELIMIT 4096
00038 
00039 void KCrashBookmarkImporter::parseCrashLog( QString filename, bool del )
00040 {
00041     QFile f(filename);
00042 
00043     if(f.open(IO_ReadOnly)) {
00044 
00045         QCString s(4096);
00046 
00047         QTextCodec * codec = QTextCodec::codecForName("UTF-8");
00048         Q_ASSERT(codec);
00049         if (!codec) return;
00050 
00051         typedef QMap<QString, QString> ViewMap;
00052         ViewMap views;
00053 
00054         while(f.readLine(s.data(), LINELIMIT)>=0) {
00055             if ( s[s.length()-1] != '\n' ) // Gosh, this line is longer than LINELIMIT. Skipping.
00056             {
00057                kdWarning() << "Crash bookmarks contain a line longer than " << LINELIMIT << ". Skipping." << endl;
00058                continue;
00059             }
00060             // KStringHandler::csqueeze()
00061             QString t = codec->toUnicode(s.stripWhiteSpace());
00062             QRegExp rx( "(.*)\\((.*)\\):(.*)$" );
00063             rx.setMinimal( TRUE );
00064             if (rx.exactMatch(t)) {
00065                if (rx.cap(1) == "opened") {
00066                   views[rx.cap(2)] = rx.cap(3);
00067                } else if (rx.cap(1) == "close") {
00068                   views.remove(rx.cap(2));
00069                }
00070             }
00071         }
00072 
00073         for ( ViewMap::Iterator it = views.begin(); it != views.end(); ++it ) {
00074            emit newBookmark( it.data(), it.data().latin1(), QString("") );
00075         }
00076 
00077         f.close();
00078 
00079         if (del) f.remove();
00080     }
00081 }
00082 
00083 QStringList KCrashBookmarkImporter::getCrashLogs() {
00084 
00085    QDir d( crashBookmarksDir() );
00086    d.setFilter( QDir::Files );
00087    d.setNameFilter("konqueror-crash-*.log");
00088 
00089    QMap<QString, bool> activeLogs;
00090 
00091    DCOPClient* dcop = kapp->dcopClient();
00092 
00093    QCStringList apps = dcop->registeredApplications();
00094    for ( QCStringList::Iterator it = apps.begin(); it != apps.end(); ++it )
00095    {
00096       QCString &clientId = *it;
00097 
00098       if ( qstrncmp(clientId, "konqueror", 9) != 0 ) 
00099          continue;
00100 
00101       QByteArray data, replyData;
00102       QCString replyType;
00103       QDataStream arg(data, IO_WriteOnly);
00104 
00105       if ( !dcop->call( clientId.data(), "KonquerorIface", "crashLogFile()", data, replyType, replyData) ) {
00106          kdWarning() << "can't find dcop function KonquerorIface::crashLogFile()" << endl;
00107       } else {
00108          QDataStream reply(replyData, IO_ReadOnly);
00109 
00110          if ( replyType == "QString" )
00111          {
00112             QString ret;
00113             reply >> ret;
00114             activeLogs[ret] = true; // AK - nicer way to put this?
00115          }
00116       }
00117    }
00118 
00119    const QFileInfoList *list = d.entryInfoList();
00120    QFileInfoListIterator it( *list );
00121 
00122    QFileInfo *fi;
00123    QStringList crashFiles;
00124 
00125    for ( ; (fi = it.current()) != 0; ++it ) {
00126       bool dead = !activeLogs.contains(fi->absFilePath());
00127       if (dead) {
00128          crashFiles << fi->absFilePath();
00129       }
00130    }
00131 
00132    return crashFiles;
00133 }
00134 
00135 void KCrashBookmarkImporter::parseCrashBookmarks( bool del )
00136 {
00137    QStringList crashFiles = KCrashBookmarkImporter::getCrashLogs();
00138    int len = crashFiles.count();
00139    int n = 1;
00140 
00141    for ( QStringList::Iterator it = crashFiles.begin(); it != crashFiles.end(); ++it ) {
00142       if (len > 1) {
00143          emit newFolder( QString("Instance %1").arg(n++), false, "" );
00144       }
00145       parseCrashLog(*it, del);
00146       if (len > 1) {
00147          emit endFolder();
00148       }
00149    }
00150 }
00151 
00152 QString KCrashBookmarkImporter::crashBookmarksDir( )
00153 {
00154    return locateLocal("tmp", "");
00155 }
00156 
00157 #include "kbookmarkimporter_crash.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Thu Jan 29 23:03:28 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001