summaryrefslogtreecommitdiffstats
path: root/kshowmail/kshowmailview.cpp
blob: d7fb3a59ad7de62982b141bdbd2342498c662774 (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
444
445
446
447
448
449
/***************************************************************************
                          kshowmailview.cpp  -  description
                             -------------------
    begin                : Sat May  6 12:13:57 MEST 2000
    copyright            : (C) 2000-2001 by Eggert Ehmke
    email                : eggert.ehmke@berlin.de

     26 Sep 2002 - Allow for columns to be hidden. Allistar Melville
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

// include files for Qt
#include <qprinter.h>
#include <qpainter.h>
#include <qmessagebox.h>
#include <qkeycode.h>
#include <qvaluelist.h>

#include <kstandarddirs.h>
#include <klocale.h>
#include <kdebug.h>

// application specific includes
#include "kshowmailview.h"
#include "kshowmaildoc.h"
#include "kshowmail.h"

KshowmailView::KshowmailView(QWidget *parent, const char *name):
	QSplitter(Vertical, parent, name)
{
  setBackgroundMode( PaletteBase );
  setFocusPolicy( QWidget::StrongFocus );

  //get application config object (kshowmailrc)
  config = KApplication::kApplication()->config();

  //load active pic for the account list
  m_pixOk = new QPixmap( ::locate( "data", "kshowmail/pics/ok.png" ) );

  //create account list
  m_pListAccounts = new KListView( this, "accounts" );
  m_pListAccounts->setSelectionMode( QListView::Extended );
  m_pListAccounts->setSorting( 1 ); // sort account column
  m_pListAccounts->setShowSortIndicator( true );
  m_pListAccounts->addColumn( i18n( "Active" ), DEFAULT_WIDTH_ACCOUNT_ACTIVE );
  m_pListAccounts->addColumn( i18n( "Account" ), DEFAULT_WIDTH_ACCOUNT_ACCOUNT );
  m_pListAccounts->addColumn( i18n( "Server" ), DEFAULT_WIDTH_ACCOUNT_SERVER );
  m_pListAccounts->addColumn( i18n( "User" ), DEFAULT_WIDTH_ACCOUNT_USER );
  m_pListAccounts->addColumn( i18n( "Messages" ), DEFAULT_WIDTH_ACCOUNT_MESSAGES );
  m_pListAccounts->addColumn( i18n( "Size" ), DEFAULT_WIDTH_ACCOUNT_SIZE );
  m_pListAccounts->setAllColumnsShowFocus( true );
  connect( m_pListAccounts, SIGNAL( rightButtonClicked( QListViewItem*, const QPoint &, int ) ), SLOT( slotAccountContext( QListViewItem*, const QPoint &, int ) ) );
  connect( m_pListAccounts, SIGNAL( clicked( QListViewItem*, const QPoint &, int) ), SLOT( slotAccountClicked( QListViewItem*, const QPoint &, int ) ) );

  //create message list
  m_pListMessages = new KListView( this, "messages" );
  m_pListMessages->setSelectionMode( QListView::Extended );
  m_pListMessages->setAllColumnsShowFocus( true );
  m_pListMessages->setSorting( ShowListViewItem::_colNumber ); // sort number column
  m_pListMessages->setShowSortIndicator( true );
  m_pListMessages->addColumn( i18n( "Number" ), DEFAULT_WIDTH_MESSAGE_NUMBER );
  m_pListMessages->addColumn( i18n( "Account" ), DEFAULT_WIDTH_MESSAGE_ACCOUNT );
  m_pListMessages->addColumn( i18n( "From" ), DEFAULT_WIDTH_MESSAGE_FROM );
  m_pListMessages->addColumn( i18n( "To" ),  DEFAULT_WIDTH_MESSAGE_TO );
  m_pListMessages->addColumn( i18n( "Subject" ), DEFAULT_WIDTH_MESSAGE_SUBJECT );
  m_pListMessages->addColumn( i18n( "Date" ), DEFAULT_WIDTH_MESSAGE_DATE );
  m_pListMessages->addColumn( i18n( "Size" ), DEFAULT_WIDTH_MESSAGE_SIZE );
  m_pListMessages->addColumn( i18n( "Content" ), DEFAULT_WIDTH_MESSAGE_CONTENT );
  m_pListMessages->addColumn( i18n( "State" ),  DEFAULT_WIDTH_MESSAGE_STATE );
  connect( m_pListMessages, SIGNAL( rightButtonClicked( QListViewItem*, const QPoint &, int) ), SLOT( slotMessageContext( QListViewItem*, const QPoint &, int ) ) );
  connect( m_pListMessages, SIGNAL( doubleClicked( QListViewItem* ) ), SLOT( slotMessageDoubleClicked( QListViewItem* ) ) );
  connect( m_pListMessages, SIGNAL( clicked( QListViewItem* ) ), SLOT( slotMessageClicked( QListViewItem* ) ) );

  //read the geometry from the application config file
  readOptions();


}

KshowmailView::~KshowmailView()
{
	delete m_pListAccounts;
	delete m_pListMessages;
}

void KshowmailView::readOptions()
{
  //read splitter geometry
  config->setGroup ("Display Options");
  QValueList<int> size;
  size.append (config->readNumEntry ("sizeaccounts", 40));
  size.append (config->readNumEntry ("sizemessages", 50));
  setSizes (size);

  // read layout of the list views
  m_pListAccounts->restoreLayout( config, CONFIG_GROUP_ACCOUNT_LIST );
  m_pListMessages->restoreLayout( config, CONFIG_GROUP_MESSAGE_LIST );
}

void KshowmailView::saveOptions (KConfig* config)
{
//	kdDebug () << "KshowmailView::saveOptions" << endl;
	config->setGroup("Display Options");
	//splitter
	QValueList<int> size = sizes ();
	config->writeEntry ("sizeaccounts",  size [0]);
	config->writeEntry ("sizemessages",  size [1]);
	// layout
  m_pListAccounts->saveLayout ( config, CONFIG_GROUP_ACCOUNT_LIST );
  m_pListMessages->saveLayout (config, CONFIG_GROUP_MESSAGE_LIST );
}


void KshowmailView::slotMessageContext( QListViewItem* pItem, const QPoint&, int )
{
  if( pItem != NULL )
  {
    //get pointer to the main widget
    KShowMailApp* theApp = ( KShowMailApp * )parentWidget();

    //create and show popup menu
    QWidget* w = theApp->factory()->container( "mail_context_popup", theApp );
    QPopupMenu* popup = static_cast<QPopupMenu*>( w );
    popup->exec( QCursor::pos() );

    //select clicked item
    m_pListMessages->setSelected (pItem, true);
  }
}

void KshowmailView::slotAccountContext( QListViewItem* pItem, const QPoint&, int )
{
  if ( pItem != NULL )
  {
    //get pointer to the main widget
    KShowMailApp* theApp = ( KShowMailApp * )parentWidget();

    //create and show popup menu
    QWidget* w = theApp->factory()->container( "account_context_popup", theApp );
    QPopupMenu* popup = static_cast<QPopupMenu*>( w );
    popup->exec( QCursor::pos() );

    //select clicked item
    m_pListAccounts->setSelected( pItem, true );
  }
}

void KshowmailView::slotAccountClicked( QListViewItem* pItem, const QPoint&, int col )
{

  //get application config object (kshowmailrc)
  KConfig* config = KApplication::kApplication()->config();

  if( col == 0 )
  {
    KShowMailApp *theApp = (KShowMailApp *)parentWidget();
    ConfigElem* pElem = theApp->m_ConfigList.getSelectedAccount();

    if( pElem != NULL )
    {
      //get account name
      QString name = pElem->getAccountName();

      //set config group
      config->setGroup( name );

      //toggle account active state
      pElem->setActive( !pElem->isActive() );

      if( pElem->isActive() )
      {
        //set active picture
        pItem->setPixmap( 0, *m_pixOk );

        //write active state into config file
        config->writeEntry( CONFIG_ENTRY_ACCOUNT_ACTIVE, true );
        config->sync();
      }
      else
      {
        //remove active picture
        pItem->setPixmap( 0, 0 );

        //write active state into config file
        config->writeEntry( CONFIG_ENTRY_ACCOUNT_ACTIVE, false );
        config->sync();
      }

      emit signalActiveChanged ();
    }
  }
}

void KshowmailView::slotMessageDoubleClicked (QListViewItem* pItem)
{
	if (pItem)
	{
//		m_pListMessages->setSelected (pItem, true);

  	KShowMailApp *theApp=(KShowMailApp *) parentWidget();
		theApp->slotShowHeader ();
	}
}

void KshowmailView::slotMessageClicked (QListViewItem* pItem)
{
  if (pItem)
	{
//		m_pListMessages->setSelected (pItem, true);

  	KShowMailApp *theApp=(KShowMailApp *) parentWidget();
		theApp->delayNextRefresh();
	}
}

void KshowmailView::clearMailListView( )
{
  m_pListMessages->clear();
}

ShowListViewItem* KshowmailView::insertMail( QString& number, QString& account, QString& from, QString& to, QString& subject, QString& date, QString& size, QString& content, QString& state, QString& time )
{
  //create item
  ShowListViewItem* item = new ShowListViewItem( m_pListMessages );

  //set content
  item->setNumber( number );
  item->setAccount( account );
  item->setFrom( from );
  item->setTo( to );
  item->setSubject( subject );
  item->setDate( date );
  item->setSize( size );
  item->setContent( content );
  item->setState( state );
  item->setTime( time );

  return item;
}

void KshowmailView::refreshSetup( )
{
  QString ConfigEntryDisplay;     //entry of the config about show or hide column
  QString ConfigEntrySavedWidth;  //entry of the config about saved width
  int DefaultWidth = 100;         //default width of the proceeded column
  bool DefaultDisplay = true;     //default show or hide of the proceeded column

  //set columns of the account list
  config->setGroup( CONFIG_GROUP_ACCOUNT_LIST );
  for( int column = 0; column <= 5; column++ )  //iterate over all columns
  {
    //get config entries and defaults of the proceeded column from the constants header
    switch( column )
    {
      case 0: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_ACCOUNT_ACTIVE;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_ACCOUNT_ACTIVE;
              DefaultWidth = DEFAULT_WIDTH_ACCOUNT_ACTIVE;
              DefaultDisplay = DEFAULT_DISPLAY_ACCOUNT_ACTIVE;
              break;

      case 1: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_ACCOUNT_ACCOUNT;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_ACCOUNT_ACCOUNT;
              DefaultWidth = DEFAULT_WIDTH_ACCOUNT_ACCOUNT;
              DefaultDisplay = DEFAULT_DISPLAY_ACCOUNT_ACCOUNT;
              break;

      case 2: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_ACCOUNT_SERVER;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_ACCOUNT_SERVER;
              DefaultWidth = DEFAULT_WIDTH_ACCOUNT_SERVER;
              DefaultDisplay = DEFAULT_DISPLAY_ACCOUNT_SERVER;
              break;

      case 3: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_ACCOUNT_USER;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_ACCOUNT_USER;
              DefaultWidth = DEFAULT_WIDTH_ACCOUNT_USER;
              DefaultDisplay = DEFAULT_DISPLAY_ACCOUNT_USER;
              break;

      case 4: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_ACCOUNT_MESSAGES;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_ACCOUNT_MESSAGES;
              DefaultWidth = DEFAULT_WIDTH_ACCOUNT_MESSAGES;
              DefaultDisplay = DEFAULT_DISPLAY_ACCOUNT_MESSAGES;
              break;

      case 5: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_ACCOUNT_SIZE;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_ACCOUNT_SIZE;
              DefaultWidth = DEFAULT_WIDTH_ACCOUNT_SIZE;
              DefaultDisplay = DEFAULT_DISPLAY_ACCOUNT_SIZE;
              break;
    } //end select

    //get desired column state
    bool showColumn = config->readBoolEntry( ConfigEntryDisplay, DefaultDisplay );

    //get currently column state
    bool curShowed = m_pListAccounts->columnWidth( column ) > 0;

    if( showColumn )
    {
      //column will be shown

      if( !curShowed )
      {
        //the column is currently not be shown

        //get saved width or take default width
        int savedWidth = config->readNumEntry( ConfigEntrySavedWidth, DefaultWidth );

        //set column width
        m_pListAccounts->setColumnWidth( column, savedWidth );
      }
        //else: the column is currently shown; do nothing
    }
    else
    {
      //column will not be shown

      if( curShowed )
      {
        //the column is currently shown

        //save current width
        config->writeEntry( ConfigEntrySavedWidth, m_pListAccounts->columnWidth( column ) );

        //hide column
        m_pListAccounts->hideColumn( column );
      }
        //else: the column is already hidden; do nothing
    }

  } //end column loop

  //set columns of the message list
  config->setGroup( CONFIG_GROUP_MESSAGE_LIST );
  for( int column = 0; column <= 8; column++ )  //iterate over all columns
  {
    //get config entries and defaults of the proceeded column from the constants header
    switch( column )
    {
      case 0: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_MESSAGE_NUMBER;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_MESSAGE_NUMBER;
              DefaultWidth = DEFAULT_WIDTH_MESSAGE_NUMBER;
              DefaultDisplay = DEFAULT_DISPLAY_MESSAGE_NUMBER;
              break;

      case 1: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_MESSAGE_ACCOUNT;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_MESSAGE_ACCOUNT;
              DefaultWidth = DEFAULT_WIDTH_MESSAGE_ACCOUNT;
              DefaultDisplay = DEFAULT_DISPLAY_MESSAGE_ACCOUNT;
              break;

      case 2: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_MESSAGE_FROM;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_MESSAGE_FROM;
              DefaultWidth = DEFAULT_WIDTH_MESSAGE_FROM;
              DefaultDisplay = DEFAULT_DISPLAY_MESSAGE_FROM;
              break;

      case 3: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_MESSAGE_TO;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_MESSAGE_TO;
              DefaultWidth = DEFAULT_WIDTH_MESSAGE_TO;
              DefaultDisplay = DEFAULT_DISPLAY_MESSAGE_TO;
              break;

      case 4: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_MESSAGE_SUBJECT;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_MESSAGE_SUBJECT;
              DefaultWidth = DEFAULT_WIDTH_MESSAGE_SUBJECT;
              DefaultDisplay = DEFAULT_DISPLAY_MESSAGE_SUBJECT;
              break;

      case 5: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_MESSAGE_DATE;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_MESSAGE_DATE;
              DefaultWidth = DEFAULT_WIDTH_MESSAGE_DATE;
              DefaultDisplay = DEFAULT_DISPLAY_MESSAGE_DATE;
              break;

      case 6: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_MESSAGE_SIZE;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_MESSAGE_SIZE;
              DefaultWidth = DEFAULT_WIDTH_MESSAGE_SIZE;
              DefaultDisplay = DEFAULT_DISPLAY_MESSAGE_SIZE;
              break;

      case 7: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_MESSAGE_CONTENT;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_MESSAGE_CONTENT;
              DefaultWidth = DEFAULT_WIDTH_MESSAGE_CONTENT;
              DefaultDisplay = DEFAULT_DISPLAY_MESSAGE_CONTENT;
              break;

      case 8: ConfigEntryDisplay = CONFIG_ENTRY_DISPLAY_MESSAGE_STATE;
              ConfigEntrySavedWidth = CONFIG_ENTRY_OLD_WIDTH_MESSAGE_STATE;
              DefaultWidth = DEFAULT_WIDTH_MESSAGE_STATE;
              DefaultDisplay = DEFAULT_DISPLAY_MESSAGE_STATE;
              break;

    } //end select

    //get desired column state
    bool showColumn = config->readBoolEntry( ConfigEntryDisplay, DefaultDisplay );

    //get currently column state
    bool curShowed = m_pListMessages->columnWidth( column ) > 0;

    if( showColumn )
    {
      //column will be shown

      if( !curShowed )
      {
        //the column is currently not be shown

        //get saved width or take default width
        int savedWidth = config->readNumEntry( ConfigEntrySavedWidth, DefaultWidth );

        //set column width
        m_pListMessages->setColumnWidth( column, savedWidth );
      }
        //else: the column is currently shown; do nothing
    }
    else
    {
      //column will not be shown

      if( curShowed )
      {
        //the column is currently shown

        //save current width
        config->writeEntry( ConfigEntrySavedWidth, m_pListMessages->columnWidth( column ) );

        //hide column
        m_pListMessages->hideColumn( column );
      }
        //else: the column is already hidden; do nothing
    }

  } //end column loop

  //save current layout
  m_pListAccounts->saveLayout( config, CONFIG_GROUP_ACCOUNT_LIST );
  m_pListMessages->saveLayout( config, CONFIG_GROUP_MESSAGE_LIST );
  config->sync();
}