F:/KPlato/koffice/libs/kofficecore/KoPictureKey.cpp

Aller à la documentation de ce fichier.
00001 
00002 /* This file is part of the KDE project
00003    Copyright (c) 2001 Simon Hausmann <hausmann@kde.org>
00004    Copyright 2002 Nicolas GOUTTE <goutte@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
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     // set the time point to 1970-01-01
00033     dt.setDate(QDate(1970,1,1));
00034     dt.setTime(QTime(0,0));
00035     // Note: we cannot use QDateTime;;setTime_t as it makes a local time correction! (### TODO: not true anymore with recent Qt versions)
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         // As we have an invalid date, set the time point to 1970-01-01
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     // Default date/time is the *nix epoch: 1970-01-01 00:00:00,000
00099     int year=1970, month=1, day=1;
00100     int hour=0, minute=0, second=0, msec=0; // We must initialize to zero, as not all compilers are C99-compliant
00101 
00102     if( elem.hasAttribute( "key" ) )
00103     {
00104          // Note: the old KWord format (up to 1.1-beta2) has no date/time
00105         m_filename=elem.attribute( "key" );
00106     }
00107     else
00108     {   
00109         // ### TODO: document which format is this?
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         // If the date/time is not valid, make it valid by force!
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     // We do not use the default QDateTime::toString has it does not show microseconds
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 }

Généré le Wed Nov 22 23:41:03 2006 pour KPlato par  doxygen 1.5.1-p1