KoPictureKey.cpp
1
2/* This file is part of the KDE project
3 Copyright (c) 2001 Simon Hausmann <hausmann@kde.org>
4 Copyright 2002 Nicolas GOUTTE <goutte@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include <tqdatetime.h>
23#include <tqfileinfo.h>
24#include <tqdom.h>
25
26#include <kdebug.h>
27
28#include "KoPictureKey.h"
29
30static void resetDateTimeToEpoch(TQDateTime& dt)
31{
32 // set the time point to 1970-01-01
33 dt.setDate(TQDate(1970,1,1));
34 dt.setTime(TQTime(0,0));
35 // Note: we cannot use TQDateTime;;setTime_t as it makes a local time correction! (### TODO: not true anymore with recent TQt versions)
36}
37
39{
40 resetDateTimeToEpoch(m_lastModified);
41}
42
43KoPictureKey::KoPictureKey( const TQString &fn, const TQDateTime &mod )
44 : m_filename( fn ), m_lastModified( mod )
45{
46 if (!m_lastModified.isValid())
47 {
48 // As we have an invalid date, set the time point to 1970-01-01
49 resetDateTimeToEpoch(m_lastModified);
50 }
51}
52
53KoPictureKey::KoPictureKey( const TQString &fn )
54 : m_filename( fn )
55{
56 resetDateTimeToEpoch(m_lastModified);
57}
58
60 : m_filename( key.m_filename ), m_lastModified( key.m_lastModified )
61{
62}
63
65{
66 m_filename = key.m_filename;
67 m_lastModified = key.m_lastModified;
68 return *this;
69}
70
71bool KoPictureKey::operator==( const KoPictureKey &key ) const
72{
73 return ( key.m_filename == m_filename &&
74 key.m_lastModified == m_lastModified );
75}
76
77bool KoPictureKey::operator<( const KoPictureKey &key ) const
78{
79 return key.toString() < toString();
80}
81
82void KoPictureKey::saveAttributes( TQDomElement &elem ) const
83{
84 TQDate date = m_lastModified.date();
85 TQTime time = m_lastModified.time();
86 elem.setAttribute( "filename", m_filename );
87 elem.setAttribute( "year", date.year() );
88 elem.setAttribute( "month", date.month() );
89 elem.setAttribute( "day", date.day() );
90 elem.setAttribute( "hour", time.hour() );
91 elem.setAttribute( "minute", time.minute() );
92 elem.setAttribute( "second", time.second() );
93 elem.setAttribute( "msec", time.msec() );
94}
95
96void KoPictureKey::loadAttributes( const TQDomElement &elem )
97{
98 // Default date/time is the *nix epoch: 1970-01-01 00:00:00,000
99 int year=1970, month=1, day=1;
100 int hour=0, minute=0, second=0, msec=0; // We must initialize to zero, as not all compilers are C99-compliant
101
102 if( elem.hasAttribute( "key" ) )
103 {
104 // Note: the old KWord format (up to 1.1-beta2) has no date/time
105 m_filename=elem.attribute( "key" );
106 }
107 else
108 {
109 // ### TODO: document which format is this?
110 m_filename=elem.attribute( "filename" );
111 }
112
113 if( elem.hasAttribute( "year" ) )
114 year=elem.attribute( "year" ).toInt();
115 if( elem.hasAttribute( "month" ) )
116 month=elem.attribute( "month" ).toInt();
117 if( elem.hasAttribute( "day" ) )
118 day=elem.attribute( "day" ).toInt();
119 if( elem.hasAttribute( "hour" ) )
120 hour=elem.attribute( "hour" ).toInt();
121 if( elem.hasAttribute( "minute" ) )
122 minute=elem.attribute( "minute" ).toInt();
123 if( elem.hasAttribute( "second" ) )
124 second=elem.attribute( "second" ).toInt();
125 if( elem.hasAttribute( "msec" ) )
126 msec=elem.attribute( "msec" ).toInt();
127
128 m_lastModified.setDate( TQDate( year, month, day ) );
129 m_lastModified.setTime( TQTime( hour, minute, second, msec ) );
130
131 if (!m_lastModified.isValid())
132 {
133 // If the date/time is not valid, make it valid by force!
134 kdWarning(30003) << "Correcting invalid date/time: " << toString() << " (in KoPictureKey::loadAttributes)" << endl;
135 resetDateTimeToEpoch(m_lastModified);
136 }
137}
138
140{
141 // We do not use the default TQDateTime::toString has it does not show microseconds
142 return TQString::fromLatin1( "%1 %2" )
143 .arg( m_filename, m_lastModified.toString("yyyy-MM-dd hh:mm:ss.zzz") );
144}
145
146void KoPictureKey::setKeyFromFile (const TQString& filename)
147{
148 TQFileInfo inf(filename);
149 m_filename = filename;
150 m_lastModified = inf.lastModified();
151}
KoPictureKey is the structure describing a picture in a unique way.
bool operator==(const KoPictureKey &key) const
Comparison operator.
KoPictureKey()
Default constructor.
TQString toString() const
Convert this key into a string representation of it.
void saveAttributes(TQDomElement &elem) const
Save this key in XML (as KOffice 1.3)
void loadAttributes(const TQDomElement &elem)
Load this key from XML (as KOffice 1.3)
bool operator<(const KoPictureKey &key) const
Comparison operator.
TQString filename() const
First part of the key: the filename.
KoPictureKey & operator=(const KoPictureKey &key)
Assignment operator.
void setKeyFromFile(const TQString &filename)
Sets the key according to filename, including modification time.