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

KIO

  • sources
  • kde-4.12
  • 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().readEntry("X-KDE-LastOpenedWith") == desktopEntryName ) {
123  KDE::utime(ddesktop, NULL);
124  return;
125  }
126  // if not append a (num) to it
127  ++i;
128  if ( i > maxEntries )
129  break;
130  ddesktop = dStr + QString::fromLatin1("[%1].desktop").arg(i);
131  }
132 
133  QDir dir(path);
134  // check for max entries, delete oldest files if exceeded
135  const QStringList list = dir.entryList(QDir::Files | QDir::Hidden, QFlags<QDir::SortFlag>(QDir::Time | QDir::Reversed));
136  i = list.count();
137  if(i > maxEntries-1){
138  QStringList::ConstIterator it;
139  it = list.begin();
140  while(i > maxEntries-1){
141  QFile::remove(dir.absolutePath() + QLatin1String("/") + (*it));
142  --i, ++it;
143  }
144  }
145 
146  // create the applnk
147  KDesktopFile configFile(ddesktop);
148  KConfigGroup conf = configFile.desktopGroup();
149  conf.writeEntry( "Type", QString::fromLatin1("Link") );
150  conf.writePathEntry( "URL", openStr );
151  // If you change the line below, change the test in the above loop
152  conf.writeEntry( "X-KDE-LastOpenedWith", desktopEntryName );
153  conf.writeEntry( "Name", url.fileName() );
154  conf.writeEntry( "Icon", KMimeType::iconNameForUrl( url ) );
155 }
156 
157 void KRecentDocument::add(const QString &openStr, bool isUrl)
158 {
159  if( isUrl ) {
160  add( KUrl( openStr ) );
161  } else {
162  KUrl url;
163  url.setPath( openStr );
164  add( url );
165  }
166 }
167 
168 void KRecentDocument::clear()
169 {
170  const QStringList list = recentDocuments();
171  QDir dir;
172  for(QStringList::ConstIterator it = list.begin(); it != list.end() ; ++it)
173  dir.remove(*it);
174 }
175 
176 int KRecentDocument::maximumItems()
177 {
178  KConfigGroup cg(KGlobal::config(), QLatin1String("RecentDocuments"));
179  return cg.readEntry(QLatin1String("MaxEntries"), 10);
180 }
181 
182 
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
KGlobal::dirs
KStandardDirs * dirs()
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
QString
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)
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
QStringList
KStandardDirs::relativeLocation
QString relativeLocation(const char *type, const QString &absPath)
ksharedconfig.h
KDesktopFile
KConfigGroup
KRecentDirs::dir
QString dir(const QString &fileClass)
Returns the most recently used directory accociated with this file-class.
Definition: krecentdirs.cpp:68
KUrl::fileName
QString fileName(const DirectoryOptions &options=IgnoreTrailingSlash) const
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, const KComponentData &cData=KGlobal::mainComponent())
kstandarddirs.h
KGlobal::mainComponent
const KComponentData & mainComponent()
krecentdocument.h
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
KRecentDocument::clear
static void clear()
Clear the recent document menu of all entries.
Definition: krecentdocument.cpp:168
kcomponentdata.h
KUrl::isLocalFile
bool isLocalFile() const
KRecentDocument::maximumItems
static int maximumItems()
Returns the maximum amount of recent document entries allowed.
Definition: krecentdocument.cpp:176
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
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-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:50:02 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • 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