summaryrefslogtreecommitdiff
path: root/libopie/pim/ocontactaccess.cpp
blob: e8c0a45087f29be12723b4c1485cf8f414377195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Class to manage the Contacts.
 *
 * Copyright (c) 2002 by Stefan Eilers (Eilers.Stefan@epost.de)
 *
 * =====================================================================
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Library General Public
 *      License as published by the Free Software Foundation; either
 *      version 2 of the License, or (at your option) any later version.
 * =====================================================================
 * Info: This class could just work with a change in the header-file
 *       of the Contact class ! Therefore our libopie only compiles
 *       with our version of libqpe
 * =====================================================================
 * ToDo: XML-Backend: Automatic reload if something was changed...
 *
 *
 * =====================================================================
 * Version: $Id$
 * =====================================================================
 * History:
 * $Log$
 * Revision 1.4  2002/10/14 16:21:54  eilers
 * Some minor interface updates
 *
 * Revision 1.3  2002/10/07 17:34:24  eilers
 * added OBackendFactory for advanced backend access
 *
 * Revision 1.2  2002/10/02 16:18:11  eilers
 * debugged and seems to work almost perfectly ..
 *
 * Revision 1.1  2002/09/27 17:11:44  eilers
 * 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 !
 *
 *
 */

#include "ocontactaccess.h"
#include "obackendfactory.h"

#include <qasciidict.h>
#include <qdatetime.h>
#include <qfile.h>
#include <qregexp.h>
#include <qlist.h>
#include <qcopchannel_qws.h>

//#include <qpe/qcopenvelope_qws.h>
#include <qpe/global.h>

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#include "ocontactaccessbackend_xml.h"


OContactAccess::OContactAccess ( const QString appname, const QString ,
			 OContactAccessBackend* end, bool autosync ):
	OPimAccessTemplate<OContact>( end ),
	m_changed ( false )
{
        /* take care of the backend. If there is no one defined, we 
	 * will use the XML-Backend as default (until we have a cute SQL-Backend..).
	 */
        if( end == 0 ) {
		qWarning ("Using BackendFactory !");
		end = OBackendFactory<OContactAccessBackend>::Default( "contact", appname );
        }
	// Set backend locally and in template
        m_backEnd = end;
	OPimAccessTemplate<OContact>::setBackEnd (end);
	

	/* Connect signal of external db change to function */
	QCopChannel *dbchannel = new QCopChannel( "QPE/PIM", this );
	connect( dbchannel, SIGNAL(received(const QCString &, const QByteArray &)),
               this, SLOT(copMessage( const QCString &, const QByteArray &)) );
	if ( autosync ){
		QCopChannel *syncchannel = new QCopChannel( "QPE/Sync", this );
		connect( syncchannel, SIGNAL(received(const QCString &, const QByteArray &)),
			 this, SLOT(copMessage( const QCString &, const QByteArray &)) );
	}


}
OContactAccess::~OContactAccess ()
{
	/* The user may forget to save the changed database, therefore try to
	 * do it for him..
	 */
	if ( m_changed ) 
		save();
	// delete m_backEnd; is done by template..
}

bool OContactAccess::load()
{
	return ( m_backEnd->load() );
}

bool OContactAccess::save ()
{
	/* If the database was changed externally, we could not save the
	 * Data. This will remove added items which is unacceptable !
	 * Therefore: Reload database and merge the data...
	 */
	if ( m_backEnd->wasChangedExternally() )
		reload();

	if ( m_changed ){ 
		bool status = m_backEnd->save();
		if ( !status ) return false;

		m_changed = false;
		/* Now tell everyone that new data is available.
		 */
		QCopEnvelope e( "QPE/PIM", "addressbookUpdated()" );

	}

	return true;
}

const uint OContactAccess::querySettings()
{
	return ( m_backEnd->querySettings() );
}

bool OContactAccess::hasQuerySettings ( int querySettings ) const
{
	return ( m_backEnd->hasQuerySettings ( querySettings ) );
}

bool OContactAccess::add ( const OContact& newcontact )
{
	m_changed = true;
	return ( m_backEnd->add ( newcontact ) );
}

bool OContactAccess::replace ( const OContact& contact )
{
	m_changed = true;
	return ( m_backEnd->replace ( contact ) );
}

bool OContactAccess::remove ( const OContact& t )
{
	m_changed = true;
	return ( m_backEnd->remove ( t.uid() ) );
}

bool OContactAccess::remove ( int uid )
{
	m_changed = true;
	return ( m_backEnd->remove ( uid ) );
}

bool OContactAccess::wasChangedExternally()const
{
	return ( m_backEnd->wasChangedExternally() );
}


bool OContactAccess::reload()
{
	return ( m_backEnd->reload() );
}

void OContactAccess::copMessage( const QCString &msg, const QByteArray & )
{
	if ( msg == "addressbookUpdated()" ){
		qWarning ("OContactAccess: Received addressbokUpdated()");
		emit signalChanged ( this );
	} else if ( msg == "flush()" ) {
		qWarning ("OContactAccess: Received flush()");
		save ();
	} else if ( msg == "reload()" ) {
		qWarning ("OContactAccess: Received reload()");
		reload ();
		emit signalChanged ( this );
	}
}