summaryrefslogtreecommitdiffstats
path: root/kshowmail/kcmconfigs/configaccounts.cpp
blob: 327b8aa8ec5c4abf685f9dd09cb0cf1f6d79dafc (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
//
// C++ Implementation: configaccounts
//
// Description:
//
//
// Author: Ulrich Weigelt <ulrich.weigelt@gmx.de>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "configaccounts.h"

typedef KGenericFactory<ConfigAccounts, TQWidget> ConfigAccountsFactory;

K_EXPORT_COMPONENT_FACTORY( kcm_kshowmailconfigaccounts, ConfigAccountsFactory(
    "kcm_kshowmailconfigaccounts" ) );

ConfigAccounts::ConfigAccounts( TQWidget * parent, const char * name, const TQStringList & args )
  : TDECModule( ConfigAccountsFactory::instance(), parent, args )
{
  //set the module name
  if ( !name )
    setName( "configaccounts" );

  //build GUI
  //---------

  //main layout
  TQHBoxLayout* layMain = new TQHBoxLayout( this, 0, 10 );

  //account list view
  AccountListView = new TDEListView( this, "AccountListView" );
  AccountListView->addColumn( i18n( "Name" ) );
  AccountListView->setColumnWidthMode( 0, TQListView::Maximum );
  AccountListView->setResizeMode( TQListView::LastColumn );

  layMain->addWidget( AccountListView );

  //button layout
  TQVBoxLayout* layButtons = new TQVBoxLayout( layMain );

  //Buttons
  btnAdd = new KPushButton( KStdGuiItem::add(), this, "btnAdd" );
  layButtons->addWidget( btnAdd );
  btnAdd->setSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Maximum );
  connect( btnAdd, SIGNAL( clicked() ), this, SLOT( slotAdd() ) );

  btnEdit = new KPushButton( KStdGuiItem::configure(), this, "btnEdit" );
  layButtons->addWidget( btnEdit );
  btnEdit->setSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Maximum );
  connect( btnEdit, SIGNAL( clicked() ), this, SLOT( slotEdit() ) );

  btnRemove = new KPushButton( KStdGuiItem::remove(), this, "btnRemove" );
  layButtons->addWidget( btnRemove );
  btnRemove->setSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Maximum );
  connect( btnRemove, SIGNAL( clicked() ), this, SLOT( slotRemove() ) );

  layButtons->addItem( new TQSpacerItem( 1, 1, TQSizePolicy::Minimum, TQSizePolicy::Expanding ) );


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

  //load configured values
  load();

}

ConfigAccounts::~ConfigAccounts()
{
}

void ConfigAccounts::load()
{
  //get list of account names
  config->setGroup( CONFIG_GROUP_ACCOUNTS );
  TQStringList accounts = config->readListEntry( CONFIG_ENTRY_ACCOUNTS_LIST, TQStringList() );

  //create list view items and order accounts to load their config
  for( TQStringList::Iterator it = accounts.begin(); it != accounts.end(); ++it )
  {
    //create item
    AccountSetupItem* item = new AccountSetupItem( AccountListView, *it );

    //load item config
    item->load();

  }
}

void ConfigAccounts::save()
{
  config->setGroup( CONFIG_GROUP_ACCOUNTS );

  //get old account list from config file to remove old account entries
  TQStringList oldList = config->readListEntry( CONFIG_ENTRY_ACCOUNTS_LIST, TQStringList() );

  //remove all account entries
  for( TQStringList::Iterator it = oldList.begin(); it != oldList.end(); ++it )
  {
    config->deleteGroup( *it );
  }


  //write a list with all account names into the config
  TQStringList accounts;       //list of all account names
  AccountSetupItem* item = NULL;
  int index = 0;

  do  //get all account names
  {
    item = (AccountSetupItem*)( AccountListView->itemAtIndex( index ) );
    if( item != NULL )
    {
      index++;
      accounts.append( item->getAccountName() );
    }
  } while( item != NULL );

  config->writeEntry( CONFIG_ENTRY_ACCOUNTS_LIST, accounts ); //write list of account names

  //order the items to save their configuration
  index = 0;
  item = NULL;
  do
  {
    item = (AccountSetupItem*)( AccountListView->itemAtIndex( index ) );
    if( item != NULL )
    {
      index++;
      item->save();
    }
  } while( item != NULL );

  //write configuration to disk
  config->sync();
}

void ConfigAccounts::defaults()
{
}

void ConfigAccounts::slotChanged( )
{
  TDECModule::changed();
}

void ConfigAccounts::slotAdd( )
{
  //open setup dialog
  AccountSetupDialog* dlg = new AccountSetupDialog( this, AccountListView, NULL );
  int res = dlg->exec();

  //inform application setup dialog about changes
  if( res == KDialogBase::Accepted )
    slotChanged();

  //delete dialog
  delete dlg;
}

void ConfigAccounts::slotEdit( )
{
  //get selected item
  AccountSetupItem* account = (AccountSetupItem*)( AccountListView->selectedItem() );

  //test item
  if( account == NULL )
    return;

  //open dialog
  AccountSetupDialog* dlg = new AccountSetupDialog( this, AccountListView, account );
  int res = dlg->exec();

  //inform application setup dialog about changes
  if( res == KDialogBase::Accepted )
    slotChanged();

  //delete dialog
  delete dlg;
}

void ConfigAccounts::slotRemove( )
{
  //get selected item
  AccountSetupItem* account = (AccountSetupItem*)( AccountListView->selectedItem() );

  //test item
  if( account == NULL )
    return;

  //remove item
  int result = KMessageBox::questionYesNo( this, i18n( "Do you really want to remove account %1?").arg( account->getAccountName() ) );
  if( result == KMessageBox::Yes )
  {
    delete account;
    slotChanged();
  }
}


#include "configaccounts.moc"