-rw-r--r-- | libopie2/opiepim/core/opimaccesstemplate.h | 144 | ||||
-rw-r--r-- | libopie2/opiepim/core/opimrecord.cpp | 6 | ||||
-rw-r--r-- | libopie2/opiepim/core/opimrecord.h | 2 | ||||
-rw-r--r-- | libopie2/opiepim/core/otemplatebase.h | 18 |
4 files changed, 116 insertions, 54 deletions
diff --git a/libopie2/opiepim/core/opimaccesstemplate.h b/libopie2/opiepim/core/opimaccesstemplate.h index f2a241d..e0708e1 100644 --- a/libopie2/opiepim/core/opimaccesstemplate.h +++ b/libopie2/opiepim/core/opimaccesstemplate.h | |||
@@ -1,82 +1,118 @@ | |||
1 | #ifndef OPIE_PIM_ACCESS_TEMPLATE_H | 1 | #ifndef OPIE_PIM_ACCESS_TEMPLATE_H |
2 | #define OPIE_PIM_ACCESS_TEMPLATE_H | 2 | #define OPIE_PIM_ACCESS_TEMPLATE_H |
3 | 3 | ||
4 | #include <qarray.h> | 4 | #include <qarray.h> |
5 | 5 | ||
6 | #include <opie/opimrecord.h> | 6 | #include <opie/opimrecord.h> |
7 | #include <opie/opimaccessbackend.h> | 7 | #include <opie/opimaccessbackend.h> |
8 | #include <opie/orecordlist.h> | 8 | #include <opie/orecordlist.h> |
9 | 9 | ||
10 | #include "otemplatebase.h" | ||
11 | |||
10 | template <class T = OPimRecord > | 12 | template <class T = OPimRecord > |
11 | class OPimAccessTemplate { | 13 | class OPimAccessTemplate : public OTemplateBase<T> { |
12 | public: | 14 | public: |
13 | typedef ORecordList<T> List; | 15 | typedef ORecordList<T> List; |
14 | typedef OPimAccessBackend<T> BackEnd; | 16 | typedef OPimAccessBackend<T> BackEnd; |
15 | OPimAccessTemplate( BackEnd* end) | 17 | OPimAccessTemplate( BackEnd* end); |
16 | : m_backEnd( end ) { | 18 | virtual ~OPimAccessTemplate(); |
17 | } | 19 | virtual void load(); |
18 | ~OPimAccessTemplate() { | 20 | virtual void reload(); |
19 | delete m_backEnd; | 21 | virtual void save(); |
20 | } | ||
21 | virtual void load() { | ||
22 | m_backEnd->load(); | ||
23 | } | ||
24 | virtual void reload() { | ||
25 | m_backEnd->reload(); | ||
26 | } | ||
27 | virtual void save() { | ||
28 | m_backEnd->save(); | ||
29 | } | ||
30 | 22 | ||
31 | /* | 23 | /* |
32 | *do array to Records conversion | 24 | *do array to Records conversion |
33 | * QArray<int> ids | 25 | * QArray<int> ids |
34 | */ | 26 | */ |
35 | virtual List allRecords()const { | 27 | virtual List allRecords()const; |
36 | QArray<int> ints = m_backEnd->allRecords(); | 28 | virtual List queryByExample( const T& t, int sortOrder ); |
37 | 29 | virtual T find( int uid ); | |
38 | List lis( ints, this ); | ||
39 | return lis; | ||
40 | } | ||
41 | virtual List queryByExample( const T& t, int sortOrder ) { | ||
42 | QArray<int> ints = m_backEnd->query( t, sortOrder ); | ||
43 | List lis( ints, this ); | ||
44 | |||
45 | return lis; | ||
46 | } | ||
47 | /* implement QCache here */ | ||
48 | virtual T find( int uid ) { | ||
49 | T t = m_backEnd->find( uid ); | ||
50 | return t; | ||
51 | } | ||
52 | 30 | ||
53 | /* invalidate cache here */ | 31 | /* invalidate cache here */ |
54 | virtual void clear() { | 32 | virtual void clear() ; |
55 | invalidateCache(); | 33 | virtual bool add( const T& t ) ; |
56 | m_backEnd->clear(); | ||
57 | } | ||
58 | virtual bool add( const T& t ) { | ||
59 | return m_backEnd->add( t ); | ||
60 | } | ||
61 | 34 | ||
62 | /* only the uid matters */ | 35 | /* only the uid matters */ |
63 | virtual bool remove( const T& t ) { | 36 | virtual bool remove( const T& t ); |
64 | /* remove from cache */ | 37 | virtual bool remove( int uid ); |
65 | return m_backEnd->remove( t.uid() ); | 38 | virtual bool replace( const T& t) ; |
66 | } | ||
67 | virtual bool remove( int uid ) { | ||
68 | /* remove from cache */ | ||
69 | return m_backEnd->remove(uid); | ||
70 | } | ||
71 | virtual bool replace( const T& t) { | ||
72 | return m_backEnd->replace( t ); | ||
73 | } | ||
74 | protected: | 39 | protected: |
75 | void invalidateCache() { | 40 | void invalidateCache(); |
76 | 41 | BackEnd* backEnd(); | |
77 | } | ||
78 | BackEnd* m_backEnd; | 42 | BackEnd* m_backEnd; |
79 | 43 | ||
80 | }; | 44 | }; |
81 | 45 | ||
46 | template <class T> | ||
47 | OPimAccessTemplate<T>::OPimAccessTemplate( BackEnd* end ) | ||
48 | : OTemplateBase<T>(), m_backEnd( end ) | ||
49 | { | ||
50 | |||
51 | } | ||
52 | template <class T> | ||
53 | OPimAccessTemplate<T>::~OPimAccessTemplate() { | ||
54 | qWarning("~OPimAccessTemplate<T>"); | ||
55 | delete m_backEnd; | ||
56 | } | ||
57 | template <class T> | ||
58 | void OPimAccessTemplate<T>::load() { | ||
59 | m_backEnd->load(); | ||
60 | } | ||
61 | template <class T> | ||
62 | void OPimAccessTemplate<T>::reload() { | ||
63 | m_backEnd->reload(); | ||
64 | } | ||
65 | template <class T> | ||
66 | void OPimAccessTemplate<T>::save() { | ||
67 | m_backEnd->save(); | ||
68 | } | ||
69 | template <class T> | ||
70 | OPimAccessTemplate<T>::List OPimAccessTemplate<T>::allRecords()const { | ||
71 | QArray<int> ints = m_backEnd->allRecords(); | ||
72 | List lis(ints, this ); | ||
73 | return lis; | ||
74 | } | ||
75 | template <class T> | ||
76 | OPimAccessTemplate<T>::List | ||
77 | OPimAccessTemplate<T>::queryByExample( const T& t, int sortOrder ) { | ||
78 | QArray<int> ints = m_backEnd->queryByExample( t, sortOrder ); | ||
79 | |||
80 | List lis(ints, this ); | ||
81 | return lis; | ||
82 | } | ||
83 | template <class T> | ||
84 | T OPimAccessTemplate<T>::find( int uid ) { | ||
85 | T t = m_backEnd->find( uid ); | ||
86 | return t; | ||
87 | } | ||
88 | template <class T> | ||
89 | void OPimAccessTemplate<T>::clear() { | ||
90 | invalidateCache(); | ||
91 | m_backEnd->clear(); | ||
92 | } | ||
93 | template <class T> | ||
94 | bool OPimAccessTemplate<T>::add( const T& t ) { | ||
95 | return m_backEnd->add( t ); | ||
96 | } | ||
97 | template <class T> | ||
98 | bool OPimAccessTemplate<T>::remove( const T& t ) { | ||
99 | return m_backEnd->remove( t.uid() ); | ||
100 | } | ||
101 | template <class T> | ||
102 | bool OPimAccessTemplate<T>::remove( int uid ) { | ||
103 | return m_backEnd->remove( uid ); | ||
104 | } | ||
105 | template <class T> | ||
106 | bool OPimAccessTemplate<T>::replace( const T& t ) { | ||
107 | return m_backEnd->replace( t ); | ||
108 | } | ||
109 | template <class T> | ||
110 | void OPimAccessTemplate<T>::invalidateCache() { | ||
111 | |||
112 | } | ||
113 | template <class T> | ||
114 | OPimAccessTemplate<T>::BackEnd* OPimAccessTemplate<T>::backEnd() { | ||
115 | return m_backEnd; | ||
116 | } | ||
117 | |||
82 | #endif | 118 | #endif |
diff --git a/libopie2/opiepim/core/opimrecord.cpp b/libopie2/opiepim/core/opimrecord.cpp index 88b6fde..60cdbf3 100644 --- a/libopie2/opiepim/core/opimrecord.cpp +++ b/libopie2/opiepim/core/opimrecord.cpp | |||
@@ -1,29 +1,32 @@ | |||
1 | #include <qpe/categories.h> | 1 | #include <qpe/categories.h> |
2 | #include <qpe/categoryselect.h> | 2 | #include <qpe/categoryselect.h> |
3 | 3 | ||
4 | #include "opimrecord.h" | 4 | #include "opimrecord.h" |
5 | 5 | ||
6 | Qtopia::UidGen OPimRecord::m_uidGen( Qtopia::UidGen::Qtopia ); | ||
7 | |||
8 | |||
6 | OPimRecord::OPimRecord( int uid ) | 9 | OPimRecord::OPimRecord( int uid ) |
7 | : Qtopia::Record() { | 10 | : Qtopia::Record() { |
8 | 11 | ||
9 | setUid( uid ); | 12 | setUid( uid ); |
10 | } | 13 | } |
11 | OPimRecord::~OPimRecord() { | 14 | OPimRecord::~OPimRecord() { |
12 | } | 15 | } |
13 | OPimRecord::OPimRecord( const OPimRecord& rec ) | 16 | OPimRecord::OPimRecord( const OPimRecord& rec ) |
14 | : Qtopia::Record( rec ) | 17 | : Qtopia::Record( rec ) |
15 | { | 18 | { |
16 | (*this) = rec; | 19 | (*this) = rec; |
17 | } | 20 | } |
18 | 21 | ||
19 | OPimRecord &OPimRecord::operator=( const OPimRecord& rec) { | 22 | OPimRecord &OPimRecord::operator=( const OPimRecord& rec) { |
20 | Qtopia::Record::operator=( rec ); | 23 | Qtopia::Record::operator=( rec ); |
21 | m_relations = rec.m_relations; | 24 | m_relations = rec.m_relations; |
22 | 25 | ||
23 | return *this; | 26 | return *this; |
24 | } | 27 | } |
25 | /* | 28 | /* |
26 | * category names | 29 | * category names |
27 | */ | 30 | */ |
28 | QStringList OPimRecord::categoryNames()const { | 31 | QStringList OPimRecord::categoryNames()const { |
29 | QStringList list; | 32 | QStringList list; |
@@ -104,24 +107,27 @@ void OPimRecord::setRelations( const QString& app, QArray<int> ids ) { | |||
104 | m_relations.replace( app, tmp ); | 107 | m_relations.replace( app, tmp ); |
105 | } | 108 | } |
106 | QString OPimRecord::crossToString()const { | 109 | QString OPimRecord::crossToString()const { |
107 | QString str; | 110 | QString str; |
108 | QMap<QString, QArray<int> >::ConstIterator it; | 111 | QMap<QString, QArray<int> >::ConstIterator it; |
109 | for (it = m_relations.begin(); it != m_relations.end(); ++it ) { | 112 | for (it = m_relations.begin(); it != m_relations.end(); ++it ) { |
110 | QArray<int> id = it.data(); | 113 | QArray<int> id = it.data(); |
111 | for ( uint i = 0; i < id.size(); ++i ) { | 114 | for ( uint i = 0; i < id.size(); ++i ) { |
112 | str += it.key() + "," + QString::number( i ) + ";"; | 115 | str += it.key() + "," + QString::number( i ) + ";"; |
113 | } | 116 | } |
114 | } | 117 | } |
115 | str = str.remove( str.length()-1, 1); // strip the ; | 118 | str = str.remove( str.length()-1, 1); // strip the ; |
116 | //qWarning("IDS " + str ); | 119 | //qWarning("IDS " + str ); |
117 | 120 | ||
118 | return str; | 121 | return str; |
119 | } | 122 | } |
120 | /* if uid = 1 assign a new one */ | 123 | /* if uid = 1 assign a new one */ |
121 | void OPimRecord::setUid( int uid ) { | 124 | void OPimRecord::setUid( int uid ) { |
122 | 125 | ||
123 | if ( uid == 1) | 126 | if ( uid == 1) |
124 | uid = uidGen().generate(); | 127 | uid = uidGen().generate(); |
125 | 128 | ||
126 | Qtopia::Record::setUid( uid ); | 129 | Qtopia::Record::setUid( uid ); |
127 | }; | 130 | }; |
131 | Qtopia::UidGen &OPimRecord::uidGen() { | ||
132 | return m_uidGen; | ||
133 | } | ||
diff --git a/libopie2/opiepim/core/opimrecord.h b/libopie2/opiepim/core/opimrecord.h index 297ff12..e4d33d6 100644 --- a/libopie2/opiepim/core/opimrecord.h +++ b/libopie2/opiepim/core/opimrecord.h | |||
@@ -83,36 +83,38 @@ public: | |||
83 | QStringList relatedApps()const; | 83 | QStringList relatedApps()const; |
84 | 84 | ||
85 | /** | 85 | /** |
86 | * the realtions between an app | 86 | * the realtions between an app |
87 | */ | 87 | */ |
88 | QArray<int> relations( const QString& app )const; | 88 | QArray<int> relations( const QString& app )const; |
89 | 89 | ||
90 | /** | 90 | /** |
91 | * | 91 | * |
92 | */ | 92 | */ |
93 | void clearRelation( const QString& app ); | 93 | void clearRelation( const QString& app ); |
94 | 94 | ||
95 | /** | 95 | /** |
96 | * | 96 | * |
97 | */ | 97 | */ |
98 | void addRelation( const QString& app, int id ); | 98 | void addRelation( const QString& app, int id ); |
99 | 99 | ||
100 | /** | 100 | /** |
101 | * | 101 | * |
102 | */ | 102 | */ |
103 | void setRelations( const QString&, QArray<int> ids ); | 103 | void setRelations( const QString&, QArray<int> ids ); |
104 | virtual void setUid( int uid ); | 104 | virtual void setUid( int uid ); |
105 | 105 | ||
106 | protected: | 106 | protected: |
107 | Qtopia::UidGen &uidGen(); | ||
107 | QString crossToString()const; | 108 | QString crossToString()const; |
108 | 109 | ||
109 | private: | 110 | private: |
110 | class OPimRecordPrivate; | 111 | class OPimRecordPrivate; |
111 | OPimRecordPrivate *d; | 112 | OPimRecordPrivate *d; |
112 | QMap<QString, QArray<int> > m_relations; | 113 | QMap<QString, QArray<int> > m_relations; |
114 | static Qtopia::UidGen m_uidGen; | ||
113 | 115 | ||
114 | }; | 116 | }; |
115 | 117 | ||
116 | 118 | ||
117 | 119 | ||
118 | #endif | 120 | #endif |
diff --git a/libopie2/opiepim/core/otemplatebase.h b/libopie2/opiepim/core/otemplatebase.h new file mode 100644 index 0000000..41cc934 --- a/dev/null +++ b/libopie2/opiepim/core/otemplatebase.h | |||
@@ -0,0 +1,18 @@ | |||
1 | #ifndef OPIE_TEMPLATE_BASE_H | ||
2 | #define OPIE_TEMPLATE_BASE_H | ||
3 | |||
4 | #include "opimrecord.h" | ||
5 | |||
6 | template <class T = OPimRecord> | ||
7 | class OTemplateBase { | ||
8 | public: | ||
9 | OTemplateBase() { | ||
10 | }; | ||
11 | virtual ~OTemplateBase() { | ||
12 | } | ||
13 | virtual T find( int uid ) = 0; | ||
14 | |||
15 | }; | ||
16 | |||
17 | |||
18 | #endif | ||