File indexing completed on 2024-05-05 16:38:59

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003,2009 Carsten Pfeiffer <pfeiffer@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, version 2.
0007 
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011     General Public License for more details.
0012 
0013    You should have received a copy of the GNU General Public License
0014    along with this program; see the file COPYING.  If not, write to
0015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016    Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include "filecache.h"
0020 
0021 #include <QCoreApplication>
0022 #include <QDir>
0023 #include <QString>
0024 #include <QTemporaryDir>
0025 #include <QTemporaryFile>
0026 #include <QUrl>
0027 
0028 #include "kuickshow_debug.h"
0029 #include "kuickfile.h"
0030 
0031 
0032 FileCache* FileCache::s_self = nullptr;
0033 
0034 FileCache::FileCache()
0035     : m_limit( 0 ),
0036       m_tempDir( 0L )
0037 {
0038     m_files.setMaxCost( 100 ); // good default? ### make configurable?
0039 }
0040 
0041 FileCache::~FileCache()
0042 {
0043     delete m_tempDir;
0044 }
0045 
0046 void FileCache::shutdown()
0047 {
0048     if ( s_self )
0049     {
0050         delete s_self;
0051         s_self = 0L;
0052     }
0053 }
0054 
0055 FileCache * FileCache::self()
0056 {
0057     if ( !s_self )
0058         s_self = new FileCache();
0059     return s_self;
0060 }
0061 
0062 KuickFile * FileCache::getFile( const QUrl& url )
0063 {
0064     QString urlString = url.toDisplayString();
0065     KuickFile *file = m_files.object( urlString );
0066     if ( !file ) {
0067         file = new KuickFile( url );
0068         m_files.insert( urlString, file );
0069     }
0070 
0071     return file;
0072 }
0073 
0074 QString FileCache::tempDir()
0075 {
0076     if ( !m_tempDir ) {
0077         m_tempDir = createTempDir();
0078 
0079         if ( !m_tempDir ) {
0080             qWarning("Unable to create temporary directory for KuickShow");
0081             return QString();
0082         }
0083     }
0084 
0085     return m_tempDir->path() + QLatin1Char('/');
0086 }
0087 
0088 QTemporaryFile* FileCache::createTempFile(const QString& suffix, const QString& prefix)
0089 {
0090     QString nameTemplate = tempDir();
0091     if(nameTemplate.isEmpty()) return nullptr;
0092 
0093     nameTemplate += prefix + QStringLiteral("XXXXXX") + suffix;
0094     return new QTemporaryFile(nameTemplate);
0095 }
0096 
0097 
0098 QTemporaryDir* FileCache::createTempDir()
0099 {
0100     QString nameTemplate = QStringLiteral("%1/%2_%3_XXXXXX")
0101             .arg(QDir::tempPath())
0102             .arg(QCoreApplication::applicationName())
0103             .arg(QCoreApplication::applicationPid());
0104     QTemporaryDir* dir = new QTemporaryDir(nameTemplate);
0105 
0106     if(!dir->isValid()) {
0107         delete dir;
0108         return 0L;
0109     }
0110 
0111     return dir;
0112 }