krecentdocument.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <krecentdocument.h>
00029 #include <ksimpleconfig.h>
00030 #include <kstandarddirs.h>
00031 #include <kapplication.h>
00032 #include <kurl.h>
00033 #include <kdebug.h>
00034 #include <kmimetype.h>
00035 #include <kdesktopfile.h>
00036 #include <qdir.h>
00037 #include <qfileinfo.h>
00038 #include <qtextstream.h>
00039 #include <qstringlist.h>
00040 #include <qregexp.h>
00041
00042 #include <sys/types.h>
00043 #include <utime.h>
00044
00045 QString KRecentDocument::recentDocumentDirectory()
00046 {
00047
00048 return locateLocal("data", QString::fromLatin1("RecentDocuments/"));
00049 }
00050
00051 QStringList KRecentDocument::recentDocuments()
00052 {
00053 QDir d(recentDocumentDirectory(), "*.desktop", QDir::Time,
00054 QDir::Files | QDir::Readable | QDir::Hidden);
00055
00056 if (!d.exists())
00057 d.mkdir(recentDocumentDirectory());
00058
00059 QStringList list = d.entryList();
00060 QStringList fullList;
00061
00062 for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
00063 QString pathDesktop = d.absFilePath( *it );
00064 KDesktopFile tmpDesktopFile( pathDesktop, false);
00065 KURL urlDesktopFile(tmpDesktopFile.readURL());
00066 if( urlDesktopFile.isLocalFile() && !QFile(urlDesktopFile.path()).exists())
00067 d.remove(pathDesktop);
00068 else
00069 fullList.append( pathDesktop );
00070 }
00071
00072 return fullList;
00073 }
00074
00075 void KRecentDocument::add(const KURL& url)
00076 {
00077 KRecentDocument::add(url, qApp->argv()[0]);
00078 }
00079
00080 void KRecentDocument::add(const KURL& url, const QString& desktopEntryName)
00081 {
00082 if ( url.isLocalFile() && !KGlobal::dirs()->relativeLocation("tmp", url.path()).startsWith("/"))
00083 return;
00084 QString openStr = url.url();
00085 openStr.replace( QRegExp("\\$"), "$$" );
00086
00087 kdDebug(250) << "KRecentDocument::add for " << openStr << endl;
00088 KConfig *config = KGlobal::config();
00089 QString oldGrp = config->group();
00090 config->setGroup(QString::fromLatin1("RecentDocuments"));
00091 bool useRecent = config->readBoolEntry(QString::fromLatin1("UseRecent"), true);
00092 int maxEntries = config->readNumEntry(QString::fromLatin1("MaxEntries"), 10);
00093
00094 config->setGroup(oldGrp);
00095 if(!useRecent)
00096 return;
00097
00098 QString path = recentDocumentDirectory();
00099
00100 QString dStr = path + url.fileName();
00101
00102 QString ddesktop = dStr + QString::fromLatin1(".desktop");
00103
00104 int i=1;
00105
00106 while(QFile::exists(ddesktop)){
00107
00108 KSimpleConfig tmp(ddesktop);
00109 tmp.setDesktopGroup();
00110 if(tmp.readEntry(QString::fromLatin1("X-KDE-LastOpenedWith"))
00111 == desktopEntryName)
00112 {
00113 utime(QFile::encodeName(ddesktop), NULL);
00114 return;
00115 }
00116
00117 ++i;
00118 if ( i > maxEntries )
00119 break;
00120 ddesktop = dStr + QString::fromLatin1("[%1].desktop").arg(i);
00121 }
00122
00123 QDir dir(path);
00124
00125 QStringList list = dir.entryList(QDir::Files | QDir::Hidden, QDir::Time | QDir::Reversed);
00126 i = list.count();
00127 if(i > maxEntries-1){
00128 QStringList::Iterator it;
00129 it = list.begin();
00130 while(i > maxEntries-1){
00131 QFile::remove(dir.absPath() + QString::fromLatin1("/") + (*it));
00132 --i, ++it;
00133 }
00134 }
00135
00136
00137 KSimpleConfig conf(ddesktop);
00138 conf.setDesktopGroup();
00139 conf.writeEntry( QString::fromLatin1("Type"), QString::fromLatin1("Link") );
00140 conf.writePathEntry( QString::fromLatin1("URL"), openStr );
00141
00142 conf.writeEntry( QString::fromLatin1("X-KDE-LastOpenedWith"), desktopEntryName );
00143 conf.writeEntry( QString::fromLatin1("Name"), url.fileName() );
00144 conf.writeEntry( QString::fromLatin1("Icon"), KMimeType::iconForURL( url ) );
00145 }
00146
00147 void KRecentDocument::add(const QString &openStr, bool isUrl)
00148 {
00149 if( isUrl ) {
00150 add( KURL( openStr ) );
00151 } else {
00152 KURL url;
00153 url.setPath( openStr );
00154 add( url );
00155 }
00156 }
00157
00158 void KRecentDocument::clear()
00159 {
00160 QStringList list = recentDocuments();
00161 QDir dir;
00162 for(QStringList::Iterator it = list.begin(); it != list.end() ; ++it)
00163 dir.remove(*it);
00164 }
00165
00166 int KRecentDocument::maximumItems()
00167 {
00168 KConfig *config = KGlobal::config();
00169 KConfigGroupSaver sa(config, QString::fromLatin1("RecentDocuments"));
00170 return config->readNumEntry(QString::fromLatin1("MaxEntries"), 10);
00171 }
00172
00173
This file is part of the documentation for kio Library Version 3.3.90.