summaryrefslogtreecommitdiffstats
path: root/kuickshow/src/filecache.cpp
blob: d68bc8979f0e84ed924562ac8b2811458293f40b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <unistd.h>

#include <tqstring.h>

#include <kdebug.h>
#include <kstandarddirs.h>
#include <ktempdir.h>

#include "filecache.h"

FileCache * FileCache::s_self;

FileCache::FileCache()
    : m_limit( 0 ),
      m_tempDir( 0L )
{
    m_files.setAutoDelete( true );
    m_files.setMaxCost( 100 ); // good default? ### make configurable?
}

FileCache::~FileCache()
{
    delete m_tempDir;
}

void FileCache::shutdown()
{
    if ( s_self )
    {
        delete s_self;
        s_self = 0L;
    }
}

FileCache * FileCache::self()
{
    if ( !s_self )
        s_self = new FileCache();
    return s_self;
}

KuickFile * FileCache::getFile( const KURL& url )
{
    TQString urlString = url.prettyURL();
    KuickFile *file = m_files.find( urlString );
    if ( !file ) {
        file = new KuickFile( url );
        m_files.insert( urlString, file );
    }
    
    return file;
}

TQString FileCache::tempDir()
{
    if ( !m_tempDir ) {
        m_tempDir = createTempDir();

        if ( !m_tempDir ) {
            kdWarning() << "Unable to create temporary directory for KuickShow" << endl;
            return TQString();
        }
    }
    
    return m_tempDir->name();
}


KTempDir * FileCache::createTempDir()
{
    TQString tmpName = TQString::tqfromLatin1( KGlobal::instance()->instanceName() );
    tmpName.append( TQString::number( getpid() ) );
    TQString dirName = locateLocal( "tmp", tmpName );
    KTempDir *dir = new KTempDir( dirName );
    dir->setAutoDelete( true );
    if ( dir->status() != 0L )
    {
        delete dir;
        return 0L;
    }

    return dir;
}