summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_dirthumbs.cpp
blob: 03e73bc43f6f15ce84899b0be2d47d66b3749cc4 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/***************************************************************************
                          sq_dirthumbs.cpp  -  description
                             -------------------
    begin                : ??? Jul 18 2007
    copyright            : (C) 2007 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_UTIME_H
#include <sys/types.h>
#include <utime.h>
#endif

#include <tqimage.h>
#include <tqfileinfo.h>
#include <tqdatetime.h>

#include <kurl.h>
#include <kmdcodec.h>

#include "sq_dirthumbs.h"
#include "sq_thumbnailinfo.h"

SQ_DirThumbs::SQ_DirThumbs() : SQ_Dir()
{
    setRoot();
}

SQ_DirThumbs::~SQ_DirThumbs()
{}

/*
 *  Save thumbnail to storage.
 */
void SQ_DirThumbs::saveThumbnail(const KURL &url, SQ_Thumbnail &thumb)
{
    TQString ab = absPath(url);

    if(!thumb.thumbnail.isNull() && needUpdate(ab, thumb.originalTime))
    {
        // thumbnail standard tags
        thumb.thumbnail.setText("Thumb::Image::Width", 0, TQString::number(thumb.w));
        thumb.thumbnail.setText("Thumb::Image::Height", 0, TQString::number(thumb.h));
        thumb.thumbnail.setText("Thumb::URI", 0, url.prettyURL());
        thumb.thumbnail.setText("Thumb::MTime", 0, TQString::number(thumb.originalTime));
        thumb.thumbnail.setText("Software", 0, "KSquirrel");

        thumb.thumbnail.save(ab, sqdirThumbFormat, sqdirThumbQuality);

#ifdef HAVE_UTIME_H
        struct utimbuf ut;
        ut.actime = thumb.originalTime;
        ut.modtime = thumb.originalTime;
        utime((const char *)ab, &ut);
#endif
    }
}

TQString SQ_DirThumbs::absPath(const KURL &relurl)
{
    KMD5 md5(TQString(TQFile::encodeName(relurl.prettyURL())));

    TQString ext = TQString::fromLatin1(".%1").arg(sqdirThumbFormat);

    return m_orig + TQDir::separator() + TQString(md5.hexDigest()) + ext.lower();
}

void SQ_DirThumbs::setRoot()
{
    m_orig = TQDir::cleanDirPath(homeDirPath() + TQDir::separator() + TQString::fromLatin1(".thumbnails"));
    TQDir::mkdir(m_orig);

    m_orig = TQDir::cleanDirPath(m_orig + TQDir::separator() + TQString::fromLatin1("normal"));
    TQDir::mkdir(m_orig);
}

void SQ_DirThumbs::removeFile(const KURL &url)
{
    // determine absolute path and remove file
    TQFile::remove(absPath(url));
}

bool SQ_DirThumbs::needUpdate(const TQString &turl, time_t tm)
{
    TQFileInfo fthumbpath(turl);

    TQDateTime dt_orig;
    dt_orig.setTime_t(tm);

    return (dt_orig > fthumbpath.lastModified());
}