• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • 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 
00029 #include "krecentdocument.h"
00030 
00031 #include <kcomponentdata.h>
00032 #include <kstandarddirs.h>
00033 #include <kurl.h>
00034 #include <kdebug.h>
00035 #include <kmimetype.h>
00036 #include <kdesktopfile.h>
00037 #include <kde_file.h>
00038 #include <QtCore/QDir>
00039 #include <QtCore/QFileInfo>
00040 #include <QtCore/QTextIStream>
00041 #include <QtCore/QMutableStringListIterator>
00042 #include <QtCore/QRegExp>
00043 
00044 #include <sys/types.h>
00045 #include <kconfiggroup.h>
00046 #include <ksharedconfig.h>
00047 
00048 QString KRecentDocument::recentDocumentDirectory()
00049 {
00050     // need to change this path, not sure where
00051     return KStandardDirs::locateLocal("data", QLatin1String("RecentDocuments/"));
00052 }
00053 
00054 QStringList KRecentDocument::recentDocuments()
00055 {
00056     QDir d(recentDocumentDirectory(), "*.desktop", QDir::Time,
00057            QDir::Files | QDir::Readable | QDir::Hidden);
00058 
00059     if (!d.exists())
00060         d.mkdir(recentDocumentDirectory());
00061 
00062     const QStringList list = d.entryList();
00063     QStringList fullList;
00064 
00065     for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
00066        QString fileName = *it ;
00067        QString pathDesktop;
00068        if (fileName.startsWith(":")) {
00069        // FIXME: Remove when Qt will be fixed
00070        // http://bugreports.qt.nokia.com/browse/QTBUG-11223
00071            pathDesktop = KRecentDocument::recentDocumentDirectory() + *it ;
00072        }
00073        else {
00074            pathDesktop = d.absoluteFilePath( *it );
00075        }
00076        KDesktopFile tmpDesktopFile( pathDesktop );
00077        KUrl urlDesktopFile(tmpDesktopFile.desktopGroup().readPathEntry("URL", QString()));
00078        if (urlDesktopFile.isLocalFile() && !QFile(urlDesktopFile.toLocalFile()).exists()) {
00079            d.remove(pathDesktop);
00080        } else {
00081            fullList.append( pathDesktop );
00082        }
00083     }
00084 
00085     return fullList;
00086 }
00087 
00088 void KRecentDocument::add(const KUrl& url)
00089 {
00090     KRecentDocument::add(url, KGlobal::mainComponent().componentName());
00091     // ### componentName might not match the service filename...
00092 }
00093 
00094 void KRecentDocument::add(const KUrl& url, const QString& desktopEntryName)
00095 {
00096     if ( url.isLocalFile() && KGlobal::dirs()->relativeLocation( "tmp", url.toLocalFile() ) != url.toLocalFile() )
00097       return; // inside tmp resource, do not save
00098 
00099     QString openStr = url.url();
00100     openStr.replace( QRegExp("\\$"), "$$" ); // Desktop files with type "Link" are $-variable expanded
00101 
00102     kDebug(250) << "KRecentDocument::add for " << openStr;
00103     KConfigGroup config = KGlobal::config()->group(QByteArray("RecentDocuments"));
00104     bool useRecent = config.readEntry(QLatin1String("UseRecent"), true);
00105     int maxEntries = config.readEntry(QLatin1String("MaxEntries"), 10);
00106 
00107     if(!useRecent || maxEntries <= 0)
00108         return;
00109 
00110     QString path = recentDocumentDirectory();
00111 
00112     QString dStr = path + url.fileName();
00113 
00114     QString ddesktop = dStr + QLatin1String(".desktop");
00115 
00116     int i=1;
00117     // check for duplicates
00118     while(QFile::exists(ddesktop)){
00119         // see if it points to the same file and application
00120         KDesktopFile tmp(ddesktop);
00121         if ( tmp.desktopGroup().readEntry("X-KDE-LastOpenedWith") == desktopEntryName ) {
00122             KDE::utime(ddesktop, NULL);
00123             return;
00124         }
00125         // if not append a (num) to it
00126         ++i;
00127         if ( i > maxEntries )
00128             break;
00129         ddesktop = dStr + QString::fromLatin1("[%1].desktop").arg(i);
00130     }
00131 
00132     QDir dir(path);
00133     // check for max entries, delete oldest files if exceeded
00134     const QStringList list = dir.entryList(QDir::Files | QDir::Hidden, QFlags<QDir::SortFlag>(QDir::Time | QDir::Reversed));
00135     i = list.count();
00136     if(i > maxEntries-1){
00137         QStringList::ConstIterator it;
00138         it = list.begin();
00139         while(i > maxEntries-1){
00140             QFile::remove(dir.absolutePath() + QLatin1String("/") + (*it));
00141             --i, ++it;
00142         }
00143     }
00144 
00145     // create the applnk
00146     KDesktopFile configFile(ddesktop);
00147     KConfigGroup conf = configFile.desktopGroup();
00148     conf.writeEntry( "Type", QString::fromLatin1("Link") );
00149     conf.writePathEntry( "URL", openStr );
00150     // If you change the line below, change the test in the above loop
00151     conf.writeEntry( "X-KDE-LastOpenedWith", desktopEntryName );
00152     conf.writeEntry( "Name", url.fileName() );
00153     conf.writeEntry( "Icon", KMimeType::iconNameForUrl( url ) );
00154 }
00155 
00156 void KRecentDocument::add(const QString &openStr, bool isUrl)
00157 {
00158     if( isUrl ) {
00159         add( KUrl( openStr ) );
00160     } else {
00161         KUrl url;
00162         url.setPath( openStr );
00163         add( url );
00164     }
00165 }
00166 
00167 void KRecentDocument::clear()
00168 {
00169   const QStringList list = recentDocuments();
00170   QDir dir;
00171   for(QStringList::ConstIterator it = list.begin(); it != list.end() ; ++it)
00172     dir.remove(*it);
00173 }
00174 
00175 int KRecentDocument::maximumItems()
00176 {
00177     KConfigGroup cg(KGlobal::config(), QLatin1String("RecentDocuments"));
00178     return cg.readEntry(QLatin1String("MaxEntries"), 10);
00179 }
00180 
00181 

KIO

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.5
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