• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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 pathDesktop = d.absoluteFilePath( *it );
00065        KDesktopFile tmpDesktopFile( pathDesktop );
00066        KUrl urlDesktopFile(tmpDesktopFile.desktopGroup().readPathEntry("URL", QString()));
00067        if (urlDesktopFile.isLocalFile() && !QFile(urlDesktopFile.toLocalFile()).exists()) {
00068            d.remove(pathDesktop);
00069        } else {
00070            fullList.append( pathDesktop );
00071        }
00072     }
00073 
00074     return fullList;
00075 }
00076 
00077 void KRecentDocument::add(const KUrl& url)
00078 {
00079     KRecentDocument::add(url, KGlobal::mainComponent().componentName());
00080     // ### componentName might not match the service filename...
00081 }
00082 
00083 void KRecentDocument::add(const KUrl& url, const QString& desktopEntryName)
00084 {
00085     if ( url.isLocalFile() && KGlobal::dirs()->relativeLocation( "tmp", url.toLocalFile() ) != url.toLocalFile() )
00086       return; // inside tmp resource, do not save
00087 
00088     QString openStr = url.url();
00089     openStr.replace( QRegExp("\\$"), "$$" ); // Desktop files with type "Link" are $-variable expanded
00090 
00091     kDebug(250) << "KRecentDocument::add for " << openStr;
00092     KConfigGroup config = KGlobal::config()->group(QByteArray("RecentDocuments"));
00093     bool useRecent = config.readEntry(QLatin1String("UseRecent"), true);
00094     int maxEntries = config.readEntry(QLatin1String("MaxEntries"), 10);
00095 
00096     if(!useRecent || maxEntries <= 0)
00097         return;
00098 
00099     QString path = recentDocumentDirectory();
00100 
00101     QString dStr = path + url.fileName();
00102 
00103     QString ddesktop = dStr + QLatin1String(".desktop");
00104 
00105     int i=1;
00106     // check for duplicates
00107     while(QFile::exists(ddesktop)){
00108         // see if it points to the same file and application
00109         KDesktopFile tmp(ddesktop);
00110         if ( tmp.desktopGroup().readEntry("X-KDE-LastOpenedWith") == desktopEntryName ) {
00111             KDE::utime(ddesktop, NULL);
00112             return;
00113         }
00114         // if not append a (num) to it
00115         ++i;
00116         if ( i > maxEntries )
00117             break;
00118         ddesktop = dStr + QString::fromLatin1("[%1].desktop").arg(i);
00119     }
00120 
00121     QDir dir(path);
00122     // check for max entries, delete oldest files if exceeded
00123     const QStringList list = dir.entryList(QDir::Files | QDir::Hidden, QFlags<QDir::SortFlag>(QDir::Time | QDir::Reversed));
00124     i = list.count();
00125     if(i > maxEntries-1){
00126         QStringList::ConstIterator it;
00127         it = list.begin();
00128         while(i > maxEntries-1){
00129             QFile::remove(dir.absolutePath() + QLatin1String("/") + (*it));
00130             --i, ++it;
00131         }
00132     }
00133 
00134     // create the applnk
00135     KDesktopFile configFile(ddesktop);
00136     KConfigGroup conf = configFile.desktopGroup();
00137     conf.writeEntry( "Type", QString::fromLatin1("Link") );
00138     conf.writePathEntry( "URL", openStr );
00139     // If you change the line below, change the test in the above loop
00140     conf.writeEntry( "X-KDE-LastOpenedWith", desktopEntryName );
00141     conf.writeEntry( "Name", url.fileName() );
00142     conf.writeEntry( "Icon", KMimeType::iconNameForUrl( url ) );
00143 }
00144 
00145 void KRecentDocument::add(const QString &openStr, bool isUrl)
00146 {
00147     if( isUrl ) {
00148         add( KUrl( openStr ) );
00149     } else {
00150         KUrl url;
00151         url.setPath( openStr );
00152         add( url );
00153     }
00154 }
00155 
00156 void KRecentDocument::clear()
00157 {
00158   const QStringList list = recentDocuments();
00159   QDir dir;
00160   for(QStringList::ConstIterator it = list.begin(); it != list.end() ; ++it)
00161     dir.remove(*it);
00162 }
00163 
00164 int KRecentDocument::maximumItems()
00165 {
00166     KConfigGroup cg(KGlobal::config(), QLatin1String("RecentDocuments"));
00167     return cg.readEntry(QLatin1String("MaxEntries"), 10);
00168 }
00169 
00170 

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal