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

KIO

  • sources
  • kde-4.14
  • kdelibs
  • 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 
48 QString KRecentDocument::recentDocumentDirectory()
49 {
50  // need to change this path, not sure where
51  return KStandardDirs::locateLocal("data", QLatin1String("RecentDocuments/"));
52 }
53 
54 QStringList 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 
88 void KRecentDocument::add(const KUrl& url)
89 {
90  KRecentDocument::add(url, KGlobal::mainComponent().componentName());
91  // ### componentName might not match the service filename...
92 }
93 
94 void 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 
158 void 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 
169 void 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 
177 int KRecentDocument::maximumItems()
178 {
179  KConfigGroup cg(KGlobal::config(), QLatin1String("RecentDocuments"));
180  return cg.readEntry(QLatin1String("MaxEntries"), 10);
181 }
182 
183 
KConfigGroup::readPathEntry
QString readPathEntry(const QString &pKey, const QString &aDefault) const
KConfigGroup::writePathEntry
void writePathEntry(const QString &pKey, const QString &path, WriteConfigFlags pFlags=Normal)
kdebug.h
kmimetype.h
kurl.h
KRecentDocument::recentDocuments
static QStringList recentDocuments()
Return a list of absolute paths to recent document .desktop files, sorted by date.
Definition: krecentdocument.cpp:54
QByteArray
QFile::remove
bool remove()
KGlobal::dirs
KStandardDirs * dirs()
QDir::remove
bool remove(const QString &fileName)
KConfig::group
KConfigGroup group(const QByteArray &group)
KConfigGroup::writeEntry
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
KUrl::toLocalFile
QString toLocalFile(AdjustPathOption trailing=LeaveTrailingSlash) const
QFile::exists
bool exists() const
KRecentDocument::add
static void add(const KUrl &url)
Add a new item to the Recent Document menu.
Definition: krecentdocument.cpp:88
kdesktopfile.h
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
QFile
KUrl
config
KSharedConfigPtr config()
KUrl::setPath
void setPath(const QString &path)
KRecentDocument::recentDocumentDirectory
static QString recentDocumentDirectory()
Returns the path to the directory where recent document .desktop files are stored.
Definition: krecentdocument.cpp:48
QRegExp
QFlags
QList::count
int count(const T &value) const
QDir::exists
bool exists() const
QList::append
void append(const T &value)
QString::isEmpty
bool isEmpty() const
KStandardDirs::relativeLocation
QString relativeLocation(const char *type, const QString &absPath)
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
ksharedconfig.h
QString
QStringList
QList::end
iterator end()
KDesktopFile
QDir
KConfigGroup
QString::replace
QString & replace(int position, int n, QChar after)
KRecentDirs::dir
QString dir(const QString &fileClass)
Returns the most recently used directory accociated with this file-class.
Definition: krecentdirs.cpp:68
QLatin1String
KUrl::fileName
QString fileName(const DirectoryOptions &options=IgnoreTrailingSlash) const
QDir::absolutePath
QString absolutePath() const
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
QDir::mkdir
bool mkdir(const QString &dirName) const
kstandarddirs.h
QDir::entryList
QStringList entryList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
QList::ConstIterator
typedef ConstIterator
QDir::absoluteFilePath
QString absoluteFilePath(const QString &fileName) const
KGlobal::mainComponent
const KComponentData & mainComponent()
krecentdocument.h
QString::fromLatin1
QString fromLatin1(const char *str, int size)
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
KRecentDocument::clear
static void clear()
Clear the recent document menu of all entries.
Definition: krecentdocument.cpp:169
kcomponentdata.h
KUrl::isLocalFile
bool isLocalFile() const
KRecentDocument::maximumItems
static int maximumItems()
Returns the maximum amount of recent document entries allowed.
Definition: krecentdocument.cpp:177
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
KDE::utime
int utime(const QString &filename, struct utimbuf *buf)
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
KDesktopFile::desktopGroup
KConfigGroup desktopGroup() const
QList::begin
iterator begin()
kconfiggroup.h
KRecentDirs::list
QStringList list(const QString &fileClass)
Returns a list of directories associated with this file-class.
Definition: krecentdirs.cpp:60
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:53 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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