• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.4 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 
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  QString path = recentDocumentDirectory();
111 
112  QString dStr = path + url.fileName();
113 
114  QString ddesktop = dStr + QLatin1String(".desktop");
115 
116  int i=1;
117  // check for duplicates
118  while(QFile::exists(ddesktop)){
119  // see if it points to the same file and application
120  KDesktopFile tmp(ddesktop);
121  if ( tmp.desktopGroup().readEntry("X-KDE-LastOpenedWith") == desktopEntryName ) {
122  KDE::utime(ddesktop, NULL);
123  return;
124  }
125  // if not append a (num) to it
126  ++i;
127  if ( i > maxEntries )
128  break;
129  ddesktop = dStr + QString::fromLatin1("[%1].desktop").arg(i);
130  }
131 
132  QDir dir(path);
133  // check for max entries, delete oldest files if exceeded
134  const QStringList list = dir.entryList(QDir::Files | QDir::Hidden, QFlags<QDir::SortFlag>(QDir::Time | QDir::Reversed));
135  i = list.count();
136  if(i > maxEntries-1){
137  QStringList::ConstIterator it;
138  it = list.begin();
139  while(i > maxEntries-1){
140  QFile::remove(dir.absolutePath() + QLatin1String("/") + (*it));
141  --i, ++it;
142  }
143  }
144 
145  // create the applnk
146  KDesktopFile configFile(ddesktop);
147  KConfigGroup conf = configFile.desktopGroup();
148  conf.writeEntry( "Type", QString::fromLatin1("Link") );
149  conf.writePathEntry( "URL", openStr );
150  // If you change the line below, change the test in the above loop
151  conf.writeEntry( "X-KDE-LastOpenedWith", desktopEntryName );
152  conf.writeEntry( "Name", url.fileName() );
153  conf.writeEntry( "Icon", KMimeType::iconNameForUrl( url ) );
154 }
155 
156 void KRecentDocument::add(const QString &openStr, bool isUrl)
157 {
158  if( isUrl ) {
159  add( KUrl( openStr ) );
160  } else {
161  KUrl url;
162  url.setPath( openStr );
163  add( url );
164  }
165 }
166 
167 void KRecentDocument::clear()
168 {
169  const QStringList list = recentDocuments();
170  QDir dir;
171  for(QStringList::ConstIterator it = list.begin(); it != list.end() ; ++it)
172  dir.remove(*it);
173 }
174 
175 int KRecentDocument::maximumItems()
176 {
177  KConfigGroup cg(KGlobal::config(), QLatin1String("RecentDocuments"));
178  return cg.readEntry(QLatin1String("MaxEntries"), 10);
179 }
180 
181 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jun 1 2013 20:20:49 by doxygen 1.8.3.1 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.10.4 API Reference

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