summaryrefslogtreecommitdiffabout
path: root/kaddressbook/views/kaddressbooktableview.cpp
blob: 1dc919825ba42b9936c104b12c696a3d1514800d (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// $Id$

#include <qvbox.h>
#include <qlistbox.h>
#include <qwidget.h>
#include <qfile.h>
#include <qimage.h>
#include <qcombobox.h>
#include <qapplication.h>
#include <qdragobject.h>
#include <qevent.h>
#include <qurl.h>
#include <qpixmap.h>

#include <kabc/addressbook.h>
#include <kapplication.h>
#include <kconfig.h>
#include <kcolorbutton.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kurl.h>
#include <kurlrequester.h>

//US#include "configuretableviewdialog.h"
#include "contactlistview.h"
#include "kabprefs.h"
#include "undocmds.h"
#include "viewmanager.h"

#include <qlayout.h>
#include <qheader.h>
#include <qregexp.h>

#include "kaddressbooktableview.h"


KAddressBookTableView::KAddressBookTableView( KABC::AddressBook *ab,
                                              QWidget *parent, const char *name )
  : KAddressBookView( ab, parent, name )
{
  mainLayout = new QVBoxLayout( viewWidget(), 2 );

  // The list view will be created when the config is read.
  mListView = 0;
}

KAddressBookTableView::~KAddressBookTableView()
{
}

void KAddressBookTableView::reconstructListView()
{
    if (mListView)
    {
        disconnect(mListView, SIGNAL(selectionChanged()),
                   this, SLOT(addresseeSelected()));
        disconnect(mListView, SIGNAL(executed(QListViewItem*)),
                   this, SLOT(addresseeExecuted(QListViewItem*)));
        disconnect(mListView, SIGNAL(doubleClicked(QListViewItem*)),
                   this, SLOT(addresseeExecuted(QListViewItem*)));
        disconnect(mListView, SIGNAL(startAddresseeDrag()), this,
                   SIGNAL(startDrag()));
        disconnect(mListView, SIGNAL(returnPressed(QListViewItem*)),
          this, SLOT(addresseeExecuted(QListViewItem*)));

        disconnect(mListView, SIGNAL(addresseeDropped(QDropEvent*)), this,
                   SIGNAL(dropped(QDropEvent*)));
        delete mListView;
    }

  mListView = new ContactListView( this, addressBook(), viewWidget() );

  // Add the columns
  KABC::Field::List fieldList = fields();
  KABC::Field::List::ConstIterator it;

  int c = 0;
  for( it = fieldList.begin(); it != fieldList.end(); ++it ) {
      mListView->addColumn( (*it)->label() );
      mListView->setColumnWidthMode(c++, QListView::Manual);
//US
      //  qDebug("KAddressBookTableView::reconstructListView: field %s", (*it)->label().latin1());
  }

  connect(mListView, SIGNAL(selectionChanged()),
          this, SLOT(addresseeSelected()));
  connect(mListView, SIGNAL(startAddresseeDrag()), this,
          SIGNAL(startDrag()));
  connect(mListView, SIGNAL(addresseeDropped(QDropEvent*)), this,
          SIGNAL(dropped(QDropEvent*)));

  if (KABPrefs::instance()->mHonorSingleClick)
    connect(mListView, SIGNAL(executed(QListViewItem*)),
          this, SLOT(addresseeExecuted(QListViewItem*)));
  else
    connect(mListView, SIGNAL(doubleClicked(QListViewItem*)),
          this, SLOT(addresseeExecuted(QListViewItem*)));
    connect(mListView, SIGNAL(returnPressed(QListViewItem*)),
          this, SLOT(addresseeExecuted(QListViewItem*)));
    connect(mListView, SIGNAL(signalDelete()),
          this, SLOT(addresseeDeleted()));

//US performceimprovement. Refresh is done from the outside
//US refresh();

  mListView->setSorting( 0, true );
  mainLayout->addWidget( mListView );
  mainLayout->activate();
  mListView->show();
}

void KAddressBookTableView::doSearch( const QString&  s, KABC::Field *field  )
{
    mListView->clear();
    if ( s.isEmpty() || s == "*" ) {
        refresh();
        return;
    }  
    QString pattern = s.lower()+"*";
    QRegExp re;
    re.setWildcard(true); // most people understand these better.
    re.setCaseSensitive(false);
    re.setPattern( pattern );
    if (!re.isValid())
        return; 
    KABC::Addressee::List addresseeList = addressees();
    KABC::Addressee::List::Iterator it; 
    if (  field ) {
        for (it = addresseeList.begin(); it != addresseeList.end(); ++it ) {
#if QT_VERSION >= 300
            if (re.search(field->value( *it ).lower()) != -1)
#else
                if (re.match(field->value( *it ).lower()) != -1)
#endif
                    ContactListViewItem *item = new ContactListViewItem(*it, mListView, addressBook(), fields());

        }
    } else {
        KABC::Field::List fieldList = fields();
        KABC::Field::List::ConstIterator fieldIt;
        for (it = addresseeList.begin(); it != addresseeList.end(); ++it ) {
            for ( fieldIt = fieldList.begin(); fieldIt != fieldList.end(); ++fieldIt ) {
#if QT_VERSION >= 300
                if (re.search((*fieldIt)->value( *it ).lower()) != -1)
#else
                    if (re.match((*fieldIt)->value( *it ).lower()) != -1)
#endif   
                        { 
                            ContactListViewItem *item = new ContactListViewItem(*it, mListView, addressBook(), fields());
                            break;
                        }
            }
        }
    }
    // Sometimes the background pixmap gets messed up when we add lots
    // of items.
    mListView->repaint();
    emit selected(QString::null);
   
}
void KAddressBookTableView::writeConfig(KConfig *config)
{
  KAddressBookView::writeConfig(config);

  mListView->saveLayout(config, config->group());
}

void KAddressBookTableView::readConfig(KConfig *config)
{
  KAddressBookView::readConfig( config );
  // The config could have changed the fields, so we need to reconstruct
  // the listview.
  reconstructListView();

  // costum colors?
  if ( config->readBoolEntry( "EnableCustomColors", false ) )
  {
    QPalette p( mListView->palette() );
    QColor c = p.color(QPalette::Normal, QColorGroup::Base );
    p.setColor( QPalette::Normal, QColorGroup::Base, config->readColorEntry( "BackgroundColor", &c ) );
    c = p.color(QPalette::Normal, QColorGroup::Text );
    p.setColor( QPalette::Normal, QColorGroup::Text, config->readColorEntry( "TextColor", &c ) );
    c = p.color(QPalette::Normal, QColorGroup::Button );
    p.setColor( QPalette::Normal, QColorGroup::Button, config->readColorEntry( "HeaderColor", &c ) );
    c = p.color(QPalette::Normal, QColorGroup::ButtonText );
    p.setColor( QPalette::Normal, QColorGroup::ButtonText, config->readColorEntry( "HeaderTextColor", &c ) );
    c = p.color(QPalette::Normal, QColorGroup::Highlight );
    p.setColor( QPalette::Normal, QColorGroup::Highlight, config->readColorEntry( "HighlightColor", &c ) );
    c = p.color(QPalette::Normal, QColorGroup::HighlightedText );
    p.setColor( QPalette::Normal, QColorGroup::HighlightedText, config->readColorEntry( "HighlightedTextColor", &c ) );
#ifndef KAB_EMBEDDED  
  c = KGlobalSettings::alternateBackgroundColor();
#else //KAB_EMBEDDED  
  c = QColor(240, 240, 240);
#endif //KAB_EMBEDDED  
    c = config->readColorEntry ("AlternatingBackgroundColor", &c);
    mListView->setAlternateColor(c);


    //US    mListView->viewport()->setPalette( p );
    mListView->setPalette( p );
  }
  else
  {
    // needed if turned off during a session.
    //US    mListView->viewport()->setPalette( mListView->palette() );
    mListView->setPalette( mListView->palette() );
  }
  
  //custom fonts?
  QFont f( font() );
  if ( config->readBoolEntry( "EnableCustomFonts", false ) )
  {
    mListView->setFont( config->readFontEntry( "TextFont", &f) );
    f.setBold( true );
    //US    mListView->setHeaderFont( config->readFontEntry( "HeaderFont", &f ) );
    mListView->header()->setFont( config->readFontEntry( "HeaderFont", &f ) );
  }
  else
  {
    mListView->setFont( f );
    f.setBold( true );
    //US    mListView->setHeaderFont( f );
    mListView->header()->setFont( f );
  }  





  // Set the list view options
  mListView->setAlternateBackgroundEnabled(config->readBoolEntry("ABackground",
                                                                 true));
  mListView->setSingleLineEnabled(config->readBoolEntry("SingleLine", false));
  mListView->setToolTipsEnabled(config->readBoolEntry("ToolTips", true));

  if (config->readBoolEntry("Background", false))
    mListView->setBackgroundPixmap(config->readEntry("BackgroundName"));

  // Restore the layout of the listview
  mListView->restoreLayout(config, config->group());
}

void KAddressBookTableView::refresh(QString uid)
{
  // For now just repopulate. In reality this method should
  // check the value of uid, and if valid iterate through
  // the listview to find the entry, then tell it to refresh.

  if (uid.isNull()) {
    // Clear the list view
    QString currentUID, nextUID;
#ifndef KAB_EMBEDDED
    ContactListViewItem *currentItem = dynamic_cast<ContactListViewItem*>( mListView->currentItem() );
#else //KAB_EMBEDDED
    ContactListViewItem *currentItem = (ContactListViewItem*)( mListView->currentItem() );
#endif //KAB_EMBEDDED

    if ( currentItem ) {
#ifndef KAB_EMBEDDED
      ContactListViewItem *nextItem = dynamic_cast<ContactListViewItem*>( currentItem->itemBelow() );
#else //KAB_EMBEDDED
      ContactListViewItem *nextItem = (ContactListViewItem*)( currentItem->itemBelow() );
#endif //KAB_EMBEDDED
      if ( nextItem )
        nextUID = nextItem->addressee().uid();
      currentUID = currentItem->addressee().uid();
    }

    mListView->clear();

    currentItem = 0;
    KABC::Addressee::List addresseeList = addressees();
    KABC::Addressee::List::Iterator it;
    for (it = addresseeList.begin(); it != addresseeList.end(); ++it ) {
      ContactListViewItem *item = new ContactListViewItem(*it, mListView, addressBook(), fields());
      if ( (*it).uid() == currentUID )
        currentItem = item;
      else if ( (*it).uid() == nextUID && !currentItem )
        currentItem = item;
    }

    // Sometimes the background pixmap gets messed up when we add lots
    // of items.
    mListView->repaint();

    if ( currentItem ) {
      mListView->setCurrentItem( currentItem );
      mListView->ensureItemVisible( currentItem );
    }
  } else {
    // Only need to update on entry. Iterate through and try to find it
    ContactListViewItem *ceItem;
    QListViewItemIterator it( mListView );
    while ( it.current() ) {
#ifndef KAB_EMBEDDED
      ceItem = dynamic_cast<ContactListViewItem*>( it.current() );
#else //KAB_EMBEDDED
      ceItem = (ContactListViewItem*)( it.current() );
#endif //KAB_EMBEDDED

      if ( ceItem && ceItem->addressee().uid() == uid ) {
        ceItem->refresh();
        return;
      }
      ++it;
    }

    refresh( QString::null );
  }
}

QStringList KAddressBookTableView::selectedUids()
{
    QStringList uidList;
    QListViewItem *item;
    ContactListViewItem *ceItem;

    for(item = mListView->firstChild(); item; item = item->itemBelow())
    {
        if (mListView->isSelected( item ))
        {
#ifndef KAB_EMBEDDED
            ceItem = dynamic_cast<ContactListViewItem*>(item);
#else //KAB_EMBEDDED
            ceItem = (ContactListViewItem*)(item);
#endif //KAB_EMBEDDED

            if (ceItem != 0L)
                uidList << ceItem->addressee().uid();
        }
    }
    if ( uidList.count() == 0 )
        if ( mListView->currentItem() ) {
            ceItem = (ContactListViewItem*)(mListView->currentItem()) ;
            uidList << ceItem->addressee().uid();
        }

    return uidList;
}

void KAddressBookTableView::setSelected(QString uid, bool selected)
{
    QListViewItem *item;
    ContactListViewItem *ceItem;

    if (uid.isNull())
    {
        mListView->selectAll(selected);
    }
    else
    {
        for(item = mListView->firstChild(); item; item = item->itemBelow())
        {
#ifndef KAB_EMBEDDED
            ceItem = dynamic_cast<ContactListViewItem*>(item);
#else //KAB_EMBEDDED
            ceItem = (ContactListViewItem*)(item);
#endif //KAB_EMBEDDED


            if ((ceItem != 0L) && (ceItem->addressee().uid() == uid))
            {
                mListView->setSelected(item, selected);

                if (selected)
                    mListView->ensureItemVisible(item);
            }
        }
    }
}

void KAddressBookTableView::addresseeSelected()
{
    // We need to try to find the first selected item. This might not be the
    // last selected item, but when QListView is in multiselection mode,
    // there is no way to figure out which one was
    // selected last.
    QListViewItem *item;
    bool found =false;
    for (item = mListView->firstChild(); item && !found;
         item = item->nextSibling())
    {
        if (item->isSelected())
        {
            found = true;
#ifndef KAB_EMBEDDED
            ContactListViewItem *ceItem
                 = dynamic_cast<ContactListViewItem*>(item);
#else //KAB_EMBEDDED
            ContactListViewItem *ceItem
                 = (ContactListViewItem*)(item);
#endif //KAB_EMBEDDED

            if ( ceItem ) emit selected(ceItem->addressee().uid());
        }
    }

    if (!found)
        emit selected(QString::null);
}

void KAddressBookTableView::addresseeExecuted(QListViewItem *item)
{
    if (item)
    {
#ifndef KAB_EMBEDDED
        ContactListViewItem *ceItem
               = dynamic_cast<ContactListViewItem*>(item);
#else //KAB_EMBEDDED
            ContactListViewItem *ceItem
                 = (ContactListViewItem*)(item);
#endif //KAB_EMBEDDED

       if (ceItem)
       {
           emit executed(ceItem->addressee().uid());
       }
    }
    else
    {
        emit executed(QString::null);
    }
}

void KAddressBookTableView::addresseeDeleted()
{

         emit deleteRequest();

}





#ifndef KAB_EMBEDDED
#include "kaddressbooktableview.moc"
#endif //KAB_EMBEDDED