author | eilers <eilers> | 2002-09-27 17:11:44 (UTC) |
---|---|---|
committer | eilers <eilers> | 2002-09-27 17:11:44 (UTC) |
commit | 44c98b497ff1cb8cddba10b56435f136a583cce2 (patch) (unidiff) | |
tree | d76ab641b0a9151031f1ec7ac19ff00c11f82b53 /libopie2/opiepim/core | |
parent | d44b205c9009af71db036b3951363a91110e1c20 (diff) | |
download | opie-44c98b497ff1cb8cddba10b56435f136a583cce2.zip opie-44c98b497ff1cb8cddba10b56435f136a583cce2.tar.gz opie-44c98b497ff1cb8cddba10b56435f136a583cce2.tar.bz2 |
Added API for accessing the Contact-Database ! It is compiling, but
please do not expect that anything is working !
I will debug that stuff in the next time ..
Please read README_COMPILE for compiling !
-rw-r--r-- | libopie2/opiepim/core/ocontactaccess.cpp | 174 | ||||
-rw-r--r-- | libopie2/opiepim/core/ocontactaccess.h | 197 |
2 files changed, 371 insertions, 0 deletions
diff --git a/libopie2/opiepim/core/ocontactaccess.cpp b/libopie2/opiepim/core/ocontactaccess.cpp new file mode 100644 index 0000000..2d808f7 --- a/dev/null +++ b/libopie2/opiepim/core/ocontactaccess.cpp | |||
@@ -0,0 +1,174 @@ | |||
1 | /* | ||
2 | * Class to manage the Contacts. | ||
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; either | ||
10 | * version 2 of the License, or (at your option) any later version. | ||
11 | * ===================================================================== | ||
12 | * Info: This class could just work with a change in the header-file | ||
13 | * of the Contact class ! Therefore our libopie only compiles | ||
14 | * with our version of libqpe | ||
15 | * ===================================================================== | ||
16 | * ToDo: XML-Backend: Automatic reload if something was changed... | ||
17 | * | ||
18 | * | ||
19 | * ===================================================================== | ||
20 | * Version: $Id$ | ||
21 | * ===================================================================== | ||
22 | * History: | ||
23 | * $Log$ | ||
24 | * Revision 1.1 2002/09/27 17:11:44 eilers | ||
25 | * Added API for accessing the Contact-Database ! It is compiling, but | ||
26 | * please do not expect that anything is working ! | ||
27 | * I will debug that stuff in the next time .. | ||
28 | * Please read README_COMPILE for compiling ! | ||
29 | * | ||
30 | * | ||
31 | */ | ||
32 | |||
33 | #include "ocontactaccess.h" | ||
34 | |||
35 | #include <qasciidict.h> | ||
36 | #include <qdatetime.h> | ||
37 | #include <qfile.h> | ||
38 | #include <qregexp.h> | ||
39 | #include <qlist.h> | ||
40 | #include <qcopchannel_qws.h> | ||
41 | |||
42 | //#include <qpe/qcopenvelope_qws.h> | ||
43 | #include <qpe/global.h> | ||
44 | |||
45 | #include <errno.h> | ||
46 | #include <fcntl.h> | ||
47 | #include <unistd.h> | ||
48 | #include <stdlib.h> | ||
49 | |||
50 | #include "ocontactaccessbackend_xml.h" | ||
51 | |||
52 | |||
53 | OContactAccess::OContactAccess ( const QString appname, const QString filename, | ||
54 | OContactAccessBackend* end, bool autosync ): | ||
55 | OPimAccessTemplate<OContact>( end ), | ||
56 | m_changed ( false ) | ||
57 | { | ||
58 | /* take care of the backend. If there is no one defined, we | ||
59 | * will use the XML-Backend as default (until we have a cute SQL-Backend..). | ||
60 | */ | ||
61 | if( end == 0 ) { | ||
62 | end = new OContactAccessBackend_XML( appname, filename ); | ||
63 | } | ||
64 | m_backEnd = end; | ||
65 | |||
66 | /* Connect signal of external db change to function */ | ||
67 | QCopChannel *dbchannel = new QCopChannel( "QPE/PIM", this ); | ||
68 | connect( dbchannel, SIGNAL(received(const QCString &, const QByteArray &)), | ||
69 | this, SLOT(copMessage( const QCString &, const QByteArray &)) ); | ||
70 | if ( autosync ){ | ||
71 | QCopChannel *syncchannel = new QCopChannel( "QPE/Sync", this ); | ||
72 | connect( syncchannel, SIGNAL(received(const QCString &, const QByteArray &)), | ||
73 | this, SLOT(copMessage( const QCString &, const QByteArray &)) ); | ||
74 | } | ||
75 | |||
76 | |||
77 | } | ||
78 | OContactAccess::~OContactAccess () | ||
79 | { | ||
80 | /* The user may forget to save the changed database, therefore try to | ||
81 | * do it for him.. | ||
82 | */ | ||
83 | if ( m_changed ) | ||
84 | save(); | ||
85 | delete m_backEnd; | ||
86 | } | ||
87 | |||
88 | bool OContactAccess::load() | ||
89 | { | ||
90 | return ( m_backEnd->load() ); | ||
91 | } | ||
92 | |||
93 | bool OContactAccess::save () | ||
94 | { | ||
95 | /* If the database was changed externally, we could not save the | ||
96 | * Data. This will remove added items which is unacceptable ! | ||
97 | * Therefore: Reload database and merge the data... | ||
98 | */ | ||
99 | if ( m_backEnd->wasChangedExternally() ) | ||
100 | reload(); | ||
101 | |||
102 | if ( m_changed ){ | ||
103 | bool status = m_backEnd->save(); | ||
104 | if ( !status ) return false; | ||
105 | |||
106 | m_changed = false; | ||
107 | /* Now tell everyone that new data is available. | ||
108 | */ | ||
109 | QCopEnvelope e( "QPE/PIM", "addressbookUpdated()" ); | ||
110 | |||
111 | } | ||
112 | |||
113 | return true; | ||
114 | } | ||
115 | |||
116 | const uint OContactAccess::querySettings() | ||
117 | { | ||
118 | return ( m_backEnd->querySettings() ); | ||
119 | } | ||
120 | |||
121 | bool OContactAccess::hasQuerySettings ( int querySettings ) const | ||
122 | { | ||
123 | return ( m_backEnd->hasQuerySettings ( querySettings ) ); | ||
124 | } | ||
125 | |||
126 | bool OContactAccess::add ( const OContact& newcontact ) | ||
127 | { | ||
128 | m_changed = true; | ||
129 | return ( m_backEnd->add ( newcontact ) ); | ||
130 | } | ||
131 | |||
132 | bool OContactAccess::replace ( const OContact& contact ) | ||
133 | { | ||
134 | m_changed = true; | ||
135 | return ( m_backEnd->replace ( contact ) ); | ||
136 | } | ||
137 | |||
138 | bool OContactAccess::remove ( const OContact& t ) | ||
139 | { | ||
140 | m_changed = true; | ||
141 | return ( m_backEnd->remove ( t.uid() ) ); | ||
142 | } | ||
143 | |||
144 | bool OContactAccess::remove ( int uid ) | ||
145 | { | ||
146 | m_changed = true; | ||
147 | return ( m_backEnd->remove ( uid ) ); | ||
148 | } | ||
149 | |||
150 | bool OContactAccess::wasChangedExternally()const | ||
151 | { | ||
152 | return ( m_backEnd->wasChangedExternally() ); | ||
153 | } | ||
154 | |||
155 | |||
156 | bool OContactAccess::reload() | ||
157 | { | ||
158 | return ( m_backEnd->reload() ); | ||
159 | } | ||
160 | |||
161 | void OContactAccess::copMessage( const QCString &msg, const QByteArray & ) | ||
162 | { | ||
163 | if ( msg == "addressbookUpdated()" ){ | ||
164 | qWarning ("OContactAccess: Received addressbokUpdated()"); | ||
165 | emit signalChanged ( this ); | ||
166 | } else if ( msg == "flush()" ) { | ||
167 | qWarning ("OContactAccess: Received flush()"); | ||
168 | save (); | ||
169 | } else if ( msg == "reload()" ) { | ||
170 | qWarning ("OContactAccess: Received reload()"); | ||
171 | reload (); | ||
172 | emit signalChanged ( this ); | ||
173 | } | ||
174 | } | ||
diff --git a/libopie2/opiepim/core/ocontactaccess.h b/libopie2/opiepim/core/ocontactaccess.h new file mode 100644 index 0000000..54f7f07 --- a/dev/null +++ b/libopie2/opiepim/core/ocontactaccess.h | |||
@@ -0,0 +1,197 @@ | |||
1 | /* | ||
2 | * Class to manage the Contacts. | ||
3 | * | ||
4 | * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de) | ||
5 | * Copyright (c) 2002 by Holger Freyther (zecke@handhelds.org) | ||
6 | * | ||
7 | * ===================================================================== | ||
8 | *This program is free software; you can redistribute it and/or | ||
9 | *modify it under the terms of the GNU Library General Public | ||
10 | * License as published by the Free Software Foundation; | ||
11 | * either version 2 of the License, or (at your option) any later | ||
12 | * version. | ||
13 | * ===================================================================== | ||
14 | * ToDo: Define enum for query settings | ||
15 | * ===================================================================== | ||
16 | * Version: $Id$ | ||
17 | * ===================================================================== | ||
18 | * History: | ||
19 | * $Log$ | ||
20 | * Revision 1.1 2002/09/27 17:11:44 eilers | ||
21 | * Added API for accessing the Contact-Database ! It is compiling, but | ||
22 | * please do not expect that anything is working ! | ||
23 | * I will debug that stuff in the next time .. | ||
24 | * Please read README_COMPILE for compiling ! | ||
25 | * | ||
26 | * ===================================================================== | ||
27 | */ | ||
28 | #ifndef _OCONTACTACCESS_H | ||
29 | #define _OCONTACTACCESS_H | ||
30 | |||
31 | #include <qobject.h> | ||
32 | |||
33 | #include <qpe/qcopenvelope_qws.h> | ||
34 | |||
35 | #include <qvaluelist.h> | ||
36 | #include <qfileinfo.h> | ||
37 | |||
38 | #include "ocontact.h" | ||
39 | #include "ocontactaccessbackend.h" | ||
40 | #include "opimaccesstemplate.h" | ||
41 | |||
42 | /** Class to access the contacts database. | ||
43 | * This is just a frontend for the real database handling which is | ||
44 | * done by the backend. | ||
45 | */ | ||
46 | class OContactAccess: public QObject, public OPimAccessTemplate<OContact> | ||
47 | { | ||
48 | Q_OBJECT | ||
49 | |||
50 | |||
51 | /* class Iterator{ | ||
52 | friend OContactAccess; | ||
53 | public: | ||
54 | Iterator(); | ||
55 | Iterator ( const Iterator& copy ); | ||
56 | |||
57 | bool operator== ( const Iterator& it ); | ||
58 | bool operator!= ( const Iterator& it ); | ||
59 | Iterator& operator= ( const Iterator& it ); | ||
60 | Iterator& operator++ (); // prefix | ||
61 | Iterator operator++ ( int ); // postfix | ||
62 | Iterator& operator-- (); // prefix | ||
63 | Iterator operator-- ( int ); // postfix | ||
64 | Contact operator*() const; | ||
65 | Contact operator->() const; | ||
66 | |||
67 | Iterator begin(); | ||
68 | Iterator end(); | ||
69 | |||
70 | uint count() const; | ||
71 | |||
72 | private: | ||
73 | QValueList<int> m_uids; | ||
74 | int m_cur_position; | ||
75 | bool m_end_reached; | ||
76 | OContactAccess *m_db; | ||
77 | |||
78 | }; | ||
79 | |||
80 | */ | ||
81 | |||
82 | public: | ||
83 | /** Create Database with contacts (addressbook). | ||
84 | * @param appname Name of application which wants access to the database | ||
85 | * (i.e. "todolist") | ||
86 | * @param filename The name of the database file. If not set, the default one | ||
87 | * is used. | ||
88 | * @param backend Pointer to an alternative Backend. If not set, we will use | ||
89 | * the default backend. | ||
90 | * @param handlesync If <b>true</b> the database stores the current state | ||
91 | * automatically if it receives the signals <i>flush()</i> and <i>reload()</i> | ||
92 | * which are used before and after synchronisation. If the application wants | ||
93 | * to react itself, it should be disabled by setting it to <b>false</b> | ||
94 | * @see OContactBackend | ||
95 | */ | ||
96 | OContactAccess (const QString appname, const QString filename = 0l, | ||
97 | OContactAccessBackend* backend = 0l, bool handlesync = true); | ||
98 | ~OContactAccess (); | ||
99 | |||
100 | /** Constants for query. | ||
101 | * Use this constants to set the query parameters. | ||
102 | * Note: <i>query_IgnoreCase</i> just make sense with one of the other attributes ! | ||
103 | * @see queryByExample() | ||
104 | * - why not enum - zecke? | ||
105 | * -> Had some implementation problems .. Will use enum soon ! .. (se) | ||
106 | */ | ||
107 | static const int query_WildCards = 0x0001; | ||
108 | static const int query_IgnoreCase = 0x0002; | ||
109 | static const int query_RegExp = 0x0004; | ||
110 | static const int query_ExactMatch = 0x0008; | ||
111 | |||
112 | /** Return all possible settings. | ||
113 | * @return All settings provided by the current backend | ||
114 | * (i.e.: query_WildCards & query_IgnoreCase) | ||
115 | */ | ||
116 | const uint querySettings(); | ||
117 | |||
118 | /** Check whether settings are correct. | ||
119 | * @return <i>true</i> if the given settings are correct and possible. | ||
120 | */ | ||
121 | bool hasQuerySettings ( int querySettings ) const; | ||
122 | |||
123 | /** Add Contact to database. | ||
124 | * @param newcontact The contact to add. | ||
125 | * @return <i>true</i> if added successfully. | ||
126 | */ | ||
127 | bool add (const OContact& newcontact); | ||
128 | |||
129 | /** Replace contact. | ||
130 | * Replaces given contact with contact with the user id <i>uid</i>. | ||
131 | * @param uid The user ID | ||
132 | * @param contact The new contact | ||
133 | * @return <i>true</i> if successful. | ||
134 | */ | ||
135 | bool replace ( const OContact& contact ); | ||
136 | |||
137 | /** Remove contact. | ||
138 | * Removes contact with the user id <i>uid</i>. | ||
139 | * @param The contact to remove | ||
140 | * @return <i>true</i> if successful. | ||
141 | */ | ||
142 | bool remove ( const OContact& t ); | ||
143 | |||
144 | /** Remove contact. | ||
145 | * Removes contact with the user id <i>uid</i>. | ||
146 | * @param The user id of the contact to remove | ||
147 | * @return <i>true</i> if successful. | ||
148 | */ | ||
149 | bool remove ( int uid ); | ||
150 | |||
151 | /** Load Database * | ||
152 | */ | ||
153 | bool load(); | ||
154 | |||
155 | /** | ||
156 | * if the resource was changed externally. | ||
157 | * You should use the signal instead of polling possible changes ! | ||
158 | */ | ||
159 | bool wasChangedExternally()const; | ||
160 | |||
161 | /** Reload database. | ||
162 | * You should execute this function if the external database | ||
163 | * was changed. | ||
164 | * This function will load the external database and afterwards | ||
165 | * rejoin the local changes. Therefore the local database will be set consistent. | ||
166 | */ | ||
167 | bool reload(); | ||
168 | |||
169 | /** Save contacts database. | ||
170 | * Save is more a "commit". After calling this function, all changes are public available. | ||
171 | * @return true if successful | ||
172 | */ | ||
173 | bool save(); | ||
174 | |||
175 | signals: | ||
176 | /* Signal is emitted if the database was changed. Therefore | ||
177 | * we may need to reload to stay consistent. | ||
178 | * @param which Pointer to the database who created this event. This pointer | ||
179 | * is useful if an application has to handle multiple databases at the same time. | ||
180 | * @see reload() | ||
181 | */ | ||
182 | void signalChanged ( const OContactAccess *which ); | ||
183 | |||
184 | |||
185 | private: | ||
186 | // class OContactAccessPrivate; | ||
187 | // OContactAccessPrivate* d; | ||
188 | OContactAccessBackend *m_backEnd; | ||
189 | bool m_loading:1; | ||
190 | bool m_changed; | ||
191 | |||
192 | private slots: | ||
193 | void copMessage( const QCString &msg, const QByteArray &data ); | ||
194 | |||
195 | |||
196 | }; | ||
197 | #endif | ||