lib Library API Documentation

koPictureKey.cc

00001 /* This file is part of the KDE project
00002    Copyright (c) 2001 Simon Hausmann <hausmann@kde.org>
00003    Copyright 2002 Nicolas GOUTTE <goutte@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <qdatetime.h>
00022 #include <qfileinfo.h>
00023 #include <qdom.h>
00024 
00025 #include <kdebug.h>
00026 
00027 #include "koPictureKey.h"
00028 
00029 // NOTE: we use the *nix epoch (1970-01-01) as a time base because it is a valid date.
00030 // That way we do not depend on a behaviour of the current QDateTime that might change in future versions of QT
00031 // and we are also nice to non-QT programs wanting to read KOffice's files.
00032 //
00033 // NOTE 2: this behaviour is also needed for re-saving KWord files having <FORMAT id="2">. When saving again,
00034 // these files get a <KEY> element as child of <PIXMAPS> but not one as child of <FORMAT> and <IMAGE>.
00035 // Therefore we need to be careful that the key remains compatible to default values (another good reason for the first note)
00036 //
00037 // Notes added by Nicolas GOUTTE <goutte@kde.org>
00038 
00039 
00040 
00041 static void resetDateTimeToEpoch(QDateTime& dt)
00042 {
00043     // set the time point to 1970-01-01
00044     dt.setDate(QDate(1970,1,1));
00045     dt.setTime(QTime(0,0));
00046     // Note: we cannot use QDateTime;;setTime_t as it makes a local time correction!
00047 }
00048 
00049 KoPictureKey::KoPictureKey()
00050 {
00051     resetDateTimeToEpoch(m_lastModified);
00052 }
00053 
00054 KoPictureKey::KoPictureKey( const QString &fn, const QDateTime &mod )
00055     : m_filename( fn ), m_lastModified( mod )
00056 {
00057     if (!m_lastModified.isValid())
00058     {
00059         // As we have an invalid date, set the time point to 1970-01-01
00060         resetDateTimeToEpoch(m_lastModified);
00061     }
00062 }
00063 
00064 KoPictureKey::KoPictureKey( const QString &fn )
00065     : m_filename( fn )
00066 {
00067     resetDateTimeToEpoch(m_lastModified);
00068 }
00069 
00070 KoPictureKey::KoPictureKey( const KoPictureKey &key )
00071     : m_filename( key.m_filename ), m_lastModified( key.m_lastModified )
00072 {
00073 }
00074 
00075 KoPictureKey& KoPictureKey::operator=( const KoPictureKey &key )
00076 {
00077     m_filename = key.m_filename;
00078     m_lastModified = key.m_lastModified;
00079     return *this;
00080 }
00081 
00082 bool KoPictureKey::operator==( const KoPictureKey &key ) const
00083 {
00084     return ( key.m_filename == m_filename &&
00085              key.m_lastModified == m_lastModified );
00086 }
00087 
00088 bool KoPictureKey::operator<( const KoPictureKey &key ) const
00089 {
00090     return key.toString() < toString();
00091 }
00092 
00093 void KoPictureKey::saveAttributes( QDomElement &elem ) const
00094 {
00095     QDate date = m_lastModified.date();
00096     QTime time = m_lastModified.time();
00097     elem.setAttribute( "filename", m_filename );
00098     elem.setAttribute( "year", date.year() );
00099     elem.setAttribute( "month", date.month() );
00100     elem.setAttribute( "day", date.day() );
00101     elem.setAttribute( "hour", time.hour() );
00102     elem.setAttribute( "minute", time.minute() );
00103     elem.setAttribute( "second", time.second() );
00104     elem.setAttribute( "msec", time.msec() );
00105 }
00106 
00107 void KoPictureKey::loadAttributes( const QDomElement &elem )
00108 {
00109     // Default date/time is the *nix epoch: 1970-01-01 00:00:00,000
00110     int year=1970, month=1, day=1;
00111     int hour=0, minute=0, second=0, msec=0; // We must initialize to zero, as not all compilers are C99-compliant
00112 
00113     if( elem.hasAttribute( "key" ) )
00114     {
00115          // Note: the old KWord format (up to 1.1-beta2) has no date/time
00116         m_filename=elem.attribute( "key" );
00117     }
00118     else
00119     {   
00120         // ### TODO: document which format is this?
00121         m_filename=elem.attribute( "filename" );
00122     }
00123 
00124     if( elem.hasAttribute( "year" ) )
00125         year=elem.attribute( "year" ).toInt();
00126     if( elem.hasAttribute( "month" ) )
00127         month=elem.attribute( "month" ).toInt();
00128     if( elem.hasAttribute( "day" ) )
00129         day=elem.attribute( "day" ).toInt();
00130     if( elem.hasAttribute( "hour" ) )
00131         hour=elem.attribute( "hour" ).toInt();
00132     if( elem.hasAttribute( "minute" ) )
00133         minute=elem.attribute( "minute" ).toInt();
00134     if( elem.hasAttribute( "second" ) )
00135         second=elem.attribute( "second" ).toInt();
00136     if( elem.hasAttribute( "msec" ) )
00137         msec=elem.attribute( "msec" ).toInt();
00138 
00139     m_lastModified.setDate( QDate( year, month, day ) );
00140     m_lastModified.setTime( QTime( hour, minute, second, msec ) );
00141 
00142     if (!m_lastModified.isValid())
00143     {
00144         // If the date/time is not valid, make it valid by force!
00145         kdWarning(30003) << "Correcting invalid date/time: " << toString()  << " (in KoPictureKey::loadAttributes)" << endl;
00146         resetDateTimeToEpoch(m_lastModified);
00147     }
00148 }
00149 
00150 QString KoPictureKey::toString() const
00151 {
00152     // m_filename must be the last argument as it can contain a sequence starting with %
00153     // We do not use the default QDateTime::toString has it does not show microseconds
00154     return QString::fromLatin1("%2 %1")
00155         .arg(m_lastModified.toString("yyyy-MM-dd hh:mm:ss.zzz")).arg(m_filename);
00156 }
00157 
00158 void KoPictureKey::setKeyFromFile (const QString& filename)
00159 {
00160     QFileInfo inf(filename);
00161     m_filename = filename;
00162     m_lastModified = inf.lastModified();
00163 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Mar 11 11:47:42 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003