kio
kbookmarkimporter_crash.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00043 void KCrashBookmarkImporter::parseCrashLog( QString , bool )
00044 {
00045 ;
00046 }
00047
00048 ViewMap KCrashBookmarkImporterImpl::parseCrashLog_noemit( const QString & filename, bool del )
00049 {
00050 static const int g_lineLimit = 16*1024;
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) && (count < 20); ++it, ++count )
00143 {
00144 bool stillAlive = activeLogs.contains( fi->absFilePath() );
00145 if ( !stillAlive )
00146 crashFiles << fi->absFilePath();
00147 }
00148
00149 for ( ; ( fi = it.current() ) != 0; ++it )
00150 {
00151 QFile::remove( fi->absFilePath() );
00152 }
00153
00154 return crashFiles;
00155 }
00156
00157 void KCrashBookmarkImporterImpl::parse()
00158 {
00159 QDict<bool> signatureMap;
00160 QStringList crashFiles = KCrashBookmarkImporterImpl::getCrashLogs();
00161 int count = 1;
00162 for ( QStringList::Iterator it = crashFiles.begin(); it != crashFiles.end(); ++it )
00163 {
00164 ViewMap views;
00165 views = parseCrashLog_noemit( *it, m_shouldDelete );
00166 QString signature;
00167 for ( ViewMap::Iterator vit = views.begin(); vit != views.end(); ++vit )
00168 signature += "|"+vit.data();
00169 if (signatureMap[signature])
00170 {
00171
00172 QFile::remove(*it);
00173 continue;
00174 }
00175
00176 signatureMap.insert(signature, (bool *) true);
00177
00178 int outerFolder = ( crashFiles.count() > 1 ) && (views.count() > 0);
00179 if ( outerFolder )
00180 emit newFolder( QString("Konqueror Window %1").arg(count++), false, "" );
00181 for ( ViewMap::Iterator vit = views.begin(); vit != views.end(); ++vit )
00182 emit newBookmark( vit.data(), vit.data().latin1(), QString("") );
00183 if ( outerFolder )
00184 emit endFolder();
00185 }
00186 }
00187
00188 QString KCrashBookmarkImporter::crashBookmarksDir()
00189 {
00190 static KCrashBookmarkImporterImpl *p = 0;
00191 if (!p)
00192 p = new KCrashBookmarkImporterImpl;
00193 return p->findDefaultLocation();
00194 }
00195
00196 void KCrashBookmarkImporterImpl::setShouldDelete( bool shouldDelete )
00197 {
00198 m_shouldDelete = shouldDelete;
00199 }
00200
00201 void KCrashBookmarkImporter::parseCrashBookmarks( bool del )
00202 {
00203 KCrashBookmarkImporterImpl importer;
00204 importer.setFilename( m_fileName );
00205 importer.setShouldDelete( del );
00206 importer.setupSignalForwards( &importer, this );
00207 importer.parse();
00208 }
00209
00210 QString KCrashBookmarkImporterImpl::findDefaultLocation( bool ) const
00211 {
00212 return locateLocal( "tmp", "" );
00213 }
00214
00215 #include "kbookmarkimporter_crash.moc"