KIO
krecentdocument.cpp
Go to the documentation of this file.
00001 /* -*- c++ -*- 00002 * Copyright (C)2000 Daniel M. Duley <mosfet@kde.org> 00003 * 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 00016 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00017 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00019 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00021 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00022 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00023 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00024 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00025 * SUCH DAMAGE. 00026 * 00027 */ 00028 #include <krecentdocument.h> 00029 #include <kconfig.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 <kde_file.h> 00037 #include <QtCore/QDir> 00038 #include <QtCore/QFileInfo> 00039 #include <QtCore/QTextIStream> 00040 #include <QtCore/QMutableStringListIterator> 00041 #include <QtCore/QRegExp> 00042 00043 #include <sys/types.h> 00044 #include <kconfiggroup.h> 00045 00046 QString KRecentDocument::recentDocumentDirectory() 00047 { 00048 // need to change this path, not sure where 00049 return KStandardDirs::locateLocal("data", QLatin1String("RecentDocuments/")); 00050 } 00051 00052 QStringList KRecentDocument::recentDocuments() 00053 { 00054 QDir d(recentDocumentDirectory(), "*.desktop", QDir::Time, 00055 QDir::Files | QDir::Readable | QDir::Hidden); 00056 00057 if (!d.exists()) 00058 d.mkdir(recentDocumentDirectory()); 00059 00060 const QStringList list = d.entryList(); 00061 QStringList fullList; 00062 00063 for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) { 00064 QString fileName = *it ; 00065 QString pathDesktop; 00066 if (fileName.startsWith(":")) { 00067 // FIXME: Remove when Qt will be fixed 00068 // http://bugreports.qt.nokia.com/browse/QTBUG-11223 00069 pathDesktop = KRecentDocument::recentDocumentDirectory() + *it ; 00070 } 00071 else { 00072 pathDesktop = d.absoluteFilePath( *it ); 00073 } 00074 KDesktopFile tmpDesktopFile( pathDesktop ); 00075 KUrl urlDesktopFile(tmpDesktopFile.desktopGroup().readPathEntry("URL", QString())); 00076 if (urlDesktopFile.isLocalFile() && !QFile(urlDesktopFile.toLocalFile()).exists()) { 00077 d.remove(pathDesktop); 00078 } else { 00079 fullList.append( pathDesktop ); 00080 } 00081 } 00082 00083 return fullList; 00084 } 00085 00086 void KRecentDocument::add(const KUrl& url) 00087 { 00088 KRecentDocument::add(url, KGlobal::mainComponent().componentName()); 00089 // ### componentName might not match the service filename... 00090 } 00091 00092 void KRecentDocument::add(const KUrl& url, const QString& desktopEntryName) 00093 { 00094 if ( url.isLocalFile() && KGlobal::dirs()->relativeLocation( "tmp", url.toLocalFile() ) != url.toLocalFile() ) 00095 return; // inside tmp resource, do not save 00096 00097 QString openStr = url.url(); 00098 openStr.replace( QRegExp("\\$"), "$$" ); // Desktop files with type "Link" are $-variable expanded 00099 00100 kDebug(250) << "KRecentDocument::add for " << openStr; 00101 KConfigGroup config = KGlobal::config()->group(QByteArray("RecentDocuments")); 00102 bool useRecent = config.readEntry(QLatin1String("UseRecent"), true); 00103 int maxEntries = config.readEntry(QLatin1String("MaxEntries"), 10); 00104 00105 if(!useRecent || maxEntries <= 0) 00106 return; 00107 00108 QString path = recentDocumentDirectory(); 00109 00110 QString dStr = path + url.fileName(); 00111 00112 QString ddesktop = dStr + QLatin1String(".desktop"); 00113 00114 int i=1; 00115 // check for duplicates 00116 while(QFile::exists(ddesktop)){ 00117 // see if it points to the same file and application 00118 KDesktopFile tmp(ddesktop); 00119 if ( tmp.desktopGroup().readEntry("X-KDE-LastOpenedWith") == desktopEntryName ) { 00120 KDE::utime(ddesktop, NULL); 00121 return; 00122 } 00123 // if not append a (num) to it 00124 ++i; 00125 if ( i > maxEntries ) 00126 break; 00127 ddesktop = dStr + QString::fromLatin1("[%1].desktop").arg(i); 00128 } 00129 00130 QDir dir(path); 00131 // check for max entries, delete oldest files if exceeded 00132 const QStringList list = dir.entryList(QDir::Files | QDir::Hidden, QFlags<QDir::SortFlag>(QDir::Time | QDir::Reversed)); 00133 i = list.count(); 00134 if(i > maxEntries-1){ 00135 QStringList::ConstIterator it; 00136 it = list.begin(); 00137 while(i > maxEntries-1){ 00138 QFile::remove(dir.absolutePath() + QLatin1String("/") + (*it)); 00139 --i, ++it; 00140 } 00141 } 00142 00143 // create the applnk 00144 KDesktopFile configFile(ddesktop); 00145 KConfigGroup conf = configFile.desktopGroup(); 00146 conf.writeEntry( "Type", QString::fromLatin1("Link") ); 00147 conf.writePathEntry( "URL", openStr ); 00148 // If you change the line below, change the test in the above loop 00149 conf.writeEntry( "X-KDE-LastOpenedWith", desktopEntryName ); 00150 conf.writeEntry( "Name", url.fileName() ); 00151 conf.writeEntry( "Icon", KMimeType::iconNameForUrl( url ) ); 00152 } 00153 00154 void KRecentDocument::add(const QString &openStr, bool isUrl) 00155 { 00156 if( isUrl ) { 00157 add( KUrl( openStr ) ); 00158 } else { 00159 KUrl url; 00160 url.setPath( openStr ); 00161 add( url ); 00162 } 00163 } 00164 00165 void KRecentDocument::clear() 00166 { 00167 const QStringList list = recentDocuments(); 00168 QDir dir; 00169 for(QStringList::ConstIterator it = list.begin(); it != list.end() ; ++it) 00170 dir.remove(*it); 00171 } 00172 00173 int KRecentDocument::maximumItems() 00174 { 00175 KConfigGroup cg(KGlobal::config(), QLatin1String("RecentDocuments")); 00176 return cg.readEntry(QLatin1String("MaxEntries"), 10); 00177 } 00178 00179
KDE 4.6 API Reference