PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/kdepim-4.3.4/kmail/messagelistview/core/configureaggregationsdialog.cpp

#
C++ | 355 lines | 268 code | 68 blank | 19 comment | 33 complexity | 17e5ca0b8b0722fb40c65d1b844651a7 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0
  1. /******************************************************************************
  2. *
  3. * Copyright 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. *
  19. *******************************************************************************/
  20. #include "messagelistview/core/configureaggregationsdialog.h"
  21. #include "messagelistview/core/aggregationeditor.h"
  22. #include "messagelistview/core/aggregation.h"
  23. #include "messagelistview/core/manager.h"
  24. #include <QGridLayout>
  25. #include <QPushButton>
  26. #include <QFrame>
  27. #include <QHash>
  28. #include <KLocale>
  29. #include <KIconLoader>
  30. #include <KMessageBox>
  31. namespace KMail
  32. {
  33. namespace MessageListView
  34. {
  35. namespace Core
  36. {
  37. class AggregationListWidgetItem : public QListWidgetItem
  38. {
  39. private:
  40. Aggregation * mAggregation;
  41. public:
  42. AggregationListWidgetItem( QListWidget * par, const Aggregation &set )
  43. : QListWidgetItem( set.name(), par )
  44. {
  45. mAggregation = new Aggregation( set );
  46. }
  47. ~AggregationListWidgetItem()
  48. {
  49. delete mAggregation;
  50. }
  51. public:
  52. Aggregation * aggregation() const
  53. { return mAggregation; };
  54. void forgetAggregation()
  55. { mAggregation = 0; };
  56. };
  57. ConfigureAggregationsDialog * ConfigureAggregationsDialog::mInstance = 0;
  58. ConfigureAggregationsDialog::ConfigureAggregationsDialog( QWidget *parent )
  59. : KDialog( parent )
  60. {
  61. mInstance = this;
  62. setAttribute( Qt::WA_DeleteOnClose );
  63. setWindowModality( Qt::ApplicationModal ); // FIXME: Sure ?
  64. setButtons( Ok | Cancel );
  65. setWindowTitle( i18n( "Customize Message Aggregation Modes" ) );
  66. QWidget * base = new QWidget( this );
  67. setMainWidget( base );
  68. QGridLayout * g = new QGridLayout( base );
  69. mAggregationList = new AggregationListWidget( base );
  70. mAggregationList->setSortingEnabled( true );
  71. g->addWidget( mAggregationList, 0, 0, 5, 1 );
  72. connect( mAggregationList, SIGNAL( currentItemChanged( QListWidgetItem *, QListWidgetItem * ) ),
  73. SLOT( aggregationListCurrentItemChanged( QListWidgetItem *, QListWidgetItem * ) ) );
  74. mNewAggregationButton = new QPushButton( i18n( "New Aggregation" ), base );
  75. mNewAggregationButton->setIcon( KIcon( "document-new" ) );
  76. mNewAggregationButton->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
  77. g->addWidget( mNewAggregationButton, 0, 1 );
  78. connect( mNewAggregationButton, SIGNAL( clicked() ),
  79. SLOT( newAggregationButtonClicked() ) );
  80. mCloneAggregationButton = new QPushButton( i18n( "Clone Aggregation" ), base );
  81. mCloneAggregationButton->setIcon( KIcon( "edit-copy" ) );
  82. mCloneAggregationButton->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
  83. g->addWidget( mCloneAggregationButton, 1, 1 );
  84. connect( mCloneAggregationButton, SIGNAL( clicked() ),
  85. SLOT( cloneAggregationButtonClicked() ) );
  86. QFrame * f = new QFrame( base );
  87. f->setFrameStyle( QFrame::Sunken | QFrame::HLine );
  88. f->setMinimumHeight( 24 );
  89. g->addWidget( f, 2, 1, Qt::AlignVCenter );
  90. mDeleteAggregationButton = new QPushButton( i18n( "Delete Aggregation" ), base );
  91. mDeleteAggregationButton->setIcon( KIcon( "edit-delete" ) );
  92. mDeleteAggregationButton->setIconSize( QSize( KIconLoader::SizeSmall, KIconLoader::SizeSmall ) );
  93. g->addWidget( mDeleteAggregationButton, 3, 1 );
  94. connect( mDeleteAggregationButton, SIGNAL( clicked() ),
  95. SLOT( deleteAggregationButtonClicked() ) );
  96. mEditor = new AggregationEditor( base );
  97. g->addWidget( mEditor, 5, 0, 1, 2 );
  98. connect( mEditor, SIGNAL( aggregationNameChanged() ),
  99. SLOT( editedAggregationNameChanged() ) );
  100. g->setColumnStretch( 0, 1 );
  101. g->setRowStretch( 4, 1 );
  102. connect( this, SIGNAL( okClicked() ),
  103. SLOT( okButtonClicked() ) );
  104. fillAggregationList();
  105. }
  106. ConfigureAggregationsDialog::~ConfigureAggregationsDialog()
  107. {
  108. mInstance = 0;
  109. }
  110. void ConfigureAggregationsDialog::display( QWidget *parent, const QString &preselectAggregationId )
  111. {
  112. if ( mInstance )
  113. return; // FIXME: reparent if needed ?
  114. mInstance = new ConfigureAggregationsDialog( parent );
  115. mInstance->selectAggregationById( preselectAggregationId );
  116. mInstance->show();
  117. }
  118. void ConfigureAggregationsDialog::cleanup()
  119. {
  120. if ( !mInstance )
  121. return;
  122. delete mInstance;
  123. mInstance = 0;
  124. }
  125. void ConfigureAggregationsDialog::selectAggregationById( const QString &aggregationId )
  126. {
  127. AggregationListWidgetItem * item = findAggregationItemById( aggregationId );
  128. if ( !item )
  129. return;
  130. mAggregationList->setCurrentItem( item );
  131. }
  132. void ConfigureAggregationsDialog::okButtonClicked()
  133. {
  134. commitEditor();
  135. Manager::instance()->removeAllAggregations();
  136. int c = mAggregationList->count();
  137. int i = 0;
  138. while ( i < c )
  139. {
  140. AggregationListWidgetItem * item = dynamic_cast< AggregationListWidgetItem * >( mAggregationList->item( i ) );
  141. if ( item )
  142. {
  143. Manager::instance()->addAggregation( item->aggregation() );
  144. item->forgetAggregation();
  145. }
  146. i++;
  147. }
  148. Manager::instance()->aggregationsConfigurationCompleted();
  149. close(); // this will delete too
  150. }
  151. void ConfigureAggregationsDialog::commitEditor()
  152. {
  153. Aggregation * editedAggregation = mEditor->editedAggregation();
  154. if ( !editedAggregation )
  155. return;
  156. mEditor->commit();
  157. AggregationListWidgetItem * editedItem = findAggregationItemByAggregation( editedAggregation );
  158. if ( editedItem )
  159. return;
  160. QString goodName = uniqueNameForAggregation( editedAggregation->name(), editedAggregation );
  161. editedAggregation->setName( goodName );
  162. editedItem->setText( goodName );
  163. }
  164. void ConfigureAggregationsDialog::editedAggregationNameChanged()
  165. {
  166. Aggregation * set = mEditor->editedAggregation();
  167. if ( !set )
  168. return;
  169. AggregationListWidgetItem * it = findAggregationItemByAggregation( set );
  170. if ( !it )
  171. return;
  172. QString goodName = uniqueNameForAggregation( set->name(), set );
  173. it->setText( goodName );
  174. }
  175. void ConfigureAggregationsDialog::fillAggregationList()
  176. {
  177. const QHash< QString, Aggregation * > & sets = Manager::instance()->aggregations();
  178. for( QHash< QString, Aggregation * >::ConstIterator it = sets.begin(); it != sets.end(); ++it )
  179. (void)new AggregationListWidgetItem( mAggregationList, *( *it ) );
  180. }
  181. void ConfigureAggregationsDialog::aggregationListCurrentItemChanged( QListWidgetItem * cur, QListWidgetItem * )
  182. {
  183. commitEditor();
  184. AggregationListWidgetItem * item = cur ? dynamic_cast< AggregationListWidgetItem * >( cur ) : 0;
  185. mDeleteAggregationButton->setEnabled( item && ( mAggregationList->count() > 1 ) );
  186. mCloneAggregationButton->setEnabled( item );
  187. mEditor->editAggregation( item ? item->aggregation() : 0 );
  188. if ( item && !item->isSelected() )
  189. item->setSelected( true ); // make sure it's true
  190. }
  191. AggregationListWidgetItem * ConfigureAggregationsDialog::findAggregationItemByName( const QString &name, Aggregation * skipAggregation )
  192. {
  193. int c = mAggregationList->count();
  194. int i = 0;
  195. while ( i < c )
  196. {
  197. AggregationListWidgetItem * item = dynamic_cast< AggregationListWidgetItem * >( mAggregationList->item( i ) );
  198. if ( item )
  199. {
  200. if ( item->aggregation() != skipAggregation )
  201. {
  202. if ( item->aggregation()->name() == name )
  203. return item;
  204. }
  205. }
  206. i++;
  207. }
  208. return 0;
  209. }
  210. AggregationListWidgetItem * ConfigureAggregationsDialog::findAggregationItemById( const QString &aggregationId )
  211. {
  212. int c = mAggregationList->count();
  213. int i = 0;
  214. while ( i < c )
  215. {
  216. AggregationListWidgetItem * item = dynamic_cast< AggregationListWidgetItem * >( mAggregationList->item( i ) );
  217. if ( item )
  218. {
  219. if ( item->aggregation()->id() == aggregationId )
  220. return item;
  221. }
  222. i++;
  223. }
  224. return 0;
  225. }
  226. AggregationListWidgetItem * ConfigureAggregationsDialog::findAggregationItemByAggregation( Aggregation * set )
  227. {
  228. int c = mAggregationList->count();
  229. int i = 0;
  230. while ( i < c )
  231. {
  232. AggregationListWidgetItem * item = dynamic_cast< AggregationListWidgetItem * >( mAggregationList->item( i ) );
  233. if ( item )
  234. {
  235. if ( item->aggregation() == set )
  236. return item;
  237. }
  238. i++;
  239. }
  240. return 0;
  241. }
  242. QString ConfigureAggregationsDialog::uniqueNameForAggregation( QString baseName, Aggregation * skipAggregation )
  243. {
  244. QString ret = baseName;
  245. if( ret.isEmpty() )
  246. ret = i18n( "Unnamed Aggregation" );
  247. int idx = 1;
  248. AggregationListWidgetItem * item = findAggregationItemByName( ret, skipAggregation );
  249. while ( item )
  250. {
  251. idx++;
  252. ret = QString("%1 %2").arg( baseName ).arg( idx );
  253. item = findAggregationItemByName( ret, skipAggregation );
  254. }
  255. return ret;
  256. }
  257. void ConfigureAggregationsDialog::newAggregationButtonClicked()
  258. {
  259. Aggregation emptyAggregation;
  260. emptyAggregation.setName( uniqueNameForAggregation( i18n( "New Aggregation" ) ) );
  261. AggregationListWidgetItem * item = new AggregationListWidgetItem( mAggregationList, emptyAggregation );
  262. mAggregationList->setCurrentItem( item );
  263. }
  264. void ConfigureAggregationsDialog::cloneAggregationButtonClicked()
  265. {
  266. AggregationListWidgetItem * item = dynamic_cast< AggregationListWidgetItem * >( mAggregationList->currentItem() );
  267. if ( !item )
  268. return;
  269. Aggregation copyAggregation( *( item->aggregation() ) );
  270. copyAggregation.generateUniqueId(); // regenerate id so it becomes different
  271. copyAggregation.setName( uniqueNameForAggregation( item->aggregation()->name() ) );
  272. item = new AggregationListWidgetItem( mAggregationList, copyAggregation );
  273. mAggregationList->setCurrentItem( item );
  274. }
  275. void ConfigureAggregationsDialog::deleteAggregationButtonClicked()
  276. {
  277. AggregationListWidgetItem * item = dynamic_cast< AggregationListWidgetItem * >( mAggregationList->currentItem() );
  278. if ( !item )
  279. return;
  280. if ( mAggregationList->count() < 2 )
  281. return; // no way: desperately try to keep at least one option set alive :)
  282. mEditor->editAggregation( 0 ); // forget it
  283. delete item; // this will trigger aggregationListCurrentItemChanged()
  284. }
  285. } // namespace Core
  286. } // namespace MessageListView
  287. } // namespace KMail