summaryrefslogtreecommitdiff
path: root/libopie2
Unidiff
Diffstat (limited to 'libopie2') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/backend/obackendfactory.h88
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend.h4
-rw-r--r--libopie2/opiepim/backend/ocontactaccessbackend_xml.h10
-rw-r--r--libopie2/opiepim/core/ocontactaccess.cpp10
4 files changed, 110 insertions, 2 deletions
diff --git a/libopie2/opiepim/backend/obackendfactory.h b/libopie2/opiepim/backend/obackendfactory.h
new file mode 100644
index 0000000..599fbf2
--- a/dev/null
+++ b/libopie2/opiepim/backend/obackendfactory.h
@@ -0,0 +1,88 @@
1/*
2 * Class to manage Backends.
3 *
4 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de)
5 *
6 * =====================================================================
7 *This program is free software; you can redistribute it and/or
8 *modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation;
10 * either version 2 of the License, or (at your option) any later
11 * version.
12 * =====================================================================
13 * ToDo: Use plugins
14 * =====================================================================
15 * Version: $Id$
16 * =====================================================================
17 * History:
18 * $Log$
19 * Revision 1.1 2002/10/07 17:35:01 eilers
20 * added OBackendFactory for advanced backend access
21 *
22 *
23 * =====================================================================
24 */
25#ifndef __OPIE_BACKENDFACTORY_H_
26#define __OPIE_BACKENDFACTORY_H_
27
28#include <qstring.h>
29#include <qasciidict.h>
30#include <qpe/config.h>
31
32#include "otodoaccessxml.h"
33#include "ocontactaccessbackend_xml.h"
34#include "otodoaccesssql.h"
35
36
37template<class T>
38class OBackendFactory
39{
40 public:
41 OBackendFactory() {};
42
43 enum BACKENDS {
44 TODO,
45 CONTACT,
46 DATE
47 };
48
49 static T* Default( const QString backendName, const QString& appName ){
50
51
52 Config config( "pimaccess" );
53 config.setGroup ( backendName );
54 QString backend = config.readEntry( "usebackend" );
55
56 QAsciiDict<int> dict ( 3 );
57 dict.setAutoDelete ( TRUE );
58
59 dict.insert( "todo", new int (TODO) );
60 dict.insert( "contact", new int (CONTACT) );
61
62 qWarning ("TODO is: %d", TODO);
63 qWarning ("CONTACT is: %d", CONTACT);
64
65 switch ( *dict.take( backendName ) ){
66 case TODO:
67 if ( backend == "sql" )
68 return (T*) new OTodoAccessBackendSQL("");
69
70 return (T*) new OTodoAccessXML( appName );
71 case CONTACT:
72 if ( backend == "sql" )
73 qWarning ("OBackendFactory:: sql Backend not implemented! Using XML instead!");
74
75 return (T*) new OContactAccessBackend_XML( appName );
76 case DATE:
77 qWarning ("OBackendFactory:: DATE-Backend not implemented!");
78 return NULL;
79 default:
80 return NULL;
81 }
82
83
84 }
85};
86
87
88#endif
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend.h b/libopie2/opiepim/backend/ocontactaccessbackend.h
index 9469bbc..a651477 100644
--- a/libopie2/opiepim/backend/ocontactaccessbackend.h
+++ b/libopie2/opiepim/backend/ocontactaccessbackend.h
@@ -1,70 +1,74 @@
1/** 1/**
2 * The class responsible for managing a backend. 2 * The class responsible for managing a backend.
3 * The implementation of this abstract class contains 3 * The implementation of this abstract class contains
4 * the complete database handling. 4 * the complete database handling.
5 * 5 *
6 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) 6 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de)
7 * Copyright (c) 2002 by Holger Freyther (zecke@handhelds.org) 7 * Copyright (c) 2002 by Holger Freyther (zecke@handhelds.org)
8 * 8 *
9 * ===================================================================== 9 * =====================================================================
10 *This program is free software; you can redistribute it and/or 10 *This program is free software; you can redistribute it and/or
11 *modify it under the terms of the GNU Library General Public 11 *modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; 12 * License as published by the Free Software Foundation;
13 * either version 2 of the License, or (at your option) any later 13 * either version 2 of the License, or (at your option) any later
14 * version. 14 * version.
15 * ===================================================================== 15 * =====================================================================
16 * ToDo: Define enum for query settings 16 * ToDo: Define enum for query settings
17 * ===================================================================== 17 * =====================================================================
18 * Version: $Id$ 18 * Version: $Id$
19 * ===================================================================== 19 * =====================================================================
20 * History: 20 * History:
21 * $Log$ 21 * $Log$
22 * Revision 1.2 2002/10/07 17:34:24 eilers
23 * added OBackendFactory for advanced backend access
24 *
22 * Revision 1.1 2002/09/27 17:11:44 eilers 25 * Revision 1.1 2002/09/27 17:11:44 eilers
23 * Added API for accessing the Contact-Database ! It is compiling, but 26 * Added API for accessing the Contact-Database ! It is compiling, but
24 * please do not expect that anything is working ! 27 * please do not expect that anything is working !
25 * I will debug that stuff in the next time .. 28 * I will debug that stuff in the next time ..
26 * Please read README_COMPILE for compiling ! 29 * Please read README_COMPILE for compiling !
27 * 30 *
28 * ===================================================================== 31 * =====================================================================
29 * 32 *
30 */ 33 */
31 34
32#ifndef _OCONTACTACCESSBACKEND_H_ 35#ifndef _OCONTACTACCESSBACKEND_H_
33#define _OCONTACTACCESSBACKEND_H_ 36#define _OCONTACTACCESSBACKEND_H_
34 37
38#include "ocontact.h"
35#include "opimaccessbackend.h" 39#include "opimaccessbackend.h"
36 40
37class OContactAccessBackend: public OPimAccessBackend<OContact> { 41class OContactAccessBackend: public OPimAccessBackend<OContact> {
38 public: 42 public:
39 OContactAccessBackend() {} 43 OContactAccessBackend() {}
40 virtual ~OContactAccessBackend() {} 44 virtual ~OContactAccessBackend() {}
41 45
42 46
43 /** Return if database was changed externally. 47 /** Return if database was changed externally.
44 * This may just make sense on file based databases like a XML-File. 48 * This may just make sense on file based databases like a XML-File.
45 * It is used to prevent to overwrite the current database content 49 * It is used to prevent to overwrite the current database content
46 * if the file was already changed by something else ! 50 * if the file was already changed by something else !
47 * If this happens, we have to reload before save our data. 51 * If this happens, we have to reload before save our data.
48 * If we use real databases, this should be handled by the database 52 * If we use real databases, this should be handled by the database
49 * management system themselve, therefore this function should always return false in 53 * management system themselve, therefore this function should always return false in
50 * this case. It is not our problem to handle this conflict ... 54 * this case. It is not our problem to handle this conflict ...
51 * @return <i>true</i> if the database was changed and if save without reload will 55 * @return <i>true</i> if the database was changed and if save without reload will
52 * be dangerous. <i>false</i> if the database was not changed or it is save to write 56 * be dangerous. <i>false</i> if the database was not changed or it is save to write
53 * in this situation. 57 * in this situation.
54 */ 58 */
55 virtual bool wasChangedExternally() = 0; 59 virtual bool wasChangedExternally() = 0;
56 60
57 /** Return all possible settings. 61 /** Return all possible settings.
58 * @return All settings provided by the current backend 62 * @return All settings provided by the current backend
59 * (i.e.: query_WildCards & query_IgnoreCase) 63 * (i.e.: query_WildCards & query_IgnoreCase)
60 */ 64 */
61 virtual const uint querySettings() = 0; 65 virtual const uint querySettings() = 0;
62 66
63 /** Check whether settings are correct. 67 /** Check whether settings are correct.
64 * @return <i>true</i> if the given settings are correct and possible. 68 * @return <i>true</i> if the given settings are correct and possible.
65 */ 69 */
66 virtual bool hasQuerySettings (uint querySettings) const = 0; 70 virtual bool hasQuerySettings (uint querySettings) const = 0;
67 71
68 72
69}; 73};
70#endif 74#endif
diff --git a/libopie2/opiepim/backend/ocontactaccessbackend_xml.h b/libopie2/opiepim/backend/ocontactaccessbackend_xml.h
index 2cdb45b..97ef40f 100644
--- a/libopie2/opiepim/backend/ocontactaccessbackend_xml.h
+++ b/libopie2/opiepim/backend/ocontactaccessbackend_xml.h
@@ -1,231 +1,241 @@
1/* 1/*
2 * XML Backend for the OPIE-Contact Database. 2 * XML Backend for the OPIE-Contact Database.
3 * 3 *
4 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) 4 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de)
5 * 5 *
6 * ===================================================================== 6 * =====================================================================
7 *This program is free software; you can redistribute it and/or 7 *This program is free software; you can redistribute it and/or
8 *modify it under the terms of the GNU Library General Public 8 *modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
11 * ===================================================================== 11 * =====================================================================
12 * ToDo: XML-Backend: Automatic reload if something was changed... 12 * ToDo: XML-Backend: Automatic reload if something was changed...
13 * 13 *
14 * 14 *
15 * ===================================================================== 15 * =====================================================================
16 * Version: $Id$ 16 * Version: $Id$
17 * ===================================================================== 17 * =====================================================================
18 * History: 18 * History:
19 * $Log$ 19 * $Log$
20 * Revision 1.2 2002/10/07 17:34:24 eilers
21 * added OBackendFactory for advanced backend access
22 *
20 * Revision 1.1 2002/09/27 17:11:44 eilers 23 * Revision 1.1 2002/09/27 17:11:44 eilers
21 * Added API for accessing the Contact-Database ! It is compiling, but 24 * Added API for accessing the Contact-Database ! It is compiling, but
22 * please do not expect that anything is working ! 25 * please do not expect that anything is working !
23 * I will debug that stuff in the next time .. 26 * I will debug that stuff in the next time ..
24 * Please read README_COMPILE for compiling ! 27 * Please read README_COMPILE for compiling !
25 * 28 *
26 * 29 *
27 */ 30 */
28 31
29#ifndef _OContactAccessBackend_XML_ 32#ifndef _OContactAccessBackend_XML_
30#define _OContactAccessBackend_XML_ 33#define _OContactAccessBackend_XML_
31 34
32#include <qasciidict.h> 35#include <qasciidict.h>
33#include <qdatetime.h> 36#include <qdatetime.h>
34#include <qfile.h> 37#include <qfile.h>
38#include <qfileinfo.h>
35#include <qregexp.h> 39#include <qregexp.h>
36#include <qarray.h> 40#include <qarray.h>
37 41
42#include <qpe/global.h>
43
38#include <opie/xmltree.h> 44#include <opie/xmltree.h>
39#include "ocontactaccessbackend.h" 45#include "ocontactaccessbackend.h"
46#include "ocontactaccess.h"
47
48#include <stdlib.h>
49#include <errno.h>
40 50
41using namespace Opie; 51using namespace Opie;
42 52
43/* the default xml implementation */ 53/* the default xml implementation */
44class OContactAccessBackend_XML : public OContactAccessBackend { 54class OContactAccessBackend_XML : public OContactAccessBackend {
45 public: 55 public:
46 OContactAccessBackend_XML ( QString appname, QString filename = 0l ) 56 OContactAccessBackend_XML ( QString appname, QString filename = 0l )
47 { 57 {
48 m_appName = appname; 58 m_appName = appname;
49 59
50 /* Set journalfile name ... */ 60 /* Set journalfile name ... */
51 m_journalName = getenv("HOME"); 61 m_journalName = getenv("HOME");
52 m_journalName +="/.abjournal" + appname; 62 m_journalName +="/.abjournal" + appname;
53 63
54 /* Expecting to access the default filename if nothing else is set */ 64 /* Expecting to access the default filename if nothing else is set */
55 if ( filename.isEmpty() ){ 65 if ( filename.isEmpty() ){
56 m_fileName = Global::applicationFileName( "addressbook","addressbook.xml" ); 66 m_fileName = Global::applicationFileName( "addressbook","addressbook.xml" );
57 } else 67 } else
58 m_fileName = filename; 68 m_fileName = filename;
59 69
60 /* Load Database now */ 70 /* Load Database now */
61 load (); 71 load ();
62 } 72 }
63 73
64 bool save() { 74 bool save() {
65 QString strNewFile = m_fileName + ".new"; 75 QString strNewFile = m_fileName + ".new";
66 QFile f( strNewFile ); 76 QFile f( strNewFile );
67 if ( !f.open( IO_WriteOnly|IO_Raw ) ) 77 if ( !f.open( IO_WriteOnly|IO_Raw ) )
68 return false; 78 return false;
69 79
70 int total_written; 80 int total_written;
71 QString out; 81 QString out;
72 out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n" 82 out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>\n"
73 " <Groups>\n" 83 " <Groups>\n"
74 " </Groups>\n" 84 " </Groups>\n"
75 " <Contacts>\n"; 85 " <Contacts>\n";
76 //QValueList<Contact>::iterator it; 86 //QValueList<Contact>::iterator it;
77 QValueListConstIterator<OContact> it; 87 QValueListConstIterator<OContact> it;
78 for ( it = m_contactList.begin(); it != m_contactList.end(); ++it ) { 88 for ( it = m_contactList.begin(); it != m_contactList.end(); ++it ) {
79 out += "<Contact "; 89 out += "<Contact ";
80 (*it).save( out ); 90 (*it).save( out );
81 out += "/>\n"; 91 out += "/>\n";
82 QCString cstr = out.utf8(); 92 QCString cstr = out.utf8();
83 total_written = f.writeBlock( cstr.data(), cstr.length() ); 93 total_written = f.writeBlock( cstr.data(), cstr.length() );
84 if ( total_written != int(cstr.length()) ) { 94 if ( total_written != int(cstr.length()) ) {
85 f.close(); 95 f.close();
86 QFile::remove( strNewFile ); 96 QFile::remove( strNewFile );
87 return false; 97 return false;
88 } 98 }
89 out = ""; 99 out = "";
90 } 100 }
91 out += " </Contacts>\n</AddressBook>\n"; 101 out += " </Contacts>\n</AddressBook>\n";
92 102
93 QCString cstr = out.utf8(); 103 QCString cstr = out.utf8();
94 total_written = f.writeBlock( cstr.data(), cstr.length() ); 104 total_written = f.writeBlock( cstr.data(), cstr.length() );
95 if ( total_written != int( cstr.length() ) ) { 105 if ( total_written != int( cstr.length() ) ) {
96 f.close(); 106 f.close();
97 QFile::remove( strNewFile ); 107 QFile::remove( strNewFile );
98 return false; 108 return false;
99 } 109 }
100 f.close(); 110 f.close();
101 111
102 // move the file over, I'm just going to use the system call 112 // move the file over, I'm just going to use the system call
103 // because, I don't feel like using QDir. 113 // because, I don't feel like using QDir.
104 if ( ::rename( strNewFile.latin1(), m_fileName.latin1() ) < 0 ) { 114 if ( ::rename( strNewFile.latin1(), m_fileName.latin1() ) < 0 ) {
105 qWarning( "problem renaming file %s to %s, errno: %d", 115 qWarning( "problem renaming file %s to %s, errno: %d",
106 strNewFile.latin1(), m_journalName.latin1(), errno ); 116 strNewFile.latin1(), m_journalName.latin1(), errno );
107 // remove the tmp file... 117 // remove the tmp file...
108 QFile::remove( strNewFile ); 118 QFile::remove( strNewFile );
109 } 119 }
110 120
111 /* The journalfile should be removed now... */ 121 /* The journalfile should be removed now... */
112 removeJournal(); 122 removeJournal();
113 return true; 123 return true;
114 } 124 }
115 125
116 bool load () { 126 bool load () {
117 m_contactList.clear(); 127 m_contactList.clear();
118 128
119 /* Load XML-File and journal if it exists */ 129 /* Load XML-File and journal if it exists */
120 if ( !load ( m_fileName, false ) ) 130 if ( !load ( m_fileName, false ) )
121 return false; 131 return false;
122 /* The returncode of the journalfile is ignored due to the 132 /* The returncode of the journalfile is ignored due to the
123 * fact that it does not exist when this class is instantiated ! 133 * fact that it does not exist when this class is instantiated !
124 * But there may such a file exist, if the application crashed. 134 * But there may such a file exist, if the application crashed.
125 * Therefore we try to load it to get the changes before the # 135 * Therefore we try to load it to get the changes before the #
126 * crash happened... 136 * crash happened...
127 */ 137 */
128 load (m_journalName, true); 138 load (m_journalName, true);
129 139
130 return true; 140 return true;
131 } 141 }
132 142
133 void clear () { 143 void clear () {
134 m_contactList.clear(); 144 m_contactList.clear();
135 145
136 } 146 }
137 147
138 bool wasChangedExternally() 148 bool wasChangedExternally()
139 { 149 {
140 QFileInfo fi( m_fileName ); 150 QFileInfo fi( m_fileName );
141 151
142 QDateTime lastmod = fi.lastModified (); 152 QDateTime lastmod = fi.lastModified ();
143 153
144 return (lastmod != m_readtime); 154 return (lastmod != m_readtime);
145 } 155 }
146 156
147 QArray<int> allRecords() const { 157 QArray<int> allRecords() const {
148 QArray<int> uid_list( m_contactList.count() ); 158 QArray<int> uid_list( m_contactList.count() );
149 159
150 uint counter = 0; 160 uint counter = 0;
151 QValueListConstIterator<OContact> it; 161 QValueListConstIterator<OContact> it;
152 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ 162 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){
153 uid_list[counter++] = (*it).uid(); 163 uid_list[counter++] = (*it).uid();
154 } 164 }
155 165
156 return ( uid_list ); 166 return ( uid_list );
157 } 167 }
158 168
159 OContact find ( int uid ) const 169 OContact find ( int uid ) const
160 { 170 {
161 bool found = false; 171 bool found = false;
162 OContact foundContact; //Create empty contact 172 OContact foundContact; //Create empty contact
163 173
164 QValueListConstIterator<OContact> it; 174 QValueListConstIterator<OContact> it;
165 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ 175 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){
166 if ((*it).uid() == uid){ 176 if ((*it).uid() == uid){
167 found = true; 177 found = true;
168 break; 178 break;
169 } 179 }
170 } 180 }
171 if ( found ){ 181 if ( found ){
172 foundContact = *it; 182 foundContact = *it;
173 } 183 }
174 184
175 return ( foundContact ); 185 return ( foundContact );
176 } 186 }
177 187
178 QArray<int> queryByExample ( const OContact &query, int settings ){ 188 QArray<int> queryByExample ( const OContact &query, int settings ){
179 189
180 QArray<int> m_currentQuery( m_contactList.count() ); 190 QArray<int> m_currentQuery( m_contactList.count() );
181 QValueListConstIterator<OContact> it; 191 QValueListConstIterator<OContact> it;
182 uint arraycounter = 0; 192 uint arraycounter = 0;
183 193
184 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){ 194 for( it = m_contactList.begin(); it != m_contactList.end(); ++it ){
185 /* Search all fields and compare them with query object. Store them into list 195 /* Search all fields and compare them with query object. Store them into list
186 * if all fields matches. 196 * if all fields matches.
187 */ 197 */
188 bool allcorrect = true; 198 bool allcorrect = true;
189 for ( int i = 0; i < Qtopia::rid; i++ ) { 199 for ( int i = 0; i < Qtopia::rid; i++ ) {
190 /* Just compare fields which are not empty in the query object */ 200 /* Just compare fields which are not empty in the query object */
191 if ( !query.field(i).isEmpty() ){ 201 if ( !query.field(i).isEmpty() ){
192 switch ( settings & ~OContactAccess::query_IgnoreCase ){ 202 switch ( settings & ~OContactAccess::query_IgnoreCase ){
193 case OContactAccess::query_RegExp:{ 203 case OContactAccess::query_RegExp:{
194 QRegExp expr ( query.field(i), 204 QRegExp expr ( query.field(i),
195 !(settings & OContactAccess::query_IgnoreCase), 205 !(settings & OContactAccess::query_IgnoreCase),
196 false ); 206 false );
197 if ( expr.find ( (*it).field(i), 0 ) == -1 ) 207 if ( expr.find ( (*it).field(i), 0 ) == -1 )
198 allcorrect = false; 208 allcorrect = false;
199 } 209 }
200 break; 210 break;
201 case OContactAccess::query_WildCards:{ 211 case OContactAccess::query_WildCards:{
202 QRegExp expr ( query.field(i), 212 QRegExp expr ( query.field(i),
203 !(settings & OContactAccess::query_IgnoreCase), 213 !(settings & OContactAccess::query_IgnoreCase),
204 true ); 214 true );
205 if ( expr.find ( (*it).field(i), 0 ) == -1 ) 215 if ( expr.find ( (*it).field(i), 0 ) == -1 )
206 allcorrect = false; 216 allcorrect = false;
207 } 217 }
208 break; 218 break;
209 case OContactAccess::query_ExactMatch:{ 219 case OContactAccess::query_ExactMatch:{
210 if (settings & OContactAccess::query_IgnoreCase){ 220 if (settings & OContactAccess::query_IgnoreCase){
211 if ( query.field(i).upper() != 221 if ( query.field(i).upper() !=
212 (*it).field(i).upper() ) 222 (*it).field(i).upper() )
213 allcorrect = false; 223 allcorrect = false;
214 }else{ 224 }else{
215 if ( query.field(i) != (*it).field(i) ) 225 if ( query.field(i) != (*it).field(i) )
216 allcorrect = false; 226 allcorrect = false;
217 } 227 }
218 } 228 }
219 break; 229 break;
220 } 230 }
221 } 231 }
222 } 232 }
223 if ( allcorrect ){ 233 if ( allcorrect ){
224 m_currentQuery[arraycounter++] = (*it).uid(); 234 m_currentQuery[arraycounter++] = (*it).uid();
225 } 235 }
226 } 236 }
227 237
228 // Shrink to fit.. 238 // Shrink to fit..
229 m_currentQuery.resize(arraycounter); 239 m_currentQuery.resize(arraycounter);
230 240
231 return m_currentQuery; 241 return m_currentQuery;
diff --git a/libopie2/opiepim/core/ocontactaccess.cpp b/libopie2/opiepim/core/ocontactaccess.cpp
index 8a8ff08..b5f358b 100644
--- a/libopie2/opiepim/core/ocontactaccess.cpp
+++ b/libopie2/opiepim/core/ocontactaccess.cpp
@@ -1,180 +1,186 @@
1/* 1/*
2 * Class to manage the Contacts. 2 * Class to manage the Contacts.
3 * 3 *
4 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) 4 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de)
5 * 5 *
6 * ===================================================================== 6 * =====================================================================
7 *This program is free software; you can redistribute it and/or 7 *This program is free software; you can redistribute it and/or
8 *modify it under the terms of the GNU Library General Public 8 *modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
11 * ===================================================================== 11 * =====================================================================
12 * Info: This class could just work with a change in the header-file 12 * Info: This class could just work with a change in the header-file
13 * of the Contact class ! Therefore our libopie only compiles 13 * of the Contact class ! Therefore our libopie only compiles
14 * with our version of libqpe 14 * with our version of libqpe
15 * ===================================================================== 15 * =====================================================================
16 * ToDo: XML-Backend: Automatic reload if something was changed... 16 * ToDo: XML-Backend: Automatic reload if something was changed...
17 * 17 *
18 * 18 *
19 * ===================================================================== 19 * =====================================================================
20 * Version: $Id$ 20 * Version: $Id$
21 * ===================================================================== 21 * =====================================================================
22 * History: 22 * History:
23 * $Log$ 23 * $Log$
24 * Revision 1.3 2002/10/07 17:34:24 eilers
25 * added OBackendFactory for advanced backend access
26 *
24 * Revision 1.2 2002/10/02 16:18:11 eilers 27 * Revision 1.2 2002/10/02 16:18:11 eilers
25 * debugged and seems to work almost perfectly .. 28 * debugged and seems to work almost perfectly ..
26 * 29 *
27 * Revision 1.1 2002/09/27 17:11:44 eilers 30 * Revision 1.1 2002/09/27 17:11:44 eilers
28 * Added API for accessing the Contact-Database ! It is compiling, but 31 * Added API for accessing the Contact-Database ! It is compiling, but
29 * please do not expect that anything is working ! 32 * please do not expect that anything is working !
30 * I will debug that stuff in the next time .. 33 * I will debug that stuff in the next time ..
31 * Please read README_COMPILE for compiling ! 34 * Please read README_COMPILE for compiling !
32 * 35 *
33 * 36 *
34 */ 37 */
35 38
36#include "ocontactaccess.h" 39#include "ocontactaccess.h"
40#include "obackendfactory.h"
37 41
38#include <qasciidict.h> 42#include <qasciidict.h>
39#include <qdatetime.h> 43#include <qdatetime.h>
40#include <qfile.h> 44#include <qfile.h>
41#include <qregexp.h> 45#include <qregexp.h>
42#include <qlist.h> 46#include <qlist.h>
43#include <qcopchannel_qws.h> 47#include <qcopchannel_qws.h>
44 48
45//#include <qpe/qcopenvelope_qws.h> 49//#include <qpe/qcopenvelope_qws.h>
46#include <qpe/global.h> 50#include <qpe/global.h>
47 51
48#include <errno.h> 52#include <errno.h>
49#include <fcntl.h> 53#include <fcntl.h>
50#include <unistd.h> 54#include <unistd.h>
51#include <stdlib.h> 55#include <stdlib.h>
52 56
53#include "ocontactaccessbackend_xml.h" 57#include "ocontactaccessbackend_xml.h"
54 58
55 59
56OContactAccess::OContactAccess ( const QString appname, const QString filename, 60OContactAccess::OContactAccess ( const QString appname, const QString ,
57 OContactAccessBackend* end, bool autosync ): 61 OContactAccessBackend* end, bool autosync ):
58 OPimAccessTemplate<OContact>( end ), 62 OPimAccessTemplate<OContact>( end ),
59 m_changed ( false ) 63 m_changed ( false )
60{ 64{
61 /* take care of the backend. If there is no one defined, we 65 /* take care of the backend. If there is no one defined, we
62 * will use the XML-Backend as default (until we have a cute SQL-Backend..). 66 * will use the XML-Backend as default (until we have a cute SQL-Backend..).
63 */ 67 */
64 if( end == 0 ) { 68 if( end == 0 ) {
65 end = new OContactAccessBackend_XML( appname, filename ); 69 // __asm__("int3");
70 qWarning ("Using BackendFactory !");
71 end = OBackendFactory<OContactAccessBackend>::Default( "contact", appname );
66 } 72 }
67 // Set backend locally and in template 73 // Set backend locally and in template
68 m_backEnd = end; 74 m_backEnd = end;
69 OPimAccessTemplate<OContact>::setBackEnd (end); 75 OPimAccessTemplate<OContact>::setBackEnd (end);
70 76
71 77
72 /* Connect signal of external db change to function */ 78 /* Connect signal of external db change to function */
73 QCopChannel *dbchannel = new QCopChannel( "QPE/PIM", this ); 79 QCopChannel *dbchannel = new QCopChannel( "QPE/PIM", this );
74 connect( dbchannel, SIGNAL(received(const QCString &, const QByteArray &)), 80 connect( dbchannel, SIGNAL(received(const QCString &, const QByteArray &)),
75 this, SLOT(copMessage( const QCString &, const QByteArray &)) ); 81 this, SLOT(copMessage( const QCString &, const QByteArray &)) );
76 if ( autosync ){ 82 if ( autosync ){
77 QCopChannel *syncchannel = new QCopChannel( "QPE/Sync", this ); 83 QCopChannel *syncchannel = new QCopChannel( "QPE/Sync", this );
78 connect( syncchannel, SIGNAL(received(const QCString &, const QByteArray &)), 84 connect( syncchannel, SIGNAL(received(const QCString &, const QByteArray &)),
79 this, SLOT(copMessage( const QCString &, const QByteArray &)) ); 85 this, SLOT(copMessage( const QCString &, const QByteArray &)) );
80 } 86 }
81 87
82 88
83} 89}
84OContactAccess::~OContactAccess () 90OContactAccess::~OContactAccess ()
85{ 91{
86 /* The user may forget to save the changed database, therefore try to 92 /* The user may forget to save the changed database, therefore try to
87 * do it for him.. 93 * do it for him..
88 */ 94 */
89 if ( m_changed ) 95 if ( m_changed )
90 save(); 96 save();
91 // delete m_backEnd; is done by template.. 97 // delete m_backEnd; is done by template..
92} 98}
93 99
94bool OContactAccess::load() 100bool OContactAccess::load()
95{ 101{
96 return ( m_backEnd->load() ); 102 return ( m_backEnd->load() );
97} 103}
98 104
99bool OContactAccess::save () 105bool OContactAccess::save ()
100{ 106{
101 /* If the database was changed externally, we could not save the 107 /* If the database was changed externally, we could not save the
102 * Data. This will remove added items which is unacceptable ! 108 * Data. This will remove added items which is unacceptable !
103 * Therefore: Reload database and merge the data... 109 * Therefore: Reload database and merge the data...
104 */ 110 */
105 if ( m_backEnd->wasChangedExternally() ) 111 if ( m_backEnd->wasChangedExternally() )
106 reload(); 112 reload();
107 113
108 if ( m_changed ){ 114 if ( m_changed ){
109 bool status = m_backEnd->save(); 115 bool status = m_backEnd->save();
110 if ( !status ) return false; 116 if ( !status ) return false;
111 117
112 m_changed = false; 118 m_changed = false;
113 /* Now tell everyone that new data is available. 119 /* Now tell everyone that new data is available.
114 */ 120 */
115 QCopEnvelope e( "QPE/PIM", "addressbookUpdated()" ); 121 QCopEnvelope e( "QPE/PIM", "addressbookUpdated()" );
116 122
117 } 123 }
118 124
119 return true; 125 return true;
120} 126}
121 127
122const uint OContactAccess::querySettings() 128const uint OContactAccess::querySettings()
123{ 129{
124 return ( m_backEnd->querySettings() ); 130 return ( m_backEnd->querySettings() );
125} 131}
126 132
127bool OContactAccess::hasQuerySettings ( int querySettings ) const 133bool OContactAccess::hasQuerySettings ( int querySettings ) const
128{ 134{
129 return ( m_backEnd->hasQuerySettings ( querySettings ) ); 135 return ( m_backEnd->hasQuerySettings ( querySettings ) );
130} 136}
131 137
132bool OContactAccess::add ( const OContact& newcontact ) 138bool OContactAccess::add ( const OContact& newcontact )
133{ 139{
134 m_changed = true; 140 m_changed = true;
135 return ( m_backEnd->add ( newcontact ) ); 141 return ( m_backEnd->add ( newcontact ) );
136} 142}
137 143
138bool OContactAccess::replace ( const OContact& contact ) 144bool OContactAccess::replace ( const OContact& contact )
139{ 145{
140 m_changed = true; 146 m_changed = true;
141 return ( m_backEnd->replace ( contact ) ); 147 return ( m_backEnd->replace ( contact ) );
142} 148}
143 149
144bool OContactAccess::remove ( const OContact& t ) 150bool OContactAccess::remove ( const OContact& t )
145{ 151{
146 m_changed = true; 152 m_changed = true;
147 return ( m_backEnd->remove ( t.uid() ) ); 153 return ( m_backEnd->remove ( t.uid() ) );
148} 154}
149 155
150bool OContactAccess::remove ( int uid ) 156bool OContactAccess::remove ( int uid )
151{ 157{
152 m_changed = true; 158 m_changed = true;
153 return ( m_backEnd->remove ( uid ) ); 159 return ( m_backEnd->remove ( uid ) );
154} 160}
155 161
156bool OContactAccess::wasChangedExternally()const 162bool OContactAccess::wasChangedExternally()const
157{ 163{
158 return ( m_backEnd->wasChangedExternally() ); 164 return ( m_backEnd->wasChangedExternally() );
159} 165}
160 166
161 167
162bool OContactAccess::reload() 168bool OContactAccess::reload()
163{ 169{
164 return ( m_backEnd->reload() ); 170 return ( m_backEnd->reload() );
165} 171}
166 172
167void OContactAccess::copMessage( const QCString &msg, const QByteArray & ) 173void OContactAccess::copMessage( const QCString &msg, const QByteArray & )
168{ 174{
169 if ( msg == "addressbookUpdated()" ){ 175 if ( msg == "addressbookUpdated()" ){
170 qWarning ("OContactAccess: Received addressbokUpdated()"); 176 qWarning ("OContactAccess: Received addressbokUpdated()");
171 emit signalChanged ( this ); 177 emit signalChanged ( this );
172 } else if ( msg == "flush()" ) { 178 } else if ( msg == "flush()" ) {
173 qWarning ("OContactAccess: Received flush()"); 179 qWarning ("OContactAccess: Received flush()");
174 save (); 180 save ();
175 } else if ( msg == "reload()" ) { 181 } else if ( msg == "reload()" ) {
176 qWarning ("OContactAccess: Received reload()"); 182 qWarning ("OContactAccess: Received reload()");
177 reload (); 183 reload ();
178 emit signalChanged ( this ); 184 emit signalChanged ( this );
179 } 185 }
180} 186}