• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kfile
krecentdocument.cpp
Go to the documentation of this file.
1/* -*- c++ -*-
2 * Copyright (C)2000 Daniel M. Duley <mosfet@kde.org>
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29#include "krecentdocument.h"
30
31#include <kcomponentdata.h>
32#include <kstandarddirs.h>
33#include <kurl.h>
34#include <kdebug.h>
35#include <kmimetype.h>
36#include <kdesktopfile.h>
37#include <kde_file.h>
38#include <QtCore/QDir>
39#include <QtCore/QFileInfo>
40#include <QtCore/QTextIStream>
41#include <QtCore/QMutableStringListIterator>
42#include <QtCore/QRegExp>
43
44#include <sys/types.h>
45#include <kconfiggroup.h>
46#include <ksharedconfig.h>
47
48QString KRecentDocument::recentDocumentDirectory()
49{
50 // need to change this path, not sure where
51 return KStandardDirs::locateLocal("data", QLatin1String("RecentDocuments/"));
52}
53
54QStringList KRecentDocument::recentDocuments()
55{
56 QDir d(recentDocumentDirectory(), "*.desktop", QDir::Time,
57 QDir::Files | QDir::Readable | QDir::Hidden);
58
59 if (!d.exists())
60 d.mkdir(recentDocumentDirectory());
61
62 const QStringList list = d.entryList();
63 QStringList fullList;
64
65 for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
66 QString fileName = *it ;
67 QString pathDesktop;
68 if (fileName.startsWith(":")) {
69 // FIXME: Remove when Qt will be fixed
70 // http://bugreports.qt.nokia.com/browse/QTBUG-11223
71 pathDesktop = KRecentDocument::recentDocumentDirectory() + *it ;
72 }
73 else {
74 pathDesktop = d.absoluteFilePath( *it );
75 }
76 KDesktopFile tmpDesktopFile( pathDesktop );
77 KUrl urlDesktopFile(tmpDesktopFile.desktopGroup().readPathEntry("URL", QString()));
78 if (urlDesktopFile.isLocalFile() && !QFile(urlDesktopFile.toLocalFile()).exists()) {
79 d.remove(pathDesktop);
80 } else {
81 fullList.append( pathDesktop );
82 }
83 }
84
85 return fullList;
86}
87
88void KRecentDocument::add(const KUrl& url)
89{
90 KRecentDocument::add(url, KGlobal::mainComponent().componentName());
91 // ### componentName might not match the service filename...
92}
93
94void KRecentDocument::add(const KUrl& url, const QString& desktopEntryName)
95{
96 if ( url.isLocalFile() && KGlobal::dirs()->relativeLocation( "tmp", url.toLocalFile() ) != url.toLocalFile() )
97 return; // inside tmp resource, do not save
98
99 QString openStr = url.url();
100 openStr.replace( QRegExp("\\$"), "$$" ); // Desktop files with type "Link" are $-variable expanded
101
102 kDebug(250) << "KRecentDocument::add for " << openStr;
103 KConfigGroup config = KGlobal::config()->group(QByteArray("RecentDocuments"));
104 bool useRecent = config.readEntry(QLatin1String("UseRecent"), true);
105 int maxEntries = config.readEntry(QLatin1String("MaxEntries"), 10);
106
107 if(!useRecent || maxEntries <= 0)
108 return;
109
110 const QString path = recentDocumentDirectory();
111 const QString fileName = url.fileName();
112 // don't create a file called ".desktop", it will lead to an empty name in kio_recentdocuments
113 const QString dStr = path + (fileName.isEmpty() ? QString("unnamed") : fileName);
114
115 QString ddesktop = dStr + QLatin1String(".desktop");
116
117 int i=1;
118 // check for duplicates
119 while(QFile::exists(ddesktop)){
120 // see if it points to the same file and application
121 KDesktopFile tmp(ddesktop);
122 if ( tmp.desktopGroup().readPathEntry("URL", QString()) == url.url()
123 && tmp.desktopGroup().readEntry("X-KDE-LastOpenedWith") == desktopEntryName ) {
124 KDE::utime(ddesktop, NULL);
125 return;
126 }
127 // if not append a (num) to it
128 ++i;
129 if ( i > maxEntries )
130 break;
131 ddesktop = dStr + QString::fromLatin1("[%1].desktop").arg(i);
132 }
133
134 QDir dir(path);
135 // check for max entries, delete oldest files if exceeded
136 const QStringList list = dir.entryList(QDir::Files | QDir::Hidden, QFlags<QDir::SortFlag>(QDir::Time | QDir::Reversed));
137 i = list.count();
138 if(i > maxEntries-1){
139 QStringList::ConstIterator it;
140 it = list.begin();
141 while(i > maxEntries-1){
142 QFile::remove(dir.absolutePath() + QLatin1String("/") + (*it));
143 --i, ++it;
144 }
145 }
146
147 // create the applnk
148 KDesktopFile configFile(ddesktop);
149 KConfigGroup conf = configFile.desktopGroup();
150 conf.writeEntry( "Type", QString::fromLatin1("Link") );
151 conf.writePathEntry( "URL", openStr );
152 // If you change the line below, change the test in the above loop
153 conf.writeEntry( "X-KDE-LastOpenedWith", desktopEntryName );
154 conf.writeEntry( "Name", url.fileName() );
155 conf.writeEntry( "Icon", KMimeType::iconNameForUrl( url ) );
156}
157
158void KRecentDocument::add(const QString &openStr, bool isUrl)
159{
160 if( isUrl ) {
161 add( KUrl( openStr ) );
162 } else {
163 KUrl url;
164 url.setPath( openStr );
165 add( url );
166 }
167}
168
169void KRecentDocument::clear()
170{
171 const QStringList list = recentDocuments();
172 QDir dir;
173 for(QStringList::ConstIterator it = list.begin(); it != list.end() ; ++it)
174 dir.remove(*it);
175}
176
177int KRecentDocument::maximumItems()
178{
179 KConfigGroup cg(KGlobal::config(), QLatin1String("RecentDocuments"));
180 return cg.readEntry(QLatin1String("MaxEntries"), 10);
181}
182
183
KConfigGroup
KConfigGroup::writeEntry
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
KConfigGroup::readPathEntry
QString readPathEntry(const char *key, const QString &aDefault) const
KConfigGroup::readEntry
QString readEntry(const char *key, const char *aDefault=0) const
KConfigGroup::writePathEntry
void writePathEntry(const char *pKey, const QString &path, WriteConfigFlags pFlags=Normal)
KDesktopFile
KDesktopFile::desktopGroup
KConfigGroup desktopGroup() const
KMimeType::iconNameForUrl
static QString iconNameForUrl(const KUrl &url, mode_t mode=0)
KRecentDocument::maximumItems
static int maximumItems()
Returns the maximum amount of recent document entries allowed.
Definition: krecentdocument.cpp:177
KRecentDocument::add
static void add(const KUrl &url)
Add a new item to the Recent Document menu.
Definition: krecentdocument.cpp:88
KRecentDocument::clear
static void clear()
Clear the recent document menu of all entries.
Definition: krecentdocument.cpp:169
KRecentDocument::recentDocumentDirectory
static QString recentDocumentDirectory()
Returns the path to the directory where recent document .desktop files are stored.
Definition: krecentdocument.cpp:48
KRecentDocument::recentDocuments
static QStringList recentDocuments()
Return a list of absolute paths to recent document .desktop files, sorted by date.
Definition: krecentdocument.cpp:54
KStandardDirs::relativeLocation
QString relativeLocation(const char *type, const QString &absPath)
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, bool createDir, const KComponentData &cData=KGlobal::mainComponent())
KUrl
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
KUrl::isLocalFile
bool isLocalFile() const
KUrl::setPath
void setPath(const QString &path)
KUrl::fileName
QString fileName(const DirectoryOptions &options=IgnoreTrailingSlash) const
KUrl::toLocalFile
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
kDebug
#define kDebug
kcomponentdata.h
kconfiggroup.h
kdebug.h
kdesktopfile.h
kmimetype.h
krecentdocument.h
ksharedconfig.h
kstandarddirs.h
kurl.h
KDE::utime
int utime(const QString &filename, struct utimbuf *buf)
KGlobal::mainComponent
const KComponentData & mainComponent()
KGlobal::dirs
KStandardDirs * dirs()
config
KSharedConfigPtr config()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • 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
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal