| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# krecentdocument.cpp

    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 <ksimpleconfig.h>
    00030 #include <kstandarddirs.h>
    00031 #include <kapplication.h>
    00032 #include <kurl.h>
    00033 #include <kdebug.h>
    00034 #include <kmimetype.h>
    00035
    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     // need to change this path, not sure where
    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         fullList.append( d.absFilePath( *it ) );
    00064     }
    00065
    00066     return fullList;
    00067 }
    00068
    00069 void KRecentDocument::add(const KURL& url)
    00070 {
    00071     KRecentDocument::add(url, qApp->argv()[0]); // ### argv[0] might not match the service filename!
    00072 }
    00073
    00074 void KRecentDocument::add(const KURL& url, const QString& desktopEntryName)
    00075 {
    00076     QString openStr = url.url();
    00077     openStr.replace( QRegExp("\\$"), "$$" ); // Desktop files with type "Link" are $-variable expanded
    00078
    00079     kdDebug(250) << "KRecentDocument::add for " << openStr << endl;
    00080     KConfig *config = KGlobal::config();
    00081     QString oldGrp = config->group();
    00082     config->setGroup(QString::fromLatin1("RecentDocuments"));
    00083     bool useRecent = config->readBoolEntry(QString::fromLatin1("UseRecent"), true);
    00084     int maxEntries = config->readNumEntry(QString::fromLatin1("MaxEntries"), 10);
    00085
    00086     config->setGroup(oldGrp);
    00087     if(!useRecent)
    00088         return;
    00089
    00090     QString path = recentDocumentDirectory();
    00091
    00092     QString dStr = path + url.fileName();
    00093
    00094     QString ddesktop = dStr + QString::fromLatin1(".desktop");
    00095
    00096     int i=1;
    00097     // check for duplicates
    00098     while(QFile::exists(ddesktop)){
    00099         // see if it points to the same file and application
    00100         KSimpleConfig tmp(ddesktop);
    00101         tmp.setDesktopGroup();
    00102         if(tmp.readEntry(QString::fromLatin1("X-KDE-LastOpenedWith"))
    00103            == desktopEntryName)
    00104         {
    00105             utime(QFile::encodeName(ddesktop), NULL);
    00106             return;
    00107         }
    00108         // if not append a (num) to it
    00109         ++i;
    00110         if ( i > maxEntries )
    00111             break;
    00112         ddesktop = dStr + QString::fromLatin1("[%1].desktop").arg(i);
    00113     }
    00114
    00115     QDir dir(path);
    00116     // check for max entries, delete oldest files if exceeded
    00117     QStringList list = dir.entryList(QDir::Files, QDir::Time | QDir::Reversed);
    00118     i = list.count();
    00119     if(i > maxEntries-1){
    00120         QStringList::Iterator it;
    00121         it = list.begin();
    00122         while(i > maxEntries-1){
    00123             QFile::remove(dir.absPath() + QString::fromLatin1("/") + (*it));
    00124             --i, ++it;
    00125         }
    00126     }
    00127
    00128     // create the applnk
    00129     KSimpleConfig conf(ddesktop);
    00130     conf.setDesktopGroup();
    00131     conf.writeEntry( QString::fromLatin1("Type"), QString::fromLatin1("Link") );
    00132     conf.writeEntry( QString::fromLatin1("URL"), openStr );
    00133     // If you change the line below, change the test in the above loop
    00134     conf.writeEntry( QString::fromLatin1("X-KDE-LastOpenedWith"), desktopEntryName );
    00135     conf.writeEntry( QString::fromLatin1("Name"), url.fileName() );
    00136     conf.writeEntry( QString::fromLatin1("Icon"), KMimeType::iconForURL( url ) );
    00137 }
    00138
    00139 void KRecentDocument::add(const QString &openStr, bool isUrl)
    00140 {
    00141     if( isUrl ) {
    00142         add( KURL( openStr ) );
    00143     } else {
    00144         KURL url;
    00145         url.setPath( openStr );
    00146         add( url );
    00147     }
    00148 }
    00149
    00150 void KRecentDocument::clear()
    00151 {
    00152   QStringList list = recentDocuments();
    00153   QDir dir;
    00154   for(QStringList::Iterator it = list.begin(); it != list.end() ; ++it)
    00155     dir.remove(*it);
    00156 }
    00157
    00158 int KRecentDocument::maximumItems()
    00159 {
    00160     KConfig *config = KGlobal::config();
    00161     KConfigGroupSaver sa(config, QString::fromLatin1("RecentDocuments"));
    00162     return config->readNumEntry(QString::fromLatin1("MaxEntries"), 10);
    00163 }
    00164
    00165

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:14:14 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001