summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp
Side-by-side diff
Diffstat (limited to 'libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp171
1 files changed, 88 insertions, 83 deletions
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp b/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp
index c5a7820..661cd51 100644
--- a/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp
+++ b/libopie2/opiepim/backend/ocontactaccessbackend_xml.cpp
@@ -17,6 +17,11 @@
* =====================================================================
* History:
* $Log$
+ * Revision 1.5 2003/04/13 18:07:10 zecke
+ * More API doc
+ * QString -> const QString&
+ * QString = 0l -> QString::null
+ *
* Revision 1.4 2003/03/21 14:32:54 mickeyl
* g++ compliance fix: default arguments belong into the declaration, but not the definition
*
@@ -93,7 +98,7 @@
#include <qregexp.h>
#include <qarray.h>
#include <qmap.h>
-#include <qdatetime.h>
+#include <qdatetime.h>
#include <qpe/global.h>
@@ -107,7 +112,7 @@
using namespace Opie;
-OContactAccessBackend_XML::OContactAccessBackend_XML ( QString appname, QString filename ):
+OContactAccessBackend_XML::OContactAccessBackend_XML ( const QString& appname, const QString& filename ):
m_changed( false )
{
// Just m_contactlist should call delete if an entry
@@ -116,32 +121,32 @@ OContactAccessBackend_XML::OContactAccessBackend_XML ( QString appname, QString
m_uidToContact.setAutoDelete( false );
m_appName = appname;
-
+
/* Set journalfile name ... */
m_journalName = getenv("HOME");
m_journalName +="/.abjournal" + appname;
-
+
/* Expecting to access the default filename if nothing else is set */
if ( filename.isEmpty() ){
m_fileName = Global::applicationFileName( "addressbook","addressbook.xml" );
} else
m_fileName = filename;
-
+
/* Load Database now */
load ();
}
-bool OContactAccessBackend_XML::save()
+bool OContactAccessBackend_XML::save()
{
-
+
if ( !m_changed )
return true;
-
+
QString strNewFile = m_fileName + ".new";
QFile f( strNewFile );
if ( !f.open( IO_WriteOnly|IO_Raw ) )
return false;
-
+
int total_written;
int idx_offset = 0;
QString out;
@@ -174,7 +179,7 @@ bool OContactAccessBackend_XML::save()
out = "";
}
out += " </Contacts>\n</AddressBook>\n";
-
+
// Write Footer
cstr = out.utf8();
total_written = f.writeBlock( cstr.data(), cstr.length() );
@@ -184,7 +189,7 @@ bool OContactAccessBackend_XML::save()
return false;
}
f.close();
-
+
// move the file over, I'm just going to use the system call
// because, I don't feel like using QDir.
if ( ::rename( strNewFile.latin1(), m_fileName.latin1() ) < 0 ) {
@@ -193,19 +198,19 @@ bool OContactAccessBackend_XML::save()
// remove the tmp file...
QFile::remove( strNewFile );
}
-
+
/* The journalfile should be removed now... */
removeJournal();
-
+
m_changed = false;
return true;
}
-bool OContactAccessBackend_XML::load ()
+bool OContactAccessBackend_XML::load ()
{
m_contactList.clear();
m_uidToContact.clear();
-
+
/* Load XML-File and journal if it exists */
if ( !load ( m_fileName, false ) )
return false;
@@ -216,11 +221,11 @@ bool OContactAccessBackend_XML::load ()
* crash happened...
*/
load (m_journalName, true);
-
+
return true;
}
-void OContactAccessBackend_XML::clear ()
+void OContactAccessBackend_XML::clear ()
{
m_contactList.clear();
m_uidToContact.clear();
@@ -231,46 +236,46 @@ void OContactAccessBackend_XML::clear ()
bool OContactAccessBackend_XML::wasChangedExternally()
{
QFileInfo fi( m_fileName );
-
+
QDateTime lastmod = fi.lastModified ();
-
+
return (lastmod != m_readtime);
}
-QArray<int> OContactAccessBackend_XML::allRecords() const
+QArray<int> OContactAccessBackend_XML::allRecords() const
{
QArray<int> uid_list( m_contactList.count() );
-
- uint counter = 0;
+
+ uint counter = 0;
QListIterator<OContact> it( m_contactList );
for( ; it.current(); ++it ){
uid_list[counter++] = (*it)->uid();
}
-
+
return ( uid_list );
}
OContact OContactAccessBackend_XML::find ( int uid ) const
{
OContact foundContact; //Create empty contact
-
+
OContact* found = m_uidToContact.find( QString().setNum( uid ) );
if ( found ){
foundContact = *found;
}
-
+
return ( foundContact );
}
-QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, int settings,
+QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, int settings,
const QDateTime& d )
{
-
+
QArray<int> m_currentQuery( m_contactList.count() );
QListIterator<OContact> it( m_contactList );
uint arraycounter = 0;
-
+
for( ; it.current(); ++it ){
/* Search all fields and compare them with query object. Store them into list
* if all fields matches.
@@ -290,26 +295,26 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
queryDate = new QDate( query.anniversary() );
checkDate = new QDate( (*it)->anniversary() );
}
-
+
if ( queryDate->isValid() ){
if( checkDate->isValid() ){
if ( settings & OContactAccess::DateYear ){
if ( queryDate->year() != checkDate->year() )
allcorrect = false;
- }
+ }
if ( settings & OContactAccess::DateMonth ){
if ( queryDate->month() != checkDate->month() )
allcorrect = false;
- }
+ }
if ( settings & OContactAccess::DateDay ){
if ( queryDate->day() != checkDate->day() )
allcorrect = false;
- }
+ }
if ( settings & OContactAccess::DateDiff ) {
QDate current;
// If we get an additional date, we
// will take this date instead of
- // the current one..
+ // the current one..
if ( !d.date().isValid() )
current = QDate::currentDate();
else
@@ -317,23 +322,23 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
// We have to equalize the year, otherwise
// the search will fail..
- checkDate->setYMD( current.year(),
- checkDate->month(),
+ checkDate->setYMD( current.year(),
+ checkDate->month(),
checkDate->day() );
if ( *checkDate < current )
- checkDate->setYMD( current.year()+1,
- checkDate->month(),
+ checkDate->setYMD( current.year()+1,
+ checkDate->month(),
checkDate->day() );
// Check whether the birthday/anniversary date is between
// the current/given date and the maximum date
// ( maximum time range ) !
- qWarning("Checking if %s is between %s and %s ! ",
+ qWarning("Checking if %s is between %s and %s ! ",
checkDate->toString().latin1(),
- current.toString().latin1(),
+ current.toString().latin1(),
queryDate->toString().latin1() );
if ( current.daysTo( *queryDate ) >= 0 ){
- if ( !( ( *checkDate >= current ) &&
+ if ( !( ( *checkDate >= current ) &&
( *checkDate <= *queryDate ) ) ){
allcorrect = false;
qWarning (" Nope!..");
@@ -345,7 +350,7 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
allcorrect = false;
}
}
-
+
delete queryDate;
queryDate = 0l;
delete checkDate;
@@ -359,9 +364,9 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
| OContactAccess::DateYear
| OContactAccess::DateMonth
| OContactAccess::DateDay
- | OContactAccess::MatchOne
+ | OContactAccess::MatchOne
) ){
-
+
case OContactAccess::RegExp:{
QRegExp expr ( query.field(i),
!(settings & OContactAccess::IgnoreCase),
@@ -397,10 +402,10 @@ QArray<int> OContactAccessBackend_XML::queryByExample ( const OContact &query, i
m_currentQuery[arraycounter++] = (*it)->uid();
}
}
-
+
// Shrink to fit..
m_currentQuery.resize(arraycounter);
-
+
return m_currentQuery;
}
@@ -409,16 +414,16 @@ QArray<int> OContactAccessBackend_XML::matchRegexp( const QRegExp &r ) const
QArray<int> m_currentQuery( m_contactList.count() );
QListIterator<OContact> it( m_contactList );
uint arraycounter = 0;
-
+
for( ; it.current(); ++it ){
if ( (*it)->match( r ) ){
m_currentQuery[arraycounter++] = (*it)->uid();
}
-
+
}
// Shrink to fit..
m_currentQuery.resize(arraycounter);
-
+
return m_currentQuery;
}
@@ -427,7 +432,7 @@ const uint OContactAccessBackend_XML::querySettings()
return ( OContactAccess::WildCards
| OContactAccess::IgnoreCase
| OContactAccess::RegExp
- | OContactAccess::ExactMatch
+ | OContactAccess::ExactMatch
| OContactAccess::DateDiff
| OContactAccess::DateYear
| OContactAccess::DateMonth
@@ -437,20 +442,20 @@ const uint OContactAccessBackend_XML::querySettings()
bool OContactAccessBackend_XML::hasQuerySettings (uint querySettings) const
{
- /* OContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay
+ /* OContactAccess::IgnoreCase, DateDiff, DateYear, DateMonth, DateDay
* may be added with any of the other settings. IgnoreCase should never used alone.
* Wildcards, RegExp, ExactMatch should never used at the same time...
*/
-
- if ( querySettings == OContactAccess::IgnoreCase )
+
+ if ( querySettings == OContactAccess::IgnoreCase )
return false;
-
+
switch ( querySettings & ~( OContactAccess::IgnoreCase
| OContactAccess::DateDiff
| OContactAccess::DateYear
| OContactAccess::DateMonth
| OContactAccess::DateDay
- )
+ )
){
case OContactAccess::RegExp:
return ( true );
@@ -463,14 +468,14 @@ bool OContactAccessBackend_XML::hasQuerySettings (uint querySettings) const
}
}
-// Currently only asc implemented..
-QArray<int> OContactAccessBackend_XML::sorted( bool asc, int , int , int )
+// Currently only asc implemented..
+QArray<int> OContactAccessBackend_XML::sorted( bool asc, int , int , int )
{
QMap<QString, int> nameToUid;
QStringList names;
QArray<int> m_currentQuery( m_contactList.count() );
-
- // First fill map and StringList with all Names
+
+ // First fill map and StringList with all Names
// Afterwards sort namelist and use map to fill array to return..
QListIterator<OContact> it( m_contactList );
for( ; it.current(); ++it ){
@@ -478,7 +483,7 @@ QArray<int> OContactAccessBackend_XML::sorted( bool asc, int , int , int )
nameToUid.insert( (*it)->fileAs() + QString::number( (*it)->uid() ), (*it)->uid() );
}
names.sort();
-
+
int i = 0;
if ( asc ){
for ( QStringList::Iterator it = names.begin(); it != names.end(); ++it )
@@ -487,9 +492,9 @@ QArray<int> OContactAccessBackend_XML::sorted( bool asc, int , int , int )
for ( QStringList::Iterator it = names.end(); it != names.begin(); --it )
m_currentQuery[i++] = nameToUid[ (*it) ];
}
-
+
return m_currentQuery;
-
+
}
bool OContactAccessBackend_XML::add ( const OContact &newcontact )
@@ -497,16 +502,16 @@ bool OContactAccessBackend_XML::add ( const OContact &newcontact )
//qWarning("odefaultbackend: ACTION::ADD");
updateJournal (newcontact, ACTION_ADD);
addContact_p( newcontact );
-
+
m_changed = true;
-
+
return true;
}
bool OContactAccessBackend_XML::replace ( const OContact &contact )
{
m_changed = true;
-
+
OContact* found = m_uidToContact.find ( QString().setNum( contact.uid() ) );
if ( found ) {
@@ -528,7 +533,7 @@ bool OContactAccessBackend_XML::replace ( const OContact &contact )
bool OContactAccessBackend_XML::remove ( int uid )
{
m_changed = true;
-
+
OContact* found = m_uidToContact.find ( QString().setNum( uid ) );
if ( found ) {
@@ -555,9 +560,9 @@ void OContactAccessBackend_XML::addContact_p( const OContact &newcontact )
}
/* This function loads the xml-database and the journalfile */
-bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
+bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
{
-
+
/* We use the time of the last read to check if the file was
* changed externally.
*/
@@ -565,10 +570,10 @@ bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
QFileInfo fi( filename );
m_readtime = fi.lastModified ();
}
-
+
const int JOURNALACTION = Qtopia::Notes + 1;
const int JOURNALROW = JOURNALACTION + 1;
-
+
bool foundAction = false;
journal_action action = ACTION_ADD;
int journalKey = 0;
@@ -576,7 +581,7 @@ bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
QMap<QString, QString> customMap;
QMap<QString, QString>::Iterator customIt;
QAsciiDict<int> dict( 47 );
-
+
dict.setAutoDelete( TRUE );
dict.insert( "Uid", new int(Qtopia::AddressUid) );
dict.insert( "Title", new int(Qtopia::Title) );
@@ -623,9 +628,9 @@ bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
dict.insert( "Notes", new int(Qtopia::Notes) );
dict.insert( "action", new int(JOURNALACTION) );
dict.insert( "actionrow", new int(JOURNALROW) );
-
+
//qWarning( "OContactDefaultBackEnd::loading %s", filename.latin1() );
-
+
XMLElement *root = XMLElement::load( filename );
if(root != 0l ){ // start parsing
/* Parse all XML-Elements and put the data into the
@@ -634,7 +639,7 @@ bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
XMLElement *element = root->firstChild();
//qWarning("OContactAccess::load tagName(): %s", root->tagName().latin1() );
element = element->firstChild();
-
+
/* Search Tag "Contacts" which is the parent of all Contacts */
while( element && !isJournal ){
if( element->tagName() != QString::fromLatin1("Contacts") ){
@@ -661,14 +666,14 @@ bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
// element->tagName().latin1() );
QString dummy;
foundAction = false;
-
+
XMLElement::AttributeMap aMap = element->attributes();
XMLElement::AttributeMap::Iterator it;
contactMap.clear();
customMap.clear();
for( it = aMap.begin(); it != aMap.end(); ++it ){
// qWarning ("Read Attribute: %s=%s", it.key().latin1(),it.data().latin1());
-
+
int *find = dict[ it.key() ];
/* Unknown attributes will be stored as "Custom" elements */
if ( !find ) {
@@ -677,7 +682,7 @@ bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
customMap.insert( it.key(), it.data() );
continue;
}
-
+
/* Check if special conversion is needed and add attribute
* into Contact class
*/
@@ -705,11 +710,11 @@ bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
}
/* now generate the Contact contact */
OContact contact( contactMap );
-
+
for (customIt = customMap.begin(); customIt != customMap.end(); ++customIt ) {
contact.setCustomField( customIt.key(), customIt.data() );
}
-
+
if (foundAction){
foundAction = false;
switch ( action ) {
@@ -734,7 +739,7 @@ bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
/* Add contact to list */
addContact_p (contact);
}
-
+
/* Move to next element */
element = element->nextChild();
}
@@ -748,16 +753,16 @@ bool OContactAccessBackend_XML::load( const QString filename, bool isJournal )
void OContactAccessBackend_XML::updateJournal( const OContact& cnt,
- journal_action action )
+ journal_action action )
{
QFile f( m_journalName );
bool created = !f.exists();
if ( !f.open(IO_WriteOnly|IO_Append) )
return;
-
+
QString buf;
QCString str;
-
+
// if the file was created, we have to set the Tag "<CONTACTS>" to
// get a XML-File which is readable by our parser.
// This is just a cheat, but better than rewrite the parser.
@@ -766,7 +771,7 @@ void OContactAccessBackend_XML::updateJournal( const OContact& cnt,
QCString cstr = buf.utf8();
f.writeBlock( cstr.data(), cstr.length() );
}
-
+
buf = "<Contact ";
cnt.save( buf );
buf += " action=\"" + QString::number( (int)action ) + "\" ";