author | eilers <eilers> | 2004-05-16 16:21:51 (UTC) |
---|---|---|
committer | eilers <eilers> | 2004-05-16 16:21:51 (UTC) |
commit | 0c5c6279a7eaadf61bfe5f113a4f6cd3558c7914 (patch) (unidiff) | |
tree | bca776a6ab6d751bdbd9861f95c4dc5bc57752a0 | |
parent | 52691aca593d6b321597d26467b9e9af9ec0b6fe (diff) | |
download | opie-0c5c6279a7eaadf61bfe5f113a4f6cd3558c7914.zip opie-0c5c6279a7eaadf61bfe5f113a4f6cd3558c7914.tar.gz opie-0c5c6279a7eaadf61bfe5f113a4f6cd3558c7914.tar.bz2 |
Improved accessing of default database style by using OPimGlobal::DEFAULT
-rw-r--r-- | libopie2/opiepim/backend/obackendfactory.h | 30 | ||||
-rw-r--r-- | libopie2/opiepim/core/opimaccessfactory.h | 18 |
2 files changed, 38 insertions, 10 deletions
diff --git a/libopie2/opiepim/backend/obackendfactory.h b/libopie2/opiepim/backend/obackendfactory.h index d8caa80..993ecb4 100644 --- a/libopie2/opiepim/backend/obackendfactory.h +++ b/libopie2/opiepim/backend/obackendfactory.h | |||
@@ -24,172 +24,182 @@ | |||
24 | -- :-=` this library; see the file COPYING.LIB. | 24 | -- :-=` this library; see the file COPYING.LIB. |
25 | If not, write to the Free Software Foundation, | 25 | If not, write to the Free Software Foundation, |
26 | Inc., 59 Temple Place - Suite 330, | 26 | Inc., 59 Temple Place - Suite 330, |
27 | Boston, MA 02111-1307, USA. | 27 | Boston, MA 02111-1307, USA. |
28 | */ | 28 | */ |
29 | /* | 29 | /* |
30 | * ===================================================================== | 30 | * ===================================================================== |
31 | * ToDo: Use plugins | 31 | * ToDo: Use plugins |
32 | * ===================================================================== | 32 | * ===================================================================== |
33 | */ | 33 | */ |
34 | #ifndef OPIE_BACKENDFACTORY_H_ | 34 | #ifndef OPIE_BACKENDFACTORY_H_ |
35 | #define OPIE_BACKENDFACTORY_H_ | 35 | #define OPIE_BACKENDFACTORY_H_ |
36 | 36 | ||
37 | #include <qstring.h> | 37 | #include <qstring.h> |
38 | #include <qasciidict.h> | 38 | #include <qasciidict.h> |
39 | #include <qpe/config.h> | 39 | #include <qpe/config.h> |
40 | 40 | ||
41 | #include <opie2/opimglobal.h> | 41 | #include <opie2/opimglobal.h> |
42 | #include <opie2/otodoaccessxml.h> | 42 | #include <opie2/otodoaccessxml.h> |
43 | #include <opie2/ocontactaccessbackend_xml.h> | 43 | #include <opie2/ocontactaccessbackend_xml.h> |
44 | #include <opie2/odatebookaccessbackend_xml.h> | 44 | #include <opie2/odatebookaccessbackend_xml.h> |
45 | 45 | ||
46 | #ifdef __USE_SQL | 46 | #ifdef __USE_SQL |
47 | #include <opie2/otodoaccesssql.h> | 47 | #include <opie2/otodoaccesssql.h> |
48 | #include <opie2/ocontactaccessbackend_sql.h> | 48 | #include <opie2/ocontactaccessbackend_sql.h> |
49 | #include <opie2/odatebookaccessbackend_sql.h> | 49 | #include <opie2/odatebookaccessbackend_sql.h> |
50 | #endif | 50 | #endif |
51 | 51 | ||
52 | using namespace Opie; | 52 | using namespace Opie; |
53 | using namespace Opie::Pim; | 53 | using namespace Opie::Pim; |
54 | 54 | ||
55 | namespace Opie { | 55 | namespace Opie { |
56 | 56 | ||
57 | class OBackendPrivate; | 57 | class OBackendPrivate; |
58 | 58 | ||
59 | /** | 59 | /** |
60 | * This class is our factory. It will give us the default implementations | 60 | * This class is our factory. It will give us the default implementations |
61 | * of at least Todolist, Contacts and Datebook. In the future this class will | 61 | * of at least Todolist, Contacts and Datebook. In the future this class will |
62 | * allow users to switch the backend with ( XML->SQLite ) without the need | 62 | * allow users to switch the backend with ( XML->SQLite ) without the need |
63 | * to recompile.# | 63 | * to recompile.# |
64 | * This class - as the whole PIM Api - is making use of templates | 64 | * This class - as the whole PIM Api - is making use of templates |
65 | * | 65 | * |
66 | * <pre> | 66 | * <pre> |
67 | * OPimTodoAccessBackend* backend = OBackEndFactory<OPimTodoAccessBackend>::Default("todo", QString::null ); | 67 | * OPimTodoAccessBackend* backend = OBackEndFactory<OPimTodoAccessBackend>::Default("todo", QString::null ); |
68 | * backend->load(); | 68 | * backend->load(); |
69 | * </pre> | 69 | * </pre> |
70 | * | 70 | * |
71 | * @author Stefan Eilers | 71 | * @author Stefan Eilers |
72 | * @version 0.1 | 72 | * @version 0.1 |
73 | */ | 73 | */ |
74 | template<class T> | 74 | template<class T> |
75 | class OBackendFactory | 75 | class OBackendFactory |
76 | { | 76 | { |
77 | public: | 77 | public: |
78 | OBackendFactory() {}; | 78 | OBackendFactory() {}; |
79 | 79 | ||
80 | /** | 80 | /** |
81 | * Returns a selected backend implementation | 81 | * Returns a selected backend implementation |
82 | * @param type the type of the backend | 82 | * @param type the type of the backend |
83 | * @param database the type of the used database | 83 | * @param database the type of the used database |
84 | * @param appName The name of your application. It will be passed on to the backend. | 84 | * @param appName The name of your application. It will be passed on to the backend. |
85 | */ | 85 | */ |
86 | static T* create( OPimGlobal::PimType type, OPimGlobal::DatabaseStyle database, | 86 | static T* create( OPimGlobal::PimType type, OPimGlobal::DatabaseStyle database, |
87 | const QString& appName ){ | 87 | const QString& appName ){ |
88 | qWarning("Selected backend for %d is: %d", type, database ); | 88 | qWarning("Selected backend for %d is: %d", type, database ); |
89 | // If we should use the dafult database style, we have to request it | ||
90 | OPimGlobal::DatabaseStyle used_database = database; | ||
91 | if ( database == OPimGlobal::DEFAULT ){ | ||
92 | used_database = defaultDB( type ); | ||
93 | } | ||
94 | |||
89 | 95 | ||
90 | switch ( type ){ | 96 | switch ( type ){ |
91 | case OPimGlobal::TODOLIST: | 97 | case OPimGlobal::TODOLIST: |
92 | #ifdef __USE_SQL | 98 | #ifdef __USE_SQL |
93 | if ( database == OPimGlobal::SQL ) | 99 | if ( used_database == OPimGlobal::SQL ) |
94 | return (T*) new OPimTodoAccessBackendSQL(""); | 100 | return (T*) new OPimTodoAccessBackendSQL(""); |
95 | #else | 101 | #else |
96 | if ( database == OPimGlobal::SQL ) | 102 | if ( used_database == OPimGlobal::SQL ) |
97 | qWarning ("OBackendFactory:: sql Backend for TODO not implemented! Using XML instead!"); | 103 | qWarning ("OBackendFactory:: sql Backend for TODO not implemented! Using XML instead!"); |
98 | #endif | 104 | #endif |
99 | 105 | ||
100 | return (T*) new OPimTodoAccessXML( appName ); | 106 | return (T*) new OPimTodoAccessXML( appName ); |
101 | case OPimGlobal::CONTACTLIST: | 107 | case OPimGlobal::CONTACTLIST: |
102 | #ifdef __USE_SQL | 108 | #ifdef __USE_SQL |
103 | if ( database == OPimGlobal::SQL ) | 109 | if ( used_database == OPimGlobal::SQL ) |
104 | return (T*) new OPimContactAccessBackend_SQL(""); | 110 | return (T*) new OPimContactAccessBackend_SQL(""); |
105 | #else | 111 | #else |
106 | if ( database == OPimGlobal::SQL ) | 112 | if ( used_database == OPimGlobal::SQL ) |
107 | qWarning ("OBackendFactory:: sql Backend for CONTACT not implemented! Using XML instead!"); | 113 | qWarning ("OBackendFactory:: sql Backend for CONTACT not implemented! Using XML instead!"); |
108 | #endif | 114 | #endif |
109 | 115 | ||
110 | return (T*) new OPimContactAccessBackend_XML( appName ); | 116 | return (T*) new OPimContactAccessBackend_XML( appName ); |
111 | case OPimGlobal::DATEBOOK: | 117 | case OPimGlobal::DATEBOOK: |
112 | #ifdef __USE_SQL | 118 | #ifdef __USE_SQL |
113 | if ( database == OPimGlobal::SQL ) | 119 | if ( used_database == OPimGlobal::SQL ) |
114 | return (T*) new ODateBookAccessBackend_SQL(""); | 120 | return (T*) new ODateBookAccessBackend_SQL(""); |
115 | #else | 121 | #else |
116 | if ( database == OPimGlobal::SQL ) | 122 | if ( used_database == OPimGlobal::SQL ) |
117 | qWarning("OBackendFactory:: sql Backend for DATEBOOK not implemented! Using XML instead!"); | 123 | qWarning("OBackendFactory:: sql Backend for DATEBOOK not implemented! Using XML instead!"); |
118 | #endif | 124 | #endif |
119 | 125 | ||
120 | return (T*) new ODateBookAccessBackend_XML( appName ); | 126 | return (T*) new ODateBookAccessBackend_XML( appName ); |
121 | default: | 127 | default: |
122 | return (T*) NULL; | 128 | return (T*) NULL; |
123 | } | 129 | } |
124 | 130 | ||
125 | 131 | ||
126 | } | 132 | } |
127 | 133 | ||
128 | 134 | /** | |
129 | static OPimGlobal::DatabaseStyle defaultDB( OPimGlobal::PimType backend ){ | 135 | * Returns the style of the default database which is used to contact PIM data. |
136 | * @param type the type of the backend | ||
137 | * @see OPimGlobal | ||
138 | */ | ||
139 | static OPimGlobal::DatabaseStyle defaultDB( OPimGlobal::PimType type ){ | ||
130 | QString group_name; | 140 | QString group_name; |
131 | switch ( backend ){ | 141 | switch ( type ){ |
132 | case OPimGlobal::TODOLIST: | 142 | case OPimGlobal::TODOLIST: |
133 | group_name = "todo"; | 143 | group_name = "todo"; |
134 | break; | 144 | break; |
135 | case OPimGlobal::CONTACTLIST: | 145 | case OPimGlobal::CONTACTLIST: |
136 | group_name = "contact"; | 146 | group_name = "contact"; |
137 | break; | 147 | break; |
138 | case OPimGlobal::DATEBOOK: | 148 | case OPimGlobal::DATEBOOK: |
139 | group_name = "datebook"; | 149 | group_name = "datebook"; |
140 | break; | 150 | break; |
141 | default: | 151 | default: |
142 | group_name = "unknown"; | 152 | group_name = "unknown"; |
143 | } | 153 | } |
144 | 154 | ||
145 | Config config( "pimaccess" ); | 155 | Config config( "pimaccess" ); |
146 | config.setGroup ( group_name ); | 156 | config.setGroup ( group_name ); |
147 | QString db_String = config.readEntry( "usebackend" ); | 157 | QString db_String = config.readEntry( "usebackend" ); |
148 | 158 | ||
149 | QAsciiDict<int> dictDbTypes( OPimGlobal::_END_DatabaseStyle ); | 159 | QAsciiDict<int> dictDbTypes( OPimGlobal::_END_DatabaseStyle ); |
150 | dictDbTypes.setAutoDelete( TRUE ); | 160 | dictDbTypes.setAutoDelete( TRUE ); |
151 | 161 | ||
152 | dictDbTypes.insert( "xml", new int (OPimGlobal::XML) ); | 162 | dictDbTypes.insert( "xml", new int (OPimGlobal::XML) ); |
153 | dictDbTypes.insert( "sql", new int (OPimGlobal::SQL) ); | 163 | dictDbTypes.insert( "sql", new int (OPimGlobal::SQL) ); |
154 | dictDbTypes.insert( "vcard", new int (OPimGlobal::VCARD) ); | 164 | dictDbTypes.insert( "vcard", new int (OPimGlobal::VCARD) ); |
155 | 165 | ||
156 | int* db_find = dictDbTypes[ db_String ]; | 166 | int* db_find = dictDbTypes[ db_String ]; |
157 | 167 | ||
158 | if ( !db_find ) | 168 | if ( !db_find ) |
159 | return OPimGlobal::UNKNOWN; | 169 | return OPimGlobal::UNKNOWN; |
160 | 170 | ||
161 | return (OPimGlobal::DatabaseStyle) *db_find; | 171 | return (OPimGlobal::DatabaseStyle) *db_find; |
162 | } | 172 | } |
163 | 173 | ||
164 | 174 | ||
165 | /** | 175 | /** |
166 | * Returns the default backend implementation for backendName. Which one is used, is defined | 176 | * Returns the default backend implementation for backendName. Which one is used, is defined |
167 | * by the configfile "pimaccess.conf". | 177 | * by the configfile "pimaccess.conf". |
168 | * @param backendName the type of the backend (use "todo", "contact" or "datebook" ) | 178 | * @param backendName the type of the backend (use "todo", "contact" or "datebook" ) |
169 | * @param appName The name of your application. It will be passed on to the backend | 179 | * @param appName The name of your application. It will be passed on to the backend |
170 | */ | 180 | */ |
171 | static T* Default( const QString backendName, const QString& appName ){ | 181 | static T* Default( const QString backendName, const QString& appName ){ |
172 | 182 | ||
173 | QAsciiDict<int> dictBackends( OPimGlobal::_END_PimType ); | 183 | QAsciiDict<int> dictBackends( OPimGlobal::_END_PimType ); |
174 | dictBackends.setAutoDelete ( TRUE ); | 184 | dictBackends.setAutoDelete ( TRUE ); |
175 | 185 | ||
176 | dictBackends.insert( "todo", new int (OPimGlobal::TODOLIST) ); | 186 | dictBackends.insert( "todo", new int (OPimGlobal::TODOLIST) ); |
177 | dictBackends.insert( "contact", new int (OPimGlobal::CONTACTLIST) ); | 187 | dictBackends.insert( "contact", new int (OPimGlobal::CONTACTLIST) ); |
178 | dictBackends.insert( "datebook", new int(OPimGlobal::DATEBOOK) ); | 188 | dictBackends.insert( "datebook", new int(OPimGlobal::DATEBOOK) ); |
179 | 189 | ||
180 | int* backend_find = dictBackends[ backendName ]; | 190 | int* backend_find = dictBackends[ backendName ]; |
181 | if ( !backend_find ) return NULL; | 191 | if ( !backend_find ) return NULL; |
182 | 192 | ||
183 | OPimGlobal::DatabaseStyle style = defaultDB( static_cast<OPimGlobal::PimType>( *backend_find ) ); | 193 | OPimGlobal::DatabaseStyle style = defaultDB( static_cast<OPimGlobal::PimType>( *backend_find ) ); |
184 | 194 | ||
185 | qDebug( "OBackendFactory::Default -> Backend is %s, Database is %d", backendName.latin1(), | 195 | qDebug( "OBackendFactory::Default -> Backend is %s, Database is %d", backendName.latin1(), |
186 | style ); | 196 | style ); |
187 | 197 | ||
188 | return create( (OPimGlobal::PimType) *backend_find, style, appName ); | 198 | return create( (OPimGlobal::PimType) *backend_find, style, appName ); |
189 | 199 | ||
190 | } | 200 | } |
191 | private: | 201 | private: |
192 | OBackendPrivate* d; | 202 | OBackendPrivate* d; |
193 | 203 | ||
194 | }; | 204 | }; |
195 | 205 | ||
diff --git a/libopie2/opiepim/core/opimaccessfactory.h b/libopie2/opiepim/core/opimaccessfactory.h index ba99a15..eecfa96 100644 --- a/libopie2/opiepim/core/opimaccessfactory.h +++ b/libopie2/opiepim/core/opimaccessfactory.h | |||
@@ -31,70 +31,88 @@ | |||
31 | #define __OPIE_OPIMACCESSFACTORY_H_ | 31 | #define __OPIE_OPIMACCESSFACTORY_H_ |
32 | 32 | ||
33 | #include <opie2/opimglobal.h> | 33 | #include <opie2/opimglobal.h> |
34 | #include <opie2/obackendfactory.h> | 34 | #include <opie2/obackendfactory.h> |
35 | #include <opie2/ocontactaccess.h> | 35 | #include <opie2/ocontactaccess.h> |
36 | #include <opie2/otodoaccess.h> | 36 | #include <opie2/otodoaccess.h> |
37 | #include <opie2/odatebookaccess.h> | 37 | #include <opie2/odatebookaccess.h> |
38 | 38 | ||
39 | using namespace Opie::Pim; | 39 | using namespace Opie::Pim; |
40 | 40 | ||
41 | namespace Opie { | 41 | namespace Opie { |
42 | 42 | ||
43 | 43 | ||
44 | /** | 44 | /** |
45 | * This class is our factory for creating PIM access objects. You should use these objects to load, store and search | 45 | * This class is our factory for creating PIM access objects. You should use these objects to load, store and search |
46 | * information in our PIM databases. Currently we support objects for accessing the datebook, the contact- and the | 46 | * information in our PIM databases. Currently we support objects for accessing the datebook, the contact- and the |
47 | * todolist. | 47 | * todolist. |
48 | * @see OPimGlobal for more information of used values. | 48 | * @see OPimGlobal for more information of used values. |
49 | * | 49 | * |
50 | * This class - as the whole PIM Api - is making use of templates | 50 | * This class - as the whole PIM Api - is making use of templates |
51 | * | 51 | * |
52 | * Example for getting an access object for the XML database of todolist: | 52 | * Example for getting an access object for the XML database of todolist: |
53 | * <pre> | 53 | * <pre> |
54 | * OPimTodoAccess* access = OPimAccessFactory<OPimTodoAccess>::create( OPimGlobal::TODOLIST, OPimGlobal::XML, "example" ); | 54 | * OPimTodoAccess* access = OPimAccessFactory<OPimTodoAccess>::create( OPimGlobal::TODOLIST, OPimGlobal::XML, "example" ); |
55 | * </pre> | 55 | * </pre> |
56 | * | 56 | * |
57 | * @author Stefan Eilers | 57 | * @author Stefan Eilers |
58 | * @version 0.1 | 58 | * @version 0.1 |
59 | */ | 59 | */ |
60 | */ | 60 | */ |
61 | 61 | ||
62 | template<class T> | 62 | template<class T> |
63 | class OPimAccessFactory | 63 | class OPimAccessFactory |
64 | { | 64 | { |
65 | public: | 65 | public: |
66 | 66 | ||
67 | // Maybe we should introduce a global class for storing such global enums | 67 | // Maybe we should introduce a global class for storing such global enums |
68 | // (something like opimglobal.h) ? (eilers) | 68 | // (something like opimglobal.h) ? (eilers) |
69 | 69 | ||
70 | OPimAccessFactory() {}; | 70 | OPimAccessFactory() {}; |
71 | 71 | ||
72 | /** | 72 | /** |
73 | * Returns the selected PIM access-object. | 73 | * Returns the selected PIM access-object. |
74 | * @param type Type of the selected database (addressbook, todolist or datebook) | 74 | * @param type Type of the selected database (addressbook, todolist or datebook) |
75 | * @param dbStyle Which database style should be used (xml, sql, vcard) | 75 | * @param dbStyle Which database style should be used (xml, sql, vcard) |
76 | * @param appName "Name" of your application. This should be any constant string which is used | 76 | * @param appName "Name" of your application. This should be any constant string which is used |
77 | * by some backends for creating special files (i.e.journal files). Please keep the | 77 | * by some backends for creating special files (i.e.journal files). Please keep the |
78 | * string unique for your application ! | 78 | * string unique for your application ! |
79 | * @see OPimGlobal | 79 | * @see OPimGlobal |
80 | */ | 80 | */ |
81 | static T* create( OPimGlobal::PimType type, OPimGlobal::DatabaseStyle dbStyle, const QString& appName ){ | 81 | static T* create( OPimGlobal::PimType type, OPimGlobal::DatabaseStyle dbStyle, const QString& appName ){ |
82 | 82 | ||
83 | switch ( type ){ | 83 | switch ( type ){ |
84 | case OPimGlobal::TODOLIST: | 84 | case OPimGlobal::TODOLIST: |
85 | return dynamic_cast<T*>( new OPimTodoAccess( OBackendFactory<OPimTodoAccessBackend>::create( type, dbStyle, appName ) ) ); | 85 | return dynamic_cast<T*>( new OPimTodoAccess( OBackendFactory<OPimTodoAccessBackend>::create( type, dbStyle, appName ) ) ); |
86 | case OPimGlobal::CONTACTLIST: | 86 | case OPimGlobal::CONTACTLIST: |
87 | return dynamic_cast<T*>( new OPimContactAccess( QString::null, QString::null, OBackendFactory<OPimContactAccessBackend>::create( type, dbStyle, appName ) ) ); | 87 | return dynamic_cast<T*>( new OPimContactAccess( QString::null, QString::null, OBackendFactory<OPimContactAccessBackend>::create( type, dbStyle, appName ) ) ); |
88 | case OPimGlobal::DATEBOOK: | 88 | case OPimGlobal::DATEBOOK: |
89 | return dynamic_cast<T*>( new ODateBookAccess( OBackendFactory<ODateBookAccessBackend>::create( type, dbStyle, appName ) ) ); | 89 | return dynamic_cast<T*>( new ODateBookAccess( OBackendFactory<ODateBookAccessBackend>::create( type, dbStyle, appName ) ) ); |
90 | default: | 90 | default: |
91 | return 0l; | 91 | return 0l; |
92 | 92 | ||
93 | } | 93 | } |
94 | } | 94 | } |
95 | |||
96 | |||
97 | /** | ||
98 | * Returns the selected PIM access-object, using the default database style | ||
99 | * Which style is selected is defined in the configfile "pimaccess.conf" in | ||
100 | * the directory "Settings" | ||
101 | * @param type Type of the selected database (addressbook, todolist or datebook) | ||
102 | * @param appName "Name" of your application. This should be any constant string which is used | ||
103 | * by some backends for creating special files (i.e.journal files). Please keep the | ||
104 | * string unique for your application ! | ||
105 | * @see OPimGlobal | ||
106 | * | ||
107 | */ | ||
108 | static T* default( OPimGlobal::PimType type, const QString& appName ){ | ||
109 | |||
110 | return create( type, OPimGlobal::DEFAULT, appName ) | ||
111 | |||
112 | } | ||
95 | }; | 113 | }; |
96 | 114 | ||
97 | } | 115 | } |
98 | 116 | ||
99 | 117 | ||
100 | #endif | 118 | #endif |