You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
5.9 KiB
178 lines
5.9 KiB
15 years ago
|
/* -*- c++ -*-
|
||
|
* Copyright (C)2000 Daniel M. Duley <mosfet@kde.org>
|
||
|
*
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* Redistribution and use in source and binary forms, with or without
|
||
|
* modification, are permitted provided that the following conditions
|
||
|
* are met:
|
||
|
* 1. Redistributions of source code must retain the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer.
|
||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer in the
|
||
|
* documentation and/or other materials provided with the distribution.
|
||
|
*
|
||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||
|
* SUCH DAMAGE.
|
||
|
*
|
||
|
*/
|
||
|
#include <krecentdocument.h>
|
||
|
#include <ksimpleconfig.h>
|
||
|
#include <kstandarddirs.h>
|
||
|
#include <kapplication.h>
|
||
|
#include <kurl.h>
|
||
|
#include <kdebug.h>
|
||
|
#include <kmimetype.h>
|
||
|
#include <kdesktopfile.h>
|
||
15 years ago
|
#include <tqdir.h>
|
||
|
#include <tqfileinfo.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
15 years ago
|
#include <tqstringlist.h>
|
||
|
#include <tqregexp.h>
|
||
15 years ago
|
|
||
|
#include <sys/types.h>
|
||
|
#include <utime.h>
|
||
|
|
||
15 years ago
|
TQString KRecentDocument::recentDocumentDirectory()
|
||
15 years ago
|
{
|
||
|
// need to change this path, not sure where
|
||
13 years ago
|
return locateLocal("data", TQString::fromLatin1("RecentDocuments/"));
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQStringList KRecentDocument::recentDocuments()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQDir d(recentDocumentDirectory(), "*.desktop", TQDir::Time,
|
||
|
TQDir::Files | TQDir::Readable | TQDir::Hidden);
|
||
15 years ago
|
|
||
|
if (!d.exists())
|
||
|
d.mkdir(recentDocumentDirectory());
|
||
|
|
||
15 years ago
|
TQStringList list = d.entryList();
|
||
|
TQStringList fullList;
|
||
15 years ago
|
|
||
15 years ago
|
for (TQStringList::ConstIterator it = list.begin(); it != list.end(); ++it) {
|
||
|
TQString pathDesktop = d.absFilePath( *it );
|
||
15 years ago
|
KDesktopFile tmpDesktopFile( pathDesktop, false);
|
||
|
KURL urlDesktopFile(tmpDesktopFile.readURL());
|
||
15 years ago
|
if( urlDesktopFile.isLocalFile() && !TQFile(urlDesktopFile.path()).exists())
|
||
15 years ago
|
d.remove(pathDesktop);
|
||
|
else
|
||
|
fullList.append( pathDesktop );
|
||
|
}
|
||
|
|
||
|
return fullList;
|
||
|
}
|
||
|
|
||
|
void KRecentDocument::add(const KURL& url)
|
||
|
{
|
||
14 years ago
|
KRecentDocument::add(url, tqApp->argv()[0]); // ### argv[0] might not match the service filename!
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void KRecentDocument::add(const KURL& url, const TQString& desktopEntryName)
|
||
15 years ago
|
{
|
||
12 years ago
|
if ( url.isLocalFile() && !TDEGlobal::dirs()->relativeLocation("tmp", url.path()).startsWith("/"))
|
||
15 years ago
|
return;
|
||
|
|
||
15 years ago
|
TQString openStr = url.url();
|
||
14 years ago
|
openStr.replace( TQRegExp("\\$"), "$$" ); // Desktop files with type "Link" are $-variable expanded
|
||
15 years ago
|
|
||
|
kdDebug(250) << "KRecentDocument::add for " << openStr << endl;
|
||
12 years ago
|
TDEConfig *config = TDEGlobal::config();
|
||
15 years ago
|
TQString oldGrp = config->group();
|
||
13 years ago
|
config->setGroup(TQString::fromLatin1("RecentDocuments"));
|
||
|
bool useRecent = config->readBoolEntry(TQString::fromLatin1("UseRecent"), true);
|
||
|
int maxEntries = config->readNumEntry(TQString::fromLatin1("MaxEntries"), 10);
|
||
15 years ago
|
|
||
|
config->setGroup(oldGrp);
|
||
|
if(!useRecent)
|
||
|
return;
|
||
|
|
||
15 years ago
|
TQString path = recentDocumentDirectory();
|
||
15 years ago
|
|
||
15 years ago
|
TQString dStr = path + url.fileName();
|
||
15 years ago
|
|
||
13 years ago
|
TQString ddesktop = dStr + TQString::fromLatin1(".desktop");
|
||
15 years ago
|
|
||
|
int i=1;
|
||
|
// check for duplicates
|
||
15 years ago
|
while(TQFile::exists(ddesktop)){
|
||
15 years ago
|
// see if it points to the same file and application
|
||
|
KSimpleConfig tmp(ddesktop);
|
||
|
tmp.setDesktopGroup();
|
||
13 years ago
|
if(tmp.readEntry(TQString::fromLatin1("X-TDE-LastOpenedWith"))
|
||
15 years ago
|
== desktopEntryName)
|
||
|
{
|
||
15 years ago
|
utime(TQFile::encodeName(ddesktop), NULL);
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
// if not append a (num) to it
|
||
|
++i;
|
||
|
if ( i > maxEntries )
|
||
|
break;
|
||
13 years ago
|
ddesktop = dStr + TQString::fromLatin1("[%1].desktop").arg(i);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQDir dir(path);
|
||
15 years ago
|
// check for max entries, delete oldest files if exceeded
|
||
15 years ago
|
TQStringList list = dir.entryList(TQDir::Files | TQDir::Hidden, TQDir::Time | TQDir::Reversed);
|
||
15 years ago
|
i = list.count();
|
||
|
if(i > maxEntries-1){
|
||
15 years ago
|
TQStringList::Iterator it;
|
||
15 years ago
|
it = list.begin();
|
||
|
while(i > maxEntries-1){
|
||
13 years ago
|
TQFile::remove(dir.absPath() + TQString::fromLatin1("/") + (*it));
|
||
15 years ago
|
--i, ++it;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// create the applnk
|
||
|
KSimpleConfig conf(ddesktop);
|
||
|
conf.setDesktopGroup();
|
||
13 years ago
|
conf.writeEntry( TQString::fromLatin1("Type"), TQString::fromLatin1("Link") );
|
||
|
conf.writePathEntry( TQString::fromLatin1("URL"), openStr );
|
||
15 years ago
|
// If you change the line below, change the test in the above loop
|
||
13 years ago
|
conf.writeEntry( TQString::fromLatin1("X-TDE-LastOpenedWith"), desktopEntryName );
|
||
15 years ago
|
TQString name = url.fileName();
|
||
15 years ago
|
if (name.isEmpty())
|
||
|
name = openStr;
|
||
13 years ago
|
conf.writeEntry( TQString::fromLatin1("Name"), name );
|
||
|
conf.writeEntry( TQString::fromLatin1("Icon"), KMimeType::iconForURL( url ) );
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void KRecentDocument::add(const TQString &openStr, bool isUrl)
|
||
15 years ago
|
{
|
||
|
if( isUrl ) {
|
||
|
add( KURL( openStr ) );
|
||
|
} else {
|
||
|
KURL url;
|
||
|
url.setPath( openStr );
|
||
|
add( url );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KRecentDocument::clear()
|
||
|
{
|
||
15 years ago
|
TQStringList list = recentDocuments();
|
||
|
TQDir dir;
|
||
|
for(TQStringList::Iterator it = list.begin(); it != list.end() ; ++it)
|
||
15 years ago
|
dir.remove(*it);
|
||
|
}
|
||
|
|
||
|
int KRecentDocument::maximumItems()
|
||
|
{
|
||
12 years ago
|
TDEConfig *config = TDEGlobal::config();
|
||
|
TDEConfigGroupSaver sa(config, TQString::fromLatin1("RecentDocuments"));
|
||
13 years ago
|
return config->readNumEntry(TQString::fromLatin1("MaxEntries"), 10);
|
||
15 years ago
|
}
|
||
|
|
||
|
|