kio Library API Documentation

kbookmarkimporter_crash.cc

00001 // -*- c-basic-offset:4; indent-tabs-mode:nil -*- 00002 // vim: set ts=4 sts=4 sw=4 et: 00003 /* This file is part of the KDE libraries 00004 Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 00021 #include "kbookmarkimporter_crash.h" 00022 00023 #include <kfiledialog.h> 00024 #include <kstringhandler.h> 00025 #include <klocale.h> 00026 #include <kdebug.h> 00027 #include <kapplication.h> 00028 #include <kstandarddirs.h> 00029 #include <qfile.h> 00030 #include <qdir.h> 00031 #include <qstring.h> 00032 #include <qtextcodec.h> 00033 #include <dcopclient.h> 00034 00035 #include <sys/types.h> 00036 #include <stddef.h> 00037 #include <dirent.h> 00038 #include <sys/stat.h> 00039 00040 typedef QMap<QString, QString> ViewMap; 00041 00042 // KDE 4.0: remove this BC keeping stub 00043 void KCrashBookmarkImporter::parseCrashLog( QString /*filename*/, bool /*del*/ ) 00044 { 00045 ; 00046 } 00047 00048 ViewMap KCrashBookmarkImporterImpl::parseCrashLog_noemit( const QString & filename, bool del ) 00049 { 00050 static const int g_lineLimit = 4096; 00051 00052 QFile f( filename ); 00053 ViewMap views; 00054 00055 if ( !f.open( IO_ReadOnly ) ) 00056 return views; 00057 00058 QCString s( g_lineLimit ); 00059 00060 QTextCodec * codec = QTextCodec::codecForName( "UTF-8" ); 00061 Q_ASSERT( codec ); 00062 if ( !codec ) 00063 return views; 00064 00065 while ( f.readLine( s.data(), g_lineLimit ) >=0 ) 00066 { 00067 if ( s[s.length()-1] != '\n' ) 00068 { 00069 kdWarning() << "Crash bookmarks contain a line longer than " << g_lineLimit << ". Skipping." << endl; 00070 continue; 00071 } 00072 QString t = codec->toUnicode( s.stripWhiteSpace() ); 00073 QRegExp rx( "(.*)\\((.*)\\):(.*)$" ); 00074 rx.setMinimal( true ); 00075 if ( !rx.exactMatch( t ) ) 00076 continue; 00077 if ( rx.cap(1) == "opened" ) 00078 views[rx.cap(2)] = rx.cap(3); 00079 else if ( rx.cap(1) == "close" ) 00080 views.remove( rx.cap(2) ); 00081 } 00082 00083 f.close(); 00084 00085 if ( del ) 00086 f.remove(); 00087 00088 return views; 00089 } 00090 00091 QStringList KCrashBookmarkImporter::getCrashLogs() 00092 { 00093 return KCrashBookmarkImporterImpl::getCrashLogs(); 00094 } 00095 00096 QStringList KCrashBookmarkImporterImpl::getCrashLogs() 00097 { 00098 QMap<QString, bool> activeLogs; 00099 00100 DCOPClient* dcop = kapp->dcopClient(); 00101 00102 QCStringList apps = dcop->registeredApplications(); 00103 for ( QCStringList::Iterator it = apps.begin(); it != apps.end(); ++it ) 00104 { 00105 QCString &clientId = *it; 00106 00107 if ( qstrncmp(clientId, "konqueror", 9) != 0 ) 00108 continue; 00109 00110 QByteArray data, replyData; 00111 QCString replyType; 00112 QDataStream arg( data, IO_WriteOnly ); 00113 00114 if ( !dcop->call( clientId.data(), "KonquerorIface", 00115 "crashLogFile()", data, replyType, replyData) ) 00116 { 00117 kdWarning() << "can't find dcop function KonquerorIface::crashLogFile()" << endl; 00118 continue; 00119 } 00120 00121 if ( replyType != "QString" ) 00122 continue; 00123 00124 QDataStream reply( replyData, IO_ReadOnly ); 00125 QString ret; 00126 reply >> ret; 00127 activeLogs[ret] = true; 00128 } 00129 00130 QDir d( KCrashBookmarkImporterImpl().findDefaultLocation() ); 00131 d.setSorting( QDir::Time ); 00132 d.setFilter( QDir::Files ); 00133 d.setNameFilter( "konqueror-crash-*.log" ); 00134 00135 const QFileInfoList *list = d.entryInfoList(); 00136 QFileInfoListIterator it( *list ); 00137 00138 QFileInfo *fi; 00139 QStringList crashFiles; 00140 00141 int count = 0; 00142 for ( ; ( fi = it.current() ) != 0; ++it ) 00143 { 00144 bool stillAlive = activeLogs.contains( fi->absFilePath() ); 00145 if ( !stillAlive ) 00146 crashFiles << fi->absFilePath(); 00147 if ( count++ > 20 ) 00148 break; 00149 } 00150 00151 return crashFiles; 00152 } 00153 00154 void KCrashBookmarkImporterImpl::parse() 00155 { 00156 QStringList crashFiles = KCrashBookmarkImporterImpl::getCrashLogs(); 00157 int count = 1; 00158 for ( QStringList::Iterator it = crashFiles.begin(); it != crashFiles.end(); ++it ) 00159 { 00160 ViewMap views; 00161 views = parseCrashLog_noemit( *it, m_shouldDelete ); 00162 int outerFolder = ( crashFiles.count() > 1 ) && (views.count() > 0); 00163 if ( outerFolder ) 00164 emit newFolder( QString("Konqueror Window %1").arg(count++), false, "" ); 00165 for ( ViewMap::Iterator it = views.begin(); it != views.end(); ++it ) 00166 emit newBookmark( it.data(), it.data().latin1(), QString("") ); 00167 if ( outerFolder ) 00168 emit endFolder(); 00169 } 00170 } 00171 00172 QString KCrashBookmarkImporter::crashBookmarksDir() 00173 { 00174 static KCrashBookmarkImporterImpl *p = 0; 00175 if (!p) 00176 p = new KCrashBookmarkImporterImpl; 00177 return p->findDefaultLocation(); 00178 } 00179 00180 void KCrashBookmarkImporterImpl::setShouldDelete( bool shouldDelete ) 00181 { 00182 m_shouldDelete = shouldDelete; 00183 } 00184 00185 void KCrashBookmarkImporter::parseCrashBookmarks( bool del ) 00186 { 00187 KCrashBookmarkImporterImpl importer; 00188 importer.setFilename( m_fileName ); 00189 importer.setShouldDelete( del ); 00190 importer.setupSignalForwards( &importer, this ); 00191 importer.parse(); 00192 } 00193 00194 QString KCrashBookmarkImporterImpl::findDefaultLocation( bool ) const 00195 { 00196 return locateLocal( "tmp", "" ); 00197 } 00198 00199 #include "kbookmarkimporter_crash.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 12 15:08:43 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003