00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <QDateTime>
00023 #include <QFileInfo>
00024 #include <qdom.h>
00025
00026 #include <kdebug.h>
00027
00028 #include "KoPictureKey.h"
00029
00030 static void resetDateTimeToEpoch(QDateTime& dt)
00031 {
00032
00033 dt.setDate(QDate(1970,1,1));
00034 dt.setTime(QTime(0,0));
00035
00036 }
00037
00038 KoPictureKey::KoPictureKey()
00039 {
00040 resetDateTimeToEpoch(m_lastModified);
00041 }
00042
00043 KoPictureKey::KoPictureKey( const QString &fn, const QDateTime &mod )
00044 : m_filename( fn ), m_lastModified( mod )
00045 {
00046 if (!m_lastModified.isValid())
00047 {
00048
00049 resetDateTimeToEpoch(m_lastModified);
00050 }
00051 }
00052
00053 KoPictureKey::KoPictureKey( const QString &fn )
00054 : m_filename( fn )
00055 {
00056 resetDateTimeToEpoch(m_lastModified);
00057 }
00058
00059 KoPictureKey::KoPictureKey( const KoPictureKey &key )
00060 : m_filename( key.m_filename ), m_lastModified( key.m_lastModified )
00061 {
00062 }
00063
00064 KoPictureKey& KoPictureKey::operator=( const KoPictureKey &key )
00065 {
00066 m_filename = key.m_filename;
00067 m_lastModified = key.m_lastModified;
00068 return *this;
00069 }
00070
00071 bool KoPictureKey::operator==( const KoPictureKey &key ) const
00072 {
00073 return ( key.m_filename == m_filename &&
00074 key.m_lastModified == m_lastModified );
00075 }
00076
00077 bool KoPictureKey::operator<( const KoPictureKey &key ) const
00078 {
00079 return key.toString() < toString();
00080 }
00081
00082 void KoPictureKey::saveAttributes( QDomElement &elem ) const
00083 {
00084 QDate date = m_lastModified.date();
00085 QTime time = m_lastModified.time();
00086 elem.setAttribute( "filename", m_filename );
00087 elem.setAttribute( "year", date.year() );
00088 elem.setAttribute( "month", date.month() );
00089 elem.setAttribute( "day", date.day() );
00090 elem.setAttribute( "hour", time.hour() );
00091 elem.setAttribute( "minute", time.minute() );
00092 elem.setAttribute( "second", time.second() );
00093 elem.setAttribute( "msec", time.msec() );
00094 }
00095
00096 void KoPictureKey::loadAttributes( const QDomElement &elem )
00097 {
00098
00099 int year=1970, month=1, day=1;
00100 int hour=0, minute=0, second=0, msec=0;
00101
00102 if( elem.hasAttribute( "key" ) )
00103 {
00104
00105 m_filename=elem.attribute( "key" );
00106 }
00107 else
00108 {
00109
00110 m_filename=elem.attribute( "filename" );
00111 }
00112
00113 if( elem.hasAttribute( "year" ) )
00114 year=elem.attribute( "year" ).toInt();
00115 if( elem.hasAttribute( "month" ) )
00116 month=elem.attribute( "month" ).toInt();
00117 if( elem.hasAttribute( "day" ) )
00118 day=elem.attribute( "day" ).toInt();
00119 if( elem.hasAttribute( "hour" ) )
00120 hour=elem.attribute( "hour" ).toInt();
00121 if( elem.hasAttribute( "minute" ) )
00122 minute=elem.attribute( "minute" ).toInt();
00123 if( elem.hasAttribute( "second" ) )
00124 second=elem.attribute( "second" ).toInt();
00125 if( elem.hasAttribute( "msec" ) )
00126 msec=elem.attribute( "msec" ).toInt();
00127
00128 m_lastModified.setDate( QDate( year, month, day ) );
00129 m_lastModified.setTime( QTime( hour, minute, second, msec ) );
00130
00131 if (!m_lastModified.isValid())
00132 {
00133
00134 kWarning(30003) << "Correcting invalid date/time: " << toString() << " (in KoPictureKey::loadAttributes)" << endl;
00135 resetDateTimeToEpoch(m_lastModified);
00136 }
00137 }
00138
00139 QString KoPictureKey::toString() const
00140 {
00141
00142 return QString::fromLatin1( "%1 %2" )
00143 .arg( m_filename, m_lastModified.toString("yyyy-MM-dd hh:mm:ss.zzz") );
00144 }
00145
00146 void KoPictureKey::setKeyFromFile (const QString& filename)
00147 {
00148 QFileInfo inf(filename);
00149 m_filename = filename;
00150 m_lastModified = inf.lastModified();
00151 }