kio Library: krecentdirs.cpp Source File (kio Library)

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

# krecentdirs.cpp

    00001 /* -*- c++ -*-
    00002  * Copyright (C)2000 Waldo Bastian <bastian@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 <krecentdirs.h>
    00029 #include <ksimpleconfig.h>
    00030 #include <kstandarddirs.h>
    00031 #include <kglobalsettings.h>
    00032
    00033 #define MAX_DIR_HISTORY 3
    00034
    00035 static void recentdirs_done(KConfig *config)
    00036 {
    00037    if (config == KGlobal::config())
    00038    {
    00039       config->sync();
    00040    }
    00041    else
    00042    {
    00043       delete config;
    00044    }
    00045 }
    00046
    00047 static KConfig *recentdirs_readList(QString &key, QStringList &result, bool readOnly)
    00048 {
    00049    KConfig *config;
    00050    if ((key.length() < 2) || (key[0] != ':'))
    00051      key = ":default";
    00052    if (key[1] == ':')
    00053    {
    00054       key = key.mid(2);
    00055       config = new KSimpleConfig(QString::fromLatin1("krecentdirsrc"), readOnly);
    00056    }
    00057    else
    00058    {
    00059       key = key.mid(1);
    00060       config = KGlobal::config();
    00061       config->setGroup(QString::fromLatin1("Recent Dirs"));
    00062    }
    00063
    00064    result=config->readPathListEntry(key);
    00065    if (result.isEmpty())
    00066    {
    00067       result.append(KGlobalSettings::documentPath());
    00068    }
    00069    return config;
    00070 }
    00071
    00072 QStringList KRecentDirs::list(const QString &fileClass)
    00073 {
    00074    QString key = fileClass;
    00075    QStringList result;
    00076    recentdirs_done(recentdirs_readList(key, result, true));
    00077    return result;
    00078 }
    00079
    00080 QString KRecentDirs::dir(const QString &fileClass)
    00081 {
    00082    QStringList result = list(fileClass);
    00083    return result[0];
    00084 }
    00085
    00086 void KRecentDirs::add(const QString &fileClass, const QString &directory)
    00087 {
    00088    QString key = fileClass;
    00089    QStringList result;
    00090    KConfig *config = recentdirs_readList(key, result, false);
    00091    // make sure the dir is first in history
    00092    result.remove(directory);
    00093    result.prepend(directory);
    00094    while(result.count() > MAX_DIR_HISTORY)
    00095       result.remove(result.fromLast());
    00096    config->writePathEntry(key, result);
    00097    recentdirs_done(config);
    00098 }
    00099

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:24:03 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003