summaryrefslogtreecommitdiffstats
path: root/kaddressbook/views/kaddressbooktableview.cpp
blob: 6b63330354564452dce4aaa6620eba245d78d617 (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
/*
    This file is part of KAddressBook.
    Copyright (c) 2002 Mike Pilone <[email protected]>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

    As a special exception, permission is given to link this program
    with any edition of TQt, and distribute the resulting executable,
    without including the source code for TQt in the source distribution.
*/

#include <tqapplication.h>
#include <tqlayout.h>
#include <tqheader.h>
#include <tqvbox.h>
#include <tqlistbox.h>
#include <tqwidget.h>
#include <tqfile.h>
#include <tqimage.h>
#include <tqcombobox.h>
#include <tqapplication.h>
#include <tqdragobject.h>
#include <tqevent.h>
#include <tqurl.h>
#include <tqpixmap.h>

#include <tdeabc/addressbook.h>
#include <tdeapplication.h>
#include <tdeconfig.h>
#include <kcolorbutton.h>
#include <kdebug.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kurl.h>
#include <kurlrequester.h>
#include <tdeimproxy.h>

#include "configuretableviewdialog.h"
#include "contactlistview.h"
#include "core.h"
#include "kabprefs.h"
#include "undocmds.h"

#include "kaddressbooktableview.h"

class TableViewFactory : public ViewFactory
{
  public:
    KAddressBookView *view( KAB::Core *core, TQWidget *parent, const char *name )
    {
      return new KAddressBookTableView( core, parent, name );
    }

    TQString type() const { return I18N_NOOP( "Table" ); }

    TQString description() const { return i18n( "A listing of contacts in a table. Each cell of "
                                  "the table holds a field of the contact." ); }

    ViewConfigureWidget *configureWidget( TDEABC::AddressBook *ab, TQWidget *parent,
                                          const char *name = 0 )
    {
      return new ConfigureTableViewWidget( ab, parent, name );
    }
};

extern "C" {
  void *init_libkaddrbk_tableview()
  {
    return ( new TableViewFactory );
  }
}

KAddressBookTableView::KAddressBookTableView( KAB::Core *core,
                                              TQWidget *parent, const char *name )
  : KAddressBookView( core, parent, name )
{
  mMainLayout = new TQVBoxLayout( viewWidget(), 2 );

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

KAddressBookTableView::~KAddressBookTableView()
{
}

void KAddressBookTableView::reconstructListView()
{
  if ( mListView ) {
    disconnect( mListView, TQ_SIGNAL( selectionChanged() ),
                this, TQ_SLOT( addresseeSelected() ) );
    disconnect( mListView, TQ_SIGNAL( executed( TQListViewItem* ) ),
                this, TQ_SLOT( addresseeExecuted( TQListViewItem* ) ) );
    disconnect( mListView, TQ_SIGNAL( doubleClicked( TQListViewItem* ) ),
                this, TQ_SLOT( addresseeExecuted( TQListViewItem* ) ) );
    disconnect( mListView, TQ_SIGNAL( startAddresseeDrag() ),
                this, TQ_SIGNAL( startDrag() ) );
    disconnect( mListView, TQ_SIGNAL( addresseeDropped( TQDropEvent* ) ),
                this, TQ_SIGNAL( dropped( TQDropEvent* ) ) );
    delete mListView;
  }

  mListView = new ContactListView( this, core()->addressBook(), viewWidget() );

  mListView->setShowIM( mIMProxy != 0 );

  // Add the columns
  const TDEABC::Field::List fieldList( fields() );
  TDEABC::Field::List::ConstIterator it;

  int c = 0;
  for ( it = fieldList.begin(); it != fieldList.end(); ++it ) {
    mListView->addColumn( (*it)->label() );
    mListView->setColumnWidthMode( c++, TQListView::Manual );
  }

  if ( mListView->showIM() ) {
    // IM presence is added separately, because it's not a KABC field.
    // If you want to make this appear as the leftmost column by default, move
    // this block immediately before the preceding for loop
    // after the declaration of c.
    mListView->addColumn( i18n( "Presence" ) );
    mListView->setIMColumn( c++ );
  }

  mListView->setFullWidth( true );

  connect( mListView, TQ_SIGNAL( selectionChanged() ),
           this, TQ_SLOT( addresseeSelected() ) );
  connect( mListView, TQ_SIGNAL( startAddresseeDrag() ),
           this, TQ_SIGNAL( startDrag() ) );
  connect( mListView, TQ_SIGNAL( addresseeDropped( TQDropEvent* ) ),
           this, TQ_SIGNAL( dropped( TQDropEvent* ) ) );
  connect( mListView, TQ_SIGNAL( contextMenu( TDEListView*, TQListViewItem*, const TQPoint& ) ),
           this, TQ_SLOT( rmbClicked( TDEListView*, TQListViewItem*, const TQPoint& ) ) );
  connect( mListView->header(), TQ_SIGNAL( clicked( int ) ),
           this, TQ_SIGNAL( sortFieldChanged() ) );

  if ( KABPrefs::instance()->honorSingleClick() )
    connect( mListView, TQ_SIGNAL( executed( TQListViewItem* ) ),
             this, TQ_SLOT( addresseeExecuted( TQListViewItem* ) ) );
  else
    connect( mListView, TQ_SIGNAL( doubleClicked( TQListViewItem* ) ),
             this, TQ_SLOT( addresseeExecuted( TQListViewItem* ) ) );

  refresh();

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

TDEABC::Field *KAddressBookTableView::sortField() const
{
  // we have hardcoded sorting, so we have to return a hardcoded field :(
  return ( mListView->sortColumn() == -1 ? fields()[ 0 ] : fields()[ mListView->sortColumn() ] );
}

void KAddressBookTableView::writeConfig( TDEConfig *config )
{
  KAddressBookView::writeConfig( config );

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

void KAddressBookTableView::readConfig( TDEConfig *config )
{
  KAddressBookView::readConfig( config );

  if ( config->readBoolEntry( "InstantMessagingPresence", false ) ) {
    if ( !mIMProxy ) {
      mIMProxy = KIMProxy::instance( kapp->dcopClient() );
      connect( mIMProxy, TQ_SIGNAL( sigContactPresenceChanged( const TQString& ) ),
               this, TQ_SLOT( updatePresence( const TQString& ) ) );
    }
  } else {
    if ( mIMProxy ) {
      disconnect( mIMProxy, TQ_SIGNAL( sigContactPresenceChanged( const TQString& ) ),
                  this, TQ_SLOT( updatePresence( const TQString& ) ) );
      mIMProxy = 0;
    }
  }

  // The config could have changed the fields, so we need to reconstruct
  // the listview.
  reconstructListView();

  // 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->readPathEntry( "BackgroundName" ) );

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

void KAddressBookTableView::refresh( const TQString &uid )
{
  if ( uid.isEmpty() ) {
    // Clear the list view
    TQString currentUID, nextUID;
    ContactListViewItem *currentItem = dynamic_cast<ContactListViewItem*>( mListView->currentItem() );
    if ( currentItem ) {
      ContactListViewItem *nextItem = dynamic_cast<ContactListViewItem*>( currentItem->itemBelow() );
      if ( nextItem )
        nextUID = nextItem->addressee().uid();
      currentUID = currentItem->addressee().uid();
    }

    mListView->clear();

    currentItem = 0;
    const TDEABC::Addressee::List addresseeList( addressees() );
    TDEABC::Addressee::List::ConstIterator it( addresseeList.begin() );
    const TDEABC::Addressee::List::ConstIterator endIt( addresseeList.end() );
    for ( ; it != endIt; ++it ) {
      ContactListViewItem *item = new ContactListViewItem( *it, mListView,
                                        core()->addressBook(), fields(), mIMProxy );
      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;
    TQPtrList<TQListViewItem> selectedItems( mListView->selectedItems() );
    TQListViewItem *it;
    for ( it = selectedItems.first(); it; it = selectedItems.next() ) {
      ceItem = dynamic_cast<ContactListViewItem*>( it );
      if ( ceItem && ceItem->addressee().uid() == uid ) {
        ceItem->refresh();
        return;
      }
    }
    refresh( TQString() );
  }
}

TQStringList KAddressBookTableView::selectedUids()
{
  TQStringList uidList;
  ContactListViewItem *item;

  TQListViewItemIterator it( mListView, TQListViewItemIterator::Selected );
  while ( it.current() ) {
    item = dynamic_cast<ContactListViewItem*>( it.current() );
    if ( item )
      uidList << item->addressee().uid();

    ++it;
  }

  return uidList;
}

void KAddressBookTableView::setSelected( const TQString &uid, bool selected )
{
  if ( uid.isEmpty() )
    mListView->selectAll( selected );
  else {
    TQListViewItemIterator it( mListView );
    while ( it.current() ) {
      ContactListViewItem *item = dynamic_cast<ContactListViewItem*>( it.current() );
      if ( item && (item->addressee().uid() == uid) ) {
        mListView->setSelected( item, selected );

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

      ++it;
    }
  }
}

void KAddressBookTableView::setFirstSelected( bool selected )
{
  if ( mListView->firstChild() ) {
    mListView->setSelected( mListView->firstChild(), selected );
    mListView->ensureItemVisible( mListView->firstChild() );
  }
}

void KAddressBookTableView::addresseeSelected()
{
  // We need to try to find the first selected item. This might not be the
  // last selected item, but when TQListView is in multiselection mode,
  // there is no way to figure out which one was
  // selected last.
  bool found =false;

  TQListViewItemIterator it( mListView, TQListViewItemIterator::Selected );
  while ( it.current() && !found ) {
    found = true;
    ContactListViewItem *item = dynamic_cast<ContactListViewItem*>( it.current() );
    if ( item )
      emit selected( item->addressee().uid() );

    ++it;
  }

  if ( !found )
      emit selected( TQString() );
}

void KAddressBookTableView::addresseeExecuted( TQListViewItem *item )
{
  if ( item ) {
    ContactListViewItem *ceItem = dynamic_cast<ContactListViewItem*>( item );

    if ( ceItem )
      emit executed( ceItem->addressee().uid() );
    else
      emit executed( TQString() );
  } else {
    emit executed( TQString() );
  }
}

void KAddressBookTableView::rmbClicked( TDEListView*, TQListViewItem*, const TQPoint &point )
{
  popup( point );
}

void KAddressBookTableView::updatePresence( const TQString &uid )
{
  // find the LVI to update and refresh() it
  TQListViewItem *item;
  ContactListViewItem *ceItem;
  for ( item = mListView->firstChild(); item; item = item->itemBelow() ) {
    ceItem = dynamic_cast<ContactListViewItem*>( item );
    if ( ( ceItem != 0L ) && ( ceItem->addressee().uid() == uid ) ) {
      ceItem->setHasIM( true );
      ceItem->refresh();
      break;
    }
  }

  if ( mListView->sortColumn() == mListView->imColumn() )
    mListView->sort();
}

void KAddressBookTableView::scrollUp()
{
  TQApplication::postEvent( mListView, new TQKeyEvent( TQEvent::KeyPress, TQt::Key_Up, 0, 0 ) );
}

void KAddressBookTableView::scrollDown()
{
  TQApplication::postEvent( mListView, new TQKeyEvent( TQEvent::KeyPress, TQt::Key_Down, 0, 0 ) );
}


#include "kaddressbooktableview.moc"