00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef OPIE_BACKENDFACTORY_H_
00035 #define OPIE_BACKENDFACTORY_H_
00036
00037
00038 #include <opie2/opimaccessbackend.h>
00039 #include <opie2/opimglobal.h>
00040 #include <opie2/otodoaccessxml.h>
00041 #include <opie2/otodoaccessvcal.h>
00042 #include <opie2/ocontactaccessbackend_xml.h>
00043 #include <opie2/ocontactaccessbackend_vcard.h>
00044 #include <opie2/odatebookaccessbackend_xml.h>
00045 #include <opie2/odebug.h>
00046
00047 #ifdef __USE_SQL
00048 #include <opie2/otodoaccesssql.h>
00049 #include <opie2/ocontactaccessbackend_sql.h>
00050 #include <opie2/odatebookaccessbackend_sql.h>
00051 #endif
00052
00053 #include <qpe/config.h>
00054
00055
00056 #include <qstring.h>
00057 #include <qasciidict.h>
00058
00059
00060
00061 using namespace Opie;
00062 using namespace Opie::Pim;
00063
00064 namespace Opie {
00065
00066 class OBackendPrivate;
00067
00083 template<class T>
00084 class OBackendFactory
00085 {
00086 public:
00087 OBackendFactory() {};
00088
00097 static T* create( OPimGlobal::PimType type, OPimGlobal::DatabaseStyle database,
00098 const QString& appName, const QString& filename = QString::null ){
00099 owarn << "Selected backend for " << type << " is: " <<
00100 database << oendl;
00101
00102
00103 OPimGlobal::DatabaseStyle use_database = database;
00104 if ( use_database == OPimGlobal::DEFAULT ){
00105 use_database = defaultDB( type );
00106 }
00107
00108 switch ( type ){
00109 case OPimGlobal::TODOLIST:
00110
00111 switch ( use_database ){
00112 default:
00113
00114 case OPimGlobal::SQL:
00115 #ifdef __USE_SQL
00116 return (T*) new OPimTodoAccessBackendSQL( filename );
00117 break;
00118 #else
00119 owarn << "OBackendFactory:: sql Backend for TODO not implemented! Using XML instead!" << oendl;
00120
00121 #endif
00122 case OPimGlobal::XML:
00123 return (T*) new OPimTodoAccessXML( appName, filename );
00124 break;
00125 case OPimGlobal::VCARD:
00126 return (T*) new OPimTodoAccessVCal( filename );
00127 break;
00128 }
00129 case OPimGlobal::CONTACTLIST:
00130 switch ( use_database ){
00131 default:
00132
00133 case OPimGlobal::SQL:
00134 #ifdef __USE_SQL
00135 return (T*) new OPimContactAccessBackend_SQL( appName, filename );
00136 break;
00137 #else
00138 owarn << "OBackendFactory:: sql Backend for CONTACT not implemented! Using XML instead!" << oendl;
00139
00140 #endif
00141 case OPimGlobal::XML:
00142 return (T*) new OPimContactAccessBackend_XML( appName, filename );
00143 break;
00144 case OPimGlobal::VCARD:
00145 return (T*) new OPimContactAccessBackend_VCard( appName, filename );
00146 break;
00147 }
00148 case OPimGlobal::DATEBOOK:
00149 switch ( use_database ){
00150 default:
00151
00152 case OPimGlobal::SQL:
00153 #ifdef __USE_SQL
00154 return (T*) new ODateBookAccessBackend_SQL( appName, filename );
00155 break;
00156 #else
00157 owarn << "OBackendFactory:: sql Backend for DATEBOOK not implemented! Using XML instead!" << oendl;
00158
00159 #endif
00160 case OPimGlobal::XML:
00161 return (T*) new ODateBookAccessBackend_XML( appName, filename );
00162 break;
00163 case OPimGlobal::VCARD:
00164 owarn << "OBackendFactory:: VCal Backend for DATEBOOK not implemented!" << oendl;
00165 return (T*) NULL;
00166 break;
00167 }
00168 default:
00169 return (T*) NULL;
00170 }
00171
00172 }
00173
00179 static OPimGlobal::DatabaseStyle defaultDB( OPimGlobal::PimType type ){
00180 QString group_name;
00181 switch ( type ){
00182 case OPimGlobal::TODOLIST:
00183 group_name = "todo";
00184 break;
00185 case OPimGlobal::CONTACTLIST:
00186 group_name = "contact";
00187 break;
00188 case OPimGlobal::DATEBOOK:
00189 group_name = "datebook";
00190 break;
00191 default:
00192 group_name = "unknown";
00193 }
00194
00195 Config config( "pimaccess" );
00196 config.setGroup ( group_name );
00197 QString db_String = config.readEntry( "usebackend", "xml" );
00198
00199 QAsciiDict<int> dictDbTypes( OPimGlobal::_END_DatabaseStyle );
00200 dictDbTypes.setAutoDelete( TRUE );
00201
00202 dictDbTypes.insert( "xml", new int (OPimGlobal::XML) );
00203 dictDbTypes.insert( "sql", new int (OPimGlobal::SQL) );
00204 dictDbTypes.insert( "vcard", new int (OPimGlobal::VCARD) );
00205
00206 int* db_find = dictDbTypes[ db_String ];
00207
00208 if ( !db_find )
00209 return OPimGlobal::UNKNOWN;
00210
00211 return (OPimGlobal::DatabaseStyle) *db_find;
00212 }
00213
00214
00222 static T* defaultBackend( OPimGlobal::PimType type, const QString& appName ){
00223 return create( type, OPimGlobal::DEFAULT, appName );
00224 }
00225 private:
00226 OBackendPrivate* d;
00227
00228 };
00229
00230 }
00231
00232 #endif