summaryrefslogtreecommitdiff
path: root/core/pim/addressbook/abview.cpp
Unidiff
Diffstat (limited to 'core/pim/addressbook/abview.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/pim/addressbook/abview.cpp186
1 files changed, 150 insertions, 36 deletions
diff --git a/core/pim/addressbook/abview.cpp b/core/pim/addressbook/abview.cpp
index d35e392..8d22129 100644
--- a/core/pim/addressbook/abview.cpp
+++ b/core/pim/addressbook/abview.cpp
@@ -1,212 +1,316 @@
1#include "abview.h" 1#include "abview.h"
2 2
3#include <qlayout.h>
4
5#include <qpe/global.h>
6
7#include <opie/ocontactaccessbackend_vcard.h>
8
9
3// Is defined in LibQPE 10// Is defined in LibQPE
4extern QString categoryFileName(); 11extern QString categoryFileName();
5 12
6#include <qlayout.h> 13QString addressbookPersonalVCardName()
14{
15 QString filename = Global::applicationFileName("addressbook",
16 "businesscard.vcf");
17 return filename;
18}
7 19
8AbView::AbView ( QWidget* parent, const QValueList<int>& ordered, const QStringList& slOrderedFields ): 20
21AbView::AbView ( QWidget* parent, const QValueList<int>& ordered ):
9 QWidget(parent), 22 QWidget(parent),
10 mCat(0), 23 mCat(0),
11 m_inSearch( false ), 24 m_inSearch( false ),
12 m_curr_category( 0 ), 25 m_inPersonal( false ),
26 m_curr_category( -1 ),
13 m_curr_View( TableView ), 27 m_curr_View( TableView ),
14 m_prev_View( TableView ), 28 m_prev_View( TableView ),
15 m_curr_Contact ( 0 ), 29 m_curr_Contact ( 0 ),
16 m_contactdb ( "addressbook", 0l, 0l, false ), // Handle syncing myself.. ! 30 m_contactdb ( 0l ),
31 m_storedDB ( 0l ),
17 m_viewStack( 0l ), 32 m_viewStack( 0l ),
18 m_abTable( 0l ), 33 m_abTable( 0l ),
19 m_orderedFields( ordered ), 34 m_orderedFields( ordered )
20 m_slOrderedFields( slOrderedFields )
21{ 35{
36 // Load default database and handle syncing myself.. !
37 m_contactdb = new OContactAccess ( "addressbook", 0l, 0l, false ),
22 mCat.load( categoryFileName() ); 38 mCat.load( categoryFileName() );
23 39
24 // Create Layout and put WidgetStack into it. 40 // Create Layout and put WidgetStack into it.
25 QVBoxLayout *vb = new QVBoxLayout( this ); 41 QVBoxLayout *vb = new QVBoxLayout( this );
26 m_viewStack = new QWidgetStack( this ); 42 m_viewStack = new QWidgetStack( this );
27 vb->addWidget( m_viewStack ); 43 vb->addWidget( m_viewStack );
28 44
29 // Creat TableView 45 // Creat TableView
30 QVBox* tableBox = new QVBox( m_viewStack ); 46 QVBox* tableBox = new QVBox( m_viewStack );
31 m_abTable = new AbTable( &m_orderedFields, tableBox, "table" ); 47 m_abTable = new AbTable( m_orderedFields, tableBox, "table" );
32 m_abTable->setCurrentCell( 0, 0 ); 48 m_abTable->setCurrentCell( 0, 0 );
33 m_abTable->setFocus(); 49 m_abTable->setFocus();
34 50
35 // Add TableView to WidgetStack and raise it 51 // Add TableView to WidgetStack and raise it
36 m_viewStack -> addWidget( tableBox , TableView ); 52 m_viewStack -> addWidget( tableBox , TableView );
37 53
38 // Create CardView and add it to WidgetStack 54 // Create CardView and add it to WidgetStack
39 QVBox* cardBox = new QVBox( m_viewStack ); 55 QVBox* cardBox = new QVBox( m_viewStack );
40 m_ablabel = new AbLabel( cardBox, "CardView"); 56 m_ablabel = new AbLabel( cardBox, "CardView");
41 m_viewStack -> addWidget( cardBox , CardView ); 57 m_viewStack -> addWidget( cardBox , CardView );
42 58
43 // Connect views to me 59 // Connect views to me
44 connect ( m_abTable, SIGNAL( signalSwitch( void ) ), 60 connect ( m_abTable, SIGNAL( signalSwitch( void ) ),
45 this, SLOT( slotSwitch( void ) ) ); 61 this, SLOT( slotSwitch( void ) ) );
46 connect ( m_ablabel, SIGNAL( signalOkPressed( void ) ), 62 connect ( m_ablabel, SIGNAL( signalOkPressed( void ) ),
47 this, SLOT( slotSwitch( void ) ) ); 63 this, SLOT( slotSwitch( void ) ) );
48 64
49 load(); 65 load();
50} 66}
51 67
68AbView::~AbView()
69{
70 m_contactdb -> save();
71 delete m_contactdb;
72
73 if ( m_storedDB ){
74 m_storedDB -> save();
75 delete m_storedDB;
76 }
77}
78
79
52void AbView::setView( Views view ) 80void AbView::setView( Views view )
53{ 81{
54 qWarning("AbView::setView( Views view )"); 82 qWarning("AbView::setView( Views view )");
55 m_curr_View = view; 83 m_curr_View = view;
56 load(); 84 load();
57} 85}
58 86
59void AbView::addEntry( const OContact &newContact ) 87void AbView::addEntry( const OContact &newContact )
60{ 88{
61 qWarning("abview:AddContact"); 89 qWarning("abview:AddContact");
62 m_contactdb.add ( newContact ); 90 m_contactdb->add ( newContact );
63 load(); 91 load();
64 92
65} 93}
66void AbView::removeEntry( const int UID ) 94void AbView::removeEntry( const int UID )
67{ 95{
68 qWarning("abview:RemoveContact"); 96 qWarning("abview:RemoveContact");
69 m_contactdb.remove( UID ); 97 m_contactdb->remove( UID );
70 load(); 98 load();
71} 99}
72 100
73void AbView::replaceEntry( const OContact &contact ) 101void AbView::replaceEntry( const OContact &contact )
74{ 102{
75 qWarning("abview:ReplaceContact"); 103 qWarning("abview:ReplaceContact");
76 m_contactdb.replace( contact ); 104 m_contactdb->replace( contact );
77 load(); 105 load();
78 106
79} 107}
80 108
81OContact AbView::currentEntry() 109OContact AbView::currentEntry()
82{ 110{
83 switch ( (int) m_curr_View ) { 111 switch ( (int) m_curr_View ) {
84 case TableView: 112 case TableView:
85 return ( m_abTable -> currentEntry() ); 113 return ( m_abTable -> currentEntry() );
86 break; 114 break;
87 case CardView: 115 case CardView:
88 return ( m_ablabel -> currentEntry() ); 116 return ( m_ablabel -> currentEntry() );
89 break; 117 break;
90 } 118 }
91 return OContact(); 119 return OContact();
92} 120}
93 121
94bool AbView::save() 122bool AbView::save()
95{ 123{
96 qWarning("abView:Save data"); 124 qWarning("abView:Save data");
97 125
98 return m_contactdb.save(); 126 return m_contactdb->save();
99} 127}
100 128
101// :SX Add: Just load for specific Category
102void AbView::load() 129void AbView::load()
103{ 130{
104 qWarning("abView:Load data"); 131 qWarning("abView:Load data");
105 132
106 m_list = m_contactdb.allRecords(); 133 if ( m_inPersonal )
134 m_list = m_contactdb->allRecords();
135 else
136 m_list = m_contactdb->sorted( true, 0, 0, 0 );
137
107 clearForCategory(); 138 clearForCategory();
108 m_curr_Contact = 0; 139
140 // Feed all views with new lists
141 updateListinViews();
109 142
110 qWarning ("Number of contacts: %d", m_list.count()); 143 qWarning ("Number of contacts: %d", m_list.count());
111 144
112 updateView(); 145 updateView();
113 146
114} 147}
115 148
116void AbView::reload() 149void AbView::reload()
117{ 150{
118 m_contactdb.reload(); 151 qWarning( "void AbView::reload()" );
152
153 m_contactdb->reload();
119 load(); 154 load();
120} 155}
121 156
122void AbView::clear() 157void AbView::clear()
123{ 158{
124 // :SX 159 // :SX
125} 160}
126 161
127void AbView::setShowByCategory( Views view, const QString& cat ) 162void AbView::setShowByCategory( Views view, const QString& cat )
128{ 163{
129 qWarning("AbView::setShowCategory( Views view, const QString& cat )"); 164 qWarning("AbView::setShowCategory( Views view, const QString& cat )");
130 m_curr_View = view;
131 165
132 emit signalClearLetterPicker(); 166 // if ( view == PersonalView ){
167 // if ( ! m_inPersonal )
168 // showPersonal( true );
169
170 // }else{
171 // if ( m_inPersonal )
172 // showPersonal( false );
133 173
134 if ( !cat.isNull() ) 174 // m_curr_View = view;
135 m_curr_category = mCat.id("Contacts", cat ); 175 // }
176
177 int intCat = 0;
178
179 // All (cat == NULL) will be stored as -1
180 if ( cat.isNull() )
181 intCat = -1;
136 else 182 else
137 m_curr_category = -1; // Set to all 183 intCat = mCat.id("Contacts", cat );
138 184
139 qWarning ("Categories: Selected %s.. Number: %d", cat.latin1(), m_curr_category); 185 // If we just change the view, we don't have to reload any data..
186 // This speeds up a lot of things !
187 if ( intCat == m_curr_category ){
188 qWarning ("Just change the View (Category is: %d)", m_curr_category);
189 m_prev_View = m_curr_View;
190 m_curr_View = view;
140 191
141 load(); 192 updateView();
193 }else{
194 qWarning ("Categories: Selected %s.. Number: %d", cat.latin1(), m_curr_category);
195
196 m_curr_View = view;
197 m_curr_category = intCat;
198 emit signalClearLetterPicker();
199
200 load();
201 }
142 202
143} 203}
144void AbView::setShowByLetter( char c ) 204void AbView::setShowByLetter( char c )
145{ 205{
146 qWarning("void AbView::setShowByLetter( %c )", c ); 206 qWarning("void AbView::setShowByLetter( %c )", c );
147 OContact query; 207 OContact query;
148 if ( c == 0 ){ 208 if ( c == 0 ){
149 load(); 209 load();
150 return; 210 return;
151 }else{ 211 }else{
152 query.setLastName( QString("%1*").arg(c) ); 212 query.setLastName( QString("%1*").arg(c) );
153 m_list = m_contactdb.queryByExample( query, OContactAccess::WildCards ); 213 m_list = m_contactdb->queryByExample( query, OContactAccess::WildCards );
154 clearForCategory(); 214 clearForCategory();
155 m_curr_Contact = 0; 215 m_curr_Contact = 0;
156 } 216 }
157 updateView(); 217 updateView();
158} 218}
159 219
220void AbView::setListOrder( const QValueList<int>& ordered )
221{
222 m_orderedFields = ordered;
223 updateView();
224}
225
226
160QString AbView::showCategory() const 227QString AbView::showCategory() const
161{ 228{
162 return mCat.label( "Contacts", m_curr_category ); 229 return mCat.label( "Contacts", m_curr_category );
163} 230}
164 231
165void AbView::showContact( const OContact& cnt ) 232void AbView::showPersonal( bool personal )
166{ 233{
167 qWarning ("void AbView::showContact( const OContact& cnt )"); 234 qWarning ("void AbView::showPersonal( %d )", personal);
168 // :SX 235
236 if ( personal ){
237
238 if ( m_inPersonal )
239 return;
240
241 // Now switch to vCard Backend and load data.
242 // The current default backend will be stored
243 // to avoid unneeded load/stores.
244 m_storedDB = m_contactdb;
245
246 OContactAccessBackend* vcard_backend = new OContactAccessBackend_VCard( QString::null,
247 addressbookPersonalVCardName() );
248 m_contactdb = new OContactAccess ( "addressbook", QString::null , vcard_backend, true );
249
250 m_inPersonal = true;
251 m_curr_View = CardView;
252
253 }else{
254
255 if ( !m_inPersonal )
256 return;
257
258 // Remove vCard Backend and restore default
259 m_contactdb->save();
260 delete m_contactdb;
261
262 m_contactdb = m_storedDB;
263 m_storedDB = 0l;
264
265 m_curr_View = TableView;
266 m_inPersonal = false;
267
268 }
269 load();
169} 270}
170 271
171QStringList AbView::categories() 272QStringList AbView::categories()
172{ 273{
173 mCat.load( categoryFileName() ); 274 mCat.load( categoryFileName() );
174 QStringList categoryList = mCat.labels( "Contacts" ); 275 QStringList categoryList = mCat.labels( "Contacts" );
175 return categoryList; 276 return categoryList;
176} 277}
177 278
178// BEGIN: Slots 279// BEGIN: Slots
179void AbView::slotDoFind( const QString &str, bool caseSensitive, bool useRegExp, 280void AbView::slotDoFind( const QString &str, bool caseSensitive, bool useRegExp,
180 bool , QString cat = QString::null ) 281 bool , QString cat = QString::null )
181{ 282{
182 qWarning( "void AbView::slotDoFind" ); 283 qWarning( "void AbView::slotDoFind" );
183 // Use the current Category if nothing else selected
184 284
285 // We reloading the data: Deselect Letterpicker
286 emit signalClearLetterPicker();
287
288 // Use the current Category if nothing else selected
185 int category = 0; 289 int category = 0;
186 290
187 if ( cat.isEmpty() ) 291 if ( cat.isEmpty() )
188 category = m_curr_category; 292 category = m_curr_category;
189 else{ 293 else{
190 category = mCat.id("Contacts", cat ); 294 category = mCat.id("Contacts", cat );
191 } 295 }
192 296
193 qWarning ("Find in Category %d", category); 297 qWarning ("Find in Category %d", category);
194 298
195 QRegExp r( str ); 299 QRegExp r( str );
196 r.setCaseSensitive( caseSensitive ); 300 r.setCaseSensitive( caseSensitive );
197 r.setWildcard( !useRegExp ); 301 r.setWildcard( !useRegExp );
198 302
199 // Get all matching entries out of the database 303 // Get all matching entries out of the database
200 m_list = m_contactdb.matchRegexp( r ); 304 m_list = m_contactdb->matchRegexp( r );
201 305
202 qWarning( "found: %d", m_list.count() ); 306 qWarning( "found: %d", m_list.count() );
203 if ( m_list.count() == 0 ){ 307 if ( m_list.count() == 0 ){
204 emit signalNotFound(); 308 emit signalNotFound();
205 return; 309 return;
206 } 310 }
207 311
208 // Now remove all contacts with wrong category (if any selected) 312 // Now remove all contacts with wrong category (if any selected)
209 // This algorithm is a litte bit ineffective, but 313 // This algorithm is a litte bit ineffective, but
210 // we will not have a lot of matching entries.. 314 // we will not have a lot of matching entries..
211 clearForCategory(); 315 clearForCategory();
212 316
@@ -236,96 +340,106 @@ void AbView::slotSwitch(){
236 break; 340 break;
237 } 341 }
238 updateView(); 342 updateView();
239 343
240} 344}
241 345
242// END: Slots 346// END: Slots
243 347
244void AbView::clearForCategory() 348void AbView::clearForCategory()
245{ 349{
246 OContactAccess::List::Iterator it; 350 OContactAccess::List::Iterator it;
247 // Now remove all contacts with wrong category if any category selected 351 // Now remove all contacts with wrong category if any category selected
248 // This algorithm is a litte bit ineffective 352
353 OContactAccess::List allList = m_list;
249 if ( m_curr_category != -1 ){ 354 if ( m_curr_category != -1 ){
250 for ( it = m_list.begin(); it != m_list.end(); ++it ){ 355 for ( it = allList.begin(); it != allList.end(); ++it ){
251 if ( !contactCompare( *it, m_curr_category ) ){ 356 if ( !contactCompare( *it, m_curr_category ) ){
252 qWarning("Removing %d", (*it).uid()); 357 qWarning("Removing %d", (*it).uid());
253 m_list.remove( (*it).uid() ); 358 m_list.remove( (*it).uid() );
254 it = m_list.begin();
255 } 359 }
256 } 360 }
257 } 361 }
362
258} 363}
259 364
260bool AbView::contactCompare( const OContact &cnt, int category ) 365bool AbView::contactCompare( const OContact &cnt, int category )
261{ 366{
262 qWarning ("bool AbView::contactCompare( const OContact &cnt, %d )", category); 367 qWarning ("bool AbView::contactCompare( const OContact &cnt, %d )", category);
263 368
264 bool returnMe; 369 bool returnMe;
265 QArray<int> cats; 370 QArray<int> cats;
266 cats = cnt.categories(); 371 cats = cnt.categories();
267 372
268 qWarning ("Number of categories: %d", cats.count() ); 373 qWarning ("Number of categories: %d", cats.count() );
269 374
270 returnMe = false; 375 returnMe = false;
271 if ( cats.count() == 0 ) 376 if ( cats.count() == 0 && category == 0 )
377 // Contacts with no category will just shown on "All" and "Unfiled"
272 returnMe = true; 378 returnMe = true;
273 else { 379 else {
274 int i; 380 int i;
275 for ( i = 0; i < int(cats.count()); i++ ) { 381 for ( i = 0; i < int(cats.count()); i++ ) {
276 qWarning("Comparing %d with %d",cats[i],category ); 382 qWarning("Comparing %d with %d",cats[i],category );
277 if ( cats[i] == category ) { 383 if ( cats[i] == category ) {
278 returnMe = true; 384 returnMe = true;
279 break; 385 break;
280 } 386 }
281 } 387 }
282 } 388 }
283 qWarning ("Return: %d", returnMe); 389 qWarning ("Return: %d", returnMe);
284 return returnMe; 390 return returnMe;
285} 391}
286 392
393// In Some rare cases we have to update all lists..
394void AbView::updateListinViews()
395{
396 m_abTable -> setContacts( m_list );
397 m_ablabel -> setContacts( m_list );
398}
399
287void AbView::updateView() 400void AbView::updateView()
288{ 401{
289 qWarning("AbView::updateView()"); 402 qWarning("AbView::updateView()");
290 403
291 if ( m_viewStack -> visibleWidget() ){ 404 if ( m_viewStack -> visibleWidget() ){
292 m_viewStack -> visibleWidget() -> clearFocus(); 405 m_viewStack -> visibleWidget() -> clearFocus();
293 } 406 }
294 407
295 // If we switching the view, we have to store some information 408 // If we switching the view, we have to store some information
296 if ( m_prev_View != m_curr_View ){ 409 if ( m_list.count() ){
297 switch ( (int) m_prev_View ) { 410 switch ( (int) m_prev_View ) {
298 case TableView: 411 case TableView:
299 m_curr_Contact = m_abTable -> currentEntry_UID(); 412 m_curr_Contact = m_abTable -> currentEntry_UID();
300 break; 413 break;
301 case CardView: 414 case CardView:
302 m_curr_Contact = m_ablabel -> currentEntry_UID(); 415 m_curr_Contact = m_ablabel -> currentEntry_UID();
303 break; 416 break;
304 } 417 }
305 } 418 emit signalViewSwitched ( (int) m_curr_View );
419 }else
420 m_curr_Contact = 0;
306 421
307 m_prev_View = m_curr_View; 422 m_prev_View = m_curr_View;
308 423
309 // Switch to new View 424 // Switch to new View
310 switch ( (int) m_curr_View ) { 425 switch ( (int) m_curr_View ) {
311 case TableView: 426 case TableView:
427 m_abTable -> setChoiceSelection( m_orderedFields );
312 m_abTable -> setContacts( m_list ); 428 m_abTable -> setContacts( m_list );
313 if ( m_curr_Contact != 0 ) 429 if ( m_curr_Contact != 0 )
314 m_abTable -> selectContact ( m_curr_Contact ); 430 m_abTable -> selectContact ( m_curr_Contact );
315 m_abTable -> setFocus(); 431 m_abTable -> setFocus();
316 emit signalViewSwitched ( (int) m_curr_View );
317 break; 432 break;
318 case CardView: 433 case CardView:
319 m_ablabel -> setContacts( m_list ); 434 m_ablabel -> setContacts( m_list );
320 if ( m_curr_Contact != 0 ) 435 if ( m_curr_Contact != 0 )
321 m_ablabel -> selectContact( m_curr_Contact ); 436 m_ablabel -> selectContact( m_curr_Contact );
322 m_ablabel -> setFocus(); 437 m_ablabel -> setFocus();
323 emit signalViewSwitched ( (int) m_curr_View );
324 break; 438 break;
325 } 439 }
326 440
327 // Raise the current View 441 // Raise the current View
328 m_viewStack -> raiseWidget( m_curr_View ); 442 m_viewStack -> raiseWidget( m_curr_View );
329} 443}
330 444
331 445