00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "./accountviewmodel.h"
00019
00020 AccountViewModel::AccountViewModel( AccountList* accounts, QObject* parent ) : QAbstractItemModel( parent )
00021 {
00022 this->accounts = accounts;
00023
00024
00025 picActive = KIcon( KStandardDirs::locate( "data", "kshowmail/pics/accountActive.png" ) );
00026 picNotActive = KIcon( KStandardDirs::locate( "data", "kshowmail/pics/accountNotActive.png" ) );
00027
00028
00029 refresh();
00030
00031 }
00032
00033 AccountViewModel::~AccountViewModel(){}
00034
00035 QModelIndex AccountViewModel::index( int row, int column, const QModelIndex& parent ) const
00036 {
00037
00038
00039 if( parent.isValid() ) return QModelIndex();
00040
00041 return createIndex( row, column );
00042 }
00043
00044 QModelIndex AccountViewModel::parent( const QModelIndex& ) const
00045 {
00046 return QModelIndex();
00047 }
00048
00049 int AccountViewModel::rowCount ( const QModelIndex & parent ) const
00050 {
00051
00052 if( parent.isValid() ) return 0;
00053
00054 return viewAccountList.size();
00055 }
00056
00057 int AccountViewModel::columnCount ( const QModelIndex & ) const
00058 {
00059 return NUMBER_ACCOUNTVIEW_COLUMNS;
00060 }
00061
00062 QVariant AccountViewModel::data ( const QModelIndex & index, int role ) const
00063 {
00064
00065 if( !index.isValid() ) return QVariant();
00066
00067 if( index.row() > rowCount() || index.column() > NUMBER_ACCOUNTVIEW_COLUMNS - 1 ) return QVariant();
00068
00069 if( index.row() > viewAccountList.size() - 1 ) return QVariant();
00070
00071
00072 AccountViewItem acc = viewAccountList.at( index.row() );
00073
00074
00075
00076 switch( role )
00077 {
00078 case( Qt::DisplayRole ) :
00079
00080 switch( index.column() )
00081 {
00082 case 0 : return QVariant(); break;
00083 case 1 : return QVariant( acc.getName() ); break;
00084 case 2 : return QVariant( acc.getServer() ); break;
00085 case 3 : return QVariant( acc.getUser() ); break;
00086 case 4 : return QVariant( acc.getNumberMails() ); break;
00087 case 5 : return QVariant( acc.getTotalSizeUnit() ); break;
00088 default : return QVariant(); break;
00089 }
00090 break;
00091
00092 case( Qt::DecorationRole ):
00093
00094 switch( index.column() )
00095 {
00096 case 0 :
00097 if( acc.isActive() )
00098 {
00099 return QVariant( picActive );
00100 }
00101 else
00102 {
00103 return QVariant( picNotActive );
00104 }
00105 break;
00106
00107 default : return QVariant();
00108 }
00109 break;
00110
00111 case( Qt::EditRole ):
00112
00113 switch( index.column() )
00114 {
00115 case 0 :
00116 if( acc.isActive() )
00117 return QVariant( true );
00118 else
00119 return QVariant( false );
00120
00121 break;
00122
00123 default :
00124 return QVariant();
00125 break;
00126 }
00127 break;
00128
00129 }
00130
00131 return QVariant();
00132 }
00133
00134 bool AccountViewModel::hasChildren ( const QModelIndex & parent ) const
00135 {
00136
00137 if( !parent.isValid() ) return true;
00138
00139
00140 return false;
00141 }
00142
00143 Qt::ItemFlags AccountViewModel::flags ( const QModelIndex & index ) const
00144 {
00145 if( !index.isValid() ) return 0;
00146
00147
00148 if( index.column() == 0 )
00149 {
00150 return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00151 }
00152
00153 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00154 }
00155
00156 QVariant AccountViewModel::headerData( int section, Qt::Orientation orientation, int role ) const
00157 {
00158
00159 if( role != Qt::DisplayRole || orientation != Qt::Horizontal )
00160 return QVariant();
00161
00162 switch( section )
00163 {
00164 case 0 : return QVariant( i18nc( "whether the account is active or not", "Active" ) ); break;
00165 case 1 : return QVariant( i18n( "Account" ) ); break;
00166 case 2 : return QVariant( i18n( "Server" ) ); break;
00167 case 3 : return QVariant( i18n( "User" ) ); break;
00168 case 4 : return QVariant( i18n( "Messages" ) ); break;
00169 case 5 : return QVariant( i18n( "Size" ) ); break;
00170 default : return QVariant();
00171 }
00172 }
00173
00174 bool AccountViewModel::setData ( const QModelIndex & index, const QVariant & value, int role )
00175 {
00176 if( role != Qt::EditRole ) return false;
00177 if( !index.isValid() ) return false;
00178 if( index.column() != 0 ) return false;
00179 if( index.row() > rowCount( ) ) return false;
00180
00181 if( index.row() > viewAccountList.size() - 1 ) return false;
00182
00183
00184 AccountViewItem item = viewAccountList.at( index.row() );
00185 QPointer<Account> acc = item.getAccount();
00186
00187 if( acc.isNull() ) return false;
00188
00189
00190 acc->setActive( value.toBool() );
00191
00192
00193 KConfigGroup* configAcc = new KConfigGroup( KGlobal::config(), acc->getName() );
00194 configAcc->writeEntry( CONFIG_ENTRY_ACCOUNT_ACTIVE, value.toBool() );
00195 configAcc->sync();
00196
00197
00198 emit dataChanged( index, index );
00199
00200
00201 return true;
00202
00203
00204 }
00205
00206 void AccountViewModel::refresh()
00207 {
00208 viewAccountList.clear();
00209 viewAccountList.append( accounts->getAllAccountViewItems() );
00210 sort();
00211 reset();
00212 }
00213
00214 void AccountViewModel::sort( int column, Qt::SortOrder order ) {
00215
00216
00217 lastSortOrder = order;
00218 lastSortColumn = column;
00219
00220 if( viewAccountList.empty() ) return;
00221
00222
00223 AccountSort_Type prop;
00224 switch( column ) {
00225
00226 case 0 : prop = AccSortActive; break;
00227 case 1 : prop = AccSortName; break;
00228 case 2 : prop = AccSortServer; break;
00229 case 3 : prop = AccSortUser; break;
00230 case 4 : prop = AccSortNrMess; break;
00231 case 5 : prop = AccSortSize; break;
00232 default : prop = AccSortName;
00233 }
00234
00235
00236 QList<AccountViewItem> sortedList;
00237
00238 QListIterator<AccountViewItem > itUnsort( viewAccountList );
00239 while( itUnsort.hasNext() ) {
00240
00241
00242 AccountViewItem accUnsorted = itUnsort.next();
00243
00244
00245 if( sortedList.size() == 0 ) {
00246
00247 sortedList.append( accUnsorted );
00248
00249 } else {
00250
00251 int sizeSortedList = sortedList.size();
00252 int indexSort = 0;
00253 bool placed = false;
00254 while( indexSort < sizeSortedList && !placed ) {
00255
00256
00257 AccountViewItem accSorted = sortedList.at( indexSort );
00258
00259
00260
00261 if( order == Qt::AscendingOrder ) {
00262
00263 if( accUnsorted.compare( accSorted, prop ) <= 0 ) {
00264
00265 sortedList.insert( indexSort, accUnsorted );
00266 placed = true;
00267 }
00268
00269 } else {
00270
00271 if( accUnsorted.compare( accSorted, prop ) > 0 ) {
00272
00273 sortedList.insert( indexSort, accUnsorted );
00274 placed = true;
00275 }
00276 }
00277
00278 indexSort++;
00279 }
00280
00281
00282 if( !placed )
00283 sortedList.append( accUnsorted );
00284 }
00285 }
00286
00287
00288 viewAccountList.clear();
00289 viewAccountList.append( sortedList );
00290
00291 reset();
00292 }
00293
00294 void AccountViewModel::sort()
00295 {
00296 sort( lastSortColumn, lastSortOrder );
00297 }
00298
00299 void AccountViewModel::saveSetup()
00300 {
00301 KConfigGroup* conf = new KConfigGroup( KGlobal::config(), CONFIG_GROUP_VIEW );
00302
00303 conf->writeEntry( CONFIG_ENTRY_SORT_COLUMN_ACCOUNT, lastSortColumn );
00304
00305 if( lastSortOrder == Qt::AscendingOrder ) {
00306
00307 conf->writeEntry( CONFIG_ENTRY_SORT_ORDER_ACCOUNT, CONFIG_VALUE_SORT_ORDER_ASCENDING );
00308
00309 } else {
00310
00311 conf->writeEntry( CONFIG_ENTRY_SORT_ORDER_ACCOUNT, CONFIG_VALUE_SORT_ORDER_DESCENDING );
00312 }
00313
00314 conf->sync();
00315 }
00316
00317 Account* AccountViewModel::getAccount(const QModelIndex index) const
00318 {
00319 AccountViewItem item = viewAccountList.at( index.row() );
00320 return item.getAccount();
00321 }