summaryrefslogtreecommitdiffstats
path: root/lib/kofficecore/KoPictureKey.cpp
blob: 0badb429e82fac0e1280a680e265c8d1d5c8b69d (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

/* This file is part of the KDE project
   Copyright (c) 2001 Simon Hausmann <[email protected]>
   Copyright 2002 Nicolas GOUTTE <[email protected]>

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

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

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

#include <kdebug.h>

#include "KoPictureKey.h"

static void resetDateTimeToEpoch(TQDateTime& dt)
{
    // set the time point to 1970-01-01
    dt.setDate(TQDate(1970,1,1));
    dt.setTime(TQTime(0,0));
    // Note: we cannot use TQDateTime;;setTime_t as it makes a local time correction! (### TODO: not true anymore with recent TQt versions)
}

KoPictureKey::KoPictureKey()
{
    resetDateTimeToEpoch(m_lastModified);
}

KoPictureKey::KoPictureKey( const TQString &fn, const TQDateTime &mod )
    : m_filename( fn ), m_lastModified( mod )
{
    if (!m_lastModified.isValid())
    {
        // As we have an invalid date, set the time point to 1970-01-01
        resetDateTimeToEpoch(m_lastModified);
    }
}

KoPictureKey::KoPictureKey( const TQString &fn )
    : m_filename( fn )
{
    resetDateTimeToEpoch(m_lastModified);
}

KoPictureKey::KoPictureKey( const KoPictureKey &key )
    : m_filename( key.m_filename ), m_lastModified( key.m_lastModified )
{
}

KoPictureKey& KoPictureKey::operator=( const KoPictureKey &key )
{
    m_filename = key.m_filename;
    m_lastModified = key.m_lastModified;
    return *this;
}

bool KoPictureKey::operator==( const KoPictureKey &key ) const
{
    return ( key.m_filename == m_filename &&
             key.m_lastModified == m_lastModified );
}

bool KoPictureKey::operator<( const KoPictureKey &key ) const
{
    return key.toString() < toString();
}

void KoPictureKey::saveAttributes( TQDomElement &elem ) const
{
    TQDate date = m_lastModified.date();
    TQTime time = m_lastModified.time();
    elem.setAttribute( "filename", m_filename );
    elem.setAttribute( "year", date.year() );
    elem.setAttribute( "month", date.month() );
    elem.setAttribute( "day", date.day() );
    elem.setAttribute( "hour", time.hour() );
    elem.setAttribute( "minute", time.minute() );
    elem.setAttribute( "second", time.second() );
    elem.setAttribute( "msec", time.msec() );
}

void KoPictureKey::loadAttributes( const TQDomElement &elem )
{
    // Default date/time is the *nix epoch: 1970-01-01 00:00:00,000
    int year=1970, month=1, day=1;
    int hour=0, minute=0, second=0, msec=0; // We must initialize to zero, as not all compilers are C99-compliant

    if( elem.hasAttribute( "key" ) )
    {
         // Note: the old KWord format (up to 1.1-beta2) has no date/time
        m_filename=elem.attribute( "key" );
    }
    else
    {   
        // ### TODO: document which format is this?
        m_filename=elem.attribute( "filename" );
    }

    if( elem.hasAttribute( "year" ) )
        year=elem.attribute( "year" ).toInt();
    if( elem.hasAttribute( "month" ) )
        month=elem.attribute( "month" ).toInt();
    if( elem.hasAttribute( "day" ) )
        day=elem.attribute( "day" ).toInt();
    if( elem.hasAttribute( "hour" ) )
        hour=elem.attribute( "hour" ).toInt();
    if( elem.hasAttribute( "minute" ) )
        minute=elem.attribute( "minute" ).toInt();
    if( elem.hasAttribute( "second" ) )
        second=elem.attribute( "second" ).toInt();
    if( elem.hasAttribute( "msec" ) )
        msec=elem.attribute( "msec" ).toInt();

    m_lastModified.setDate( TQDate( year, month, day ) );
    m_lastModified.setTime( TQTime( hour, minute, second, msec ) );

    if (!m_lastModified.isValid())
    {
        // If the date/time is not valid, make it valid by force!
        kdWarning(30003) << "Correcting invalid date/time: " << toString()  << " (in KoPictureKey::loadAttributes)" << endl;
        resetDateTimeToEpoch(m_lastModified);
    }
}

TQString KoPictureKey::toString() const
{
    // We do not use the default TQDateTime::toString has it does not show microseconds
    return TQString::tqfromLatin1( "%1 %2" )
        .tqarg( m_filename, m_lastModified.toString("yyyy-MM-dd hh:mm:ss.zzz") );
}

void KoPictureKey::setKeyFromFile (const TQString& filename)
{
    TQFileInfo inf(filename);
    m_filename = filename;
    m_lastModified = inf.lastModified();
}